[Xfce4-commits] [apps/xfmpc] 03/03: Implement GTK2-style progress bar + further fixes

noreply at xfce.org noreply at xfce.org
Sat Feb 23 20:11:13 CET 2019


This is an automated email from the git hooks/post-receive script.

a   n   d   r   e       p   u   s   h   e   d       a       c   o   m   m   i   t       t   o       b   r   a   n   c   h       m   a   s   t   e   r   
   in repository apps/xfmpc.

commit 2cc3be4120f0d98fcc646d0aac7509504fcd0c16
Author: Johannes Marbach <n0-0ne+xfce at mailbox.org>
Date:   Tue Feb 19 21:52:59 2019 +0100

    Implement GTK2-style progress bar + further fixes
    
    This uses a Gtk.CellRendererProgress inside
    a treeview to imitate the looks of progress
    bars on GTK+2. The cell background is derived
    from the bar's toplevel.
    
    In addition, CSS styles for coloring the search
    entry in the DB browser and defining the padding
    on the main playback buttons are applied.
    
    Finally, the GLib search timeout in the DB
    browser is being reset after triggering which
    fixes a "source ID was not found when attempting
    to remove it" warning on the console.
---
 src/Makefile.am       |   1 +
 src/dbbrowser.vala    |  34 +++-------------
 src/interface.vala    |   9 ++++-
 src/main-window.vala  |  14 +++++++
 src/progress-bar.vala | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 133 insertions(+), 30 deletions(-)

diff --git a/src/Makefile.am b/src/Makefile.am
index d8f83bb..1bea76a 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -19,6 +19,7 @@ xfmpc_SOURCES =								\
 	main-window.vala						\
 	playlist.vala							\
 	preferences.vala						\
+	progress-bar.vala						\
 	main.vala							\
 	mpdclient.c							\
 	xfce-arrow-button.c
diff --git a/src/dbbrowser.vala b/src/dbbrowser.vala
index d27a808..e0469c4 100644
--- a/src/dbbrowser.vala
+++ b/src/dbbrowser.vala
@@ -234,42 +234,23 @@ namespace Xfmpc {
 
 			bool no_result_buf = false, no_result = false;
 
-			var color = new Gdk.RGBA ();
-			color.parse ("#F66");
-
 			if (i == 0)
 				no_result = true;
 			else if (no_result)
 				no_result = false;
 
 			if (no_result == no_result_buf && no_result) {
-#if MORE_FUNKY_COLOR_ON_SEARCH_ENTRY
-				this.search_entry.modify_base (Gtk.StateType.NORMAL, color);
-#endif
-				this.search_entry.override_background_color (Gtk.StateFlags.NORMAL, color);
-				this.search_entry.override_background_color (Gtk.StateFlags.SELECTED, color);
+				this.search_entry.get_style_context ().add_class ("red");
 			}
 			else if (no_result == no_result_buf && !no_result) {
-#if MORE_FUNKY_COLOR_ON_SEARCH_ENTRY
-				this.search_entry.modify_base (Gtk.StateFlags.NORMAL, null);
-#endif
-				this.search_entry.override_background_color (Gtk.StateFlags.NORMAL, null);
-				this.search_entry.override_background_color (Gtk.StateFlags.SELECTED, null);
+				this.search_entry.get_style_context ().remove_class ("red");
 			}
 
 			if (i == 0) {
-#if MORE_FUNKY_COLOR_ON_SEARCH_ENTRY
-      				this.search_entry.modify_base (Gtk.StateFlags.NORMAL, color);
-#endif
-				this.search_entry.override_background_color (Gtk.StateFlags.NORMAL, color);
-				this.search_entry.override_background_color (Gtk.StateFlags.SELECTED, color);
+				this.search_entry.get_style_context ().add_class ("red");
 			}
 			else if (no_result) {
-#if MORE_FUNKY_COLOR_ON_SEARCH_ENTRY
-      				this.search_entry.modify_base (Gtk.StateFlags.NORMAL, null);
-#endif
-				this.search_entry.override_background_color (Gtk.StateFlags.NORMAL, null);
-				this.search_entry.override_background_color (Gtk.StateFlags.SELECTED, null);
+				this.search_entry.get_style_context ().remove_class ("red");
 			}
 		}
 
