Add a simple ui::Theme impl

Now ui::Theme is not an abstract class anymore and can be used for
simple tests/benchmarks. Some member functions are empty, but can be
implemented later if needed.
This commit is contained in:
David Capello 2024-11-29 18:10:57 -03:00
parent ab599c5863
commit a71909d0ca
5 changed files with 72 additions and 22 deletions

View File

@ -109,7 +109,7 @@ namespace app {
if (it != m_styles.end())
return it->second;
else
return getDefaultStyle();
return EmptyStyle();
}
SkinPartPtr getPartById(const std::string& id) const {

View File

@ -127,7 +127,7 @@ void Grid::setStyle(Style* style)
{
ASSERT(style);
if (!style)
style = Theme::getDefaultStyle();
style = Theme::EmptyStyle();
Widget::setStyle(style);
setGap(style->gap());
}

View File

@ -34,6 +34,10 @@ namespace ui {
namespace {
// Colors for a simple default theme.
constexpr const gfx::Color kBgColor = gfx::rgba(32, 32, 32, 255);
constexpr const gfx::Color kFgColor = gfx::rgba(255, 255, 200, 255);
int current_ui_scale = 1; // Global UI Screen Scaling factor
int old_ui_scale = 1; // Add this field in InitThemeEvent
Theme* current_theme = nullptr; // Global active theme
@ -145,8 +149,14 @@ Theme::~Theme()
set_theme(nullptr, guiscale());
}
text::FontRef Theme::getDefaultFont() const
{
return m_fontMgr->defaultFont(kDefaultFontHeight);
}
// static
ui::Style Theme::m_defaultStyle(nullptr);
ui::Style Theme::m_emptyStyle(nullptr);
ui::Style Theme::m_simpleStyle(nullptr);
void Theme::regenerateTheme()
{
@ -154,6 +164,27 @@ void Theme::regenerateTheme()
onRegenerateTheme();
}
void Theme::initWidget(Widget* widget)
{
if (m_simpleStyle.layers().empty()) {
Style::Layer bg;
Style::Layer br;
Style::Layer fg;
bg.setType(Style::Layer::Type::kBackground);
bg.setColor(kBgColor);
br.setType(Style::Layer::Type::kBorder);
br.setColor(kFgColor);
fg.setType(Style::Layer::Type::kText);
fg.setColor(kFgColor);
m_simpleStyle.layers().push_back(bg);
m_simpleStyle.layers().push_back(br);
m_simpleStyle.layers().push_back(fg);
}
widget->setFont(getDefaultFont());
widget->setStyle(&m_simpleStyle);
}
void Theme::setDecorativeWidgetBounds(Widget* widget)
{
switch (widget->type()) {
@ -193,6 +224,18 @@ void Theme::setDecorativeWidgetBounds(Widget* widget)
}
}
void Theme::paintListBox(PaintEvent& ev)
{
Graphics* g = ev.graphics();
g->fillRect(kBgColor, g->getClipBounds());
}
void Theme::paintViewViewport(PaintEvent& ev)
{
Graphics* g = ev.graphics();
g->fillRect(kBgColor, g->getClipBounds());
}
void Theme::paintWidgetPart(Graphics* g,
const Style* style,
const gfx::Rect& bounds,

View File

@ -62,28 +62,34 @@ namespace ui {
class Theme {
friend void set_theme(Theme* theme, const int uiscale);
public:
static constexpr int kDefaultFontHeight = 16;
Theme();
virtual ~Theme();
virtual text::FontMgrRef fontMgr() const { return m_fontMgr; }
virtual text::FontRef getDefaultFont() const = 0;
virtual text::FontRef getWidgetFont(const Widget* widget) const = 0;
virtual text::FontRef getDefaultFont() const;
virtual text::FontRef getWidgetFont(const Widget* widget) const {
return getDefaultFont();
}
virtual ui::Cursor* getStandardCursor(CursorType type) = 0;
virtual void initWidget(Widget* widget) = 0;
virtual void getWindowMask(Widget* widget, gfx::Region& region) = 0;
virtual ui::Cursor* getStandardCursor(CursorType type) { return nullptr; }
virtual void initWidget(Widget* widget);
virtual void getWindowMask(Widget* widget, gfx::Region& region) { }
virtual void setDecorativeWidgetBounds(Widget* widget);
virtual int getScrollbarSize() = 0;
virtual gfx::Size getEntryCaretSize(Widget* widget) = 0;
virtual int getScrollbarSize() { return kDefaultFontHeight; }
virtual gfx::Size getEntryCaretSize(Widget* widget) {
return gfx::Size(kDefaultFontHeight, 1);
}
virtual void paintEntry(PaintEvent& ev) = 0;
virtual void paintListBox(PaintEvent& ev) = 0;
virtual void paintMenu(PaintEvent& ev) = 0;
virtual void paintMenuItem(PaintEvent& ev) = 0;
virtual void paintSlider(PaintEvent& ev) = 0;
virtual void paintComboBoxEntry(PaintEvent& ev) = 0;
virtual void paintTextBox(PaintEvent& ev) = 0;
virtual void paintViewViewport(PaintEvent& ev) = 0;
virtual void paintEntry(PaintEvent& ev) { }
virtual void paintListBox(PaintEvent& ev);
virtual void paintMenu(PaintEvent& ev) { }
virtual void paintMenuItem(PaintEvent& ev) { }
virtual void paintSlider(PaintEvent& ev) { }
virtual void paintComboBoxEntry(PaintEvent& ev) { }
virtual void paintTextBox(PaintEvent& ev) { }
virtual void paintViewViewport(PaintEvent& ev);
virtual void paintWidgetPart(Graphics* g,
const Style* style,
@ -145,10 +151,10 @@ namespace ui {
static void drawTextBox(Graphics* g, const Widget* textbox,
int* w, int* h, gfx::Color bg, gfx::Color fg);
static ui::Style* getDefaultStyle() { return &m_defaultStyle; }
static ui::Style* EmptyStyle() { return &m_emptyStyle; }
protected:
virtual void onRegenerateTheme() = 0;
virtual void onRegenerateTheme() { }
text::FontMgrRef m_fontMgr;
@ -175,7 +181,8 @@ namespace ui {
gfx::Border& borderHint,
gfx::Rect& textHint, int& textAlign);
static ui::Style m_defaultStyle;
static ui::Style m_emptyStyle;
static ui::Style m_simpleStyle;
};
} // namespace ui

View File

@ -216,7 +216,7 @@ void Widget::setStyle(Style* style)
assert_ui_thread();
ASSERT(style);
if (!style)
style = Theme::getDefaultStyle();
style = Theme::EmptyStyle();
m_style = style;
m_border = m_theme->calcBorder(this, style);
m_bgColor = m_theme->calcBgColor(this, style);