summaryrefslogtreecommitdiff
path: root/Userland/Libraries
diff options
context:
space:
mode:
authorthankyouverycool <66646555+thankyouverycool@users.noreply.github.com>2021-03-11 10:35:40 -0500
committerAndreas Kling <kling@serenityos.org>2021-03-12 09:59:03 +0100
commit0fc81d23f445c6706684e280aea501a5fa5bae87 (patch)
tree998192140ba9890df9cc017c1d53ce191e3e140c /Userland/Libraries
parent142ca4b818deee7df9766d4d9615d25dcd353642 (diff)
downloadserenity-0fc81d23f445c6706684e280aea501a5fa5bae87.zip
LibGUI: Add variable padding and center bitmaps in TableViews
This lets us make nicer looking bitmap tables and fixes a content rect issue in TreeView. Also makes key column highlighting optional
Diffstat (limited to 'Userland/Libraries')
-rw-r--r--Userland/Libraries/LibGUI/AbstractTableView.cpp10
-rw-r--r--Userland/Libraries/LibGUI/AbstractTableView.h13
-rw-r--r--Userland/Libraries/LibGUI/TableView.cpp13
-rw-r--r--Userland/Libraries/LibGUI/TableView.h5
-rw-r--r--Userland/Libraries/LibGUI/TreeView.h3
5 files changed, 28 insertions, 16 deletions
diff --git a/Userland/Libraries/LibGUI/AbstractTableView.cpp b/Userland/Libraries/LibGUI/AbstractTableView.cpp
index 69e5c13458..64e1093479 100644
--- a/Userland/Libraries/LibGUI/AbstractTableView.cpp
+++ b/Userland/Libraries/LibGUI/AbstractTableView.cpp
@@ -392,14 +392,4 @@ 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() + icon_padding();
-}
-
}
diff --git a/Userland/Libraries/LibGUI/AbstractTableView.h b/Userland/Libraries/LibGUI/AbstractTableView.h
index f20eee617c..d4b2dff341 100644
--- a/Userland/Libraries/LibGUI/AbstractTableView.h
+++ b/Userland/Libraries/LibGUI/AbstractTableView.h
@@ -39,7 +39,12 @@ public:
class AbstractTableView : public AbstractView {
public:
- int row_height() const;
+ int row_height() const { return font().glyph_height() + vertical_padding(); }
+
+ virtual int horizontal_padding() const { return m_horizontal_padding; }
+ void set_horizontal_padding(int padding) { m_horizontal_padding = padding; }
+ virtual int vertical_padding() const { return m_vertical_padding; }
+ void set_vertical_padding(int padding) { m_vertical_padding = padding; }
bool alternating_row_colors() const { return m_alternating_row_colors; }
void set_alternating_row_colors(bool b) { m_alternating_row_colors = b; }
@@ -59,9 +64,6 @@ public:
void set_column_painting_delegate(int column, OwnPtr<TableCellPaintingDelegate>);
- int horizontal_padding() const;
- int icon_padding() const { return 8; }
-
Gfx::IntPoint adjusted_position(const Gfx::IntPoint&) const;
virtual Gfx::IntRect content_rect(const ModelIndex&) const override;
@@ -123,6 +125,9 @@ private:
bool m_alternating_row_colors { true };
bool m_highlight_selected_rows { true };
+
+ int m_vertical_padding { 8 };
+ int m_horizontal_padding { font().glyph_height() / 2 };
};
}
diff --git a/Userland/Libraries/LibGUI/TableView.cpp b/Userland/Libraries/LibGUI/TableView.cpp
index 2946eb601b..f965c9448b 100644
--- a/Userland/Libraries/LibGUI/TableView.cpp
+++ b/Userland/Libraries/LibGUI/TableView.cpp
@@ -37,6 +37,8 @@
#include <LibGUI/Window.h>
#include <LibGfx/Palette.h>
+REGISTER_WIDGET(GUI, TableView)
+
namespace GUI {
TableView::TableView()
@@ -113,7 +115,7 @@ void TableView::paint_event(PaintEvent& event)
bool is_key_column = m_key_column == column_index;
Gfx::IntRect cell_rect(horizontal_padding() + x, y, column_width, row_height());
auto cell_rect_for_fill = cell_rect.inflated(horizontal_padding() * 2, 0);
- if (is_key_column)
+ if (is_key_column && is_key_column_highlighted())
painter.fill_rect(cell_rect_for_fill, key_column_background_color);
auto cell_index = model()->index(row_index, column_index);
@@ -122,7 +124,14 @@ void TableView::paint_event(PaintEvent& event)
} else {
auto data = cell_index.data();
if (data.is_bitmap()) {
- painter.blit(cell_rect.location(), data.as_bitmap(), data.as_bitmap().rect());
+ auto cell_constrained_bitmap_rect = data.as_bitmap().rect();
+ if (data.as_bitmap().rect().width() > column_width)
+ cell_constrained_bitmap_rect.set_width(column_width);
+ if (data.as_bitmap().rect().height() > row_height())
+ cell_constrained_bitmap_rect.set_height(row_height());
+ cell_rect.set_y(cell_rect.y() + (row_height() - cell_constrained_bitmap_rect.height()) / 2);
+ cell_rect.set_x(cell_rect.x() + (column_width - cell_constrained_bitmap_rect.width()) / 2);
+ painter.blit(cell_rect.location(), data.as_bitmap(), cell_constrained_bitmap_rect);
} else if (data.is_icon()) {
if (auto bitmap = data.as_icon().bitmap_for_size(16)) {
cell_rect.set_y(cell_rect.y() + (row_height() - bitmap->height()) / 2);
diff --git a/Userland/Libraries/LibGUI/TableView.h b/Userland/Libraries/LibGUI/TableView.h
index 04c44de64a..ab0e9a2530 100644
--- a/Userland/Libraries/LibGUI/TableView.h
+++ b/Userland/Libraries/LibGUI/TableView.h
@@ -51,6 +51,9 @@ public:
GridStyle grid_style() const { return m_grid_style; }
void set_grid_style(GridStyle);
+ void set_highlight_key_column(bool b) { m_highlight_key_column = b; }
+ bool is_key_column_highlighted() const { return m_highlight_key_column; }
+
virtual void move_cursor(CursorMovement, SelectionUpdate) override;
protected:
@@ -61,6 +64,8 @@ protected:
private:
GridStyle m_grid_style { GridStyle::None };
+
+ bool m_highlight_key_column { true };
};
}
diff --git a/Userland/Libraries/LibGUI/TreeView.h b/Userland/Libraries/LibGUI/TreeView.h
index 76b1754302..a7eef52e29 100644
--- a/Userland/Libraries/LibGUI/TreeView.h
+++ b/Userland/Libraries/LibGUI/TreeView.h
@@ -51,6 +51,8 @@ public:
void set_should_fill_selected_rows(bool fill) { m_should_fill_selected_rows = fill; }
bool should_fill_selected_rows() const { return m_should_fill_selected_rows; }
+ virtual int vertical_padding() const override { return m_vertical_padding; }
+
protected:
TreeView();
@@ -89,6 +91,7 @@ private:
RefPtr<Gfx::Bitmap> m_collapse_bitmap;
bool m_should_fill_selected_rows { false };
+ int m_vertical_padding { 6 };
};
}