Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minimized to Tray #167

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Frédéric Péters <[email protected]>
* Frédéric Péters <[email protected]>
* Daniel Sheeler <[email protected]>
* Athanasios Silis <[email protected]>
* Ryno Kotze <[email protected]>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this you real name? If not, please use a less gross Pseudonym.



# Translation
Expand Down
48 changes: 37 additions & 11 deletions jack_mixer/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from .serialization import SerializedObject, Serializator
from .styling import load_css_styles
from .version import __version__

from .indicator import Indicator
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep imports from the '.' package in alphabetical order, unless other reasons prevent it.


__program__ = "jack_mixer"
# A "locale" directory present within the package take precedence
Expand Down Expand Up @@ -92,7 +92,8 @@ class JackMixer(SerializedObject):
# scales suitable as volume slider scales
slider_scales = [scale.Linear30dB(), scale.Linear70dB()]

def __init__(self, client_name=__program__):
def __init__(self, client_name=__program__, minimized=False):
self.minimize_on_start = minimized
Comment on lines +95 to +96
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please name the init arg the same as the instance attribute, i.e. minimize_on_start

self.visible = False
self.nsm_client = None
# name of project file that is currently open
Expand All @@ -103,6 +104,9 @@ def __init__(self, client_name=__program__):
self.last_xml_serialization = None
self.cached_xml_serialization = None

log.info("Starting %s %s", __program__, __version__)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change the log level to DEBUG.

self.indicator = Indicator(self)

if os.environ.get("NSM_URL"):
self.nsm_client = NSMClient(
prettyName=__program__,
Expand Down Expand Up @@ -719,7 +723,14 @@ def on_save_as_cb(self, *args):
dlg.destroy()

def on_quit_cb(self, *args, on_delete=False):
if not self.nsm_client and self.gui_factory.get_confirm_quit():
log.info("on_quit_cb")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this log call.

if self.indicator.available and self.gui_factory.get_tray_minimized():
log.info("on_quit_cb: hiding window")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this log call.

self.window.set_visible(False)
return True

elif not self.nsm_client and self.gui_factory.get_confirm_quit():
log.info("on_quit_cb: confirm quit")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this log call.

dlg = Gtk.MessageDialog(
parent=self.window,
message_type=Gtk.MessageType.QUESTION,
Expand All @@ -741,6 +752,7 @@ def on_quit_cb(self, *args, on_delete=False):
if response != Gtk.ResponseType.OK:
return on_delete

log.info("on_quit_cb: quitting")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this log call.

Gtk.main_quit()

def on_shrink_channels_cb(self, widget):
Expand Down Expand Up @@ -1049,7 +1061,7 @@ def save_to_xml(self, file):
b.save(file)

def load_from_xml(self, file, silence_errors=False, from_nsm=False):
log.debug("Loading from XML...")
log.debug("Loading from XML... YES WE ARE!")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this logging message change.

self.unserialized_channels = []
b = XmlSerialization()
try:
Expand Down Expand Up @@ -1146,11 +1158,17 @@ def main(self):
if not self.mixer:
return

if self.visible or self.nsm_client is None:
width, height = self.window.get_size()
self.window.show_all()
if hasattr(self, "paned_position"):
self.paned.set_position(self.paned_position / self.width * width)
if not self.minimize_on_start:
log.info("Showing main window")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change the log level to DEBUG.

if self.visible or self.nsm_client is None:
width, height = self.window.get_size()
self.window.show_all()
if hasattr(self, "paned_position"):
self.paned.set_position(self.paned_position / self.width * width)
else:
if self.indicator.available:
Comment on lines +1168 to +1169
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this an elif.

log.info("Minimizing main window")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change the log level to DEBUG.

self.window.hide()

signal.signal(signal.SIGUSR1, self.sighandler)
signal.signal(signal.SIGTERM, self.sighandler)
Expand All @@ -1176,7 +1194,16 @@ def error_dialog(parent, msg, *args, **kw):


def main():
log.debug("JACK Mixer version %s" % __version__)
sys.stdout.flush()
parser = argparse.ArgumentParser(prog=__program__, description=_(__doc__.splitlines()[0]))
parser.add_argument(
"-m",
"--minimized",
action="store_true",
default=False,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default for store_true args is always False. Please remove this line.

help=_("start JACK Mixer minimized to system tray (default: %(default)s) If system tray is available."),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"start application minimized to system tray if system tray is available (default: no)"

(default=False is not suitable for a translated string.)

Please update translation message catalog to include this string.

)
parser.add_argument(
"-c",
"--config",
Expand Down Expand Up @@ -1204,7 +1231,7 @@ def main():
)

try:
mixer = JackMixer(args.client_name)
mixer = JackMixer(args.client_name, args.minimized)
except Exception as e:
error_dialog(None, _("Mixer creation failed:\n\n{}"), e, debug=args.debug)
sys.exit(1)
Expand All @@ -1230,6 +1257,5 @@ def main():
mixer.main()
mixer.cleanup()


if __name__ == "__main__":
main()
24 changes: 23 additions & 1 deletion jack_mixer/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def __init__(self, topwindow, meter_scales, slider_scales):
)

def set_default_preferences(self):
self.tray_minimized = False
self.confirm_quit = False
self.default_meter_scale = self.meter_scales[0]
self.default_project_path = None
Expand All @@ -93,6 +94,10 @@ def read_preferences(self):
"Preferences", "confirm_quit", fallback=self.confirm_quit
)

