Skip to content

Commit f197c31

Browse files
authored
Merge pull request #1653 from alainm23/fix/1447
Simplify time label formatting logic
2 parents 7a93c8c + 47284a6 commit f197c31

File tree

1 file changed

+115
-114
lines changed

1 file changed

+115
-114
lines changed

src/Widgets/EventRow.vala

Lines changed: 115 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,118 +1,119 @@
11
/*
2-
* Copyright © 2023 Alain M. (https://github.com/alainm23/planify)
3-
*
4-
* This program is free software; you can redistribute it and/or
5-
* modify it under the terms of the GNU General Public
6-
* License as published by the Free Software Foundation; either
7-
* version 3 of the License, or (at your option) any later version.
8-
*
9-
* This program is distributed in the hope that it will be useful,
10-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12-
* General Public License for more details.
13-
*
14-
* You should have received a copy of the GNU General Public
15-
* License along with this program; if not, write to the
16-
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17-
* Boston, MA 02110-1301 USA
18-
*
19-
* Authored by: Alain M. <[email protected]>
20-
*/
2+
* Copyright © 2023 Alain M. (https://github.com/alainm23/planify)
3+
*
4+
* This program is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU General Public
6+
* License as published by the Free Software Foundation; either
7+
* version 3 of the License, or (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public
15+
* License along with this program; if not, write to the
16+
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17+
* Boston, MA 02110-1301 USA
18+
*
19+
* Authored by: Alain M. <[email protected]>
20+
*/
2121

2222
public class Widgets.EventRow : Gtk.ListBoxRow {
23-
public unowned ICal.Component component { get; construct; }
24-
public unowned E.SourceCalendar cal { get; construct; }
25-
public E.Source source { get; set; }
26-
27-
public GLib.DateTime start_time { get; private set; }
28-
public GLib.DateTime? end_time { get; private set; }
29-
public bool is_allday { get; private set; default = false; }
30-
31-
private Gtk.Grid color_grid;
32-
private Gtk.Label time_label;
33-
34-
public EventRow (ICal.Component component, E.Source source) {
35-
Object (
36-
component: component,
37-
cal: (E.SourceCalendar?) source.get_extension (E.SOURCE_EXTENSION_CALENDAR)
38-
);
39-
}
40-
41-
construct {
42-
add_css_class ("no-selectable");
43-
add_css_class ("transition");
44-
45-
var dt_start = component.get_dtstart ();
46-
end_time = CalendarEventsUtil.ical_to_date_time (component.get_dtend ());
47-
48-
if (dt_start.is_date ()) {
49-
// Don't convert timezone for date with only day info, leave it at midnight UTC
50-
start_time = CalendarEventsUtil.ical_to_date_time (dt_start);
51-
} else {
52-
start_time = CalendarEventsUtil.ical_to_date_time (dt_start).to_local ();
53-
}
54-
55-
var dt_end = component.get_dtend ();
56-
if (dt_end.is_date ()) {
57-
// Don't convert timezone for date with only day info, leave it at midnight UTC
58-
end_time = CalendarEventsUtil.ical_to_date_time (dt_end);
59-
} else {
60-
end_time = CalendarEventsUtil.ical_to_date_time (dt_end).to_local ();
61-
}
62-
63-
if (end_time != null && CalendarEventsUtil.is_the_all_day (start_time, end_time)) {
64-
is_allday = true;
65-
}
66-
67-
color_grid = new Gtk.Grid () {
68-
width_request = 3,
69-
height_request = 12,
70-
valign = Gtk.Align.CENTER,
71-
halign = Gtk.Align.CENTER,
72-
css_classes = { "event-bar" }
73-
};
74-
75-
time_label = new Gtk.Label (null) {
76-
xalign = 0,
77-
valign = Gtk.Align.CENTER,
78-
width_chars = 7,
79-
css_classes = { "dim-label", "caption" }
80-
};
81-
82-
var name_label = new Gtk.Label (component.get_summary ()) {
83-
valign = Gtk.Align.CENTER,
84-
ellipsize = Pango.EllipsizeMode.END,
85-
wrap = true,
86-
use_markup = true,
87-
wrap_mode = Pango.WrapMode.WORD_CHAR,
88-
margin_start = 3,
89-
css_classes = { "caption" }
90-
};
91-
92-
var grid = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 6) {
93-
margin_top = 3,
94-
margin_bottom = 3
95-
};
96-
97-
if (!is_allday) {
98-
grid.append (time_label);
99-
}
100-
101-
grid.append (color_grid);
102-
grid.append (name_label);
103-
104-
child = grid;
105-
106-
update_color ();
107-
cal.notify["color"].connect (update_color);
108-
update_timelabel ();
109-
}
110-
111-
private void update_timelabel () {
112-
time_label.label = "%s".printf (start_time.format ("%I:%M %p"));
113-
}
114-
115-
private void update_color () {
116-
Util.get_default ().set_widget_color (cal.dup_color (), color_grid);
117-
}
23+
public unowned ICal.Component component { get; construct; }
24+
public unowned E.SourceCalendar cal { get; construct; }
25+
public E.Source source { get; set; }
26+
27+
public GLib.DateTime start_time { get; private set; }
28+
public GLib.DateTime ? end_time { get; private set; }
29+
public bool is_allday { get; private set; default = false; }
30+
31+
private Gtk.Grid color_grid;
32+
private Gtk.Label time_label;
33+
34+
public EventRow (ICal.Component component, E.Source source) {
35+
Object (
36+
component : component,
37+
cal: (E.SourceCalendar ?) source.get_extension (E.SOURCE_EXTENSION_CALENDAR)
38+
);
39+
}
40+
41+
construct {
42+
add_css_class ("no-selectable");
43+
add_css_class ("transition");
44+
45+
var dt_start = component.get_dtstart ();
46+
end_time = CalendarEventsUtil.ical_to_date_time (component.get_dtend ());
47+
48+
if (dt_start.is_date ()) {
49+
// Don't convert timezone for date with only day info, leave it at midnight UTC
50+
start_time = CalendarEventsUtil.ical_to_date_time (dt_start);
51+
} else {
52+
start_time = CalendarEventsUtil.ical_to_date_time (dt_start).to_local ();
53+
}
54+
55+
var dt_end = component.get_dtend ();
56+
if (dt_end.is_date ()) {
57+
// Don't convert timezone for date with only day info, leave it at midnight UTC
58+
end_time = CalendarEventsUtil.ical_to_date_time (dt_end);
59+
} else {
60+
end_time = CalendarEventsUtil.ical_to_date_time (dt_end).to_local ();
61+
}
62+
63+
if (end_time != null && CalendarEventsUtil.is_the_all_day (start_time, end_time)) {
64+
is_allday = true;
65+
}
66+
67+
color_grid = new Gtk.Grid () {
68+
width_request = 3,
69+
height_request = 12,
70+
valign = Gtk.Align.CENTER,
71+
halign = Gtk.Align.CENTER,
72+
css_classes = { "event-bar" }
73+
};
74+
75+
time_label = new Gtk.Label (null) {
76+
xalign = 0,
77+
valign = Gtk.Align.CENTER,
78+
css_classes = { "dim-label", "caption" }
79+
};
80+
81+
var name_label = new Gtk.Label (component.get_summary ()) {
82+
valign = Gtk.Align.CENTER,
83+
ellipsize = Pango.EllipsizeMode.END,
84+
wrap = true,
85+
use_markup = true,
86+
wrap_mode = Pango.WrapMode.WORD_CHAR,
87+
margin_start = 3,
88+
css_classes = { "caption" }
89+
};
90+
91+
var grid = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 6) {
92+
margin_top = 3,
93+
margin_bottom = 3
94+
};
95+
96+
if (!is_allday) {
97+
grid.append (time_label);
98+
}
99+
100+
grid.append (color_grid);
101+
grid.append (name_label);
102+
103+
child = grid;
104+
105+
update_color ();
106+
cal.notify["color"].connect (update_color);
107+
update_timelabel ();
108+
Services.Settings.get_default ().settings.changed["clock-format"].connect (update_timelabel);
109+
}
110+
111+
private void update_timelabel () {
112+
string format = Utils.Datetime.is_clock_format_12h () ? "%I:%M %p" : "%H:%M";
113+
time_label.label = start_time.format (format);
114+
}
115+
116+
private void update_color () {
117+
Util.get_default ().set_widget_color (cal.dup_color (), color_grid);
118+
}
118119
}

0 commit comments

Comments
 (0)