ui: add option to show the main window or run solaar without tray

This commit is contained in:
Peter F. Patel-Schneider 2020-02-05 15:34:54 -05:00 committed by Filipe Laíns
parent 5afa094175
commit 4afe8c893b
4 changed files with 24 additions and 20 deletions

View File

@ -48,7 +48,7 @@ def _parse_arguments():
help='unifying receiver to use; the first detected receiver if unspecified. Example: /dev/hidraw2') help='unifying receiver to use; the first detected receiver if unspecified. Example: /dev/hidraw2')
arg_parser.add_argument('--restart-on-wake-up', action='store_true', arg_parser.add_argument('--restart-on-wake-up', action='store_true',
help='restart Solaar on sleep wake-up (experimental)') help='restart Solaar on sleep wake-up (experimental)')
arg_parser.add_argument('-t', '--tray', action='store_true', help='start GUI with only tray icon') arg_parser.add_argument('-w', '--window', choices=('hide','show','only'), help='start with window hidden / showing / only (no tray icon)')
arg_parser.add_argument('-V', '--version', action='version', version='%(prog)s ' + __version__) arg_parser.add_argument('-V', '--version', action='version', version='%(prog)s ' + __version__)
arg_parser.add_argument('--help-actions', action='store_true', arg_parser.add_argument('--help-actions', action='store_true',
help='print help for the optional actions') help='print help for the optional actions')
@ -61,6 +61,9 @@ def _parse_arguments():
_cli.print_help() _cli.print_help()
return return
if args.window is None:
args.window = 'hide'
import logging import logging
if args.debug > 0: if args.debug > 0:
log_level = logging.WARNING - 10 * args.debug log_level = logging.WARNING - 10 * args.debug
@ -106,7 +109,7 @@ def main():
_upower.watch(listener.ping_all) _upower.watch(listener.ping_all)
# main UI event loop # main UI event loop
ui.run_loop(listener.start_all, listener.stop_all, args.tray) ui.run_loop(listener.start_all, listener.stop_all, args.window!='only', args.window!='hide')
except Exception as e: except Exception as e:
import sys import sys
sys.exit('%s: error: %s' % (NAME.lower(), e)) sys.exit('%s: error: %s' % (NAME.lower(), e))

View File

@ -86,7 +86,7 @@ def ui_async(function, *args, **kwargs):
from . import notify, tray, window from . import notify, tray, window
def _startup(app, startup_hook, tray_only): def _startup(app, startup_hook, use_tray, show_window):
if _log.isEnabledFor(_DEBUG): if _log.isEnabledFor(_DEBUG):
_log.debug("startup registered=%s, remote=%s", app.get_is_registered(), app.get_is_remote()) _log.debug("startup registered=%s, remote=%s", app.get_is_registered(), app.get_is_remote())
@ -96,8 +96,9 @@ def _startup(app, startup_hook, tray_only):
_task_runner.start() _task_runner.start()
notify.init() notify.init()
if use_tray:
tray.init(lambda _ignore: window.destroy()) tray.init(lambda _ignore: window.destroy())
window.init(tray_only) window.init(show_window, use_tray)
startup_hook() startup_hook()
@ -133,12 +134,13 @@ def _shutdown(app, shutdown_hook):
notify.uninit() notify.uninit()
def run_loop(startup_hook, shutdown_hook, tray_only, args=None): def run_loop(startup_hook, shutdown_hook, use_tray, show_window, args=None):
assert use_tray or show_window, 'need either tray or visible window'
# from gi.repository.Gio import ApplicationFlags as _ApplicationFlags # from gi.repository.Gio import ApplicationFlags as _ApplicationFlags
APP_ID = 'io.github.pwr.solaar' APP_ID = 'io.github.pwr.solaar'
application = Gtk.Application.new(APP_ID, 0) # _ApplicationFlags.HANDLES_COMMAND_LINE) application = Gtk.Application.new(APP_ID, 0) # _ApplicationFlags.HANDLES_COMMAND_LINE)
application.connect('startup', lambda app, startup_hook:_startup(app,startup_hook,tray_only), startup_hook) application.connect('startup', lambda app, startup_hook:_startup(app,startup_hook,use_tray,show_window), startup_hook)
application.connect('command-line', _command_line) application.connect('command-line', _command_line)
application.connect('activate', _activate) application.connect('activate', _activate)
application.connect('shutdown', _shutdown, shutdown_hook) application.connect('shutdown', _shutdown, shutdown_hook)