self.tray_minimized = self.config.getboolean(
"Preferences", "tray_minimized", fallback=self.tray_minimized
)

scale_id = self.config["Preferences"]["default_meter_scale"]
self.default_meter_scale = lookup_scale(self.meter_scales, scale_id)
if not self.default_meter_scale:
Expand Down Expand Up @@ -137,6 +142,7 @@ def read_preferences(self):

def write_preferences(self):
self.config["Preferences"] = {}
self.config["Preferences"]["tray_minimized"] = str(self.tray_minimized)
self.config["Preferences"]["confirm_quit"] = str(self.confirm_quit)
self.config["Preferences"]["default_meter_scale"] = self.default_meter_scale.scale_id
self.config["Preferences"]["default_project_path"] = self.default_project_path or ""
Expand Down Expand Up @@ -164,6 +170,9 @@ def _update_setting(self, name, value):
signal = "{}-changed".format(name.replace("_", "-"))
self.emit(signal, value)

def set_tray_minimized(self, tray_minimized):
self._update_setting("tray_minimized", tray_minimized)

def set_confirm_quit(self, confirm_quit):
self._update_setting("confirm_quit", confirm_quit)

Expand Down Expand Up @@ -210,6 +219,9 @@ def set_auto_reset_peak_meters_time_seconds(self, time):
def set_meter_refresh_period_milliseconds(self, period):
self._update_setting("meter_refresh_period_milliseconds", period)

def get_tray_minimized(self):
return self.tray_minimized

def get_confirm_quit(self):
return self.confirm_quit

Expand Down Expand Up @@ -284,7 +296,10 @@ def serialize(self, object_backend):
)

def unserialize_property(self, name, value):
if name == "confirm_quit":
if name == "tray_minimized":
self.set_tray_minimized(value == "True")
return True
elif name == "confirm_quit":
self.set_confirm_quit(value == "True")
return True
elif name == "default_meter_scale":
Expand Down Expand Up @@ -319,6 +334,13 @@ def unserialize_property(self, name, value):
return False


