implement accelerators as application actions
This commit is contained in:
parent
d20848878a
commit
b9416da9c8
@ -1,23 +1,28 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import subprocess
|
|
||||||
import selectors
|
|
||||||
import html
|
|
||||||
import gi
|
import gi
|
||||||
|
import html
|
||||||
|
import logging
|
||||||
import os
|
import os
|
||||||
|
import selectors
|
||||||
|
import signal
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
|
||||||
gi.require_version('Gtk', '3.0')
|
|
||||||
gi.require_version('Gdk', '3.0')
|
gi.require_version('Gdk', '3.0')
|
||||||
|
gi.require_version('Gtk', '3.0')
|
||||||
try:
|
|
||||||
from gi.repository import Gdk
|
|
||||||
except ImportError:
|
|
||||||
print("ERROR: Could not import Gdk")
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from gi.repository import Gtk
|
from gi.repository import Gtk
|
||||||
except ImportError:
|
except ImportError:
|
||||||
print("ERROR: Could not import Gtk")
|
logging.error("Could not import Gtk")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
try:
|
||||||
|
from gi.repository import Gio
|
||||||
|
except ImportError:
|
||||||
|
logging.error("Could not import Gio")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
class UpdateWindow(Gtk.ApplicationWindow):
|
class UpdateWindow(Gtk.ApplicationWindow):
|
||||||
@ -78,12 +83,12 @@ class UpdateWindow(Gtk.ApplicationWindow):
|
|||||||
|
|
||||||
def run(self, args, ignore_stderr=False, output_msg=None,
|
def run(self, args, ignore_stderr=False, output_msg=None,
|
||||||
empty_msg=None, env={}):
|
empty_msg=None, env={}):
|
||||||
self.clear()
|
|
||||||
self.lock()
|
self.lock()
|
||||||
self.spinner.start()
|
self.clear()
|
||||||
self.prepend_mesg(
|
self.prepend_mesg(
|
||||||
"INFO",
|
"INFO",
|
||||||
"Running command \"%s\" ..." % " ".join(args))
|
"Running command \"%s\" ..." % " ".join(args))
|
||||||
|
|
||||||
p = subprocess.Popen(
|
p = subprocess.Popen(
|
||||||
args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False,
|
args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False,
|
||||||
env=dict(os.environ, **env))
|
env=dict(os.environ, **env))
|
||||||
@ -101,9 +106,11 @@ class UpdateWindow(Gtk.ApplicationWindow):
|
|||||||
|
|
||||||
if data:
|
if data:
|
||||||
if key.fileobj is p.stdout:
|
if key.fileobj is p.stdout:
|
||||||
|
logging.debug("STDOUT: " + data)
|
||||||
self.append_mesg("STDOUT", data)
|
self.append_mesg("STDOUT", data)
|
||||||
output = True
|
output = True
|
||||||
elif not ignore_stderr:
|
elif not ignore_stderr:
|
||||||
|
logging.debug("STDERR: " + data)
|
||||||
self.append_mesg("STDERR", data)
|
self.append_mesg("STDERR", data)
|
||||||
|
|
||||||
exit_code = p.poll()
|
exit_code = p.poll()
|
||||||
@ -119,7 +126,6 @@ class UpdateWindow(Gtk.ApplicationWindow):
|
|||||||
done = True
|
done = True
|
||||||
break
|
break
|
||||||
|
|
||||||
self.spinner.stop()
|
|
||||||
self.unlock()
|
self.unlock()
|
||||||
|
|
||||||
if error:
|
if error:
|
||||||
@ -137,11 +143,13 @@ class UpdateWindow(Gtk.ApplicationWindow):
|
|||||||
self.update_button.set_sensitive(False)
|
self.update_button.set_sensitive(False)
|
||||||
self.upgrade_button.set_sensitive(False)
|
self.upgrade_button.set_sensitive(False)
|
||||||
self.list_button.set_sensitive(False)
|
self.list_button.set_sensitive(False)
|
||||||
|
self.spinner.start()
|
||||||
|
|
||||||
def unlock(self):
|
def unlock(self):
|
||||||
self.update_button.set_sensitive(True)
|
self.update_button.set_sensitive(True)
|
||||||
self.upgrade_button.set_sensitive(True)
|
self.upgrade_button.set_sensitive(True)
|
||||||
self.list_button.set_sensitive(True)
|
self.list_button.set_sensitive(True)
|
||||||
|
self.spinner.stop()
|
||||||
|
|
||||||
def upgrade(self):
|
def upgrade(self):
|
||||||
args = ['/usr/bin/apt-get', '-yqq', 'full-upgrade']
|
args = ['/usr/bin/apt-get', '-yqq', 'full-upgrade']
|
||||||
@ -173,26 +181,19 @@ class UpdateWindow(Gtk.ApplicationWindow):
|
|||||||
def on_list(self, *args):
|
def on_list(self, *args):
|
||||||
self.list()
|
self.list()
|
||||||
|
|
||||||
def on_ctrl_w(self, *args):
|
def on_quit(self, *args):
|
||||||
self.app.quit()
|
self.application.quit()
|
||||||
|
|
||||||
def __init__(self, app):
|
def __init__(self, application):
|
||||||
super(UpdateWindow, self).__init__(
|
super(UpdateWindow, self).__init__(
|
||||||
application=app,
|
application=application,
|
||||||
title="Simple APT Update")
|
title="Simple APT Update")
|
||||||
self.app = app
|
self.application = application
|
||||||
self.init_ui()
|
self.init_ui()
|
||||||
|
|
||||||
def init_ui(self):
|
def init_ui(self):
|
||||||
self.set_icon_from_file('/usr/share/icons/simple-apt-update.svg')
|
|
||||||
self.set_border_width(10)
|
self.set_border_width(10)
|
||||||
self.set_default_size(630, 390)
|
self.set_default_size(630, 390)
|
||||||
accel = Gtk.AccelGroup()
|
|
||||||
accel.connect(Gdk.keyval_from_name('W'), Gdk.ModifierType.CONTROL_MASK,
|
|
||||||
0, self.on_ctrl_w)
|
|
||||||
accel.connect(Gdk.keyval_from_name('Q'), Gdk.ModifierType.CONTROL_MASK,
|
|
||||||
0, self.on_ctrl_w)
|
|
||||||
self.add_accel_group(accel)
|
|
||||||
|
|
||||||
hbox = Gtk.Box(spacing=6, orientation=Gtk.Orientation.VERTICAL)
|
hbox = Gtk.Box(spacing=6, orientation=Gtk.Orientation.VERTICAL)
|
||||||
self.add(hbox)
|
self.add(hbox)
|
||||||
@ -221,13 +222,13 @@ class UpdateWindow(Gtk.ApplicationWindow):
|
|||||||
|
|
||||||
self.spinner = Gtk.Spinner()
|
self.spinner = Gtk.Spinner()
|
||||||
self.spinner.set_hexpand(True)
|
self.spinner.set_hexpand(True)
|
||||||
grid.attach(self.spinner, 4, 0, 1, 1)
|
grid.attach(self.spinner, 3, 0, 1, 1)
|
||||||
|
|
||||||
self.quit_button = Gtk.Button.new_from_icon_name(
|
self.quit_button = Gtk.Button.new_from_icon_name(
|
||||||
"gtk-cancel", Gtk.IconSize.BUTTON)
|
"exit", Gtk.IconSize.BUTTON)
|
||||||
self.quit_button.set_tooltip_text("Exit the program")
|
self.quit_button.set_tooltip_text("Exit the program")
|
||||||
self.quit_button.set_halign(Gtk.Align.END)
|
self.quit_button.set_halign(Gtk.Align.END)
|
||||||
self.quit_button.connect("clicked", self.on_ctrl_w)
|
self.quit_button.connect("clicked", self.on_quit)
|
||||||
grid.attach(self.quit_button, 4, 0, 1, 1)
|
grid.attach(self.quit_button, 4, 0, 1, 1)
|
||||||
|
|
||||||
self.scrolledwindow = Gtk.ScrolledWindow()
|
self.scrolledwindow = Gtk.ScrolledWindow()
|
||||||
@ -246,14 +247,50 @@ class UpdateWindow(Gtk.ApplicationWindow):
|
|||||||
hbox.pack_start(self.scrolledwindow, True, True, 0)
|
hbox.pack_start(self.scrolledwindow, True, True, 0)
|
||||||
|
|
||||||
|
|
||||||
def on_activate(app):
|
class SimpleAptUpdate(Gtk.Application):
|
||||||
win = UpdateWindow(app)
|
def __init__(self):
|
||||||
win.present()
|
super().__init__(application_id='de.linuxfoo.SimpleAptUpdate',
|
||||||
win.show_all()
|
flags=Gio.ApplicationFlags.FLAGS_NONE)
|
||||||
win.update()
|
self.connect('activate', self.on_activate)
|
||||||
win.list()
|
signal.signal(signal.SIGINT, signal.SIG_DFL)
|
||||||
|
|
||||||
|
def do_command_line(self, cmdline):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def on_activate(self, application):
|
||||||
|
self.window = UpdateWindow(application)
|
||||||
|
|
||||||
|
action = Gio.SimpleAction.new("quit", None)
|
||||||
|
action.connect("activate", self.window.on_quit)
|
||||||
|
self.add_action(action)
|
||||||
|
self.set_accels_for_action('app.quit', ['<Primary>q', '<Primary>w'])
|
||||||
|
|
||||||
|
action = Gio.SimpleAction.new("update", None)
|
||||||
|
action.connect("activate", self.window.on_update)
|
||||||
|
self.add_action(action)
|
||||||
|
self.set_accels_for_action('app.update', ['<Primary>u'])
|
||||||
|
|
||||||
|
action = Gio.SimpleAction.new("upgrade", None)
|
||||||
|
action.connect("activate", self.window.on_upgrade)
|
||||||
|
self.add_action(action)
|
||||||
|
self.set_accels_for_action('app.upgrade', ['<Primary>g'])
|
||||||
|
|
||||||
|
action = Gio.SimpleAction.new("list", None)
|
||||||
|
action.connect("activate", self.window.on_list)
|
||||||
|
self.add_action(action)
|
||||||
|
self.set_accels_for_action('app.list', ['<Primary>l'])
|
||||||
|
|
||||||
|
self.window.present()
|
||||||
|
self.window.show_all()
|
||||||
|
self.window.update()
|
||||||
|
self.window.list()
|
||||||
|
|
||||||
|
|
||||||
app = Gtk.Application(application_id='org.linuxfoo.SimpleAptUpdate')
|
def main():
|
||||||
app.connect('activate', on_activate)
|
application = SimpleAptUpdate()
|
||||||
app.run(None)
|
exit_status = application.run()
|
||||||
|
sys.exit(exit_status)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
|
Loading…
Reference in New Issue
Block a user