lbry-desktop/ui/util/zoomWindow.js
infiinte-persistence bc19503419 Allow zooming on Desktop
## Issue
Closes 4501 `Font size`

## New behavior
The Desktop app can now zoom the same way as browsers:
- Zoom In: "Ctrl+=" or "Ctrl+numpadPlus" or "Ctrl+WheelUp"
- Zoom Out: "Ctrl+-" or "Ctrl+numpadMinus" or "Ctrl+WheelDown"
- Zoom Reset: "Ctrl+0" or "Ctrl+numpad0"

## Code changes
(1) Electron provides this functionality through the `zoomIn|zoomOut|resetZoom` roles in the Menu, so it would have been a quick job.

However, given that Electron currently does not support having multiple accelerators for one item, we can't add `Ctrl+WheelUp` to the mix and would have to implement our own handler and use `webFrame`.

Given that we need to add code anyways, we handle both keyboard and mouse cases through the same handler, hence the existence of `zoomWindow.js`.  It also provides the opportunity to few a few quirks with Electron's default implementation (e.g. stuck at both extremes)

(2) I recall there is another Issue for adding keyboard shortcuts.  Given that these shortcuts are universally used in browsers, they are probably "reserved", so shouldn't clash with that task.
2020-07-14 16:08:24 -04:00

42 lines
1.1 KiB
JavaScript

import { webFrame } from 'electron';
const isDev = process.env.NODE_ENV !== 'production';
export const ZOOM = {
INCREMENT: 'INCREMENT',
DECREMENT: 'DECREMENT',
RESET: 'RESET',
};
function getNextZoomFactor(curFactor, isIncreasing) {
const zoomTable = [0.25, 0.33, 0.5, 0.67, 0.75, 0.8, 0.9, 1.0, 1.1, 1.25, 1.5, 1.75, 2.0, 2.5, 3.0];
curFactor = curFactor.toPrecision(3);
let i = zoomTable.length;
while (curFactor < zoomTable[--i]) {}
if (isIncreasing) {
return zoomTable[Math.min(zoomTable.length - 1, i + 1)];
} else {
return zoomTable[Math.max(0, i - 1)];
}
}
export function changeZoomFactor(action) {
const ZOOM_DFLT_FACTOR = 1.0;
const curFactor = webFrame.getZoomFactor();
switch (action) {
case ZOOM.INCREMENT:
webFrame.setZoomFactor(getNextZoomFactor(curFactor, true));
break;
case ZOOM.DECREMENT:
webFrame.setZoomFactor(getNextZoomFactor(curFactor, false));
break;
case ZOOM.RESET:
webFrame.setZoomFactor(ZOOM_DFLT_FACTOR);
break;
default:
if (isDev) throw new Error('changeZoomFactor: unexpected action');
break;
}
}