From b4f413826c73d5693600d8786a1fbb830d7f20b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Backenk=C3=B6hler?= Date: Sat, 21 May 2022 13:21:10 +0200 Subject: [PATCH] Display current laptime in stopwatch The running time of the current lap is displayed under the total elapsed time once the "lap" button is clicked. On reset the lap time disappears again. See ticket #41 --- data/css/gnome-clocks.css | 5 +++ data/ui/stopwatch-face.ui | 80 ++++++++++++++++++++++++++++++++++++++- src/stopwatch-face.vala | 66 ++++++++++++++++++++++++++++++-- 3 files changed, 147 insertions(+), 4 deletions(-) diff --git a/data/css/gnome-clocks.css b/data/css/gnome-clocks.css index 9afdf460..8511562c 100644 --- a/data/css/gnome-clocks.css +++ b/data/css/gnome-clocks.css @@ -88,6 +88,11 @@ font-weight: 300; } +.lap-stopwatch label { + font-size: 30px; + font-weight: 300; +} + /* World */ .clock-location { diff --git a/data/ui/stopwatch-face.ui b/data/ui/stopwatch-face.ui index 48b00ee1..d99ad11b 100644 --- a/data/ui/stopwatch-face.ui +++ b/data/ui/stopwatch-face.ui @@ -17,7 +17,6 @@ center - 18 00 @@ -90,6 +89,85 @@ + + + crossfade + + + center + 18 + + + 00 + 0 + 0 + + + + + + + + + + + 00 + 0 + 0 + + + + + + + 0 + 0 + + + + + 00 + 0 + 0 + + + + + + . + 0 + 0 + + + + + 4 + end + 0 + 0 + 0 + + + + + + + + 12 diff --git a/src/stopwatch-face.vala b/src/stopwatch-face.vala index 75467000..1d235a52 100644 --- a/src/stopwatch-face.vala +++ b/src/stopwatch-face.vala @@ -51,6 +51,10 @@ public class Face : Gtk.Box, Clocks.Clock { private int stored_minute; private int stored_second; double stored_milisecond; + private int stored_lap_hour; + private int stored_lap_minute; + private int stored_lap_second; + double stored_lap_milisecond; private int current_lap; [GtkChild] @@ -64,6 +68,19 @@ public class Face : Gtk.Box, Clocks.Clock { [GtkChild] private unowned Gtk.Box time_container; + [GtkChild] + private unowned Gtk.Label lap_hours_label; + [GtkChild] + private unowned Gtk.Label lap_minutes_label; + [GtkChild] + private unowned Gtk.Label lap_seconds_label; + [GtkChild] + private unowned Gtk.Label lap_miliseconds_label; + [GtkChild] + private unowned Gtk.Box lap_time_container; + + [GtkChild] + private unowned Gtk.Revealer lap_time_revealer; [GtkChild] private unowned Gtk.Revealer laps_revealer; @@ -86,6 +103,7 @@ public class Face : Gtk.Box, Clocks.Clock { tick_id = 0; time_container.set_direction (Gtk.TextDirection.LTR); + lap_time_container.set_direction (Gtk.TextDirection.LTR); laps_list.bind_model (laps, (lap) => { var total_items = laps.get_n_items (); @@ -191,6 +209,7 @@ public class Face : Gtk.Box, Clocks.Clock { private void reset () { laps_revealer.set_reveal_child (false); + lap_time_revealer.set_reveal_child (false); timer.reset (); state = State.RESET; @@ -220,11 +239,15 @@ public class Face : Gtk.Box, Clocks.Clock { return total; } + private double current_lap_duration () { + var e = timer.elapsed (); + return e - this.total_laps_duration (); + } + private void lap () { current_lap += 1; - laps_revealer.reveal_child = current_lap >= 1; - var e = timer.elapsed (); - double lap_duration = e - this.total_laps_duration (); + laps_revealer.reveal_child = lap_time_revealer.reveal_child = current_lap >= 1; + double lap_duration = current_lap_duration (); var lap = new Lap (current_lap, lap_duration); laps.insert (0, lap); } @@ -274,9 +297,46 @@ public class Face : Gtk.Box, Clocks.Clock { stored_milisecond = ds; } + if (current_lap >= 1) { + update_lap_time_label (); + } + return true; } + private void update_lap_time_label () { + int h = 0; + int m = 0; + int s = 0; + double r = 0; + if (state != State.RESET) { + double lap_duration = current_lap_duration (); + Utils.time_to_hms (lap_duration, out h, out m, out s, out r); + } + + int ds = (int) (r * 10); + + // Note that the format uses unicode RATIO character + // We also prepend the LTR mark to make sure text is always in this direction + if (stored_lap_hour != h) { + lap_hours_label.label = "%02i\u200E".printf (h); + stored_lap_hour = h; + } + if (stored_lap_minute != m) { + lap_minutes_label.label = "%02i\u200E".printf (m); + stored_lap_minute = m; + } + if (stored_lap_second != s) { + lap_seconds_label.label = "%02i".printf (s); + stored_lap_second = s; + } + if (stored_lap_milisecond != ds) { + lap_miliseconds_label.label = "%i".printf (ds); + stored_lap_milisecond = ds; + } + return; + } + public override bool grab_focus () { start_btn.grab_focus (); return true; -- GitLab