Skip to content

Commit

Permalink
Merge pull request #4 from jdmg94/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
José Muñoz committed Aug 21, 2022
2 parents 007d0be + 73ac996 commit 30e4ee3
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 78 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
*~
build/
build/

debian

obj-x86_64-linux-gnu
Binary file modified assets/screenshot.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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: [
Expand Down
2 changes: 1 addition & 1 deletion src/Caffeinate.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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 ();
Expand Down
124 changes: 53 additions & 71 deletions src/PopOverWidget.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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 ();
}
}
10 changes: 6 additions & 4 deletions src/RevealerSwitch.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
Expand All @@ -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);
}

}
60 changes: 60 additions & 0 deletions src/TimeOptions.vala
Original file line number Diff line number Diff line change
@@ -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;
}

}
}

0 comments on commit 30e4ee3

Please sign in to comment.