Skip to content

Commit 9a36755

Browse files
vkarehmonsta
authored andcommitted
typing-break: Make images larger and load as surface
1 parent 08866fc commit 9a36755

File tree

7 files changed

+100
-69
lines changed

7 files changed

+100
-69
lines changed

‎typing-break/bar-disabled.png‎

6.29 KB
Loading

‎typing-break/bar-green.png‎

3.38 KB
Loading

‎typing-break/bar-red.png‎

3.07 KB
Loading

‎typing-break/bar.png‎

4.12 KB
Loading

‎typing-break/drw-break-window.c‎

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ static void postpone_clicked_cb (GtkWidget *button,
7777
static gboolean label_draw_event_cb (GtkLabel *label,
7878
cairo_t *cr,
7979
gpointer user_data);
80-
static void label_size_request_cb (GtkLabel *label,
81-
GtkRequisition *requisition,
80+
static void label_size_allocate_cb (GtkLabel *label,
81+
GdkRectangle *allocation,
8282
gpointer user_data);
8383

8484
G_DEFINE_TYPE (DrwBreakWindow, drw_break_window, GTK_TYPE_WINDOW)
@@ -131,6 +131,7 @@ drw_break_window_init (DrwBreakWindow *window)
131131
GdkRectangle monitor;
132132
gint right_padding;
133133
gint bottom_padding;
134+
gint scale;
134135
GSettings *settings;
135136

136137
priv = DRW_BREAK_WINDOW_GET_PRIVATE (window);
@@ -143,25 +144,26 @@ drw_break_window_init (DrwBreakWindow *window)
143144
allow_postpone = g_settings_get_boolean (settings, "allow-postpone");
144145
g_object_unref (settings);
145146

146-
g_object_set (window, "type", GTK_WINDOW_POPUP, NULL);
147147
gtk_window_set_keep_above (GTK_WINDOW (window), TRUE);
148148
gtk_window_fullscreen (GTK_WINDOW (window));
149149
gtk_window_set_modal (GTK_WINDOW (window), TRUE);
150150

151151
screen = gdk_screen_get_default ();
152152
display = gdk_screen_get_display (screen);
153+
scale = gtk_widget_get_scale_factor (GTK_WIDGET (window));
154+
153155
gdk_monitor_get_geometry (gdk_display_get_monitor (display, root_monitor), &monitor);
154156

155157
gtk_window_set_default_size (GTK_WINDOW (window),
156-
WidthOfScreen (gdk_x11_screen_get_xscreen (screen)),
157-
HeightOfScreen (gdk_x11_screen_get_xscreen (screen)));
158+
WidthOfScreen (gdk_x11_screen_get_xscreen (screen)) / scale,
159+
HeightOfScreen (gdk_x11_screen_get_xscreen (screen)) / scale);
158160

159161
gtk_window_set_decorated (GTK_WINDOW (window), FALSE);
160162
gtk_widget_set_app_paintable (GTK_WIDGET (window), TRUE);
161163
drw_setup_background (GTK_WIDGET (window));
162164

163-
right_padding = WidthOfScreen (gdk_x11_screen_get_xscreen (screen)) - monitor.width - monitor.x;
164-
bottom_padding = HeightOfScreen (gdk_x11_screen_get_xscreen (screen)) - monitor.height - monitor.y;
165+
right_padding = WidthOfScreen (gdk_x11_screen_get_xscreen (screen)) / scale - monitor.width - monitor.x;
166+
bottom_padding = HeightOfScreen (gdk_x11_screen_get_xscreen (screen)) / scale - monitor.height - monitor.y;
165167

166168
outer_vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
167169
gtk_widget_set_hexpand (outer_vbox, TRUE);
@@ -234,8 +236,8 @@ drw_break_window_init (DrwBreakWindow *window)
234236
NULL);
235237

236238
g_signal_connect_after (priv->break_label,
237-
"size_request",
238-
G_CALLBACK (label_size_request_cb),
239+
"size-allocate",
240+
G_CALLBACK (label_size_allocate_cb),
239241
NULL);
240242

241243
str = g_strdup_printf ("<span size=\"xx-large\" foreground=\"white\"><b>%s</b></span>",
@@ -256,8 +258,8 @@ drw_break_window_init (DrwBreakWindow *window)
256258
NULL);
257259

258260
g_signal_connect_after (priv->clock_label,
259-
"size_request",
260-
G_CALLBACK (label_size_request_cb),
261+
"size-allocate",
262+
G_CALLBACK (label_size_allocate_cb),
261263
NULL);
262264

