mirror of
https://github.com/RGBCube/serenity
synced 2025-07-10 12:27:35 +00:00
StylePainter: Move progress bar painting from GProgressBar to here
We need to be able to paint progress bars without having a widget. :^)
This commit is contained in:
parent
f9b8a18fae
commit
b777f740a4
3 changed files with 36 additions and 24 deletions
|
@ -226,3 +226,32 @@ void StylePainter::paint_window_frame(Painter& painter, const Rect& rect)
|
||||||
painter.draw_line(rect.bottom_left().translated(1, -1), rect.bottom_right().translated(-1, -1), mid_shade);
|
painter.draw_line(rect.bottom_left().translated(1, -1), rect.bottom_right().translated(-1, -1), mid_shade);
|
||||||
painter.draw_line(rect.bottom_left().translated(2, -2), rect.bottom_right().translated(-2, -2), base_color);
|
painter.draw_line(rect.bottom_left().translated(2, -2), rect.bottom_right().translated(-2, -2), base_color);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void StylePainter::paint_progress_bar(Painter& painter, const Rect& rect, int min, int max, int value, const StringView& text)
|
||||||
|
{
|
||||||
|
// 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(110, 34, 9);
|
||||||
|
Color end_color(244, 202, 158);
|
||||||
|
painter.fill_rect_with_gradient(rect, start_color, end_color);
|
||||||
|
|
||||||
|
if (!text.is_null()) {
|
||||||
|
painter.draw_text(rect.translated(1, 1), text, TextAlignment::Center, Color::Black);
|
||||||
|
painter.draw_text(rect, text, TextAlignment::Center, Color::White);
|
||||||
|
}
|
||||||
|
|
||||||
|
float range_size = max - min;
|
||||||
|
float progress = (value - min) / range_size;
|
||||||
|
|
||||||
|
// 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();
|
||||||
|
Rect hole_rect { (int)progress_width, 0, (int)(rect.width() - progress_width), rect.height() };
|
||||||
|
hole_rect.move_by(rect.location());
|
||||||
|
PainterStateSaver saver(painter);
|
||||||
|
painter.fill_rect(hole_rect, Color::White);
|
||||||
|
|
||||||
|
painter.add_clip_rect(hole_rect);
|
||||||
|
if (!text.is_null())
|
||||||
|
painter.draw_text(rect.translated(0, 0), text, TextAlignment::Center, Color::Black);
|
||||||
|
}
|
||||||
|
|
|
@ -30,6 +30,7 @@ public:
|
||||||
static void paint_surface(Painter&, const Rect&, bool paint_vertical_lines = true, bool paint_top_line = true);
|
static void paint_surface(Painter&, const Rect&, bool paint_vertical_lines = true, bool paint_top_line = true);
|
||||||
static void paint_frame(Painter&, const Rect&, FrameShape, FrameShadow, int thickness, bool skip_vertical_lines = false);
|
static void paint_frame(Painter&, const Rect&, FrameShape, FrameShadow, int thickness, bool skip_vertical_lines = false);
|
||||||
static void paint_window_frame(Painter&, const Rect&);
|
static void paint_window_frame(Painter&, const Rect&);
|
||||||
|
static void paint_progress_bar(Painter&, const Rect&, int min, int max, int value, const StringView& text = {});
|
||||||
|
|
||||||
static Color hover_highlight_color() { return Color::from_rgb(0xe6e5e2); }
|
static Color hover_highlight_color() { return Color::from_rgb(0xe6e5e2); }
|
||||||
};
|
};
|
||||||
|
|
|
@ -42,39 +42,21 @@ void GProgressBar::paint_event(GPaintEvent& event)
|
||||||
painter.add_clip_rect(rect);
|
painter.add_clip_rect(rect);
|
||||||
painter.add_clip_rect(event.rect());
|
painter.add_clip_rect(event.rect());
|
||||||
|
|
||||||
// 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(110, 34, 9);
|
|
||||||
Color end_color(244, 202, 158);
|
|
||||||
painter.fill_rect_with_gradient(rect, start_color, end_color);
|
|
||||||
|
|
||||||
float range_size = m_max - m_min;
|
|
||||||
float progress = (m_value - m_min) / range_size;
|
|
||||||
|
|
||||||
String progress_text;
|
String progress_text;
|
||||||
if (m_format != Format::NoText) {
|
if (m_format != Format::NoText) {
|
||||||
// Then we draw the progress text over the gradient.
|
// Then we draw the progress text over the gradient.
|
||||||
// We draw it twice, once offset (1, 1) for a drop shadow look.
|
// We draw it twice, once offset (1, 1) for a drop shadow look.
|
||||||
StringBuilder builder;
|
StringBuilder builder;
|
||||||
builder.append(m_caption);
|
builder.append(m_caption);
|
||||||
if (m_format == Format::Percentage)
|
if (m_format == Format::Percentage) {
|
||||||
|
float range_size = m_max - m_min;
|
||||||
|
float progress = (m_value - m_min) / range_size;
|
||||||
builder.appendf("%d%%", (int)(progress * 100));
|
builder.appendf("%d%%", (int)(progress * 100));
|
||||||
else if (m_format == Format::ValueSlashMax)
|
} else if (m_format == Format::ValueSlashMax) {
|
||||||
builder.appendf("%d/%d", m_value, m_max);
|
builder.appendf("%d/%d", m_value, m_max);
|
||||||
|
}
|
||||||
progress_text = builder.to_string();
|
progress_text = builder.to_string();
|
||||||
|
|
||||||
painter.draw_text(rect.translated(1, 1), progress_text, TextAlignment::Center, Color::Black);
|
|
||||||
painter.draw_text(rect, progress_text, TextAlignment::Center, Color::White);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Then we carve out a hole in the remaining part of the widget.
|
StylePainter::paint_progress_bar(painter, rect, m_min, m_max, m_value, progress_text);
|
||||||
// We draw the text a third time, clipped and inverse, for sharp contrast.
|
|
||||||
float progress_width = progress * width();
|
|
||||||
Rect hole_rect { (int)progress_width, 0, (int)(width() - progress_width), height() };
|
|
||||||
painter.add_clip_rect(hole_rect);
|
|
||||||
painter.fill_rect(hole_rect, Color::White);
|
|
||||||
|
|
||||||
if (m_format != Format::NoText)
|
|
||||||
painter.draw_text(rect.translated(0, 0), progress_text, TextAlignment::Center, Color::Black);
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue