hc
2023-02-13 e440ec23c5a540cdd3f7464e8779219be6fd3d95
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
From 6f7938949af240b24e3bddce8f4c18ff7d0f73b2 Mon Sep 17 00:00:00 2001
From: Jeffy Chen <jeffy.chen@rock-chips.com>
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 <jeffy.chen@rock-chips.com>
---
 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