diff --git a/.gitignore b/.gitignore index df8c4f9..0df17d9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,6 @@ *~ -build/ \ No newline at end of file +build/ + +debian + +obj-x86_64-linux-gnu \ No newline at end of file diff --git a/assets/screenshot.jpeg b/assets/screenshot.jpeg index c990a3d..f8ba03d 100644 Binary files a/assets/screenshot.jpeg and b/assets/screenshot.jpeg differ diff --git a/meson.build b/meson.build index e359bc3..897331f 100644 --- a/meson.build +++ b/meson.build @@ -2,10 +2,11 @@ project( 'wingpanel-caffeine', 'vala', 'c', - version : '0.1.1' + version : '0.2.0' ) i18n = import('i18n') +app_id = 'dev.josemunoz.wingpanel-caffeine' gettext_name = meson.project_name() + '-indicator' add_global_arguments('-DGETTEXT_PACKAGE="@0@"'.format(gettext_name), language:'c') @@ -29,6 +30,7 @@ shared_module( meson.project_name(), 'src/Indicator.vala', 'src/Caffeinate.vala', + 'src/TimeOptions.vala', 'src/PopOverWidget.vala', 'src/RevealerSwitch.vala', dependencies: [ diff --git a/src/Caffeinate.vala b/src/Caffeinate.vala index 5ea5e55..ea003ee 100644 --- a/src/Caffeinate.vala +++ b/src/Caffeinate.vala @@ -48,7 +48,7 @@ public class Caffeinate { } public void timed_session (int duration, Callback callback) { - timer = new TimeoutSource.seconds (duration); + timer = new TimeoutSource (duration); timer.set_callback (() => { this.stop (); diff --git a/src/PopOverWidget.vala b/src/PopOverWidget.vala index aa4b8a7..26bc792 100644 --- a/src/PopOverWidget.vala +++ b/src/PopOverWidget.vala @@ -2,89 +2,71 @@ public class PopOverWidget : Gtk.Box { public signal void toggle_caffeine (bool is_active); private Caffeinate caffeine; - private Granite.SwitchModelButton main_switch; private RevealerSwitch indefinite_switch; private Notify.Notification disable_alert; - private Notify.Notification invalid_input_alert; + private Granite.SwitchModelButton main_switch; public PopOverWidget () { - try { - caffeine = new Caffeinate (); - main_switch = new Granite.SwitchModelButton ("Caffeinate"); - indefinite_switch = new RevealerSwitch ("Indefinite", true, true); - invalid_input_alert = new Notify.Notification ("Caffeine", "Timeout can only be numeric", "dialog-warning"); - disable_alert = new Notify.Notification ("Decaffeinated", "Caffeine will no longer keep the system awake", "caffeine-cup-empty-symbolic"); - - bool is_indefinite = true; - Regex only_numbers = new Regex ("^[0-9]*$"); - var timeout_label = new Gtk.Label ("timeout in minutes:"); - var container = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 0); - var timeout_entry = new Granite.ValidatedEntry.from_regex (only_numbers); - - main_switch.get_style_context ().add_class (Granite.STYLE_CLASS_H3_LABEL); - indefinite_switch.get_style_context ().add_class (Granite.STYLE_CLASS_H3_LABEL); - - indefinite_switch.toggled.connect ((nextState) => { - is_indefinite = nextState; - }); - - main_switch.toggled.connect ((nextState) => { - toggle_caffeine (nextState.active); - timeout_entry.set_sensitive (!nextState.active); - indefinite_switch.set_sensitive (!nextState.active); - - if (nextState.active) { - if (is_indefinite) { - caffeine.start (); - } else if (timeout_entry.is_valid) { - var timeout_in_seconds = int.parse (timeout_entry.get_text ()) * 60; - - caffeine.timed_session (timeout_in_seconds, () => { - main_switch.set_active (false); - - try { - disable_alert.show (); - } catch {} - }); - } else { - TimeoutSource timer = new TimeoutSource (1); - - timer.set_callback (() => { - main_switch.set_active (false); - - return false; - }); + bool is_indefinite = true; + Gtk.Label timeout_label = new Gtk.Label ("Timeout:"); + Gtk.Box container = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 0); + TimeOptions.ComboBox time_options_combobox = new TimeOptions.ComboBox (); + + this.caffeine = new Caffeinate (); + this.orientation = Gtk.Orientation.VERTICAL; + this.main_switch = new Granite.SwitchModelButton ("Caffeinate"); + this.indefinite_switch = new RevealerSwitch ("Indefinite", true, true); + this.disable_alert = new Notify.Notification ( + "Decaffeinated", + "Caffeine will no longer keep the system awake", + "caffeine-cup-empty-symbolic" + ); + + container.set_margin_top (4); + disable_alert.set_timeout (3000); + timeout_label.set_padding (10, 0); + indefinite_switch.add (container); + time_options_combobox.set_margin_end (10); + timeout_label.set_halign (Gtk.Align.START); + container.pack_start (timeout_label, true, true, 0); + container.pack_start (time_options_combobox, false, false, 0); + main_switch.get_style_context ().add_class (Granite.STYLE_CLASS_H3_LABEL); + indefinite_switch.get_style_context ().add_class (Granite.STYLE_CLASS_H3_LABEL); + + indefinite_switch.toggled.connect ((nextState) => { + is_indefinite = nextState; + }); + + main_switch.toggled.connect ((nextState) => { + toggle_caffeine (nextState.active); + indefinite_switch.set_sensitive (!nextState.active); + time_options_combobox.set_sensitive (!nextState.active); + + if (nextState.active) { + if (is_indefinite) { + caffeine.start (); + } else { + var timeout = time_options_combobox.get_selected_value (); - timer.attach (); + caffeine.timed_session (timeout, () => { + main_switch.set_active (false); try { - invalid_input_alert.show (); + disable_alert.show (); } catch {} - } - } else { - caffeine.stop (); + }); } - }); - - disable_alert.set_timeout (3000); - timeout_entry.set_text ("30"); - timeout_entry.set_alignment (1); - timeout_entry.set_margin_end (10); - timeout_entry.set_width_chars (9); - indefinite_switch.add (container); - container.pack_start (timeout_label, true, true, 0); - container.pack_start (timeout_entry, false, false, 0); - container.get_style_context ().add_class (Granite.STYLE_CLASS_SMALL_LABEL); - - this.pack_start (main_switch); - this.pack_start (new Gtk.Separator (Gtk.Orientation.HORIZONTAL)); - this.pack_start (indefinite_switch); - } catch {} + } else { + caffeine.stop (); + } + }); + + this.pack_start (main_switch); + this.pack_start (new Gtk.Separator (Gtk.Orientation.HORIZONTAL)); + this.pack_start (indefinite_switch); } construct { - orientation = Gtk.Orientation.VERTICAL; - show_all (); } } \ No newline at end of file diff --git a/src/RevealerSwitch.vala b/src/RevealerSwitch.vala index daa4b9a..f35f381 100644 --- a/src/RevealerSwitch.vala +++ b/src/RevealerSwitch.vala @@ -2,9 +2,12 @@ class RevealerSwitch : Gtk.Box { public signal void toggled (bool is_active); private Gtk.Revealer revealer; + private Gtk.Box revealer_content; private Granite.SwitchModelButton main_switch; public RevealerSwitch (string title, bool defaultOpen = false, bool inverted = false) { + this.orientation = Gtk.Orientation.VERTICAL; + this.revealer_content = new Gtk.Box (Gtk.Orientation.VERTICAL, 0); this.main_switch = new Granite.SwitchModelButton (title) { active = defaultOpen }; @@ -18,19 +21,18 @@ class RevealerSwitch : Gtk.Box { this.revealer.reveal_child = inverted ? !nextState.active : nextState.active; }); - + this.revealer_content.pack_start (new Gtk.Separator (Gtk.Orientation.HORIZONTAL)); + this.revealer.add (revealer_content); this.pack_start (this.main_switch); this.pack_start (this.revealer); } construct { - orientation = Gtk.Orientation.VERTICAL; - show_all (); } public new void add (Gtk.Widget child) { - this.revealer.add (child); + this.revealer_content.pack_start (child); } } \ No newline at end of file diff --git a/src/TimeOptions.vala b/src/TimeOptions.vala new file mode 100644 index 0000000..0506147 --- /dev/null +++ b/src/TimeOptions.vala @@ -0,0 +1,60 @@ +namespace TimeOptions { + private int minutes_to_milis (int minutes) { + int one_second = 1000; + int one_minute = 60 * one_second; + + return minutes * one_minute; + } + + private class Value { + public string label; + public int timeout; + + public Value (string label, int timeout) { + this.label = label; + this.timeout = timeout; + } + + } + + public class ComboBox : Gtk.ComboBox { + private Gtk.ListStore data; + + public ComboBox () { + Gtk.TreeIter iterator; + Gtk.CellRendererText renderer = new Gtk.CellRendererText (); + + this.data = new Gtk.ListStore (2, typeof (string), typeof (int)); + + TimeOptions.Value[] options = { + new TimeOptions.Value ("30 Minutes", minutes_to_milis (30)), + new TimeOptions.Value ("1 Hour", minutes_to_milis (60)), + new TimeOptions.Value ("2 Hours", minutes_to_milis (120)), + new TimeOptions.Value ("4 Hours", minutes_to_milis (240)), + new TimeOptions.Value ("5 Hours", minutes_to_milis (300)), + }; + + foreach (TimeOptions.Value item in options) { + data.append (out iterator); + data.set (iterator, 0, item.label, 1, item.timeout, -1); + } + + this.set_model (data); + this.set_active (0); + this.pack_start (renderer, true); + this.add_attribute (renderer, "text", 0); + } + + public int get_selected_value () { + int result = 0; + Gtk.TreeIter iterator; + + if (this.get_active_iter (out iterator)) { + this.data.get (iterator, 1, out result, -1); + } + + return result; + } + + } +}