Skip to content

Build your personal knowledge base with Trilium Notes

fix(desktop): honour disabled smooth scrolling on startup

Elian Doran21331ba0fcb0
diff --git a/apps/desktop/src/main.spec.ts b/apps/desktop/src/main.spec.ts
index 8929b9678d..715f5086e9 100644
--- a/apps/desktop/src/main.spec.ts
+++ b/apps/desktop/src/main.spec.ts
@@ -1,4 +1,5 @@
import { join as pathJoin } from "node:path";
+
import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from "vitest";
type Handler = (...args: unknown[]) => unknown;
@@ -51,6 +52,8 @@ const h = vi.hoisted(() => ({
isPrimaryInstance: true as boolean,
allWindows: [] as unknown[],
smoothScroll: "true" as string | null,
+ // When true, reading an option throws (simulates first run before the schema exists).
+ dbUninitialized: false as boolean,
isDbInitialized: true as boolean,
securitySettings: {} as SecuritySettings,
lastFocusedWindow: null as FakeWindow | null,
@@ -139,8 +142,10 @@ vi.mock("@triliumnext/core", async (importOriginal) => {
return Promise.resolve();
}),
options: {
+ // NB: smoothScrollEnabled is deliberately NOT served here — it is read before
+ // core init straight from the shared provider (see the sql_provider mock), so
+ // getOptionOrNull() returns null for it at that point, as in production.
getOptionOrNull: (key: string) => {
- if (key === "smoothScrollEnabled") return h.smoothScroll;
if (key === "locale") return h.locale;
if (key === "formattingLocale") return h.formattingLocale;
return null;
@@ -171,8 +176,22 @@ vi.mock("@triliumnext/server/src/core_assets.js", () => ({ loadCoreSchema: vi.fn
vi.mock("@triliumnext/server/src/crypto_provider.js", () => ({ default: class {} }));
vi.mock("@triliumnext/server/src/in_app_help_provider.js", () => ({ default: class {} }));
vi.mock("@triliumnext/server/src/log_provider.js", () => ({ default: class {} }));
+// readDbOption() reads the pre-`ready` switch options from this shared provider via
+// prepare().pluck().get(name). Back it with h.smoothScroll, and let h.dbUninitialized
+// simulate a first run where the schema (and so the options table) does not exist yet.
vi.mock("@triliumnext/server/src/sql_provider.js", () => ({
- default: class { loadFromFile = vi.fn(); }
+ default: class {
+ loadFromFile = vi.fn();
+ prepare() {
+ if (h.dbUninitialized) throw new Error("no such table: options");
+ return {
+ pluck: () => ({
+ get: (name: string) =>
+ (name === "smoothScrollEnabled" && h.smoothScroll !== null ? h.smoothScroll : undefined)
+ })
+ };
+ }
+ }
}));
vi.mock("@triliumnext/server/src/zip_provider.js", () => ({ default: class {} }));
@@ -244,6 +263,7 @@ function resetState() {
h.isPrimaryInstance = true;
h.allWindows = [];
h.smoothScroll = "true";
+ h.dbUninitialized = false;
h.isDbInitialized = true;
h.securitySettings = {};
h.lastFocusedWindow = null;
@@ -354,6 +374,37 @@ describe("main() bootstrap", () => {
expect(h.setName).not.toHaveBeenCalled();
});
+ // Regression for #10559: the switch is applied before core init, when
+ // options.getOptionOrNull() still returns null, so the disabled state must be
+ // read straight from the shared provider (sql_provider mock) — not core options.
+ it("appends the smooth-scroll switch from the shared provider even though core options are unavailable", async () => {
+ setPlatform("darwin");
+ h.smoothScroll = "false";
+ const { main } = await importMain();
+ await main();
+ const switches = h.appendSwitch.mock.calls.map((c) => c[0]);
+ expect(switches).toContain("disable-smooth-scrolling");
+ });
+
+ it("skips the smooth-scroll switch when the database has no schema yet (first run)", async () => {
+ setPlatform("darwin");
+ h.smoothScroll = "false";
+ h.dbUninitialized = true;
+ const { main } = await importMain();
+ await main();
+ const switches = h.appendSwitch.mock.calls.map((c) => c[0]);
+ expect(switches).not.toContain("disable-smooth-scrolling");
+ });
+
+ it("skips the smooth-scroll switch when the option row is absent from the DB", async () => {
+ setPlatform("darwin");
+ h.smoothScroll = null;
+ const { main } = await importMain();
+ await main();
+ const switches = h.appendSwitch.mock.calls.map((c) => c[0]);
+ expect(switches).not.toContain("disable-smooth-scrolling");
+ });
+
it("exits when it is not the primary instance", async () => {
h.isPrimaryInstance = false;
const { main } = await importMain();
diff --git a/apps/desktop/src/main.ts b/apps/desktop/src/main.ts
index ccdf60b9af..64813a7666 100644
--- a/apps/desktop/src/main.ts
+++ b/apps/desktop/src/main.ts
@@ -5,11 +5,10 @@ import { loadCoreSchema } from "@triliumnext/server/src/core_assets.js";
import NodejsCryptoProvider from "@triliumnext/server/src/crypto_provider.js";
import NodejsInAppHelpProvider from "@triliumnext/server/src/in_app_help_provider.js";
import ServerLogService from "@triliumnext/server/src/log_provider.js";
+import config from "@triliumnext/server/src/services/config.js";
import dataDirs from "@triliumnext/server/src/services/data_dir.js";
import port from "@triliumnext/server/src/services/port.js";
-import ElectronRequestProvider from "./services/request";
import { RESOURCE_DIR } from "@triliumnext/server/src/services/resource_dir.js";
-import windowService, { setupWindowing } from "./services/window";
import BetterSqlite3Provider from "@triliumnext/server/src/sql_provider.js";
import NodejsZipProvider from "@triliumnext/server/src/zip_provider.js";
import { app, BrowserWindow,globalShortcut } from "electron";
@@ -32,10 +31,12 @@ import { setupExportHandlers } from "./services/export";
import { setupImportHandlers } from "./services/import";
import { setupOneNoteHandlers } from "./services/onenote";
import { setupPrintingHandlers } from "./services/printing";
+import ElectronRequestProvider from "./services/request";
import { getSecuritySettings, registerSecurityIpcHandlers } from "./services/security_settings";
import { setupShellHandlers } from "./services/shell";
import { markStartupMetric, setupStartupMetricsIpc } from "./services/startup_metrics";
import { setupSystemTray } from "./services/tray";
+import windowService, { setupWindowing } from "./services/window";
export async function main() {
markStartupMetric("main-process-start");
@@ -90,12 +91,6 @@ export async function main() {
app.commandLine.appendSwitch("disable-http-cache");
}
- // Disable smooth scroll if the option is set
- const smoothScrollEnabled = options.getOptionOrNull("smoothScrollEnabled");
- if (smoothScrollEnabled === "false") {
- app.commandLine.appendSwitch("disable-smooth-scrolling");
- }
-
if (process.platform === "linux") {
app.setName(PRODUCT_NAME);
@@ -155,7 +150,11 @@ export async function main() {
// await initializeTranslations();
- const isPrimaryInstance = (await import("electron")).app.requestSingleInstanceLock();
+ // Synchronous: `app` and `config`/`dataDirs` are all statically imported, so nothing
+ // between the top of main() and the database open below awaits. That keeps the whole
+ // prologue — including the pre-`ready` Chromium switches — running before Electron can
+ // emit `ready`, while still letting us open the database and read options from it first.
+ const isPrimaryInstance = app.requestSingleInstanceLock();
if (!isPrimaryInstance) {
console.info(t("desktop.instance_already_running"));
process.exit(0);
@@ -164,9 +163,6 @@ export async function main() {
// this is to disable electron warning spam in the dev console (local development only)
process.env["ELECTRON_DISABLE_SECURITY_WARNINGS"] = "true";
- const { DOCUMENT_PATH } = (await import("@triliumnext/server/src/services/data_dir.js")).default;
- const config = (await import("@triliumnext/server/src/services/config.js")).default;
-
// Override scripting config from security.json (lives outside the DB for tamper resistance)
const securitySettings = getSecuritySettings();
if (securitySettings.backendScriptingEnabled !== undefined) {
@@ -182,9 +178,18 @@ export async function main() {
}
const dbProvider = new BetterSqlite3Provider();
- dbProvider.loadFromFile(DOCUMENT_PATH, config.General.readOnly);
+ dbProvider.loadFromFile(dataDirs.DOCUMENT_PATH, config.General.readOnly);
markStartupMetric("database-opened");
+ // Disable smooth scrolling if the option is off. This Chromium switch must be applied
+ // before `ready`; the prologue above is await-free, so `ready` cannot have fired yet.
+ // The value is read straight from the provider we just opened (and reuse for the whole
+ // app below) because the core options service isn't wired up until initializeCore() runs
+ // — reading via options.getOptionOrNull() here would always return null (#10559).
+ if (readDbOption(dbProvider, "smoothScrollEnabled") === "false") {
+ app.commandLine.appendSwitch("disable-smooth-scrolling");
+ }
+
// The IPC provider just registers an `ipcMain.on` listener; no TCP socket
// or session parser needed, so we can init it here (before startTriliumServer)
// instead of going through www.ts. www.ts then only knows about the
@@ -255,6 +260,24 @@ export async function main() {
}
}
+/**
+ * Reads a single option value from an already-open database provider, synchronously.
+ *
+ * Needed for the handful of settings consulted before `app.ready` (e.g. the
+ * `--disable-smooth-scrolling` Chromium switch): at that point initializeCore() has not
+ * run and the core options service is not wired up, so options.getOptionOrNull() always
+ * returns null — which silently dropped a persisted "false" (#10559). Returns null (the
+ * safe default) when the database has no schema yet, e.g. on the very first run.
+ */
+function readDbOption(provider: BetterSqlite3Provider, name: string): string | null {
+ try {
+ const value = provider.prepare("SELECT value FROM options WHERE name = ?").pluck().get(name);
+ return typeof value === "string" ? value : null;
+ } catch {
+ return null;
+ }
+}
+
/**
* Returns a unique user data directory for Electron so that single instance locks between legitimately different instances such as different port or data directory can still act independently, but we are focusing the main window otherwise.
*