Add a user option to set the preferred color scheme
authorAlexander Ebert <ebert@woltlab.com>
Thu, 10 Aug 2023 11:40:20 +0000 (13:40 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Thu, 10 Aug 2023 11:40:20 +0000 (13:40 +0200)
com.woltlab.wcf/templates/settings.tpl
com.woltlab.wcf/userOption.xml
wcfsetup/install/files/lib/form/SettingsForm.class.php
wcfsetup/install/files/lib/system/user/command/SetColorScheme.class.php [new file with mode: 0644]
wcfsetup/install/lang/de.xml
wcfsetup/install/lang/en.xml

index 481070105f0ea09a0ba65f5a4f3a4bb13efd7599..f0d0f64b200f025ae5719aad4230716c84a1a8db 100644 (file)
                        </section>
                {/if}
                
-               {if $availableStyles|count > 1}
-                       <section class="section" id="section_style">
-                               <h2 class="sectionTitle">{lang}wcf.user.styles{/lang}</h2>
+               <section class="section" id="section_style">
+                       <h2 class="sectionTitle">{lang}wcf.user.styles{/lang}</h2>
                                
+                       {if $availableStyles|count > 1}
                                <dl>
                                        <dt><label for="styleID">{lang}wcf.user.style{/lang}</label></dt>
                                        <dd>
                                                <small>{lang}wcf.user.style.description{/lang}</small>
                                        </dd>
                                </dl>
-                               
-                               {event name='styleFields'}
-                       </section>
-               {/if}
+                       {/if}
+                       
+                       <dl>
+                               <dt><label for="colorScheme">{lang}wcf.user.style.colorScheme{/lang}</label></dt>
+                               <dd>
+                                       <select id="colorScheme" name="colorScheme">
+                                               <option value="system"{if $colorScheme === 'system'} selected{/if}>{lang}wcf.style.setColorScheme.system{/lang}</option>
+                                               <option value="light"{if $colorScheme === 'light'} selected{/if}>{lang}wcf.style.setColorScheme.light{/lang}</option>
+                                               <option value="dark"{if $colorScheme === 'dark'} selected{/if}>{lang}wcf.style.setColorScheme.dark{/lang}</option>
+                                       </select>
+                                       <small>{lang}wcf.user.style.colorScheme.description{/lang}</small>
+                               </dd>
+                       </dl>
+                       
+                       {event name='styleFields'}
+               </section>
                
                {if MODULE_TROPHY && $__wcf->getSession()->getPermission('user.profile.trophy.maxUserSpecialTrophies') > 0 && $availableTrophies|count}
                        <section class="section" id="section_trophy">
index 549330a9b679cebd44393c7ccd19fd1898e15a9a..8afa12b4aa2922d5e4905252534725754cc1ab5c 100644 (file)
                                <optiontype>boolean</optiontype>
                                <defaultvalue>1</defaultvalue>
                        </option>
+                       <option name="colorScheme">
+                               <categoryname>hidden</categoryname>
+                               <optiontype>select</optiontype>
+                               <selectoptions>system:wcf.style.setColorScheme.system
+light:wcf.style.setColorScheme.light
+dark:wcf.style.setColorScheme.dark</selectoptions>
+                               <defaultvalue>system</defaultvalue>
+                       </option>
                        <option name="timezone">
                                <categoryname>settings.general.date</categoryname>
                                <optiontype>timezone</optiontype>
index 9a47ebe70f020703f39f1afad5a9530c10b26a91..7e08aa2c5ecf65277037998721e7c17de0fb4332 100644 (file)
@@ -18,6 +18,7 @@ use wcf\system\menu\user\UserMenu;
 use wcf\system\option\user\UserOptionHandler;
 use wcf\system\request\LinkHandler;
 use wcf\system\style\StyleHandler;
+use wcf\system\user\command\SetColorScheme;
 use wcf\system\user\storage\UserStorageHandler;
 use wcf\system\WCF;
 use wcf\util\ArrayUtil;
@@ -101,6 +102,8 @@ class SettingsForm extends AbstractForm
      */
     public $specialTrophies = [];
 
+    public string $colorScheme = 'system';
+
     /**
      * @inheritDoc
      */
@@ -161,6 +164,9 @@ class SettingsForm extends AbstractForm
             if (isset($_POST['specialTrophies'])) {
                 $this->specialTrophies = ArrayUtil::toIntegerArray($_POST['specialTrophies']);
             }
+            if (isset($_POST['colorScheme'])) {
+                $this->colorScheme = $_POST['colorScheme'];
+            }
         }
     }
 
@@ -215,6 +221,10 @@ class SettingsForm extends AbstractForm
                     throw new UserInputException('specialTrophies', 'invalid');
                 }
             }
+
+            if ($this->colorScheme !== 'light' && $this->colorScheme !== 'dark') {
+                $this->colorScheme = 'system';
+            }
         }
     }
 
