[libhandy] tab: Stop spinner when not visible
- From: Adrien Plazas <aplazas src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libhandy] tab: Stop spinner when not visible
- Date: Wed, 10 Feb 2021 09:28:15 +0000 (UTC)
commit 3e33f2aef3b0ead3fd26df9f4d0b8869cd63c0b5
Author: Alexander Mikhaylenko <alexm gnome org>
Date: Mon Feb 8 16:06:55 2021 +0500
tab: Stop spinner when not visible
Surprisingly, an active spinner still uses CPU when not mapped. Stopping
it significantly reduces CPU usage.
src/hdy-tab.c | 39 +++++++++++++++++++++++++++++++++++++++
src/hdy-tab.ui | 3 +--
2 files changed, 40 insertions(+), 2 deletions(-)
---
diff --git a/src/hdy-tab.c b/src/hdy-tab.c
index 4e80b628..2b117237 100644
--- a/src/hdy-tab.c
+++ b/src/hdy-tab.c
@@ -29,6 +29,7 @@ struct _HdyTab
GtkWidget *title;
GtkWidget *icon_stack;
GtkImage *icon;
+ GtkSpinner *spinner;
GtkImage *indicator_icon;
GtkWidget *indicator_btn;
GtkWidget *close_btn;
@@ -185,6 +186,19 @@ update_title (HdyTab *self)
update_tooltip (self);
}
+static void
+update_spinner (HdyTab *self)
+{
+ gboolean loading = self->page && hdy_tab_page_get_loading (self->page);
+ gboolean mapped = gtk_widget_get_mapped (GTK_WIDGET (self));
+
+ /* Don't use CPU when not needed */
+ if (loading && mapped)
+ gtk_spinner_start (self->spinner);
+ else
+ gtk_spinner_stop (self->spinner);
+}
+
static void
update_icons (HdyTab *self)
{
@@ -226,6 +240,7 @@ static void
update_loading (HdyTab *self)
{
update_icons (self);
+ update_spinner (self);
set_style_class (GTK_WIDGET (self), "loading",
hdy_tab_page_get_loading (self->page));
}
@@ -583,6 +598,26 @@ hdy_tab_unrealize (GtkWidget *widget)
self->window = NULL;
}
+static void
+hdy_tab_map (GtkWidget *widget)
+{
+ HdyTab *self = HDY_TAB (widget);
+
+ GTK_WIDGET_CLASS (hdy_tab_parent_class)->map (widget);
+
+ update_spinner (self);
+}
+
+static void
+hdy_tab_unmap (GtkWidget *widget)
+{
+ HdyTab *self = HDY_TAB (widget);
+
+ GTK_WIDGET_CLASS (hdy_tab_parent_class)->unmap (widget);
+
+ update_spinner (self);
+}
+
static gint
get_end_padding (HdyTab *self)
{
@@ -857,6 +892,8 @@ hdy_tab_class_init (HdyTabClass *klass)
widget_class->size_allocate = hdy_tab_size_allocate;
widget_class->realize = hdy_tab_realize;
widget_class->unrealize = hdy_tab_unrealize;
+ widget_class->map = hdy_tab_map;
+ widget_class->unmap = hdy_tab_unmap;
widget_class->draw = hdy_tab_draw;
widget_class->direction_changed = hdy_tab_direction_changed;
@@ -920,6 +957,7 @@ hdy_tab_class_init (HdyTabClass *klass)
gtk_widget_class_bind_template_child (widget_class, HdyTab, title);
gtk_widget_class_bind_template_child (widget_class, HdyTab, icon_stack);
gtk_widget_class_bind_template_child (widget_class, HdyTab, icon);
+ gtk_widget_class_bind_template_child (widget_class, HdyTab, spinner);
gtk_widget_class_bind_template_child (widget_class, HdyTab, indicator_icon);
gtk_widget_class_bind_template_child (widget_class, HdyTab, indicator_btn);
gtk_widget_class_bind_template_child (widget_class, HdyTab, close_btn);
@@ -979,6 +1017,7 @@ hdy_tab_set_page (HdyTab *self,
update_state (self);
update_title (self);
update_tooltip (self);
+ update_spinner (self);
update_icons (self);
update_indicator (self);
update_needs_attention (self);
diff --git a/src/hdy-tab.ui b/src/hdy-tab.ui
index 634f6d55..7f79a33b 100644
--- a/src/hdy-tab.ui
+++ b/src/hdy-tab.ui
@@ -37,9 +37,8 @@
</packing>
</child>
<child>
- <object class="GtkSpinner">
+ <object class="GtkSpinner" id="spinner">
<property name="visible">True</property>
- <property name="active">True</property>
</object>
<packing>
<property name="name">spinner</property>
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]