From 84ca9cf4ae783873c1f599a38fba9a044566d2d7 Mon Sep 17 00:00:00 2001 From: Graeme Ritchie Date: Fri, 1 Nov 2019 12:03:07 +0000 Subject: [PATCH] Allow user to specify any duration for suspension Extend redshift-gtk's "Suspend for" menu with a "Custom" option, which pops up a dialogue for the user to enter a number of minutes (up to a maximum of 720, i.e. 12 hours). --- src/redshift-gtk/statusicon.py | 60 ++++++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 2 deletions(-) diff --git a/src/redshift-gtk/statusicon.py b/src/redshift-gtk/statusicon.py index b4adfb00..9e20d377 100644 --- a/src/redshift-gtk/statusicon.py +++ b/src/redshift-gtk/statusicon.py @@ -44,6 +44,7 @@ _ = gettext.gettext +MAXSUSPENSION = 720 # Limit to custom suspensions -- 12 hours class RedshiftStatusIcon(object): """The status icon tracking the RedshiftController.""" @@ -86,7 +87,8 @@ def __init__(self, controller): (60, _('1 hour')), (120, _('2 hours')), (240, _('4 hours')), - (480, _('8 hours'))]: + (480, _('8 hours')), + (0, _('Custom'))]: suspend_item = Gtk.MenuItem.new_with_label(label) suspend_item.connect('activate', self.suspend_cb, minutes) suspend_menu.append(suspend_item) @@ -184,7 +186,7 @@ def __init__(self, controller): # Initialize suspend timer self.suspend_timer = None - + def remove_suspend_timer(self): """Disable any previously set suspend timer.""" if self.suspend_timer is not None: @@ -198,6 +200,11 @@ def suspend_cb(self, item, minutes): redshift is not disabled when called, it will still set a suspend timer and reactive redshift when the timer is up. """ + if not minutes: # Custom interval, not specific duration + minutes = self.get_user_duration() + if not minutes: # User did not give valid duration + return + # Now minutes is a valid duration # Inhibit self._controller.set_inhibit(True) @@ -208,6 +215,30 @@ def suspend_cb(self, item, minutes): # If redshift was already disabled we reenable it nonetheless. self.suspend_timer = GLib.timeout_add_seconds( minutes * 60, self.reenable_cb) + + + def get_user_duration(self, default='0'): + """Ask user to specify suspension time in minutes. Return integer.""" + menulabels = ['15', '45', '90', '150'] # To be offered to user + title = "Suspension interval" # Heading for window + dialogue = DurationDialog(title, menulabels) + response = dialogue.run() + if response == Gtk.ResponseType.OK: + txt = dialogue.get_result() + else: + txt = '' + dialogue.destroy() + if txt: + try: + value = int(float(txt)) + if value > MAXSUSPENSION: + value = 0 + except ValueError: + value = 0 + else: + value = 0 + return value + def reenable_cb(self): """Callback to reenable redshift when a suspend timer expires.""" @@ -349,6 +380,31 @@ def destroy_cb(self, widget, data=None): self._controller.terminate_child() return False +class DurationDialog(Gtk.Dialog): + def __init__(self, title, menulabels): + Gtk.Dialog.__init__(self, title, None, 0, + (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, + Gtk.STOCK_OK, Gtk.ResponseType.OK)) + self.result = "" + self.connect("response", self.on_response) + prompt = Gtk.Label("Number of minutes:") + databox = Gtk.ComboBoxText.new_with_entry() + databox.set_wrap_width(1) # width of menu to 1 column + for lab in menulabels: + databox.append_text(lab) + self.entry = databox.get_child() + limit = Gtk.Label("(Maximum " + str(MAXSUSPENSION) + ")") + box = self.get_content_area() + box.add(prompt) + box.add(databox) + box.add(limit) + self.show_all() + + def on_response(self, widget, response_id): + self.result = self.entry.get_text () + + def get_result(self): + return self.result def run(): utils.setproctitle('redshift-gtk')