From 770313365c781ca52518340c57e2f06e093cfebe Mon Sep 17 00:00:00 2001 From: Jaakko Keränen Date: Tue, 25 Aug 2020 13:01:44 +0300 Subject: Window: Avoid setting cursor multiple times per event --- src/ui/sidebarwidget.c | 16 +++++++--------- src/ui/window.c | 15 +++++++++++++-- src/ui/window.h | 1 + 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/ui/sidebarwidget.c b/src/ui/sidebarwidget.c index 73bd1646..e3d7d4a6 100644 --- a/src/ui/sidebarwidget.c +++ b/src/ui/sidebarwidget.c @@ -627,15 +627,13 @@ static iBool processEvent_SidebarWidget_(iSidebarWidget *d, const SDL_Event *ev) else if (contains_Widget(constAs_Widget(d->scroll), mouse)) { setCursor_Window(get_Window(), SDL_SYSTEM_CURSOR_ARROW); } - else { - if (contains_Widget(w, mouse)) { - hover = itemIndex_SidebarWidget_(d, mouse); - if (hover != iInvalidPos && d->mode != identities_SidebarMode) { - setCursor_Window(get_Window(), SDL_SYSTEM_CURSOR_HAND); - } - else { - setCursor_Window(get_Window(), SDL_SYSTEM_CURSOR_ARROW); - } + else if (contains_Widget(w, mouse)) { + hover = itemIndex_SidebarWidget_(d, mouse); + if (hover != iInvalidPos && d->mode != identities_SidebarMode) { + setCursor_Window(get_Window(), SDL_SYSTEM_CURSOR_HAND); + } + else { + setCursor_Window(get_Window(), SDL_SYSTEM_CURSOR_ARROW); } } if (hover != d->hoverItem) { diff --git a/src/ui/window.c b/src/ui/window.c index 82109c07..ef8aea71 100644 --- a/src/ui/window.c +++ b/src/ui/window.c @@ -420,6 +420,7 @@ static void drawBlank_Window_(iWindow *d) { void init_Window(iWindow *d, iRect rect) { theWindow_ = d; iZap(d->cursors); + d->pendingCursor = NULL; d->isDrawFrozen = iTrue; uint32_t flags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI; #if defined (iPlatformApple) @@ -515,6 +516,13 @@ static iBool handleWindowEvent_Window_(iWindow *d, const SDL_WindowEvent *ev) { return iFalse; } +static void applyCursor_Window_(iWindow *d) { + if (d->pendingCursor) { + SDL_SetCursor(d->pendingCursor); + d->pendingCursor = NULL; + } +} + iBool processEvent_Window(iWindow *d, const SDL_Event *ev) { switch (ev->type) { case SDL_WINDOWEVENT: { @@ -552,6 +560,9 @@ iBool processEvent_Window(iWindow *d, const SDL_Event *ev) { if (oldHover != hover_Widget()) { postRefresh_App(); } + if (event.type == SDL_MOUSEMOTION) { + applyCursor_Window_(d); + } return wasUsed; } } @@ -610,11 +621,11 @@ void setFreezeDraw_Window(iWindow *d, iBool freezeDraw) { d->isDrawFrozen = freezeDraw; } -void setCursor_Window(iWindow *d, int cursor) { +void setCursor_Window(iWindow *d, int cursor) { if (!d->cursors[cursor]) { d->cursors[cursor] = SDL_CreateSystemCursor(cursor); } - SDL_SetCursor(d->cursors[cursor]); + d->pendingCursor = d->cursors[cursor]; } iInt2 rootSize_Window(const iWindow *d) { diff --git a/src/ui/window.h b/src/ui/window.h index e6f8cf30..4aec2fa7 100644 --- a/src/ui/window.h +++ b/src/ui/window.h @@ -42,6 +42,7 @@ struct Impl_Window { uint32_t frameTime; double presentTime; SDL_Cursor * cursors[SDL_NUM_SYSTEM_CURSORS]; + SDL_Cursor * pendingCursor; }; iBool processEvent_Window (iWindow *, const SDL_Event *); -- cgit v1.2.3