GObject.signal_new(
"tray-minimized-changed",
Factory,
GObject.SignalFlags.RUN_FIRST | GObject.SignalFlags.ACTION,
None,
[bool],
)
GObject.signal_new(
"confirm-quit-changed",
Factory,
Expand Down
82 changes: 82 additions & 0 deletions jack_mixer/indicator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import gi
try:
gi.require_version('Gtk', '3.0')
except Exception as e:
print(e)
print('Repository version required not present')
exit(1)

try:
from gi.repository import AppIndicator3 as appindicator
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This triggers the following warning. Please insert the appropriate gi.require_version call.

PyGIWarning: AppIndicator3 was imported without specifying a version first. Use gi.require_version('AppIndicator3', '0.1') before import to ensure that the right version gets loaded.

except ImportError:
appindicator = None

import os
import logging as log
from gi.repository import Gtk
from os import environ, path

prefix = environ.get('MESON_INSTALL_PREFIX', '/usr')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work. MESON_INSTALL_PREFIX is not set at run-time. Just use an icon name withoutt a path. It is the responsibility of the packaging to install the icon in a path where it will be found.

datadir = path.join(prefix, 'share')
icondir = path.join(datadir, 'icons', 'hicolor', 'scalable', 'apps')

class Indicator:
def __init__(self, jack_mixer):
self.app = jack_mixer
if appindicator is None:
log.warning('AppIndicator3 not found, indicator will not be available')
self.available = False
return
self.available = True
icon = os.path.join(icondir, 'jack_mixer.svg')
self.indicator = appindicator.Indicator.new("Jack Mixer",
icon,
appindicator.IndicatorCategory.APPLICATION_STATUS)
self.indicator.set_status(appindicator.IndicatorStatus.ACTIVE)
self.indicator.set_menu(self.create_menu())

def create_menu(self):
self.menu = Gtk.Menu()
self.menu.set_title('Jack Mixer')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The title should not be hard-coded here. I suggest you make it an init keyword arg and pass it from app.py.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example:

    def __init__(self, app, name, icon=None):
        self.available = bool(appindicator)

        if appindicator is None:
            log.warning('AppIndicator3 not found, indicator will not be available')
            return

        self.app = app
        self.name = name
        self.indicator = appindicator.Indicator.new(
            name,
            icon or name,
            appindicator.IndicatorCategory.APPLICATION_STATUS)
        self.indicator.set_status(appindicator.IndicatorStatus.ACTIVE)
        self.indicator.set_menu(self.create_menu())

    def create_menu(self):
        self.menu = Gtk.Menu()
        self.menu.set_title(self.name)
        ....


self.hidewindow = Gtk.MenuItem(label = 'Hide / Show Jack Mixer')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This string should be translatable.

self.hidewindow.connect('activate', self.hideshow)
self.menu.append(self.hidewindow)

self.separator = Gtk.SeparatorMenuItem()
self.menu.append(self.separator)

self.exittray = Gtk.MenuItem(label = 'Quit')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This string should be translatable.

self.exittray.connect('activate', self.quit)
self.menu.append(self.exittray)

self.menu.show_all()
return self.menu

def hideshow(self, source):
self.app.window.set_visible(not self.app.window.get_visible())

def quit(self, source, on_delete=False):
if not self.app.nsm_client and self.app.gui_factory.get_confirm_quit():
dlg = Gtk.MessageDialog(
parent=self.app.window,
message_type=Gtk.MessageType.QUESTION,
buttons=Gtk.ButtonsType.NONE,
)
dlg.set_markup(_("<b>Quit application?</b>"))
dlg.format_secondary_markup(
_(
"All jack_mixer ports will be closed and connections lost,"
"\nstopping all sound going through jack_mixer.\n\n"
"Are you sure?"
)
)
dlg.add_buttons(
Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_QUIT, Gtk.ResponseType.OK
)
response = dlg.run()
dlg.destroy()
if response != Gtk.ResponseType.OK:
return on_delete
Comment on lines +60 to +80
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't duplicate code from app.py here. Refactor the code after the elif from on_quit_cb into it's own method and call that method from here (via self.app).


Gtk.main_quit()
1 change: 1 addition & 0 deletions jack_mixer/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ python_sources = files([
'serialization_xml.py',
'slider.py',
'styling.py',
'indicator.py',
])


Expand Down
12 changes: 12 additions & 0 deletions jack_mixer/preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ def create_ui(self):
self.language_box.pack_start(Gtk.Label(_("Language:")), False, True, 5)
self.language_box.pack_start(self.language_combo, True, True, 0)

if self.app.indicator.available:
self.tray_minimized_checkbutton = Gtk.CheckButton(_("Minimize to tray"))
self.tray_minimized_checkbutton.set_tooltip_text(
_("Minimize the application to the system tray when the window is closed")
)
Comment on lines +84 to +87
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update the translation message catalog for these new translatable strings.

self.tray_minimized_checkbutton.set_active(self.app.gui_factory.get_tray_minimized())
self.tray_minimized_checkbutton.connect("toggled", self.on_tray_minimized_toggled)
interface_vbox.pack_start(self.tray_minimized_checkbutton, True, True, 3)

self.confirm_quit_checkbutton = Gtk.CheckButton(_("Confirm quit"))
self.confirm_quit_checkbutton.set_tooltip_text(
_("Always ask for confirmation before quitting the application")
Expand Down Expand Up @@ -336,6 +345,9 @@ def on_vumeter_color_change(self, *args):

self.custom_color_box.set_sensitive(self.vumeter_color_checkbutton.get_active())

def on_tray_minimized_toggled(self, *args):
self.app.gui_factory.set_tray_minimized(self.tray_minimized_checkbutton.get_active())

def on_confirm_quit_toggled(self, *args):
self.app.gui_factory.set_confirm_quit(self.confirm_quit_checkbutton.get_active())

Expand Down
13 changes: 13 additions & 0 deletions jackmixer.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[Unit]
Description=Jack Mixer Service
Requires=pipewire.socket
After=graphical.target pwg.service

[Service]
Type=simple
ExecStart=/usr/local/bin/jack_mixer --minimized --config /home/lemonxah/.config/jack_mixer/lemonxah.xml
ExecStop=/bin/kill -9 $MAINPID
Restart=always

[Install]
WantedBy=default.target
Comment on lines +1 to +13
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a different feature. Please make a separate PR for it and remove it from this one.