From 070a2f8980594d256579fe9f6963be331edae89c Mon Sep 17 00:00:00 2001 From: Rhin Date: Mon, 1 Jul 2019 02:46:36 -0500 Subject: [PATCH] LibGUI: GScrollbar compression when very small (#255) Fixes #124. --- LibGUI/GScrollBar.cpp | 14 ++++++++------ LibGUI/GScrollBar.h | 3 ++- LibGUI/GWidget.h | 1 + 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/LibGUI/GScrollBar.cpp b/LibGUI/GScrollBar.cpp index dabd4649e98..f1ce3913090 100644 --- a/LibGUI/GScrollBar.cpp +++ b/LibGUI/GScrollBar.cpp @@ -165,20 +165,20 @@ bool GScrollBar::has_scrubber() const int GScrollBar::scrubber_size() const { - int pixel_range = (orientation() == Orientation::Vertical ? height() : width()) - button_size() * 2; + int pixel_range = length(orientation()) - button_size() * 2; int value_range = m_max - m_min; return ::max(pixel_range - value_range, button_size()); } Rect GScrollBar::scrubber_rect() const { - if (!has_scrubber()) + if (!has_scrubber() || length(orientation()) <= (button_size() * 2) + scrubber_size()) return {}; float x_or_y; if (m_value == m_min) x_or_y = button_size(); else if (m_value == m_max) - x_or_y = ((orientation() == Orientation::Vertical ? height() : width()) - button_size() - scrubber_size()) + 1; + x_or_y = (length(orientation()) - button_size() - scrubber_size()) + 1; else { float range_size = m_max - m_min; float available = scrubbable_range_in_pixels(); @@ -200,10 +200,12 @@ void GScrollBar::paint_event(GPaintEvent& event) painter.fill_rect(rect(), Color::from_rgb(0xd6d2ce)); StylePainter::paint_button(painter, decrement_button_rect(), ButtonStyle::Normal, false, m_hovered_component == Component::DecrementButton); - painter.draw_bitmap(decrement_button_rect().location().translated(3, 3), orientation() == Orientation::Vertical ? *s_up_arrow_bitmap : *s_left_arrow_bitmap, has_scrubber() ? Color::Black : Color::MidGray); - StylePainter::paint_button(painter, increment_button_rect(), ButtonStyle::Normal, false, m_hovered_component == Component::IncrementButton); - painter.draw_bitmap(increment_button_rect().location().translated(3, 3), orientation() == Orientation::Vertical ? *s_down_arrow_bitmap : *s_right_arrow_bitmap, has_scrubber() ? Color::Black : Color::MidGray); + + if (length(orientation()) > default_button_size()) { + painter.draw_bitmap(decrement_button_rect().location().translated(3, 3), orientation() == Orientation::Vertical ? *s_up_arrow_bitmap : *s_left_arrow_bitmap, has_scrubber() ? Color::Black : Color::MidGray); + painter.draw_bitmap(increment_button_rect().location().translated(3, 3), orientation() == Orientation::Vertical ? *s_down_arrow_bitmap : *s_right_arrow_bitmap, has_scrubber() ? Color::Black : Color::MidGray); + } if (has_scrubber()) StylePainter::paint_button(painter, scrubber_rect(), ButtonStyle::Normal, false, m_hovered_component == Component::Scrubber); diff --git a/LibGUI/GScrollBar.h b/LibGUI/GScrollBar.h index 938328ae707..ff906d2edc9 100644 --- a/LibGUI/GScrollBar.h +++ b/LibGUI/GScrollBar.h @@ -45,7 +45,8 @@ private: virtual void leave_event(CEvent&) override; virtual void change_event(GEvent&) override; - int button_size() const { return 16; } + int default_button_size() const { return 16; } + int button_size() const { return length(orientation()) <= (default_button_size() * 2) ? length(orientation()) / 2 : default_button_size(); } int button_width() const { return orientation() == Orientation::Vertical ? width() : button_size(); } int button_height() const { return orientation() == Orientation::Horizontal ? height() : button_size(); } Rect decrement_button_rect() const; diff --git a/LibGUI/GWidget.h b/LibGUI/GWidget.h index ff23613c229..b18e55d107b 100644 --- a/LibGUI/GWidget.h +++ b/LibGUI/GWidget.h @@ -94,6 +94,7 @@ public: int y() const { return m_relative_rect.y(); } int width() const { return m_relative_rect.width(); } int height() const { return m_relative_rect.height(); } + int length(Orientation orientation) const { return orientation == Orientation::Vertical ? height() : width(); } Rect rect() const { return { 0, 0, width(), height() }; } Size size() const { return m_relative_rect.size(); }