1From d7c6d93ed8b27f2dc12dd4f918a255318df505c4 Mon Sep 17 00:00:00 2001 2From: Jeffy Chen <jeffy.chen@rock-chips.com> 3Date: Wed, 15 Jun 2022 11:02:44 +0800 4Subject: [PATCH 70/79] desktop-shell: Support clock without date 5 6Tested with: 7[shell] 8clock-with-date=false 9 10Signed-off-by: Jeffy Chen <jeffy.chen@rock-chips.com> 11--- 12 clients/desktop-shell.c | 73 ++++++++++++++++++++++++++++------------- 13 1 file changed, 50 insertions(+), 23 deletions(-) 14 15diff --git a/clients/desktop-shell.c b/clients/desktop-shell.c 16index a81eb0a..b2414be 100644 17--- a/clients/desktop-shell.c 18+++ b/clients/desktop-shell.c 19@@ -75,6 +75,7 @@ struct desktop { 20 int want_panel; 21 enum weston_desktop_shell_panel_position panel_position; 22 enum clock_format clock_format; 23+ bool clock_with_date; 24 25 struct window *grab_window; 26 struct widget *grab_widget; 27@@ -108,6 +109,7 @@ struct panel { 28 int painted; 29 enum weston_desktop_shell_panel_position panel_position; 30 enum clock_format clock_format; 31+ bool clock_with_date; 32 uint32_t color; 33 double scale; 34 }; 35@@ -154,7 +156,7 @@ struct panel_clock { 36 struct widget *widget; 37 struct panel *panel; 38 struct toytimer timer; 39- char *format_string; 40+ char format_string[128]; 41 time_t refresh_timer; 42 }; 43 44@@ -167,6 +169,36 @@ struct unlock_dialog { 45 struct desktop *desktop; 46 }; 47 48+static int 49+clock_get_preferred_width(enum clock_format clock_format, bool with_date) 50+{ 51+ int width; 52+ 53+ switch (clock_format) { 54+ case CLOCK_FORMAT_NONE: 55+ return 0; 56+ case CLOCK_FORMAT_MINUTES: 57+ width = 160; 58+ break; 59+ case CLOCK_FORMAT_MINUTES_24H: 60+ width = 130; 61+ break; 62+ case CLOCK_FORMAT_SECONDS: 63+ width = 180; 64+ break; 65+ case CLOCK_FORMAT_SECONDS_24H: 66+ width = 160; 67+ break; 68+ default: 69+ assert(!"not reached"); 70+ } 71+ 72+ if (!with_date) 73+ width -= 80; 74+ 75+ return width; 76+} 77+ 78 static void 79 panel_add_launchers(struct panel *panel, struct desktop *desktop); 80 81@@ -413,7 +445,7 @@ panel_clock_redraw_handler(struct widget *widget, void *data) 82 cairo_text_extents(cr, string, &extents); 83 if (allocation.x > 0) 84 allocation.x += 85- allocation.width - spacing * 1.5 - extents.width; 86+ allocation.width - spacing - extents.width; 87 else 88 allocation.x += 89 allocation.width / 2 - extents.width / 2; 90@@ -464,21 +496,24 @@ panel_add_clock(struct panel *panel) 91 clock->panel = panel; 92 panel->clock = clock; 93 94+ if (panel->clock_with_date) 95+ strcpy(clock->format_string, "%a %b %d, "); 96+ 97 switch (panel->clock_format) { 98 case CLOCK_FORMAT_MINUTES: 99- clock->format_string = "%a %b %d, %I:%M %p"; 100+ strcat(clock->format_string, "%I:%M %p"); 101 clock->refresh_timer = 60; 102 break; 103 case CLOCK_FORMAT_SECONDS: 104- clock->format_string = "%a %b %d, %I:%M:%S %p"; 105+ strcat(clock->format_string, "%I:%M:%S %p"); 106 clock->refresh_timer = 1; 107 break; 108 case CLOCK_FORMAT_MINUTES_24H: 109- clock->format_string = "%a %b %d, %H:%M"; 110+ strcat(clock->format_string, "%H:%M"); 111 clock->refresh_timer = 60; 112 break; 113 case CLOCK_FORMAT_SECONDS_24H: 114- clock->format_string = "%a %b %d, %H:%M:%S"; 115+ strcat(clock->format_string, "%H:%M:%S"); 116 clock->refresh_timer = 1; 117 break; 118 case CLOCK_FORMAT_NONE: 119@@ -519,10 +554,8 @@ panel_resize_handler(struct widget *widget, 120 first_pad_h = first_pad_w = 0; 121 } 122 123- if (panel->clock_format == CLOCK_FORMAT_SECONDS) 124- w = 170 * scale; 125- else /* CLOCK_FORMAT_MINUTES and 24H versions */ 126- w = 150 * scale; 127+ w = clock_get_preferred_width(panel->clock_format, 128+ panel->clock_with_date) * scale; 129 130 if (horizontal) 131 x = width - w; 132@@ -563,19 +596,9 @@ panel_configure(void *data, 133 break; 134 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT: 135 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT: 136- switch (desktop->clock_format) { 137- case CLOCK_FORMAT_NONE: 138- width = 32 * panel->scale; 139- break; 140- case CLOCK_FORMAT_MINUTES: 141- case CLOCK_FORMAT_MINUTES_24H: 142- case CLOCK_FORMAT_SECONDS_24H: 143- width = 150 * panel->scale; 144- break; 145- case CLOCK_FORMAT_SECONDS: 146- width = 170 * panel->scale; 147- break; 148- } 149+ width = clock_get_preferred_width(desktop->clock_format, 150+ desktop->clock_with_date); 151+ width = MAX(32, width) * panel->scale; 152 break; 153 } 154 window_schedule_resize(panel->window, width, height); 155@@ -637,6 +660,7 @@ panel_create(struct desktop *desktop, struct output *output) 156 157 panel->panel_position = desktop->panel_position; 158 panel->clock_format = desktop->clock_format; 159+ panel->clock_with_date = desktop->clock_with_date; 160 if (panel->clock_format != CLOCK_FORMAT_NONE) 161 panel_add_clock(panel); 162 163@@ -1556,6 +1580,9 @@ parse_clock_format(struct desktop *desktop, struct weston_config_section *s) 164 else 165 desktop->clock_format = DEFAULT_CLOCK_FORMAT; 166 free(clock_format); 167+ 168+ weston_config_section_get_bool(s, "clock-with-date", 169+ &desktop->clock_with_date, true); 170 } 171 172 int main(int argc, char *argv[]) 173-- 1742.20.1 175 176