View File

@ -458,7 +458,7 @@ def init(_quit_handler):
def destroy(): def destroy():
global _icon, _menu, _devices_info global _icon, _menu, _devices_info
assert _icon is not None if _icon is not None:
i, _icon = _icon, None i, _icon = _icon, None
_destroy(i) _destroy(i)
i = None i = None

View File

@ -304,11 +304,10 @@ def _create_window_layout():
panel.pack_start(_info, True, True, 0) panel.pack_start(_info, True, True, 0)
panel.pack_start(_empty, True, True, 0) panel.pack_start(_empty, True, True, 0)
about_button = _new_button(_("About") + ' ' + NAME, 'help-about',
icon_size=_SMALL_BUTTON_ICON_SIZE, clicked=_show_about_window)
bottom_buttons_box = Gtk.ButtonBox(Gtk.Orientation.HORIZONTAL) bottom_buttons_box = Gtk.ButtonBox(Gtk.Orientation.HORIZONTAL)
bottom_buttons_box.set_layout(Gtk.ButtonBoxStyle.START) bottom_buttons_box.set_layout(Gtk.ButtonBoxStyle.START)
about_button = _new_button(_("About") + ' ' + NAME, 'help-about',
icon_size=_SMALL_BUTTON_ICON_SIZE, clicked=_show_about_window)
bottom_buttons_box.add(about_button) bottom_buttons_box.add(about_button)
# solaar_version = Gtk.Label() # solaar_version = Gtk.Label()
@ -327,7 +326,7 @@ def _create_window_layout():
return vbox return vbox
def _create(): def _create(delete_action):
window = Gtk.Window() window = Gtk.Window()
window.set_title(NAME) window.set_title(NAME)
window.set_role('status-window') window.set_role('status-window')
@ -335,7 +334,7 @@ def _create():
# window.set_type_hint(Gdk.WindowTypeHint.UTILITY) # window.set_type_hint(Gdk.WindowTypeHint.UTILITY)
# window.set_skip_taskbar_hint(True) # window.set_skip_taskbar_hint(True)
# window.set_skip_pager_hint(True) # window.set_skip_pager_hint(True)
window.connect('delete-event', _hide) window.connect('delete-event', delete_action)
vbox = _create_window_layout() vbox = _create_window_layout()
window.add(vbox) window.add(vbox)
@ -731,7 +730,7 @@ _empty = None
_window = None _window = None
def init(tray_only): def init(show_window,hide_on_close):
Gtk.Window.set_default_icon_name(NAME.lower()) Gtk.Window.set_default_icon_name(NAME.lower())
Gtk.Window.set_default_icon_from_file(_icons.icon_file(NAME.lower())) Gtk.Window.set_default_icon_from_file(_icons.icon_file(NAME.lower()))
@ -741,12 +740,12 @@ def init(tray_only):
_details = _create_details_panel() _details = _create_details_panel()
_info = _create_info_panel() _info = _create_info_panel()
_empty = _create_empty_panel() _empty = _create_empty_panel()
_window = _create() _window = _create(_hide if hide_on_close else destroy)
if (not tray_only) : if show_window:
_window.present() _window.present()
def destroy(): def destroy(_ignore1=None, _ignore2=None):
global _model, _tree, _details, _info, _empty, _window global _model, _tree, _details, _info, _empty, _window
w, _window = _window, None w, _window = _window, None
w.destroy() w.destroy()