From f4b8effd52d6ce0a4128ed6dcd7fedcc3c66a2a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Capello?= Date: Mon, 6 May 2024 17:00:27 -0300 Subject: [PATCH] Use ui::Style* instead of strings in ButtonSet::addItem() (fix #4188) --- src/app/ui/brush_popup.cpp | 8 ++-- src/app/ui/button_set.cpp | 48 +++++++++------------ src/app/ui/button_set.h | 12 +++--- src/app/ui/color_bar.cpp | 14 +++---- src/app/ui/context_bar.cpp | 63 +++++++++++++++------------- src/app/ui/selection_mode_field.cpp | 10 ++--- src/app/ui/timeline/ani_controls.cpp | 12 +++--- 7 files changed, 81 insertions(+), 86 deletions(-) diff --git a/src/app/ui/brush_popup.cpp b/src/app/ui/brush_popup.cpp index 2c34f74e9..d8ee03a03 100644 --- a/src/app/ui/brush_popup.cpp +++ b/src/app/ui/brush_popup.cpp @@ -361,9 +361,10 @@ BrushPopup::BrushPopup() m_box.addChild(new Separator("", HORIZONTAL)); for (const auto& brush : brushes.getStandardBrushes()) { + auto* theme = SkinTheme::get(this); m_standardBrushes.addItem( new SelectBrushItem( - BrushSlot(BrushSlot::Flags::BrushType, brush)), "standard_brush"); + BrushSlot(BrushSlot::Flags::BrushType, brush)), theme->styles.standardBrush()); } m_standardBrushes.setTransparent(true); @@ -398,6 +399,7 @@ void BrushPopup::setBrush(Brush* brush) void BrushPopup::regenerate(ui::Display* display, const gfx::Point& pos) { + auto* theme = SkinTheme::get(this); auto& brushSlots = App::instance()->brushes().getBrushSlots(); if (m_customBrushes) { @@ -428,11 +430,11 @@ void BrushPopup::regenerate(ui::Display* display, } m_customBrushes->addItem(new SelectBrushItem(brush, slot)); m_customBrushes->addItem(new BrushShortcutItem(shortcut, slot)); - m_customBrushes->addItem(new BrushOptionsItem(this, slot), "buttonset_item_icon_mono"); + m_customBrushes->addItem(new BrushOptionsItem(this, slot), theme->styles.buttonsetItemIconMono()); } m_customBrushes->addItem(new NewCustomBrushItem, 2, 1); - m_customBrushes->addItem(new NewBrushOptionsItem, "buttonset_item_icon_mono"); + m_customBrushes->addItem(new NewBrushOptionsItem, theme->styles.buttonsetItemIconMono()); m_customBrushes->setExpansive(true); m_customBrushes->initTheme(); m_box.addChild(m_customBrushes); diff --git a/src/app/ui/button_set.cpp b/src/app/ui/button_set.cpp index 361fef8a8..3b0a2096d 100644 --- a/src/app/ui/button_set.cpp +++ b/src/app/ui/button_set.cpp @@ -208,60 +208,50 @@ ButtonSet::ButtonSet(int columns) initTheme(); } -ButtonSet::Item* ButtonSet::addItem(const std::string& text, const char* styleId) +ButtonSet::Item* ButtonSet::addItem(const std::string& text, ui::Style* style) { - return addItem(text, 1, 1, styleId); + return addItem(text, 1, 1, style); } -ButtonSet::Item* ButtonSet::addItem(const std::string& text, int hspan, int vspan, const char* styleId) +ButtonSet::Item* ButtonSet::addItem(const std::string& text, int hspan, int vspan, ui::Style* style) { Item* item = new Item(); item->setText(text); - addItem(item, hspan, vspan, styleId); + addItem(item, hspan, vspan, style); return item; } -ButtonSet::Item* ButtonSet::addItem(const skin::SkinPartPtr& icon, const char* styleId) +ButtonSet::Item* ButtonSet::addItem(const skin::SkinPartPtr& icon, ui::Style* style) { - return addItem(icon, 1, 1, styleId); + return addItem(icon, 1, 1, style); } -ButtonSet::Item* ButtonSet::addItem(const skin::SkinPartPtr& icon, int hspan, int vspan, const char* styleId) +ButtonSet::Item* ButtonSet::addItem(const skin::SkinPartPtr& icon, int hspan, int vspan, ui::Style* style) { Item* item = new Item(); item->setIcon(icon); - addItem(item, hspan, vspan, styleId); + addItem(item, hspan, vspan, style); return item; } -ButtonSet::Item* ButtonSet::addItem(Item* item, const char* styleId) +ButtonSet::Item* ButtonSet::addItem(Item* item, ui::Style* style) { - return addItem(item, 1, 1, styleId); + return addItem(item, 1, 1, style); } -ButtonSet::Item* ButtonSet::addItem(Item* item, int hspan, int vspan, const char* styleIdStr) +ButtonSet::Item* ButtonSet::addItem(Item* item, int hspan, int vspan, ui::Style* style) { - std::string styleId; - if (styleIdStr) - styleId = styleIdStr; - item->InitTheme.connect( - [item, styleId] { - auto theme = SkinTheme::get(item); - ui::Style* style; - if (!styleId.empty()) { - style = theme->getStyleById(styleId); - if (!style) - throw base::Exception(fmt::format("Style {} not found", styleId)); - } - else { - style = theme->styles.buttonsetItemIcon(); + [item, style] { + ui::Style* s = style; + if (!s) { + auto* theme = SkinTheme::get(item); + s = theme->styles.buttonsetItemIcon(); if (!item->text().empty()) { - style = (item->icon() ? theme->styles.buttonsetItemTextTopIconBottom() : - theme->styles.buttonsetItemText()); + s = (item->icon() ? theme->styles.buttonsetItemTextTopIconBottom() : + theme->styles.buttonsetItemText()); } } - - item->setStyle(style); + item->setStyle(s); } ); addChildInCell(item, hspan, vspan, HORIZONTAL | VERTICAL); diff --git a/src/app/ui/button_set.h b/src/app/ui/button_set.h index 98f3786ed..a8a0ae002 100644 --- a/src/app/ui/button_set.h +++ b/src/app/ui/button_set.h @@ -44,12 +44,12 @@ namespace app { ButtonSet(int columns); - Item* addItem(const std::string& text, const char* styleId); - Item* addItem(const std::string& text, int hspan = 1, int vspan = 1, const char* styleId = nullptr); - Item* addItem(const skin::SkinPartPtr& icon, const char* styleId); - Item* addItem(const skin::SkinPartPtr& icon, int hspan = 1, int vspan = 1, const char* styleId = nullptr); - Item* addItem(Item* item, const char* styleId); - Item* addItem(Item* item, int hspan = 1, int vspan = 1, const char* styleId = nullptr); + Item* addItem(const std::string& text, ui::Style* style); + Item* addItem(const std::string& text, int hspan = 1, int vspan = 1, ui::Style* style = nullptr); + Item* addItem(const skin::SkinPartPtr& icon, ui::Style* style); + Item* addItem(const skin::SkinPartPtr& icon, int hspan = 1, int vspan = 1, ui::Style* style = nullptr); + Item* addItem(Item* item, ui::Style* style); + Item* addItem(Item* item, int hspan = 1, int vspan = 1, ui::Style* style = nullptr); Item* getItem(int index); int getItemIndex(const Item* item) const; diff --git a/src/app/ui/color_bar.cpp b/src/app/ui/color_bar.cpp index 99328a91d..ccd48578a 100644 --- a/src/app/ui/color_bar.cpp +++ b/src/app/ui/color_bar.cpp @@ -185,7 +185,7 @@ ColorBar::ColorBar(int align, TooltipManager* tooltipManager) m_instance = this; auto& pref = Preferences::instance(); - auto theme = SkinTheme::get(this); + auto* theme = SkinTheme::get(this); auto item = m_editPal.addItem(""); item->InitTheme.connect( @@ -195,9 +195,9 @@ ColorBar::ColorBar(int align, TooltipManager* tooltipManager) SkinTheme::instance()->styles.palEditLock()); item->setStyle(style); }); - m_buttons.addItem(theme->parts.palSort(), "pal_button"); - m_buttons.addItem(theme->parts.palPresets(), "pal_button"); - m_buttons.addItem(theme->parts.palOptions(), "pal_button"); + m_buttons.addItem(theme->parts.palSort(), theme->styles.palButton()); + m_buttons.addItem(theme->parts.palPresets(), theme->styles.palButton()); + m_buttons.addItem(theme->parts.palOptions(), theme->styles.palButton()); item = m_tilesButton.addItem(theme->parts.tiles()); item->InitTheme.connect( [this, item]() { @@ -213,9 +213,9 @@ ColorBar::ColorBar(int align, TooltipManager* tooltipManager) 1 == int(TilesetMode::Auto) && 2 == int(TilesetMode::Stack), "Tileset mode buttons doesn't match TilesetMode enum values"); - m_tilesetModeButtons.addItem(theme->parts.tilesManual(), "pal_button"); - m_tilesetModeButtons.addItem(theme->parts.tilesAuto(), "pal_button"); - m_tilesetModeButtons.addItem(theme->parts.tilesStack(), "pal_button"); + m_tilesetModeButtons.addItem(theme->parts.tilesManual(), theme->styles.palButton()); + m_tilesetModeButtons.addItem(theme->parts.tilesAuto(), theme->styles.palButton()); + m_tilesetModeButtons.addItem(theme->parts.tilesStack(), theme->styles.palButton()); m_tilesetMode = pref.colorBar.defaultTilesetMode(); setTilesetMode(m_tilesetMode); diff --git a/src/app/ui/context_bar.cpp b/src/app/ui/context_bar.cpp index d9f0a296a..a15ee240f 100644 --- a/src/app/ui/context_bar.cpp +++ b/src/app/ui/context_bar.cpp @@ -164,7 +164,8 @@ public: , m_brushes(App::instance()->brushes()) { SkinPartPtr part(new SkinPart); part->setBitmap(0, BrushPopup::createSurfaceForBrush(BrushRef(nullptr))); - addItem(part, "brush_type"); + auto* theme = SkinTheme::get(this); + addItem(part, theme->styles.brushType()); m_popupWindow.Open.connect( [this]{ @@ -385,8 +386,8 @@ protected: class ContextBar::PaintBucketSettingsField : public ButtonSet { public: PaintBucketSettingsField() : ButtonSet(1) { - auto theme = SkinTheme::get(this); - addItem(theme->parts.timelineGear(), "context_bar_button"); + auto* theme = SkinTheme::get(this); + addItem(theme->parts.timelineGear(), theme->styles.contextBarButton()); } protected: @@ -472,8 +473,8 @@ class ContextBar::InkTypeField : public ButtonSet { public: InkTypeField(ContextBar* owner) : ButtonSet(1) , m_owner(owner) { - auto theme = SkinTheme::get(this); - addItem(theme->parts.inkSimple(), "ink_type"); + auto* theme = SkinTheme::get(this); + addItem(theme->parts.inkSimple(), theme->styles.inkType()); } void setInkType(InkType inkType) { @@ -872,7 +873,8 @@ class ContextBar::PivotField : public ButtonSet { public: PivotField() : ButtonSet(1) { - addItem(SkinTheme::get(this)->parts.pivotCenter(), "pivot_field"); + auto* theme = SkinTheme::get(this); + addItem(SkinTheme::get(this)->parts.pivotCenter(), theme->styles.pivotField()); m_pivotConn = Preferences::instance().selection.pivotPosition.AfterChange.connect( [this]{ onPivotChange(); }); @@ -885,22 +887,22 @@ private: void onItemChange(Item* item) override { ButtonSet::onItemChange(item); - auto theme = SkinTheme::get(this); + auto* theme = SkinTheme::get(this); gfx::Rect bounds = this->bounds(); Menu menu; CheckBox visible(Strings::context_bar_default_display_pivot()); HBox box; ButtonSet buttonset(3); - buttonset.addItem(theme->parts.pivotNorthwest(), "pivot_dir"); - buttonset.addItem(theme->parts.pivotNorth(), "pivot_dir"); - buttonset.addItem(theme->parts.pivotNortheast(), "pivot_dir"); - buttonset.addItem(theme->parts.pivotWest(), "pivot_dir"); - buttonset.addItem(theme->parts.pivotCenter(), "pivot_dir"); - buttonset.addItem(theme->parts.pivotEast(), "pivot_dir"); - buttonset.addItem(theme->parts.pivotSouthwest(), "pivot_dir"); - buttonset.addItem(theme->parts.pivotSouth(), "pivot_dir"); - buttonset.addItem(theme->parts.pivotSoutheast(), "pivot_dir"); + buttonset.addItem(theme->parts.pivotNorthwest(), theme->styles.pivotDir()); + buttonset.addItem(theme->parts.pivotNorth(), theme->styles.pivotDir()); + buttonset.addItem(theme->parts.pivotNortheast(), theme->styles.pivotDir()); + buttonset.addItem(theme->parts.pivotWest(), theme->styles.pivotDir()); + buttonset.addItem(theme->parts.pivotCenter(), theme->styles.pivotDir()); + buttonset.addItem(theme->parts.pivotEast(), theme->styles.pivotDir()); + buttonset.addItem(theme->parts.pivotSouthwest(), theme->styles.pivotDir()); + buttonset.addItem(theme->parts.pivotSouth(), theme->styles.pivotDir()); + buttonset.addItem(theme->parts.pivotSoutheast(), theme->styles.pivotDir()); box.addChild(&buttonset); menu.addChild(&visible); @@ -1154,7 +1156,8 @@ public: DynamicsField(ContextBar* ctxBar) : ButtonSet(1) , m_ctxBar(ctxBar) { - addItem(SkinTheme::get(this)->parts.dynamics(), "dynamics_field"); + auto* theme = SkinTheme::get(this); + addItem(theme->parts.dynamics(), theme->styles.dynamicsField()); loadDynamicsPref(); initTheme(); @@ -1381,10 +1384,10 @@ protected: class ContextBar::GradientTypeField : public ButtonSet { public: GradientTypeField() : ButtonSet(2) { - auto theme = SkinTheme::get(this); + auto* theme = SkinTheme::get(this); - addItem(theme->parts.linearGradient(), "context_bar_button"); - addItem(theme->parts.radialGradient(), "context_bar_button"); + addItem(theme->parts.linearGradient(), theme->styles.contextBarButton()); + addItem(theme->parts.radialGradient(), theme->styles.contextBarButton()); setSelectedItem(0); } @@ -1404,10 +1407,10 @@ public: class ContextBar::DropPixelsField : public ButtonSet { public: DropPixelsField() : ButtonSet(2) { - auto theme = SkinTheme::get(this); + auto* theme = SkinTheme::get(this); - addItem(theme->parts.dropPixelsOk(), "context_bar_button"); - addItem(theme->parts.dropPixelsCancel(), "context_bar_button"); + addItem(theme->parts.dropPixelsOk(), theme->styles.contextBarButton()); + addItem(theme->parts.dropPixelsCancel(), theme->styles.contextBarButton()); setOfferCapture(false); } @@ -1529,10 +1532,10 @@ class ContextBar::SymmetryField : public ButtonSet { public: SymmetryField() : ButtonSet(3) { setMultiMode(MultiMode::Set); - auto theme = SkinTheme::get(this); - addItem(theme->parts.horizontalSymmetry(), "symmetry_field"); - addItem(theme->parts.verticalSymmetry(), "symmetry_field"); - addItem("...", "symmetry_options"); + auto* theme = SkinTheme::get(this); + addItem(theme->parts.horizontalSymmetry(), theme->styles.symmetryField()); + addItem(theme->parts.verticalSymmetry(), theme->styles.symmetryField()); + addItem("...", theme->styles.symmetryOptions()); } void setupTooltips(TooltipManager* tooltipManager) { @@ -1654,7 +1657,7 @@ public: , m_combobox(this) , m_action(2) { - auto theme = SkinTheme::get(this); + auto* theme = SkinTheme::get(this); m_sel.addItem(Strings::context_bar_all()); m_sel.addItem(Strings::context_bar_none()); @@ -1667,8 +1670,8 @@ public: m_combobox.setExpansive(true); m_combobox.setMinSize(gfx::Size(256*guiscale(), 0)); - m_action.addItem(theme->parts.iconUserData(), "buttonset_item_icon_mono"); - m_action.addItem(theme->parts.iconClose(), "buttonset_item_icon_mono"); + m_action.addItem(theme->parts.iconUserData(), theme->styles.buttonsetItemIconMono()); + m_action.addItem(theme->parts.iconClose(), theme->styles.buttonsetItemIconMono()); m_action.ItemChange.connect( [this](ButtonSet::Item* item){ onAction(m_action.selectedItem()); diff --git a/src/app/ui/selection_mode_field.cpp b/src/app/ui/selection_mode_field.cpp index c476d5611..8ef32d0c7 100644 --- a/src/app/ui/selection_mode_field.cpp +++ b/src/app/ui/selection_mode_field.cpp @@ -23,12 +23,12 @@ using namespace ui; SelectionModeField::SelectionModeField() : ButtonSet(4) { - auto theme = SkinTheme::get(this); + auto* theme = SkinTheme::get(this); - addItem(theme->parts.selectionReplace(), "selection_mode"); - addItem(theme->parts.selectionAdd(), "selection_mode"); - addItem(theme->parts.selectionSubtract(), "selection_mode"); - addItem(theme->parts.selectionIntersect(), "selection_mode"); + addItem(theme->parts.selectionReplace(), theme->styles.selectionMode()); + addItem(theme->parts.selectionAdd(), theme->styles.selectionMode()); + addItem(theme->parts.selectionSubtract(), theme->styles.selectionMode()); + addItem(theme->parts.selectionIntersect(), theme->styles.selectionMode()); setSelectedItem((int)Preferences::instance().selection.mode()); initTheme(); diff --git a/src/app/ui/timeline/ani_controls.cpp b/src/app/ui/timeline/ani_controls.cpp index 4ee51431d..0e0bd9738 100644 --- a/src/app/ui/timeline/ani_controls.cpp +++ b/src/app/ui/timeline/ani_controls.cpp @@ -42,13 +42,13 @@ enum AniAction { AniControls::AniControls(TooltipManager* tooltipManager) : ButtonSet(5) { - auto theme = SkinTheme::get(this); + auto* theme = SkinTheme::get(this); - addItem(theme->parts.aniFirst(), "ani_button"); - addItem(theme->parts.aniPrevious(), "ani_button"); - addItem(theme->parts.aniPlay(), "ani_button"); - addItem(theme->parts.aniNext(), "ani_button"); - addItem(theme->parts.aniLast(), "ani_button"); + addItem(theme->parts.aniFirst(), theme->styles.aniButton()); + addItem(theme->parts.aniPrevious(), theme->styles.aniButton()); + addItem(theme->parts.aniPlay(), theme->styles.aniButton()); + addItem(theme->parts.aniNext(), theme->styles.aniButton()); + addItem(theme->parts.aniLast(), theme->styles.aniButton()); ItemChange.connect([this]{ onClickButton(); }); setTriggerOnMouseUp(true);