Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Color mode preference stored in db #5339

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion app/assets/stylesheets/parameters.scss
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,3 @@ $table-border-factor: .1;
$list-group-hover-bg: rgba(var(--bs-emphasis-color-rgb), .075);

$enable-negative-margins: true;
$color-mode-type: media-query;
3 changes: 3 additions & 0 deletions app/assets/stylesheets/screen-auto-ltr.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@use "common" with (
$color-mode-type: media-query
);
3 changes: 3 additions & 0 deletions app/assets/stylesheets/screen-auto-rtl.rtlcss.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@use "common" with (
$color-mode-type: media-query
);
1 change: 0 additions & 1 deletion app/assets/stylesheets/screen-ltr.scss

This file was deleted.

3 changes: 3 additions & 0 deletions app/assets/stylesheets/screen-manual-ltr.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@use "common" with (
$color-mode-type: data
);
3 changes: 3 additions & 0 deletions app/assets/stylesheets/screen-manual-rtl.rtlcss.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@use "common" with (
$color-mode-type: data
);
1 change: 0 additions & 1 deletion app/assets/stylesheets/screen-rtl.rtlcss.scss

This file was deleted.

10 changes: 9 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,15 @@ def preferred_editor
end
end

helper_method :preferred_editor
def preferred_site_color_scheme
if current_user
current_user.preferences.find_by(:k => "site.color_scheme")&.v || "auto"
else
"auto"
end
end

helper_method :preferred_editor, :preferred_site_color_scheme

def update_totp
if Settings.key?(:totp_key)
Expand Down
10 changes: 9 additions & 1 deletion app/controllers/preferences_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,15 @@ def update
else
params[:user][:preferred_editor]
end
if current_user.save

success = current_user.save

if params[:site_color_scheme]
site_color_scheme_preference = current_user.preferences.find_or_create_by(:k => "site.color_scheme")
success &= site_color_scheme_preference.update(:v => params[:site_color_scheme])
end

if success
# Use a partial so that it is rendered during the next page load in the correct language.
flash[:notice] = { :partial => "preferences/update_success_flash" }
redirect_to preferences_path
Expand Down
6 changes: 5 additions & 1 deletion app/views/layouts/_head.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
<%= javascript_include_tag "turbo", :type => "module" %>
<%= javascript_include_tag "application" %>
<%= javascript_include_tag "i18n/#{I18n.locale}" %>
<%= stylesheet_link_tag "screen-#{dir}", :media => "screen" %>
<% if preferred_site_color_scheme == "auto" %>
<%= stylesheet_link_tag "screen-auto-#{dir}", :media => "screen" %>
<% else %>
<%= stylesheet_link_tag "screen-manual-#{dir}", :media => "screen" %>
<% end %>
<%= stylesheet_link_tag "print-#{dir}", :media => "print" %>
<%= stylesheet_link_tag "leaflet-all", :media => "screen, print" %>
<%= render :partial => "layouts/meta" %>
Expand Down
4 changes: 2 additions & 2 deletions app/views/layouts/site.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!DOCTYPE html>
<html lang="<%= I18n.locale %>" dir="<%= dir %>">
<%= tag.html :lang => I18n.locale, :dir => dir, :data => { :bs_theme => (preferred_site_color_scheme if preferred_site_color_scheme != "auto") } do %>
<%= render :partial => "layouts/head" %>
<body class="<%= body_class %>">
<%= render :partial => "layouts/header" %>
Expand All @@ -8,4 +8,4 @@
<noscript><p><img src="<%= request.protocol %><%= Settings.matomo["location"] %>/matomo.php?idsite=<%= Settings.matomo["site"] %>" class="matomo" alt="" /></p></noscript>
<% end -%>
</body>
</html>
<% end %>
8 changes: 8 additions & 0 deletions app/views/preferences/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@

<%= f.text_field :languages %>

<div class="mb-3">
<%= label_tag "site_color_scheme", t("preferences.show.preferred_site_color_scheme"), :class => "form-label" %>
<%= select_tag "site_color_scheme",
options_for_select(%w[auto light dark].map { |scheme| [t("preferences.show.site_color_schemes.#{scheme}"), scheme] },
preferred_site_color_scheme),
:class => "form-select" %>
</div>

<%= f.primary t(".save") %>
<%= link_to t(".cancel"), preferences_path, :class => "btn btn-link" %>
<% end %>
4 changes: 4 additions & 0 deletions app/views/preferences/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
<li><%= locale %></li>
<% end %>
</ul>
</dd>

<dt class="col-sm-4"><%= t ".preferred_site_color_scheme" %></dt>
<dd class="col-sm-8">
<%= t ".site_color_schemes.#{preferred_site_color_scheme}" %>
</dd>
</dl>

Expand Down
5 changes: 5 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1807,6 +1807,11 @@ en:
title: My Preferences
preferred_editor: Preferred Editor
preferred_languages: Preferred Languages
preferred_site_color_scheme: Preferred Website Color Scheme
site_color_schemes:
auto: Auto
light: Light
dark: Dark
edit_preferences: Edit Preferences
edit:
title: Edit Preferences
Expand Down
26 changes: 26 additions & 0 deletions test/controllers/preferences_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def test_routes

def test_update_preferred_editor
user = create(:user, :languages => [])
user.preferences.create(:k => "site.color_scheme", :v => "light")
session_for(user)

# Changing to a invalid editor should fail
Expand All @@ -32,6 +33,7 @@ def test_update_preferred_editor
assert_select ".alert-success", false
assert_select ".alert-danger", true
assert_select "form > div > select#user_preferred_editor > option[selected]", false
assert_equal "light", user.preferences.find_by(:k => "site.color_scheme")&.v

# Changing to a valid editor should work
user.preferred_editor = "id"
Expand All @@ -41,6 +43,7 @@ def test_update_preferred_editor
assert_template :show
assert_select ".alert-success", /^Preferences updated/
assert_select "dd", "iD (in-browser editor)"
assert_equal "light", user.preferences.find_by(:k => "site.color_scheme")&.v

# Changing to the default editor should work
user.preferred_editor = "default"
Expand All @@ -50,5 +53,28 @@ def test_update_preferred_editor
assert_template :show
assert_select ".alert-success", /^Preferences updated/
assert_select "dd", "Default (currently iD)"
assert_equal "light", user.preferences.find_by(:k => "site.color_scheme")&.v
end

def test_update_preferred_site_color_scheme
user = create(:user, :languages => [])
session_for(user)
assert_nil user.preferences.find_by(:k => "site.color_scheme")

# Changing when previously not defined
put preferences_path, :params => { :user => user.attributes, :site_color_scheme => "light" }
assert_redirected_to preferences_path
follow_redirect!
assert_template :show
assert_select ".alert-success", /^Preferences updated/
assert_equal "light", user.preferences.find_by(:k => "site.color_scheme")&.v

# Changing when previously defined
put preferences_path, :params => { :user => user.attributes, :site_color_scheme => "auto" }
assert_redirected_to preferences_path
follow_redirect!
assert_template :show
assert_select ".alert-success", /^Preferences updated/
assert_equal "auto", user.preferences.find_by(:k => "site.color_scheme")&.v
end
end