Skip to content

Commit

Permalink
typing-break: Make images larger and load as surface
Browse files Browse the repository at this point in the history
  • Loading branch information
vkareh committed Aug 26, 2018
1 parent 19fdb3f commit 8c4ebef
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 69 deletions.
Binary file modified typing-break/bar-disabled.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified typing-break/bar-green.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified typing-break/bar-red.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified typing-break/bar.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 24 additions & 16 deletions typing-break/drw-break-window.c
Expand Up @@ -77,8 +77,8 @@ static void postpone_clicked_cb (GtkWidget *button,
static gboolean label_draw_event_cb (GtkLabel *label, static gboolean label_draw_event_cb (GtkLabel *label,
cairo_t *cr, cairo_t *cr,
gpointer user_data); gpointer user_data);
static void label_size_request_cb (GtkLabel *label, static void label_size_allocate_cb (GtkLabel *label,
GtkRequisition *requisition, GdkRectangle *allocation,
gpointer user_data); gpointer user_data);


G_DEFINE_TYPE (DrwBreakWindow, drw_break_window, GTK_TYPE_WINDOW) G_DEFINE_TYPE (DrwBreakWindow, drw_break_window, GTK_TYPE_WINDOW)
Expand Down Expand Up @@ -131,6 +131,7 @@ drw_break_window_init (DrwBreakWindow *window)
GdkRectangle monitor; GdkRectangle monitor;
gint right_padding; gint right_padding;
gint bottom_padding; gint bottom_padding;
gint scale;
GSettings *settings; GSettings *settings;


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


g_object_set (window, "type", GTK_WINDOW_POPUP, NULL);
gtk_window_set_keep_above (GTK_WINDOW (window), TRUE); gtk_window_set_keep_above (GTK_WINDOW (window), TRUE);
gtk_window_fullscreen (GTK_WINDOW (window)); gtk_window_fullscreen (GTK_WINDOW (window));
gtk_window_set_modal (GTK_WINDOW (window), TRUE); gtk_window_set_modal (GTK_WINDOW (window), TRUE);


screen = gdk_screen_get_default (); screen = gdk_screen_get_default ();
display = gdk_screen_get_display (screen); display = gdk_screen_get_display (screen);
scale = gtk_widget_get_scale_factor (GTK_WIDGET (window));

gdk_monitor_get_geometry (gdk_display_get_monitor (display, root_monitor), &monitor); gdk_monitor_get_geometry (gdk_display_get_monitor (display, root_monitor), &monitor);


gtk_window_set_default_size (GTK_WINDOW (window), gtk_window_set_default_size (GTK_WINDOW (window),
WidthOfScreen (gdk_x11_screen_get_xscreen (screen)), WidthOfScreen (gdk_x11_screen_get_xscreen (screen)) / scale,
HeightOfScreen (gdk_x11_screen_get_xscreen (screen))); HeightOfScreen (gdk_x11_screen_get_xscreen (screen)) / scale);


gtk_window_set_decorated (GTK_WINDOW (window), FALSE); gtk_window_set_decorated (GTK_WINDOW (window), FALSE);
gtk_widget_set_app_paintable (GTK_WIDGET (window), TRUE); gtk_widget_set_app_paintable (GTK_WIDGET (window), TRUE);
drw_setup_background (GTK_WIDGET (window)); drw_setup_background (GTK_WIDGET (window));


right_padding = WidthOfScreen (gdk_x11_screen_get_xscreen (screen)) - monitor.width - monitor.x; right_padding = WidthOfScreen (gdk_x11_screen_get_xscreen (screen)) / scale - monitor.width - monitor.x;
bottom_padding = HeightOfScreen (gdk_x11_screen_get_xscreen (screen)) - monitor.height - monitor.y; bottom_padding = HeightOfScreen (gdk_x11_screen_get_xscreen (screen)) / scale - monitor.height - monitor.y;


outer_vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0); outer_vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
gtk_widget_set_hexpand (outer_vbox, TRUE); gtk_widget_set_hexpand (outer_vbox, TRUE);
Expand Down Expand Up @@ -234,8 +236,8 @@ drw_break_window_init (DrwBreakWindow *window)
NULL); NULL);


g_signal_connect_after (priv->break_label, g_signal_connect_after (priv->break_label,
"size_request", "size-allocate",
G_CALLBACK (label_size_request_cb), G_CALLBACK (label_size_allocate_cb),
NULL); NULL);


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


g_signal_connect_after (priv->clock_label, g_signal_connect_after (priv->clock_label,
"size_request", "size-allocate",
G_CALLBACK (label_size_request_cb), G_CALLBACK (label_size_allocate_cb),
NULL); NULL);