263265
gtk_window_stick (GTK_WINDOW (window));
@@ -540,9 +542,11 @@ get_layout_location (GtkLabel *label,
540542
gint x, y;
541543
gint xpad, ypad;
542544
gint margin_start, margin_end, margin_top, margin_bottom;
545+
gint scale;
543546

544547
widget = GTK_WIDGET (label);
545548

549+
scale = gtk_widget_get_scale_factor (widget);
546550
xalign = gtk_label_get_xalign (GTK_LABEL (label));
547551
yalign = gtk_label_get_yalign (GTK_LABEL (label));
548552
margin_start = gtk_widget_get_margin_start (widget);
@@ -555,6 +559,10 @@ get_layout_location (GtkLabel *label,
555559

556560
gtk_widget_get_allocation (widget, &widget_allocation);
557561
gtk_widget_get_requisition (widget, &widget_requisition);
562+
widget_allocation.x /= scale;
563+
widget_allocation.y /= scale;
564+
widget_requisition.width /= scale;
565+
widget_requisition.height /= scale;
558566

559567
if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
560568
xalign = 1.0 - xalign;
@@ -603,10 +611,10 @@ label_draw_event_cb (GtkLabel *label,
603611
}
604612

605613
static void
606-
label_size_request_cb (GtkLabel *label,
607-
GtkRequisition *requisition,
608-
gpointer user_data)
614+
label_size_allocate_cb (GtkLabel *label,
615+
GdkRectangle *allocation,
616+
gpointer user_data)
609617
{
610-
requisition->width += 1;
611-
requisition->height += 1;
618+
allocation->width += 1;
619+
allocation->height += 1;
612620
}

‎typing-break/drw-utils.c‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,15 @@ set_pixmap_background (GtkWidget *window)
119119
GdkPixbuf *tmp_pixbuf, *pixbuf, *tile_pixbuf;
120120
GdkRectangle rect;
121121
GdkColor color;
122-
gint width, height;
122+
gint width, height, scale;
123123
cairo_t *cr;
124124

125125
gtk_widget_realize (window);
126126

127127
screen = gtk_widget_get_screen (window);
128-
width = WidthOfScreen (gdk_x11_screen_get_xscreen (screen));
129-
height = HeightOfScreen (gdk_x11_screen_get_xscreen (screen));
128+
scale = gtk_widget_get_scale_factor (window);
129+
width = WidthOfScreen (gdk_x11_screen_get_xscreen (screen)) / scale;
130+
height = HeightOfScreen (gdk_x11_screen_get_xscreen (screen)) / scale;
130131

131132
tmp_pixbuf = gdk_pixbuf_get_from_window (gdk_screen_get_root_window (screen),
132133
0,
@@ -160,8 +161,8 @@ set_pixmap_background (GtkWidget *window)
160161
height,
161162
0,
162163
0,
163-
1,
164-
1,
164+
scale,
165+
scale,
165166
GDK_INTERP_NEAREST,
166167
225);
167168

‎typing-break/drwright.c‎

Lines changed: 70 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -69,42 +69,42 @@ typedef enum {
6969

7070
struct _DrWright {
7171
/* Widgets. */
72-
GtkWidget *break_window;
73-
GList *secondary_break_windows;
72+
GtkWidget *break_window;
73+
GList *secondary_break_windows;
7474

75-
DrwMonitor *monitor;
75+
DrwMonitor *monitor;
7676

77-
GtkUIManager *ui_manager;
77+
GtkUIManager *ui_manager;
7878

79-
DrwState state;
80-
DrwTimer *timer;
81-
DrwTimer *idle_timer;
79+
DrwState state;
80+
DrwTimer *timer;
81+
DrwTimer *idle_timer;
8282

83-
gint last_elapsed_time;
84-
gint save_last_time;
83+
gint last_elapsed_time;
84+
gint save_last_time;
8585

8686
/* Time settings. */
87-
gint type_time;
88-
gint break_time;
89-
gint warn_time;
87+
gint type_time;
88+
gint break_time;
89+
gint warn_time;
9090

91-
gboolean enabled;
91+
gboolean enabled;
9292

93-
guint clock_timeout_id;
93+
guint clock_timeout_id;
9494
#ifdef HAVE_APP_INDICATOR
95-
AppIndicator *indicator;
95+
AppIndicator *indicator;
9696
#else
97-
guint blink_timeout_id;
97+
guint blink_timeout_id;
9898

99-
gboolean blink_on;
99+
gboolean blink_on;
100100

101-
GtkStatusIcon *icon;
101+
GtkStatusIcon *icon;
102102

103-
GdkPixbuf *neutral_bar;
104-
GdkPixbuf *red_bar;
105-
GdkPixbuf *green_bar;
106-
GdkPixbuf *disabled_bar;
107-
GdkPixbuf *composite_bar;
103+
cairo_surface_t *neutral_bar;
104+
cairo_surface_t *red_bar;
105+
cairo_surface_t *green_bar;
106+
cairo_surface_t *disabled_bar;
107+
GdkPixbuf *composite_bar;
108108
#endif /* HAVE_APP_INDICATOR */
109109

110110
GtkWidget *warn_dialog;
@@ -175,6 +175,21 @@ update_app_indicator (DrWright *dr)
175175
app_indicator_set_status (dr->indicator, new_status);
176176
}
177177
#else
178+
179+
static void
180+
set_status_icon (GtkStatusIcon *icon, cairo_surface_t *surface)
181+
{
182+
GdkPixbuf *pixbuf;
183+
184+
pixbuf = gdk_pixbuf_get_from_surface (surface, 0, 0,
185+
cairo_image_surface_get_width (surface),
186+
cairo_image_surface_get_height (surface));
187+
188+
gtk_status_icon_set_from_pixbuf (icon, pixbuf);
189+
190+
g_object_unref (pixbuf);
191+
}
192+
178193
static void
179194
update_icon (DrWright *dr)
180195
{
@@ -186,15 +201,16 @@ update_icon (DrWright *dr)
186201
gboolean set_pixbuf;
187202

188203
if (!dr->enabled) {
189-
gtk_status_icon_set_from_pixbuf (dr->icon,
190-
dr->disabled_bar);
204+
set_status_icon (dr->icon, dr->disabled_bar);
191205
return;
192206
}
193207

194-
tmp_pixbuf = gdk_pixbuf_copy (dr->neutral_bar);
208+
width = cairo_image_surface_get_width (dr->neutral_bar);
209+
height = cairo_image_surface_get_height (dr->neutral_bar);
195210

196-
width = gdk_pixbuf_get_width (tmp_pixbuf);
197-
height = gdk_pixbuf_get_height (tmp_pixbuf);
211+
tmp_pixbuf = gdk_pixbuf_get_from_surface (dr->neutral_bar,
212+
0, 0,
213+
width, height);
198214

199215
set_pixbuf = TRUE;
200216

@@ -220,17 +236,17 @@ update_icon (DrWright *dr)
220236

221237
switch (dr->state) {
222238
case STATE_WARN:
223-
pixbuf = dr->red_bar;
239+
pixbuf = gdk_pixbuf_get_from_surface (dr->red_bar, 0, 0, width, height);
224240
set_pixbuf = FALSE;
225241
break;
226242

227243
case STATE_BREAK_SETUP:
228244
case STATE_BREAK:
229-
pixbuf = dr->red_bar;
245+
pixbuf = gdk_pixbuf_get_from_surface (dr->red_bar, 0, 0, width, height);
230246
break;
231247

232248
default:
233-
pixbuf = dr->green_bar;
249+
pixbuf = gdk_pixbuf_get_from_surface (dr->green_bar, 0, 0, width, height);
234250
}
235251

236252
gdk_pixbuf_composite (pixbuf,
@@ -256,6 +272,8 @@ update_icon (DrWright *dr)
256272
}
257273

258274
dr->composite_bar = tmp_pixbuf;
275+
276+
g_object_unref (pixbuf);
259277
}
260278

261279
static gboolean
@@ -272,11 +290,9 @@ blink_timeout_cb (DrWright *dr)
272290
}
273291

274292
if (dr->blink_on || timeout == 0) {
275-
gtk_status_icon_set_from_pixbuf (dr->icon,
276-
dr->composite_bar);
293+
gtk_status_icon_set_from_pixbuf (dr->icon, dr->composite_bar);
277294
} else {
278-
gtk_status_icon_set_from_pixbuf (dr->icon,
279-
dr->neutral_bar);
295+
set_status_icon (dr->icon, dr->neutral_bar);
280296
}
281297

282298
dr->blink_on = !dr->blink_on;
@@ -385,8 +401,7 @@ maybe_change_state (DrWright *dr)
385401
}
386402

387403
#ifndef HAVE_APP_INDICATOR
388-
gtk_status_icon_set_from_pixbuf (dr->icon,
389-
dr->neutral_bar);
404+
set_status_icon (dr->icon, dr->neutral_bar);
390405
#endif /* HAVE_APP_INDICATOR */
391406

392407
dr->save_last_time = 0;
@@ -426,8 +441,7 @@ maybe_change_state (DrWright *dr)
426441

427442
stop_blinking (dr);
428443
#ifndef HAVE_APP_INDICATOR
429-
gtk_status_icon_set_from_pixbuf (dr->icon,
430-
dr->red_bar);
444+
set_status_icon (dr->icon, dr->red_bar);
431445
#endif /* HAVE_APP_INDICATOR */
432446

433447
drw_timer_start (dr->timer);
@@ -470,8 +484,7 @@ maybe_change_state (DrWright *dr)
470484
case STATE_BREAK_DONE_SETUP:
471485
stop_blinking (dr);
472486
#ifndef HAVE_APP_INDICATOR
473-
gtk_status_icon_set_from_pixbuf (dr->icon,
474-
dr->green_bar);
487+
set_status_icon (dr->icon, dr->green_bar);
475488
#endif /* HAVE_APP_INDICATOR */
476489

477490
dr->state = STATE_BREAK_DONE;
@@ -765,7 +778,14 @@ init_app_indicator (DrWright *dr)
765778
static void
766779
init_tray_icon (DrWright *dr)
767780
{
768-
dr->icon = gtk_status_icon_new_from_pixbuf (dr->neutral_bar);
781+
GdkPixbuf *pixbuf;
782+
783+
pixbuf = gdk_pixbuf_get_from_surface (dr->neutral_bar, 0, 0,
784+
cairo_image_surface_get_width (dr->neutral_bar),
785+
cairo_image_surface_get_height (dr->neutral_bar));
786+
787+
dr->icon = gtk_status_icon_new_from_pixbuf (pixbuf);
788+
g_object_unref (pixbuf);
769789

770790
update_status (dr);
771791
update_icon (dr);
@@ -784,6 +804,7 @@ create_secondary_break_windows (void)
784804
GdkScreen *screen;
785805
GtkWidget *window;
786806
GList *windows = NULL;
807+
gint scale;
787808

788809
display = gdk_display_get_default ();
789810

@@ -795,12 +816,13 @@ create_secondary_break_windows (void)
795816
window = gtk_window_new (GTK_WINDOW_POPUP);
796817

797818
windows = g_list_prepend (windows, window);
819+
scale = gtk_widget_get_scale_factor (GTK_WIDGET (window));
798820

799821
gtk_window_set_screen (GTK_WINDOW (window), screen);
800822

801823
gtk_window_set_default_size (GTK_WINDOW (window),
802-
WidthOfScreen (gdk_x11_screen_get_xscreen (screen)),
803-
HeightOfScreen (gdk_x11_screen_get_xscreen (screen)));
824+
WidthOfScreen (gdk_x11_screen_get_xscreen (screen)) / scale,
825+
HeightOfScreen (gdk_x11_screen_get_xscreen (screen)) / scale);
804826

805827
gtk_widget_set_app_paintable (GTK_WIDGET (window), TRUE);
806828
drw_setup_background (GTK_WIDGET (window));
@@ -873,10 +895,10 @@ drwright_new (void)
873895
#ifdef HAVE_APP_INDICATOR
874896
init_app_indicator (dr);
875897
#else
876-
dr->neutral_bar = gdk_pixbuf_new_from_file (IMAGEDIR "/bar.png", NULL);
877-
dr->red_bar = gdk_pixbuf_new_from_file (IMAGEDIR "/bar-red.png", NULL);
878-
dr->green_bar = gdk_pixbuf_new_from_file (IMAGEDIR "/bar-green.png", NULL);
879-
dr->disabled_bar = gdk_pixbuf_new_from_file (IMAGEDIR "/bar-disabled.png", NULL);
898+
dr->neutral_bar = cairo_image_surface_create_from_png (IMAGEDIR "/bar.png");
899+
dr->red_bar = cairo_image_surface_create_from_png (IMAGEDIR "/bar-red.png");
900+
dr->green_bar = cairo_image_surface_create_from_png (IMAGEDIR "/bar-green.png");
901+
dr->disabled_bar = cairo_image_surface_create_from_png (IMAGEDIR "/bar-disabled.png");
880902

881903
init_tray_icon (dr);
882904
#endif /* HAVE_APP_INDICATOR */

0 commit comments

Comments
 (0)