From ad5a094fb5d87cb464ab9607a6764efb84802dc0 Mon Sep 17 00:00:00 2001 From: gdh1995 Date: Sun, 27 Oct 2019 01:52:03 +0800 Subject: [PATCH] add commands: zoomIn + zoomOut --- README.md | 2 ++ _locales/en/messages.json | 6 ++++++ _locales/zh/messages.json | 6 ++++++ background/commands.ts | 4 +++- background/help_dialog.ts | 2 +- background/main.ts | 38 +++++++++++++++++++++++++++++++++++++- types/messages.d.ts | 2 +- types/vimium_c.d.ts | 1 + 8 files changed, 57 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index d6eb11593..9d117da46 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,8 @@ __Other extensions supporting Vimium C:__ * Chrome: add a feature to hook in-page "access keys" and enable it by default * Firefox: support 63+ and more conditions * VHints: Tab: now only switch visibility in limited ranges +* add `zoomIn` and `zoomOut` commands, and use a huge count like `1111` to reset zoom + ([#83](https://github.com/gdh1995/vimium-c/issues/83), [philc#2978](https://github.com/philc/vimium/pull/2978)) * fix some old bugs 1.78.3 diff --git a/_locales/en/messages.json b/_locales/en/messages.json index 348fc97d5..fe9317667 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -473,6 +473,12 @@ "visitPreviousTab": { "message": "Go to previously-visited tab on current window" }, + "zoomIn": { + "message": "Make a current tab zoom in" + }, + "zoomOut": { + "message": "Make a current tab zoom out" + }, "createTab_s": { "message": "Create new tab" }, diff --git a/_locales/zh/messages.json b/_locales/zh/messages.json index 2884a43ad..0fa08c31b 100644 --- a/_locales/zh/messages.json +++ b/_locales/zh/messages.json @@ -347,6 +347,12 @@ "visitPreviousTab": { "message": "切换到最近访问的上一个标签页" }, + "zoomIn": { + "message": "放大网页" + }, + "zoomOut": { + "message": "缩小网页" + }, "createTab_s": { "message": "打开新的标签页" }, diff --git a/background/commands.ts b/background/commands.ts index 27200a305..36553d37c 100644 --- a/background/commands.ts +++ b/background/commands.ts @@ -479,7 +479,9 @@ availableCommands_: { __proto__: null as never, toggleVomnibarStyle: [ kBgCmd.toggleVomnibarStyle, 1, 1, { style: "dark" } ], closeDownloadBar: [ kBgCmd.moveTabToNewWindow, 1, 1, { all: 1 } ], showTip: [ kBgCmd.showTip, 1, 1 ], - visitPreviousTab: [ kBgCmd.visitPreviousTab, 1, 0 ] + visitPreviousTab: [ kBgCmd.visitPreviousTab, 1, 0 ], + zoomIn: [ kBgCmd.toggleZoom, 1, 0 ], + zoomOut: [ kBgCmd.toggleZoom, 1, 0, { count: -1 } ], } as ReadonlySafeDict }, CommandsData_: CommandsDataTy = CommandsData_ as never || { diff --git a/background/help_dialog.ts b/background/help_dialog.ts index 8a3ae4fa1..691351146 100644 --- a/background/help_dialog.ts +++ b/background/help_dialog.ts @@ -210,7 +210,7 @@ var HelpDialog = { , "scrollToBottom", "scrollToLeft", "scrollToRight", "scrollPageDown", "scrollPageUp" , "scrollPxDown", "scrollPxUp", "scrollPxLeft", "scrollPxRight" , "scrollFullPageDown", "scrollFullPageUp", "reload", "reloadTab", "reloadGivenTab" - , "toggleViewSource" + , "zoomIn", "zoomOut", "toggleViewSource" , "copyCurrentUrl", "copyCurrentTitle", "switchFocus", "simBackspace" , "LinkHints.activateModeToCopyLinkUrl", "LinkHints.activateModeToCopyLinkText" , "openCopiedUrlInCurrentTab", "openCopiedUrlInNewTab", "goUp", "goToRoot" diff --git a/background/main.ts b/background/main.ts index 2f2c1d979..93f5be759 100644 --- a/background/main.ts +++ b/background/main.ts @@ -970,7 +970,8 @@ UseTab.CurWndTabs, UseTab.NoTab, UseTab.CurWndTabs, UseTab.NoTab, UseTab.ActiveTab, UseTab.ActiveTab, UseTab.NoTab, UseTab.CurWndTabs, UseTab.NoTab, UseTab.CurWndTabs, UseTab.NoTab, UseTab.NoTab, - UseTab.ActiveTab, UseTab.NoTab, UseTab.ActiveTab, UseTab.ActiveTab, UseTab.NoTab, UseTab.NoTab + UseTab.ActiveTab, UseTab.NoTab, UseTab.ActiveTab, UseTab.ActiveTab, UseTab.NoTab, UseTab.NoTab, + UseTab.NoTab ], BackgroundCommands: { [K in kBgCmd & number]: @@ -1882,6 +1883,41 @@ keyword: cOptions.keyword, }); BackgroundCommands[kBgCmd.openUrl](); + }, + /* kBgCmd.toggleZoom: */ function (this: void): void { + if (Build.BTypes & BrowserType.Edge && (!(Build.BTypes & ~BrowserType.Edge) || OnOther === BrowserType.Edge)) { + Backend_.complain_("control zoom settings of tabs"); + return; + } + if (Build.BTypes & BrowserType.Chrome && Build.MinCVer < BrowserVer.Min$Tabs$$setZoom + && CurCVer_ < BrowserVer.Min$Tabs$$setZoom) { + Backend_.showHUD_(`Vimium C can not control zoom settings before Chrome ${BrowserVer.Min$Tabs$$setZoom}`); + return; + } + chrome.tabs.getZoom(curZoom => { + if (!curZoom) { return onRuntimeError(); } + cRepeat < -4 && (cRepeat = -cRepeat); + let newZoom = curZoom, m = Math; + if (cRepeat > 4) { + newZoom = cRepeat / (cRepeat > 1000 ? cRepeat : cRepeat > 49 ? 100 : 10); + newZoom = !(Build.BTypes & ~BrowserType.Firefox) + || Build.BTypes & BrowserType.Firefox && OnOther === BrowserType.Firefox + ? m.max(0.3, m.min(newZoom, 3)) : m.max(0.25, m.min(newZoom, 5)); + } else { + let nearest = 0, delta = 9, + steps = !(Build.BTypes & ~BrowserType.Firefox) + || Build.BTypes & BrowserType.Firefox && OnOther === BrowserType.Firefox + ? [0.3, 0.5, 0.67, 0.8, 0.9, 1, 1.1, 1.2, 1.33, 1.5, 1.7, 2, 2.4, 3] + : [0.25, 1 / 3, 0.5, 2 / 3, 0.75, 0.8, 0.9, 1, 1.1, 1.25, 1.5, 1.75, 2, 2.5, 3, 4, 5]; + for (let ind = 0, d2 = 0; ind < steps.length && (d2 = m.abs(steps[ind] - curZoom)) < delta; ind++) { + nearest = ind; delta = d2; + } + newZoom = steps[nearest + cRepeat < 0 ? 0 : m.min(nearest + cRepeat, steps.length - 1)]; + } + if (m.abs(newZoom - curZoom) > 0.005) { + chrome.tabs.setZoom(newZoom); + } + }); } ], numHeadRe = /^-?\d+|^-/; diff --git a/types/messages.d.ts b/types/messages.d.ts index e123d9c87..2f5dd5083 100644 --- a/types/messages.d.ts +++ b/types/messages.d.ts @@ -189,7 +189,7 @@ declare const enum kBgCmd { goToRoot, goUp, moveTab, mainFrame, visitPreviousTab, copyTabInfo, clearFindHistory, toggleViewSource, clearMarks, toggleVomnibarStyle, - goBackFallback, showTip, autoOpenFallback, + goBackFallback, showTip, autoOpenFallback, toggleZoom, END = "END", } diff --git a/types/vimium_c.d.ts b/types/vimium_c.d.ts index dbffc2688..01f15083f 100644 --- a/types/vimium_c.d.ts +++ b/types/vimium_c.d.ts @@ -563,6 +563,7 @@ declare const enum BrowserVer { // before 42, event.path is a simple NodeList instance ; even if EXPERIMENTAL or LEGACY Min$Event$$path$IsStdArrayAndIncludesWindow = 42, Min$Tabs$$getZoom = 42, + Min$Tabs$$setZoom = 42, Min$EnableSitePerProcess$Flag = 42, MinParentNodeGetterInNodePrototype = 42, // also .childNodes; even if even if EXPERIMENTAL or LEGACY MinEnsured$fetch = 42, // even if LEGACY; can not fetch chrome-extension:// before C47