forked from ccz-2/Anki-Zoom
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
60 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# Anki Zoom | ||
# v1.1.3 3/28/2020 | ||
# v1.1.4 4/3/2020 | ||
# Copyright (c) 2020 Quip13 ([email protected]) | ||
# Based in part on code by Damien Elmes <[email protected]>, Roland Sieker <[email protected]> and github.com/krassowski | ||
# Big thanks to u/Glutanimate and u/yumenogotoshi for code suggestions | ||
|
@@ -13,17 +13,7 @@ | |
from anki.hooks import * | ||
from anki.lang import _ | ||
from .toolbar import * | ||
|
||
def configUpdated(*args): | ||
global scrl_threshold | ||
global zoom_step | ||
global config | ||
config = mw.addonManager.getConfig(__name__) #can be moved out of func for performance but config will not update automatically | ||
scrl_threshold = config[ 'scroll_sensitivity' ] | ||
zoom_step = config[ 'zoom_step' ] | ||
|
||
configUpdated() | ||
mw.addonManager.setConfigUpdatedAction(__name__, configUpdated) | ||
from aqt.utils import closeTooltip, tooltip, _tooltipLabel, _tooltipTimer | ||
|
||
def zoom_in(step=None): | ||
if not step: | ||
|
@@ -44,10 +34,39 @@ def reset_zoom(state=None, *args): | |
change_zoom( config[ 'overview_zoom_default' ] ) | ||
elif state == 'review': | ||
change_zoom( config[ 'review_zoom_default' ] ) | ||
showZoomLvl() | ||
|
||
def change_zoom_by(interval): | ||
currZoom = QWebEngineView.zoomFactor(mw.web) #use non-overridden method | ||
change_zoom(currZoom * interval) | ||
showZoomLvl() | ||
|
||
def showZoomLvl(duration = 1000): | ||
def getZoom(): | ||
currZoom = QWebEngineView.zoomFactor(mw.web) | ||
return str(round(currZoom*100)) | ||
def newToolTip(): | ||
tooltip(f'Zoom: {getZoom()}%',duration) | ||
# Tries to replace string in existing tooltip to prevent flickering when recreating | ||
def replaceTooltipTxt(label): | ||
txt = label.text() | ||
if "Zoom: " in txt: | ||
sI = txt.rfind("Zoom: ") + 6 | ||
eI = txt.rfind("%") | ||
txt = txt[:sI] + getZoom() + txt[eI:] | ||
label.setText(txt) | ||
aqt.utils._tooltipTimer.start(duration) | ||
return True | ||
else: | ||
newToolTip() | ||
return False | ||
|
||
if zoomLvlToggle.isChecked(): | ||
if not aqt.utils._tooltipLabel: | ||
newToolTip() | ||
else: | ||
replaceTooltipTxt(aqt.utils._tooltipLabel) | ||
|
||
|
||
def change_zoom(zoom_lvl): | ||
mw.web.setZoomFactor(zoom_lvl) | ||
|
@@ -56,6 +75,7 @@ def set_save_zoom(new_state, old_state, *args): | |
if old_state == new_state: #skips if reset | ||
return | ||
|
||
config = mw.addonManager.getConfig(__name__) | ||
#Saves state zoom | ||
old_state_zoom = QWebEngineView.zoomFactor(mw.web) | ||
if old_state == 'deckBrowser': | ||
|
@@ -107,27 +127,43 @@ def add_action(submenu, label, callback, shortcut=None): | |
submenu.addAction(action) | ||
return action | ||
|
||
def userAction(): | ||
config = mw.addonManager.getConfig(__name__) | ||
config['zoom_lvl_tooltip_enabled'] = zoomLvlToggle.isChecked() | ||
mw.addonManager.writeConfig(__name__, config) | ||
|
||
def setupConfig(*args): | ||
global scrl_threshold | ||
global zoom_step | ||
global config | ||
config = mw.addonManager.getConfig(__name__) | ||
scrl_threshold = config[ 'scroll_sensitivity' ] | ||
zoom_step = config[ 'zoom_step' ] | ||
zoomLvlToggle.setChecked(config['zoom_lvl_tooltip_enabled']) | ||
|
||
def setup_menu(): | ||
try: | ||
global zoomLvlToggle | ||
mw.addon_view_menu = getMenu(mw, "&View") | ||
mw.form.menubar.insertMenu( | ||
mw.form.menuTools.menuAction(), | ||
mw.addon_view_menu | ||
except AttributeError: | ||
mw.addon_view_menu = getMenu(mw, "&View") | ||
mw.form.menubar.insertMenu( | ||
mw.form.menuTools.menuAction(), | ||
mw.addon_view_menu | ||
) | ||
) | ||
|
||
mw.zoom_submenu = QMenu(_('&Zoom'), mw) | ||
mw.addon_view_menu.addMenu(mw.zoom_submenu) | ||
|
||
add_action(mw.zoom_submenu, 'Zoom &In', zoom_in, ['Ctrl++','Ctrl+=']) | ||
add_action(mw.zoom_submenu, 'Zoom &Out', zoom_out, ['Ctrl+-']) | ||
mw.zoom_submenu.addSeparator() | ||
add_action(mw.zoom_submenu, '&Reset', reset_zoom, ['Ctrl+0']) | ||
mw.zoom_submenu.addSeparator() | ||
zoomLvlToggle = add_action(mw.zoom_submenu, 'Show Zoom Level', userAction , ['']) | ||
zoomLvlToggle.setCheckable(True) | ||
setupConfig() | ||
|
||
def onClose(): | ||
set_save_zoom(None,mw.state) | ||
|
||
addHook("afterStateChange", set_save_zoom) | ||
addHook("unloadProfile", onClose) | ||
mw.addonManager.setConfigUpdatedAction(__name__, setupConfig) | ||
setup_menu() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters