[gnome-todo] night-light: Introduce Night Light plugin



commit 4ca7b869ef4417d2797e4a6e4f0887c2d676e030
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Sat Sep 15 22:51:48 2018 -0300

    night-light: Introduce Night Light plugin
    
    This is an enabled-by-default plugin that monitors Night Light
    and switches to the dark theme variant when Night Light is on.
    
    It does some effort to return to the correct theme variant after,
    but not too much.
    
    It can be disabled by editing the list of enabled plugins with
    D-Conf editor.

 build-aux/flatpak/org.gnome.Todo.json        |   1 +
 data/org.gnome.todo.gschema.xml              |   2 +-
 data/todo.gresource.xml                      |   5 +
 plugins/meson.build                          |   1 +
 plugins/night-light/gtd-night-light-plugin.c | 266 +++++++++++++++++++++++++++
 plugins/night-light/gtd-night-light-plugin.h |  35 ++++
 plugins/night-light/meson.build              |  14 ++
 plugins/night-light/night-light.plugin.in    |  14 ++
 8 files changed, 337 insertions(+), 1 deletion(-)
---
diff --git a/build-aux/flatpak/org.gnome.Todo.json b/build-aux/flatpak/org.gnome.Todo.json
index f33b7a4..1de8687 100644
--- a/build-aux/flatpak/org.gnome.Todo.json
+++ b/build-aux/flatpak/org.gnome.Todo.json
@@ -16,6 +16,7 @@
         "--share=network",
         "--system-talk-name=org.freedesktop.login1",
         "--talk-name=org.gnome.OnlineAccounts",
+        "--talk-name=org.gnome.SettingsDaemon.Color",
         "--talk-name=org.gnome.evolution.dataserver.AddressBook9",
         "--talk-name=org.gnome.evolution.dataserver.Calendar7",
         "--talk-name=org.gnome.evolution.dataserver.Sources5",
diff --git a/data/org.gnome.todo.gschema.xml b/data/org.gnome.todo.gschema.xml
index 639a5dc..fe4bfe7 100644
--- a/data/org.gnome.todo.gschema.xml
+++ b/data/org.gnome.todo.gschema.xml
@@ -27,7 +27,7 @@
             <description>The identifier of the default provider to add new lists to</description>
         </key>
         <key name="active-extensions" type="as">
-            <default>["all-tasks-panel", "background", "eds", "today-panel", "next-week-panel"]</default>
+            <default>["all-tasks-panel", "background", "eds", "today-panel", "next-week-panel", 
"night-light"]</default>
             <summary>List of active extensions</summary>
             <description>The list of active extensions</description>
         </key>
diff --git a/data/todo.gresource.xml b/data/todo.gresource.xml
index 3414390..73718a6 100644
--- a/data/todo.gresource.xml
+++ b/data/todo.gresource.xml
@@ -64,6 +64,11 @@
     <file compressed="true" 
alias="theme/next-week-panel/Adwaita.css">../plugins/next-week-panel/theme/Adwaita.css</file>
   </gresource>
 
+  <!--Night Light-->
+  <gresource prefix="/org/gnome/todo">
+    <file alias="plugins/night-light/night-light.plugin">../plugins/night-light/night-light.plugin</file>
+  </gresource>
+
   <!--Scheduled panel-->
   <gresource prefix="/org/gnome/todo">
     <file 
alias="plugins/scheduled-panel/scheduled-panel.plugin">../plugins/scheduled-panel/scheduled-panel.plugin</file>
diff --git a/plugins/meson.build b/plugins/meson.build
index 575ce83..c40f7c9 100644
--- a/plugins/meson.build
+++ b/plugins/meson.build
@@ -12,6 +12,7 @@ subdir('background')
 subdir('dark-theme')
 subdir('eds')
 subdir('next-week-panel')
+subdir('night-light')
 subdir('scheduled-panel')
 subdir('today-panel')
 
