Use ui::Style* instead of strings in ButtonSet::addItem() (fix #4188)

This commit is contained in:
Martín Capello 2024-05-06 17:00:27 -03:00 committed by David Capello
parent 611713e5fc
commit f4b8effd52
7 changed files with 81 additions and 86 deletions

View File

@ -361,9 +361,10 @@ BrushPopup::BrushPopup()
m_box.addChild(new Separator("", HORIZONTAL)); m_box.addChild(new Separator("", HORIZONTAL));
for (const auto& brush : brushes.getStandardBrushes()) { for (const auto& brush : brushes.getStandardBrushes()) {
auto* theme = SkinTheme::get(this);
m_standardBrushes.addItem( m_standardBrushes.addItem(
new SelectBrushItem( new SelectBrushItem(
BrushSlot(BrushSlot::Flags::BrushType, brush)), "standard_brush"); BrushSlot(BrushSlot::Flags::BrushType, brush)), theme->styles.standardBrush());
} }
m_standardBrushes.setTransparent(true); m_standardBrushes.setTransparent(true);
@ -398,6 +399,7 @@ void BrushPopup::setBrush(Brush* brush)
void BrushPopup::regenerate(ui::Display* display, void BrushPopup::regenerate(ui::Display* display,
const gfx::Point& pos) const gfx::Point& pos)
{ {
auto* theme = SkinTheme::get(this);
auto& brushSlots = App::instance()->brushes().getBrushSlots(); auto& brushSlots = App::instance()->brushes().getBrushSlots();
if (m_customBrushes) { if (m_customBrushes) {
@ -428,11 +430,11 @@ void BrushPopup::regenerate(ui::Display* display,
} }
m_customBrushes->addItem(new SelectBrushItem(brush, slot)); m_customBrushes->addItem(new SelectBrushItem(brush, slot));
m_customBrushes->addItem(new BrushShortcutItem(shortcut, 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 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->setExpansive(true);
m_customBrushes->initTheme(); m_customBrushes->initTheme();
m_box.addChild(m_customBrushes); m_box.addChild(m_customBrushes);

View File

@ -208,60 +208,50 @@ ButtonSet::ButtonSet(int columns)
initTheme(); 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* item = new Item();
item->setText(text); item->setText(text);
addItem(item, hspan, vspan, styleId); addItem(item, hspan, vspan, style);
return item; 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* item = new Item();
item->setIcon(icon); item->setIcon(icon);
addItem(item, hspan, vspan, styleId); addItem(item, hspan, vspan, style);
return item; 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->InitTheme.connect(
[item, styleId] { [item, style] {
auto theme = SkinTheme::get(item); ui::Style* s = style;
ui::Style* style; if (!s) {
if (!styleId.empty()) { auto* theme = SkinTheme::get(item);
style = theme->getStyleById(styleId); s = theme->styles.buttonsetItemIcon();
if (!style)
throw base::Exception(fmt::format("Style {} not found", styleId));
}
else {
style = theme->styles.buttonsetItemIcon();
if (!item->text().empty()) { if (!item->text().empty()) {
style = (item->icon() ? theme->styles.buttonsetItemTextTopIconBottom() : s = (item->icon() ? theme->styles.buttonsetItemTextTopIconBottom() :
theme->styles.buttonsetItemText()); theme->styles.buttonsetItemText());
} }
} }
item->setStyle(s);
item->setStyle(style);
} }
); );
addChildInCell(item, hspan, vspan, HORIZONTAL | VERTICAL); addChildInCell(item, hspan, vspan, HORIZONTAL | VERTICAL);

View File

@ -44,12 +44,12 @@ namespace app {
ButtonSet(int columns); ButtonSet(int columns);
Item* addItem(const std::string& text, const char* styleId); Item* addItem(const std::string& text, ui::Style* style);
Item* addItem(const std::string& text, int hspan = 1, int vspan = 1, const char* styleId = nullptr); Item* addItem(const std::string& text, int hspan = 1, int vspan = 1, ui::Style* style = nullptr);
Item* addItem(const skin::SkinPartPtr& icon, const char* styleId); Item* addItem(const skin::SkinPartPtr& icon, ui::Style* style);
Item* addItem(const skin::SkinPartPtr& icon, int hspan = 1, int vspan = 1, const char* styleId = nullptr); Item* addItem(const skin::SkinPartPtr& icon, int hspan = 1, int vspan = 1, ui::Style* style = nullptr);
Item* addItem(Item* item, const char* styleId); Item* addItem(Item* item, ui::Style* style);
Item* addItem(Item* item, int hspan = 1, int vspan = 1, const char* styleId = nullptr); Item* addItem(Item* item, int hspan = 1, int vspan = 1, ui::Style* style = nullptr);
Item* getItem(int index); Item* getItem(int index);
int getItemIndex(const Item* item) const; int getItemIndex(const Item* item) const;

View File

@ -185,7 +185,7 @@ ColorBar::ColorBar(int align, TooltipManager* tooltipManager)
m_instance = this; m_instance = this;
auto& pref = Preferences::instance(); auto& pref = Preferences::instance();
auto theme = SkinTheme::get(this); auto* theme = SkinTheme::get(this);
auto item = m_editPal.addItem(""); auto item = m_editPal.addItem("");
item->InitTheme.connect( item->InitTheme.connect(
@ -195,9 +195,9 @@ ColorBar::ColorBar(int align, TooltipManager* tooltipManager)
SkinTheme::instance()->styles.palEditLock()); SkinTheme::instance()->styles.palEditLock());
item->setStyle(style); item->setStyle(style);
}); });
m_buttons.addItem(theme->parts.palSort(), "pal_button"); m_buttons.addItem(theme->parts.palSort(), theme->styles.palButton());
m_buttons.addItem(theme->parts.palPresets(), "pal_button"); m_buttons.addItem(theme->parts.palPresets(), theme->styles.palButton());
m_buttons.addItem(theme->parts.palOptions(), "pal_button"); m_buttons.addItem(theme->parts.palOptions(), theme->styles.palButton());
item = m_tilesButton.addItem(theme->parts.tiles()); item = m_tilesButton.addItem(theme->parts.tiles());
item->InitTheme.connect( item->InitTheme.connect(
[this, item]() { [this, item]() {
@ -213,9 +213,9 @@ ColorBar::ColorBar(int align, TooltipManager* tooltipManager)
1 == int(TilesetMode::Auto) && 1 == int(TilesetMode::Auto) &&
2 == int(TilesetMode::Stack), "Tileset mode buttons doesn't match TilesetMode enum values"); 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.tilesManual(), theme->styles.palButton());
m_tilesetModeButtons.addItem(theme->parts.tilesAuto(), "pal_button"); m_tilesetModeButtons.addItem(theme->parts.tilesAuto(), theme->styles.palButton());
m_tilesetModeButtons.addItem(theme->parts.tilesStack(), "pal_button"); m_tilesetModeButtons.addItem(theme->parts.tilesStack(), theme->styles.palButton());
m_tilesetMode = pref.colorBar.defaultTilesetMode(); m_tilesetMode = pref.colorBar.defaultTilesetMode();
setTilesetMode(m_tilesetMode); setTilesetMode(m_tilesetMode);

View File

@ -164,7 +164,8 @@ public:
, m_brushes(App::instance()->brushes()) { , m_brushes(App::instance()->brushes()) {
SkinPartPtr part(new SkinPart); SkinPartPtr part(new SkinPart);
part->setBitmap(0, BrushPopup::createSurfaceForBrush(BrushRef(nullptr))); 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( m_popupWindow.Open.connect(
[this]{ [this]{
@ -385,8 +386,8 @@ protected:
class ContextBar::PaintBucketSettingsField : public ButtonSet { class ContextBar::PaintBucketSettingsField : public ButtonSet {
public: public:
PaintBucketSettingsField() : ButtonSet(1) { PaintBucketSettingsField() : ButtonSet(1) {
auto theme = SkinTheme::get(this); auto* theme = SkinTheme::get(this);
addItem(theme->parts.timelineGear(), "context_bar_button"); addItem(theme->parts.timelineGear(), theme->styles.contextBarButton());
} }
protected: protected:
@ -472,8 +473,8 @@ class ContextBar::InkTypeField : public ButtonSet {
public: public:
InkTypeField(ContextBar* owner) : ButtonSet(1) InkTypeField(ContextBar* owner) : ButtonSet(1)
, m_owner(owner) { , m_owner(owner) {
auto theme = SkinTheme::get(this); auto* theme = SkinTheme::get(this);
addItem(theme->parts.inkSimple(), "ink_type"); addItem(theme->parts.inkSimple(), theme->styles.inkType());
} }
void setInkType(InkType inkType) { void setInkType(InkType inkType) {
@ -872,7 +873,8 @@ class ContextBar::PivotField : public ButtonSet {
public: public:
PivotField() PivotField()
: ButtonSet(1) { : 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( m_pivotConn = Preferences::instance().selection.pivotPosition.AfterChange.connect(
[this]{ onPivotChange(); }); [this]{ onPivotChange(); });
@ -885,22 +887,22 @@ private:
void onItemChange(Item* item) override { void onItemChange(Item* item) override {
ButtonSet::onItemChange(item); ButtonSet::onItemChange(item);
auto theme = SkinTheme::get(this); auto* theme = SkinTheme::get(this);
gfx::Rect bounds = this->bounds(); gfx::Rect bounds = this->bounds();
Menu menu; Menu menu;
CheckBox visible(Strings::context_bar_default_display_pivot()); CheckBox visible(Strings::context_bar_default_display_pivot());
HBox box; HBox box;
ButtonSet buttonset(3); ButtonSet buttonset(3);
buttonset.addItem(theme->parts.pivotNorthwest(), "pivot_dir"); buttonset.addItem(theme->parts.pivotNorthwest(), theme->styles.pivotDir());
buttonset.addItem(theme->parts.pivotNorth(), "pivot_dir"); buttonset.addItem(theme->parts.pivotNorth(), theme->styles.pivotDir());
buttonset.addItem(theme->parts.pivotNortheast(), "pivot_dir"); buttonset.addItem(theme->parts.pivotNortheast(), theme->styles.pivotDir());
buttonset.addItem(theme->parts.pivotWest(), "pivot_dir"); buttonset.addItem(theme->parts.pivotWest(), theme->styles.pivotDir());
buttonset.addItem(theme->parts.pivotCenter(), "pivot_dir"); buttonset.addItem(theme->parts.pivotCenter(), theme->styles.pivotDir());
buttonset.addItem(theme->parts.pivotEast(), "pivot_dir"); buttonset.addItem(theme->parts.pivotEast(), theme->styles.pivotDir());
buttonset.addItem(theme->parts.pivotSouthwest(), "pivot_dir"); buttonset.addItem(theme->parts.pivotSouthwest(), theme->styles.pivotDir());
buttonset.addItem(theme->parts.pivotSouth(), "pivot_dir"); buttonset.addItem(theme->parts.pivotSouth(), theme->styles.pivotDir());
buttonset.addItem(theme->parts.pivotSoutheast(), "pivot_dir"); buttonset.addItem(theme->parts.pivotSoutheast(), theme->styles.pivotDir());
box.addChild(&buttonset); box.addChild(&buttonset);
menu.addChild(&visible); menu.addChild(&visible);
@ -1154,7 +1156,8 @@ public:
DynamicsField(ContextBar* ctxBar) DynamicsField(ContextBar* ctxBar)
: ButtonSet(1) : ButtonSet(1)
, m_ctxBar(ctxBar) { , m_ctxBar(ctxBar) {
addItem(SkinTheme::get(this)->parts.dynamics(), "dynamics_field"); auto* theme = SkinTheme::get(this);
addItem(theme->parts.dynamics(), theme->styles.dynamicsField());
loadDynamicsPref(); loadDynamicsPref();
initTheme(); initTheme();
@ -1381,10 +1384,10 @@ protected:
class ContextBar::GradientTypeField : public ButtonSet { class ContextBar::GradientTypeField : public ButtonSet {
public: public:
GradientTypeField() : ButtonSet(2) { GradientTypeField() : ButtonSet(2) {
auto theme = SkinTheme::get(this); auto* theme = SkinTheme::get(this);
addItem(theme->parts.linearGradient(), "context_bar_button"); addItem(theme->parts.linearGradient(), theme->styles.contextBarButton());
addItem(theme->parts.radialGradient(), "context_bar_button"); addItem(theme->parts.radialGradient(), theme->styles.contextBarButton());
setSelectedItem(0); setSelectedItem(0);
} }
@ -1404,10 +1407,10 @@ public:
class ContextBar::DropPixelsField : public ButtonSet { class ContextBar::DropPixelsField : public ButtonSet {
public: public:
DropPixelsField() : ButtonSet(2) { DropPixelsField() : ButtonSet(2) {
auto theme = SkinTheme::get(this); auto* theme = SkinTheme::get(this);
addItem(theme->parts.dropPixelsOk(), "context_bar_button"); addItem(theme->parts.dropPixelsOk(), theme->styles.contextBarButton());
addItem(theme->parts.dropPixelsCancel(), "context_bar_button"); addItem(theme->parts.dropPixelsCancel(), theme->styles.contextBarButton());
setOfferCapture(false); setOfferCapture(false);
} }
@ -1529,10 +1532,10 @@ class ContextBar::SymmetryField : public ButtonSet {
public: public:
SymmetryField() : ButtonSet(3) { SymmetryField() : ButtonSet(3) {
setMultiMode(MultiMode::Set); setMultiMode(MultiMode::Set);
auto theme = SkinTheme::get(this); auto* theme = SkinTheme::get(this);
addItem(theme->parts.horizontalSymmetry(), "symmetry_field"); addItem(theme->parts.horizontalSymmetry(), theme->styles.symmetryField());
addItem(theme->parts.verticalSymmetry(), "symmetry_field"); addItem(theme->parts.verticalSymmetry(), theme->styles.symmetryField());
addItem("...", "symmetry_options"); addItem("...", theme->styles.symmetryOptions());
} }
void setupTooltips(TooltipManager* tooltipManager) { void setupTooltips(TooltipManager* tooltipManager) {
@ -1654,7 +1657,7 @@ public:
, m_combobox(this) , m_combobox(this)
, m_action(2) , 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_all());
m_sel.addItem(Strings::context_bar_none()); m_sel.addItem(Strings::context_bar_none());
@ -1667,8 +1670,8 @@ public:
m_combobox.setExpansive(true); m_combobox.setExpansive(true);
m_combobox.setMinSize(gfx::Size(256*guiscale(), 0)); m_combobox.setMinSize(gfx::Size(256*guiscale(), 0));
m_action.addItem(theme->parts.iconUserData(), "buttonset_item_icon_mono"); m_action.addItem(theme->parts.iconUserData(), theme->styles.buttonsetItemIconMono());
m_action.addItem(theme->parts.iconClose(), "buttonset_item_icon_mono"); m_action.addItem(theme->parts.iconClose(), theme->styles.buttonsetItemIconMono());
m_action.ItemChange.connect( m_action.ItemChange.connect(
[this](ButtonSet::Item* item){ [this](ButtonSet::Item* item){
onAction(m_action.selectedItem()); onAction(m_action.selectedItem());

View File

@ -23,12 +23,12 @@ using namespace ui;
SelectionModeField::SelectionModeField() SelectionModeField::SelectionModeField()
: ButtonSet(4) : ButtonSet(4)
{ {
auto theme = SkinTheme::get(this); auto* theme = SkinTheme::get(this);
addItem(theme->parts.selectionReplace(), "selection_mode"); addItem(theme->parts.selectionReplace(), theme->styles.selectionMode());
addItem(theme->parts.selectionAdd(), "selection_mode"); addItem(theme->parts.selectionAdd(), theme->styles.selectionMode());
addItem(theme->parts.selectionSubtract(), "selection_mode"); addItem(theme->parts.selectionSubtract(), theme->styles.selectionMode());
addItem(theme->parts.selectionIntersect(), "selection_mode"); addItem(theme->parts.selectionIntersect(), theme->styles.selectionMode());
setSelectedItem((int)Preferences::instance().selection.mode()); setSelectedItem((int)Preferences::instance().selection.mode());
initTheme(); initTheme();

View File

@ -42,13 +42,13 @@ enum AniAction {
AniControls::AniControls(TooltipManager* tooltipManager) AniControls::AniControls(TooltipManager* tooltipManager)
: ButtonSet(5) : ButtonSet(5)
{ {
auto theme = SkinTheme::get(this); auto* theme = SkinTheme::get(this);
addItem(theme->parts.aniFirst(), "ani_button"); addItem(theme->parts.aniFirst(), theme->styles.aniButton());
addItem(theme->parts.aniPrevious(), "ani_button"); addItem(theme->parts.aniPrevious(), theme->styles.aniButton());
addItem(theme->parts.aniPlay(), "ani_button"); addItem(theme->parts.aniPlay(), theme->styles.aniButton());
addItem(theme->parts.aniNext(), "ani_button"); addItem(theme->parts.aniNext(), theme->styles.aniButton());
addItem(theme->parts.aniLast(), "ani_button"); addItem(theme->parts.aniLast(), theme->styles.aniButton());
ItemChange.connect([this]{ onClickButton(); }); ItemChange.connect([this]{ onClickButton(); });
setTriggerOnMouseUp(true); setTriggerOnMouseUp(true);