[gnome-boxes] searchbar: Derive from Gtk.SearchBar



commit 4b7b6b856cc97f722229b61ba3091abf66a9d96d
Author: Zeeshan Ali (Khattak) <zeeshanak gnome org>
Date:   Fri Jan 31 16:53:07 2014 +0000

    searchbar: Derive from Gtk.SearchBar
    
    This makes use of Boxes.Revealer and Clutter redundant to pack the
    searchbar apart from saving us a lot of code.

 src/app.vala       |   46 +++++++-------------
 src/searchbar.vala |  118 +++++++++-------------------------------------------
 src/topbar.vala    |    4 +-
 3 files changed, 38 insertions(+), 130 deletions(-)
---
diff --git a/src/app.vala b/src/app.vala
index c6b6ccc..b79f0fc 100644
--- a/src/app.vala
+++ b/src/app.vala
@@ -75,7 +75,6 @@ private class Boxes.App: GLib.Object, Boxes.UI {
     public Topbar topbar;
     public Notificationbar notificationbar;
     public Boxes.Revealer sidebar_revealer;
-    public Boxes.Revealer searchbar_revealer;
     public Sidebar sidebar;
     public Selectionbar selectionbar;
     public uint duration;
@@ -313,10 +312,8 @@ private class Boxes.App: GLib.Object, Boxes.UI {
         if (opt_search != null) {
             call_when_ready (() => {
                 searchbar.text = string.joinv (" ", opt_search);
-                searchbar.visible = true;
-                if (ui_state == UIState.COLLECTION) {
-                    searchbar_revealer.revealed = true;
-                }
+                if (ui_state == UIState.COLLECTION)
+                    searchbar.visible = true;
             });
         }
 
@@ -527,8 +524,19 @@ private class Boxes.App: GLib.Object, Boxes.UI {
         notebook.show_border = false;
         notebook.show_tabs = false;
         main_vbox.add (notebook);
+
+        var vbox = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
+        vbox.halign = Gtk.Align.FILL;
+        vbox.valign = Gtk.Align.FILL;
+        vbox.hexpand = true;
+        vbox.vexpand = true;
+        notebook.append_page (vbox, null);
+
+        searchbar = new Searchbar ();
+        vbox.add (searchbar);
+
         embed = new ClutterWidget ();
-        notebook.append_page (embed, null);
+        vbox.add (embed);
 
         display_page = new DisplayPage ();
         notebook.append_page (display_page, null);
@@ -565,35 +573,14 @@ private class Boxes.App: GLib.Object, Boxes.UI {
 
         sidebar = new Sidebar ();
         view = new CollectionView ();
-        searchbar = new Searchbar ();
         topbar = new Topbar ();
         notificationbar = new Notificationbar ();
         wizard = new Wizard ();
         properties = new Properties ();
         empty_boxes = new EmptyBoxes ();
 
-        var vbox_actor = new Clutter.Actor ();
-        vbox_actor.name = "top-vbox";
-        var vbox = new Clutter.BoxLayout ();
-        vbox_actor.set_layout_manager (vbox);
-        vbox.orientation = Clutter.Orientation.VERTICAL;
-        vbox_actor.x_align = Clutter.ActorAlign.FILL;
-        vbox_actor.y_align = Clutter.ActorAlign.FILL;
-        vbox_actor.x_expand = true;
-        vbox_actor.y_expand = true;
-
-        stage.add_child (vbox_actor);
-
         window.set_titlebar (topbar);
 
-        searchbar_revealer = new Boxes.Revealer (true);
-        searchbar_revealer.resize = true;
-        searchbar_revealer.unreveal ();
-        searchbar_revealer.name = "searchbar-revealer";
-        searchbar_revealer.x_expand = true;
-        vbox_actor.add_child (searchbar_revealer);
-        searchbar_revealer.add (searchbar.actor);
-
         var below_bin_actor = new Clutter.Actor ();
         below_bin_actor.name = "below-bin";
         var below_bin = new Clutter.BinLayout (Clutter.BinAlignment.START,
@@ -602,7 +589,7 @@ private class Boxes.App: GLib.Object, Boxes.UI {
 
         below_bin_actor.x_expand = true;
         below_bin_actor.y_expand = true;
-        vbox_actor.add_child (below_bin_actor);
+        stage.add_child (below_bin_actor);
 
         below_bin_actor.add_child (view.actor);
 
@@ -705,7 +692,7 @@ private class Boxes.App: GLib.Object, Boxes.UI {
             set_main_ui_state ();
 
         if (ui_state != UIState.COLLECTION)
-            searchbar_revealer.revealed = false;
+            searchbar.visible = false;
 
         switch (ui_state) {
         case UIState.COLLECTION:
@@ -722,7 +709,6 @@ private class Boxes.App: GLib.Object, Boxes.UI {
             }
             fullscreen = false;
             view.visible = true;
-            searchbar_revealer.revealed = searchbar.visible;
 
             // Animate current_item actor to collection position
             if (current_item != null) {
diff --git a/src/searchbar.vala b/src/searchbar.vala
index 17ca4ed..bf0ae5d 100644
--- a/src/searchbar.vala
+++ b/src/searchbar.vala
@@ -1,7 +1,16 @@
 // This file is part of GNOME Boxes. License: LGPLv2+
 
-private class Boxes.Searchbar: GLib.Object, Boxes.UI {
-    public Clutter.Actor actor { get { return gtk_actor; } }
+private class Boxes.Searchbar: Gtk.SearchBar, Boxes.UI {
+    // See FIXME note on Boxes.Topbar.actor
+    public Clutter.Actor actor {
+        get {
+            if (gtk_actor == null)
+                gtk_actor = new Clutter.Actor ();
+            return gtk_actor;
+        }
+    }
+    private Clutter.Actor gtk_actor;
+
     public UIState previous_ui_state { get; protected set; }
     public UIState ui_state { get; protected set; }
     public bool enable_key_handler {
@@ -12,7 +21,6 @@ private class Boxes.Searchbar: GLib.Object, Boxes.UI {
                 GLib.SignalHandler.block (App.app.window, key_handler_id);
         }
     }
-    private GtkClutter.Actor gtk_actor;
     private Gtk.SearchEntry entry;
 
     private ulong key_handler_id;
@@ -25,6 +33,11 @@ private class Boxes.Searchbar: GLib.Object, Boxes.UI {
         entry.activate.connect ( () => {
             App.app.view.activate ();
         });
+
+        notify["search-mode-enabled"].connect (() => {
+            if (!search_mode_enabled)
+                text = "";
+        });
     }
 
     private void on_search_changed () {
@@ -32,115 +45,24 @@ private class Boxes.Searchbar: GLib.Object, Boxes.UI {
         App.app.view.refilter ();
     }
 
-    private bool _visible;
-    public bool visible {
-        get {
-            return _visible;
-        }
-        set {
-            if (_visible == value)
-                return;
-
-            App.app.searchbar_revealer.revealed = value;
-            if (value)
-                entry.grab_focus ();
-            else
-                text = "";
-
-            _visible = value;
-        }
-    }
-
     public string text {
         get { return entry.text; }
         set { entry.set_text (value); }
     }
 
     private bool on_app_key_pressed (Gtk.Widget widget, Gdk.EventKey event) {
-        var handled = false;
-
         if (ui_state != UIState.COLLECTION)
-            return handled;
-
-        if (!entry.get_realized ()) {
-            actor.show ();
-            // should not needed since searchbar actor
-            // is inside hidden revealer, but it ensure
-            // it's not visible..
-            actor.hide ();
-        }
-
-        var preedit_changed = false;
-        var preedit_changed_id =
-            entry.preedit_changed.connect (() => { preedit_changed = true; });
-
-        var old_text = text;
-        if (!visible)
-            text = "";
-
-        if (visible && event.keyval == Gdk.Key.Escape) {
-            text = "";
-            visible = false;
-            return true;
-        }
-
-        if (!entry.has_focus && (event.keyval == Gdk.Key.space || event.keyval == Gdk.Key.Return))
             return false;
 
-        var res = false;
-
-        // Don't pass on keynav keys, or CTRL/ALT using keys to search
-        if (event.keyval != Gdk.Key.Tab &&
-            event.keyval != Gdk.Key.KP_Tab &&
-            event.keyval != Gdk.Key.Up &&
-            event.keyval != Gdk.Key.KP_Up &&
-            event.keyval != Gdk.Key.Down &&
-            event.keyval != Gdk.Key.KP_Down &&
-            event.keyval != Gdk.Key.Left &&
-            event.keyval != Gdk.Key.KP_Left &&
-            event.keyval != Gdk.Key.Right &&
-            event.keyval != Gdk.Key.KP_Right &&
-            event.keyval != Gdk.Key.Home &&
-            event.keyval != Gdk.Key.KP_Home &&
-            event.keyval != Gdk.Key.End &&
-            event.keyval != Gdk.Key.KP_End &&
-            event.keyval != Gdk.Key.Page_Up &&
-            event.keyval != Gdk.Key.KP_Page_Up &&
-            event.keyval != Gdk.Key.Page_Down &&
-            event.keyval != Gdk.Key.KP_Page_Down &&
-            ((event.state & (Gdk.ModifierType.CONTROL_MASK | Gdk.ModifierType.MOD1_MASK)) == 0))
-            res = entry.event ((Gdk.Event)(&event));
-        var new_text = text;
-
-        entry.disconnect (preedit_changed_id);
-
-        if (((res && (new_text != old_text)) || preedit_changed)) {
-            handled = true;
-            if (!visible)
-                visible = true;
-            else if (!entry.has_focus)
-                entry.grab_focus ();
-        }
-
-        return handled;
+        return handle_event ((Gdk.Event *) (&event));
     }
 
     private void setup_searchbar () {
-        var toolbar = new Gtk.Toolbar ();
-        toolbar.get_style_context ().add_class (Gtk.STYLE_CLASS_PRIMARY_TOOLBAR);
-
-        var item = new Gtk.ToolItem ();
-        toolbar.insert(item, 0);
-        item.set_expand (true);
-
         entry = new Gtk.SearchEntry ();
-        entry.width_request = 260;
+        entry.width_chars = 40;
         entry.hexpand = true;
-        entry.margin_left = entry.margin_right = 64;
-        item.add (entry);
+        add (entry);
 
-        toolbar.show_all ();
-        gtk_actor = new GtkClutter.Actor.with_contents (toolbar);
-        gtk_actor.name = "searchbar";
+        show_all ();
     }
 }
diff --git a/src/topbar.vala b/src/topbar.vala
index 4213a34..855bf35 100644
--- a/src/topbar.vala
+++ b/src/topbar.vala
@@ -82,8 +82,8 @@ private class Boxes.Topbar: Gtk.Notebook, Boxes.UI {
                                                                      "go-previous-symbolic";
         back_image.set_from_icon_name (back_icon, Gtk.IconSize.MENU);
 
-        search_btn.bind_property ("active", App.app.searchbar, "visible", BindingFlags.BIDIRECTIONAL);
-        search2_btn.bind_property ("active", App.app.searchbar, "visible", BindingFlags.BIDIRECTIONAL);
+        search_btn.bind_property ("active", App.app.searchbar, "search-mode-enabled", 
BindingFlags.BIDIRECTIONAL);
+        search2_btn.bind_property ("active", App.app.searchbar, "search-mode-enabled", 
BindingFlags.BIDIRECTIONAL);
 
         App.app.notify["selection-mode"].connect (() => {
             page = App.app.selection_mode ?


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