From df9409c74bc65f7df2d40ddfff939daf04bc734b Mon Sep 17 00:00:00 2001 From: cosven Date: Sat, 30 Nov 2024 15:39:56 +0800 Subject: [PATCH 1/3] [bugfix](gui): improve search view UI on KDE --- feeluown/gui/components/menu.py | 11 +++++++++-- feeluown/gui/components/search.py | 31 +++++++++++++++++++------------ feeluown/gui/helpers.py | 14 +++++++++++++- 3 files changed, 41 insertions(+), 15 deletions(-) diff --git a/feeluown/gui/components/menu.py b/feeluown/gui/components/menu.py index 845514502..4ce972abd 100644 --- a/feeluown/gui/components/menu.py +++ b/feeluown/gui/components/menu.py @@ -83,7 +83,15 @@ def keyPressEvent(self, event): super().keyPressEvent(event) q = f'{song.title} {song.artists_name}' - view = SearchResultViewWithEsc(self._app, parent=self._app) + view = SearchResultViewWithEsc(self._app, transparent_bg=False, parent=self._app) + + view.setStyleSheet(''' + SearchResultView { + border: 1px solid gray; + border-radius: 2px; + } + ''') + source_in = self._app.browser.local_storage.get(KeySourceIn, None) run_afn(view.search_and_render, q, SearchType.so, source_in) @@ -96,7 +104,6 @@ def keyPressEvent(self, event): view.resize(width, height) pos = self._app.mapToGlobal(QPoint(0, 0)) view.move(pos.x() + x, pos.y() + y) - view.setWindowFlags(Qt.Popup) view.show() view.raise_() diff --git a/feeluown/gui/components/search.py b/feeluown/gui/components/search.py index 68c85ca86..d186efe19 100644 --- a/feeluown/gui/components/search.py +++ b/feeluown/gui/components/search.py @@ -1,14 +1,14 @@ from datetime import datetime -from PyQt5.QtWidgets import QAbstractItemView, QFrame, QVBoxLayout +from PyQt5.QtWidgets import QAbstractItemView, QFrame, QVBoxLayout, QScrollArea from feeluown.library import SearchType from feeluown.gui.page_containers.table import TableContainer, Renderer -from feeluown.gui.page_containers.scroll_area import ScrollArea from feeluown.gui.widgets.img_card_list import ImgCardListDelegate from feeluown.gui.widgets.songs import SongsTableView, ColumnsMode from feeluown.gui.base_renderer import TabBarRendererMixin -from feeluown.gui.helpers import BgTransparentMixin +from feeluown.gui.helpers import BgTransparentMixin, unify_scroll_area_style, \ + set_widget_bg_transparent from feeluown.gui.widgets.magicbox import KeySourceIn, KeyType from feeluown.gui.widgets.header import LargeHeader, MidHeader from feeluown.gui.widgets.accordion import Accordion @@ -30,9 +30,20 @@ def get_tab_idx(search_type): raise ValueError("unknown search type") -class SearchResultView(ScrollArea): - def __init__(self, app, *args, **kwargs): - super().__init__(*args, **kwargs) +class SearchResultView(QScrollArea): + """ + Usage: + view = SearchResultView(app) + await view.search_and_render(q, search_type, source_in) + """ + def __init__(self, app, transparent_bg=True, parent=None): + super().__init__(parent=parent) + + self.setWidgetResizable(True) + self.setFrameShape(QFrame.NoFrame) + unify_scroll_area_style(self) + if transparent_bg: + set_widget_bg_transparent(self) self.body = Body(app) self.setWidget(self.body) @@ -41,15 +52,11 @@ def fillable_bg_height(self): """Implement VFillableBg protocol""" return self.body.height() - self.body.accordion.height() - async def search_and_render(self, *args, **kwargs): - await self.body.search_and_render(*args, **kwargs) + async def search_and_render(self, q, search_type, source_in): + await self.body.search_and_render(q, search_type, source_in) class Body(QFrame, BgTransparentMixin): - """ - view = SearchResultView(app, q) - await view.render() - """ def __init__(self, app, **kwargs): super().__init__(**kwargs) diff --git a/feeluown/gui/helpers.py b/feeluown/gui/helpers.py index 0cc7dd49a..0f0c14438 100644 --- a/feeluown/gui/helpers.py +++ b/feeluown/gui/helpers.py @@ -29,7 +29,8 @@ QAbstractListModel, QPoint from PyQt5.QtGui import QPalette, QFontMetrics, QColor, QPainter, QMouseEvent, \ QKeySequence -from PyQt5.QtWidgets import QApplication, QScrollArea, QWidget, QShortcut +from PyQt5.QtWidgets import QApplication, QScrollArea, QWidget, QShortcut, \ + QAbstractScrollArea from feeluown.utils.aio import run_afn, run_fn from feeluown.utils.reader import AsyncReader, Reader @@ -115,6 +116,17 @@ def palette_set_bg_color(palette, color): palette.setColor(QPalette.Inactive, QPalette.Window, color) +def unify_scroll_area_style(scroll_area: QAbstractScrollArea): + if not IS_MACOS: + scroll_area.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) + + +def set_widget_bg_transparent(widget: QWidget): + palette = widget.palette() + palette_set_bg_color(palette, Qt.transparent) + widget.setPalette(palette) + + class BgTransparentMixin: def __init__(self: QWidget, *args, **kwargs): # type: ignore[misc] palette = self.palette() From 5b58900d8fa34973f79c571fab6d15dcb9740b82 Mon Sep 17 00:00:00 2001 From: cosven Date: Sat, 30 Nov 2024 16:08:57 +0800 Subject: [PATCH 2/3] clean code --- feeluown/gui/components/menu.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/feeluown/gui/components/menu.py b/feeluown/gui/components/menu.py index 4ce972abd..db7e26b44 100644 --- a/feeluown/gui/components/menu.py +++ b/feeluown/gui/components/menu.py @@ -1,7 +1,7 @@ import logging from typing import Optional, TYPE_CHECKING -from PyQt5.QtCore import Qt, QPoint +from PyQt5.QtCore import Qt from feeluown.excs import ProviderIOError from feeluown.utils.aio import run_fn, run_afn @@ -75,6 +75,7 @@ async def goto_song_album(song): def show_similar_resource(self, song): from feeluown.gui.components.search import SearchResultView + # TODO: add a close button, and rename it to ClosableSearchResultView class SearchResultViewWithEsc(SearchResultView): def keyPressEvent(self, event): if event.key() == Qt.Key_Escape: @@ -100,10 +101,7 @@ def keyPressEvent(self, event): x = self._app.ui.sidebar.width() y = self._app.height() - height - self._app.ui.player_bar.height() - # Set the size using resize() and position using move() - view.resize(width, height) - pos = self._app.mapToGlobal(QPoint(0, 0)) - view.move(pos.x() + x, pos.y() + y) + view.setGeometry(x, y, width, height) view.show() view.raise_() From 3ba1b782a186d59728c9ea40a2c3eee3a9233c35 Mon Sep 17 00:00:00 2001 From: cosven Date: Sat, 30 Nov 2024 16:12:01 +0800 Subject: [PATCH 3/3] add some comments --- feeluown/gui/components/menu.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/feeluown/gui/components/menu.py b/feeluown/gui/components/menu.py index db7e26b44..cbe6f9e96 100644 --- a/feeluown/gui/components/menu.py +++ b/feeluown/gui/components/menu.py @@ -101,6 +101,9 @@ def keyPressEvent(self, event): x = self._app.ui.sidebar.width() y = self._app.height() - height - self._app.ui.player_bar.height() + # Do not use Qt.Popup, because it behaves differently on different + # platforms. For example, on macOS, it will pop up a window with a + # shadow, while on Linux(KDE) it won't. view.setGeometry(x, y, width, height) view.show() view.raise_()