Add find bar for searching for text in the editor

This commit is contained in:
Miguel Jacq 2025-11-07 17:03:14 +11:00
parent cf594487bc
commit 5489854d58
Signed by: mig5
GPG key ID: 59B3F0C24135C6A9
5 changed files with 320 additions and 1 deletions

186
bouquin/find_bar.py Normal file
View file

@ -0,0 +1,186 @@
from __future__ import annotations
from PySide6.QtCore import Qt, Signal
from PySide6.QtGui import (
QShortcut,
QTextCursor,
QTextCharFormat,
QTextDocument,
)
from PySide6.QtWidgets import (
QWidget,
QHBoxLayout,
QLineEdit,
QLabel,
QPushButton,
QCheckBox,
QTextEdit,
)
class FindBar(QWidget):
"""Widget for finding text in the Editor"""
closed = (
Signal()
) # emitted when the bar is hidden (Esc/✕), so caller can refocus editor
def __init__(
self,
editor: QTextEdit,
shortcut_parent: QWidget | None = None,
parent: QWidget | None = None,
):
super().__init__(parent)
self.editor = editor
# UI
layout = QHBoxLayout(self)
layout.setContentsMargins(6, 0, 6, 0)
layout.addWidget(QLabel("Find:"))
self.edit = QLineEdit(self)
self.edit.setPlaceholderText("Type to search…")
layout.addWidget(self.edit)
self.case = QCheckBox("Match case", self)
layout.addWidget(self.case)
self.prevBtn = QPushButton("Prev", self)
self.nextBtn = QPushButton("Next", self)
self.closeBtn = QPushButton("", self)
self.closeBtn.setFlat(True)
layout.addWidget(self.prevBtn)
layout.addWidget(self.nextBtn)
layout.addWidget(self.closeBtn)
self.setVisible(False)
# Shortcut escape key to close findBar
sp = shortcut_parent if shortcut_parent is not None else (parent or self)
self._scEsc = QShortcut(Qt.Key_Escape, sp, activated=self._maybe_hide)
# Signals
self.edit.returnPressed.connect(self.find_next)
self.edit.textChanged.connect(self._update_highlight)
self.case.toggled.connect(self._update_highlight)
self.nextBtn.clicked.connect(self.find_next)
self.prevBtn.clicked.connect(self.find_prev)
self.closeBtn.clicked.connect(self.hide_bar)
# ----- Public API -----
def show_bar(self):
"""Show the bar, seed with current selection if sensible, focus the line edit."""
tc = self.editor.textCursor()
sel = tc.selectedText().strip()
if sel and "\u2029" not in sel: # ignore multi-paragraph selections
self.edit.setText(sel)
self.setVisible(True)
self.edit.setFocus(Qt.ShortcutFocusReason)
self.edit.selectAll()
self._update_highlight()
def hide_bar(self):
self.setVisible(False)
self._clear_highlight()
self.closed.emit()
def refresh(self):
"""Recompute highlights"""
self._update_highlight()
# ----- Internals -----
def _maybe_hide(self):
if self.isVisible():
self.hide_bar()
def _flags(self, backward: bool = False) -> QTextDocument.FindFlags:
flags = QTextDocument.FindFlags()
if backward:
flags |= QTextDocument.FindBackward
if self.case.isChecked():
flags |= QTextDocument.FindCaseSensitively
return flags
def find_next(self):
txt = self.edit.text()
if not txt:
return
# If current selection == query, bump caret to the end so we don't re-match it.
c = self.editor.textCursor()
if c.hasSelection():
sel = c.selectedText()
same = (
(sel == txt)
if self.case.isChecked()
else (sel.casefold() == txt.casefold())
)
if same:
end = max(c.position(), c.anchor())
c.setPosition(end, QTextCursor.MoveAnchor)
self.editor.setTextCursor(c)
if not self.editor.find(txt, self._flags(False)):
cur = self.editor.textCursor()
cur.movePosition(QTextCursor.Start)
self.editor.setTextCursor(cur)
self.editor.find(txt, self._flags(False))
self.editor.ensureCursorVisible()
self._update_highlight()
def find_prev(self):
txt = self.edit.text()
if not txt:
return
# If current selection == query, bump caret to the start so we don't re-match it.
c = self.editor.textCursor()
if c.hasSelection():
sel = c.selectedText()
same = (
(sel == txt)
if self.case.isChecked()
else (sel.casefold() == txt.casefold())
)
if same:
start = min(c.position(), c.anchor())
c.setPosition(start, QTextCursor.MoveAnchor)
self.editor.setTextCursor(c)
if not self.editor.find(txt, self._flags(True)):
cur = self.editor.textCursor()
cur.movePosition(QTextCursor.End)
self.editor.setTextCursor(cur)
self.editor.find(txt, self._flags(True))
self.editor.ensureCursorVisible()
self._update_highlight()
def _update_highlight(self):
txt = self.edit.text()
if not txt:
self._clear_highlight()
return
doc = self.editor.document()
flags = self._flags(False)
cur = QTextCursor(doc)
cur.movePosition(QTextCursor.Start)
fmt = QTextCharFormat()
hl = self.palette().highlight().color()
hl.setAlpha(90)
fmt.setBackground(hl)
selections = []
while True:
cur = doc.find(txt, cur, flags)
if cur.isNull():
break
sel = QTextEdit.ExtraSelection()
sel.cursor = cur
sel.format = fmt
selections.append(sel)
self.editor.setExtraSelections(selections)
def _clear_highlight(self):
self.editor.setExtraSelections([])

