From 2ca74ab3ef47143cb42403f207e438eacda08dc5 Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Sat, 16 Nov 2024 17:51:50 +0300 Subject: [PATCH 1/1] Add auto/manual color scheme stylesheets depending on user preferences --- app/assets/stylesheets/parameters.scss | 1 - app/assets/stylesheets/screen-auto-ltr.scss | 3 +++ app/assets/stylesheets/screen-auto-rtl.rtlcss.scss | 3 +++ app/assets/stylesheets/screen-ltr.scss | 1 - app/assets/stylesheets/screen-manual-ltr.scss | 3 +++ app/assets/stylesheets/screen-manual-rtl.rtlcss.scss | 3 +++ app/assets/stylesheets/screen-rtl.rtlcss.scss | 1 - app/controllers/application_controller.rb | 10 +++++++++- app/views/layouts/_head.html.erb | 6 +++++- app/views/layouts/site.html.erb | 4 ++-- 10 files changed, 28 insertions(+), 7 deletions(-) create mode 100644 app/assets/stylesheets/screen-auto-ltr.scss create mode 100644 app/assets/stylesheets/screen-auto-rtl.rtlcss.scss delete mode 100644 app/assets/stylesheets/screen-ltr.scss create mode 100644 app/assets/stylesheets/screen-manual-ltr.scss create mode 100644 app/assets/stylesheets/screen-manual-rtl.rtlcss.scss delete mode 100644 app/assets/stylesheets/screen-rtl.rtlcss.scss diff --git a/app/assets/stylesheets/parameters.scss b/app/assets/stylesheets/parameters.scss index 28bf56901..07549d69b 100644 --- a/app/assets/stylesheets/parameters.scss +++ b/app/assets/stylesheets/parameters.scss @@ -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; diff --git a/app/assets/stylesheets/screen-auto-ltr.scss b/app/assets/stylesheets/screen-auto-ltr.scss new file mode 100644 index 000000000..89e31599a --- /dev/null +++ b/app/assets/stylesheets/screen-auto-ltr.scss @@ -0,0 +1,3 @@ +@use "common" with ( + $color-mode-type: media-query +); diff --git a/app/assets/stylesheets/screen-auto-rtl.rtlcss.scss b/app/assets/stylesheets/screen-auto-rtl.rtlcss.scss new file mode 100644 index 000000000..89e31599a --- /dev/null +++ b/app/assets/stylesheets/screen-auto-rtl.rtlcss.scss @@ -0,0 +1,3 @@ +@use "common" with ( + $color-mode-type: media-query +); diff --git a/app/assets/stylesheets/screen-ltr.scss b/app/assets/stylesheets/screen-ltr.scss deleted file mode 100644 index c525060af..000000000 --- a/app/assets/stylesheets/screen-ltr.scss +++ /dev/null @@ -1 +0,0 @@ -@import "common"; diff --git a/app/assets/stylesheets/screen-manual-ltr.scss b/app/assets/stylesheets/screen-manual-ltr.scss new file mode 100644 index 000000000..00f65f79a --- /dev/null +++ b/app/assets/stylesheets/screen-manual-ltr.scss @@ -0,0 +1,3 @@ +@use "common" with ( + $color-mode-type: data +); diff --git a/app/assets/stylesheets/screen-manual-rtl.rtlcss.scss b/app/assets/stylesheets/screen-manual-rtl.rtlcss.scss new file mode 100644 index 000000000..00f65f79a --- /dev/null +++ b/app/assets/stylesheets/screen-manual-rtl.rtlcss.scss @@ -0,0 +1,3 @@ +@use "common" with ( + $color-mode-type: data +); diff --git a/app/assets/stylesheets/screen-rtl.rtlcss.scss b/app/assets/stylesheets/screen-rtl.rtlcss.scss deleted file mode 100644 index c525060af..000000000 --- a/app/assets/stylesheets/screen-rtl.rtlcss.scss +++ /dev/null @@ -1 +0,0 @@ -@import "common"; diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 7ce804ced..56d9a6763 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -281,7 +281,15 @@ class ApplicationController < ActionController::Base 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) diff --git a/app/views/layouts/_head.html.erb b/app/views/layouts/_head.html.erb index 3c691612a..8cab52223 100644 --- a/app/views/layouts/_head.html.erb +++ b/app/views/layouts/_head.html.erb @@ -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" %> diff --git a/app/views/layouts/site.html.erb b/app/views/layouts/site.html.erb index 7c921658d..dd573d476 100644 --- a/app/views/layouts/site.html.erb +++ b/app/views/layouts/site.html.erb @@ -1,5 +1,5 @@ - +<%= 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" %> <%= render :partial => "layouts/header" %> @@ -8,4 +8,4 @@ <% end -%> - +<% end %> -- 2.39.5