@@ -238,6 +248,8 @@ class SettingsForm extends AbstractForm
                 $this->specialTrophies = \array_unique(\array_map(static function ($trophy) {
                     return $trophy->trophyID;
                 }, (new UserProfile(WCF::getUser()))->getSpecialTrophies()));
+
+                $this->colorScheme = WCF::getUser()->getUserOption('colorScheme');
             }
         }
     }
@@ -272,6 +284,11 @@ class SettingsForm extends AbstractForm
                 'trophyIDs' => $this->specialTrophies,
             ]);
             $userProfileAction->executeAction();
+
+            if (WCF::getUser()->getUserOption('colorScheme') !== $this->colorScheme) {
+                $command = new SetColorScheme(WCF::getUser(), $this->colorScheme);
+                $command();
+            }
         }
         $this->saved();
 
@@ -310,6 +327,7 @@ class SettingsForm extends AbstractForm
                 'languageID' => $this->languageID,
                 'styleID' => $this->styleID,
                 'specialTrophies' => $this->specialTrophies,
+                'colorScheme' => $this->colorScheme,
             ]);
         }
     }
diff --git a/wcfsetup/install/files/lib/system/user/command/SetColorScheme.class.php b/wcfsetup/install/files/lib/system/user/command/SetColorScheme.class.php
new file mode 100644 (file)
index 0000000..1e630a4
--- /dev/null
@@ -0,0 +1,36 @@
+<?php
+
+namespace wcf\system\user\command;
+
+use wcf\data\user\User;
+use wcf\data\user\UserAction;
+
+/**
+ * Sets the preferred color scheme of a user.
+ *
+ * @author Alexander Ebert
+ * @copyright 2001-2023 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @since 6.0
+ */
+final class SetColorScheme
+{
+    private readonly User $user;
+    private readonly string $colorScheme;
+
+    public function __construct(User $user, string $colorScheme)
+    {
+        $this->user = $user;
+        $this->colorScheme = $colorScheme;
+    }
+
+    public function __invoke(): void
+    {
+        $userAction = new UserAction([$this->user], 'update', [
+            'options' => [
+                User::getUserOptionID('colorScheme') => $this->colorScheme,
+            ],
+        ]);
+        $userAction->executeAction();
+    }
+}
index 5b51d6567cafaf573e8d142a975afe78fdda98da..a5f4df91e266b375e02e41a2a9e5cbf6de57da4f 100644 (file)
@@ -4659,6 +4659,8 @@ Dateianhänge:
                <item name="wcf.user.style"><![CDATA[Stil]]></item>
                <item name="wcf.user.styles"><![CDATA[Stile]]></item>
                <item name="wcf.user.style.description"><![CDATA[Stil der Benutzeroberfläche]]></item>
+               <item name="wcf.user.style.colorScheme"><![CDATA[Bevorzugtes Farbschema]]></item>
+               <item name="wcf.user.style.colorScheme.description"><![CDATA[Die automatische Erkennung wendet das Farbschema basierend auf den Präferenzen Ihres Geräts an. Einige Stile verfügen möglicherweise nicht über angepasste Farbschemas.]]></item>
                <item name="wcf.user.username.description"><![CDATA[Der Benutzername muss mindestens {REGISTER_USERNAME_MIN_LENGTH} und darf maximal {REGISTER_USERNAME_MAX_LENGTH} Zeichen lang sein.]]></item>
                <item name="wcf.user.password.description"><![CDATA[Ein sicheres Kennwort sollte mindestens 10 Zeichen lang sein.]]></item>
                <item name="wcf.user.password.strength"><![CDATA[Kennwortstärke]]></item>
index 6f957be019d18cf8e36dd04edc3f91f055341756..9ac15eb8990d4ffb20813c1682823209eea5caf2 100644 (file)
@@ -4664,6 +4664,8 @@ Attachments:
                <item name="wcf.user.style"><![CDATA[Style]]></item>
                <item name="wcf.user.styles"><![CDATA[Styles]]></item>
                <item name="wcf.user.style.description"><![CDATA[Forces a specific style instead of the default one.]]></item>
+               <item name="wcf.user.style.colorScheme"><![CDATA[Preferred Color Scheme]]></item>
+               <item name="wcf.user.style.colorScheme.description"><![CDATA[The automatic detection applies the color scheme based on your device’s preferences. Some styles might not offer a custom color scheme.]]></item>
                <item name="wcf.user.username.description"><![CDATA[Username must be {REGISTER_USERNAME_MIN_LENGTH} up to {REGISTER_USERNAME_MAX_LENGTH} characters long.]]></item>
                <item name="wcf.user.password.description"><![CDATA[A secure password should be at least 10 characters long.]]></item>
                <item name="wcf.user.password.strength"><![CDATA[Password Strength]]></item>