1414#include < hyprutils/string/String.hpp>
1515
1616#include < print>
17+ #include < algorithm>
1718
1819using namespace Hyprutils ::Memory;
1920using namespace Hyprutils ::Math;
@@ -25,6 +26,7 @@ using namespace Hyprtoolkit;
2526#define UP CUniquePointer
2627
2728static SP<IBackend> backend;
29+ static SP<IWindow> window;
2830
2931//
3032int main (int argc, char ** argv, char ** envp) {
@@ -97,25 +99,22 @@ int main(int argc, char** argv, char** envp) {
9799 replaceInString (textStr, " <br/>" , " \n " );
98100 replaceInString (textStr, " \\ n" , " \n " );
99101
100- //
101- auto window =
102- CWindowBuilder::begin ()->preferredSize ({480 , 180 })->minSize ({480 , 180 })->maxSize ({480 , 180 })->appTitle (std::move (appTitle))->appClass (" hyprland-dialog" )->commence ();
103-
104- window->m_rootElement ->addChild (CRectangleBuilder::begin ()->color ([] { return backend->getPalette ()->m_colors .background ; })->commence ());
102+ const auto FONT_SIZE = CFontSize{CFontSize::HT_FONT_TEXT}.ptSize ();
103+ const float WINDOW_X = FONT_SIZE * 50 .F ;
105104
106- auto layout = CColumnLayoutBuilder::begin ()->size ({CDynamicSize::HT_SIZE_PERCENT, CDynamicSize::HT_SIZE_PERCENT, {1 .F , 1 .F }})->commence ();
105+ // for now do auto to estimate size
106+ auto layout = CColumnLayoutBuilder::begin ()->size ({CDynamicSize::HT_SIZE_ABSOLUTE, CDynamicSize::HT_SIZE_AUTO, {WINDOW_X, 1 .F }})->commence ();
107107 layout->setMargin (3 );
108108
109109 auto layoutInner = CColumnLayoutBuilder::begin ()->size ({CDynamicSize::HT_SIZE_PERCENT, CDynamicSize::HT_SIZE_AUTO, {0 .85F , 1 .F }})->commence ();
110110
111- window->m_rootElement ->addChild (layout);
112-
113111 layout->addChild (layoutInner);
114112 layoutInner->setGrow (true );
115113 layoutInner->setPositionMode (Hyprtoolkit::IElement::HT_POSITION_ABSOLUTE);
116114 layoutInner->setPositionFlag (Hyprtoolkit::IElement::HT_POSITION_FLAG_HCENTER, true );
117115
118- auto title = CTextBuilder::begin ()->text (std::move (titleStr))->fontSize ({CFontSize::HT_FONT_H2})->color ([] { return backend->getPalette ()->m_colors .text ; })->commence ();
116+ auto title =
117+ CTextBuilder::begin ()->text (std::move (titleStr))->fontSize ({CFontSize::HT_FONT_H2})->async (false )->color ([] { return backend->getPalette ()->m_colors .text ; })->commence ();
119118
120119 auto hr = CRectangleBuilder::begin () //
121120 ->color ([] { return CHyprColor{backend->getPalette ()->m_colors .text .darken (0.65 )}; })
@@ -124,18 +123,22 @@ int main(int argc, char** argv, char** envp) {
124123
125124 hr->setMargin (4 );
126125
127- auto content =
128- CTextBuilder::begin ()->text (std::move (textStr))->fontSize (CFontSize{CFontSize::HT_FONT_TEXT})->color ([] { return backend->getPalette ()->m_colors .text ; })->commence ();
126+ auto content = CTextBuilder::begin ()
127+ ->text (std::move (textStr))
128+ ->fontSize (CFontSize{CFontSize::HT_FONT_TEXT})
129+ ->async (false )
130+ ->color ([] { return backend->getPalette ()->m_colors .text ; })
131+ ->commence ();
129132
130133 std::vector<SP<CButtonElement>> buttons;
131134
132135 for (const auto & bstr : buttonsStrs) {
133136 buttons.emplace_back (CButtonBuilder::begin ()
134137 ->label (std::string{bstr})
135- ->onMainClick ([w = WP<IWindow>{window}, str = bstr](auto ) {
138+ ->onMainClick ([str = bstr](auto ) {
136139 std::println (" {}" , str);
137- if (w )
138- w ->close ();
140+ if (window )
141+ window ->close ();
139142 backend->destroy ();
140143 })
141144 ->size ({CDynamicSize::HT_SIZE_AUTO, CDynamicSize::HT_SIZE_AUTO, {1 , 1 }})
@@ -159,6 +162,30 @@ int main(int argc, char** argv, char** envp) {
159162
160163 layout->addChild (layout2);
161164
165+ {
166+ // calculate the layout size before we open the window to size it properly
167+ SP<CNullElement> null = CNullBuilder::begin ()->size ({CDynamicSize::HT_SIZE_ABSOLUTE, CDynamicSize::HT_SIZE_ABSOLUTE, {WINDOW_X, 99999999 .F }})->commence ();
168+
169+ null->addChild (layout);
170+ layout->forceReposition ();
171+ }
172+
173+ const float WINDOW_Y = layout->size ().y + 50 /* pad */ ;
174+
175+ layout->rebuild ()->size ({CDynamicSize::HT_SIZE_PERCENT, CDynamicSize::HT_SIZE_PERCENT, {1 .F , 1 .F }})->commence ();
176+
177+ //
178+ window = CWindowBuilder::begin ()
179+ ->preferredSize ({WINDOW_X, WINDOW_Y})
180+ ->minSize ({WINDOW_X, WINDOW_Y})
181+ ->maxSize ({WINDOW_X, WINDOW_Y})
182+ ->appTitle (std::move (appTitle))
183+ ->appClass (" hyprland-dialog" )
184+ ->commence ();
185+
186+ window->m_rootElement ->addChild (CRectangleBuilder::begin ()->color ([] { return backend->getPalette ()->m_colors .background ; })->commence ());
187+ window->m_rootElement ->addChild (layout);
188+
162189 window->m_events .closeRequest .listenStatic ([w = WP<IWindow>{window}] {
163190 w->close ();
164191 backend->destroy ();
0 commit comments