-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
-
30
-
31
-
32
-
33
-
34
-
35
-
36
-
37
-
38
-
39
-
40
-
41
-
42
-
43
-
44
-
45
-
46
-
47
-
48
-
49
-
50
-
51
-
52
-
53
-
54
-
55
-
56
-
57
-
58
-
59
-
60
-
61
-
62
-
63
-
64
-
65
-
66
-
67
-
68
-
69
-
70
-
71
-
72
-
73
-
74
-
75
-
76
-
77
-
78
-
79
-
80
-
81
-
82
-
83
-
84
-
85
-
86
-
87
-
88
-
89
-
90
-
91
-
92
-
93
-
94
-
95
// Copyright 2025 Shota FUJI
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//
// SPDX-License-Identifier: MPL-2.0
using GLib;
namespace TimeTracker.Widget {
private class TimerListRow : Gtk.Box {
/**
* User requested to stop the timer.
*/
public signal void stop(Model.Timer timer);
public Model.Timer? timer { get; set construct; }
private Binding? timer_binding = null;
private Gtk.Label title = new Gtk.Label("");
private Gtk.Label elapsed = new Gtk.Label("--:--:--");
private Gtk.Button stop_button = new Gtk.Button();
construct {
this.notify["timer"].connect(() => {
if (timer_binding != null) {
timer_binding.unbind();
timer_binding = null;
}
if (timer != null) {
stop_button.visible = true;
timer_binding = timer.bind_property("title", title, "label", SYNC_CREATE);
update_elapsed();
if (!timer.is_stopped) {
Timeout.add(1000, () => {
update_elapsed();
return !timer.is_stopped;
});
} else {
stop_button.visible = false;
}
}
});
this.margin_top = 16;
this.margin_bottom = this.margin_top;
this.margin_start = 12;
this.margin_end = this.margin_start;
this.spacing = 8;
var info_col = new Gtk.Box(VERTICAL, 4);
info_col.hexpand = true;
info_col.hexpand_set = true;
title.halign = START;
title.css_classes = { "heading" };
info_col.append(title);
elapsed.halign = START;
elapsed.css_classes = { "numeric" };
info_col.append(elapsed);
this.append(info_col);
stop_button.label = "Stop";
stop_button.visible = false;
stop_button.clicked.connect(() => {
if (timer != null) {
this.stop(timer);
}
});
this.append(stop_button);
}
private void update_elapsed() {
var start = new DateTime.from_unix_utc(timer.started_at);
var end = timer.is_stopped ? new DateTime.from_unix_utc(timer.stopped_at) : new DateTime.now_utc();
var diff = end.difference(start);
elapsed.label = "%02d:%02d:%02d".printf(
(int) (diff / TimeSpan.HOUR),
(int) (diff / TimeSpan.MINUTE) % 60,
(int) (diff / TimeSpan.SECOND) % 60
);
}
}
}