Fix #7378 - search bar in files brower
[roobuilder] / src / Builder4 / PopoverFiles.bjs
index 5f2c0c5..8cde049 100644 (file)
@@ -3,6 +3,7 @@
  "items" : [
   {
    "# Gdk.Pixbuf missing_thumb_pixbuf" : "",
+   "# Gee.HashMap<string,Gdk.Pixbuf> image_cache" : "null",
    "# Project.Project selectedProject" : "",
    "# Xcls_MainWindow win" : "",
    "# bool is_loaded" : false,
@@ -12,6 +13,7 @@
    "Gtk.PositionType position" : "Gtk.PositionType.TOP",
    "bool active" : false,
    "bool hexpand" : false,
+   "bool in_onprojectselected" : false,
    "bool modal" : true,
    "id" : "PopoverFiles",
    "int height_request" : 800,
         },
         {
          "$ xns" : "Gtk",
-         "* init" : "  this.el.set_policy (Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC);",
-         "* pack" : "add",
-         "Gtk.ShadowType shadow_type" : "Gtk.ShadowType.IN",
+         "Gtk.Orientation orientation" : "Gtk.Orientation.VERTICAL",
          "bool expand" : true,
-         "id" : "iconscroll",
+         "int spacing" : 0,
          "int width_request" : 600,
          "items" : [
+          {
+           "$ xns" : "Gtk",
+           "Gtk.Orientation orientation" : "Gtk.Orientation.HORIZONTAL",
+           "bool hexpand" : true,
+           "int spacing" : 0,
+           "items" : [
+            {
+             "$ xns" : "Gtk",
+             "* init" : [
+              "var description =   Pango.FontDescription.from_string(\"monospace\");",
+              "\tdescription.set_size(8000);",
+              "\t this.el.override_font(description);",
+              "",
+              ""
+             ],
+             "bool hexpand" : true,
+             "id" : "iconsearch",
+             "listeners" : {
+              "changed" : [
+               "( ) => {",
+               "\tGLib.debug(\"Got '%s'\", this.el.text);",
+               "\t",
+               "\tif (this.el.text.down() != _this.lastfilter) {",
+               "\t\t_this.loadIconView();",
+               "\t}",
+               "}",
+               ""
+              ]
+             },
+             "string placeholder_text" : "type to filter results",
+             "xtype" : "SearchEntry"
+            }
+           ],
+           "xtype" : "Box"
+          },
           {
            "$ xns" : "Gtk",
            "* init" : [
-            "{",
-            "",
-            "}",
-            ""
+            "  this.el.set_policy (Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC);",
+            "  "
            ],
            "* pack" : "add",
-           "bool has_tooltip" : true,
-           "id" : "iconview",
-           "int item_width" : 100,
-           "int markup_column" : 1,
-           "int pixbuf_column" : 3,
+           "Gtk.ShadowType shadow_type" : "Gtk.ShadowType.IN",
+           "bool expand" : true,
+           "id" : "iconscroll",
+           "int width_request" : 600,
            "items" : [
             {
-             "$ columns" : "typeof(Object), typeof(string), typeof(string), typeof(Gdk.Pixbuf), typeof(Gdk.Pixbuf) ",
              "$ xns" : "Gtk",
-             "* prop" : "model",
-             "id" : "iconmodel",
-             "int n_columns" : 4,
-             "xtype" : "ListStore"
+             "* init" : [
+              "{",
+              " ",
+              "}",
+              ""
+             ],
+             "* pack" : "add",
+             "bool has_tooltip" : true,
+             "id" : "iconview",
+             "int item_width" : 100,
+             "int markup_column" : 1,
+             "int pixbuf_column" : 3,
+             "items" : [
+              {
+               "$ columns" : "typeof(Object), typeof(string), typeof(string), typeof(Gdk.Pixbuf), typeof(Gdk.Pixbuf) ",
+               "$ xns" : "Gtk",
+               "* prop" : "model",
+               "id" : "iconmodel",
+               "int n_columns" : 4,
+               "xtype" : "ListStore"
+              }
+             ],
+             "listeners" : {
+              "item_activated" : [
+               "(path) => {",
+               "    ",
+               "    _this.win.windowstate.project = _this.selectedProject;",
+               "    _this.el.hide();",
+               "    ",
+               "    ",
+               " \tGtk.TreeIter iter;",
+               "   ",
+               "            ",
+               "\tthis.el.model.get_iter(out iter, path);",
+               "    ",
+               "    GLib.Value gval;",
+               "",
+               "    this.el.model.get_value(iter, 0 , out gval);",
+               "    var file = (JsRender.JsRender)gval;",
+               "    ",
+               "    ",
+               "    _this.win.windowstate.fileViewOpen(file, _this.new_window);",
+               "",
+               "    ",
+               "    ",
+               "}"
+              ],
+              "query_tooltip" : [
+               "(x, y, keyboard_tooltip, tooltip) => {",
+               "",
+               "\tGtk.TreePath path;",
+               "\tGtk.CellRenderer cell;",
+               "\tvar s = _this.iconview.el.get_item_at_pos(x,y + (int) _this.iconscroll.el.vadjustment.value, out path, out cell);",
+               "\t",
+               "\t",
+               "   // GLib.debug(\"Tooltip? %d,%d scroll: %d\",x,y, (int)_this.iconscroll.el.vadjustment.value);",
+               "\t ",
+               "\t",
+               "\tif (path == null) {",
+               "\t\t// GLib.debug(\"Tooltip? - no path\");",
+               "\t\treturn false;",
+               "\t}",
+               "\t",
+               "\tGtk.TreeIter iter;",
+               "\t_this.iconmodel.el.get_iter(out iter, path);",
+               "\tGLib.Value val;",
+               "\t_this.iconmodel.el.get_value(iter, 4, out val);",
+               "\t",
+               "\ttooltip.set_icon((Gdk.Pixbuf) val.get_object());",
+               "\t _this.iconview.el.set_tooltip_item(tooltip, path);",
+               "\treturn true;",
+               "}",
+               ""
+              ]
+             },
+             "xtype" : "IconView"
             }
            ],
-           "listeners" : {
-            "item_activated" : [
-             "(path) => {",
-             "    ",
-             "    _this.win.windowstate.project = _this.selectedProject;",
-             "    _this.el.hide();",
-             "    ",
-             "    ",
-             " \tGtk.TreeIter iter;",
-             "   ",
-             "            ",
-             "\tthis.el.model.get_iter(out iter, path);",
-             "    ",
-             "    GLib.Value gval;",
-             "",
-             "    this.el.model.get_value(iter, 0 , out gval);",
-             "    var file = (JsRender.JsRender)gval;",
-             "    ",
-             "    ",
-             "    _this.win.windowstate.fileViewOpen(file, _this.new_window);",
-             "",
-             "    ",
-             "    ",
-             "}"
-            ],
-            "query_tooltip" : [
-             "(x, y, keyboard_tooltip, tooltip) => {",
-             "",
-             "\tGtk.TreePath path;",
-             "\tGtk.CellRenderer cell;",
-             "\tvar s = _this.iconview.el.get_item_at_pos(x,y + (int) _this.iconscroll.el.vadjustment.value, out path, out cell);",
-             "\t",
-             "\t",
-             "   // GLib.debug(\"Tooltip? %d,%d scroll: %d\",x,y, (int)_this.iconscroll.el.vadjustment.value);",
-             "\t ",
-             "\t",
-             "\tif (path == null) {",
-             "\t\t// GLib.debug(\"Tooltip? - no path\");",
-             "\t\treturn false;",
-             "\t}",
-             "\t",
-             "\tGtk.TreeIter iter;",
-             "\t_this.iconmodel.el.get_iter(out iter, path);",
-             "\tGLib.Value val;",
-             "\t_this.iconmodel.el.get_value(iter, 4, out val);",
-             "\t",
-             "\ttooltip.set_icon((Gdk.Pixbuf) val.get_object());",
-             "\t _this.iconview.el.set_tooltip_item(tooltip, path);",
-             "\treturn true;",
-             "}",
-             ""
-            ]
-           },
-           "xtype" : "IconView"
+           "xtype" : "ScrolledWindow"
           }
          ],
-         "xtype" : "ScrolledWindow"
+         "xtype" : "Box"
         },
         {
          "$ xns" : "Gtk",
      "}"
     ]
    },