View file

@ -24,6 +24,7 @@ from PySide6.QtGui import (
QDesktopServices,
QFont,
QGuiApplication,
QKeySequence,
QPalette,
QTextCharFormat,
QTextCursor,
@ -44,6 +45,7 @@ from PySide6.QtWidgets import (
from .db import DBManager
from .editor import Editor
from .find_bar import FindBar
from .history_dialog import HistoryDialog
from .key_prompt import KeyPrompt
from .lock_overlay import LockOverlay
@ -159,6 +161,11 @@ class MainWindow(QMainWindow):
# Status bar for feedback
self.statusBar().showMessage("Ready", 800)
# Add findBar and add it to the statusBar
self.findBar = FindBar(self.editor, shortcut_parent=self, parent=self)
self.statusBar().addPermanentWidget(self.findBar)
# When the findBar closes, put the caret back in the editor
self.findBar.closed.connect(self._focus_editor_now)
# Menu bar (File)
mb = self.menuBar()
@ -213,6 +220,24 @@ class MainWindow(QMainWindow):
nav_menu.addAction(act_today)
self.addAction(act_today)
act_find = QAction("Find on page", self)
act_find.setShortcut(QKeySequence.Find)
act_find.triggered.connect(self.findBar.show_bar)
nav_menu.addAction(act_find)
self.addAction(act_find)
act_find_next = QAction("Find Next", self)
act_find_next.setShortcut(QKeySequence.FindNext)
act_find_next.triggered.connect(self.findBar.find_next)
nav_menu.addAction(act_find_next)
self.addAction(act_find_next)
act_find_prev = QAction("Find Previous", self)
act_find_prev.setShortcut(QKeySequence.FindPrevious)
act_find_prev.triggered.connect(self.findBar.find_prev)
nav_menu.addAction(act_find_prev)
self.addAction(act_find_prev)
# Help menu with drop-down
help_menu = mb.addMenu("&Help")
act_docs = QAction("Documentation", self)
@ -977,3 +1002,7 @@ If you want an encrypted backup, choose Backup instead of Export.
)
cur.setPosition(old_pos, mode)
ed.setTextCursor(cur)
# Refresh highlights if the theme changed
if hasattr(self, "findBar"):
self.findBar.refresh()