summaryrefslogtreecommitdiff
path: root/src/ui/documentwidget.c
diff options
context:
space:
mode:
authorJaakko Keränen <jaakko.keranen@iki.fi>2021-04-09 16:41:47 +0300
committerJaakko Keränen <jaakko.keranen@iki.fi>2021-04-10 06:29:31 +0300
commitf76d37091f575e3578f10c8fb514b97c2248a0cf (patch)
treec441f9051069252f174a11a4abeebe8c11097819 /src/ui/documentwidget.c
parent6d36a826ce502737bd1b6ff2b9fe47c79338e58e (diff)
Mobile: Zooming using pinch gestures
Diffstat (limited to 'src/ui/documentwidget.c')
-rw-r--r--src/ui/documentwidget.c47
1 files changed, 47 insertions, 0 deletions
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 {
199 centerVertically_DocumentWidgetFlag = iBit(6), 199 centerVertically_DocumentWidgetFlag = iBit(6),
200 selectWords_DocumentWidgetFlag = iBit(7), 200 selectWords_DocumentWidgetFlag = iBit(7),
201 selectLines_DocumentWidgetFlag = iBit(8), 201 selectLines_DocumentWidgetFlag = iBit(8),
202 pinchZoom_DocumentWidgetFlag = iBit(9),
202}; 203};
203 204
204enum iDocumentLinkOrdinalMode { 205enum iDocumentLinkOrdinalMode {
@@ -263,6 +264,8 @@ struct Impl_DocumentWidget {
263 iDrawBufs * drawBufs; /* dynamic state for drawing */ 264 iDrawBufs * drawBufs; /* dynamic state for drawing */
264 iTranslation * translation; 265 iTranslation * translation;
265 iWidget * phoneToolbar; 266 iWidget * phoneToolbar;
267 int pinchZoomInitial;
268 int pinchZoomPosted;
266}; 269};
267 270
268iDefineObjectConstruction(DocumentWidget) 271iDefineObjectConstruction(DocumentWidget)
@@ -1663,6 +1666,37 @@ static iBool updateDocumentWidthRetainingScrollPosition_DocumentWidget_(iDocumen
1663 return iTrue; 1666 return iTrue;
1664} 1667}
1665 1668
1669static iBool handlePinch_DocumentWidget_(iDocumentWidget *d, const char *cmd) {
1670 if (equal_Command(cmd, "pinch.began")) {
1671 d->pinchZoomInitial = d->pinchZoomPosted = prefs_App()->zoomPercent;
1672 d->flags |= pinchZoom_DocumentWidgetFlag;
1673 refresh_Widget(d);
1674 }
1675 else if (equal_Command(cmd, "pinch.moved")) {
1676 const float rel = argf_Command(cmd);
1677 int zoom = iClamp(iRound(d->pinchZoomInitial * rel / 5.0f) * 5, 50, 200);
1678 /* Snap to 100%. */
1679 if (zoom > 90 && zoom < 110) {
1680 zoom = 100;
1681 }
1682 else if (zoom > 100) {
1683 zoom = iMax(100, zoom - 10);
1684 }
1685 else {
1686 zoom = iMin(100, zoom + 10);
1687 }
1688 if (d->pinchZoomPosted != zoom) {
1689 d->pinchZoomPosted = zoom;
1690 postCommandf_App("zoom.set arg:%d", zoom);
1691 }
1692 }
1693 else if (equal_Command(cmd, "pinch.ended")) {
1694 d->flags &= ~pinchZoom_DocumentWidgetFlag;
1695 refresh_Widget(d);
1696 }
1697 return iTrue;
1698}
1699
1666static iBool handleCommand_DocumentWidget_(iDocumentWidget *d, const char *cmd) { 1700static iBool handleCommand_DocumentWidget_(iDocumentWidget *d, const char *cmd) {
1667 iWidget *w = as_Widget(d); 1701 iWidget *w = as_Widget(d);
1668 if (equal_Command(cmd, "window.resized") || equal_Command(cmd, "font.changed")) { 1702 if (equal_Command(cmd, "window.resized") || equal_Command(cmd, "font.changed")) {
@@ -2257,6 +2291,9 @@ static iBool handleCommand_DocumentWidget_(iDocumentWidget *d, const char *cmd)
2257 else if (equal_Command(cmd, "document.autoreload.set") && document_App() == d) { 2291 else if (equal_Command(cmd, "document.autoreload.set") && document_App() == d) {
2258 d->mod.reloadInterval = arg_Command(cmd); 2292 d->mod.reloadInterval = arg_Command(cmd);
2259 } 2293 }
2294 else if (startsWith_CStr(cmd, "pinch.") && document_Command(cmd) == d) {
2295 return handlePinch_DocumentWidget_(d, cmd);
2296 }
2260 return iFalse; 2297 return iFalse;
2261} 2298}
2262 2299
@@ -3593,6 +3630,16 @@ static void draw_DocumentWidget_(const iDocumentWidget *d) {
3593 setOpacity_Text(1.0f); 3630 setOpacity_Text(1.0f);
3594 } 3631 }
3595 } 3632 }
3633 /* Pinch zoom indicator. */
3634 if (d->flags & pinchZoom_DocumentWidgetFlag) {
3635 const int font = defaultLargeBold_FontId;
3636 const int height = lineHeight_Text(font) * 2;
3637 const iInt2 size = init_I2(height * 2, height);
3638 const iRect rect = { sub_I2(mid_Rect(bounds), divi_I2(size, 2)), size };
3639 fillRect_Paint(&ctx.paint, rect, uiTextAction_ColorId);
3640 drawCentered_Text(font, bounds, iFalse, uiBackground_ColorId, "%d %%",
3641 d->pinchZoomPosted);
3642 }
3596} 3643}
3597 3644
3598/*----------------------------------------------------------------------------------------------*/ 3645/*----------------------------------------------------------------------------------------------*/