ladybird/Libraries/LibGUI/GProgressBar.cpp
Andreas Kling a79bac428b LibGUI+LibDraw: Add "Palette" concept for scoped color theming
GApplication now has a palette. This palette contains all the system
theme colors by default, and is inherited by a new top-level GWidget.
New child widgets inherit their parents palette.

It is possible to override the GApplication palette, and the palette
of any GWidget.

The Palette object contains a bunch of colors, each corresponding to
a ColorRole. Each role has a convenience getter as well.

Each GWidget now has a background_role() and foreground_role(), which
are then looked up in their current palette when painting. This means
that you no longer alter the background color of a widget by setting
it directly, rather you alter either its background role, or the
widget's palette.
2019-12-24 21:27:16 +01:00

62 lines
1.6 KiB
C++

#include <AK/StringBuilder.h>
#include <LibGUI/GPainter.h>
#include <LibGUI/GProgressBar.h>
GProgressBar::GProgressBar(GWidget* parent)
: GFrame(parent)
{
set_frame_shape(FrameShape::Container);
set_frame_shadow(FrameShadow::Sunken);
set_frame_thickness(2);
}
GProgressBar::~GProgressBar()
{
}
void GProgressBar::set_value(int value)
{
if (m_value == value)
return;
m_value = value;
update();
}
void GProgressBar::set_range(int min, int max)
{
ASSERT(min < max);
m_min = min;
m_max = max;
if (m_value > m_max)
m_value = m_max;
if (m_value < m_min)
m_value = m_min;
}
void GProgressBar::paint_event(GPaintEvent& event)
{
GFrame::paint_event(event);
GPainter painter(*this);
auto rect = frame_inner_rect();
painter.add_clip_rect(rect);
painter.add_clip_rect(event.rect());
String progress_text;
if (m_format != Format::NoText) {
// Then we draw the progress text over the gradient.
// We draw it twice, once offset (1, 1) for a drop shadow look.
StringBuilder builder;
builder.append(m_caption);
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));
} else if (m_format == Format::ValueSlashMax) {
builder.appendf("%d/%d", m_value, m_max);
}
progress_text = builder.to_string();
}
StylePainter::paint_progress_bar(painter, rect, palette(), m_min, m_max, m_value, progress_text);
}