Skip to content

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

Code maintenance/66395 Add user current theme inquiry methods (#19903)

Kabiru Mwenja4a973bf44a5a
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index bb0b55ff591..f4796bc56e8 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -317,7 +317,7 @@ module ApplicationHelper
end
def user_theme_data_attributes
- if User.current.pref.sync_with_system_theme?
+ if User.current.pref.sync_with_os_theme?
# Theme will be set by inline script before body renders to prevent flickering
{ auto_theme_switcher_mode_value: User.current.pref.theme }
else
diff --git a/app/helpers/colors_helper.rb b/app/helpers/colors_helper.rb
index ccd857ec17a..12c34fe4351 100644
--- a/app/helpers/colors_helper.rb
+++ b/app/helpers/colors_helper.rb
@@ -143,17 +143,15 @@ module ColorsHelper
end
def set_generic_color_for(class_name:, color:)
- mode = User.current.pref.theme.split("_", 2)[0]
- mode_variables = mode == "dark" ? default_variables_dark : default_variables_light
+ mode_variables = User.current.pref.base_theme_dark? ? default_variables_dark : default_variables_light
concat "#{class_name} { #{default_color_styles(color.hexcode)} #{mode_variables} }"
end
def set_background_colors_for(class_name:, color:)
- mode = User.current.pref.theme.split("_", 2)[0]
-
concat "#{class_name} { #{default_color_styles(color.hexcode)} }"
- if mode == "dark"
+
+ if User.current.pref.base_theme_dark?
concat "#{class_name} { #{default_variables_dark} }"
concat "#{class_name} { #{highlighted_background_dark} }"
else
@@ -163,10 +161,9 @@ module ColorsHelper
end
def set_foreground_colors_for(class_name:, color:)
- mode = User.current.pref.theme.split("_", 2)[0]
-
concat "#{class_name} { #{default_color_styles(color.hexcode)} }"
- if mode == "dark"
+
+ if User.current.pref.base_theme_dark?
concat "#{class_name} { #{default_variables_dark} }"
concat "#{class_name} { #{highlighted_foreground_dark} }"
else
@@ -211,9 +208,8 @@ module ColorsHelper
color: hsl(var(--color-h), calc(var(--color-s) * 1%), calc((var(--color-l) + var(--lighten-by)) * 1%)) !important;
background: rgba(var(--color-r), var(--color-g), var(--color-b), var(--background-alpha)) !important;
CSS
- mode = User.current.pref.theme
- style += if mode == "dark_high_contrast"
+ style += if User.current.pref.dark_high_contrast_theme?
<<~CSS.squish
border: 1px solid hsl(var(--color-h), calc(var(--color-s) * 1%), calc((var(--color-l) + 10 + var(--lighten-by)) * 1%)) !important;
CSS
@@ -231,9 +227,8 @@ module ColorsHelper
color: hsl(0deg, 0%, calc(var(--lightness-switch) * 100%)) !important;
background: rgb(var(--color-r), var(--color-g), var(--color-b)) !important;
CSS
- mode = User.current.pref.theme
- style += if mode == "light_high_contrast"
+ style += if User.current.pref.light_high_contrast_theme?
<<~CSS.squish
border: 1px solid hsla(var(--color-h), calc(var(--color-s) * 1%), calc((var(--color-l) - 75) * 1%), 1) !important;
CSS
@@ -247,9 +242,7 @@ module ColorsHelper
end
def highlighted_foreground_dark
- mode = User.current.pref.theme
-
- if mode == "dark_high_contrast"
+ if User.current.pref.dark_high_contrast_theme?
<<~CSS.squish
color: hsla(var(--color-h), calc(var(--color-s) * 1%), calc((var(--color-l) + 10 + var(--lighten-by)) * 1%), 1) !important;
CSS
@@ -261,9 +254,7 @@ module ColorsHelper
end
def highlighted_foreground_light
- mode = User.current.pref.theme
-
- if mode == "light_high_contrast"
+ if User.current.pref.light_high_contrast_theme?
<<~CSS.squish
color: hsla(var(--color-h), calc(var(--color-s) * 1%), calc((var(--color-l) - (var(--color-l) * 0.5)) * 1%), 1) !important;
CSS
diff --git a/app/models/user_preference.rb b/app/models/user_preference.rb
index d564cd86329..96001645cb9 100644
--- a/app/models/user_preference.rb
+++ b/app/models/user_preference.rb
@@ -38,6 +38,8 @@ class UserPreference < ApplicationRecord
WORKDAYS_FROM_MONDAY_TO_FRIDAY = [1, 2, 3, 4, 5].freeze
+ THEMES = %w[light light_high_contrast dark dark_high_contrast sync_with_os].freeze
+
##
# Retrieve keys from settings, and allow accessing
# as boolean with ? suffix
@@ -130,12 +132,12 @@ class UserPreference < ApplicationRecord
super.presence || Setting.user_default_theme
end
- def sync_with_system_theme?
- theme == "sync_with_os"
+ %w[dark light].each do |base_theme_name|
+ define_method("base_theme_#{base_theme_name}?") { theme.split("_", 2)[0] == base_theme_name.to_s }
end
- def light_high_contrast_theme?
- theme == "light_high_contrast"
+ THEMES.each do |theme_name|
+ define_method("#{theme_name}_theme?") { theme == theme_name.to_s }
end
def time_zone
diff --git a/app/views/layouts/base.html.erb b/app/views/layouts/base.html.erb
index c4bed97c8b6..4ec5ede53d1 100644
--- a/app/views/layouts/base.html.erb
+++ b/app/views/layouts/base.html.erb
@@ -41,7 +41,7 @@ See COPYRIGHT and LICENSE files for more details.
<%# Disable prefetching for now %>
<meta name="turbo-prefetch" content="false">
<%# Apply system theme immediately to prevent flickering %>
- <% if User.current.pref.sync_with_system_theme? %>
+ <% if User.current.pref.sync_with_os_theme? %>
<%= nonced_javascript_tag do %>
// Apply system theme before body renders to prevent flickering
(function() {
diff --git a/spec/models/user_preference_spec.rb b/spec/models/user_preference_spec.rb
index 6884add80fc..f638896ec39 100644
--- a/spec/models/user_preference_spec.rb
+++ b/spec/models/user_preference_spec.rb
@@ -247,4 +247,53 @@ RSpec.describe UserPreference do
end
end
end
+
+ describe "#theme" do
+ context "when none is specified" do
+ it "defaults to light" do
+ expect(subject.theme).to eq("light")
+ expect(subject).to be_a_light_theme
+ expect(subject).to be_a_base_theme_light
+ end
+ end
+
+ context "with a dark theme specified" do
+ let(:settings) { { "theme" => "dark" } }
+
+ it "returns the dark theme" do
+ expect(subject.theme).to eq("dark")
+ expect(subject).to be_a_dark_theme
+ expect(subject).to be_a_base_theme_dark
+ end
+ end
+
+ context "with a light high contrast theme specified" do
+ let(:settings) { { "theme" => "light_high_contrast" } }
+
+ it "returns the light high contrast theme" do
+ expect(subject.theme).to eq("light_high_contrast")
+ expect(subject).to be_a_light_high_contrast_theme
+ expect(subject).to be_a_base_theme_light
+ end
+ end
+
+ context "with a dark high contrast theme specified" do
+ let(:settings) { { "theme" => "dark_high_contrast" } }
+
+ it "returns the dark high contrast theme" do
+ expect(subject.theme).to eq("dark_high_contrast")
+ expect(subject).to be_a_dark_high_contrast_theme
+ expect(subject).to be_a_base_theme_dark
+ end
+ end
+
+ context "with system theme specified" do
+ let(:settings) { { "theme" => "sync_with_os" } }
+
+ it "returns the system theme" do
+ expect(subject.theme).to eq("sync_with_os")
+ expect(subject).to be_a_sync_with_os_theme
+ end
+ end
+ end
end