1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 15:57:45 +00:00

LibGUI: Refactor AbstractView "multi select" mode into "selection mode"

There are three possible selection modes for a GUI::AbstractView.

- NoSelection
- SingleSelection
- MultiSelection

We don't enforce these modes fully yet, this patch mostly adds them in
place of the old "multi select" flag.
This commit is contained in:
Andreas Kling 2020-12-28 20:14:17 +01:00
parent 207ecf454a
commit f7116bba43
8 changed files with 31 additions and 25 deletions

View file

@ -403,16 +403,21 @@ void AbstractView::drop_event(DropEvent& event)
on_drop(index, event);
}
void AbstractView::set_multi_select(bool multi_select)
void AbstractView::set_selection_mode(SelectionMode selection_mode)
{
if (m_multi_select == multi_select)
if (m_selection_mode == selection_mode)
return;
m_multi_select = multi_select;
if (!multi_select && m_selection.size() > 1) {
m_selection_mode = selection_mode;
if (m_selection_mode == SelectionMode::NoSelection)
m_selection.clear();
else if (m_selection_mode != SelectionMode::SingleSelection && m_selection.size() > 1) {
auto first_selected = m_selection.first();
m_selection.clear();
m_selection.set(first_selected);
}
update();
}
void AbstractView::set_key_column_and_sort_order(int column, SortOrder sort_order)

View file

@ -65,6 +65,12 @@ public:
SelectRows,
};
enum class SelectionMode {
SingleSelection,
MultiSelection,
NoSelection,
};
virtual void move_cursor(CursorMovement, SelectionUpdate) { }
void set_model(RefPtr<Model>);
@ -96,8 +102,8 @@ public:
SelectionBehavior selection_behavior() const { return m_selection_behavior; }
void set_selection_behavior(SelectionBehavior behavior) { m_selection_behavior = behavior; }
bool is_multi_select() const { return m_multi_select; }
void set_multi_select(bool);
SelectionMode selection_mode() const { return m_selection_mode; }
void set_selection_mode(SelectionMode);
virtual void model_did_update(unsigned flags) override;
virtual void did_update_selection();
@ -197,9 +203,9 @@ private:
RefPtr<Core::Timer> m_searching_timer;
ModelIndex m_cursor_index;
SelectionBehavior m_selection_behavior { SelectionBehavior::SelectItems };
SelectionMode m_selection_mode { SelectionMode::SingleSelection };
unsigned m_edit_triggers { EditTrigger::DoubleClicked | EditTrigger::EditKeyPressed };
bool m_activates_on_selection { false };
bool m_multi_select { true };
bool m_tab_key_navigation_enabled { false };
bool m_is_dragging { false };
bool m_draw_item_text_with_shadow { false };

View file

@ -123,7 +123,7 @@ FilePicker::FilePicker(Window* parent_window, Mode mode, Options options, const
m_location_textbox->set_text(path);
m_view = vertical_container.add<MultiView>();
m_view->set_multi_select(m_mode == Mode::OpenMultiple);
m_view->set_selection_mode(m_mode == Mode::OpenMultiple ? GUI::AbstractView::SelectionMode::MultiSelection : GUI::AbstractView::SelectionMode::SingleSelection);
m_view->set_model(SortingProxyModel::create(*m_model));
m_view->set_model_column(FileSystemModel::Column::Name);
m_view->set_key_column_and_sort_order(GUI::FileSystemModel::Column::Name, GUI::SortOrder::Ascending);

View file

@ -247,7 +247,7 @@ void IconView::mousedown_event(MouseEvent& event)
auto adjusted_position = to_content_position(event.position());
m_might_drag = false;
if (is_multi_select()) {
if (selection_mode() == SelectionMode::MultiSelection) {
m_rubber_banding = true;
m_rubber_band_origin = adjusted_position;
m_rubber_band_current = adjusted_position;

View file

@ -97,7 +97,6 @@ MultiView::MultiView()
build_actions();
set_view_mode(ViewMode::Icon);
apply_multi_select();
}
MultiView::~MultiView()
@ -176,19 +175,16 @@ void MultiView::build_actions()
m_view_type_action_group->add_action(*m_view_as_columns_action);
}
void MultiView::apply_multi_select()
AbstractView::SelectionMode MultiView::selection_mode() const
{
m_table_view->set_multi_select(m_multi_select);
m_icon_view->set_multi_select(m_multi_select);
m_columns_view->set_multi_select(m_multi_select);
return m_table_view->selection_mode();
}
void MultiView::set_multi_select(bool multi_select)
void MultiView::set_selection_mode(AbstractView::SelectionMode selection_mode)
{
if (m_multi_select == multi_select)
return;
m_multi_select = multi_select;
apply_multi_select();
m_table_view->set_selection_mode(selection_mode);
m_icon_view->set_selection_mode(selection_mode);
m_columns_view->set_selection_mode(selection_mode);
}
void MultiView::set_key_column_and_sort_order(int column, SortOrder sort_order)

View file

@ -97,14 +97,13 @@ public:
Action& view_as_icons_action() { return *m_view_as_icons_action; }
Action& view_as_columns_action() { return *m_view_as_columns_action; }
bool is_multi_select() const { return m_multi_select; }
void set_multi_select(bool);
AbstractView::SelectionMode selection_mode() const;
void set_selection_mode(AbstractView::SelectionMode);
private:
MultiView();
void build_actions();
void apply_multi_select();
ViewMode m_view_mode { Invalid };
int m_model_column { 0 };
@ -120,8 +119,6 @@ private:
RefPtr<Action> m_view_as_columns_action;
OwnPtr<ActionGroup> m_view_type_action_group;
bool m_multi_select { true };
};
}