diff options
author | Andreas Kling <kling@serenityos.org> | 2020-04-24 22:27:46 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-04-24 22:27:46 +0200 |
commit | 53cb5325eeeda878a1179a0771bc7390441958a1 (patch) | |
tree | 0527b92fd78cc977a22a328dcd1645126e7b4f0c /Libraries/LibGUI/TabWidget.cpp | |
parent | 2be184f49c891dd0d57643448ad519b6f242084f (diff) | |
download | serenity-53cb5325eeeda878a1179a0771bc7390441958a1.zip |
LibGUI: Allow TabWidget tabs to have icons and custom text alignment
Diffstat (limited to 'Libraries/LibGUI/TabWidget.cpp')
-rw-r--r-- | Libraries/LibGUI/TabWidget.cpp | 34 |
1 files changed, 30 insertions, 4 deletions
diff --git a/Libraries/LibGUI/TabWidget.cpp b/Libraries/LibGUI/TabWidget.cpp index 1be9c6f9d9..36bc3d91f0 100644 --- a/Libraries/LibGUI/TabWidget.cpp +++ b/Libraries/LibGUI/TabWidget.cpp @@ -27,6 +27,7 @@ #include <LibGUI/BoxLayout.h> #include <LibGUI/Painter.h> #include <LibGUI/TabWidget.h> +#include <LibGfx/Bitmap.h> #include <LibGfx/Font.h> #include <LibGfx/Palette.h> #include <LibGfx/StylePainter.h> @@ -43,7 +44,7 @@ TabWidget::~TabWidget() void TabWidget::add_widget(const StringView& title, Widget& widget) { - m_tabs.append({ title, &widget }); + m_tabs.append({ title, nullptr, &widget }); add_child(widget); } @@ -159,13 +160,25 @@ void TabWidget::paint_event(PaintEvent& event) if (container_padding() > 0) Gfx::StylePainter::paint_frame(painter, container_rect, palette(), Gfx::FrameShape::Container, Gfx::FrameShadow::Raised, 2); + auto paint_tab_icon_if_needed = [&](auto& icon, auto& button_rect, auto& text_rect) { + if (!icon) + return; + Gfx::Rect icon_rect { button_rect.x(), button_rect.y(), 16, 16 }; + icon_rect.move_by(3, 3); + painter.draw_scaled_bitmap(icon_rect, *icon, icon->rect()); + text_rect.set_x(icon_rect.right() + 1 + 4); + text_rect.intersect(button_rect); + }; + for (size_t i = 0; i < m_tabs.size(); ++i) { if (m_tabs[i].widget == m_active_widget) continue; bool hovered = static_cast<int>(i) == m_hovered_tab_index; auto button_rect = this->button_rect(i); Gfx::StylePainter::paint_tab_button(painter, button_rect, palette(), false, hovered, m_tabs[i].widget->is_enabled()); - painter.draw_text(button_rect.translated(0, 1), m_tabs[i].title, Gfx::TextAlignment::Center, palette().button_text()); + auto text_rect = button_rect.translated(0, 1); + paint_tab_icon_if_needed(m_tabs[i].icon, button_rect, text_rect); + painter.draw_text(text_rect, m_tabs[i].title, m_text_alignment, palette().button_text()); } for (size_t i = 0; i < m_tabs.size(); ++i) { @@ -174,7 +187,9 @@ void TabWidget::paint_event(PaintEvent& event) bool hovered = static_cast<int>(i) == m_hovered_tab_index; auto button_rect = this->button_rect(i); Gfx::StylePainter::paint_tab_button(painter, button_rect, palette(), true, hovered, m_tabs[i].widget->is_enabled()); - painter.draw_text(button_rect.translated(0, 1), m_tabs[i].title, Gfx::TextAlignment::Center, palette().button_text()); + auto text_rect = button_rect.translated(0, 1); + paint_tab_icon_if_needed(m_tabs[i].icon, button_rect, text_rect); + painter.draw_text(text_rect, m_tabs[i].title, m_text_alignment, palette().button_text()); painter.draw_line(button_rect.bottom_left().translated(1, 1), button_rect.bottom_right().translated(-1, 1), palette().button()); break; } @@ -199,7 +214,7 @@ Gfx::Rect TabWidget::button_rect(int index) const int TabWidget::TabData::width(const Gfx::Font& font) const { - return 16 + font.width(title); + return 16 + font.width(title) + (icon ? (16 + 4) : 0); } void TabWidget::mousedown_event(MouseEvent& event) @@ -277,6 +292,17 @@ void TabWidget::set_tab_title(Widget& tab, const StringView& title) } } +void TabWidget::set_tab_icon(Widget& tab, const Gfx::Bitmap* icon) +{ + for (auto& t : m_tabs) { + if (t.widget == &tab) { + t.icon = icon; + update(); + return; + } + } +} + void TabWidget::activate_next_tab() { if (m_tabs.size() <= 1) |