Skip to content

Commit

Permalink
Added zoom level tooltip
Browse files Browse the repository at this point in the history
  • Loading branch information
ccz-2 committed Apr 4, 2020
1 parent abc20d5 commit df7de94
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 23 deletions.
76 changes: 56 additions & 20 deletions __init__.py
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
Expand All @@ -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:
Expand All @@ -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)
Expand All @@ -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':
Expand Down Expand Up @@ -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()
3 changes: 2 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
"deckBrowser_zoom": 1.0,
"deckBrowser_zoom_default": 1.0,
"scroll_sensitivity": 120,
"zoom_step": 1.1
"zoom_step": 1.1,
"zoom_lvl_tooltip_enabled": false
}
2 changes: 1 addition & 1 deletion config.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
### Anki Zoom v1.1.3 Config:
### Anki Zoom v1.1.4 Config:
All config is effective immediately. If having issues, please click "Restore Defaults".

- `deckBrowser_zoom_default`: Zoom level that the reset button applies for the deck browser. Must be >`0.2` and <`5`
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
"author": "Quip13",
"human_version": "v1.1.3",
"homepage": "https://ankiweb.net/shared/info/538879081",
"conflicts": [ "1846592880" ]
"conflicts": [ "1846592880", "Anki-Zoom_build" ]
}

0 comments on commit df7de94

Please sign in to comment.