From 688675e89b3faf0ad3444f0ae958e3a25b9e06fe Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 24 Oct 2020 16:17:26 +0200 Subject: [PATCH] LibGUI: Make table view row height+padding font-size-relative This makes tables look a lot nicer with different-sized fonts. :^) --- Applications/Spreadsheet/SpreadsheetView.cpp | 2 -- Libraries/LibGUI/AbstractTableView.cpp | 19 ++++++++++--------- Libraries/LibGUI/AbstractTableView.h | 7 ++----- Libraries/LibGUI/HeaderView.cpp | 12 ++++++------ Libraries/LibGUI/HeaderView.h | 2 -- 5 files changed, 18 insertions(+), 24 deletions(-) diff --git a/Applications/Spreadsheet/SpreadsheetView.cpp b/Applications/Spreadsheet/SpreadsheetView.cpp index d4925ec5c6..f1023a5427 100644 --- a/Applications/Spreadsheet/SpreadsheetView.cpp +++ b/Applications/Spreadsheet/SpreadsheetView.cpp @@ -66,8 +66,6 @@ SpreadsheetView::SpreadsheetView(Sheet& sheet) m_table_view->row_header().set_visible(true); m_table_view->set_model(SheetModel::create(*m_sheet)); - m_table_view->set_row_height(18); - set_focus_proxy(m_table_view); // FIXME: This is dumb. diff --git a/Libraries/LibGUI/AbstractTableView.cpp b/Libraries/LibGUI/AbstractTableView.cpp index 97102f3499..260f168a48 100644 --- a/Libraries/LibGUI/AbstractTableView.cpp +++ b/Libraries/LibGUI/AbstractTableView.cpp @@ -374,15 +374,6 @@ void AbstractTableView::layout_headers() } } -void AbstractTableView::set_row_height(int height) -{ - if (m_row_height == height) - return; - - m_row_height = height; - update_row_sizes(); -} - void AbstractTableView::keydown_event(KeyEvent& event) { if (is_tab_key_navigation_enabled()) { @@ -401,4 +392,14 @@ void AbstractTableView::keydown_event(KeyEvent& event) AbstractView::keydown_event(event); } +int AbstractTableView::horizontal_padding() const +{ + return font().glyph_height() / 2; +} + +int AbstractTableView::row_height() const +{ + return font().glyph_height() + 6; +} + } diff --git a/Libraries/LibGUI/AbstractTableView.h b/Libraries/LibGUI/AbstractTableView.h index 9fc6ea76ac..df0f48c581 100644 --- a/Libraries/LibGUI/AbstractTableView.h +++ b/Libraries/LibGUI/AbstractTableView.h @@ -39,8 +39,7 @@ public: class AbstractTableView : public AbstractView { public: - int row_height() const { return m_row_height; } - void set_row_height(int); + int row_height() const; bool alternating_row_colors() const { return m_alternating_row_colors; } void set_alternating_row_colors(bool b) { m_alternating_row_colors = b; } @@ -60,7 +59,7 @@ public: void set_column_painting_delegate(int column, OwnPtr); - int horizontal_padding() const { return m_horizontal_padding; } + int horizontal_padding() const; Gfx::IntPoint adjusted_position(const Gfx::IntPoint&) const; @@ -123,8 +122,6 @@ private: bool m_alternating_row_colors { true }; bool m_highlight_selected_rows { true }; - int m_horizontal_padding { 5 }; - int m_row_height { 16 }; }; } diff --git a/Libraries/LibGUI/HeaderView.cpp b/Libraries/LibGUI/HeaderView.cpp index 0ac6be586a..35f461e663 100644 --- a/Libraries/LibGUI/HeaderView.cpp +++ b/Libraries/LibGUI/HeaderView.cpp @@ -93,10 +93,10 @@ Gfx::IntRect HeaderView::section_rect(int section) const continue; offset += section_data(i).size; if (orientation() == Gfx::Orientation::Horizontal) - offset += horizontal_padding() * 2; + offset += m_table_view.horizontal_padding() * 2; } if (orientation() == Gfx::Orientation::Horizontal) - return { offset, 0, section_size(section) + horizontal_padding() * 2, height() }; + return { offset, 0, section_size(section) + m_table_view.horizontal_padding() * 2, height() }; return { 0, offset, width(), section_size(section) }; } @@ -232,7 +232,7 @@ void HeaderView::paint_horizontal(Painter& painter) continue; int section_width = section_size(section); bool is_key_column = m_table_view.key_column() == section; - Gfx::IntRect cell_rect(x_offset, 0, section_width + horizontal_padding() * 2, height()); + Gfx::IntRect cell_rect(x_offset, 0, section_width + m_table_view.horizontal_padding() * 2, height()); bool pressed = section == m_pressed_section && m_pressed_section_is_pressed; bool hovered = section == m_hovered_section && model()->is_column_sortable(section); Gfx::StylePainter::paint_button(painter, cell_rect, palette(), Gfx::ButtonStyle::Normal, pressed, hovered); @@ -248,11 +248,11 @@ void HeaderView::paint_horizontal(Painter& painter) } else { text = model()->column_name(section); } - auto text_rect = cell_rect.shrunken(horizontal_padding() * 2, 0); + auto text_rect = cell_rect.shrunken(m_table_view.horizontal_padding() * 2, 0); if (pressed) text_rect.move_by(1, 1); painter.draw_text(text_rect, text, font(), section_alignment(section), palette().button_text()); - x_offset += section_width + horizontal_padding() * 2; + x_offset += section_width + m_table_view.horizontal_padding() * 2; } if (x_offset < rect().right()) { @@ -276,7 +276,7 @@ void HeaderView::paint_vertical(Painter& painter) bool hovered = false; Gfx::StylePainter::paint_button(painter, cell_rect, palette(), Gfx::ButtonStyle::Normal, pressed, hovered); String text = String::format("%d", section); - auto text_rect = cell_rect.shrunken(horizontal_padding() * 2, 0); + auto text_rect = cell_rect.shrunken(m_table_view.horizontal_padding() * 2, 0); if (pressed) text_rect.move_by(1, 1); painter.draw_text(text_rect, text, font(), section_alignment(section), palette().button_text()); diff --git a/Libraries/LibGUI/HeaderView.h b/Libraries/LibGUI/HeaderView.h index eb68fc7403..5132be3fb6 100644 --- a/Libraries/LibGUI/HeaderView.h +++ b/Libraries/LibGUI/HeaderView.h @@ -64,8 +64,6 @@ private: virtual void context_menu_event(ContextMenuEvent&) override; virtual void leave_event(Core::Event&) override; - int horizontal_padding() const { return 5; } - Gfx::IntRect section_resize_grabbable_rect(int) const; void paint_horizontal(Painter&);