diff --git a/plugins/night-light/gtd-night-light-plugin.c b/plugins/night-light/gtd-night-light-plugin.c
new file mode 100644
index 0000000..8d4502a
--- /dev/null
+++ b/plugins/night-light/gtd-night-light-plugin.c
@@ -0,0 +1,266 @@
+/* gtd-night-light-plugin.c
+ *
+ * Copyright 2018 Georges Basile Stavracas Neto <georges stavracas gmail com>
+ *
+ * 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/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#define G_LOG_DOMAIN "GtdNightLightPlugin"
+
+#include "gtd-debug.h"
+#include "gtd-night-light-plugin.h"
+
+#include <glib/gi18n.h>
+#include <glib-object.h>
+
+struct _GtdNightLightPlugin
+{
+  PeasExtensionBase   parent;
+
+  GDBusProxy         *night_light_proxy;
+
+  gboolean            dark_theme_preferred;
+  gboolean            plugin_active;
+};
+
+static void          gtd_activatable_iface_init                  (GtdActivatableInterface *iface);
+
+G_DEFINE_DYNAMIC_TYPE_EXTENDED (GtdNightLightPlugin, gtd_night_light_plugin, PEAS_TYPE_EXTENSION_BASE,
+                                0,
+                                G_IMPLEMENT_INTERFACE_DYNAMIC (GTD_TYPE_ACTIVATABLE, 
gtd_activatable_iface_init))
+
+enum
+{
+  PROP_0,
+  PROP_PREFERENCES_PANEL,
+  N_PROPS
+};
+
+
+/*
+ * Auxiliary methods
+ */
+
+static gboolean
+has_night_light_property (GtdNightLightPlugin *self)
+{
+  g_auto (GStrv) properties = g_dbus_proxy_get_cached_property_names (self->night_light_proxy);
+  return properties ? g_strv_contains ((const gchar * const *)properties, "NightLightActive") : FALSE;
+}
+
+static void
+update_night_light_state (GtdNightLightPlugin *self)
+{
+  g_autoptr (GVariant) activev = NULL;
+  gboolean active;
+
+  GTD_ENTRY;
+
+  g_assert (GTD_IS_NIGHT_LIGHT_PLUGIN (self));
+  g_assert (G_IS_DBUS_PROXY (self->night_light_proxy));
+
+  if (!has_night_light_property (self))
+    GTD_RETURN ();
+
+  if (!self->plugin_active)
+    {
+      g_object_set (gtk_settings_get_default (),
+                    "gtk-application-prefer-dark-theme", self->dark_theme_preferred,
+                    NULL);
+      GTD_RETURN ();
+    }
+
+  activev = g_dbus_proxy_get_cached_property (self->night_light_proxy, "NightLightActive");
+  active = g_variant_get_boolean (activev);
+
+  g_object_get (gtk_settings_get_default (),
+                "gtk-application-prefer-dark-theme", &self->dark_theme_preferred,
+                NULL);
+
+  if (active == self->dark_theme_preferred)
+    GTD_RETURN ();
+
+  g_object_set (gtk_settings_get_default (),
+                "gtk-application-prefer-dark-theme", active,
+                NULL);
+
+  GTD_TRACE_MSG ("NightLightActive: %d", active);
+
+  GTD_EXIT;
+}
+
+
+/*
+ * Callbacks
+ */
+
+static void
+on_night_light_proxy_properties_changed_cb (GtdNightLightPlugin *self,
+                                            GVariant            *properties,
+                                            const gchar * const *invalidated,
+                                            GDBusProxy          *proxy)
+{
+  update_night_light_state (self);
+}
+
+
+/*
+ * GtdActivatable interface implementation
+ */
+
+static void
+gtd_night_light_plugin_activate (GtdActivatable *activatable)
+{
+  GtdNightLightPlugin *self = (GtdNightLightPlugin*) activatable;
+
+  GTD_ENTRY;
+
+  self->plugin_active = TRUE;
+  update_night_light_state (self);
+
+  GTD_EXIT;
+}
+
+static void
+gtd_night_light_plugin_deactivate (GtdActivatable *activatable)
+{
+  GtdNightLightPlugin *self = (GtdNightLightPlugin*) activatable;
+
+  GTD_ENTRY;
+
+  self->plugin_active = FALSE;
+  update_night_light_state (self);
+
+  GTD_EXIT;
+}
+
+static GList*
+gtd_night_light_plugin_get_header_widgets (GtdActivatable *activatable)
+{
+  return NULL;
+}
+
+static GtkWidget*
+gtd_night_light_plugin_get_preferences_panel (GtdActivatable *activatable)
+{
+  return NULL;
+}
+
+static GList*
+gtd_night_light_plugin_get_panels (GtdActivatable *activatable)
+{
+  return NULL;
+}
+
+static GList*
+gtd_night_light_plugin_get_providers (GtdActivatable *activatable)
+{
+  return NULL;
+}
+
+static void
+gtd_activatable_iface_init (GtdActivatableInterface *iface)
+{
+  iface->activate = gtd_night_light_plugin_activate;
+  iface->deactivate = gtd_night_light_plugin_deactivate;
+  iface->get_header_widgets = gtd_night_light_plugin_get_header_widgets;
+  iface->get_preferences_panel = gtd_night_light_plugin_get_preferences_panel;
+  iface->get_panels = gtd_night_light_plugin_get_panels;
+  iface->get_providers = gtd_night_light_plugin_get_providers;
+}
+
+
+/*
+ * GObject overrides
+ */
+
+static void
+gtd_night_light_plugin_get_property (GObject    *object,
+                                    guint       prop_id,
+                                    GValue     *value,
+                                    GParamSpec *pspec)
+{
+  switch (prop_id)
+    {
+    case PROP_PREFERENCES_PANEL:
+      g_value_set_object (value, NULL);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+gtd_night_light_plugin_class_init (GtdNightLightPluginClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->get_property = gtd_night_light_plugin_get_property;
+
+  g_object_class_override_property (object_class,
+                                    PROP_PREFERENCES_PANEL,
+                                    "preferences-panel");
+}
+
+static void
+gtd_night_light_plugin_init (GtdNightLightPlugin *self)
+{
+  g_autoptr (GDBusProxy) proxy = NULL;
+
+  GTD_ENTRY;
+
+  proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
+                                         G_DBUS_PROXY_FLAGS_NONE,
+                                         NULL,
+                                         "org.gnome.SettingsDaemon.Color",
+                                         "/org/gnome/SettingsDaemon/Color",
+                                         "org.gnome.SettingsDaemon.Color",
+                                         NULL,
+                                         NULL);
+
+  if (!proxy)
+    GTD_RETURN ();
+
+  g_debug ("Creating Night Light monitor");
+
+  g_signal_connect_object (proxy,
+                           "g-properties-changed",
+                           G_CALLBACK (on_night_light_proxy_properties_changed_cb),
+                           self,
+                           G_CONNECT_SWAPPED);
+
+  self->night_light_proxy = g_steal_pointer (&proxy);
+
+  on_night_light_proxy_properties_changed_cb (self, NULL, NULL, self->night_light_proxy);
+
+  GTD_EXIT;
+}
+
+static void
+gtd_night_light_plugin_class_finalize (GtdNightLightPluginClass *klass)
+{
+}
+
+G_MODULE_EXPORT void
+gtd_night_light_plugin_register_types (PeasObjectModule *module)
+{
+  gtd_night_light_plugin_register_type (G_TYPE_MODULE (module));
+
+  peas_object_module_register_extension_type (module,
+                                              GTD_TYPE_ACTIVATABLE,
+                                              GTD_TYPE_NIGHT_LIGHT_PLUGIN);
+}
diff --git a/plugins/night-light/gtd-night-light-plugin.h b/plugins/night-light/gtd-night-light-plugin.h
new file mode 100644
index 0000000..6172eca
--- /dev/null
+++ b/plugins/night-light/gtd-night-light-plugin.h
@@ -0,0 +1,35 @@
+/* gtd-night-light-plugin.h
+ *
+ * Copyright 2018 Georges Basile Stavracas Neto <georges stavracas gmail com>
+ *
+ * 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/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#pragma once
+
+#include "gnome-todo.h"
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+#define GTD_TYPE_NIGHT_LIGHT_PLUGIN (gtd_night_light_plugin_get_type())
+
+G_DECLARE_FINAL_TYPE (GtdNightLightPlugin, gtd_night_light_plugin, GTD, NIGHT_LIGHT_PLUGIN, 
PeasExtensionBase)
+
+G_MODULE_EXPORT void gtd_night_light_plugin_register_types       (PeasObjectModule   *module);
+
+G_END_DECLS
diff --git a/plugins/night-light/meson.build b/plugins/night-light/meson.build
new file mode 100644
index 0000000..ea1529d
--- /dev/null
+++ b/plugins/night-light/meson.build
@@ -0,0 +1,14 @@
+plugins_ldflags += ['-Wl,--undefined=gtd_night_light_plugin_register_types']
+
+plugins_libs += static_library(
+          'nightlight',
+              sources : 'gtd-night-light-plugin.c',
+  include_directories : plugins_incs,
+         dependencies : gnome_todo_deps
+)
+
+plugins_confs += configure_file(
+          input : 'night-light.plugin.in',
+         output : 'night-light.plugin',
+  configuration : plugins_conf
+)
diff --git a/plugins/night-light/night-light.plugin.in b/plugins/night-light/night-light.plugin.in
new file mode 100644
index 0000000..56b1b21
--- /dev/null
+++ b/plugins/night-light/night-light.plugin.in
@@ -0,0 +1,14 @@
+[Plugin]
+Name = Night Light
+Module = night-light
+Description = Night Light integration for GNOME To Do
+Version = @VERSION@
+Authors = Georges Basile Stavracas Neto <gbsneto gnome org>
+Copyright = Copyleft © The To Do maintainers
+Website = https://wiki.gnome.org/Apps/Todo
+Builtin = true
+Hidden = true
+License = GPL
+Loader = C
+Embedded = gtd_night_light_plugin_register_types
+Depends =


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]