From f76d37091f575e3578f10c8fb514b97c2248a0cf Mon Sep 17 00:00:00 2001 From: Jaakko Keränen Date: Fri, 9 Apr 2021 16:41:47 +0300 Subject: Mobile: Zooming using pinch gestures --- src/ui/documentwidget.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'src/ui/documentwidget.c') diff --git a/src/ui/documentwidget.c b/src/ui/documentwidget.c index 64172a08..9df88068 100644 --- a/src/ui/documentwidget.c +++ b/src/ui/documentwidget.c @@ -199,6 +199,7 @@ enum iDocumentWidgetFlag { centerVertically_DocumentWidgetFlag = iBit(6), selectWords_DocumentWidgetFlag = iBit(7), selectLines_DocumentWidgetFlag = iBit(8), + pinchZoom_DocumentWidgetFlag = iBit(9), }; enum iDocumentLinkOrdinalMode { @@ -263,6 +264,8 @@ struct Impl_DocumentWidget { iDrawBufs * drawBufs; /* dynamic state for drawing */ iTranslation * translation; iWidget * phoneToolbar; + int pinchZoomInitial; + int pinchZoomPosted; }; iDefineObjectConstruction(DocumentWidget) @@ -1663,6 +1666,37 @@ static iBool updateDocumentWidthRetainingScrollPosition_DocumentWidget_(iDocumen return iTrue; } +static iBool handlePinch_DocumentWidget_(iDocumentWidget *d, const char *cmd) { + if (equal_Command(cmd, "pinch.began")) { + d->pinchZoomInitial = d->pinchZoomPosted = prefs_App()->zoomPercent; + d->flags |= pinchZoom_DocumentWidgetFlag; + refresh_Widget(d); + } + else if (equal_Command(cmd, "pinch.moved")) { + const float rel = argf_Command(cmd); + int zoom = iClamp(iRound(d->pinchZoomInitial * rel / 5.0f) * 5, 50, 200); + /* Snap to 100%. */ + if (zoom > 90 && zoom < 110) { + zoom = 100; + } + else if (zoom > 100) { + zoom = iMax(100, zoom - 10); + } + else { + zoom = iMin(100, zoom + 10); + } + if (d->pinchZoomPosted != zoom) { + d->pinchZoomPosted = zoom; + postCommandf_App("zoom.set arg:%d", zoom); + } + } + else if (equal_Command(cmd, "pinch.ended")) { + d->flags &= ~pinchZoom_DocumentWidgetFlag; + refresh_Widget(d); + } + return iTrue; +} + static iBool handleCommand_DocumentWidget_(iDocumentWidget *d, const char *cmd) { iWidget *w = as_Widget(d); if (equal_Command(cmd, "window.resized") || equal_Command(cmd, "font.changed")) { @@ -2257,6 +2291,9 @@ static iBool handleCommand_DocumentWidget_(iDocumentWidget *d, const char *cmd) else if (equal_Command(cmd, "document.autoreload.set") && document_App() == d) { d->mod.reloadInterval = arg_Command(cmd); } + else if (startsWith_CStr(cmd, "pinch.") && document_Command(cmd) == d) { + return handlePinch_DocumentWidget_(d, cmd); + } return iFalse; } @@ -3593,6 +3630,16 @@ static void draw_DocumentWidget_(const iDocumentWidget *d) { setOpacity_Text(1.0f); } } + /* Pinch zoom indicator. */ + if (d->flags & pinchZoom_DocumentWidgetFlag) { + const int font = defaultLargeBold_FontId; + const int height = lineHeight_Text(font) * 2; + const iInt2 size = init_I2(height * 2, height); + const iRect rect = { sub_I2(mid_Rect(bounds), divi_I2(size, 2)), size }; + fillRect_Paint(&ctx.paint, rect, uiTextAction_ColorId); + drawCentered_Text(font, bounds, iFalse, uiBackground_ColorId, "%d %%", + d->pinchZoomPosted); + } } /*----------------------------------------------------------------------------------------------*/ -- cgit v1.2.3