mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 15:48:12 +00:00
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.
This commit is contained in:
parent
383ee279ee
commit
f3e4b62be9
4 changed files with 27 additions and 12 deletions
|
@ -248,8 +248,10 @@ void AbstractTableView::doubleclick_event(MouseEvent& event)
|
||||||
if (!model())
|
if (!model())
|
||||||
return;
|
return;
|
||||||
if (event.button() == MouseButton::Left) {
|
if (event.button() == MouseButton::Left) {
|
||||||
if (!selection().is_empty())
|
if (is_editable() && edit_triggers() & EditTrigger::DoubleClicked)
|
||||||
activate_or_edit_selected();
|
begin_editing(cursor_index());
|
||||||
|
else
|
||||||
|
activate(cursor_index());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -362,15 +362,10 @@ void AbstractView::doubleclick_event(MouseEvent& event)
|
||||||
else if (!m_selection.contains(index))
|
else if (!m_selection.contains(index))
|
||||||
set_selection(index);
|
set_selection(index);
|
||||||
|
|
||||||
activate_or_edit_selected();
|
if (is_editable() && edit_triggers() & EditTrigger::DoubleClicked)
|
||||||
}
|
begin_editing(cursor_index());
|
||||||
|
|
||||||
void AbstractView::activate_or_edit_selected()
|
|
||||||
{
|
|
||||||
if (is_editable())
|
|
||||||
begin_editing(selection().first());
|
|
||||||
else
|
else
|
||||||
activate_selected();
|
activate(cursor_index());
|
||||||
}
|
}
|
||||||
|
|
||||||
void AbstractView::context_menu_event(ContextMenuEvent& event)
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -67,6 +67,16 @@ public:
|
||||||
bool is_editable() const { return m_editable; }
|
bool is_editable() const { return m_editable; }
|
||||||
void set_editable(bool editable) { m_editable = 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; }
|
bool is_multi_select() const { return m_multi_select; }
|
||||||
void set_multi_select(bool);
|
void set_multi_select(bool);
|
||||||
|
|
||||||
|
@ -128,7 +138,6 @@ protected:
|
||||||
void set_hovered_index(const ModelIndex&);
|
void set_hovered_index(const ModelIndex&);
|
||||||
void activate(const ModelIndex&);
|
void activate(const ModelIndex&);
|
||||||
void activate_selected();
|
void activate_selected();
|
||||||
void activate_or_edit_selected();
|
|
||||||
void update_edit_widget_position();
|
void update_edit_widget_position();
|
||||||
|
|
||||||
bool m_editable { false };
|
bool m_editable { false };
|
||||||
|
@ -150,6 +159,7 @@ private:
|
||||||
OwnPtr<ModelEditingDelegate> m_editing_delegate;
|
OwnPtr<ModelEditingDelegate> m_editing_delegate;
|
||||||
ModelSelection m_selection;
|
ModelSelection m_selection;
|
||||||
ModelIndex m_cursor_index;
|
ModelIndex m_cursor_index;
|
||||||
|
unsigned m_edit_triggers { EditTrigger::DoubleClicked | EditTrigger::EditKeyPressed };
|
||||||
bool m_activates_on_selection { false };
|
bool m_activates_on_selection { false };
|
||||||
bool m_multi_select { true };
|
bool m_multi_select { true };
|
||||||
};
|
};
|
||||||
|
|
|
@ -165,7 +165,10 @@ void TableView::keydown_event(KeyEvent& event)
|
||||||
if (!model())
|
if (!model())
|
||||||
return;
|
return;
|
||||||
if (event.key() == KeyCode::Key_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;
|
||||||
}
|
}
|
||||||
return AbstractTableView::keydown_event(event);
|
return AbstractTableView::keydown_event(event);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue