Dark mode for process-statusbar
This commit is contained in:
@@ -47,7 +47,6 @@ static void xtm_process_monitor_set_property (GObject *object, guint property_id
|
||||
static gboolean xtm_process_monitor_draw (GtkWidget *widget, cairo_t *cr);
|
||||
static void xtm_process_monitor_paint (XtmProcessMonitor *monitor, cairo_t *cr);
|
||||
|
||||
static gboolean xtm_process_monitor_is_dark_mode (XtmProcessMonitor *monitor);
|
||||
|
||||
|
||||
static void
|
||||
@@ -68,21 +67,22 @@ xtm_process_monitor_class_init (XtmProcessMonitorClass *klass)
|
||||
}
|
||||
|
||||
static void
|
||||
xtm_process_monitor_on_notify_theme_name (GtkSettings *settings, GParamSpec* pSpec, XtmProcessMonitor* monitor)
|
||||
xtm_process_monitor_on_notify_theme_name (XtmProcessMonitor *monitor)
|
||||
{
|
||||
monitor->dark_mode = xtm_process_monitor_is_dark_mode (monitor);
|
||||
monitor->dark_mode = xtm_gtk_widget_is_dark_mode (GTK_WIDGET (monitor));
|
||||
}
|
||||
|
||||
static void
|
||||
xtm_process_monitor_init (XtmProcessMonitor *monitor)
|
||||
{
|
||||
GtkSettings *settings = gtk_settings_get_default();
|
||||
GtkSettings *settings = gtk_settings_get_default ();
|
||||
|
||||
monitor->history = g_array_new (FALSE, TRUE, sizeof (gfloat));
|
||||
monitor->history_swap = g_array_new (FALSE, TRUE, sizeof (gfloat));
|
||||
monitor->dark_mode = xtm_process_monitor_is_dark_mode (monitor);
|
||||
monitor->dark_mode = xtm_gtk_widget_is_dark_mode (GTK_WIDGET (monitor));
|
||||
|
||||
g_signal_connect (settings, "notify::gtk-theme-name", G_CALLBACK (xtm_process_monitor_on_notify_theme_name), monitor);
|
||||
g_signal_connect_swapped (settings, "notify::gtk-theme-name", G_CALLBACK (xtm_process_monitor_on_notify_theme_name), monitor);
|
||||
g_signal_connect (monitor, "realize", G_CALLBACK (xtm_process_monitor_on_notify_theme_name), NULL);
|
||||
}
|
||||
|
||||
static void
|
||||
@@ -155,23 +155,6 @@ xtm_process_monitor_draw (GtkWidget *widget, cairo_t *cr)
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
xtm_process_monitor_is_dark_mode (XtmProcessMonitor *monitor)
|
||||
{
|
||||
GtkStyleContext *context;
|
||||
GdkRGBA *color;
|
||||
double luminosity;
|
||||
gboolean dark_mode;
|
||||
|
||||
context = gtk_widget_get_style_context (gtk_widget_get_toplevel (GTK_WIDGET (monitor)));
|
||||
gtk_style_context_get (context, GTK_STATE_FLAG_NORMAL, GTK_STYLE_PROPERTY_BACKGROUND_COLOR, &color, NULL);
|
||||
luminosity = (0.2126 * color->red + 0.7152 * color->green + 0.0722 * color->blue);
|
||||
gdk_rgba_free (color);
|
||||
dark_mode = (luminosity < 0.5);
|
||||
|
||||
return dark_mode;
|
||||
}
|
||||
|
||||
static void
|
||||
xtm_process_monitor_cairo_set_source_rgba (cairo_t *cr, double red, double green, double blue, double alpha, gboolean invert)
|
||||
{
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
|
||||
#include "process-statusbar.h"
|
||||
#include "settings.h"
|
||||
#include "task-manager.h"
|
||||
|
||||
|
||||
|
||||
@@ -48,6 +49,8 @@ struct _XtmProcessStatusbar
|
||||
gchar memory[64];
|
||||
gchar swap[64];
|
||||
guint num_processes;
|
||||
|
||||
gboolean dark_mode;
|
||||
};
|
||||
G_DEFINE_TYPE (XtmProcessStatusbar, xtm_process_statusbar, GTK_TYPE_BOX)
|
||||
|
||||
@@ -75,13 +78,43 @@ xtm_process_statusbar_class_init (XtmProcessStatusbarClass *klass)
|
||||
g_param_spec_uint ("num-processes", "NumProcesses", "Number of processes", 0, G_MAXUINT, 0, G_PARAM_CONSTRUCT|G_PARAM_WRITABLE));
|
||||
}
|
||||
|
||||
static void
|
||||
xtm_process_statusbar_set_label_style (XtmProcessStatusbar *statusbar, GtkWidget *label)
|
||||
{
|
||||
GtkStyleContext *context;
|
||||
gboolean has_dark_mode;
|
||||
|
||||
context = gtk_widget_get_style_context (label);
|
||||
has_dark_mode = gtk_style_context_has_class (context, "dark");
|
||||
if (has_dark_mode && !statusbar->dark_mode)
|
||||
gtk_style_context_remove_class (context, "dark");
|
||||
if (!has_dark_mode && statusbar->dark_mode)
|
||||
gtk_style_context_add_class (context, "dark");
|
||||
}
|
||||
|
||||
static void
|
||||
xtm_process_statusbar_on_notify_theme_name (XtmProcessStatusbar *statusbar)
|
||||
{
|
||||
statusbar->dark_mode = xtm_gtk_widget_is_dark_mode (GTK_WIDGET (statusbar));
|
||||
|
||||
xtm_process_statusbar_set_label_style (statusbar, statusbar->label_cpu);
|
||||
xtm_process_statusbar_set_label_style (statusbar, statusbar->label_num_processes);
|
||||
xtm_process_statusbar_set_label_style (statusbar, statusbar->label_memory);
|
||||
xtm_process_statusbar_set_label_style (statusbar, statusbar->label_swap);
|
||||
}
|
||||
|
||||
static void
|
||||
xtm_process_statusbar_init (XtmProcessStatusbar *statusbar)
|
||||
{
|
||||
GtkSettings *settings = gtk_settings_get_default ();
|
||||
GtkWidget *hbox, *hbox_cpu, *hbox_mem;
|
||||
GtkStyleContext *context;
|
||||
GtkCssProvider *provider;
|
||||
statusbar->settings = xtm_settings_get_default ();
|
||||
statusbar->dark_mode = xtm_gtk_widget_is_dark_mode (GTK_WIDGET (statusbar));
|
||||
|
||||
g_signal_connect_swapped (settings, "notify::gtk-theme-name", G_CALLBACK (xtm_process_statusbar_on_notify_theme_name), statusbar);
|
||||
g_signal_connect (statusbar, "realize", G_CALLBACK (xtm_process_statusbar_on_notify_theme_name), NULL);
|
||||
|
||||
hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 16);
|
||||
hbox_cpu = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 16);
|
||||
@@ -91,7 +124,7 @@ xtm_process_statusbar_init (XtmProcessStatusbar *statusbar)
|
||||
gtk_box_pack_start (GTK_BOX (hbox_cpu), statusbar->label_cpu, TRUE, FALSE, 0);
|
||||
context = gtk_widget_get_style_context (statusbar->label_cpu);
|
||||
provider = gtk_css_provider_new ();
|
||||
gtk_css_provider_load_from_data (provider, "* { color: #ff6e00; }", -1, NULL);
|
||||
gtk_css_provider_load_from_data (provider, "* { color: #ff6e00; } .dark { color: #0091ff; }", -1, NULL);
|
||||
gtk_style_context_add_provider (context, GTK_STYLE_PROVIDER (provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
|
||||
g_object_unref (provider);
|
||||
|
||||
@@ -103,7 +136,7 @@ xtm_process_statusbar_init (XtmProcessStatusbar *statusbar)
|
||||
gtk_box_pack_start (GTK_BOX (hbox_mem), statusbar->label_memory, TRUE, FALSE, 0);
|
||||
context = gtk_widget_get_style_context (statusbar->label_memory);
|
||||
provider = gtk_css_provider_new ();
|
||||
gtk_css_provider_load_from_data (provider, "* { color: #cb386c; }", -1, NULL);
|
||||
gtk_css_provider_load_from_data (provider, "* { color: #cb386c; } .dark { color: #34c793; }", -1, NULL);
|
||||
gtk_style_context_add_provider (context, GTK_STYLE_PROVIDER (provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
|
||||
g_object_unref (provider);
|
||||
|
||||
@@ -112,7 +145,7 @@ xtm_process_statusbar_init (XtmProcessStatusbar *statusbar)
|
||||
gtk_box_pack_start (GTK_BOX (hbox_mem), statusbar->label_swap, TRUE, FALSE, 0);
|
||||
context = gtk_widget_get_style_context (statusbar->label_swap);
|
||||
provider = gtk_css_provider_new ();
|
||||
gtk_css_provider_load_from_data (provider, "* { color: #75324d; }", -1, NULL);
|
||||
gtk_css_provider_load_from_data (provider, "* { color: #75324d; } .dark { color: #8acdb2; }", -1, NULL);
|
||||
gtk_style_context_add_provider (context, GTK_STYLE_PROVIDER (provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
|
||||
g_object_unref (provider);
|
||||
|
||||
@@ -122,6 +155,11 @@ xtm_process_statusbar_init (XtmProcessStatusbar *statusbar)
|
||||
|
||||
gtk_box_pack_start (GTK_BOX (statusbar), hbox, TRUE, TRUE, 0);
|
||||
|
||||
xtm_process_statusbar_set_label_style (statusbar, statusbar->label_cpu);
|
||||
xtm_process_statusbar_set_label_style (statusbar, statusbar->label_num_processes);
|
||||
xtm_process_statusbar_set_label_style (statusbar, statusbar->label_memory);
|
||||
xtm_process_statusbar_set_label_style (statusbar, statusbar->label_swap);
|
||||
|
||||
gtk_widget_show_all (hbox);
|
||||
}
|
||||
|
||||
|
||||
@@ -594,3 +594,21 @@ task_pid_compare_fn(gconstpointer a, gconstpointer b)
|
||||
{
|
||||
return (((const Task*)a)->pid - ((const Task*)b)->pid);
|
||||
}
|
||||
|
||||
/* About the constants used below, see for example: https://stackoverflow.com/a/596243 */
|
||||
gboolean
|
||||
xtm_gtk_widget_is_dark_mode (GtkWidget *widget)
|
||||
{
|
||||
GtkStyleContext *context;
|
||||
GdkRGBA *color;
|
||||
double luminosity;
|
||||
gboolean dark_mode;
|
||||
|
||||
context = gtk_widget_get_style_context (gtk_widget_get_toplevel (widget));
|
||||
gtk_style_context_get (context, GTK_STATE_FLAG_NORMAL, GTK_STYLE_PROPERTY_BACKGROUND_COLOR, &color, NULL);
|
||||
luminosity = (0.2126 * color->red + 0.7152 * color->green + 0.0722 * color->blue);
|
||||
gdk_rgba_free (color);
|
||||
dark_mode = (luminosity < 0.5);
|
||||
|
||||
return dark_mode;
|
||||
}
|
||||
|
||||
@@ -115,5 +115,6 @@ gboolean set_priority_to_pid (GPid pid, gint priority);
|
||||
# define G_DEBUG_FMT(fmt, args...)
|
||||
#endif
|
||||
|
||||
gboolean xtm_gtk_widget_is_dark_mode (GtkWidget *widget);
|
||||
|
||||
#endif /* !TASK_MANAGER_H */
|
||||
|
||||
Reference in New Issue
Block a user