gtk_window_stick (GTK_WINDOW (window)); gtk_window_stick (GTK_WINDOW (window));
Expand Down Expand Up @@ -540,9 +542,11 @@ get_layout_location (GtkLabel *label,
gint x, y; gint x, y;
gint xpad, ypad; gint xpad, ypad;
gint margin_start, margin_end, margin_top, margin_bottom; gint margin_start, margin_end, margin_top, margin_bottom;
gint scale;


widget = GTK_WIDGET (label); widget = GTK_WIDGET (label);


scale = gtk_widget_get_scale_factor (widget);
xalign = gtk_label_get_xalign (GTK_LABEL (label)); xalign = gtk_label_get_xalign (GTK_LABEL (label));
yalign = gtk_label_get_yalign (GTK_LABEL (label)); yalign = gtk_label_get_yalign (GTK_LABEL (label));
margin_start = gtk_widget_get_margin_start (widget); margin_start = gtk_widget_get_margin_start (widget);
Expand All @@ -555,6 +559,10 @@ get_layout_location (GtkLabel *label,


gtk_widget_get_allocation (widget, &widget_allocation); gtk_widget_get_allocation (widget, &widget_allocation);
gtk_widget_get_requisition (widget, &widget_requisition); gtk_widget_get_requisition (widget, &widget_requisition);
widget_allocation.x /= scale;
widget_allocation.y /= scale;
widget_requisition.width /= scale;
widget_requisition.height /= scale;


if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR) if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
xalign = 1.0 - xalign; xalign = 1.0 - xalign;
Expand Down Expand Up @@ -603,10 +611,10 @@ label_draw_event_cb (GtkLabel *label,
} }


static void static void
label_size_request_cb (GtkLabel *label, label_size_allocate_cb (GtkLabel *label,
GtkRequisition *requisition, GdkRectangle *allocation,
gpointer user_data) gpointer user_data)
{ {
requisition->width += 1; allocation->width += 1;
requisition->height += 1; allocation->height += 1;
} }
11 changes: 6 additions & 5 deletions typing-break/drw-utils.c
Expand Up @@ -119,14 +119,15 @@ set_pixmap_background (GtkWidget *window)
GdkPixbuf *tmp_pixbuf, *pixbuf, *tile_pixbuf; GdkPixbuf *tmp_pixbuf, *pixbuf, *tile_pixbuf;
GdkRectangle rect; GdkRectangle rect;
GdkColor color; GdkColor color;
gint width, height; gint width, height, scale;
cairo_t *cr; cairo_t *cr;


gtk_widget_realize (window); gtk_widget_realize (window);


screen = gtk_widget_get_screen (window); screen = gtk_widget_get_screen (window);
width = WidthOfScreen (gdk_x11_screen_get_xscreen (screen)); scale = gtk_widget_get_scale_factor (window);
height = HeightOfScreen (gdk_x11_screen_get_xscreen (screen)); width = WidthOfScreen (gdk_x11_screen_get_xscreen (screen)) / scale;
height = HeightOfScreen (gdk_x11_screen_get_xscreen (screen)) / scale;


tmp_pixbuf = gdk_pixbuf_get_from_window (gdk_screen_get_root_window (screen), tmp_pixbuf = gdk_pixbuf_get_from_window (gdk_screen_get_root_window (screen),
0, 0,
Expand Down Expand Up @@ -160,8 +161,8 @@ set_pixmap_background (GtkWidget *window)
height, height,
0, 0,
0, 0,
1, scale,
1, scale,
GDK_INTERP_NEAREST, GDK_INTERP_NEAREST,
225); 225);