+   "string lastfilter" : "\"\"",
    "xtype" : "Popover",
    "| void load" : [
     "() {",
     "}",
     ""
    ],
-   "| void onProjectSelected" : [
-    "(Project.Project project) ",
-    "{",
-    "\tthis.selectedProject = project;",
-    "\tproject.scanDirs();",
-    "\t//this.clutterfiles.loadProject(proj);",
+   "| void loadIconView" : [
+    "() {",
     "\t",
+    "\tif (_this.image_cache == null) {",
+    "\t\t_this.image_cache = new Gee.HashMap<string,Gdk.Pixbuf>();",
+    "\t}",
     "\t",
-    "\t ",
-    "",
-    "    ",
-    "    ",
-    "    //this.project_title_name.el.text = pr.name;",
-    "    //this.project_title_path.el.text = pr.firstPath();",
-    "    ",
-    "    // file items contains a reference until we reload ...",
-    "  \t Gdk.Pixbuf pixbuf = null;",
-    "  \tGdk.Pixbuf bigpixbuf = null;",
+    "\t var project =  this.selectedProject;",
+    " ",
+    " \t Gdk.Pixbuf pixbuf = null;",
+    "  \t Gdk.Pixbuf bigpixbuf = null;",
     "\t Gtk.TreeIter iter;",
     "     var m = this.iconmodel.el;",
     "     m.clear();",
     " ",
+    " ",
+    " \tvar filter = _this.iconsearch.el.text.down();",
+    " \tthis.lastfilter = filter;",
+    " ",
     "    var fiter = project.sortedFiles().list_iterator();",
+    "    ",
+    "    ",
+    "      try {",
+    "\t        if (_this.missing_thumb_pixbuf == null) {",
+    "\t            var icon_theme = Gtk.IconTheme.get_default ();",
+    "\t            _this.missing_thumb_pixbuf = icon_theme.load_icon (\"package-x-generic\", 92, 0);",
+    "\t            _this.missing_thumb_pixbuf.ref();",
+    "\t        }",
+    "\t        ",
+    "",
+    "\t    } catch (Error e) {",
+    "\t        // noop?",
+    "\t    }",
+    "    ",
+    "",
+    "    ",
     "    while (fiter.next()) {",
-    "        m.append(out iter);",
+    "    ",
     "        var file = fiter.get();",
+    "        if (filter != \"\") {",
+    "        \tif (!file.name.down().contains(filter)) {",
+    "        \t\tcontinue;",
+    "    \t\t}",
+    "        ",
+    "        }    ",
+    "    \t",
+    "    ",
+    "    ",
+    "        m.append(out iter);",
+    "",
     "        m.set(iter,   0,file ); // zero contains the file reference",
     "        m.set(iter,   1,file.nickType() + \"\\n\" + file.nickName()); // marked up title?",
     "        m.set(iter,   2,file.nickType() ); // file type?",
     "        ",
-    "        ",
-    "        var fname = file.getIconFileName(false);",
+    "       ",
+    "",
     "        try {",
-    "\t\t    if (FileUtils.test(fname, FileTest.EXISTS)) {",
-    "\t\t        var npixbuf = new Gdk.Pixbuf.from_file(fname);",
-    "\t\t        pixbuf = npixbuf.scale_simple(92, (int) (npixbuf.height * 92.0 /npixbuf.width * 1.0 )",
-    "\t\t\t\t    , Gdk.InterpType.NEAREST) ;",
-    "\t\t\t\tbigpixbuf = npixbuf.scale_simple(368, (int) (npixbuf.height * 368.0 /npixbuf.width * 1.0 )",
-    "\t\t\t\t    , Gdk.InterpType.NEAREST) ;",
-    "\t\t\t\t",
-    "\t\t    } ",
+    "\t\t    ",
+    "\t\t    pixbuf = file.getIcon(92);",
+    "\t\t\tbigpixbuf = file.getIcon(368);",
+    "",
     "\t\t} catch (Error e) {",
     "\t\t    // noop",
     "\t\t",
-    "\t\t}",
-    "        if (pixbuf == null) {",
+    "\t\t} ",
     "        ",
-    "\t\t    try {",
-    "\t\t        if (_this.missing_thumb_pixbuf == null) {",
-    "\t\t            var icon_theme = Gtk.IconTheme.get_default ();",
-    "\t\t            _this.missing_thumb_pixbuf = icon_theme.load_icon (\"package-x-generic\", 92, 0);",
-    "\t\t            _this.missing_thumb_pixbuf.ref();",
-    "\t\t        }",
-    "\t\t        pixbuf = _this.missing_thumb_pixbuf;",
-    "\t\t        bigpixbuf = _this.missing_thumb_pixbuf;",
-    "",
-    "\t\t    } catch (Error e) {",
-    "\t\t        // noop?",
-    "\t\t    }",
+    "         ",
+    "        if (pixbuf == null) {",
+    "        \tGLib.debug(\"PIXBUF is null? %s\", file.name);",
+    "\t\t    pixbuf = _this.missing_thumb_pixbuf;",
+    "        \tbigpixbuf = _this.missing_thumb_pixbuf;",
     "\t\t}",
     "\t\t",
     "\t\t",
     "",
     "        //this.filelayout.el.add_child(a.el);",
     "    }",
+    "}"
+   ],
+   "| void onProjectSelected" : [
+    "(Project.Project project) ",
+    "{",
+    "\tif (this.in_onprojectselected) { ",
+    "\t\treturn;",
+    "\t}",
+    "\tthis.in_onprojectselected = true;",
+    "\t",
+    "\t",
+    "\tthis.selectedProject = project;",
+    "\tproject.scanDirs();",
+    "\t//this.clutterfiles.loadProject(proj);",
+    "\t",
+    "\t",
+    "\t",
+    "\t_this.iconsearch.el.text = \"\";",
+    "\t",
+    "\t ",
+    "\t",
+    "    ",
     "    ",
+    "    //this.project_title_name.el.text = pr.name;",
+    "    //this.project_title_path.el.text = pr.firstPath();",
+    "    ",
+    "    // file items contains a reference until we reload ...",
+    "  \t this.loadIconView();",
+    "  \t ",
+    "  \t ",
+    "  \t GLib.Timeout.add(500, () => {",
+    "\t     _this.iconsearch.el.grab_focus();",
+    "\t     return false;",
+    "     });",
     "    ",
     "     this.filemodel.el.clear();",
     "    ",
     "    if (!(project is Project.Gtk)) {",
     "        print (\"not gtk... skipping files\");",
     "        this.file_container.el.hide();",
+    "    \tthis.in_onprojectselected = false;",
     "        return;",
     "    }",
     "    this.file_container.el.show();",
     "\t    //this.el.set_value(citer, 1,   items.get(i) );",
     "\t}",
     "    _this.fileview.el.expand_all();",
-    "    ",
+    "    \tthis.in_onprojectselected = false;",
     "\t",
     "}",
     ""