trilium/src/public/javascripts/services/zoom.js

51 lines
1.2 KiB
JavaScript
Raw Normal View History

import server from "./server.js";
import utils from "./utils.js";
import optionsService from "./options.js";
2018-06-09 22:34:51 +08:00
const MIN_ZOOM = 0.5;
const MAX_ZOOM = 2.0;
2018-06-09 22:34:51 +08:00
async function decreaseZoomFactor() {
await setZoomFactorAndSave(getCurrentZoom() - 0.1);
}
2018-06-09 22:34:51 +08:00
async function increaseZoomFactor() {
await setZoomFactorAndSave(getCurrentZoom() + 0.1);
}
function setZoomFactor(zoomFactor) {
zoomFactor = parseFloat(zoomFactor);
const webFrame = require('electron').webFrame;
webFrame.setZoomFactor(zoomFactor);
}
async function setZoomFactorAndSave(zoomFactor) {
2018-06-09 22:34:51 +08:00
if (!utils.isElectron()) {
return;
}
if (zoomFactor >= MIN_ZOOM && zoomFactor <= MAX_ZOOM) {
setZoomFactor(zoomFactor);
await server.put('options/zoomFactor/' + zoomFactor);
}
else {
console.log(`Zoom factor ${zoomFactor} outside of the range, ignored.`);
}
}
2018-06-09 22:34:51 +08:00
function getCurrentZoom() {
return require('electron').webFrame.getZoomFactor();
}
if (utils.isElectron()) {
optionsService.addLoadListener(options => setZoomFactor(options.getFloat('zoomFactor')))
}
export default {
decreaseZoomFactor,
increaseZoomFactor,
setZoomFactor,
setZoomFactorAndSave
}