From 5720b8abe62a917d52357d0ec8665a431f55c27e Mon Sep 17 00:00:00 2001 From: Jaakko Keränen Date: Sat, 13 Mar 2021 14:00:37 +0200 Subject: Touch: Long-press context click on any widget --- src/ui/touch.c | 13 +++++++++---- src/ui/window.c | 28 +++++++++++++++++----------- src/ui/window.h | 1 + 3 files changed, 27 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/ui/touch.c b/src/ui/touch.c index 416819a0..968e3011 100644 --- a/src/ui/touch.c +++ b/src/ui/touch.c @@ -134,6 +134,7 @@ static void dispatchMotion_Touch_(iFloat3 pos, int buttonState) { static iBool dispatchClick_Touch_(const iTouch *d, int button) { const iFloat3 tapPos = d->pos[0]; + iWindow *window = get_Window(); SDL_MouseButtonEvent btn = { .type = SDL_MOUSEBUTTONDOWN, .button = button, @@ -144,14 +145,16 @@ static iBool dispatchClick_Touch_(const iTouch *d, int button) { .x = x_F3(tapPos), .y = y_F3(tapPos) }; - iBool wasUsed = dispatchEvent_Widget(get_Window()->root, (SDL_Event *) &btn); + iBool wasUsed = dispatchEvent_Widget(window->root, (SDL_Event *) &btn); /* Immediately released, too. */ btn.type = SDL_MOUSEBUTTONUP; btn.state = SDL_RELEASED; btn.timestamp = SDL_GetTicks(); - dispatchEvent_Widget(get_Window()->root, (SDL_Event *) &btn); - //dispatchMotion_Touch_(zero_F3(), 0); + dispatchEvent_Widget(window->root, (SDL_Event *) &btn); setHover_Widget(NULL); /* FIXME: this doesn't seem to do anything? */ + if (!wasUsed && button == SDL_BUTTON_RIGHT) { + postContextClick_Window(window, &btn); + } return wasUsed; } @@ -181,7 +184,8 @@ static void update_TouchState_(void *ptr) { /* Looks like a possible tap. */ dispatchMotion_Touch_(touch->pos[0], 0); } - if (!touch->isTapAndHold && nowTime - touch->startTime >= longPressSpanMs_ && + if (~flags_Widget(touch->affinity) & touchDrag_WidgetFlag && + !touch->isTapAndHold && nowTime - touch->startTime >= longPressSpanMs_ && touch->affinity) { dispatchClick_Touch_(touch, SDL_BUTTON_RIGHT); touch->isTapAndHold = iTrue; @@ -381,6 +385,7 @@ iBool processEvent_Touch(const SDL_Event *ev) { touch->isTouchDrag = iTrue; touch->edge = none_TouchEdge; pushPos_Touch_(touch, pos, fing->timestamp); + dispatchMotion_Touch_(touch->startPos, 0); dispatchEvent_Widget(window->root, (SDL_Event *) &(SDL_MouseButtonEvent){ .type = SDL_MOUSEBUTTONDOWN, .timestamp = fing->timestamp, diff --git a/src/ui/window.c b/src/ui/window.c index dc056887..d896ac21 100644 --- a/src/ui/window.c +++ b/src/ui/window.c @@ -1159,7 +1159,7 @@ static void setupUserInterface_Window(iWindow *d) { (iMenuItem[]) { { leftHalf_Icon " Toggle Left Sidebar", 0, 0, "sidebar.toggle" }, { rightHalf_Icon " Toggle Right Sidebar", 0, 0, "sidebar2.toggle" }, - }, 2); + }, deviceType_App() == phone_AppDeviceType ? 1 : 2); iWidget *clipMenu = makeMenu_Widget(d->root, (iMenuItem[]){ { scissor_Icon " Cut", 0, 0, "input.copy cut:1" }, @@ -1765,16 +1765,7 @@ iBool processEvent_Window(iWindow *d, const SDL_Event *ev) { wasUsed = dispatchEvent_Widget(widget, &paste); } if (event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_RIGHT) { - /* A context menu may still get triggered here. */ - const iWidget *hit = hitChild_Widget(d->root, init_I2(event.button.x, event.button.y)); - while (hit && isEmpty_String(id_Widget(hit))) { - hit = parent_Widget(hit); - } - if (hit) { - postCommandf_App("contextclick id:%s ptr:%p coord:%d %d", - cstr_String(id_Widget(hit)), hit, - event.button.x, event.button.y); - } + postContextClick_Window(d, &event.button); } } if (isMetricsChange_UserEvent(&event)) { @@ -1792,6 +1783,21 @@ iBool processEvent_Window(iWindow *d, const SDL_Event *ev) { return iFalse; } +iBool postContextClick_Window(iWindow *d, const SDL_MouseButtonEvent *ev) { + /* A context menu may still get triggered here. */ + const iWidget *hit = hitChild_Widget(d->root, init_I2(ev->x, ev->y)); + while (hit && isEmpty_String(id_Widget(hit))) { + hit = parent_Widget(hit); + } + if (hit) { + postCommandf_App("contextclick id:%s ptr:%p coord:%d %d", + cstr_String(id_Widget(hit)), hit, + ev->x, ev->y); + return iTrue; + } + return iFalse; +} + void draw_Window(iWindow *d) { if (d->isDrawFrozen) { return; diff --git a/src/ui/window.h b/src/ui/window.h index 2ab5d990..98c63957 100644 --- a/src/ui/window.h +++ b/src/ui/window.h @@ -90,6 +90,7 @@ void setCursor_Window (iWindow *, int cursor); void setSnap_Window (iWindow *, int snapMode); void setKeyboardHeight_Window(iWindow *, int height); void dismissPortraitPhoneSidebars_Window (iWindow *); +iBool postContextClick_Window (iWindow *, const SDL_MouseButtonEvent *); uint32_t id_Window (const iWindow *); iInt2 rootSize_Window (const iWindow *); -- cgit v1.2.3