mirror of
https://github.com/aseprite/aseprite.git
synced 2025-01-29 21:33:12 +00:00
[lua] Fix tabs ID handling (fix #4268)
This commit is contained in:
parent
9ef6c4848d
commit
f7f2c56abf
@ -1,5 +1,5 @@
|
||||
// Aseprite
|
||||
// Copyright (C) 2018-2023 Igara Studio S.A.
|
||||
// Copyright (C) 2018-2024 Igara Studio S.A.
|
||||
// Copyright (C) 2018 David Capello
|
||||
//
|
||||
// This program is distributed under the terms of
|
||||
@ -1336,27 +1336,22 @@ int Dialog_tab(lua_State* L)
|
||||
dlg->wipTab = new app::script::Tabs(ui::CENTER);
|
||||
}
|
||||
|
||||
auto tabContent = new ui::Grid(2, false);
|
||||
tabContent->setExpansive(true);
|
||||
tabContent->setVisible(false);
|
||||
tabContent->setText(text);
|
||||
tabContent->setId(id.c_str());
|
||||
auto tabBtn = dlg->wipTab->addTab(tabContent);
|
||||
dlg->currentGrid = tabContent;
|
||||
auto tab = dlg->wipTab->addTab(id, text);
|
||||
dlg->currentGrid = tab->content();
|
||||
|
||||
if (hasId) dlg->dataWidgets[id] = tabBtn;
|
||||
if (hasId) dlg->dataWidgets[id] = tab;
|
||||
|
||||
if (lua_istable(L, 2)) {
|
||||
int type = lua_getfield(L, 2, "onclick");
|
||||
if (type == LUA_TFUNCTION) {
|
||||
Dialog_connect_signal(L, 1, tabBtn->Click,
|
||||
Dialog_connect_signal(L, 1, tab->Click,
|
||||
[id](lua_State* L){
|
||||
lua_pushstring(L, id.c_str());
|
||||
lua_setfield(L, -2, "tab");
|
||||
});
|
||||
}
|
||||
|
||||
set_widget_flags(L, 2, tabBtn);
|
||||
set_widget_flags(L, 2, tab);
|
||||
}
|
||||
|
||||
lua_pushvalue(L, 1);
|
||||
|
@ -1,10 +1,13 @@
|
||||
// Aseprite
|
||||
// Copyright (c) 2022-2023 Igara Studio S.A.
|
||||
// Copyright (c) 2022-2024 Igara Studio S.A.
|
||||
//
|
||||
// This program is distributed under the terms of
|
||||
// the End-User License Agreement for Aseprite.
|
||||
|
||||
#include "app/script/tabs_widget.h"
|
||||
#include "tabs_widget.h"
|
||||
|
||||
#define TAB_CONTENT_ID(tabid) (tabid + "_content")
|
||||
|
||||
#ifdef ENABLE_UI
|
||||
|
||||
@ -31,18 +34,22 @@ Tabs::Tabs(int selectorFlags) : m_selectorFlags(selectorFlags)
|
||||
layoutChilds();
|
||||
}
|
||||
|
||||
Tab* Tabs::addTab(Grid* content)
|
||||
Tab* Tabs::addTab(const std::string& id, const std::string& text)
|
||||
{
|
||||
auto content = new ui::Grid(2, false);
|
||||
content->setExpansive(true);
|
||||
content->setVisible(false);
|
||||
content->setId(TAB_CONTENT_ID(id).c_str());
|
||||
m_pages.addChild(content);
|
||||
|
||||
if (m_buttons.children().empty()) {
|
||||
m_buttonsBox.addChild(&m_buttons);
|
||||
m_buttons.ItemChange.connect([this](ButtonSet::Item* selItem) {
|
||||
m_buttons.ItemChange.connect([this](ButtonSet::Item* tab) {
|
||||
int oldSelectedTabIndex = m_selectedTab;
|
||||
for (int i=0; i<m_pages.children().size(); ++i) {
|
||||
auto tab = m_pages.children()[i];
|
||||
bool isSelectedTab = selItem->text() == tab->text();
|
||||
tab->setVisible(isSelectedTab);
|
||||
auto tabContent = m_pages.children()[i];
|
||||
bool isSelectedTab = (TAB_CONTENT_ID(tab->id()) == tabContent->id());
|
||||
tabContent->setVisible(isSelectedTab);
|
||||
if (isSelectedTab)
|
||||
m_selectedTab = i;
|
||||
}
|
||||
@ -55,8 +62,9 @@ Tab* Tabs::addTab(Grid* content)
|
||||
else {
|
||||
m_buttons.setColumns(m_pages.children().size());
|
||||
}
|
||||
auto tab = new Tab();
|
||||
tab->setText(content->text());
|
||||
auto tab = new Tab(content);
|
||||
tab->setText(text);
|
||||
tab->setId(id.c_str());
|
||||
m_buttons.addItem(tab);
|
||||
// Select the first tab by default.
|
||||
if (m_buttons.children().size() == 1) {
|
||||
@ -89,8 +97,8 @@ void Tabs::setSelectorFlags(int selectorFlags)
|
||||
|
||||
int Tabs::tabIndexById(const std::string& id) const
|
||||
{
|
||||
for (int i=0; i<m_pages.children().size(); ++i) {
|
||||
if (m_pages.children()[i]->id() == id)
|
||||
for (int i=0; i<m_buttons.children().size(); ++i) {
|
||||
if (m_buttons.children()[i]->id() == id)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
@ -98,8 +106,8 @@ int Tabs::tabIndexById(const std::string& id) const
|
||||
|
||||
int Tabs::tabIndexByText(const std::string& text) const
|
||||
{
|
||||
for (int i=0; i<m_pages.children().size(); ++i) {
|
||||
if (m_pages.children()[i]->text() == text)
|
||||
for (int i=0; i<m_buttons.children().size(); ++i) {
|
||||
if (m_buttons.children()[i]->text() == text)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
@ -107,15 +115,15 @@ int Tabs::tabIndexByText(const std::string& text) const
|
||||
|
||||
std::string Tabs::tabId(int index) const
|
||||
{
|
||||
return index >= 0 && index < m_pages.children().size() ?
|
||||
m_pages.children()[index]->id() :
|
||||
return index >= 0 && index < m_buttons.children().size() ?
|
||||
m_buttons.children()[index]->id() :
|
||||
std::string();
|
||||
}
|
||||
|
||||
std::string Tabs::tabText(int index) const
|
||||
{
|
||||
return index >= 0 && index < m_pages.children().size() ?
|
||||
m_pages.children()[index]->text() :
|
||||
return index >= 0 && index < m_buttons.children().size() ?
|
||||
m_buttons.children()[index]->text() :
|
||||
std::string();
|
||||
}
|
||||
|
||||
@ -171,6 +179,9 @@ void Pages::onSizeHint(ui::SizeHintEvent& ev)
|
||||
ev.setSizeHint(prefSize);
|
||||
}
|
||||
|
||||
Tab::Tab(ui::Grid* content) : m_content(content)
|
||||
{
|
||||
}
|
||||
|
||||
void Tab::onClick()
|
||||
{
|
||||
|
@ -1,5 +1,5 @@
|
||||
// Aseprite
|
||||
// Copyright (C) 2023 Igara Studio S.A.
|
||||
// Copyright (C) 2023-2024 Igara Studio S.A.
|
||||
//
|
||||
// This program is distributed under the terms of
|
||||
// the End-User License Agreement for Aseprite.
|
||||
@ -20,9 +20,16 @@ namespace script {
|
||||
|
||||
class Tab : public app::ButtonSet::Item {
|
||||
public:
|
||||
Tab(ui::Grid* content);
|
||||
ui::Grid* content() { return m_content; }
|
||||
|
||||
obs::signal<void()> Click;
|
||||
|
||||
protected:
|
||||
virtual void onClick();
|
||||
|
||||
private:
|
||||
ui::Grid* m_content;
|
||||
};
|
||||
|
||||
class Pages : public ui::VBox {
|
||||
@ -37,7 +44,7 @@ public:
|
||||
|
||||
Tabs(int selectorFlags);
|
||||
|
||||
Tab* addTab(ui::Grid* content);
|
||||
Tab* addTab(const std::string& id, const std::string& text);
|
||||
void selectTab(int index);
|
||||
void setSelectorFlags(int selectorFlags);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user