From 6f7938949af240b24e3bddce8f4c18ff7d0f73b2 Mon Sep 17 00:00:00 2001 From: Jeffy Chen Date: Wed, 15 Jun 2022 11:02:44 +0800 Subject: [PATCH 70/74] desktop-shell: Support clock without date Tested with: [shell] clock-with-date=false Signed-off-by: Jeffy Chen --- clients/desktop-shell.c | 73 ++++++++++++++++++++++++++++------------- 1 file changed, 50 insertions(+), 23 deletions(-) diff --git a/clients/desktop-shell.c b/clients/desktop-shell.c index a81eb0a..b2414be 100644 --- a/clients/desktop-shell.c +++ b/clients/desktop-shell.c @@ -75,6 +75,7 @@ struct desktop { int want_panel; enum weston_desktop_shell_panel_position panel_position; enum clock_format clock_format; + bool clock_with_date; struct window *grab_window; struct widget *grab_widget; @@ -108,6 +109,7 @@ struct panel { int painted; enum weston_desktop_shell_panel_position panel_position; enum clock_format clock_format; + bool clock_with_date; uint32_t color; double scale; }; @@ -154,7 +156,7 @@ struct panel_clock { struct widget *widget; struct panel *panel; struct toytimer timer; - char *format_string; + char format_string[128]; time_t refresh_timer; }; @@ -167,6 +169,36 @@ struct unlock_dialog { struct desktop *desktop; }; +static int +clock_get_preferred_width(enum clock_format clock_format, bool with_date) +{ + int width; + + switch (clock_format) { + case CLOCK_FORMAT_NONE: + return 0; + case CLOCK_FORMAT_MINUTES: + width = 160; + break; + case CLOCK_FORMAT_MINUTES_24H: + width = 130; + break; + case CLOCK_FORMAT_SECONDS: + width = 180; + break; + case CLOCK_FORMAT_SECONDS_24H: + width = 160; + break; + default: + assert(!"not reached"); + } + + if (!with_date) + width -= 80; + + return width; +} + static void panel_add_launchers(struct panel *panel, struct desktop *desktop); @@ -413,7 +445,7 @@ panel_clock_redraw_handler(struct widget *widget, void *data) cairo_text_extents(cr, string, &extents); if (allocation.x > 0) allocation.x += - allocation.width - spacing * 1.5 - extents.width; + allocation.width - spacing - extents.width; else allocation.x += allocation.width / 2 - extents.width / 2; @@ -464,21 +496,24 @@ panel_add_clock(struct panel *panel) clock->panel = panel; panel->clock = clock; + if (panel->clock_with_date) + strcpy(clock->format_string, "%a %b %d, "); + switch (panel->clock_format) { case CLOCK_FORMAT_MINUTES: - clock->format_string = "%a %b %d, %I:%M %p"; + strcat(clock->format_string, "%I:%M %p"); clock->refresh_timer = 60; break; case CLOCK_FORMAT_SECONDS: - clock->format_string = "%a %b %d, %I:%M:%S %p"; + strcat(clock->format_string, "%I:%M:%S %p"); clock->refresh_timer = 1; break; case CLOCK_FORMAT_MINUTES_24H: - clock->format_string = "%a %b %d, %H:%M"; + strcat(clock->format_string, "%H:%M"); clock->refresh_timer = 60; break; case CLOCK_FORMAT_SECONDS_24H: - clock->format_string = "%a %b %d, %H:%M:%S"; + strcat(clock->format_string, "%H:%M:%S"); clock->refresh_timer = 1; break; case CLOCK_FORMAT_NONE: @@ -519,10 +554,8 @@ panel_resize_handler(struct widget *widget, first_pad_h = first_pad_w = 0; } - if (panel->clock_format == CLOCK_FORMAT_SECONDS) - w = 170 * scale; - else /* CLOCK_FORMAT_MINUTES and 24H versions */ - w = 150 * scale; + w = clock_get_preferred_width(panel->clock_format, + panel->clock_with_date) * scale; if (horizontal) x = width - w; @@ -563,19 +596,9 @@ panel_configure(void *data, break; case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT: case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT: - switch (desktop->clock_format) { - case CLOCK_FORMAT_NONE: - width = 32 * panel->scale; - break; - case CLOCK_FORMAT_MINUTES: - case CLOCK_FORMAT_MINUTES_24H: - case CLOCK_FORMAT_SECONDS_24H: - width = 150 * panel->scale; - break; - case CLOCK_FORMAT_SECONDS: - width = 170 * panel->scale; - break; - } + width = clock_get_preferred_width(desktop->clock_format, + desktop->clock_with_date); + width = MAX(32, width) * panel->scale; break; } window_schedule_resize(panel->window, width, height); @@ -637,6 +660,7 @@ panel_create(struct desktop *desktop, struct output *output) panel->panel_position = desktop->panel_position; panel->clock_format = desktop->clock_format; + panel->clock_with_date = desktop->clock_with_date; if (panel->clock_format != CLOCK_FORMAT_NONE) panel_add_clock(panel); @@ -1556,6 +1580,9 @@ parse_clock_format(struct desktop *desktop, struct weston_config_section *s) else desktop->clock_format = DEFAULT_CLOCK_FORMAT; free(clock_format); + + weston_config_section_get_bool(s, "clock-with-date", + &desktop->clock_with_date, true); } int main(int argc, char *argv[]) -- 2.20.1