mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 21:42:43 +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:
		
							parent
							
								
									207ecf454a
								
							
						
					
					
						commit
						f7116bba43
					
				
					 8 changed files with 31 additions and 25 deletions
				
			
		|  | @ -202,6 +202,7 @@ void DirectoryView::setup_model() | ||||||
| void DirectoryView::setup_icon_view() | void DirectoryView::setup_icon_view() | ||||||
| { | { | ||||||
|     m_icon_view = add<GUI::IconView>(); |     m_icon_view = add<GUI::IconView>(); | ||||||
|  |     m_icon_view->set_selection_mode(GUI::AbstractView::SelectionMode::MultiSelection); | ||||||
|     m_icon_view->set_editable(true); |     m_icon_view->set_editable(true); | ||||||
|     m_icon_view->set_edit_triggers(GUI::AbstractView::EditTrigger::EditKeyPressed); |     m_icon_view->set_edit_triggers(GUI::AbstractView::EditTrigger::EditKeyPressed); | ||||||
|     m_icon_view->aid_create_editing_delegate = [](auto&) { |     m_icon_view->aid_create_editing_delegate = [](auto&) { | ||||||
|  | @ -236,6 +237,7 @@ void DirectoryView::setup_icon_view() | ||||||
| void DirectoryView::setup_columns_view() | void DirectoryView::setup_columns_view() | ||||||
| { | { | ||||||
|     m_columns_view = add<GUI::ColumnsView>(); |     m_columns_view = add<GUI::ColumnsView>(); | ||||||
|  |     m_columns_view->set_selection_mode(GUI::AbstractView::SelectionMode::MultiSelection); | ||||||
|     m_columns_view->set_editable(true); |     m_columns_view->set_editable(true); | ||||||
|     m_columns_view->set_edit_triggers(GUI::AbstractView::EditTrigger::EditKeyPressed); |     m_columns_view->set_edit_triggers(GUI::AbstractView::EditTrigger::EditKeyPressed); | ||||||
|     m_columns_view->aid_create_editing_delegate = [](auto&) { |     m_columns_view->aid_create_editing_delegate = [](auto&) { | ||||||
|  | @ -266,6 +268,7 @@ void DirectoryView::setup_columns_view() | ||||||
| void DirectoryView::setup_table_view() | void DirectoryView::setup_table_view() | ||||||
| { | { | ||||||
|     m_table_view = add<GUI::TableView>(); |     m_table_view = add<GUI::TableView>(); | ||||||
|  |     m_table_view->set_selection_mode(GUI::AbstractView::SelectionMode::MultiSelection); | ||||||
|     m_table_view->set_editable(true); |     m_table_view->set_editable(true); | ||||||
|     m_table_view->set_edit_triggers(GUI::AbstractView::EditTrigger::EditKeyPressed); |     m_table_view->set_edit_triggers(GUI::AbstractView::EditTrigger::EditKeyPressed); | ||||||
|     m_table_view->aid_create_editing_delegate = [](auto&) { |     m_table_view->aid_create_editing_delegate = [](auto&) { | ||||||
|  |  | ||||||
|  | @ -160,7 +160,6 @@ void CellTypeDialog::setup_tabs(GUI::TabWidget& tabs, const Vector<Position>& po | ||||||
| 
 | 
 | ||||||
|         auto& type_list = left_side.add<GUI::ListView>(); |         auto& type_list = left_side.add<GUI::ListView>(); | ||||||
|         type_list.set_model(*GUI::ItemListModel<String>::create(g_types)); |         type_list.set_model(*GUI::ItemListModel<String>::create(g_types)); | ||||||
|         type_list.set_multi_select(false); |  | ||||||
|         type_list.set_should_hide_unnecessary_scrollbars(true); |         type_list.set_should_hide_unnecessary_scrollbars(true); | ||||||
|         type_list.on_selection = [&](auto& index) { |         type_list.on_selection = [&](auto& index) { | ||||||
|             if (!index.is_valid()) { |             if (!index.is_valid()) { | ||||||
|  |  | ||||||
|  | @ -403,16 +403,21 @@ void AbstractView::drop_event(DropEvent& event) | ||||||
|         on_drop(index, 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; |         return; | ||||||
|     m_multi_select = multi_select; |     m_selection_mode = selection_mode; | ||||||
|     if (!multi_select && m_selection.size() > 1) { | 
 | ||||||
|  |     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(); |         auto first_selected = m_selection.first(); | ||||||
|         m_selection.clear(); |         m_selection.clear(); | ||||||
|         m_selection.set(first_selected); |         m_selection.set(first_selected); | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     update(); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void AbstractView::set_key_column_and_sort_order(int column, SortOrder sort_order) | void AbstractView::set_key_column_and_sort_order(int column, SortOrder sort_order) | ||||||
|  |  | ||||||
|  | @ -65,6 +65,12 @@ public: | ||||||
|         SelectRows, |         SelectRows, | ||||||
|     }; |     }; | ||||||
| 
 | 
 | ||||||
|  |     enum class SelectionMode { | ||||||
|  |         SingleSelection, | ||||||
|  |         MultiSelection, | ||||||
|  |         NoSelection, | ||||||
|  |     }; | ||||||
|  | 
 | ||||||
|     virtual void move_cursor(CursorMovement, SelectionUpdate) { } |     virtual void move_cursor(CursorMovement, SelectionUpdate) { } | ||||||
| 
 | 
 | ||||||
|     void set_model(RefPtr<Model>); |     void set_model(RefPtr<Model>); | ||||||
|  | @ -96,8 +102,8 @@ public: | ||||||
|     SelectionBehavior selection_behavior() const { return m_selection_behavior; } |     SelectionBehavior selection_behavior() const { return m_selection_behavior; } | ||||||
|     void set_selection_behavior(SelectionBehavior behavior) { m_selection_behavior = behavior; } |     void set_selection_behavior(SelectionBehavior behavior) { m_selection_behavior = behavior; } | ||||||
| 
 | 
 | ||||||
|     bool is_multi_select() const { return m_multi_select; } |     SelectionMode selection_mode() const { return m_selection_mode; } | ||||||
|     void set_multi_select(bool); |     void set_selection_mode(SelectionMode); | ||||||
| 
 | 
 | ||||||
|     virtual void model_did_update(unsigned flags) override; |     virtual void model_did_update(unsigned flags) override; | ||||||
|     virtual void did_update_selection(); |     virtual void did_update_selection(); | ||||||
|  | @ -197,9 +203,9 @@ private: | ||||||
|     RefPtr<Core::Timer> m_searching_timer; |     RefPtr<Core::Timer> m_searching_timer; | ||||||
|     ModelIndex m_cursor_index; |     ModelIndex m_cursor_index; | ||||||
|     SelectionBehavior m_selection_behavior { SelectionBehavior::SelectItems }; |     SelectionBehavior m_selection_behavior { SelectionBehavior::SelectItems }; | ||||||
|  |     SelectionMode m_selection_mode { SelectionMode::SingleSelection }; | ||||||
|     unsigned m_edit_triggers { EditTrigger::DoubleClicked | EditTrigger::EditKeyPressed }; |     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_tab_key_navigation_enabled { false }; |     bool m_tab_key_navigation_enabled { false }; | ||||||
|     bool m_is_dragging { false }; |     bool m_is_dragging { false }; | ||||||
|     bool m_draw_item_text_with_shadow { false }; |     bool m_draw_item_text_with_shadow { false }; | ||||||
|  |  | ||||||
|  | @ -123,7 +123,7 @@ FilePicker::FilePicker(Window* parent_window, Mode mode, Options options, const | ||||||
|     m_location_textbox->set_text(path); |     m_location_textbox->set_text(path); | ||||||
| 
 | 
 | ||||||
|     m_view = vertical_container.add<MultiView>(); |     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(SortingProxyModel::create(*m_model)); | ||||||
|     m_view->set_model_column(FileSystemModel::Column::Name); |     m_view->set_model_column(FileSystemModel::Column::Name); | ||||||
|     m_view->set_key_column_and_sort_order(GUI::FileSystemModel::Column::Name, GUI::SortOrder::Ascending); |     m_view->set_key_column_and_sort_order(GUI::FileSystemModel::Column::Name, GUI::SortOrder::Ascending); | ||||||
|  |  | ||||||
|  | @ -247,7 +247,7 @@ void IconView::mousedown_event(MouseEvent& event) | ||||||
|     auto adjusted_position = to_content_position(event.position()); |     auto adjusted_position = to_content_position(event.position()); | ||||||
| 
 | 
 | ||||||
|     m_might_drag = false; |     m_might_drag = false; | ||||||
|     if (is_multi_select()) { |     if (selection_mode() == SelectionMode::MultiSelection) { | ||||||
|         m_rubber_banding = true; |         m_rubber_banding = true; | ||||||
|         m_rubber_band_origin = adjusted_position; |         m_rubber_band_origin = adjusted_position; | ||||||
|         m_rubber_band_current = adjusted_position; |         m_rubber_band_current = adjusted_position; | ||||||
|  |  | ||||||
|  | @ -97,7 +97,6 @@ MultiView::MultiView() | ||||||
| 
 | 
 | ||||||
|     build_actions(); |     build_actions(); | ||||||
|     set_view_mode(ViewMode::Icon); |     set_view_mode(ViewMode::Icon); | ||||||
|     apply_multi_select(); |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| MultiView::~MultiView() | MultiView::~MultiView() | ||||||
|  | @ -176,19 +175,16 @@ void MultiView::build_actions() | ||||||
|     m_view_type_action_group->add_action(*m_view_as_columns_action); |     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); |     return m_table_view->selection_mode(); | ||||||
|     m_icon_view->set_multi_select(m_multi_select); |  | ||||||
|     m_columns_view->set_multi_select(m_multi_select); |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void MultiView::set_multi_select(bool multi_select) | void MultiView::set_selection_mode(AbstractView::SelectionMode selection_mode) | ||||||
| { | { | ||||||
|     if (m_multi_select == multi_select) |     m_table_view->set_selection_mode(selection_mode); | ||||||
|         return; |     m_icon_view->set_selection_mode(selection_mode); | ||||||
|     m_multi_select = multi_select; |     m_columns_view->set_selection_mode(selection_mode); | ||||||
|     apply_multi_select(); |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void MultiView::set_key_column_and_sort_order(int column, SortOrder sort_order) | void MultiView::set_key_column_and_sort_order(int column, SortOrder sort_order) | ||||||
|  |  | ||||||
|  | @ -97,14 +97,13 @@ public: | ||||||
|     Action& view_as_icons_action() { return *m_view_as_icons_action; } |     Action& view_as_icons_action() { return *m_view_as_icons_action; } | ||||||
|     Action& view_as_columns_action() { return *m_view_as_columns_action; } |     Action& view_as_columns_action() { return *m_view_as_columns_action; } | ||||||
| 
 | 
 | ||||||
|     bool is_multi_select() const { return m_multi_select; } |     AbstractView::SelectionMode selection_mode() const; | ||||||
|     void set_multi_select(bool); |     void set_selection_mode(AbstractView::SelectionMode); | ||||||
| 
 | 
 | ||||||
| private: | private: | ||||||
|     MultiView(); |     MultiView(); | ||||||
| 
 | 
 | ||||||
|     void build_actions(); |     void build_actions(); | ||||||
|     void apply_multi_select(); |  | ||||||
| 
 | 
 | ||||||
|     ViewMode m_view_mode { Invalid }; |     ViewMode m_view_mode { Invalid }; | ||||||
|     int m_model_column { 0 }; |     int m_model_column { 0 }; | ||||||
|  | @ -120,8 +119,6 @@ private: | ||||||
|     RefPtr<Action> m_view_as_columns_action; |     RefPtr<Action> m_view_as_columns_action; | ||||||
| 
 | 
 | ||||||
|     OwnPtr<ActionGroup> m_view_type_action_group; |     OwnPtr<ActionGroup> m_view_type_action_group; | ||||||
| 
 |  | ||||||
|     bool m_multi_select { true }; |  | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Andreas Kling
						Andreas Kling