Fix problem resetting fonts after F5 for widgets with "mini font" (after theme is reloaded)

There are some widgets (e.g. fg/bg color buttons in the ColorBar, and
ContextBar's check-boxes) that use a "mini font". We could setup the mini
font in onInitTheme(), but the whole program is not ready to do something
like that (there are too much child_spacing/borders values that are set
outside onInitTheme).

A better way is to ask to the theme itself (Theme::getWidgetFont())
about what font to use for each specific widget. And the Widget::m_font
field can act as a cache of this requested font. So now the "mini font"
is specified in a SkinProperty's flag.
This commit is contained in:
David Capello 2015-05-05 19:14:33 -03:00
parent fed03e7d7b
commit eeb6d6e1b5
15 changed files with 87 additions and 83 deletions

View File

@ -281,7 +281,8 @@ void save_window_pos(Widget* window, const char *section)
Widget* setup_mini_font(Widget* widget)
{
widget->setFont(((SkinTheme*)widget->getTheme())->getMiniFont());
SkinPropertyPtr skinProp = get_skin_property(widget);
skinProp->setMiniFont();
return widget;
}

View File

@ -47,7 +47,7 @@ ColorButton::ColorButton(const app::Color& color, PixelFormat pixelFormat)
{
this->setFocusStop(true);
setFont(static_cast<SkinTheme*>(getTheme())->getMiniFont());
setup_mini_font(this);
}
ColorButton::~ColorButton()

View File

@ -651,7 +651,6 @@ public:
}
protected:
void onClick(Event& ev) override {
CheckBox::onClick(ev);
@ -804,15 +803,16 @@ ContextBar::ContextBar()
// addChild(new InkSelectionField());
addChild(m_sprayBox = new HBox());
m_sprayBox->addChild(setup_mini_font(new Label("Spray:")));
m_sprayBox->addChild(m_sprayLabel = new Label("Spray:"));
m_sprayBox->addChild(m_sprayWidth = new SprayWidthField());
m_sprayBox->addChild(m_spraySpeed = new SpraySpeedField());
setup_mini_font(m_sprayLabel);
addChild(m_freehandBox = new HBox());
#if 0 // TODO for v1.1
Label* freehandLabel;
m_freehandBox->addChild(freehandLabel = new Label("Freehand:"));
setup_mini_font(freehandLabel);
m_freehandBox->addChild(m_freehandLabel = new Label("Freehand:"));
setup_mini_font(m_freehandLabel);
#endif
m_freehandBox->addChild(m_freehandAlgo = new FreehandAlgorithmField());
@ -856,11 +856,6 @@ ContextBar::~ContextBar()
m_toolSettings->removeObserver(this);
}
bool ContextBar::onProcessMessage(Message* msg)
{
return Box::onProcessMessage(msg);
}
void ContextBar::onPreferredSize(PreferredSizeEvent& ev)
{
ev.setPreferredSize(gfx::Size(0, 18*guiscale())); // TODO calculate height

View File

@ -61,7 +61,6 @@ namespace app {
IBrushSettings* brushSettings = nullptr);
protected:
bool onProcessMessage(ui::Message* msg) override;
void onPreferredSize(ui::PreferredSizeEvent& ev) override;
// ToolSettingsObserver impl
@ -122,6 +121,7 @@ namespace app {
FreehandAlgorithmField* m_freehandAlgo;
BrushPatternField* m_brushPatternField;
ui::Box* m_sprayBox;
ui::Label* m_sprayLabel;
SprayWidthField* m_sprayWidth;
SpraySpeedField* m_spraySpeed;
ui::Box* m_selectionOptionsBox;

View File

@ -38,6 +38,9 @@ namespace app {
LookType getLook() const { return m_look; }
void setLook(LookType look) { m_look = look; }
bool hasMiniFont() const { return m_miniFont; }
void setMiniFont() { m_miniFont = true; }
int getUpperLeft() const { return m_upperLeft; }
int getUpperRight() const { return m_upperRight; }
int getLowerLeft() const { return m_lowerLeft; }
@ -50,6 +53,7 @@ namespace app {
private:
LookType m_look;
bool m_miniFont;
int m_upperLeft;
int m_upperRight;
int m_lowerLeft;

View File

@ -627,6 +627,15 @@ she::Surface* SkinTheme::sliceSheet(she::Surface* sur, const gfx::Rect& bounds)
return sur;
}
she::Font* SkinTheme::getWidgetFont(const Widget* widget) const
{
SkinPropertyPtr skinPropery = widget->getProperty(SkinProperty::Name);
if (skinPropery && skinPropery->hasMiniFont())
return getMiniFont();
else
return getDefaultFont();
}
Cursor* SkinTheme::getCursor(CursorType type)
{
if (type == kNoCursor) {

View File

@ -47,40 +47,41 @@ namespace app {
SkinTheme();
~SkinTheme();
she::Font* getDefaultFont() const { return m_defaultFont; }
she::Font* getDefaultFont() const override { return m_defaultFont; }
she::Font* getWidgetFont(const ui::Widget* widget) const override;
she::Font* getMiniFont() const { return m_miniFont; }
ui::Cursor* getCursor(ui::CursorType type);
void initWidget(ui::Widget* widget);
void getWindowMask(ui::Widget* widget, gfx::Region& region);
void setDecorativeWidgetBounds(ui::Widget* widget);
ui::Cursor* getCursor(ui::CursorType type) override;
void initWidget(ui::Widget* widget) override;
void getWindowMask(ui::Widget* widget, gfx::Region& region) override;
void setDecorativeWidgetBounds(ui::Widget* widget) override;
void paintDesktop(ui::PaintEvent& ev);
void paintBox(ui::PaintEvent& ev);
void paintButton(ui::PaintEvent& ev);
void paintCheckBox(ui::PaintEvent& ev);
void paintEntry(ui::PaintEvent& ev);
void paintGrid(ui::PaintEvent& ev);
void paintLabel(ui::PaintEvent& ev);
void paintLinkLabel(ui::PaintEvent& ev);
void paintListBox(ui::PaintEvent& ev);
void paintListItem(ui::PaintEvent& ev);
void paintMenu(ui::PaintEvent& ev);
void paintMenuItem(ui::PaintEvent& ev);
void paintSplitter(ui::PaintEvent& ev);
void paintRadioButton(ui::PaintEvent& ev);
void paintSeparator(ui::PaintEvent& ev);
void paintSlider(ui::PaintEvent& ev);
void paintComboBoxEntry(ui::PaintEvent& ev);
void paintComboBoxButton(ui::PaintEvent& ev);
void paintTextBox(ui::PaintEvent& ev);
void paintView(ui::PaintEvent& ev);
void paintViewScrollbar(ui::PaintEvent& ev);
void paintViewViewport(ui::PaintEvent& ev);
void paintWindow(ui::PaintEvent& ev);
void paintPopupWindow(ui::PaintEvent& ev);
void paintDesktop(ui::PaintEvent& ev) override;
void paintBox(ui::PaintEvent& ev) override;
void paintButton(ui::PaintEvent& ev) override;
void paintCheckBox(ui::PaintEvent& ev) override;
void paintEntry(ui::PaintEvent& ev) override;
void paintGrid(ui::PaintEvent& ev) override;
void paintLabel(ui::PaintEvent& ev) override;
void paintLinkLabel(ui::PaintEvent& ev) override;
void paintListBox(ui::PaintEvent& ev) override;
void paintListItem(ui::PaintEvent& ev) override;
void paintMenu(ui::PaintEvent& ev) override;
void paintMenuItem(ui::PaintEvent& ev) override;
void paintSplitter(ui::PaintEvent& ev) override;
void paintRadioButton(ui::PaintEvent& ev) override;
void paintSeparator(ui::PaintEvent& ev) override;
void paintSlider(ui::PaintEvent& ev) override;
void paintComboBoxEntry(ui::PaintEvent& ev) override;
void paintComboBoxButton(ui::PaintEvent& ev) override;
void paintTextBox(ui::PaintEvent& ev) override;
void paintView(ui::PaintEvent& ev) override;
void paintViewScrollbar(ui::PaintEvent& ev) override;
void paintViewViewport(ui::PaintEvent& ev) override;
void paintWindow(ui::PaintEvent& ev) override;
void paintPopupWindow(ui::PaintEvent& ev) override;
void paintTooltip(ui::PaintEvent& ev) override;
void paintWindowButton(ui::PaintEvent& ev);
void paintTooltip(ui::PaintEvent& ev);
int get_button_selected_offset() const { return 0; } // TODO Configurable in xml

View File

@ -1,5 +1,5 @@
// Aseprite UI Library
// Copyright (C) 2001-2013 David Capello
// Copyright (C) 2001-2013, 2015 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
@ -22,9 +22,9 @@ Component::~Component()
{
}
PropertyPtr Component::getProperty(const std::string& name)
PropertyPtr Component::getProperty(const std::string& name) const
{
Properties::iterator it = m_properties.find(name);
auto it = m_properties.find(name);
if (it != m_properties.end())
return it->second;
else
@ -36,10 +36,9 @@ void Component::setProperty(PropertyPtr property)
m_properties[property->getName()] = property;
}
bool Component::hasProperty(const std::string& name)
bool Component::hasProperty(const std::string& name) const
{
Properties::iterator it = m_properties.find(name);
return it != m_properties.end();
return (m_properties.find(name) != m_properties.end());
}
void Component::removeProperty(const std::string& name)

View File

@ -1,5 +1,5 @@
// Aseprite UI Library
// Copyright (C) 2001-2013 David Capello
// Copyright (C) 2001-2013, 2015 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
@ -27,10 +27,10 @@ namespace ui {
Component();
virtual ~Component();
PropertyPtr getProperty(const std::string& name);
PropertyPtr getProperty(const std::string& name) const;
void setProperty(PropertyPtr property);
bool hasProperty(const std::string& name);
bool hasProperty(const std::string& name) const;
void removeProperty(const std::string& name);
const Properties& getProperties() const;

View File

@ -1,5 +1,5 @@
// Aseprite UI Library
// Copyright (C) 2001-2013 David Capello
// Copyright (C) 2001-2013, 2015 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
@ -37,36 +37,29 @@ void addWidget(Widget* widget)
void removeWidget(Widget* widget)
{
std::list<Widget*>::iterator it =
std::find(widgets->begin(), widgets->end(), widget);
auto it = std::find(widgets->begin(), widgets->end(), widget);
if (it != widgets->end())
widgets->erase(it);
}
void setFontOfAllWidgets(she::Font* font)
void resetFontAllWidgets()
{
for (std::list<Widget*>::iterator
it=widgets->begin(), end=widgets->end();
it != end; ++it) {
(*it)->setFont(font);
}
for (auto widget : *widgets)
widget->resetFont();
}
void reinitThemeForAllWidgets()
{
// Reinitialize the theme of each widget
for (std::list<Widget*>::iterator it=widgets->begin(), end=widgets->end();
it != end; ++it) {
(*it)->setTheme(CurrentTheme::get());
(*it)->initTheme();
}
for (auto widget : *widgets) {
widget->setTheme(CurrentTheme::get());
widget->initTheme();
}
// Remap the windows
for (std::list<Widget*>::iterator it=widgets->begin(), end=widgets->end();
it != end; ++it) {
if ((*it)->type == kWindowWidget)
static_cast<Window*>(*it)->remapWindow();
for (auto widget : *widgets) {
if (widget->type == kWindowWidget)
static_cast<Window*>(widget)->remapWindow();
}
// Redraw the whole screen

View File

@ -1,5 +1,5 @@
// Aseprite UI Library
// Copyright (C) 2001-2013 David Capello
// Copyright (C) 2001-2013, 2015 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
@ -26,7 +26,7 @@ namespace ui {
void addWidget(Widget* widget);
void removeWidget(Widget* widget);
void setFontOfAllWidgets(she::Font* font);
void resetFontAllWidgets();
void reinitThemeForAllWidgets();
// theme.cpp

View File

@ -45,7 +45,7 @@ void Theme::regenerate()
onRegenerate();
setFontOfAllWidgets(getDefaultFont());
resetFontAllWidgets();
// TODO We cannot reinitialize all widgets because this mess all
// child spacing, border, etc. But it could be good to change the

View File

@ -39,6 +39,7 @@ namespace ui {
void setScale(int value) { m_guiscale = value; }
virtual she::Font* getDefaultFont() const = 0;
virtual she::Font* getWidgetFont(const Widget* widget) const = 0;
virtual Cursor* getCursor(CursorType type) = 0;
virtual void initWidget(Widget* widget) = 0;

View File

@ -80,7 +80,7 @@ Widget::Widget(WidgetType type)
this->m_theme = CurrentTheme::get();
this->m_align = 0;
this->m_font = (this->m_theme ? this->m_theme->getDefaultFont(): nullptr);
this->m_font = nullptr;
this->m_bgColor = gfx::ColorNone;
m_preferredSize = NULL;
@ -167,13 +167,16 @@ void Widget::setTextQuiet(const std::string& text)
she::Font* Widget::getFont() const
{
if (!m_font) {
ASSERT(m_theme);
m_font = m_theme->getWidgetFont(this);
}
return m_font;
}
void Widget::setFont(she::Font* font)
void Widget::resetFont()
{
m_font = font;
invalidate();
m_font = nullptr;
}
void Widget::setBgColor(gfx::Color color)
@ -185,9 +188,7 @@ void Widget::setBgColor(gfx::Color color)
void Widget::setTheme(Theme* theme)
{
m_theme = theme;
// TODO maybe some Style in Widget should be great
setFont(m_theme ? m_theme->getDefaultFont(): nullptr);
m_font = nullptr;
}
// ===============================================================

View File

@ -142,7 +142,7 @@ namespace ui {
// ===============================================================
she::Font* getFont() const;
void setFont(she::Font* font);
void resetFont();
// Gets the background color of the widget.
gfx::Color getBgColor() const {
@ -382,7 +382,7 @@ namespace ui {
Theme* m_theme; // Widget's theme
int m_align; // Widget alignment
std::string m_text; // Widget text
she::Font* m_font; // Text font type
mutable she::Font* m_font; // Cached font returned by the theme
gfx::Color m_bgColor; // Background color
gfx::Rect m_bounds;
gfx::Region m_updateRegion; // Region to be redrawed.