@@ -434,11 +415,7 @@ namespace Xfmpc {
 				reload ();
 
       	      	      	      	/* revert possible previous applied color */
-#if MORE_FUNKY_COLOR_ON_SEARCH_ENTRY
-				this.search_entry.modify_base (Gtk.StateFlags.NORMAL, null);
-#endif
-				this.search_entry.override_background_color (Gtk.StateFlags.NORMAL, null);
-				this.search_entry.override_background_color (Gtk.StateFlags.SELECTED, null);
+				this.search_entry.get_style_context ().remove_class ("red");
 
 				return;
 			}
@@ -473,6 +450,7 @@ namespace Xfmpc {
 
 		private bool timeout_search () {
 			cb_search_entry_activated ();
+			this.search_timeout = 0;
 			return false;
 		}
 
diff --git a/src/interface.vala b/src/interface.vala
index 4918cd3..41ff5a1 100644
--- a/src/interface.vala
+++ b/src/interface.vala
@@ -30,7 +30,7 @@ namespace Xfmpc {
 		private Gtk.Button button_pp;
 		private Gtk.Button button_next;
 		private Gtk.VolumeButton button_volume;
-		private Gtk.ProgressBar progress_bar;
+		private Xfmpc.ProgressBar progress_bar;
 		private Gtk.Label title;
 		private Gtk.Label subtitle;
 
@@ -47,29 +47,34 @@ namespace Xfmpc {
 			this.button_prev = new Gtk.Button ();
 			this.button_prev.set_relief (Gtk.ReliefStyle.NONE);
 			this.button_prev.add (image);
+			this.button_prev.get_style_context ().add_class ("primary-button");
 
 			image = new Gtk.Image.from_icon_name ("media-playback-start", Gtk.IconSize.BUTTON);
 			this.button_pp = new Gtk.Button ();
 			this.button_pp.set_relief (Gtk.ReliefStyle.NONE);
 			this.button_pp.add (image);
+			this.button_pp.get_style_context ().add_class ("primary-button");
 
 			image = new Gtk.Image.from_icon_name ("media-skip-forward", Gtk.IconSize.BUTTON);
 			this.button_next = new Gtk.Button ();
 			this.button_next.set_relief (Gtk.ReliefStyle.NONE);
 			this.button_next.add (image);
+			this.button_next.get_style_context ().add_class ("primary-button");
 
 			this.button_volume = new Gtk.VolumeButton ();
 			this.button_volume.set_relief (Gtk.ReliefStyle.NONE);
+			this.button_volume.get_style_context ().add_class ("primary-button");
 			var adjustment = button_volume.get_adjustment ();
 			adjustment.upper *= 100;
 			adjustment.step_increment *= 100;
 			adjustment.page_increment *= 100;
 
 			var progress_box = new Gtk.EventBox ();
-			progress_bar = new Gtk.ProgressBar ();
+			progress_bar = new Xfmpc.ProgressBar ();
 			progress_bar.set_text ("0:00 / 0:00");
 			progress_bar.set_fraction (1.0);
 			progress_box.add (progress_bar);
+			progress_box.set_above_child (true);
 
   	  	  	/* Title */
 			var attrs = new Pango.AttrList ();
diff --git a/src/main-window.vala b/src/main-window.vala
index 5079c94..9856ac8 100644
--- a/src/main-window.vala
+++ b/src/main-window.vala
@@ -66,6 +66,8 @@ namespace Xfmpc {
 			mpdclient = Xfmpc.Mpdclient.get_default ();
 			preferences = Xfmpc.Preferences.get_default ();
 
+			MainWindow.load_css ();
+
 			/* StatusIcon */
 			this.status_icon = new Gtk.StatusIcon.from_icon_name ("stock_volume");
 			show_hide_status_icon ();
@@ -135,6 +137,18 @@ namespace Xfmpc {
 			Gdk.threads_add_timeout (1000, refresh);
 		}
 
+		private static void load_css () {
+			var provider = new Gtk.CssProvider ();
+			provider.load_from_data (
+				".primary-button { padding-left: 8px; padding-right: 8px; }" +
+				".red { background-color: #FF6666; }",
+				-1);
+			Gtk.StyleContext.add_provider_for_screen (
+				Gdk.Screen.get_default (),
+				provider,
+				Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
+		}
+
 		private void show_hide_status_icon () {
 			this.status_icon.set_visible (this.preferences.show_status_icon);
 		}
diff --git a/src/progress-bar.vala b/src/progress-bar.vala
new file mode 100644
index 0000000..7b72c89
--- /dev/null
+++ b/src/progress-bar.vala
@@ -0,0 +1,105 @@
+/*
+ *  Copyright (c) 2019 Johannes Marbach <n0-0ne+xfce at mailbox.org>
+ *
+ *  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 2 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, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+using Gdk;
+using Gtk;
+
+namespace Xfmpc {
+
+	public class ProgressBar : Box {
+
+		private Gtk.TreeView treeview;
+		private Gtk.ListStore store;
+		private Gtk.TreeIter iter;
+
+		private enum Columns {
+		  COLUMN_PERCENTAGE,
+		  COLUMN_TEXT,
+		  COLUMN_BACKGROUND,
+		  N_COLUMNS
+		}
+
+		construct {
+			set_orientation (Gtk.Orientation.VERTICAL);
+
+			store = new Gtk.ListStore (Columns.N_COLUMNS,
+						   typeof (double),
+						   typeof (string),
+						   typeof (Gdk.RGBA));
+
+			store.append (out iter);
+
+			treeview = new Gtk.TreeView ();
+			treeview.set_headers_visible (false);
+			treeview.set_model (store);
+			treeview.get_selection ().set_mode (Gtk.SelectionMode.NONE);
+
+			treeview.hierarchy_changed.connect (cb_hierarchy_changed);
+			treeview.style_updated.connect (cb_style_updated);
+
+			var renderer = new Gtk.CellRendererProgress ();
+			renderer.height = 32;
+			treeview.insert_column_with_attributes (-1, null, renderer,
+								"value", Columns.COLUMN_PERCENTAGE,
+								"text", Columns.COLUMN_TEXT,
+								"cell-background-rgba", Columns.COLUMN_BACKGROUND, null);
+
+			pack_start (treeview, true, true, 0);
+		}
+
+		public void set_text (string text) {
+			store.set (iter,
+				   Columns.COLUMN_TEXT, text,
+				   -1);
+		}
+
+		public void set_fraction (double fraction) {
+			store.set (iter,
+				   Columns.COLUMN_PERCENTAGE, fraction * 100.0,
+				   -1);
+		}
+
+		private void cb_hierarchy_changed (Widget widget, Widget? previous_toplevel) {
+			update_background_color ();
+		}
+
+		private void cb_style_updated (Widget widget) {
+			update_background_color ();
+		}
+
+		private void update_background_color () {
+			var toplevel = get_toplevel ();
+			if (!toplevel.get_type ().is_a (typeof (Gtk.Window))) {
+				return;
+			}
+
+			var context = toplevel.get_style_context ();
+
+			var value = context.get_property (Gtk.STYLE_PROPERTY_BACKGROUND_COLOR, Gtk.StateFlags.NORMAL);
+			if (!value.holds (typeof (Gdk.RGBA))) {
+				return;
+			}
+
+			store.set (iter,
+				Columns.COLUMN_BACKGROUND, (Gdk.RGBA *) value.get_boxed (),
+				-1);
+		}
+
+	}
+
+}

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the Xfce4-commits mailing list