Skip to content

Build your personal knowledge base with Trilium Notes

client/launch bar: implement a color scheme switcher widget

Adorian Doranca7c6e2ab7f4
diff --git a/apps/client/src/services/color_scheme.spec.ts b/apps/client/src/services/color_scheme.spec.ts
new file mode 100644
index 0000000000..a887974680
--- /dev/null
+++ b/apps/client/src/services/color_scheme.spec.ts
@@ -0,0 +1,37 @@
+import { describe, expect, it } from "vitest";
+
+import { getNextColorSchemeTheme, resolveColorScheme } from "./color_scheme.js";
+
+describe("resolveColorScheme", () => {
+ it("resolves each built-in theme to its family and scheme", () => {
+ expect(resolveColorScheme("next")).toEqual({ family: "modern", scheme: "system", isCustom: false });
+ expect(resolveColorScheme("next-light")).toEqual({ family: "modern", scheme: "light", isCustom: false });
+ expect(resolveColorScheme("next-dark")).toEqual({ family: "modern", scheme: "dark", isCustom: false });
+ expect(resolveColorScheme("auto")).toEqual({ family: "legacy", scheme: "system", isCustom: false });
+ expect(resolveColorScheme("light")).toEqual({ family: "legacy", scheme: "light", isCustom: false });
+ expect(resolveColorScheme("dark")).toEqual({ family: "legacy", scheme: "dark", isCustom: false });
+ });
+
+ it("treats unknown, empty and nullish themes as custom", () => {
+ for (const theme of [ "my-theme", "", null, undefined ]) {
+ expect(resolveColorScheme(theme)).toEqual({ family: null, scheme: "system", isCustom: true });
+ }
+ });
+});
+
+describe("getNextColorSchemeTheme", () => {
+ it("cycles system → light → dark → system while keeping the family", () => {
+ expect(getNextColorSchemeTheme("next")).toBe("next-light");
+ expect(getNextColorSchemeTheme("next-light")).toBe("next-dark");
+ expect(getNextColorSchemeTheme("next-dark")).toBe("next");
+
+ expect(getNextColorSchemeTheme("auto")).toBe("light");
+ expect(getNextColorSchemeTheme("light")).toBe("dark");
+ expect(getNextColorSchemeTheme("dark")).toBe("auto");
+ });
+
+ it("returns null for a custom theme that cannot switch color scheme", () => {
+ expect(getNextColorSchemeTheme("my-theme")).toBeNull();
+ expect(getNextColorSchemeTheme(null)).toBeNull();
+ });
+});
diff --git a/apps/client/src/services/color_scheme.ts b/apps/client/src/services/color_scheme.ts
new file mode 100644
index 0000000000..e4492a17dc
--- /dev/null
+++ b/apps/client/src/services/color_scheme.ts
@@ -0,0 +1,41 @@
+export type ColorScheme = "system" | "light" | "dark";
+
+/** Order the color-scheme switcher cycles through on each click. */
+export const COLOR_SCHEME_CYCLE: readonly ColorScheme[] = [ "system", "light", "dark" ];
+
+/** Maps each theme family and color scheme to the concrete `theme` option value. */
+export const THEME_FAMILY_SCHEMES: Record<string, Record<ColorScheme, string>> = {
+ modern: { system: "next", light: "next-light", dark: "next-dark" },
+ legacy: { system: "auto", light: "light", dark: "dark" }
+};
+
+export interface ResolvedColorScheme {
+ /** The theme family key, or `null` for a custom theme that has no color-scheme variants. */
+ family: string | null;
+ scheme: ColorScheme;
+ isCustom: boolean;
+}
+
+export function resolveColorScheme(theme: string | null | undefined): ResolvedColorScheme {
+ for (const [ family, schemes ] of Object.entries(THEME_FAMILY_SCHEMES)) {
+ for (const [ scheme, value ] of Object.entries(schemes)) {
+ if (value === theme) {
+ return { family, scheme: scheme as ColorScheme, isCustom: false };
+ }
+ }
+ }
+ return { family: null, scheme: "system", isCustom: true };
+}
+
+/**
+ * Returns the `theme` value for the next color scheme in the cycle, keeping the current family.
+ * Returns `null` when the theme is custom and therefore has no color-scheme variants to cycle through.
+ */
+export function getNextColorSchemeTheme(theme: string | null | undefined): string | null {
+ const { family, scheme } = resolveColorScheme(theme);
+ if (!family) {
+ return null;
+ }
+ const nextScheme = COLOR_SCHEME_CYCLE[(COLOR_SCHEME_CYCLE.indexOf(scheme) + 1) % COLOR_SCHEME_CYCLE.length];
+ return THEME_FAMILY_SCHEMES[family][nextScheme];
+}
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 6658512bad..25da6619d9 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -1505,6 +1505,11 @@
"color_scheme_light": "Light",
"color_scheme_dark": "Dark",
"color_scheme_custom_disabled": "Color scheme override is not available for custom themes",
+ "color_scheme_switcher_system": "System mode",
+ "color_scheme_switcher_light": "Light mode",
+ "color_scheme_switcher_dark": "Dark mode",
+ "color_scheme_switcher_hint": "Click to switch the color scheme.",
+ "color_scheme_switcher_unsupported": "The current theme doesn't support changing the color scheme.",
"layout": "Layout",
"layout-vertical-title": "Vertical",
"layout-horizontal-title": "Horizontal",
diff --git a/apps/client/src/widgets/launch_bar/ColorSchemeSwitcher.css b/apps/client/src/widgets/launch_bar/ColorSchemeSwitcher.css
new file mode 100644
index 0000000000..5674eb303f
--- /dev/null
+++ b/apps/client/src/widgets/launch_bar/ColorSchemeSwitcher.css
@@ -0,0 +1,22 @@
+.launcher-button.color-scheme-switcher {
+ &.unsupported {
+ opacity: 0.5;
+ cursor: default;
+ }
+}
+
+.tooltip.color-scheme-switcher-tooltip {
+ .tooltip-inner {
+ text-align: center;
+ }
+
+ .mode {
+ display: block;
+ padding-bottom: 4px;
+ font-weight: 600;
+ }
+
+ .hint {
+ opacity: 0.75;
+ }
+}
diff --git a/apps/client/src/widgets/launch_bar/ColorSchemeSwitcher.tsx b/apps/client/src/widgets/launch_bar/ColorSchemeSwitcher.tsx
new file mode 100644
index 0000000000..c8b42ea3d0
--- /dev/null
+++ b/apps/client/src/widgets/launch_bar/ColorSchemeSwitcher.tsx
@@ -0,0 +1,44 @@
+import "./ColorSchemeSwitcher.css";
+
+import { ColorScheme, getNextColorSchemeTheme, resolveColorScheme } from "../../services/color_scheme";
+import { t } from "../../services/i18n";
+import { useTriliumOption } from "../react/hooks";
+import { LaunchBarActionButton, LauncherNoteProps } from "./launch_bar_widgets";
+
+const SCHEME_ICONS: Record<ColorScheme, string> = {
+ system: "bx bx-brightness-half",
+ light: "bx bx-sun",
+ dark: "bx bx-moon"
+};
+
+const SCHEME_LABELS: Record<ColorScheme, string> = {
+ system: t("theme.color_scheme_switcher_system"),
+ light: t("theme.color_scheme_switcher_light"),
+ dark: t("theme.color_scheme_switcher_dark")
+};
+
+export default function ColorSchemeSwitcher({ launcherNote }: LauncherNoteProps) {
+ const [ theme, setTheme ] = useTriliumOption("theme");
+ const { scheme, isCustom } = resolveColorScheme(theme);
+
+ const tooltip = isCustom
+ ? t("theme.color_scheme_switcher_unsupported")
+ : `<span class="mode">${SCHEME_LABELS[scheme]}</span><span class="hint">${t("theme.color_scheme_switcher_hint")}</span>`;
+
+ return (
+ <LaunchBarActionButton
+ launcherNote={launcherNote}
+ className={`color-scheme-switcher${isCustom ? " unsupported" : ""}`}
+ icon={SCHEME_ICONS[scheme]}
+ text={tooltip}
+ tooltipHtml
+ tooltipClass="color-scheme-switcher-tooltip"
+ onClick={() => {
+ const nextTheme = getNextColorSchemeTheme(theme);
+ if (nextTheme) {
+ void setTheme(nextTheme);
+ }
+ }}
+ />
+ );
+}
diff --git a/apps/client/src/widgets/launch_bar/LauncherContainer.tsx b/apps/client/src/widgets/launch_bar/LauncherContainer.tsx
index dd3ae9d6e3..4d7007b559 100644
--- a/apps/client/src/widgets/launch_bar/LauncherContainer.tsx
+++ b/apps/client/src/widgets/launch_bar/LauncherContainer.tsx
@@ -9,6 +9,7 @@ import { useTriliumEvent } from "../react/hooks";
import { onWheelHorizontalScroll } from "../widget_utils";
import BookmarkButtons from "./BookmarkButtons";
import CalendarWidget from "./CalendarWidget";
+import ColorSchemeSwitcher from "./ColorSchemeSwitcher";
import HistoryNavigationButton from "./HistoryNavigation";
import { LaunchBarContext } from "./launch_bar_widgets";
import { CommandButton, CustomWidget, NoteLauncher, QuickSearchLauncherWidget, ScriptLauncher, TodayLauncher } from "./LauncherDefinitions";
@@ -102,6 +103,8 @@ function initBuiltinWidget(note: FNote, isHorizontalLayout: boolean) {
return <TabSwitcher launcherNote={note} />;
case "sidebarChat":
return isExperimentalFeatureEnabled("llm") ? <SidebarChatButton launcherNote={note} /> : undefined;
+ case "colorSchemeSwitcher":
+ return <ColorSchemeSwitcher launcherNote={note} />;
default:
console.warn(`Unrecognized builtin widget ${builtinWidget} for launcher ${note.noteId} "${note.title}"`);
}
diff --git a/apps/client/src/widgets/react/ActionButton.tsx b/apps/client/src/widgets/react/ActionButton.tsx
index 03c1735622..1e3129ae47 100644
--- a/apps/client/src/widgets/react/ActionButton.tsx
+++ b/apps/client/src/widgets/react/ActionButton.tsx
@@ -1,4 +1,4 @@
-import type { Tooltip } from "bootstrap";
+import { Tooltip } from "bootstrap";
import { HTMLAttributes } from "preact";
import { useEffect, useMemo, useRef, useState } from "preact/hooks";
@@ -12,6 +12,8 @@ export interface ActionButtonProps extends Pick<HTMLAttributes<HTMLButtonElement
titlePosition?: "top" | "right" | "bottom" | "left";
/** Extra class applied to the tooltip popup (e.g. `tooltip-top` to raise its z-index above modals). */
tooltipClass?: string;
+ /** Renders `text` as (sanitized) HTML in the tooltip instead of plain text. Only pass trusted, non-user content. */
+ tooltipHtml?: boolean;
icon: string;
className?: string;
triggerCommand?: CommandNames;
@@ -23,22 +25,36 @@ export interface ActionButtonProps extends Pick<HTMLAttributes<HTMLButtonElement
const cachedIsMobile = isMobile();
-export default function ActionButton({ text, icon, className, triggerCommand, titlePosition, tooltipClass, noIconActionClass, frame, active, disabled, ...restProps }: ActionButtonProps) {
+export default function ActionButton({ text, icon, className, triggerCommand, titlePosition, tooltipClass, tooltipHtml, noIconActionClass, frame, active, disabled, ...restProps }: ActionButtonProps) {
const buttonRef = useRef<HTMLButtonElement>(null);
const [ keyboardShortcut, setKeyboardShortcut ] = useState<string[]>();
- // Memoized so useStaticTooltip's effect (keyed on config identity) doesn't dispose and
- // recreate the Bootstrap tooltip on every re-render.
+ const title = keyboardShortcut?.length ? `${text} (${keyboardShortcut.join(",")})` : text;
+ const titleRef = useRef(title);
+ titleRef.current = title;
+ const hasTitle = title.length > 0;
+
+ // The tooltip is recreated only when its structural options (or its presence) change — not when
+ // the label text changes. A plain text change is pushed into the live tooltip via setContent
+ // below, so a dynamic label updates in place instead of disposing and recreating the tooltip,
+ // which would drop the current hover.
const tooltipConfig = useMemo<Partial<Tooltip.Options>>(() => ({
- title: keyboardShortcut?.length ? `${text} (${keyboardShortcut?.join(",")})` : text,
+ title: hasTitle ? titleRef.current : undefined,
placement: titlePosition ?? "bottom",
fallbackPlacements: [ titlePosition ?? "bottom" ],
customClass: tooltipClass ?? "",
- trigger: cachedIsMobile ? "focus" : "hover focus",
+ html: tooltipHtml ?? false,
+ trigger: cachedIsMobile ? "focus" : "hover",
animation: false
- }), [text, keyboardShortcut, titlePosition, tooltipClass]);
+ }), [titlePosition, tooltipClass, tooltipHtml, hasTitle]);
useStaticTooltip(buttonRef, tooltipConfig);
+ useEffect(() => {
+ if (buttonRef.current) {
+ Tooltip.getInstance(buttonRef.current)?.setContent({ ".tooltip-inner": title });
+ }
+ }, [title]);
+
useEffect(() => {
if (triggerCommand) {
keyboard_actions.getAction(triggerCommand, true).then(action => setKeyboardShortcut(action?.effectiveShortcuts));
diff --git a/apps/client/src/widgets/type_widgets/options/appearance.tsx b/apps/client/src/widgets/type_widgets/options/appearance.tsx
index ca38720a23..972f2da637 100644
--- a/apps/client/src/widgets/type_widgets/options/appearance.tsx
+++ b/apps/client/src/widgets/type_widgets/options/appearance.tsx
@@ -6,6 +6,7 @@ import { createPortal } from "preact/compat";
import { useEffect, useMemo, useState } from "preact/hooks";
import zoomService from "../../../components/zoom";
+import { ColorScheme, resolveColorScheme, THEME_FAMILY_SCHEMES } from "../../../services/color_scheme";
import { t } from "../../../services/i18n";
import server from "../../../services/server";
import { isElectron, isMobile, reloadFrontendApp, restartDesktopApp } from "../../../services/utils";
@@ -34,8 +35,6 @@ interface CustomTheme {
noteId?: string;
}
-type ColorScheme = "system" | "light" | "dark";
-
interface ThemeFamily {
key: string;
title: string;
@@ -48,13 +47,13 @@ const THEME_FAMILIES: ThemeFamily[] = [
key: "modern",
title: t("theme.modern_themes"),
icon: "bx bx-star",
- schemes: { system: "next", light: "next-light", dark: "next-dark" }
+ schemes: THEME_FAMILY_SCHEMES.modern
},
{
key: "legacy",
title: t("theme.legacy_themes"),
icon: "bx bx-history",
- schemes: { system: "auto", light: "light", dark: "dark" }
+ schemes: THEME_FAMILY_SCHEMES.legacy
}
];
@@ -65,15 +64,9 @@ const COLOR_SCHEMES: { key: ColorScheme; label: string; icon: string }[] = [
];
function resolveTheme(themeVal: string | null): { family: ThemeFamily | null; scheme: ColorScheme; isCustom: boolean } {
- for (const family of THEME_FAMILIES) {
- for (const [scheme, val] of Object.entries(family.schemes)) {
- if (val === themeVal) {
- return { family, scheme: scheme as ColorScheme, isCustom: false };
- }
- }
- }
- // Custom theme
- return { family: null, scheme: "system", isCustom: true };
+ const { family: familyKey, scheme, isCustom } = resolveColorScheme(themeVal);
+ const family = THEME_FAMILIES.find(f => f.key === familyKey) ?? null;
+ return { family, scheme, isCustom };
}
interface FontFamilyEntry {
diff --git a/apps/server/src/assets/translations/en/server.json b/apps/server/src/assets/translations/en/server.json
index 5b18df2760..81f1a791dc 100644
--- a/apps/server/src/assets/translations/en/server.json
+++ b/apps/server/src/assets/translations/en/server.json
@@ -281,6 +281,7 @@
"quick-search-title": "Quick Search",
"protected-session-title": "Protected Session",
"sync-status-title": "Sync Status",
+ "color-scheme-switcher-title": "Color Scheme Switcher",
"settings-title": "Settings",
"options-title": "Options",
"appearance-title": "Appearance",
diff --git a/packages/commons/src/lib/hidden_subtree.ts b/packages/commons/src/lib/hidden_subtree.ts
index 0998ef4b15..ff4060119e 100644
--- a/packages/commons/src/lib/hidden_subtree.ts
+++ b/packages/commons/src/lib/hidden_subtree.ts
@@ -50,7 +50,8 @@ export interface HiddenSubtreeItem {
| "commandPalette"
| "toggleZenMode"
| "mobileTabSwitcher"
- | "sidebarChat";
+ | "sidebarChat"
+ | "colorSchemeSwitcher";
command?: keyof typeof Command;
/**
* If set to true, then branches will be enforced to be in the correct place.
diff --git a/packages/trilium-core/src/services/hidden_subtree_launcherbar.ts b/packages/trilium-core/src/services/hidden_subtree_launcherbar.ts
index 773065c6f8..97724d64ac 100644
--- a/packages/trilium-core/src/services/hidden_subtree_launcherbar.ts
+++ b/packages/trilium-core/src/services/hidden_subtree_launcherbar.ts
@@ -85,13 +85,21 @@ export default function buildLaunchBarConfig() {
command: "toggleZenMode",
icon: "bx bxs-yin-yang"
},
+ {
+ id: "_colorSchemeSwitcher",
+ title: t("hidden-subtree.color-scheme-switcher-title"),
+ type: "launcher",
+ builtinWidget: "colorSchemeSwitcher",
+ icon: "bx bx-adjust"
+ },
{
id: "_lbSidebarChat",
title: t("hidden-subtree.sidebar-chat-title"),
type: "launcher",
builtinWidget: "sidebarChat",
icon: "bx bx-message-square-dots"
- }
+ },
+
];
const desktopVisibleLaunchers: HiddenSubtreeItem[] = [