Skip to content

Commit f0138fc

Browse files
committed
LibGUI: Move selection behavior from TableView up to AbstractView
Let's make SelectionBehavior a view concept where views can either select individual items (row, index) or whole rows. Maybe some day we'll do whole columns, but I don't think we need that now.
1 parent c8fb00f commit f0138fc

File tree

5 files changed

+13
-15
lines changed

5 files changed

+13
-15
lines changed

Applications/Spreadsheet/SpreadsheetView.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ SpreadsheetView::SpreadsheetView(Sheet& sheet)
148148
set_layout<GUI::VerticalBoxLayout>().set_margins({ 2, 2, 2, 2 });
149149
m_table_view = add<InfinitelyScrollableTableView>();
150150
m_table_view->set_grid_style(GUI::TableView::GridStyle::Both);
151-
m_table_view->set_cursor_style(GUI::TableView::CursorStyle::Item);
151+
m_table_view->set_selection_behavior(GUI::AbstractView::SelectionBehavior::SelectItems);
152152
m_table_view->set_edit_triggers(GUI::AbstractView::EditTrigger::EditKeyPressed | GUI::AbstractView::AnyKeyPressed | GUI::AbstractView::DoubleClicked);
153153
m_table_view->set_tab_key_navigation_enabled(true);
154154
m_table_view->row_header().set_visible(true);

Libraries/LibGUI/AbstractTableView.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ namespace GUI {
4141

4242
AbstractTableView::AbstractTableView()
4343
{
44+
set_selection_behavior(SelectionBehavior::SelectRows);
4445
m_corner_button = add<Button>();
4546
m_corner_button->move_to_back();
4647
m_corner_button->set_background_role(Gfx::ColorRole::ThreedShadow1);

Libraries/LibGUI/AbstractView.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ class AbstractView
6060
ClearIfNotSelected
6161
};
6262

63+
enum class SelectionBehavior {
64+
SelectItems,
65+
SelectRows,
66+
};
67+
6368
virtual void move_cursor(CursorMovement, SelectionUpdate) { }
6469

6570
void set_model(RefPtr<Model>);
@@ -88,6 +93,9 @@ class AbstractView
8893
unsigned edit_triggers() const { return m_edit_triggers; }
8994
void set_edit_triggers(unsigned);
9095

96+
SelectionBehavior selection_behavior() const { return m_selection_behavior; }
97+
void set_selection_behavior(SelectionBehavior behavior) { m_selection_behavior = behavior; }
98+
9199
bool is_multi_select() const { return m_multi_select; }
92100
void set_multi_select(bool);
93101

@@ -188,6 +196,7 @@ class AbstractView
188196
String m_searching;
189197
RefPtr<Core::Timer> m_searching_timer;
190198
ModelIndex m_cursor_index;
199+
SelectionBehavior m_selection_behavior { SelectionBehavior::SelectItems };
191200
unsigned m_edit_triggers { EditTrigger::DoubleClicked | EditTrigger::EditKeyPressed };
192201
bool m_activates_on_selection { false };
193202
bool m_multi_select { true };

Libraries/LibGUI/TableView.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,13 @@ void TableView::paint_event(PaintEvent& event)
149149
if (m_grid_style == GridStyle::Vertical || m_grid_style == GridStyle::Both)
150150
painter.draw_line(cell_rect_for_fill.top_right(), cell_rect_for_fill.bottom_right(), palette().ruler());
151151

152-
if (m_cursor_style == CursorStyle::Item && cell_index == cursor_index())
152+
if (selection_behavior() == SelectionBehavior::SelectItems && cell_index == cursor_index())
153153
painter.draw_rect(cell_rect_for_fill, palette().text_cursor());
154154

155155
x += column_width + horizontal_padding() * 2;
156156
}
157157

158-
if (is_focused() && cursor_style() == CursorStyle::Row && row_index == cursor_index().row()) {
158+
if (is_focused() && selection_behavior() == SelectionBehavior::SelectRows && row_index == cursor_index().row()) {
159159
painter.draw_rect(row_rect, widget_background_color);
160160
painter.draw_focus_rect(row_rect, palette().focus_outline());
161161
}
@@ -244,12 +244,4 @@ void TableView::set_grid_style(GridStyle style)
244244
update();
245245
}
246246

247-
void TableView::set_cursor_style(CursorStyle style)
248-
{
249-
if (m_cursor_style == style)
250-
return;
251-
m_cursor_style = style;
252-
update();
253-
}
254-
255247
}

Libraries/LibGUI/TableView.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,6 @@ class TableView : public AbstractTableView {
5151
GridStyle grid_style() const { return m_grid_style; }
5252
void set_grid_style(GridStyle);
5353

54-
CursorStyle cursor_style() const { return m_cursor_style; }
55-
void set_cursor_style(CursorStyle);
56-
5754
virtual void move_cursor(CursorMovement, SelectionUpdate) override;
5855

5956
protected:
@@ -64,7 +61,6 @@ class TableView : public AbstractTableView {
6461

6562
private:
6663
GridStyle m_grid_style { GridStyle::None };
67-
CursorStyle m_cursor_style { CursorStyle::Row };
6864
};
6965

7066
}

0 commit comments

Comments
 (0)