OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Add Auto Sync Color Theme with System (OS) user preference
Kabiru Mwenjab2561c6716d9
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rbindex 4dc295948d4..338d7d6adc2 100644--- a/app/helpers/application_helper.rb+++ b/app/helpers/application_helper.rb@@ -244,8 +244,8 @@ module ApplicationHelpercss = ["theme-#{OpenProject::CustomStyles::Design.identifier}"]if controller_path && action_name- css << ("controller-#{controller_path}")- css << ("action-#{action_name}")+ css << "controller-#{controller_path}"+ css << "action-#{action_name}"endif EnterpriseToken.hide_banners?@@ -294,7 +294,8 @@ module ApplicationHelper[[I18n.t("themes.light"), "light"],[I18n.t("themes.light_high_contrast"), "light_high_contrast"],- [I18n.t("themes.dark"), "dark"]+ [I18n.t("themes.dark"), "dark"],+ [I18n.t("themes.sync_with_system"), "sync_with_system"]]end@@ -305,7 +306,7 @@ module ApplicationHelperdef body_data_attributes(local_assigns){- controller: "application hover-card-trigger beforeunload",+ controller: "application auto-theme-switcher hover-card-trigger beforeunload",relative_url_root: root_path,overflowing_identifier: ".__overflowing_body",rendered_at: Time.zone.now.iso8601,@@ -315,11 +316,21 @@ module ApplicationHelperenddef user_theme_data_attributes- mode, _theme_suffix = User.current.pref.theme.split("_", 2)- {- color_mode: mode,- "#{mode}_theme": User.current.pref.theme- }+ if User.current.pref.sync_with_system_theme?+ # Default to light mode initially before System theme is applied+ # FIXME: Causes flickering on page navigation and load+ {+ color_mode: "light",+ light_theme: "light",+ auto_theme_switcher_mode_value: User.current.pref.theme+ }+ else+ mode, _theme_suffix = User.current.pref.theme.split("_", 2)+ {+ color_mode: mode,+ "#{mode}_theme": User.current.pref.theme+ }+ endenddef highlight_default_language(lang_options)diff --git a/app/models/user_preference.rb b/app/models/user_preference.rbindex 6c79fd21779..15428ebda81 100644--- a/app/models/user_preference.rb+++ b/app/models/user_preference.rb@@ -130,6 +130,10 @@ class UserPreference < ApplicationRecordsuper.presence || Setting.user_default_themeend+ def sync_with_system_theme?+ theme == "sync_with_system"+ end+def high_contrast_theme?theme.end_with?("high_contrast")enddiff --git a/config/locales/en.yml b/config/locales/en.ymlindex d733fa16300..b517f61cce2 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -742,6 +742,7 @@ en:dark: "Dark"light: "Light"light_high_contrast: "Light high contrast"+ sync_with_system: "Sync with system"types:index:diff --git a/config/schemas/user_preferences.schema.json b/config/schemas/user_preferences.schema.jsonindex 04ff02a5d11..10d23409b11 100644--- a/config/schemas/user_preferences.schema.json+++ b/config/schemas/user_preferences.schema.json@@ -11,7 +11,7 @@},"theme": {"type": "string",- "enum": ["light", "light_high_contrast", "dark"]+ "enum": ["light", "light_high_contrast", "dark", "sync_with_system"]},"disable_keyboard_shortcuts": {"type": "boolean"diff --git a/frontend/src/react/OpBlockNoteContainer.tsx b/frontend/src/react/OpBlockNoteContainer.tsxindex a9790ea1726..3c8b08332a5 100644--- a/frontend/src/react/OpBlockNoteContainer.tsx+++ b/frontend/src/react/OpBlockNoteContainer.tsx@@ -33,14 +33,13 @@ import { BlockNoteView } from "@blocknote/mantine";import { getDefaultReactSlashMenuItems, SuggestionMenuController, useCreateBlockNote } from "@blocknote/react";import { dummyBlockSpec, getDefaultOpenProjectSlashMenuItems, openProjectWorkPackageBlockSpec } from "op-blocknote-extensions";import { useEffect, useState } from "react";+import { OpTheme } from "../stimulus/controllers/auto-theme-switcher.controller";export interface OpBlockNoteContainerProps {inputField: HTMLInputElement;inputText?: string;}-type OpTheme = "light" | "dark";-const schema = BlockNoteSchema.create({blockSpecs: {...defaultBlockSpecs,diff --git a/frontend/src/stimulus/controllers/auto-theme-switcher.controller.ts b/frontend/src/stimulus/controllers/auto-theme-switcher.controller.tsnew file mode 100644index 00000000000..560d2c65822--- /dev/null+++ b/frontend/src/stimulus/controllers/auto-theme-switcher.controller.ts@@ -0,0 +1,79 @@+/*+ * -- copyright+ * OpenProject is an open source project management software.+ * Copyright (C) the OpenProject GmbH+ *+ * This program is free software; you can redistribute it and/or+ * modify it under the terms of the GNU General Public License version 3.+ *+ * OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+ * Copyright (C) 2006-2013 Jean-Philippe Lang+ * Copyright (C) 2010-2013 the ChiliProject Team+ *+ * This program is free software; you can redistribute it and/or+ * modify it under the terms of the GNU General Public License+ * as published by the Free Software Foundation; either version 2+ * of the License, or (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU General Public License for more details.+ *+ * You should have received a copy of the GNU General Public License+ * along with this program; if not, write to the Free Software+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+ *+ * See COPYRIGHT and LICENSE files for more details.+ * +++ */++import { Controller } from '@hotwired/stimulus';+import { useMatchMedia } from 'stimulus-use';++export type OpTheme = 'light' | 'dark';++export default class AutoThemeSwitcher extends Controller {+ static values = {+ mode: String,+ };++ declare readonly modeValue:string;++ connect() {+ if (this.modeValue !== 'sync_with_system') return;++ useMatchMedia(this, {+ mediaQueries: {+ lightMode: '(prefers-color-scheme: light)',+ },+ });+ }++ isLightMode():void {+ this.applyTheme('light');+ }++ notLightMode():void {+ this.applyTheme('dark');+ }++ private applyTheme(theme:OpTheme):void {+ const body = document.body;++ switch (theme) {+ case 'dark':+ body.setAttribute('data-color-mode', 'dark');+ body.setAttribute('data-dark-theme', 'dark');+ body.removeAttribute('data-light-theme');+ break;+ case 'light':+ body.setAttribute('data-color-mode', 'light');+ body.setAttribute('data-light-theme', 'light');+ body.removeAttribute('data-dark-theme');+ break;+ default: // Do nothing+ break;+ }+ }+}diff --git a/frontend/src/stimulus/setup.ts b/frontend/src/stimulus/setup.tsindex fcf4f23c0a6..3565ee7662a 100644--- a/frontend/src/stimulus/setup.ts+++ b/frontend/src/stimulus/setup.ts@@ -26,6 +26,7 @@ import StemsController from './controllers/dynamic/work-packages/activities-tab/import EditorController from './controllers/dynamic/work-packages/activities-tab/editor.controller';import AutoSubmit from '@stimulus-components/auto-submit';+import AutoThemeSwitcher from './controllers/auto-theme-switcher.controller';import { OpenProjectStimulusApplication } from 'core-stimulus/openproject-stimulus-application';import { Application } from '@hotwired/stimulus';import { BeforeunloadController } from './controllers/beforeunload.controller';@@ -63,6 +64,7 @@ OpenProjectStimulusApplication.preregister('work-packages--activities-tab--polliOpenProjectStimulusApplication.preregister('work-packages--activities-tab--stems', StemsController);OpenProjectStimulusApplication.preregister('work-packages--activities-tab--editor', EditorController);OpenProjectStimulusApplication.preregister('beforeunload', BeforeunloadController);+OpenProjectStimulusApplication.preregister('auto-theme-switcher', AutoThemeSwitcher);const instance = OpenProjectStimulusApplication.start();window.Stimulus = instance;