diff options
author | Jaakko Keränen <jaakko.keranen@iki.fi> | 2020-08-14 14:09:59 +0300 |
---|---|---|
committer | Jaakko Keränen <jaakko.keranen@iki.fi> | 2020-08-14 14:09:59 +0300 |
commit | c2dd6107db2ff4b8001085068f4c1c5971cdd9c3 (patch) | |
tree | f4dc684dae9611abf1f672c853de44c8911db8fd /src/ui | |
parent | bf390e96735a156cb221385cc057aeb957c088b9 (diff) |
Cleaner window initialization
Create the window using the correct size and position, and don’t show the window contents while app state is being restored.
Diffstat (limited to 'src/ui')
-rw-r--r-- | src/ui/documentwidget.c | 8 | ||||
-rw-r--r-- | src/ui/window.c | 30 | ||||
-rw-r--r-- | src/ui/window.h | 5 |
3 files changed, 33 insertions, 10 deletions
diff --git a/src/ui/documentwidget.c b/src/ui/documentwidget.c index 7e4bdc73..2e53e6ef 100644 --- a/src/ui/documentwidget.c +++ b/src/ui/documentwidget.c | |||
@@ -808,10 +808,10 @@ static iBool handleMediaCommand_DocumentWidget_(iDocumentWidget *d, const char * | |||
808 | const enum iGmStatusCode code = status_GmRequest(req->req); | 808 | const enum iGmStatusCode code = status_GmRequest(req->req); |
809 | /* Give the media to the document for presentation. */ | 809 | /* Give the media to the document for presentation. */ |
810 | if (code == success_GmStatusCode) { | 810 | if (code == success_GmStatusCode) { |
811 | printf("media finished: %s\n size: %zu\n type: %s\n", | 811 | // printf("media finished: %s\n size: %zu\n type: %s\n", |
812 | cstr_String(url_GmRequest(req->req)), | 812 | // cstr_String(url_GmRequest(req->req)), |
813 | size_Block(body_GmRequest(req->req)), | 813 | // size_Block(body_GmRequest(req->req)), |
814 | cstr_String(meta_GmRequest(req->req))); | 814 | // cstr_String(meta_GmRequest(req->req))); |
815 | if (startsWith_String(meta_GmRequest(req->req), "image/")) { | 815 | if (startsWith_String(meta_GmRequest(req->req), "image/")) { |
816 | setImage_GmDocument(d->doc, req->linkId, meta_GmRequest(req->req), | 816 | setImage_GmDocument(d->doc, req->linkId, meta_GmRequest(req->req), |
817 | body_GmRequest(req->req)); | 817 | body_GmRequest(req->req)); |
diff --git a/src/ui/window.c b/src/ui/window.c index 4f59ef1a..315c4243 100644 --- a/src/ui/window.c +++ b/src/ui/window.c | |||
@@ -36,7 +36,7 @@ static float initialUiScale_ = 1.0f; | |||
36 | static float initialUiScale_ = 1.1f; | 36 | static float initialUiScale_ = 1.1f; |
37 | #endif | 37 | #endif |
38 | 38 | ||
39 | iDefineTypeConstruction(Window) | 39 | iDefineTypeConstructionArgs(Window, (iRect rect), rect) |
40 | 40 | ||
41 | static iBool handleRootCommands_(iWidget *root, const char *cmd) { | 41 | static iBool handleRootCommands_(iWidget *root, const char *cmd) { |
42 | iUnused(root); | 42 | iUnused(root); |
@@ -354,8 +354,16 @@ static float pixelRatio_Window_(const iWindow *d) { | |||
354 | return (float) dx / (float) x; | 354 | return (float) dx / (float) x; |
355 | } | 355 | } |
356 | 356 | ||
357 | void init_Window(iWindow *d) { | 357 | static void drawBlank_Window_(iWindow *d) { |
358 | const iColor bg = get_Color(gray25_ColorId); | ||
359 | SDL_SetRenderDrawColor(d->render, bg.r, bg.g, bg.b, 255); | ||
360 | SDL_RenderClear(d->render); | ||
361 | SDL_RenderPresent(d->render); | ||
362 | } | ||
363 | |||
364 | void init_Window(iWindow *d, iRect rect) { | ||
358 | theWindow_ = d; | 365 | theWindow_ = d; |
366 | d->isBlank = iTrue; | ||
359 | uint32_t flags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI; | 367 | uint32_t flags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI; |
360 | #if defined (iPlatformApple) | 368 | #if defined (iPlatformApple) |
361 | SDL_SetHint(SDL_HINT_RENDER_DRIVER, "metal"); | 369 | SDL_SetHint(SDL_HINT_RENDER_DRIVER, "metal"); |
@@ -363,17 +371,22 @@ void init_Window(iWindow *d) { | |||
363 | flags |= SDL_WINDOW_OPENGL; | 371 | flags |= SDL_WINDOW_OPENGL; |
364 | #endif | 372 | #endif |
365 | SDL_SetHint(SDL_HINT_RENDER_VSYNC, "1"); | 373 | SDL_SetHint(SDL_HINT_RENDER_VSYNC, "1"); |
366 | if (SDL_CreateWindowAndRenderer(800, 500, flags, &d->win, &d->render)) { | 374 | if (SDL_CreateWindowAndRenderer( |
375 | width_Rect(rect), height_Rect(rect), flags, &d->win, &d->render)) { | ||
367 | fprintf(stderr, "Error when creating window: %s\n", SDL_GetError()); | 376 | fprintf(stderr, "Error when creating window: %s\n", SDL_GetError()); |
368 | exit(-2); | 377 | exit(-2); |
369 | } | 378 | } |
379 | if (left_Rect(rect) >= 0) { | ||
380 | SDL_SetWindowPosition(d->win, left_Rect(rect), top_Rect(rect)); | ||
381 | } | ||
370 | SDL_SetWindowMinimumSize(d->win, 640, 400); | 382 | SDL_SetWindowMinimumSize(d->win, 640, 400); |
371 | SDL_SetWindowTitle(d->win, "Lagrange"); | 383 | SDL_SetWindowTitle(d->win, "Lagrange"); |
372 | /* Some info. */ { | 384 | /* Some info. */ { |
373 | SDL_RendererInfo info; | 385 | SDL_RendererInfo info; |
374 | SDL_GetRendererInfo(d->render, &info); | 386 | SDL_GetRendererInfo(d->render, &info); |
375 | printf("[window] renderer: %s\n", info.name); | 387 | printf("[window] renderer: %s\n", info.name); |
376 | } | 388 | } |
389 | drawBlank_Window_(d); | ||
377 | d->uiScale = initialUiScale_; | 390 | d->uiScale = initialUiScale_; |
378 | d->pixelRatio = pixelRatio_Window_(d); | 391 | d->pixelRatio = pixelRatio_Window_(d); |
379 | setPixelRatio_Metrics(d->pixelRatio * d->uiScale); | 392 | setPixelRatio_Metrics(d->pixelRatio * d->uiScale); |
@@ -447,6 +460,11 @@ iBool processEvent_Window(iWindow *d, const SDL_Event *ev) { | |||
447 | } | 460 | } |
448 | default: { | 461 | default: { |
449 | SDL_Event event = *ev; | 462 | SDL_Event event = *ev; |
463 | if (event.type == SDL_USEREVENT && isCommand_UserEvent(ev, "window.unblank")) { | ||
464 | d->isBlank = iFalse; | ||
465 | postRefresh_App(); | ||
466 | return iTrue; | ||
467 | } | ||
450 | /* Map mouse pointer coordinate to our coordinate system. */ | 468 | /* Map mouse pointer coordinate to our coordinate system. */ |
451 | if (event.type == SDL_MOUSEMOTION) { | 469 | if (event.type == SDL_MOUSEMOTION) { |
452 | const iInt2 pos = coord_Window(d, event.motion.x, event.motion.y); | 470 | const iInt2 pos = coord_Window(d, event.motion.x, event.motion.y); |
@@ -479,6 +497,10 @@ iBool processEvent_Window(iWindow *d, const SDL_Event *ev) { | |||
479 | 497 | ||
480 | void draw_Window(iWindow *d) { | 498 | void draw_Window(iWindow *d) { |
481 | /* Clear the window. */ | 499 | /* Clear the window. */ |
500 | if (d->isBlank) { | ||
501 | drawBlank_Window_(d); | ||
502 | return; | ||
503 | } | ||
482 | SDL_SetRenderDrawColor(d->render, 0, 0, 0, 255); | 504 | SDL_SetRenderDrawColor(d->render, 0, 0, 0, 255); |
483 | SDL_RenderClear(d->render); | 505 | SDL_RenderClear(d->render); |
484 | /* Draw widgets. */ | 506 | /* Draw widgets. */ |
diff --git a/src/ui/window.h b/src/ui/window.h index 413c454c..6f4e01ce 100644 --- a/src/ui/window.h +++ b/src/ui/window.h | |||
@@ -2,16 +2,17 @@ | |||
2 | 2 | ||
3 | #include "widget.h" | 3 | #include "widget.h" |
4 | 4 | ||
5 | #include <the_Foundation/defs.h> | 5 | #include <the_Foundation/rect.h> |
6 | #include <SDL_events.h> | 6 | #include <SDL_events.h> |
7 | #include <SDL_render.h> | 7 | #include <SDL_render.h> |
8 | #include <SDL_video.h> | 8 | #include <SDL_video.h> |
9 | 9 | ||
10 | iDeclareType(Window) | 10 | iDeclareType(Window) |
11 | iDeclareTypeConstruction(Window) | 11 | iDeclareTypeConstructionArgs(Window, iRect rect) |
12 | 12 | ||
13 | struct Impl_Window { | 13 | struct Impl_Window { |
14 | SDL_Window * win; | 14 | SDL_Window * win; |
15 | iBool isBlank; /* avoids premature draws while restoring window state */ | ||
15 | SDL_Renderer *render; | 16 | SDL_Renderer *render; |
16 | iWidget * root; | 17 | iWidget * root; |
17 | float pixelRatio; | 18 | float pixelRatio; |