Skip to content
This repository has been archived by the owner on Jul 6, 2024. It is now read-only.

Commit

Permalink
Update timer picker view.
Browse files Browse the repository at this point in the history
  • Loading branch information
naz013 committed Feb 19, 2019
1 parent 86dbf4f commit 73bb1b6
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 58 deletions.
53 changes: 14 additions & 39 deletions src/config/Utils.vala
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,24 @@ namespace Tasks {
return dt.to_unix();
}

public static string to_label_from_seconds(int64 seconds) {
return to_label(from_seconds(seconds));
public static string to_label_from_seconds(int64 all_seconds) {
int hours = (int) (all_seconds / HOUR);
int minutes = (int) ((all_seconds - (hours * HOUR)) / MINUTE);
int seconds = (int) (all_seconds - (hours * HOUR) - (minutes * MINUTE));
return to_label(hours, minutes, seconds);
}

public static string to_label(string input) {
var tmp = "";
if (input.length < LENGTH) {
for (int i = 0; i < LENGTH - input.length; i++) {
tmp = tmp + ZERO;
}
}
tmp = tmp + input;
// Logger.log(@"create_label: tmp -> $tmp");

var d01 = tmp.substring(0, 2);
var d23 = tmp.substring(2, 2);
var d45 = tmp.substring(4, 2);

var hours = int.parse(d01);
var minutes = int.parse(d23);

string h = _("hrs");
public static string to_label(int hours, int minutes, int seconds) {
string h = _("hrs");
string m = _("mins");
string s = _("sec");

if (hours > 0) {
return @"$d01 $h $d23 $m $d45 $s";
return @"$hours $h $minutes $m $seconds $s";
} else if (minutes > 0) {
return @"$d23 $m $d45 $s";
return @"$minutes $m $seconds $s";
} else {
return @"$d45 $s";
return @"$seconds $s";
}
}

Expand Down Expand Up @@ -75,23 +62,11 @@ namespace Tasks {
return tmp;
}

public static int64 to_seconds(string input) {
var tmp = "";
if (input.length < LENGTH) {
for (int i = 0; i < LENGTH - input.length; i++) {
tmp = tmp + ZERO;
}
}
tmp = tmp + input;

var d01 = tmp.substring(0, 2);
var d23 = tmp.substring(2, 2);
var d45 = tmp.substring(4, 2);

public static int64 to_seconds(int hours, int minutes, int seconds) {
int64 secs = 0;
secs += int.parse(d01) * HOUR;
secs += int.parse(d23) * MINUTE;
secs += int.parse(d45);
secs += hours * HOUR;
secs += minutes * MINUTE;
secs += seconds;

// Logger.log(@"to_seconds: tmp -> $tmp, secs -> $secs");

Expand Down
80 changes: 61 additions & 19 deletions src/ui/TimerView.vala
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
namespace Tasks {

public class TimerView : Gtk.EventBox {

delegate void IntType (int val);

private Gtk.Label label;
private Gtk.Popover? popover;

private string input = "";
private int seconds = 0;
private int hours = 0;
private int minutes = 0;
private string re_presentation = "";

public TimerView() {
Expand All @@ -26,40 +31,77 @@ namespace Tasks {
date_button.hexpand = false;
date_button.set_always_show_image(true);
date_button.get_style_context().add_class("icon_button");
date_button.clicked.connect (() => {
show_picker(date_button);
});

timer_field.add(date_button);
add(timer_field);
}

private void show_picker(Gtk.Widget parent) {
Gtk.Grid time_grid = new Gtk.Grid();
time_grid.column_spacing = 4;
time_grid.orientation = Gtk.Orientation.HORIZONTAL;

var seconds_view = create_spin_button(0, 99, 1, (val) => {
seconds = val;
create_label();
});

var hours_view = create_spin_button(0, 99, 1, (val) => {
hours = val;
create_label();
});

var minutes_view = create_spin_button(0, 99, 1, (val) => {
minutes = val;
create_label();
});

time_grid.add(hours_view);
time_grid.add(new Gtk.Label(":"));
time_grid.add(minutes_view);
time_grid.add(new Gtk.Label(":"));
time_grid.add(seconds_view);

time_grid.show_all();

seconds_view.set_value(seconds);
hours_view.set_value(hours);
minutes_view.set_value(minutes);

popover = new Gtk.Popover (parent);
popover.add (time_grid);
popover.get_style_context().add_class("popover");
popover.popup();
}

public int64 get_seconds() {
return Utils.to_seconds(input);
return Utils.to_seconds(hours, minutes, seconds);
}

public void set_seconds(int64 seconds) {
input = Utils.from_seconds(seconds);
this.hours = (int) (seconds / Utils.HOUR);
this.minutes = (int) ((seconds - (this.hours * Utils.HOUR)) / Utils.MINUTE);
this.seconds = (int) (seconds - (this.hours * Utils.HOUR) - (this.minutes * Utils.MINUTE));
create_label();
}

private void add_digit(string digit) {
if (input.length >= Utils.LENGTH) {
return;
}
input = input + digit;
create_label();
}
private Gtk.SpinButton create_spin_button(int from, int to, int step, owned IntType action) {
var spin = new Gtk.SpinButton.with_range(from, to, step);
spin.orientation = Gtk.Orientation.VERTICAL;
spin.get_style_context().add_class("time_button");
spin.value_changed.connect (() => {
action(spin.get_value_as_int());
});
return spin;
}

private void create_label() {
re_presentation = Utils.to_label(input);
re_presentation = Utils.to_label(hours, minutes, seconds);
Logger.log(@"create_label: re_p -> $re_presentation");
label.set_label(re_presentation);
}

private void remove_digit() {
if (input.length == 0) {
return;
}
input = input.substring(0, input.length - 1);
create_label();
}
}
}

0 comments on commit 73bb1b6

Please sign in to comment.