Expand Down
118 changes: 70 additions & 48 deletions typing-break/drwright.c
Expand Up @@ -69,42 +69,42 @@ typedef enum {


struct _DrWright { struct _DrWright {
/* Widgets. */ /* Widgets. */
GtkWidget *break_window; GtkWidget *break_window;
GList *secondary_break_windows; GList *secondary_break_windows;


DrwMonitor *monitor; DrwMonitor *monitor;


GtkUIManager *ui_manager; GtkUIManager *ui_manager;


DrwState state; DrwState state;
DrwTimer *timer; DrwTimer *timer;
DrwTimer *idle_timer; DrwTimer *idle_timer;


gint last_elapsed_time; gint last_elapsed_time;
gint save_last_time; gint save_last_time;


/* Time settings. */ /* Time settings. */
gint type_time; gint type_time;
gint break_time; gint break_time;
gint warn_time; gint warn_time;


gboolean enabled; gboolean enabled;


guint clock_timeout_id; guint clock_timeout_id;
#ifdef HAVE_APP_INDICATOR #ifdef HAVE_APP_INDICATOR
AppIndicator *indicator; AppIndicator *indicator;
#else #else
guint blink_timeout_id; guint blink_timeout_id;


gboolean blink_on; gboolean blink_on;


GtkStatusIcon *icon; GtkStatusIcon *icon;


GdkPixbuf *neutral_bar; cairo_surface_t *neutral_bar;
GdkPixbuf *red_bar; cairo_surface_t *red_bar;
GdkPixbuf *green_bar; cairo_surface_t *green_bar;
GdkPixbuf *disabled_bar; cairo_surface_t *disabled_bar;
GdkPixbuf *composite_bar; GdkPixbuf *composite_bar;
#endif /* HAVE_APP_INDICATOR */ #endif /* HAVE_APP_INDICATOR */


GtkWidget *warn_dialog; GtkWidget *warn_dialog;
Expand Down Expand Up @@ -175,6 +175,21 @@ update_app_indicator (DrWright *dr)
app_indicator_set_status (dr->indicator, new_status); app_indicator_set_status (dr->indicator, new_status);
} }
#else #else

static void
set_status_icon (GtkStatusIcon *icon, cairo_surface_t *surface)
{
GdkPixbuf *pixbuf;

pixbuf = gdk_pixbuf_get_from_surface (surface, 0, 0,
cairo_image_surface_get_width (surface),
cairo_image_surface_get_height (surface));

gtk_status_icon_set_from_pixbuf (icon, pixbuf);

g_object_unref (pixbuf);
}

static void static void
update_icon (DrWright *dr) update_icon (DrWright *dr)
{ {
Expand All @@ -186,15 +201,16 @@ update_icon (DrWright *dr)
gboolean set_pixbuf; gboolean set_pixbuf;


if (!dr->enabled) { if (!dr->enabled) {
gtk_status_icon_set_from_pixbuf (dr->icon, set_status_icon (dr->icon, dr->disabled_bar);
dr->disabled_bar);
return; return;
} }


tmp_pixbuf = gdk_pixbuf_copy (dr->neutral_bar); width = cairo_image_surface_get_width (dr->neutral_bar);
height = cairo_image_surface_get_height (dr->neutral_bar);


width = gdk_pixbuf_get_width (tmp_pixbuf); tmp_pixbuf = gdk_pixbuf_get_from_surface (dr->neutral_bar,
height = gdk_pixbuf_get_height (tmp_pixbuf); 0, 0,
width, height);


set_pixbuf = TRUE; set_pixbuf = TRUE;


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


switch (dr->state) { switch (dr->state) {
case STATE_WARN: case STATE_WARN:
pixbuf = dr->red_bar; pixbuf = gdk_pixbuf_get_from_surface (dr->red_bar, 0, 0, width, height);
set_pixbuf = FALSE; set_pixbuf = FALSE;
break; break;


case STATE_BREAK_SETUP: case STATE_BREAK_SETUP:
case STATE_BREAK: case STATE_BREAK:
pixbuf = dr->red_bar; pixbuf = gdk_pixbuf_get_from_surface (dr->red_bar, 0, 0, width, height);
break; break;


default: default:
pixbuf = dr->green_bar; pixbuf = gdk_pixbuf_get_from_surface (dr->green_bar, 0, 0, width, height);
} }


gdk_pixbuf_composite (pixbuf, gdk_pixbuf_composite (pixbuf,
Expand All @@ -256,6 +272,8 @@ update_icon (DrWright *dr)
} }


dr->composite_bar = tmp_pixbuf; dr->composite_bar = tmp_pixbuf;

g_object_unref (pixbuf);
} }


static gboolean static gboolean
Expand All @@ -272,11 +290,9 @@ blink_timeout_cb (DrWright *dr)
} }


if (dr->blink_on || timeout == 0) { if (dr->blink_on || timeout == 0) {
gtk_status_icon_set_from_pixbuf (dr->icon, gtk_status_icon_set_from_pixbuf (dr->icon, dr->composite_bar);
dr->composite_bar);
} else { } else {
gtk_status_icon_set_from_pixbuf (dr->icon, set_status_icon (dr->icon, dr->neutral_bar);
dr->neutral_bar);
} }


dr->blink_on = !dr->blink_on; dr->blink_on = !dr->blink_on;
Expand Down Expand Up @@ -385,8 +401,7 @@ maybe_change_state (DrWright *dr)
} }


#ifndef HAVE_APP_INDICATOR #ifndef HAVE_APP_INDICATOR
gtk_status_icon_set_from_pixbuf (dr->icon, set_status_icon (dr->icon, dr->neutral_bar);
dr->neutral_bar);
#endif /* HAVE_APP_INDICATOR */ #endif /* HAVE_APP_INDICATOR */


