Verified Commit 4c424a55 authored by Zander Brown's avatar Zander Brown
Browse files

Manage gtk theme at the application level

Using a GtkApplication subclass makes things a little neater and will simplify adding DBus support
parent 17d807e4
.buildconfig
*~
......@@ -12,6 +12,7 @@
"--filesystem=xdg-run/dconf",
"--filesystem=~/.config/dconf:ro",
"--talk-name=org.freedesktop.FileManager1",
"--talk-name=org.freedesktop.Flatpak",
"--talk-name=ca.desrt.dconf",
"--env=DCONF_USER_CONFIG_DIR=.config/dconf",
"--device=dri"
......
/* kgx-window.c
*
* Copyright 2019 Zander Brown
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <glib/gi18n.h>
#include <vte/vte.h>
#include "rgba.h"
#include "kgx.h"
#include "kgx-config.h"
#include "kgx-application.h"
#include "kgx-window.h"
#include "kgx-enums.h"
struct _KgxApplication
{
GtkApplication parent_instance;
KgxTheme theme;
};
G_DEFINE_TYPE (KgxApplication, kgx_application, GTK_TYPE_APPLICATION)
enum {
PROP_0,
PROP_THEME,
LAST_PROP
};
static GParamSpec *pspecs[LAST_PROP] = { NULL, };
void
kgx_application_set_theme (KgxApplication *self,
KgxTheme theme)
{
GtkSettings *settings;
self->theme = theme;
settings = gtk_settings_get_default ();
switch (theme) {
case KGX_THEME_NIGHT:
case KGX_THEME_HACKER:
g_object_set (G_OBJECT (settings),
"gtk-application-prefer-dark-theme", TRUE,
NULL);
break;
case KGX_THEME_DAY:
default:
g_object_set (G_OBJECT (settings),
"gtk-application-prefer-dark-theme", FALSE,
NULL);
break;
}
g_object_notify_by_pspec (G_OBJECT (self), pspecs[PROP_THEME]);
}
static void
kgx_application_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
KgxApplication *self = KGX_APPLICATION (object);
switch (property_id) {
case PROP_THEME:
kgx_application_set_theme (self, g_value_get_enum (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
kgx_application_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec)
{
KgxApplication *self = KGX_APPLICATION (object);
switch (property_id) {
case PROP_THEME:
g_value_set_enum (value, self->theme);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
kgx_application_activate (GApplication *app)
{
GtkWindow *window;
guint32 timestamp;
timestamp = GDK_CURRENT_TIME;
/* Get the current window or create one if necessary. */
window = gtk_application_get_active_window (GTK_APPLICATION (app));
if (window == NULL) {
window = g_object_new (KGX_TYPE_WINDOW,
"application", app,
NULL);
}
gtk_window_present_with_time (window, timestamp);
}
static void
kgx_application_startup (GApplication *app)
{
const char *const new_window_accels[] = { "<shift><primary>n", NULL };
g_type_ensure (VTE_TYPE_TERMINAL);
gtk_application_set_accels_for_action (GTK_APPLICATION (app),
"win.new-window", new_window_accels);
G_APPLICATION_CLASS (kgx_application_parent_class)->startup (app);
}
static void
kgx_application_class_init (KgxApplicationClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GApplicationClass *app_class = G_APPLICATION_CLASS (klass);
object_class->set_property = kgx_application_set_property;
object_class->get_property = kgx_application_get_property;
app_class->activate = kgx_application_activate;
app_class->startup = kgx_application_startup;
pspecs[PROP_THEME] =
g_param_spec_enum ("theme", "Theme", "Terminal theme",
KGX_TYPE_THEME, KGX_THEME_NIGHT,
G_PARAM_READWRITE);
g_object_class_install_properties (object_class, LAST_PROP, pspecs);
}
static void
kgx_application_init (KgxApplication *self)
{
GSettings *settings;
GtkCssProvider *provider;
settings = g_settings_new ("org.gnome.zbrown.KingsCross");
g_settings_bind (settings, "theme", self, "theme", G_SETTINGS_BIND_DEFAULT);
provider = gtk_css_provider_new ();
gtk_css_provider_load_from_resource (provider, "/org/gnome/zbrown/KingsCross/styles.css");
gtk_style_context_add_provider_for_screen (gdk_screen_get_default (),
GTK_STYLE_PROVIDER (provider),
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
}
/* kgx-application.h
*
* Copyright 2019 Zander Brown
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <gtk/gtk.h>
G_BEGIN_DECLS
#define KGX_TYPE_APPLICATION (kgx_application_get_type())
G_DECLARE_FINAL_TYPE (KgxApplication, kgx_application, KGX, APPLICATION, GtkApplication)
G_END_DECLS
......@@ -161,7 +161,7 @@ kgx_window_class_init (KgxWindowClass *klass)
KGX_TYPE_THEME, KGX_THEME_HACKER,
G_PARAM_READWRITE);
g_object_class_install_properties (object_class, LAST_PROP, pspecs);
g_object_class_install_properties (object_class, LAST_PROP, pspecs);
gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/zbrown/KingsCross/kgx-window.ui");
gtk_widget_class_bind_template_child (widget_class, KgxWindow, header_bar);
......@@ -328,6 +328,16 @@ static GActionEntry win_entries[] =
{ "about", about_activated, NULL, NULL, NULL },
};
static void
application_set (GObject *object, GParamSpec *pspec, gpointer data)
{
g_object_bind_property (gtk_window_get_application (GTK_WINDOW (object)),
"theme",
object,
"theme",
G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
}
static void
context_menu (KgxWindow *self, GtkWidget *term, GdkEventButton *event)
{
......@@ -417,8 +427,6 @@ kgx_window_init (KgxWindow *self)
{
GAction *act;
GPropertyAction *pact;
GSettings *settings;
GtkCssProvider *provider;
gchar *shell[2] = {NULL, NULL};
gtk_widget_init_template (GTK_WIDGET (self));
......@@ -428,20 +436,13 @@ kgx_window_init (KgxWindow *self)
G_N_ELEMENTS (win_entries),
self);
self->theme = KGX_THEME_HACKER;
self->theme = KGX_THEME_NIGHT;
settings = g_settings_new ("org.gnome.zbrown.KingsCross");
g_settings_bind (settings, "theme", self, "theme", G_SETTINGS_BIND_DEFAULT);
g_signal_connect (self, "notify::application", G_CALLBACK (application_set), NULL);
pact = g_property_action_new ("theme", G_OBJECT (self), "theme");
g_action_map_add_action (G_ACTION_MAP (self), G_ACTION (pact));
provider = gtk_css_provider_new ();
gtk_css_provider_load_from_resource (provider, "/org/gnome/zbrown/KingsCross/styles.css");
gtk_style_context_add_provider_for_screen (gdk_screen_get_default (),
GTK_STYLE_PROVIDER (provider),
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
act = g_action_map_lookup_action (G_ACTION_MAP (self), "open-link");
g_simple_action_set_enabled (G_SIMPLE_ACTION (act), FALSE);
act = g_action_map_lookup_action (G_ACTION_MAP (self), "copy-link");
......
/* kgx-window.h
/* kgx.h
*
* Copyright 2019 Zander Brown
*
......
......@@ -17,42 +17,9 @@
*/
#include <glib/gi18n.h>
#include <vte/vte.h>
#include "kgx-config.h"
#include "kgx-window.h"
static void
on_activate (GtkApplication *app)
{
GtkWindow *window;
/* It's good practice to check your parameters at the beginning of the
* function. It helps catch errors early and in development instead of
* by your users.
*/
g_assert (GTK_IS_APPLICATION (app));
/* Get the current window or create one if necessary. */
window = gtk_application_get_active_window (app);
if (window == NULL)
window = g_object_new (KGX_TYPE_WINDOW,
"application", app,
"default-width", 600,
"default-height", 300,
NULL);
/* Ask the window manager/compositor to present the window. */
gtk_window_present (window);
}
static void
on_startup (GtkApplication *app)
{
const char * const new_window_accels[] = { "<shift><primary>n", NULL };
gtk_application_set_accels_for_action (GTK_APPLICATION (app), "win.new-window", new_window_accels);
}
#include "kgx-application.h"
int
main (int argc,
......@@ -68,26 +35,15 @@ main (int argc,
g_set_application_name (_("King's Cross"));
g_type_ensure (VTE_TYPE_TERMINAL);
/*
* Create a new GtkApplication. The application manages our main loop,
* application windows, integration with the window manager/compositor, and
* desktop features such as file opening and single-instance applications.
*/
app = gtk_application_new ("org.gnome.zbrown.KingsCross", G_APPLICATION_FLAGS_NONE);
/*
* We connect to the activate signal to create a window when the application
* has been lauched. Additionally, this signal notifies us when the user
* tries to launch a "second instance" of the application. When they try
* to do that, we'll just present any existing window.
*
* Because we can't pass a pointer to any function type, we have to cast
* our "on_activate" function to a GCallback.
*/
g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL);
g_signal_connect (app, "startup", G_CALLBACK (on_startup), NULL);
app = g_object_new (KGX_TYPE_APPLICATION,
"application_id", "org.gnome.zbrown.KingsCross",
"flags", G_APPLICATION_FLAGS_NONE,
NULL);
/*
* Run the application. This function will block until the applicaiton
......
kgx_sources = [
'main.c',
'kgx-application.c',
'kgx-window.c',
]
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment