[gnome-documents] selections: special case clicks on OK when a collection is being typed



commit f9d9e8b8f965b57cdc20429e6a3e2f986b1b9037
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Sat May 12 16:33:47 2012 -0400

    selections: special case clicks on OK when a collection is being typed
    
    See the comment in the code for why this is necessary, but in a
    nutshell, GtkCellRenderer will emit editing-canceled as soon as any
    button is clicked (since we focus out of the editable in order to click
    the button), and we don't want "focus-out" to mean "cancel" if we
    clicked on the OK button.

 src/selections.js |   59 +++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 48 insertions(+), 11 deletions(-)
---
diff --git a/src/selections.js b/src/selections.js
index 846c0af..ef1f963 100644
--- a/src/selections.js
+++ b/src/selections.js
@@ -431,11 +431,15 @@ OrganizeCollectionModel.prototype = {
         }
     },
 
-    forgetPlaceholder: function() {
-        let path = this._placeholderRef.get_path();
-        this._placeholderRef = null;
+    getPlaceholder: function(forget) {
+        let ret = null;
+
+        if (this._placeholderRef)
+            ret = this._placeholderRef.get_path();
+        if (forget)
+            this._placeholderRef = null;
 
-        return path;
+        return ret;
     },
 
     destroy: function() {
@@ -457,6 +461,8 @@ function OrganizeCollectionView() {
 
 OrganizeCollectionView.prototype = {
     _init: function() {
+        this._choiceConfirmed = false;
+
         this._model = new OrganizeCollectionModel();
         this.widget = new Gtk.TreeView({ headers_visible: false,
                                          vexpand: true,
@@ -516,7 +522,7 @@ OrganizeCollectionView.prototype = {
             return;
         }
 
-        let path = this._model.forgetPlaceholder();
+        let path = this._model.getPlaceholder(true);
         if (!path)
             return;
 
@@ -527,7 +533,7 @@ OrganizeCollectionView.prototype = {
         job.run(null);
     },
 
-    _onTextEdited: function(cell, pathStr, newText) {
+    _onTextEditedReal: function(cell, path, newText) {
         cell.editable = false;
 
         if (!newText || newText == '') {
@@ -537,17 +543,30 @@ OrganizeCollectionView.prototype = {
         }
 
         // update the new name immediately
-        let path = Gtk.TreePath.new_from_string(pathStr);
         let iter = this._model.model.get_iter(path)[1];
         this._model.model.set_value(iter, OrganizeModelColumns.NAME, newText);
 
         // actually create the new collection
         let job = new CreateCollectionJob(newText);
-        job.run(Lang.bind(this, this._onNewCollectionCreated, path));
+        job.run(Lang.bind(this, this._onNewCollectionCreated));
     },
 
-    _onTextEditCanceled: function() {
-        this._model.removePlaceholder();
+    _onTextEdited: function(cell, pathStr, newText) {
+        this._onTextEditedReal(cell, Gtk.TreePath.new_from_string(pathStr), newText);
+    },
+
+    _onTextEditCanceled: function(cell) {
+        if (this._choiceConfirmed) {
+            this._choiceConfirmed = false;
+
+            let entry = this._viewCol.cell_area.get_edit_widget();
+            let path = this._model.getPlaceholder(false);
+
+            if (entry && path)
+                this._onTextEditedReal(cell, path, entry.get_text());
+        } else {
+            this._model.removePlaceholder();
+        }
     },
 
     _checkCellFunc: function(col, cell, model, iter) {
@@ -580,6 +599,10 @@ OrganizeCollectionView.prototype = {
 
         this._rendererText.editable = true;
         this.widget.set_cursor_on_cell(path, this._viewCol, this._rendererText, true);
+    },
+
+    confirmedChoice: function() {
+        this._choiceConfirmed = true;
     }
 };
 
@@ -600,7 +623,7 @@ OrganizeCollectionDialog.prototype = {
                                        default_height: 250 });
 
         this.widget.add_button('gtk-add', OrganizeCollectionDialogResponse.ADD);
-        this.widget.add_button('gtk-ok', Gtk.ResponseType.OK);
+        let okButton = this.widget.add_button('gtk-ok', Gtk.ResponseType.OK);
         this.widget.set_default_response(Gtk.ResponseType.OK);
 
         let contentArea = this.widget.get_content_area();
@@ -613,6 +636,20 @@ OrganizeCollectionDialog.prototype = {
         sw.add(collView.widget);
         contentArea.add(sw);
 
+        // HACK:
+        // - We want clicking on "OK" to add the typed-in collection if we're editing.
+        // - Unfortunately, since we focus out of the editable entry in order to
+        //   click the button, we'll get an editing-canceled signal on the renderer
+        //   from GTK. As this handler will run before focus-out, we here signal the
+        //   view to ignore the next editing-canceled signal and add the collection in
+        //   that case instead.
+        //
+        okButton.connect('button-press-event', Lang.bind(this,
+            function() {
+                collView.confirmedChoice();
+                return false;
+            }));
+
         this.widget.connect('response', Lang.bind(this,
             function(widget, response) {
                 if (response == OrganizeCollectionDialogResponse.ADD)



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