Skip to content

The only downloader you need. 下载器的集大成者。

refactor(Setting): 搜索过滤收进设置组

XiaoYouChRdf36b8d03eab
diff --git a/app/view/components/setting_card_group.py b/app/view/components/setting_card_group.py
index fc99196a..c49cff92 100644
--- a/app/view/components/setting_card_group.py
+++ b/app/view/components/setting_card_group.py
@@ -105,6 +105,12 @@ class CollapsibleSettingCard(QWidget):
+def isSearchMatch(card: SettingCard | CollapsibleSettingCard, text: str) -> bool:
+ if isinstance(card, CollapsibleSettingCard):
+ card = card.card
+ return text in card.titleLabel.text().lower() or text in card.contentLabel.text().lower()
+
+
class CollapsibleSettingCardGroup(CardWidget):
orderChanged = Signal()
@@ -192,6 +198,20 @@ class CollapsibleSettingCardGroup(CardWidget):
for card in cards:
self.addSettingCard(card)
+ def setSearchText(self, text: str) -> None:
+ text = text.strip().lower()
+ hasMatch = False
+ for i in range(self.cardLayout.count()):
+ card = self.cardLayout.itemAt(i).widget()
+ if card is None:
+ continue
+ isMatch = not text or isSearchMatch(card, text)
+ card.setVisible(isMatch)
+ hasMatch = hasMatch or isMatch
+ self.setVisible(hasMatch or not text)
+ isExpand = (bool(text) and hasMatch) or self.objectName() in cfg.expandedSettingGroups.value
+ self._setCollapsed(not isExpand)
+
def mousePressEvent(self, event) -> None:
if event.button() == Qt.MouseButton.LeftButton and event.position().y() < self.cardContainer.geometry().top():
self._onExpandClicked()
@@ -227,6 +247,8 @@ class CollapsibleSettingCardGroup(CardWidget):
cfg.set(cfg.expandedSettingGroups, items)
def _setCollapsed(self, collapsed: bool) -> None:
+ if collapsed == self._collapsed:
+ return
self._collapsed = collapsed
self.expandButton.setExpand(not collapsed)
self._collapseAnim.stop()
diff --git a/app/view/pages/setting_page.py b/app/view/pages/setting_page.py
index 70c4e84b..f2bce6f7 100644
--- a/app/view/pages/setting_page.py
+++ b/app/view/pages/setting_page.py
@@ -23,9 +23,7 @@ from app.config.constants import (
FEEDBACK_URL, FIREFOX_ADDONS_URL, VERSION, YEAR,
)
from app.view.components.category_settings import CategoryRulesCard
-from app.view.components.setting_card_group import (
- CollapsibleSettingCard, CollapsibleSettingCardGroup, QWIDGETSIZE_MAX,
-)
+from app.view.components.setting_card_group import CollapsibleSettingCardGroup
from app.view.components.setting_cards import (
HeadersPresetSettingCard, IdentitySettingCard, LineEditSettingCard,
PercentSpinBoxSettingCard, ProxySettingCard, SpinBoxSettingCard,
@@ -562,60 +560,19 @@ class SettingPage(ScrollArea):
return self.tr("搜索设置")
def setSearchText(self, text: str) -> None:
- text = text.strip().lower()
- if not text:
- self._clearSearchFilter()
- return
-
hasMatch = False
for i in range(self.vBoxLayout.count()):
group = self.vBoxLayout.itemAt(i).widget()
if not isinstance(group, CollapsibleSettingCardGroup):
continue
- groupHasMatch = False
- for j in range(group.cardLayout.count()):
- card = group.cardLayout.itemAt(j).widget()
- if card is None:
- continue
- if self._isSearchMatch(card, text):
- card.show()
- groupHasMatch = True
- else:
- card.hide()
- if groupHasMatch:
- group.show()
- group.cardContainer.setMaximumHeight(QWIDGETSIZE_MAX)
- hasMatch = True
- else:
- group.hide()
+ group.setSearchText(text)
+ hasMatch = hasMatch or not group.isHidden()
self.emptyStatusWidget.setVisible(not hasMatch)
if not hasMatch:
self.emptyStatusWidget.adjustSize()
self._refreshEmptyWidgetGeometry()
- def _isSearchMatch(self, widget, text: str) -> bool:
- if isinstance(widget, CollapsibleSettingCard):
- widget = widget.card
- title = widget.titleLabel.text().lower()
- content = widget.contentLabel.text().lower()
- return text in title or text in content
-
- def _clearSearchFilter(self) -> None:
- for i in range(self.vBoxLayout.count()):
- group = self.vBoxLayout.itemAt(i).widget()
- if not isinstance(group, CollapsibleSettingCardGroup):
- continue
- group.show()
- for j in range(group.cardLayout.count()):
- card = group.cardLayout.itemAt(j).widget()
- if card is not None:
- card.show()
- group.cardContainer.setMaximumHeight(
- 0 if group._collapsed else QWIDGETSIZE_MAX
- )
- self.emptyStatusWidget.hide()
-
def _refreshEmptyWidgetGeometry(self) -> None:
self.emptyStatusWidget.move(
(self.width() - self.emptyStatusWidget.width()) // 2,