Commit 0802f09b authored by Zander Brown's avatar Zander Brown Committed by Zander Brown
Browse files

slower polling when not focused

parent 585da646
...@@ -233,15 +233,30 @@ watch (gpointer data) ...@@ -233,15 +233,30 @@ watch (gpointer data)
} }
#endif #endif
static inline void
set_watcher (KgxApplication *self, gboolean focused)
{
g_debug ("updated watcher focused? %s", focused ? "yes" : "no");
#if HAS_GTOP
if (self->timeout != 0) {
g_source_remove (self->timeout);
}
// Slow down polling when nothing is focused
self->timeout = g_timeout_add (focused ? 500 : 2000, watch, self);
// Translators: This is the name of the timeout that looks for programs
// running in the terminal
g_source_set_name_by_id (self->timeout, _("child watcher"));
#endif
}
static void static void
kgx_application_startup (GApplication *app) kgx_application_startup (GApplication *app)
{ {
GtkSettings *gtk_settings; GtkSettings *gtk_settings;
GSettings *settings; GSettings *settings;
GtkCssProvider *provider; GtkCssProvider *provider;
#if HAS_GTOP
guint source;
#endif
const char *const new_window_accels[] = { "<shift><primary>n", NULL }; const char *const new_window_accels[] = { "<shift><primary>n", NULL };
const char *const copy_accels[] = { "<shift><primary>c", NULL }; const char *const copy_accels[] = { "<shift><primary>c", NULL };
const char *const paste_accels[] = { "<shift><primary>v", NULL }; const char *const paste_accels[] = { "<shift><primary>v", NULL };
...@@ -286,12 +301,7 @@ kgx_application_startup (GApplication *app) ...@@ -286,12 +301,7 @@ kgx_application_startup (GApplication *app)
* priority for fallback styles? Yes*/ * priority for fallback styles? Yes*/
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION + 1); GTK_STYLE_PROVIDER_PRIORITY_APPLICATION + 1);
#if HAS_GTOP set_watcher (KGX_APPLICATION (app), TRUE);
source = g_timeout_add (500, watch, app);
// Translators: This is the name of the timeout that looks for programs
// running in the terminal
g_source_set_name_by_id (source, _("child watcher"));
#endif
} }
static int static int
...@@ -550,6 +560,9 @@ kgx_application_init (KgxApplication *self) ...@@ -550,6 +560,9 @@ kgx_application_init (KgxApplication *self)
self->watching = g_ptr_array_new_with_free_func ((GDestroyNotify) clear_watch); self->watching = g_ptr_array_new_with_free_func ((GDestroyNotify) clear_watch);
self->children = g_ptr_array_new_with_free_func ((GDestroyNotify) clear_watch); self->children = g_ptr_array_new_with_free_func ((GDestroyNotify) clear_watch);
self->active = 0;
self->timeout = 0;
} }
#if HAS_GTOP #if HAS_GTOP
...@@ -637,3 +650,47 @@ kgx_application_get_font (KgxApplication *self) ...@@ -637,3 +650,47 @@ kgx_application_get_font (KgxApplication *self)
return pango_font_description_from_string (font); return pango_font_description_from_string (font);
} }
/**
* kgx_application_push_active:
* @self: the #KgxApplication
*
* Increase the active window count
*/
void
kgx_application_push_active (KgxApplication *self)
{
g_return_if_fail (KGX_IS_APPLICATION (self));
self->active++;
g_debug ("push_active");
if (G_LIKELY (self->active > 0)) {
set_watcher (self, TRUE);
} else {
set_watcher (self, FALSE);
}
}
/**
* kgx_application_pop_active:
* @self: the #KgxApplication
*
* Decrease the active window count
*/
void
kgx_application_pop_active (KgxApplication *self)
{
g_return_if_fail (KGX_IS_APPLICATION (self));
self->active--;
g_debug ("pop_active");
if (G_LIKELY (self->active < 1)) {
set_watcher (self, FALSE);
} else {
set_watcher (self, TRUE);
}
}
\ No newline at end of file
...@@ -60,6 +60,9 @@ struct ProcessWatch { ...@@ -60,6 +60,9 @@ struct ProcessWatch {
* @desktop_interface: the #GSettings storing the system monospace font * @desktop_interface: the #GSettings storing the system monospace font
* @watching: (element-type ProcessWatch): the shells running in windows * @watching: (element-type ProcessWatch): the shells running in windows
* @children: (element-type ProcessWatch): the processes running in shells * @children: (element-type ProcessWatch): the processes running in shells
* @active: counter of #KgxWindow's with #GtkWindow:is-active = %TRUE,
* obviously this should only ever be 1 or but we can't be certain
* @timeout: the current #GSource id of the watcher
* *
* Stability: Private * Stability: Private
*/ */
...@@ -77,6 +80,9 @@ struct _KgxApplication ...@@ -77,6 +80,9 @@ struct _KgxApplication
GPtrArray *watching; GPtrArray *watching;
GPtrArray *children; GPtrArray *children;
guint timeout;
int active;
}; };
G_DECLARE_FINAL_TYPE (KgxApplication, kgx_application, KGX, APPLICATION, GtkApplication) G_DECLARE_FINAL_TYPE (KgxApplication, kgx_application, KGX, APPLICATION, GtkApplication)
...@@ -89,5 +95,7 @@ void kgx_application_remove_watch (KgxApplication *self, ...@@ -89,5 +95,7 @@ void kgx_application_remove_watch (KgxApplication *self,
GPid pid); GPid pid);
#endif #endif
PangoFontDescription *kgx_application_get_font (KgxApplication *self); PangoFontDescription *kgx_application_get_font (KgxApplication *self);
void kgx_application_push_active (KgxApplication *self);
void kgx_application_pop_active (KgxApplication *self);
G_END_DECLS G_END_DECLS
...@@ -356,6 +356,20 @@ application_set (GObject *object, GParamSpec *pspec, gpointer data) ...@@ -356,6 +356,20 @@ application_set (GObject *object, GParamSpec *pspec, gpointer data)
G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
} }
static void
active_changed (GObject *object, GParamSpec *pspec, gpointer data)
{
GtkApplication *app;
app = gtk_window_get_application (GTK_WINDOW (object));
if (gtk_window_is_active (GTK_WINDOW (object))) {
kgx_application_push_active (KGX_APPLICATION (app));
} else {
kgx_application_pop_active (KGX_APPLICATION (app));
}
}
static void static void
search_enabled (GObject *object, search_enabled (GObject *object,
GParamSpec *pspec, GParamSpec *pspec,
...@@ -546,6 +560,7 @@ kgx_window_class_init (KgxWindowClass *klass) ...@@ -546,6 +560,7 @@ kgx_window_class_init (KgxWindowClass *klass)
gtk_widget_class_bind_template_child (widget_class, KgxWindow, zoom_level); gtk_widget_class_bind_template_child (widget_class, KgxWindow, zoom_level);
gtk_widget_class_bind_template_callback (widget_class, application_set); gtk_widget_class_bind_template_callback (widget_class, application_set);
gtk_widget_class_bind_template_callback (widget_class, active_changed);
gtk_widget_class_bind_template_callback (widget_class, search_enabled); gtk_widget_class_bind_template_callback (widget_class, search_enabled);
gtk_widget_class_bind_template_callback (widget_class, search_changed); gtk_widget_class_bind_template_callback (widget_class, search_changed);
......
...@@ -89,6 +89,7 @@ ...@@ -89,6 +89,7 @@
<property name="default-width">800</property> <property name="default-width">800</property>
<property name="default-height">450</property> <property name="default-height">450</property>
<signal name="notify::application" handler="application_set" swapped="no"/> <signal name="notify::application" handler="application_set" swapped="no"/>
<signal name="notify::is-active" handler="active_changed" swapped="no"/>
<child type="titlebar"> <child type="titlebar">
<object class="GtkHeaderBar" id="header_bar"> <object class="GtkHeaderBar" id="header_bar">
<property name="visible">1</property> <property name="visible">1</property>
......
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