summaryrefslogtreecommitdiff
path: root/Libraries/LibGUI
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-08-28 20:27:51 +0200
committerAndreas Kling <kling@serenityos.org>2020-08-28 20:40:12 +0200
commitf3e4b62be99aeb3f1df825212252f52287b78a93 (patch)
tree7ae62051a03974a17c5235888311f7409736aa9d /Libraries/LibGUI
parent383ee279eec0c9cd7eeb864f7a0297c0d97fb51d (diff)
downloadserenity-f3e4b62be99aeb3f1df825212252f52287b78a93.zip
LibGUI: Add AbstractView "edit triggers" to improve editing control
This API allows the embedder of a view to decide which actions upon the view will begin editing the current item. To maintain the old behavior, we will begin editing when an item is either double-clicked, or when the "edit key" (return) is pressed.
Diffstat (limited to 'Libraries/LibGUI')
-rw-r--r--Libraries/LibGUI/AbstractTableView.cpp6
-rw-r--r--Libraries/LibGUI/AbstractView.cpp16
-rw-r--r--Libraries/LibGUI/AbstractView.h12
-rw-r--r--Libraries/LibGUI/TableView.cpp5
4 files changed, 27 insertions, 12 deletions
diff --git a/Libraries/LibGUI/AbstractTableView.cpp b/Libraries/LibGUI/AbstractTableView.cpp
index 1ba86b1f7e..b2ffda454f 100644
--- a/Libraries/LibGUI/AbstractTableView.cpp
+++ b/Libraries/LibGUI/AbstractTableView.cpp
@@ -248,8 +248,10 @@ void AbstractTableView::doubleclick_event(MouseEvent& event)
if (!model())
return;
if (event.button() == MouseButton::Left) {
- if (!selection().is_empty())
- activate_or_edit_selected();
+ if (is_editable() && edit_triggers() & EditTrigger::DoubleClicked)
+ begin_editing(cursor_index());
+ else
+ activate(cursor_index());
}
}
diff --git a/Libraries/LibGUI/AbstractView.cpp b/Libraries/LibGUI/AbstractView.cpp
index 343167cc93..3c8e455cb2 100644
--- a/Libraries/LibGUI/AbstractView.cpp
+++ b/Libraries/LibGUI/AbstractView.cpp
@@ -362,15 +362,10 @@ void AbstractView::doubleclick_event(MouseEvent& event)
else if (!m_selection.contains(index))
set_selection(index);
- activate_or_edit_selected();
-}
-
-void AbstractView::activate_or_edit_selected()
-{
- if (is_editable())
- begin_editing(selection().first());
+ if (is_editable() && edit_triggers() & EditTrigger::DoubleClicked)
+ begin_editing(cursor_index());
else
- activate_selected();
+ activate(cursor_index());
}
void AbstractView::context_menu_event(ContextMenuEvent& event)
@@ -449,4 +444,9 @@ void AbstractView::set_cursor(ModelIndex index, SelectionUpdate selection_update
}
}
+void AbstractView::set_edit_triggers(unsigned triggers)
+{
+ m_edit_triggers = triggers;
+}
+
}
diff --git a/Libraries/LibGUI/AbstractView.h b/Libraries/LibGUI/AbstractView.h
index 1a648fcd72..70850fdb62 100644
--- a/Libraries/LibGUI/AbstractView.h
+++ b/Libraries/LibGUI/AbstractView.h
@@ -67,6 +67,16 @@ public:
bool is_editable() const { return m_editable; }
void set_editable(bool editable) { m_editable = editable; }
+ enum EditTrigger {
+ None = 0,
+ DoubleClicked = 1 << 0,
+ EditKeyPressed = 1 << 1,
+ AnyKeyPressed = 1 << 2,
+ };
+
+ unsigned edit_triggers() const { return m_edit_triggers; }
+ void set_edit_triggers(unsigned);
+
bool is_multi_select() const { return m_multi_select; }
void set_multi_select(bool);
@@ -128,7 +138,6 @@ protected:
void set_hovered_index(const ModelIndex&);
void activate(const ModelIndex&);
void activate_selected();
- void activate_or_edit_selected();
void update_edit_widget_position();
bool m_editable { false };
@@ -150,6 +159,7 @@ private:
OwnPtr<ModelEditingDelegate> m_editing_delegate;
ModelSelection m_selection;
ModelIndex m_cursor_index;
+ unsigned m_edit_triggers { EditTrigger::DoubleClicked | EditTrigger::EditKeyPressed };
bool m_activates_on_selection { false };
bool m_multi_select { true };
};
diff --git a/Libraries/LibGUI/TableView.cpp b/Libraries/LibGUI/TableView.cpp
index a3581d9594..4309d307bf 100644
--- a/Libraries/LibGUI/TableView.cpp
+++ b/Libraries/LibGUI/TableView.cpp
@@ -165,7 +165,10 @@ void TableView::keydown_event(KeyEvent& event)
if (!model())
return;
if (event.key() == KeyCode::Key_Return) {
- activate_or_edit_selected();
+ if (is_editable() && edit_triggers() & EditTrigger::EditKeyPressed)
+ begin_editing(cursor_index());
+ else
+ activate(cursor_index());
return;
}
return AbstractTableView::keydown_event(event);