33# https://github.com/helloSystem/Menu/issues/3
44
55
6- from PyQt5 .QtWidgets import QApplication , QSystemTrayIcon , QMenu , QAction , QHBoxLayout , QGroupBox , QSlider , QWidget , \
7- QActionGroup , QDesktopWidget , QMessageBox
8- from PyQt5 .QtGui import QIcon , QPixmap , QCursor
9- from PyQt5 .QtCore import Qt , QProcess , QMetaObject , QCoreApplication , QEvent , QObject , QTimer
10- import sys , os
11-
6+ import os
7+ import platform
128import re
139import subprocess
10+ import sys
11+
12+ from PyQt5 .QtCore import (QCoreApplication , QEvent , QMetaObject , QObject ,
13+ QProcess , Qt , QTimer )
14+ from PyQt5 .QtGui import QCursor , QIcon , QPixmap
15+ from PyQt5 .QtWidgets import (QAction , QActionGroup , QApplication ,
16+ QDesktopWidget , QGroupBox , QHBoxLayout , QMenu ,
17+ QMessageBox , QSlider , QSystemTrayIcon , QWidget )
1418
1519
1620# Show the slider UI when QSystemTrayIcon is left-clicked
@@ -136,16 +140,39 @@ class VolumeMenu(QObject):
136140 self .tray .setVisible (True )
137141 self .menu = QMenu ()
138142
139- self .tray .activated .connect (self .onClicked ) # Refresh each time the menu is clicked. FIXME: Does not work on right- click; why?
140-
141- self .tray . setContextMenu (self .menu )
143+ self .tray .activated .connect (self .onClicked ) # Refresh each time the menu is clicked (left click)
144+ # Also refresh the list of devices when the context menu is requested (right click)
145+ self .menu . aboutToShow . connect (self .refreshMenu )
142146
143- # TODO: Add a check to ensure that
144- # sysctl hw.snd.verbose is 0
147+ self .tray .setContextMenu (self .menu )
148+
149+ # If we are on FreeBSD, ensure that sysctl hw.snd.verbose is 0
145150 # Otherwise this application will not work correctly
146- # and that
147- # sysctl hw.snd.default_auto is 2
148- # Otherwise newly attached sound devices will not be activated automatically
151+ if platform .system () == "FreeBSD" :
152+ if int (subprocess .check_output ("sysctl hw.snd.verbose" , shell = True ).decode ('utf-8' ).split (" " )[- 1 ]) != 0 :
153+ print ("hw.snd.verbose is not 0" )
154+ # Show a warning dialog
155+ msg = QMessageBox ()
156+ msg .setIcon (QMessageBox .Warning )
157+ msg .setText ("hw.snd.verbose is not 0" )
158+ msg .setInformativeText ("This application will not work correctly unless hw.snd.verbose is 0" )
159+ msg .setWindowTitle ("Warning" )
160+ msg .setStandardButtons (QMessageBox .Ok )
161+ msg .exec_ ()
162+
163+ # If we are on FreeBSD, check that hw.snd.default_auto is 2
164+ if platform .system () == "FreeBSD" :
165+ default_auto = subprocess .check_output ("sysctl hw.snd.default_auto" , shell = True ).decode ('utf-8' ).split (" " )[- 1 ]
166+ if int (default_auto ) != 2 :
167+ print ("hw.snd.default_auto is not 2 but it is " + default_auto )
168+ # Show a warning dialog
169+ msg = QMessageBox ()
170+ msg .setIcon (QMessageBox .Warning )
171+ msg .setText ("hw.snd.default_auto is not 2" )
172+ msg .setInformativeText ("In order to switch automatically to the sound device plugged in most recently, set hw.snd.default_auto to 2" )
173+ msg .setWindowTitle ("Warning" )
174+ msg .setStandardButtons (QMessageBox .Ok )
175+ msg .exec_ ()
149176
150177 # NOTE:
151178 # https://forum.learnpyqt.com/t/qsystemtrayicon-example/689
@@ -154,12 +181,6 @@ class VolumeMenu(QObject):
154181 self .sliderWindow = None
155182
156183 self .refreshMenu () # Initially populate the menu
157- self .tray .installEventFilter (self ) # FIXME: This never seems to get called, why?
158- self .installEventFilter (self ) # FIXME: This never seems to get called, why?
159-
160- def eventFilter (self , obj , event ):
161- print ("eventFilter function running" ) # FIXME: Why is this never called when the icon is right-clicked?
162- # We need to refresh the contents of the right-click menu somehow when the user right-clicks...
163184
164185 def onClicked (self , reason ):
165186 # TODO: Is using customContextMenuRequested the proper way to do this? How?
@@ -169,6 +190,7 @@ class VolumeMenu(QObject):
169190 S .show ()
170191
171192 def refreshMenu (self ):
193+ print ("refreshMenu function running" )
172194 self .actions = []
173195 self .menu .clear ()
174196 # Get the sound devices from
0 commit comments