file search let's go!

This commit is contained in:
N0\A
2025-10-22 11:13:21 +02:00
parent e99084bc7f
commit 2cd74fdcb2

59
main.py
View File

@@ -1,10 +1,36 @@
#!/usr/bin/python3 #!/usr/bin/python3
import sys import sys, os
from pathlib import Path from pathlib import Path
from PySide6 import QtCore, QtGui, QtWidgets from PySide6 import QtCore, QtGui, QtWidgets
from core.file_search import find
ASSET = Path(__file__).parent / "assets" / "2ktan.png" ASSET = Path(__file__).parent / "assets" / "2ktan.png"
class SearchResultsDialog(QtWidgets.QDialog):
def __init__(self, results, parent=None):
super().__init__(parent)
self.setWindowTitle("Search Results")
self.setMinimumSize(600, 400)
# Create a list widget to display the results
self.list_widget = QtWidgets.QListWidget()
self.list_widget.addItems(results)
self.list_widget.itemDoubleClicked.connect(self.open_file_location)
# Set up the layout
layout = QtWidgets.QVBoxLayout()
layout.addWidget(self.list_widget)
self.setLayout(layout)
def open_file_location(self, item: QtWidgets.QListWidgetItem):
"""Opens the directory containing the selected file."""
file_path = item.text()
if os.path.exists(file_path):
directory = os.path.dirname(file_path)
url = QtCore.QUrl.fromLocalFile(directory)
QtGui.QDesktopServices.openUrl(url)
class MainWindow(QtWidgets.QMainWindow): class MainWindow(QtWidgets.QMainWindow):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
@@ -13,6 +39,7 @@ class MainWindow(QtWidgets.QMainWindow):
QtCore.Qt.FramelessWindowHint #type: ignore QtCore.Qt.FramelessWindowHint #type: ignore
| QtCore.Qt.WindowStaysOnTopHint #type: ignore | QtCore.Qt.WindowStaysOnTopHint #type: ignore
| QtCore.Qt.Tool #type: ignore | QtCore.Qt.Tool #type: ignore
| QtCore.Qt.WindowDoesNotAcceptFocus #type: ignore
) )
self.setWindowFlags(flags) self.setWindowFlags(flags)
@@ -31,7 +58,10 @@ class MainWindow(QtWidgets.QMainWindow):
self.tray = QtWidgets.QSystemTrayIcon(self) self.tray = QtWidgets.QSystemTrayIcon(self)
self.tray.setIcon(QtGui.QIcon(str(ASSET))) self.tray.setIcon(QtGui.QIcon(str(ASSET)))
# MENU
menu = QtWidgets.QMenu() menu = QtWidgets.QMenu()
menu.addAction("Search Files", self.start_file_search)
menu.addAction("Hide/Show", self.toggle_visible) menu.addAction("Hide/Show", self.toggle_visible)
menu.addSeparator() menu.addSeparator()
menu.addAction("Quit", QtWidgets.QApplication.quit) menu.addAction("Quit", QtWidgets.QApplication.quit)
@@ -48,12 +78,15 @@ class MainWindow(QtWidgets.QMainWindow):
def ensure_on_top(self): def ensure_on_top(self):
if self.isVisible(): if self.isVisible():
self.raise_() self.raise_()
self.activateWindow()
def showEvent(self, event): def showEvent(self, event):
super().showEvent(event) super().showEvent(event)
self.raise_() self.raise_()
self.activateWindow()
def mouseDoubleClickEvent(self, event: QtGui.QMouseEvent):
if event.button() == QtCore.Qt.LeftButton: #type: ignore
self.start_file_search()
event.accept()
def mousePressEvent(self, event: QtGui.QMouseEvent): def mousePressEvent(self, event: QtGui.QMouseEvent):
if event.button() == QtCore.Qt.LeftButton: #type: ignore if event.button() == QtCore.Qt.LeftButton: #type: ignore
@@ -73,6 +106,26 @@ class MainWindow(QtWidgets.QMainWindow):
def toggle_visible(self): def toggle_visible(self):
self.setVisible(not self.isVisible()) self.setVisible(not self.isVisible())
def start_file_search(self):
pattern, ok = QtWidgets.QInputDialog.getText(self, "File Search", "Enter search pattern:")
if ok and pattern:
try:
QtWidgets.QApplication.setOverrideCursor(QtCore.Qt.WaitCursor) #type: ignore
results = find(pattern, root='~')
except RuntimeError as e:
QtWidgets.QMessageBox.critical(self, "Search Error", str(e))
return
finally:
QtWidgets.QApplication.restoreOverrideCursor()
if results:
self.results_dialog = SearchResultsDialog(results, self)
self.results_dialog.show()
else:
QtWidgets.QMessageBox.information(self, "No Results", f"No files found matching '{pattern}'.")
def main(): def main():
app = QtWidgets.QApplication(sys.argv) app = QtWidgets.QApplication(sys.argv)
app.setApplicationName("CLARA") app.setApplicationName("CLARA")