Jelajahi Sumber

LibGUI: Allow rollback of model editing delegate input

In the StringModelEditingDelegate convenience class, we simply hook up
the escape key to editor rollback. This means you can cancel an ongoing
cell edit by pressing escape. :^)
Andreas Kling 5 tahun lalu
induk
melakukan
12dfeb9845

+ 4 - 0
Libraries/LibGUI/AbstractView.cpp

@@ -147,6 +147,10 @@ void AbstractView::begin_editing(const ModelIndex& index)
         model()->set_data(m_edit_index, m_editing_delegate->value());
         stop_editing();
     };
+    m_editing_delegate->on_rollback = [this] {
+        ASSERT(model());
+        stop_editing();
+    };
 }
 
 void AbstractView::stop_editing()

+ 9 - 0
Libraries/LibGUI/ModelEditingDelegate.h

@@ -49,6 +49,7 @@ public:
     const Widget* widget() const { return m_widget; }
 
     Function<void()> on_commit;
+    Function<void()> on_rollback;
 
     virtual Variant value() const = 0;
     virtual void set_value(const Variant&) = 0;
@@ -64,6 +65,11 @@ protected:
         if (on_commit)
             on_commit();
     }
+    void rollback()
+    {
+        if (on_rollback)
+            on_rollback();
+    }
 
     const ModelIndex& index() const { return m_index; }
 
@@ -84,6 +90,9 @@ public:
         textbox->on_return_pressed = [this] {
             commit();
         };
+        textbox->on_escape_pressed = [this] {
+            rollback();
+        };
         return textbox;
     }
     virtual Variant value() const override { return static_cast<const TextBox*>(widget())->text(); }