mirror of
https://gerrit.wikimedia.org/r/mediawiki/skins/MinervaNeue
synced 2024-11-23 22:03:39 +00:00
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:
parent
6e4c2e4c60
commit
ef46f238a7
|
@ -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.
|
||||
*
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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 );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue