[lua] Added `autofit` and `sizeHint` properties to Dialog (fix #5176)

This commit is contained in:
Christian Kaiser 2025-08-02 21:08:02 -03:00
parent b3814ec912
commit aecb5539a8
1 changed files with 64 additions and 5 deletions

View File

@ -107,6 +107,7 @@ struct Dialog {
std::map<std::string, ui::Widget*> dataWidgets; std::map<std::string, ui::Widget*> dataWidgets;
std::map<std::string, ui::Widget*> labelWidgets; std::map<std::string, ui::Widget*> labelWidgets;
int currentRadioGroup = 0; int currentRadioGroup = 0;
int autofit = ui::LEFT | ui::TOP;
// Member used to hold current state about the creation of a tabs // Member used to hold current state about the creation of a tabs
// widget. After creation it is reset to null to be ready for the // widget. After creation it is reset to null to be ready for the
@ -193,6 +194,13 @@ struct Dialog {
it->second->setText(text); it->second->setText(text);
} }
void setAutofit(int align)
{
// Accept both 0 or a valid subset of align parameters.
if (align == 0 || (align & (ui::LEFT | ui::RIGHT | ui::TOP | ui::BOTTOM)))
autofit = align;
}
Display* parentDisplay() const Display* parentDisplay() const
{ {
Display* parentDisplay = window.parentDisplay(); Display* parentDisplay = window.parentDisplay();
@ -231,8 +239,10 @@ struct Dialog {
window.display()->nativeWindow()->setFrame(frame); window.display()->nativeWindow()->setFrame(frame);
} }
else { else {
gfx::Rect oldBounds(window.bounds());
window.setBounds(rc); window.setBounds(rc);
window.invalidate(); window.invalidate();
parentDisplay()->invalidateRect(oldBounds);
} }
} }
@ -367,6 +377,7 @@ int Dialog_new(lua_State* L)
ui::Window::Type windowType = ui::Window::WithTitleBar; ui::Window::Type windowType = ui::Window::WithTitleBar;
std::string title = "Script"; std::string title = "Script";
bool sizeable = true; bool sizeable = true;
int autofit = -1;
if (lua_isstring(L, 1)) { if (lua_isstring(L, 1)) {
title = lua_tostring(L, 1); title = lua_tostring(L, 1);
} }
@ -385,9 +396,16 @@ int Dialog_new(lua_State* L)
if (type != LUA_TNIL && !lua_toboolean(L, -1)) if (type != LUA_TNIL && !lua_toboolean(L, -1))
sizeable = false; sizeable = false;
lua_pop(L, 1); lua_pop(L, 1);
type = lua_getfield(L, 1, "autofit");
if (type != LUA_TNIL) {
autofit = lua_tointeger(L, -1);
}
lua_pop(L, 1);
} }
auto dlg = push_new<Dialog>(L, windowType, title, sizeable); auto dlg = push_new<Dialog>(L, windowType, title, sizeable);
dlg->setAutofit(autofit);
// The uservalue of the dialog userdata will contain a table that // The uservalue of the dialog userdata will contain a table that
// stores all the callbacks to handle events. As these callbacks can // stores all the callbacks to handle events. As these callbacks can
@ -1654,8 +1672,26 @@ int Dialog_modify(lua_State* L)
if (relayout && !dlg->window.isResizing()) { if (relayout && !dlg->window.isResizing()) {
dlg->window.layout(); dlg->window.layout();
gfx::Rect bounds(dlg->window.bounds().w, dlg->window.sizeHint().h); if (dlg->autofit > 0) {
dlg->window.expandWindow(bounds.size()); gfx::Rect oldBounds = dlg->window.bounds();
gfx::Size resize(oldBounds.size());
if (dlg->autofit & ui::TOP || dlg->autofit & ui::BOTTOM)
resize.h = dlg->window.sizeHint().h;
if (dlg->autofit & ui::LEFT || dlg->autofit & ui::RIGHT)
resize.w = dlg->window.sizeHint().w;
gfx::Size difference = resize - oldBounds.size();
const auto& bounds = dlg->getWindowBounds();
gfx::Rect newBounds(bounds.x, bounds.y, resize.w, resize.h);
if (dlg->autofit & ui::BOTTOM)
newBounds.y = bounds.y - difference.h;
if (dlg->autofit & ui::RIGHT)
newBounds.x = bounds.x - difference.w;
dlg->setWindowBounds(newBounds);
}
} }
} }
lua_pushvalue(L, 1); lua_pushvalue(L, 1);
@ -1877,6 +1913,27 @@ int Dialog_get_bounds(lua_State* L)
return 1; return 1;
} }
int Dialog_get_sizeHint(lua_State* L)
{
auto dlg = get_obj<Dialog>(L, 1);
push_new<gfx::Size>(L, dlg->window.sizeHint());
return 1;
}
int Dialog_get_autofit(lua_State* L)
{
auto dlg = get_obj<Dialog>(L, 1);
lua_pushinteger(L, dlg->autofit);
return 1;
}
int Dialog_set_autofit(lua_State* L)
{
auto dlg = get_obj<Dialog>(L, 1);
dlg->setAutofit(lua_tointeger(L, 2));
return 0;
}
int Dialog_set_bounds(lua_State* L) int Dialog_set_bounds(lua_State* L)
{ {
auto dlg = get_obj<Dialog>(L, 1); auto dlg = get_obj<Dialog>(L, 1);
@ -1920,6 +1977,8 @@ const luaL_Reg Dialog_methods[] = {
const Property Dialog_properties[] = { const Property Dialog_properties[] = {
{ "data", Dialog_get_data, Dialog_set_data }, { "data", Dialog_get_data, Dialog_set_data },
{ "bounds", Dialog_get_bounds, Dialog_set_bounds }, { "bounds", Dialog_get_bounds, Dialog_set_bounds },
{ "autofit", Dialog_get_autofit, Dialog_set_autofit },
{ "sizeHint", Dialog_get_sizeHint, nullptr },
{ nullptr, nullptr, nullptr } { nullptr, nullptr, nullptr }
}; };