Skip to content

Build your personal knowledge base with Trilium Notes

persisting zoom setting in electron, fixes #112

azivner0f8f707acd1d
diff --git a/db/migrations/0097__add_zoomFactor.sql b/db/migrations/0097__add_zoomFactor.sql
new file mode 100644
index 0000000000..b371a3dc76
--- /dev/null
+++ b/db/migrations/0097__add_zoomFactor.sql
@@ -0,0 +1,2 @@
+INSERT INTO options (optionId, name, value, dateCreated, dateModified, isSynced)
+VALUES ('zoomFactor_key', 'zoomFactor', '1.0', '2018-06-01T03:35:55.041Z', '2018-06-01T03:35:55.041Z', 0);
\ No newline at end of file
diff --git a/src/public/javascripts/services/entrypoints.js b/src/public/javascripts/services/entrypoints.js
index 515ca64a91..ca4608d6c8 100644
--- a/src/public/javascripts/services/entrypoints.js
+++ b/src/public/javascripts/services/entrypoints.js
@@ -2,6 +2,7 @@ import utils from "./utils.js";
import treeService from "./tree.js";
import linkService from "./link.js";
import fileService from "./file.js";
+import zoomService from "./zoom.js";
import noteRevisionsDialog from "../dialogs/note_revisions.js";
import optionsDialog from "../dialogs/options.js";
import addLinkDialog from "../dialogs/add_link.js";
@@ -109,27 +110,10 @@ function registerEntrypoints() {
$("#note-detail-text").focus();
});
- $(document).bind('keydown', 'ctrl+-', () => {
- if (utils.isElectron()) {
- const webFrame = require('electron').webFrame;
-
- if (webFrame.getZoomFactor() > 0.2) {
- webFrame.setZoomFactor(webFrame.getZoomFactor() - 0.1);
- }
-
- return false;
- }
- });
-
- $(document).bind('keydown', 'ctrl+=', () => {
- if (utils.isElectron()) {
- const webFrame = require('electron').webFrame;
-
- webFrame.setZoomFactor(webFrame.getZoomFactor() + 0.1);
-
- return false;
- }
- });
+ if (utils.isElectron()) {
+ $(document).bind('keydown', 'ctrl+-', zoomService.decreaseZoomFactor);
+ $(document).bind('keydown', 'ctrl+=', zoomService.increaseZoomFactor);
+ }
$("#note-title").bind('keydown', 'return', () => $("#note-detail-text").focus());
diff --git a/src/public/javascripts/services/options_init.js b/src/public/javascripts/services/options_init.js
new file mode 100644
index 0000000000..02c60aecff
--- /dev/null
+++ b/src/public/javascripts/services/options_init.js
@@ -0,0 +1,9 @@
+import server from "./server.js";
+
+const optionsReady = new Promise((resolve, reject) => {
+ $(document).ready(() => server.get('options').then(resolve));
+});
+
+export default {
+ optionsReady
+}
\ No newline at end of file
diff --git a/src/public/javascripts/services/protected_session_holder.js b/src/public/javascripts/services/protected_session_holder.js
index b1d402e205..fd98aef671 100644
--- a/src/public/javascripts/services/protected_session_holder.js
+++ b/src/public/javascripts/services/protected_session_holder.js
@@ -1,13 +1,11 @@
import utils from "./utils.js";
-import server from "./server.js";
+import optionsInitService from './options_init.js';
let lastProtectedSessionOperationDate = null;
let protectedSessionTimeout = null;
let protectedSessionId = null;
-$(document).ready(() => {
- server.get('options').then(options => protectedSessionTimeout = options.protectedSessionTimeout);
-});
+optionsInitService.optionsReady.then(options => protectedSessionTimeout = options.protectedSessionTimeout);
setInterval(() => {
if (lastProtectedSessionOperationDate !== null && new Date().getTime() - lastProtectedSessionOperationDate.getTime() > protectedSessionTimeout * 1000) {
diff --git a/src/public/javascripts/services/zoom.js b/src/public/javascripts/services/zoom.js
new file mode 100644
index 0000000000..8a99b57375
--- /dev/null
+++ b/src/public/javascripts/services/zoom.js
@@ -0,0 +1,42 @@
+import server from "./server.js";
+import utils from "./utils.js";
+import optionsInitService from "./options_init.js";
+
+function decreaseZoomFactor() {
+ const webFrame = require('electron').webFrame;
+
+ if (webFrame.getZoomFactor() > 0.2) {
+ const webFrame = require('electron').webFrame;
+ const newZoomFactor = webFrame.getZoomFactor() - 0.1;
+
+ webFrame.setZoomFactor(newZoomFactor);
+
+ server.put('options/zoomFactor/' + newZoomFactor);
+ }
+}
+
+function increaseZoomFactor() {
+ const webFrame = require('electron').webFrame;
+ const newZoomFactor = webFrame.getZoomFactor() + 0.1;
+
+ webFrame.setZoomFactor(newZoomFactor);
+
+ server.put('options/zoomFactor/' + newZoomFactor);
+}
+
+function setZoomFactor(zoomFactor) {
+ zoomFactor = parseFloat(zoomFactor);
+
+ const webFrame = require('electron').webFrame;
+ webFrame.setZoomFactor(zoomFactor);
+}
+
+if (utils.isElectron()) {
+ optionsInitService.optionsReady.then(options => setZoomFactor(options.zoomFactor))
+}
+
+export default {
+ decreaseZoomFactor,
+ increaseZoomFactor,
+ setZoomFactor
+}
\ No newline at end of file
diff --git a/src/routes/api/options.js b/src/routes/api/options.js
index 4ce2da70af..9402a98a50 100644
--- a/src/routes/api/options.js
+++ b/src/routes/api/options.js
@@ -2,9 +2,10 @@
const sql = require('../../services/sql');
const optionService = require('../../services/options');
+const log = require('../../services/log');
// options allowed to be updated directly in options dialog
-const ALLOWED_OPTIONS = ['protectedSessionTimeout', 'noteRevisionSnapshotTimeInterval'];
+const ALLOWED_OPTIONS = ['protectedSessionTimeout', 'noteRevisionSnapshotTimeInterval', 'zoomFactor'];
async function getOptions() {
const options = await sql.getMap("SELECT name, value FROM options WHERE name IN ("
@@ -20,6 +21,8 @@ async function updateOption(req) {
return [400, "not allowed option to set"];
}
+ log.info(`Updating option ${name} to ${value}`);
+
await optionService.setOption(name, value);
}
diff --git a/src/services/app_info.js b/src/services/app_info.js
index 4511c75fbb..33c07ef118 100644
--- a/src/services/app_info.js
+++ b/src/services/app_info.js
@@ -3,7 +3,7 @@
const build = require('./build');
const packageJson = require('../../package');
-const APP_DB_VERSION = 96;
+const APP_DB_VERSION = 97;
module.exports = {
appVersion: packageJson.version,
diff --git a/src/services/options.js b/src/services/options.js
index 4214795d7d..8668a9e2f5 100644
--- a/src/services/options.js
+++ b/src/services/options.js
@@ -53,6 +53,8 @@ async function initOptions(startNotePath) {
await createOption('lastSyncedPull', appInfo.dbVersion, false);
await createOption('lastSyncedPush', 0, false);
+
+ await createOption('zoomFactor', 1.0, false);
}
module.exports = {