From f8b7d6044d43387679620e7f268f531a42a0013e Mon Sep 17 00:00:00 2001 From: Jaakko Keränen Date: Thu, 13 Aug 2020 15:41:19 +0300 Subject: App-wide document zoom; added --echo option for debugging Fonts are shared by all documents, so zoom likewise needs to be app-wide. --- src/app.c | 34 +++++++++++++++++++++++++++------- src/app.h | 1 + src/gmrequest.c | 2 +- src/ui/documentwidget.c | 40 +++++++--------------------------------- src/ui/sidebarwidget.c | 1 + src/ui/window.c | 3 +++ 6 files changed, 40 insertions(+), 41 deletions(-) (limited to 'src') diff --git a/src/app.c b/src/app.c index 12f3393c..488e7b42 100644 --- a/src/app.c +++ b/src/app.c @@ -53,12 +53,14 @@ static const char *stateFileName_App_ = "state.bin"; struct Impl_App { iCommandLine args; + iBool commandEcho; /* --echo */ iBool running; iWindow * window; iSortedArray tickers; iBool pendingRefresh; iGmCerts * certs; iVisited * visited; + int zoomPercent; int tabEnum; /* Preferences: */ iBool retainWindowSize; @@ -104,6 +106,7 @@ static iString *serializePrefs_App_(const iApp *d) { } appendFormat_String(str, "sidebar.mode arg:%d\n", mode_SidebarWidget(sidebar)); appendFormat_String(str, "uiscale arg:%f\n", uiScale_Window(d->window)); + appendFormat_String(str, "zoom.set arg:%d\n", d->zoomPercent); return str; } @@ -214,10 +217,12 @@ static void saveState_App_(const iApp *d) { static void init_App_(iApp *d, int argc, char **argv) { init_CommandLine(&d->args, argc, argv); init_SortedArray(&d->tickers, sizeof(iTicker), cmp_Ticker_); + d->commandEcho = checkArgument_CommandLine(&d->args, "echo") != NULL; d->running = iFalse; d->window = NULL; d->retainWindowSize = iTrue; d->pendingRefresh = iFalse; + d->zoomPercent = 100; d->certs = new_GmCerts(dataDir_App_); d->visited = new_Visited(); d->tabEnum = 0; @@ -336,6 +341,10 @@ iBool isRefreshPending_App(void) { return app_.pendingRefresh; } +int zoom_App(void) { + return app_.zoomPercent; +} + int run_App(int argc, char **argv) { init_App_(&app_, argc, argv); const int rc = run_App_(&app_); @@ -365,9 +374,9 @@ void postCommand_App(const char *command) { ev.user.data1 = strdup(command); ev.user.data2 = NULL; SDL_PushEvent(&ev); -#if !defined (NDEBUG) - printf("[command] %s\n", command); fflush(stdout); -#endif + if (app_.commandEcho) { + printf("[command] %s\n", command); fflush(stdout); + } } void postCommandf_App(const char *command, ...) { @@ -542,11 +551,10 @@ iBool handleCommand_App(const char *cmd) { SDL_PushEvent(&ev); } else if (equal_Command(cmd, "preferences")) { - iWindow *win = get_Window(); iWidget *dlg = makePreferences_Widget(); setToggle_Widget(findChild_Widget(dlg, "prefs.retainwindow"), d->retainWindowSize); setText_InputWidget(findChild_Widget(dlg, "prefs.uiscale"), - collectNewFormat_String("%g", uiScale_Window(get_Window()))); + collectNewFormat_String("%g", uiScale_Window(d->window))); setCommandHandler_Widget(dlg, handlePrefsCommands_); } else if (equal_Command(cmd, "restorewindow")) { @@ -559,8 +567,20 @@ iBool handleCommand_App(const char *cmd) { postCommand_App("open url:about:home"); return iTrue; } - else if (equal_Command(cmd, "font.setfactor")) { - setContentFontSize_Text((float) arg_Command(cmd) / 100.0f); + else if (equal_Command(cmd, "zoom.set")) { + d->zoomPercent = arg_Command(cmd); + setContentFontSize_Text((float) d->zoomPercent / 100.0f); + postCommand_App("font.changed"); + refresh_App(); + return iTrue; + } + else if (equal_Command(cmd, "zoom.delta")) { + int delta = arg_Command(cmd); + if (d->zoomPercent < 100 || (delta < 0 && d->zoomPercent == 100)) { + delta /= 2; + } + d->zoomPercent = iClamp(d->zoomPercent + delta, 50, 200); + setContentFontSize_Text((float) d->zoomPercent / 100.0f); postCommand_App("font.changed"); refresh_App(); return iTrue; diff --git a/src/app.h b/src/app.h index 12c636ba..cf68fb5f 100644 --- a/src/app.h +++ b/src/app.h @@ -28,6 +28,7 @@ void processEvents_App (enum iAppEventMode mode); iBool handleCommand_App (const char *cmd); void refresh_App (void); iBool isRefreshPending_App(void); +int zoom_App (void); iGmCerts * certs_App (void); iVisited * visited_App (void); diff --git a/src/gmrequest.c b/src/gmrequest.c index ce7a3522..996c77ea 100644 --- a/src/gmrequest.c +++ b/src/gmrequest.c @@ -287,7 +287,7 @@ void submit_GmRequest(iGmRequest *d) { " d\" YD d\" YD\n" " \"Y88888P' \"Y88888P'\n" "```\n" -"# A Beautiful Gemini Browser\n" +"# A Beautiful Gemini Client\n" "## Version " LAGRANGE_APP_VERSION "\n\n" "=> https://skyjake.fi/@jk by Jaakko Keränen \n" "Crafted with \u2615 in Finland\n"); diff --git a/src/ui/documentwidget.c b/src/ui/documentwidget.c index 11b92e63..ff1c577a 100644 --- a/src/ui/documentwidget.c +++ b/src/ui/documentwidget.c @@ -76,13 +76,11 @@ struct Impl_Model { /* state that persists across sessions */ iHistory *history; iString * url; - int textSizePercent; }; void init_Model(iModel *d) { d->history = new_History(); d->url = new_String(); - d->textSizePercent = 100; } void deinit_Model(iModel *d) { @@ -92,13 +90,13 @@ void deinit_Model(iModel *d) { void serialize_Model(const iModel *d, iStream *outs) { serialize_String(d->url, outs); - write16_Stream(outs, d->textSizePercent); + write16_Stream(outs, 0 /*d->zoomPercent*/); serialize_History(d->history, outs); } void deserialize_Model(iModel *d, iStream *ins) { deserialize_String(d->url, ins); - d->textSizePercent = read16_Stream(ins); + /*d->zoomPercent =*/ read16_Stream(ins); deserialize_History(d->history, ins); } @@ -204,7 +202,7 @@ static int documentWidth_DocumentWidget_(const iDocumentWidget *d) { const iWidget *w = constAs_Widget(d); const iRect bounds = bounds_Widget(w); return iMini(bounds.size.x - gap_UI * d->pageMargin * 2, - fontSize_UI * 38 * d->mod.textSizePercent / 100); /* TODO: Add user preference .*/ + fontSize_UI * 38 * zoom_App() / 100); /* TODO: Add user preference .*/ } static iRect documentBounds_DocumentWidget_(const iDocumentWidget *d) { @@ -559,7 +557,7 @@ static iBool updateFromHistory_DocumentWidget_(iDocumentWidget *d) { const iGmResponse *resp = recent->cachedResponse; d->state = fetching_RequestState; /* Use the cached response data. */ - d->scrollY = d->initialScrollY = recent->scrollY; + d->scrollY = d->initialScrollY = recent->scrollY * gap_UI; updateTrust_DocumentWidget_(d, resp); updateDocument_DocumentWidget_(d, resp); d->state = ready_RequestState; @@ -602,9 +600,8 @@ void setUrlFromCache_DocumentWidget(iDocumentWidget *d, const iString *url, iBoo iDocumentWidget *duplicate_DocumentWidget(const iDocumentWidget *orig) { iDocumentWidget *d = new_DocumentWidget(); delete_History(d->mod.history); - d->mod.textSizePercent = orig->mod.textSizePercent; - d->initialScrollY = orig->scrollY; - d->mod.history = copy_History(orig->mod.history); + d->initialScrollY = orig->scrollY; + d->mod.history = copy_History(orig->mod.history); setUrlFromCache_DocumentWidget(d, orig->mod.url, iTrue); return d; } @@ -811,20 +808,6 @@ static iBool handleMediaCommand_DocumentWidget_(iDocumentWidget *d, const char * return iFalse; } -static void changeTextSize_DocumentWidget_(iDocumentWidget *d, int delta) { - if (delta == 0) { - d->mod.textSizePercent = 100; - } - else { - if (d->mod.textSizePercent < 100 || (delta < 0 && d->mod.textSizePercent == 100)) { - delta /= 2; - } - d->mod.textSizePercent += delta; - d->mod.textSizePercent = iClamp(d->mod.textSizePercent, 50, 200); - } - postCommandf_App("font.setfactor arg:%d", d->mod.textSizePercent); -} - void updateSize_DocumentWidget(iDocumentWidget *d) { setWidth_GmDocument(d->doc, documentWidth_DocumentWidget_(d)); updateVisible_DocumentWidget_(d); @@ -1070,15 +1053,6 @@ static iBool processEvent_DocumentWidget_(iDocumentWidget *d, const SDL_Event *e case ' ': postCommand_Widget(w, "scroll.page arg:%d", key == SDLK_PAGEUP ? -1 : +1); return iTrue; - case SDLK_MINUS: - case SDLK_EQUALS: - case SDLK_0: - if (mods == KMOD_PRIMARY) { - changeTextSize_DocumentWidget_( - d, key == SDLK_EQUALS ? 10 : key == SDLK_MINUS ? -10 : 0); - return iTrue; - } - break; case SDLK_9: { iBlock *seed = new_Block(64); for (size_t i = 0; i < 64; ++i) { @@ -1107,7 +1081,7 @@ static iBool processEvent_DocumentWidget_(iDocumentWidget *d, const SDL_Event *e scroll_DocumentWidget_(d, -ev->wheel.y * get_Window()->pixelRatio); #else if (keyMods_Sym(SDL_GetModState()) == KMOD_PRIMARY) { - changeTextSize_DocumentWidget_(d, ev->wheel.y > 0 ? 10 : -10); + postCommandf_App("zoom.delta arg:%d", ev->wheel.y > 0 ? 10 : -10); return iTrue; } scroll_DocumentWidget_(d, -3 * ev->wheel.y * lineHeight_Text(default_FontId)); diff --git a/src/ui/sidebarwidget.c b/src/ui/sidebarwidget.c index a5e0e4ee..ca10f0da 100644 --- a/src/ui/sidebarwidget.c +++ b/src/ui/sidebarwidget.c @@ -333,6 +333,7 @@ static iBool processEvent_SidebarWidget_(iSidebarWidget *d, const SDL_Event *ev) return iTrue; } else if (equal_Command(cmd, "tabs.changed") || equal_Command(cmd, "document.changed")) { + d->scrollY = 0; updateItems_SidebarWidget_(d); } } diff --git a/src/ui/window.c b/src/ui/window.c index 599044aa..9c1b9bbe 100644 --- a/src/ui/window.c +++ b/src/ui/window.c @@ -74,6 +74,9 @@ static const iMenuItem navMenuItems[] = { { "Copy Source Text", SDLK_c, KMOD_PRIMARY, "copy" }, { "---", 0, 0, NULL }, { "Toggle Sidebar", SDLK_s, KMOD_PRIMARY | KMOD_ALT, "sidebar.toggle" }, + { "Zoom In", SDLK_EQUALS, KMOD_PRIMARY, "zoom.delta arg:10" }, + { "Zoom Out", SDLK_MINUS, KMOD_PRIMARY, "zoom.delta arg:-10" }, + { "Reset Zoom", SDLK_0, KMOD_PRIMARY, "zoom.set arg:100" }, { "---", 0, 0, NULL }, { "Preferences...", SDLK_COMMA, KMOD_PRIMARY, "preferences" }, { "---", 0, 0, NULL }, -- cgit v1.2.3