Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitad0ee6b

Browse files
committed
feat(context-menu): add custom context menu for workspace actions
- Introduced a ContextMenu class to enhance the workspace button's context menu functionality.- Added options to set wallpaper for the current and all virtual desktops.- Improved PopupWidget documentation for clarity on its usage and parameters.
1 parent78e3d6a commitad0ee6b

File tree

2 files changed

+78
-8
lines changed

2 files changed

+78
-8
lines changed

‎src/core/utils/utilities.py

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
importplatform
22
importre
3-
fromPyQt6.QtWidgetsimportQApplication,QFrame,QGraphicsOpacityEffect
4-
fromPyQt6.QtCoreimportQEvent,QTimer
3+
fromPyQt6.QtWidgetsimportQApplication,QFrame,QMenu
4+
fromPyQt6.QtCoreimportQEvent
55
fromPyQt6.QtGuiimportQScreen
66
fromcore.utils.win32.blurWindowimportBlur
77

@@ -19,8 +19,19 @@ def get_screen_by_name(screen_name: str) -> QScreen:
1919
returnnext(filter(lambdascr:screen_nameinscr.name(),QApplication.screens()),None)
2020

2121
classPopupWidget(QFrame):
22-
"""
23-
A custom QFrame widget that acts as a popup and hides itself when a mouse click occurs outside its geometry
22+
"""A custom QFrame widget that acts as a popup and hides itself when a mouse click occurs outside its geometry.
23+
This widget provides functionality for creating popup windows with optional blur effects and rounded corners.
24+
When a mouse click is detected outside the popup's geometry, it automatically hides and deletes itself.
25+
Args:
26+
parent (QWidget, optional): The parent widget. Defaults to None.
27+
blur (bool, optional): Whether to apply blur effect to the popup. Defaults to False.
28+
round_corners (bool, optional): Whether to apply rounded corners to the popup. Defaults to False.
29+
round_corners_type (str, optional): Type of rounded corners ('normal', 'small'). Defaults to "normal".
30+
border_color (str, optional): Color of the popup border ('System','hex', 'None'). Defaults to "None".
31+
Methods:
32+
showEvent(event): Handles the show event, applying blur effects if enabled.
33+
eventFilter(obj, event): Filters mouse click events to hide popup when clicked outside.
34+
hideEvent(event): Handles the hide event, cleaning up event filters.
2435
"""
2536
def__init__(self,parent=None,blur=False,round_corners=False,round_corners_type="normal",border_color="None"):
2637
super().__init__(parent)
@@ -56,6 +67,38 @@ def hideEvent(self, event):
5667
QApplication.instance().removeEventFilter(self)
5768
super().hideEvent(event)
5869

70+
classContextMenu(QMenu):
71+
"""A custom context menu class that extends QMenu with additional functionality.
72+
This class implements a context menu that automatically closes when clicking outside
73+
its boundaries and provides proper window activation handling.
74+
Methods:
75+
showEvent(event): Handles the menu show event by activating the window
76+
hideEvent(event): Handles the menu hide event by removing the event filter
77+
eventFilter(obj, event): Filters events to detect clicks outside the menu
78+
Inherits:
79+
QMenu: Base menu class from Qt
80+
"""
81+
def__init__(self,*args,**kwargs):
82+
super().__init__(*args,**kwargs)
83+
QApplication.instance().installEventFilter(self)
84+
85+
defshowEvent(self,event):
86+
self.activateWindow()
87+
super().showEvent(event)
88+
89+
defhideEvent(self,event):
90+
QApplication.instance().removeEventFilter(self)
91+
super().hideEvent(event)
92+
93+
defeventFilter(self,obj,event):
94+
ifevent.type()==QEvent.Type.MouseButtonPress:
95+
global_pos=event.globalPosition().toPoint()
96+
ifnotself.geometry().contains(global_pos):
97+
self.hide()
98+
self.deleteLater()
99+
returnTrue
100+
returnsuper().eventFilter(obj,event)
101+
59102

60103
classSingleton(type):
61104
_instances= {}

‎src/core/widgets/yasb/windows_desktops.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
importlogging
2-
fromPyQt6.QtWidgetsimportQPushButton,QWidget,QHBoxLayout,QMenu,QInputDialog
2+
fromPyQt6.QtWidgetsimportQPushButton,QWidget,QHBoxLayout,QInputDialog,QFileDialog
33
fromPyQt6.QtCoreimportpyqtSignal,Qt,QTimer
44
fromPyQt6.QtGuiimportQCursor
55
fromcore.widgets.baseimportBaseWidget
66
fromcore.validation.widgets.yasb.windows_desktopsimportVALIDATION_SCHEMA
77
fromcore.event_serviceimportEventService
8-
frompyvdaimportVirtualDesktop,get_virtual_desktops
9-
8+
frompyvdaimportVirtualDesktop,get_virtual_desktops,set_wallpaper_for_all_desktops
9+
fromcore.utils.utilitiesimportContextMenu
10+
1011
classWorkspaceButton(QPushButton):
1112
def__init__(self,workspace_index:int,label:str=None,active_label:str=None,parent=None):
1213
super().__init__(parent)
@@ -78,19 +79,45 @@ def update_width():
7879
self._animation_timer.start(step_duration)
7980

8081
defcontextMenuEvent(self,event):
81-
menu=QMenu(self)
82+
menu=ContextMenu(self)
8283
menu.setProperty("class","context-menu")
8384
rename_action=menu.addAction("Rename")
8485
delete_action=menu.addAction("Delete")
8586
menu.addSeparator()
8687
create_action=menu.addAction("Create New Desktop")
88+
menu.addSeparator()
89+
set_wallpaper_action=menu.addAction("Set Wallpaper On This Desktop")
90+
set_wallpaper_action_all=menu.addAction("Set Wallpaper On All Desktops")
8791

8892
rename_action.triggered.connect(self.rename_desktop)
8993
delete_action.triggered.connect(self.delete_desktop)
9094
create_action.triggered.connect(self.create_new_desktop)
95+
set_wallpaper_action.triggered.connect(self.set_wallpaper)
96+
set_wallpaper_action_all.triggered.connect(self.set_wallpaper_all)
9197

9298
menu.exec(self.mapToGlobal(event.pos()))
9399

100+
101+
defset_wallpaper(self):
102+
image_path,_=QFileDialog.getOpenFileName(
103+
self,
104+
"Select Wallpaper Image",
105+
"",
106+
"Images (*.png *.jpg *.jpeg *.bmp *.gif)"
107+
)
108+
ifimage_path:
109+
VirtualDesktop(self.workspace_index).set_wallpaper(image_path)
110+
111+
defset_wallpaper_all(self):
112+
image_path,_=QFileDialog.getOpenFileName(
113+
self,
114+
"Select Wallpaper Image",
115+
"",
116+
"Images (*.png *.jpg *.jpeg *.bmp *.gif)"
117+
)
118+
ifimage_path:
119+
set_wallpaper_for_all_desktops(image_path)
120+
94121
defrename_desktop(self):
95122
dialog=QInputDialog(self)
96123
dialog.setWindowFlags(Qt.WindowType.Dialog|Qt.WindowType.CustomizeWindowHint|Qt.WindowType.WindowTitleHint)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp