Skip to content

Build your personal knowledge base with Trilium Notes

feat: Create a more seamless PWA top bar

qwreey4c0addd92980
diff --git a/apps/client/src/assets/manifest.webmanifest b/apps/client/src/assets/manifest.webmanifest
index b2cbca184c..1f8cb3b69a 100644
--- a/apps/client/src/assets/manifest.webmanifest
+++ b/apps/client/src/assets/manifest.webmanifest
@@ -7,6 +7,9 @@
"display": "standalone",
"scope": "/",
"start_url": "/",
+ "display_override": [
+ "window-controls-overlay"
+ ],
"icons": [
{
"src": "icon.png",
diff --git a/apps/client/src/desktop.ts b/apps/client/src/desktop.ts
index cf644cd3bf..6fa4286564 100644
--- a/apps/client/src/desktop.ts
+++ b/apps/client/src/desktop.ts
@@ -45,6 +45,10 @@ if (utils.isElectron()) {
electronContextMenu.setupContextMenu();
}
+if (utils.isPWA()) {
+ initPWATopbarColor();
+}
+
function initOnElectron() {
const electron: typeof Electron = utils.dynamicRequire("electron");
electron.ipcRenderer.on("globalShortcut", async (event, actionName) => appContext.triggerCommand(actionName));
@@ -113,3 +117,20 @@ function initDarkOrLightMode(style: CSSStyleDeclaration) {
const { nativeTheme } = utils.dynamicRequire("@electron/remote") as typeof ElectronRemote;
nativeTheme.themeSource = themeSource;
}
+
+function initPWATopbarColor() {
+ const tracker = $("#background-color-tracker");
+
+ if (tracker.length) {
+ const applyThemeColor = () => {
+ let meta = $("meta[name='theme-color']");
+ if (!meta.length) {
+ meta = $(`<meta name="theme-color">`).appendTo($("head"));
+ }
+ meta.attr("content", tracker.css("color"));
+ };
+
+ tracker.on("transitionend", applyThemeColor);
+ applyThemeColor();
+ }
+}
diff --git a/apps/client/src/services/utils.ts b/apps/client/src/services/utils.ts
index a8f4f567fe..3757fa939d 100644
--- a/apps/client/src/services/utils.ts
+++ b/apps/client/src/services/utils.ts
@@ -149,6 +149,18 @@ export function isElectron() {
return !!(window && window.process && window.process.type);
}
+/**
+ * Returns `true` if the client is running as a PWA, otherwise `false`.
+ */
+export function isPWA() {
+ return (
+ window.matchMedia('(display-mode: standalone)').matches
+ || window.matchMedia('(display-mode: window-controls-overlay)').matches
+ || window.navigator.standalone
+ || window.navigator.windowControlsOverlay
+ );
+}
+
export function isMac() {
return navigator.platform.indexOf("Mac") > -1;
}
@@ -891,6 +903,7 @@ export default {
localNowDateTime,
now,
isElectron,
+ isPWA,
isMac,
isCtrlKey,
assertArguments,
diff --git a/apps/server/src/assets/views/desktop.ejs b/apps/server/src/assets/views/desktop.ejs
index 374ed0b8cc..6ebe751aeb 100644
--- a/apps/server/src/assets/views/desktop.ejs
+++ b/apps/server/src/assets/views/desktop.ejs
@@ -29,6 +29,10 @@
}
</style>
+<!-- Required for match the PWA's top bar color with the theme -->
+<!-- This works even when the user directly changes --root-background in CSS -->
+<div id="background-color-tracker" style="position: absolute; visibility: hidden; color: var(--root-background); transition: color 1ms;"></div>
+
<!-- Required for correct loading of scripts in Electron -->
<script>if (typeof module === 'object') {window.module = module; module = undefined;}</script>