2020-02-06 05:08:45 +08:00
|
|
|
import options from "./options.js";
|
|
|
|
import Component from "../widgets/component.js";
|
2018-06-03 01:02:20 +08:00
|
|
|
|
2018-06-09 22:34:51 +08:00
|
|
|
const MIN_ZOOM = 0.5;
|
|
|
|
const MAX_ZOOM = 2.0;
|
2018-06-03 01:02:20 +08:00
|
|
|
|
2020-02-06 05:08:45 +08:00
|
|
|
export default class ZoomService extends Component {
|
2020-02-18 05:14:39 +08:00
|
|
|
constructor(parent) {
|
|
|
|
super(parent);
|
2018-06-03 01:02:20 +08:00
|
|
|
|
2020-02-06 05:08:45 +08:00
|
|
|
this.setZoomFactor(options.getFloat('zoomFactor'));
|
2018-06-09 22:34:51 +08:00
|
|
|
}
|
|
|
|
|
2020-02-06 05:08:45 +08:00
|
|
|
setZoomFactor(zoomFactor) {
|
|
|
|
zoomFactor = parseFloat(zoomFactor);
|
|
|
|
|
|
|
|
const webFrame = require('electron').webFrame;
|
|
|
|
webFrame.setZoomFactor(zoomFactor);
|
2018-06-09 22:34:51 +08:00
|
|
|
}
|
2020-02-06 05:08:45 +08:00
|
|
|
|
|
|
|
async setZoomFactorAndSave(zoomFactor) {
|
|
|
|
if (zoomFactor >= MIN_ZOOM && zoomFactor <= MAX_ZOOM) {
|
|
|
|
this.setZoomFactor(zoomFactor);
|
|
|
|
|
|
|
|
await options.save('zoomFactor', zoomFactor);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.log(`Zoom factor ${zoomFactor} outside of the range, ignored.`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getCurrentZoom() {
|
|
|
|
return require('electron').webFrame.getZoomFactor();
|
2018-06-09 22:34:51 +08:00
|
|
|
}
|
2018-06-09 11:18:53 +08:00
|
|
|
|
2020-02-17 02:23:49 +08:00
|
|
|
zoomOutEvent() {
|
2020-02-06 05:08:45 +08:00
|
|
|
this.setZoomFactorAndSave(this.getCurrentZoom() - 0.1);
|
|
|
|
}
|
2018-06-09 11:18:53 +08:00
|
|
|
|
2020-02-17 02:23:49 +08:00
|
|
|
zoomInEvent() {
|
2020-02-06 05:08:45 +08:00
|
|
|
this.setZoomFactorAndSave(this.getCurrentZoom() + 0.1);
|
|
|
|
}
|
2018-06-03 01:02:20 +08:00
|
|
|
|
2020-02-17 02:23:49 +08:00
|
|
|
setZoomFactorAndSaveEvent({zoomFactor}) {
|
2020-02-06 05:08:45 +08:00
|
|
|
this.setZoomFactorAndSave(zoomFactor);
|
|
|
|
}
|
2018-06-03 01:02:20 +08:00
|
|
|
}
|