Set up user option for night mode

Add a new user option, `minerva-night-mode`, and configure it with a
default value of 2 (automatic based on OS).  Rework the code in
`getHtmlElementAttributes` to set the initial class value according to
the user option, which will be set and saved inside of MobileFrontend

Bug: T355118
Change-Id: I4b0d63a186f490ba10b7655b3af4dcb8b64bd400
This commit is contained in:
Steph Toyofuku 2024-02-01 15:05:55 -08:00
parent 6e4c2e4c60
commit ef46f238a7
4 changed files with 46 additions and 4 deletions

View file

@ -35,6 +35,7 @@ use MediaWiki\Minerva\Hooks\HookRunner;
use MediaWiki\Minerva\Skins\SkinMinerva;
use MediaWiki\Minerva\Skins\SkinUserPageHelper;
use MediaWiki\Output\OutputPage;
use MediaWiki\Preferences\Hook\GetPreferencesHook;
use MediaWiki\ResourceLoader\Context;
use MediaWiki\ResourceLoader\Hook\ResourceLoaderGetConfigVarsHook;
use MediaWiki\ResourceLoader\Hook\ResourceLoaderRegisterModulesHook;
@ -57,6 +58,7 @@ use Wikimedia\Services\NoSuchServiceException;
class Hooks implements
DifferenceEngineViewHeaderHook,
FetchChangesListHook,
GetPreferencesHook,
OutputPageBodyAttributesHook,
ResourceLoaderGetConfigVarsHook,
ResourceLoaderRegisterModulesHook,
@ -92,6 +94,22 @@ class Hooks implements
}
}
/**
* Adds Minerva-specific user preferences that can only be accessed via API
*
* @param User $user user whose preferences are being modified
* @param array[] &$prefs preferences description array, to be fed to a HTMLForm object
*/
public function onGetPreferences( $user, &$prefs ): void {
$minervaPrefs = [
'minerva-night-mode' => [
'type' => 'api'
],
];
$prefs += $minervaPrefs;
}
/**
* PreferencesGetLayout hook handler.
*

View file

@ -617,7 +617,11 @@ class SkinMinerva extends SkinMustache {
// check to see if night mode is enabled via query params
$forceNightMode = $this->getContext()->getRequest()->getBool( 'minervanightmode' );
if ( $skinOptions->get( SkinOptions::NIGHT_MODE ) || $forceNightMode ) {
$attributes['class'] .= ' skin-night-mode-clientpref-1';
$user = $this->getUser();
$optionsManager = MediaWikiServices::getInstance()->getUserOptionsManager();
$value = $optionsManager->getOption( $user, 'minerva-night-mode' );
$attributes['class'] .= " skin-night-mode-clientpref-$value";
}
return $attributes;

View file

@ -159,6 +159,7 @@
"FetchChangesList": "main",
"MobileFrontendFeaturesRegistration": "mobilefrontend",
"RequestContextCreateSkinMobile": "mobilefrontend",
"GetPreferences": "main",
"PreferencesGetLayout": "main",
"SpecialPageBeforeExecute": "main",
"ResourceLoaderGetConfigVars": "main",
@ -569,5 +570,8 @@
]
}
},
"DefaultUserOptions": {
"minerva-night-mode": 2
},
"manifest_version": 2
}

View file

@ -2,6 +2,7 @@
namespace MediaWiki\Minerva;
use MediaWiki\MediaWikiServices;
use MediaWiki\Minerva\Skins\SkinMinerva;
use MediaWiki\Output\OutputPage;
use MediaWiki\Title\Title;
@ -176,7 +177,7 @@ class SkinMinervaTest extends MediaWikiIntegrationTestCase {
$skin = new SkinMinerva();
$classes = $skin->getHtmlElementAttributes()['class'];
$this->assertStringNotContainsString( 'skin-night-mode-clientpref-1', $classes );
$this->assertStringNotContainsString( 'skin-night-mode-clientpref-', $classes );
}
/**
@ -188,7 +189,7 @@ class SkinMinervaTest extends MediaWikiIntegrationTestCase {
$skin = new SkinMinerva();
$classes = $skin->getHtmlElementAttributes()['class'];
$this->assertStringContainsString( 'skin-night-mode-clientpref-1', $classes );
$this->assertStringContainsString( 'skin-night-mode-clientpref-2', $classes );
}
/**
@ -203,7 +204,22 @@ class SkinMinervaTest extends MediaWikiIntegrationTestCase {
$skin->setContext( $context );
$classes = $skin->getHtmlElementAttributes()['class'];
$this->assertStringContainsString( 'skin-night-mode-clientpref-1', $classes );
$this->assertStringContainsString( 'skin-night-mode-clientpref-2', $classes );
}
/**
* @covers ::getHtmlElementAttributes when night mode is enabled and the value is not default
*/
public function testGetHtmlElementAttributesNightModeUserOption() {
$this->overrideSkinOptions( [ SkinOptions::NIGHT_MODE => true ] );
$skin = new SkinMinerva();
$user = $skin->getUser();
MediaWikiServices::getInstance()->getUserOptionsManager()->setOption( $user, 'minerva-night-mode', 0 );
$classes = $skin->getHtmlElementAttributes()['class'];
$this->assertStringContainsString( 'skin-night-mode-clientpref-0', $classes );
}
/**