Skip to content

Commit 1faa3a4

Browse files
committed
darwin implementation for uiWindowSetCentered
1 parent 2ceeff8 commit 1faa3a4

5 files changed

Lines changed: 45 additions & 9 deletions

File tree

darwin/window.m

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
BOOL suppressSizeChanged;
1717
int fullscreen;
1818
int borderless;
19+
int centered;
1920
};
2021

2122
@implementation uiprivNSWindow
@@ -172,9 +173,9 @@ static int uiWindowVisible(uiControl *c)
172173
return [w->window isVisible];
173174
}
174175

175-
void uiWindowShow(uiControl *c)
176+
void uiWindowShow(uiWindow *w)
176177
{
177-
uiWindow *w = (uiWindow *) c;
178+
// uiWindow *w = (uiWindow *) c;
178179
// TODO: D4
179180
// https://developer.apple.com/documentation/appkit/nswindow/1419208-makekeyandorderfront?language=objc
180181
[w->window makeKeyAndOrderFront:w->window];
@@ -184,6 +185,13 @@ void uiWindowShow(uiControl *c)
184185
// [w->window orderFront: nil];
185186
}
186187

188+
void uiWindowSetCentered(uiWindow *w, int centered) {
189+
w->centered = centered;
190+
if (centered) {
191+
[w->window center];
192+
}
193+
}
194+
187195
static void uiWindowHide(uiControl *c)
188196
{
189197
uiWindow *w = (uiWindow *) c;
@@ -226,6 +234,10 @@ static void windowRelayout(uiWindow *w)
226234
uiDarwinControlHugsBottom(uiDarwinControl(w->child)),
227235
w->margined,
228236
@"uiWindow");
237+
238+
if (w->centered) {
239+
[w->window center];
240+
}
229241
}
230242

231243
uiDarwinControlDefaultHugsTrailingEdge(uiWindow, window)
@@ -386,6 +398,17 @@ static void defaultOnPositionContentSizeChanged(uiWindow *w, void *data)
386398
defer:YES];
387399
[w->window setTitle:uiprivToNSString(title)];
388400

401+
w->centered = 1;
402+
403+
// https://github.com/andlabs/libui/pull/271/files
404+
// if (NSEqualPoints(lastTopLeftPoint, NSZeroPoint)) {
405+
// // issue "cascadeTopLeftFromPoint" twice on first time
406+
// // to have window position in a good place
407+
// lastTopLeftPoint = [w->window cascadeTopLeftFromPoint:lastTopLeftPoint];
408+
// }
409+
// lastTopLeftPoint = [w->window cascadeTopLeftFromPoint:lastTopLeftPoint];
410+
411+
389412
// do NOT release when closed
390413
// we manually do this in uiWindowDestroy() above
391414
[w->window setReleasedWhenClosed:NO];

examples/controlgallery/main.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,8 @@ int main(void)
305305
}
306306

307307
mainwin = uiNewWindow("libui Control Gallery", 640, 480, 1);
308+
309+
308310
uiWindowOnClosing(mainwin, onClosing, NULL);
309311
uiOnShouldQuit(onShouldQuit, mainwin);
310312

@@ -324,7 +326,7 @@ int main(void)
324326
uiControlShow(uiControl(mainwin));
325327
uiMain();
326328

327-
uiWindowShow(uiControl(mainwin));
329+
uiWindowShow(mainwin);
328330
return 0;
329331
}
330332

ui.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ _UI_EXTERN void uiWindowSetChild(uiWindow *w, uiControl *child);
131131
_UI_EXTERN int uiWindowMargined(uiWindow *w);
132132
_UI_EXTERN void uiWindowSetMargined(uiWindow *w, int margined);
133133
_UI_EXTERN uiWindow *uiNewWindow(const char *title, int width, int height, int hasMenubar);
134-
_UI_EXTERN void uiWindowShow(uiControl *c);
134+
_UI_EXTERN void uiWindowShow(uiWindow *c);
135+
_UI_EXTERN void uiWindowSetCentered(uiWindow *c, int centered);
135136

136137
typedef struct uiButton uiButton;
137138
#define uiButton(this) ((uiButton *) (this))

unix/window.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ struct uiWindow {
1919

2020
uiControl *child;
2121
int margined;
22+
int centered;
2223

2324
int (*onClosing)(uiWindow *, void *);
2425
void *onClosingData;
@@ -98,16 +99,19 @@ static int uiWindowToplevel(uiControl *c)
9899

99100
uiUnixControlDefaultVisible(uiWindow)
100101

101-
void uiWindowShow(uiControl *c)
102+
void uiWindowShow(uiWindow *w)
102103
{
103-
uiWindow *w = uiWindow(c);
104-
105104
// don't use gtk_widget_show_all() as that will show all children, regardless of user settings
106105
// don't use gtk_widget_show(); that doesn't bring to front or give keyboard focus
107106
// (gtk_window_present() does call gtk_widget_show() though)
108107
gtk_window_present(w->window);
109108
}
110109

110+
void uiWindowSetCentered(uiWindow *w, int centered) {
111+
w->centered = centered;
112+
// TODO:
113+
}
114+
111115
uiUnixControlDefaultHide(uiWindow)
112116
uiUnixControlDefaultEnabled(uiWindow)
113117
uiUnixControlDefaultEnable(uiWindow)

windows/window.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ struct uiWindow {
2020
int fullscreen;
2121
WINDOWPLACEMENT fsPrevPlacement;
2222
int borderless;
23+
int centered;
2324
};
2425

2526
// from https://msdn.microsoft.com/en-us/library/windows/desktop/dn742486.aspx#sizingandspacing
@@ -201,9 +202,9 @@ static int uiWindowVisible(uiControl *c)
201202
return w->visible;
202203
}
203204

204-
void uiWindowShow(uiControl *c)
205+
void uiWindowShow(uiWindow *w)
205206
{
206-
uiWindow *w = uiWindow(c);
207+
// uiWindow *w = uiWindow(c);
207208

208209
w->visible = 1;
209210
// just in case the window's minimum size wasn't recalculated already
@@ -228,6 +229,11 @@ static void uiWindowHide(uiControl *c)
228229
ShowWindow(w->hwnd, SW_HIDE);
229230
}
230231

232+
void uiWindowSetCentered(uiWindow *w, int centered) {
233+
w->centered = centered;
234+
// TODO:
235+
}
236+
231237
// TODO we don't want the window to be disabled completely; that would prevent it from being moved! ...would it?
232238
uiWindowsControlDefaultEnabled(uiWindow)
233239
uiWindowsControlDefaultEnable(uiWindow)

0 commit comments

Comments
 (0)