[lua] Create app.window to access new main window properties (#3927)

We moved the app.width/heigth to app.window.width/height and
app.events.on('resize', ...) to app.window.events.on('resize', ...).
This commit is contained in:
David Capello 2023-07-18 17:54:57 -03:00
parent 7ee866643b
commit a2d8a080f5
8 changed files with 155 additions and 32 deletions

View File

@ -217,6 +217,7 @@ if(ENABLE_SCRIPTING)
script/uuid_class.cpp
script/values.cpp
script/version_class.cpp
script/window_class.cpp
shell.cpp
${scripting_files_ws}
${scripting_files_ui})

View File

@ -576,6 +576,8 @@ void App::close()
{
#ifdef ENABLE_UI
if (isGui()) {
ExitGui();
// Select no document
static_cast<UIContext*>(context())->setActiveView(nullptr);

View File

@ -123,6 +123,7 @@ namespace app {
// App Signals
obs::signal<void()> Exit;
obs::signal<void()> ExitGui;
obs::signal<void()> PaletteChange;
obs::signal<void()> ColorSpaceChange;
obs::signal<void()> PalettePresetsChange;

View File

@ -701,25 +701,18 @@ int App_get_defaultPalette(lua_State* L)
return 1;
}
int App_get_width(lua_State* L)
int App_get_window(lua_State* L)
{
#if ENABLE_UI
App* app = App::instance();
lua_pushinteger(L, app->mainWindow()->bounds().w);
return 1;
if (app && app->mainWindow()) {
push_ptr(L, (ui::Window*)app->mainWindow());
}
else
#endif
lua_pushinteger(L, 0);
return 1;
}
int App_get_height(lua_State* L)
{
#if ENABLE_UI
App* app = App::instance();
lua_pushinteger(L, app->mainWindow()->bounds().h);
return 1;
#endif
lua_pushinteger(L, 0);
{
lua_pushnil(L);
}
return 1;
}
@ -837,8 +830,7 @@ const Property App_properties[] = {
{ "range", App_get_range, nullptr },
{ "isUIAvailable", App_get_isUIAvailable, nullptr },
{ "defaultPalette", App_get_defaultPalette, App_set_defaultPalette },
{ "width", App_get_width, nullptr },
{ "height", App_get_height, nullptr },
{ "window", App_get_window, nullptr },
{ "events", App_get_events, nullptr },
{ "theme", App_get_theme, nullptr },
{ "uiScale", App_get_uiScale, nullptr },

View File

@ -165,6 +165,7 @@ void register_color_space_class(lua_State* L);
void register_dialog_class(lua_State* L);
void register_editor_class(lua_State* L);
void register_graphics_context_class(lua_State* L);
void register_window_class(lua_State* L);
#endif
void register_events_class(lua_State* L);
void register_frame_class(lua_State* L);
@ -469,6 +470,7 @@ Engine::Engine()
register_dialog_class(L);
register_editor_class(L);
register_graphics_context_class(L);
register_window_class(L);
#endif
register_events_class(L);
register_frame_class(L);

View File

@ -54,6 +54,10 @@ namespace doc {
class WithUserData;
}
namespace ui {
class Window;
}
namespace app {
class Editor;
@ -176,6 +180,9 @@ namespace app {
void push_tilesets(lua_State* L, doc::Tilesets* tilesets);
void push_tool(lua_State* L, app::tools::Tool* tool);
void push_version(lua_State* L, const base::Version& ver);
#ifdef ENABLE_UI
void push_window_events(lua_State* L, ui::Window* window);
#endif
gfx::Point convert_args_into_point(lua_State* L, int index);
gfx::Rect convert_args_into_rect(lua_State* L, int index);

View File

@ -49,8 +49,10 @@ namespace {
using EventListener = int;
class AppEvents;
class WindowEvents;
class SpriteEvents;
static std::unique_ptr<AppEvents> g_appEvents;
static std::unique_ptr<WindowEvents> g_windowEvents;
static std::map<doc::ObjectId, std::unique_ptr<SpriteEvents>> g_spriteEvents;
class Events {
@ -160,7 +162,6 @@ public:
BgColorChange,
BeforeCommand,
AfterCommand,
Resize,
};
AppEvents() {
@ -177,8 +178,6 @@ public:
return BeforeCommand;
else if (std::strcmp(eventName, "aftercommand") == 0)
return AfterCommand;
else if (std::strcmp(eventName, "resize") == 0)
return Resize;
else
return Unknown;
}
@ -209,10 +208,6 @@ private:
m_afterCmdConn = ctx->AfterCommandExecution
.connect(&AppEvents::onAfterCommand, this);
break;
case Resize:
m_resizeConn = app->mainWindow()->Resize
.connect(&AppEvents::onResize, this);
break;
}
}
@ -233,9 +228,6 @@ private:
case AfterCommand:
m_afterCmdConn.disconnect();
break;
case Resize:
m_resizeConn.disconnect();
break;
}
}
@ -266,11 +258,6 @@ private:
{ "params", ev.params() } });
}
void onResize(ui::ResizeEvent& ev) {
call(Resize, { { "width", ev.bounds().w },
{ "height", ev.bounds().h } });
}
// ContextObserver impl
void onActiveSiteChange(const Site& site) override {
const bool fromUndo = (site.document() &&
@ -283,6 +270,53 @@ private:
obs::scoped_connection m_beforeCmdConn;
obs::scoped_connection m_afterCmdConn;
obs::scoped_connection m_beforePaintConn;
};
class WindowEvents : public Events
, private ContextObserver {
public:
enum : EventType {
Unknown = -1,
Resize,
};
WindowEvents(ui::Window* window)
: m_window(window) {
}
ui::Window* window() const { return m_window; }
EventType eventType(const char* eventName) const override {
if (std::strcmp(eventName, "resize") == 0)
return Resize;
else
return Unknown;
}
private:
void onAddFirstListener(EventType eventType) override {
switch (eventType) {
case Resize:
m_resizeConn = m_window->Resize.connect(&WindowEvents::onResize, this);
break;
}
}
void onRemoveLastListener(EventType eventType) override {
switch (eventType) {
case Resize:
m_resizeConn.disconnect();
break;
}
}
void onResize(ui::ResizeEvent& ev) {
call(Resize, { { "width", ev.bounds().w },
{ "height", ev.bounds().h } });
}
ui::Window* m_window;
obs::scoped_connection m_resizeConn;
};
@ -537,5 +571,21 @@ void push_sprite_events(lua_State* L, Sprite* sprite)
push_ptr<Events>(L, spriteEvents);
}
#ifdef ENABLE_UI
void push_window_events(lua_State* L, ui::Window* window)
{
if (!g_windowEvents) {
App::instance()->ExitGui.connect([]{ g_windowEvents.reset(); });
g_windowEvents = std::make_unique<WindowEvents>(window);
}
else {
ASSERT(g_windowEvents->window() == window);
}
push_ptr<Events>(L, g_windowEvents.get());
}
#endif // ENABLE_UI
} // namespace script
} // namespace app