dr->save_last_time = 0; dr->save_last_time = 0;
Expand Down Expand Up @@ -426,8 +441,7 @@ maybe_change_state (DrWright *dr)


stop_blinking (dr); stop_blinking (dr);
#ifndef HAVE_APP_INDICATOR #ifndef HAVE_APP_INDICATOR
gtk_status_icon_set_from_pixbuf (dr->icon, set_status_icon (dr->icon, dr->red_bar);
dr->red_bar);
#endif /* HAVE_APP_INDICATOR */ #endif /* HAVE_APP_INDICATOR */


drw_timer_start (dr->timer); drw_timer_start (dr->timer);
Expand Down Expand Up @@ -470,8 +484,7 @@ maybe_change_state (DrWright *dr)
case STATE_BREAK_DONE_SETUP: case STATE_BREAK_DONE_SETUP:
stop_blinking (dr); stop_blinking (dr);
#ifndef HAVE_APP_INDICATOR #ifndef HAVE_APP_INDICATOR
gtk_status_icon_set_from_pixbuf (dr->icon, set_status_icon (dr->icon, dr->green_bar);
dr->green_bar);
#endif /* HAVE_APP_INDICATOR */ #endif /* HAVE_APP_INDICATOR */


dr->state = STATE_BREAK_DONE; dr->state = STATE_BREAK_DONE;
Expand Down Expand Up @@ -765,7 +778,14 @@ init_app_indicator (DrWright *dr)
static void static void
init_tray_icon (DrWright *dr) init_tray_icon (DrWright *dr)
{ {
dr->icon = gtk_status_icon_new_from_pixbuf (dr->neutral_bar); GdkPixbuf *pixbuf;

pixbuf = gdk_pixbuf_get_from_surface (dr->neutral_bar, 0, 0,
cairo_image_surface_get_width (dr->neutral_bar),
cairo_image_surface_get_height (dr->neutral_bar));

dr->icon = gtk_status_icon_new_from_pixbuf (pixbuf);
g_object_unref (pixbuf);


update_status (dr); update_status (dr);
update_icon (dr); update_icon (dr);
Expand All @@ -784,6 +804,7 @@ create_secondary_break_windows (void)
GdkScreen *screen; GdkScreen *screen;
GtkWidget *window; GtkWidget *window;
GList *windows = NULL; GList *windows = NULL;
gint scale;


display = gdk_display_get_default (); display = gdk_display_get_default ();


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


windows = g_list_prepend (windows, window); windows = g_list_prepend (windows, window);
scale = gtk_widget_get_scale_factor (GTK_WIDGET (window));


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


gtk_window_set_default_size (GTK_WINDOW (window), gtk_window_set_default_size (GTK_WINDOW (window),
WidthOfScreen (gdk_x11_screen_get_xscreen (screen)), WidthOfScreen (gdk_x11_screen_get_xscreen (screen)) / scale,
HeightOfScreen (gdk_x11_screen_get_xscreen (screen))); HeightOfScreen (gdk_x11_screen_get_xscreen (screen)) / scale);


gtk_widget_set_app_paintable (GTK_WIDGET (window), TRUE); gtk_widget_set_app_paintable (GTK_WIDGET (window), TRUE);
drw_setup_background (GTK_WIDGET (window)); drw_setup_background (GTK_WIDGET (window));
Expand Down Expand Up @@ -873,10 +895,10 @@ drwright_new (void)
#ifdef HAVE_APP_INDICATOR #ifdef HAVE_APP_INDICATOR
init_app_indicator (dr); init_app_indicator (dr);
#else #else
dr->neutral_bar = gdk_pixbuf_new_from_file (IMAGEDIR "/bar.png", NULL); dr->neutral_bar = cairo_image_surface_create_from_png (IMAGEDIR "/bar.png");
dr->red_bar = gdk_pixbuf_new_from_file (IMAGEDIR "/bar-red.png", NULL); dr->red_bar = cairo_image_surface_create_from_png (IMAGEDIR "/bar-red.png");
dr->green_bar = gdk_pixbuf_new_from_file (IMAGEDIR "/bar-green.png", NULL); dr->green_bar = cairo_image_surface_create_from_png (IMAGEDIR "/bar-green.png");
dr->disabled_bar = gdk_pixbuf_new_from_file (IMAGEDIR "/bar-disabled.png", NULL); dr->disabled_bar = cairo_image_surface_create_from_png (IMAGEDIR "/bar-disabled.png");


init_tray_icon (dr); init_tray_icon (dr);
#endif /* HAVE_APP_INDICATOR */ #endif /* HAVE_APP_INDICATOR */
Expand Down

0 comments on commit 8c4ebef

Please sign in to comment.