From 5806630cf49f66cffe0bc2f01042f709fc7b24ee Mon Sep 17 00:00:00 2001 From: thankyouverycool <66646555+thankyouverycool@users.noreply.github.com> Date: Sun, 7 Mar 2021 17:39:01 -0500 Subject: [PATCH] LibGfx+LibGUI: Add support for vertical ProgressBars --- Userland/Libraries/LibGUI/ProgressBar.cpp | 15 +++++++-- Userland/Libraries/LibGUI/ProgressBar.h | 32 ++++++++++++++++++- .../Libraries/LibGfx/ClassicStylePainter.cpp | 14 +++++--- .../Libraries/LibGfx/ClassicStylePainter.h | 2 +- Userland/Libraries/LibGfx/Painter.cpp | 2 +- Userland/Libraries/LibGfx/StylePainter.cpp | 4 +-- Userland/Libraries/LibGfx/StylePainter.h | 4 +-- 7 files changed, 60 insertions(+), 13 deletions(-) diff --git a/Userland/Libraries/LibGUI/ProgressBar.cpp b/Userland/Libraries/LibGUI/ProgressBar.cpp index e2732a738c9..e9747f33698 100644 --- a/Userland/Libraries/LibGUI/ProgressBar.cpp +++ b/Userland/Libraries/LibGUI/ProgressBar.cpp @@ -31,10 +31,13 @@ #include REGISTER_WIDGET(GUI, ProgressBar) +REGISTER_WIDGET(GUI, VerticalProgressBar) +REGISTER_WIDGET(GUI, HorizontalProgressBar) namespace GUI { -ProgressBar::ProgressBar() +ProgressBar::ProgressBar(Orientation orientation) + : m_orientation(orientation) { REGISTER_STRING_PROPERTY("text", text, set_text); REGISTER_ENUM_PROPERTY("format", format, set_format, Format, @@ -90,7 +93,15 @@ void ProgressBar::paint_event(PaintEvent& event) progress_text = builder.to_string(); } - Gfx::StylePainter::paint_progress_bar(painter, rect, palette(), m_min, m_max, m_value, progress_text); + Gfx::StylePainter::paint_progress_bar(painter, rect, palette(), m_min, m_max, m_value, progress_text, m_orientation); +} + +void ProgressBar::set_orientation(Orientation value) +{ + if (m_orientation == value) + return; + m_orientation = value; + update(); } } diff --git a/Userland/Libraries/LibGUI/ProgressBar.h b/Userland/Libraries/LibGUI/ProgressBar.h index 9c9fddacd7c..b4965ca6c84 100644 --- a/Userland/Libraries/LibGUI/ProgressBar.h +++ b/Userland/Libraries/LibGUI/ProgressBar.h @@ -44,6 +44,9 @@ public: int min() const { return m_min; } int max() const { return m_max; } + void set_orientation(Orientation value); + Orientation orientation() const { return m_orientation; } + String text() const { return m_text; } void set_text(String text) { m_text = move(text); } @@ -56,7 +59,7 @@ public: void set_format(Format format) { m_format = format; } protected: - ProgressBar(); + ProgressBar(Orientation = Orientation::Horizontal); virtual void paint_event(PaintEvent&) override; @@ -66,6 +69,33 @@ private: int m_max { 100 }; int m_value { 0 }; String m_text; + Orientation m_orientation { Orientation::Horizontal }; +}; + +class VerticalProgressBar final : public ProgressBar { + C_OBJECT(VerticalProgressBar); + +public: + virtual ~VerticalProgressBar() override { } + +private: + VerticalProgressBar() + : ProgressBar(Orientation::Vertical) + { + } +}; + +class HorizontalProgressBar final : public ProgressBar { + C_OBJECT(HorizontalProgressBar); + +public: + virtual ~HorizontalProgressBar() override { } + +private: + HorizontalProgressBar() + : ProgressBar(Orientation::Horizontal) + { + } }; } diff --git a/Userland/Libraries/LibGfx/ClassicStylePainter.cpp b/Userland/Libraries/LibGfx/ClassicStylePainter.cpp index 95c9e9ffb6f..b90b9d7a004 100644 --- a/Userland/Libraries/LibGfx/ClassicStylePainter.cpp +++ b/Userland/Libraries/LibGfx/ClassicStylePainter.cpp @@ -309,13 +309,13 @@ void ClassicStylePainter::paint_window_frame(Painter& painter, const IntRect& re painter.draw_line(rect.bottom_left().translated(3, -3), rect.bottom_right().translated(-3, -3), base_color); } -void ClassicStylePainter::paint_progress_bar(Painter& painter, const IntRect& rect, const Palette& palette, int min, int max, int value, const StringView& text) +void ClassicStylePainter::paint_progress_bar(Painter& painter, const IntRect& rect, const Palette& palette, int min, int max, int value, const StringView& text, Orientation orientation) { // First we fill the entire widget with the gradient. This incurs a bit of // overdraw but ensures a consistent look throughout the progression. Color start_color = palette.active_window_border1(); Color end_color = palette.active_window_border2(); - painter.fill_rect_with_gradient(rect, start_color, end_color); + painter.fill_rect_with_gradient(orientation, rect, start_color, end_color); if (!text.is_null()) { painter.draw_text(rect.translated(1, 1), text, TextAlignment::Center, palette.base_text()); @@ -327,8 +327,14 @@ void ClassicStylePainter::paint_progress_bar(Painter& painter, const IntRect& re // Then we carve out a hole in the remaining part of the widget. // We draw the text a third time, clipped and inverse, for sharp contrast. - float progress_width = progress * rect.width(); - IntRect hole_rect { (int)progress_width, 0, (int)(rect.width() - progress_width), rect.height() }; + IntRect hole_rect; + if (orientation == Orientation::Horizontal) { + float progress_width = progress * rect.width(); + hole_rect = { (int)progress_width, 0, (int)(rect.width() - progress_width), rect.height() }; + } else { + float progress_height = progress * rect.height(); + hole_rect = { 0, 0, rect.width(), (int)(rect.height() - progress_height) }; + } hole_rect.move_by(rect.location()); hole_rect.set_right_without_resize(rect.right()); PainterStateSaver saver(painter); diff --git a/Userland/Libraries/LibGfx/ClassicStylePainter.h b/Userland/Libraries/LibGfx/ClassicStylePainter.h index de5302ecb5a..7054056b198 100644 --- a/Userland/Libraries/LibGfx/ClassicStylePainter.h +++ b/Userland/Libraries/LibGfx/ClassicStylePainter.h @@ -40,7 +40,7 @@ public: void paint_surface(Painter&, const IntRect&, const Palette&, bool paint_vertical_lines = true, bool paint_top_line = true) override; void paint_frame(Painter&, const IntRect&, const Palette&, FrameShape, FrameShadow, int thickness, bool skip_vertical_lines = false) override; void paint_window_frame(Painter&, const IntRect&, const Palette&) override; - void paint_progress_bar(Painter&, const IntRect&, const Palette&, int min, int max, int value, const StringView& text) override; + void paint_progress_bar(Painter&, const IntRect&, const Palette&, int min, int max, int value, const StringView& text, Orientation = Orientation::Horizontal) override; void paint_radio_button(Painter&, const IntRect&, const Palette&, bool is_checked, bool is_being_pressed) override; void paint_check_box(Painter&, const IntRect&, const Palette&, bool is_enabled, bool is_checked, bool is_being_pressed) override; void paint_transparency_grid(Painter&, const IntRect&, const Palette&) override; diff --git a/Userland/Libraries/LibGfx/Painter.cpp b/Userland/Libraries/LibGfx/Painter.cpp index 383df0ca91a..9d0e37d5b09 100644 --- a/Userland/Libraries/LibGfx/Painter.cpp +++ b/Userland/Libraries/LibGfx/Painter.cpp @@ -245,7 +245,7 @@ void Painter::fill_rect_with_gradient(Orientation orientation, const IntRect& a_ float c = offset * increment; float c_alpha = gradient_start.alpha() + offset * alpha_increment; for (int i = clipped_rect.height() - 1; i >= 0; --i) { - auto color = gamma_accurate_blend(gradient_start, gradient_end, c); + auto color = gamma_accurate_blend(gradient_end, gradient_start, c); color.set_alpha(c_alpha); for (int j = 0; j < clipped_rect.width(); ++j) { dst[j] = color.value(); diff --git a/Userland/Libraries/LibGfx/StylePainter.cpp b/Userland/Libraries/LibGfx/StylePainter.cpp index ad830958d88..ec38c063fb5 100644 --- a/Userland/Libraries/LibGfx/StylePainter.cpp +++ b/Userland/Libraries/LibGfx/StylePainter.cpp @@ -63,9 +63,9 @@ void StylePainter::paint_window_frame(Painter& painter, const IntRect& rect, con current().paint_window_frame(painter, rect, palette); } -void StylePainter::paint_progress_bar(Painter& painter, const IntRect& rect, const Palette& palette, int min, int max, int value, const StringView& text) +void StylePainter::paint_progress_bar(Painter& painter, const IntRect& rect, const Palette& palette, int min, int max, int value, const StringView& text, Orientation orientation) { - current().paint_progress_bar(painter, rect, palette, min, max, value, text); + current().paint_progress_bar(painter, rect, palette, min, max, value, text, orientation); } void StylePainter::paint_radio_button(Painter& painter, const IntRect& rect, const Palette& palette, bool is_checked, bool is_being_pressed) diff --git a/Userland/Libraries/LibGfx/StylePainter.h b/Userland/Libraries/LibGfx/StylePainter.h index 779241c2521..0969178f1e4 100644 --- a/Userland/Libraries/LibGfx/StylePainter.h +++ b/Userland/Libraries/LibGfx/StylePainter.h @@ -59,7 +59,7 @@ public: virtual void paint_surface(Painter&, const IntRect&, const Palette&, bool paint_vertical_lines = true, bool paint_top_line = true) = 0; virtual void paint_frame(Painter&, const IntRect&, const Palette&, FrameShape, FrameShadow, int thickness, bool skip_vertical_lines = false) = 0; virtual void paint_window_frame(Painter&, const IntRect&, const Palette&) = 0; - virtual void paint_progress_bar(Painter&, const IntRect&, const Palette&, int min, int max, int value, const StringView& text) = 0; + virtual void paint_progress_bar(Painter&, const IntRect&, const Palette&, int min, int max, int value, const StringView& text, Orientation = Orientation::Horizontal) = 0; virtual void paint_radio_button(Painter&, const IntRect&, const Palette&, bool is_checked, bool is_being_pressed) = 0; virtual void paint_check_box(Painter&, const IntRect&, const Palette&, bool is_enabled, bool is_checked, bool is_being_pressed) = 0; virtual void paint_transparency_grid(Painter&, const IntRect&, const Palette&) = 0; @@ -78,7 +78,7 @@ public: static void paint_surface(Painter&, const IntRect&, const Palette&, bool paint_vertical_lines = true, bool paint_top_line = true); static void paint_frame(Painter&, const IntRect&, const Palette&, FrameShape, FrameShadow, int thickness, bool skip_vertical_lines = false); static void paint_window_frame(Painter&, const IntRect&, const Palette&); - static void paint_progress_bar(Painter&, const IntRect&, const Palette&, int min, int max, int value, const StringView& text); + static void paint_progress_bar(Painter&, const IntRect&, const Palette&, int min, int max, int value, const StringView& text, Orientation = Orientation::Horizontal); static void paint_radio_button(Painter&, const IntRect&, const Palette&, bool is_checked, bool is_being_pressed); static void paint_check_box(Painter&, const IntRect&, const Palette&, bool is_enabled, bool is_checked, bool is_being_pressed); static void paint_transparency_grid(Painter&, const IntRect&, const Palette&);