View File

@ -0,0 +1,68 @@
// Aseprite
// Copyright (C) 2023 Igara Studio S.A.
//
// This program is distributed under the terms of
// the End-User License Agreement for Aseprite.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "app/script/engine.h"
#include "app/script/luacpp.h"
#include "ui/window.h"
#ifdef ENABLE_UI
namespace app {
namespace script {
namespace {
int Window_get_width(lua_State* L)
{
auto window = get_ptr<ui::Window>(L, 1);
lua_pushinteger(L, window->bounds().w);
return 1;
}
int Window_get_height(lua_State* L)
{
auto window = get_ptr<ui::Window>(L, 1);
lua_pushinteger(L, window->bounds().h);
return 1;
}
int Window_get_events(lua_State* L)
{
auto window = get_ptr<ui::Window>(L, 1);
push_window_events(L, window);
return 1;
}
const luaL_Reg Window_methods[] = {
{ nullptr, nullptr }
};
const Property Window_properties[] = {
{ "width", Window_get_width, nullptr },
{ "height", Window_get_height, nullptr },
{ "events", Window_get_events, nullptr },
{ nullptr, nullptr, nullptr }
};
} // anonymous namespace
DEF_MTNAME(ui::Window);
void register_window_class(lua_State* L)
{
using ui::Window;
REG_CLASS(L, Window);
REG_CLASS_PROPERTIES(L, Window);
}
} // namespace script
} // namespace app
#endif // ENABLE_UI