diff --git a/site/app/Application/Theme.php b/site/app/Application/Theme.php deleted file mode 100644 index 8ad288945..000000000 --- a/site/app/Application/Theme.php +++ /dev/null @@ -1,53 +0,0 @@ -setCookie(self::DARK); - } - - - public function setLightMode(): void - { - $this->setCookie(self::LIGHT); - } - - - public function isDarkMode(): ?bool - { - $cookie = $this->cookies->getString(CookieName::Theme); - return $cookie === self::DARK ? true : ($cookie === self::LIGHT ? false : null); - } - - - private function setCookie(string $mode): void - { - $this->cookies->set(CookieName::Theme, $mode, $this->getCookieLifetime(), sameSite: 'None'); - } - - - public function getCookieLifetime(): string - { - return '365 days'; - } - -} diff --git a/site/app/Application/Theme/Theme.php b/site/app/Application/Theme/Theme.php new file mode 100644 index 000000000..ad4217e89 --- /dev/null +++ b/site/app/Application/Theme/Theme.php @@ -0,0 +1,57 @@ +setCookie(ThemeMode::Dark); + } + + + public function setLightMode(): void + { + $this->setCookie(ThemeMode::Light); + } + + + public function isDarkMode(): ?bool + { + $cookie = $this->cookies->getString(CookieName::Theme) ?? ''; + if ($cookie === 'bright') { + // Support legacy cookie value, can be removed in August 2025 because then all legacy cookies will expire + $cookie = ThemeMode::Light->value; + } + try { + return ThemeMode::from($cookie) === ThemeMode::Dark; + } catch (ValueError) { + return null; + } + } + + + private function setCookie(ThemeMode $mode): void + { + $this->cookies->set(CookieName::Theme, $mode->value, $this->getCookieLifetime(), sameSite: 'None'); + } + + + public function getCookieLifetime(): string + { + return '365 days'; + } + +} diff --git a/site/app/Application/Theme/ThemeMode.php b/site/app/Application/Theme/ThemeMode.php new file mode 100644 index 000000000..b45095692 --- /dev/null +++ b/site/app/Application/Theme/ThemeMode.php @@ -0,0 +1,12 @@ +factory->create(); + $form->addSubmit(ThemeMode::Light->value)->onClick[] = function () use ($onSuccess): void { + $this->theme->setLightMode(); + $onSuccess(); + }; + $form->addSubmit(ThemeMode::Dark->value)->onClick[] = function () use ($onSuccess): void { + $this->theme->setDarkMode(); + $onSuccess(); + }; + return $form; + } + +} diff --git a/site/app/Http/Cookies/CookieDescriptions.php b/site/app/Http/Cookies/CookieDescriptions.php index f3f9572b2..54d4c7f67 100644 --- a/site/app/Http/Cookies/CookieDescriptions.php +++ b/site/app/Http/Cookies/CookieDescriptions.php @@ -3,7 +3,7 @@ namespace MichalSpacekCz\Http\Cookies; -use MichalSpacekCz\Application\Theme; +use MichalSpacekCz\Application\Theme\Theme; use MichalSpacekCz\DateTime\DateTimeParser; use MichalSpacekCz\Formatter\TexyFormatter; use MichalSpacekCz\ShouldNotHappenException; diff --git a/site/app/Templating/TemplateFactory.php b/site/app/Templating/TemplateFactory.php index 039d23b29..88cb683c1 100644 --- a/site/app/Templating/TemplateFactory.php +++ b/site/app/Templating/TemplateFactory.php @@ -4,7 +4,7 @@ namespace MichalSpacekCz\Templating; use Contributte\Translation\Translator; -use MichalSpacekCz\Application\Theme; +use MichalSpacekCz\Application\Theme\Theme; use MichalSpacekCz\Templating\Exceptions\WrongTemplateClassException; use Nette\Application\UI\Control; use Nette\Application\UI\TemplateFactory as UiTemplateFactory; diff --git a/site/app/Www/Presenters/BasePresenter.php b/site/app/Www/Presenters/BasePresenter.php index c2e525ae1..5a97a13a0 100644 --- a/site/app/Www/Presenters/BasePresenter.php +++ b/site/app/Www/Presenters/BasePresenter.php @@ -6,9 +6,10 @@ use DateTimeInterface; use MichalSpacekCz\Application\Locale\LocaleLink; use MichalSpacekCz\Application\Locale\LocaleLinkGenerator; -use MichalSpacekCz\Application\Theme; use MichalSpacekCz\Css\CriticalCss; use MichalSpacekCz\Css\CriticalCssFactory; +use MichalSpacekCz\Form\ThemeFormFactory; +use MichalSpacekCz\Form\UiForm; use MichalSpacekCz\User\Manager; use Nette\Application\Request; use Nette\Application\UI\InvalidLinkException; @@ -27,7 +28,7 @@ abstract class BasePresenter extends Presenter private LocaleLinkGenerator $localeLinkGenerator; - private Theme $theme; + private ThemeFormFactory $themeFormFactory; private IResponse $httpResponse; @@ -55,9 +56,9 @@ public function injectLocaleLinkGenerator(LocaleLinkGenerator $localeLinkGenerat /** * @internal */ - public function injectTheme(Theme $theme): void + public function injectThemeFormFactory(ThemeFormFactory $themeFormFactory): void { - $this->theme = $theme; + $this->themeFormFactory = $themeFormFactory; } @@ -147,22 +148,6 @@ protected function getLocaleLinkParams(): array } - public function handleDarkFuture(): never - { - $this->theme->setDarkMode(); - $this->httpResponse->setExpiration(null); - $this->redirectPermanent('this'); - } - - - public function handleBrightFuture(): never - { - $this->theme->setLightMode(); - $this->httpResponse->setExpiration(null); - $this->redirectPermanent('this'); - } - - #[Override] public function lastModified(string|int|DateTimeInterface|null $lastModified, string $etag = null, string $expire = null): void { @@ -179,4 +164,13 @@ protected function createComponentCriticalCss(): CriticalCss return $this->criticalCssFactory->create(); } + + protected function createComponentTheme(): UiForm + { + return $this->themeFormFactory->create(function (): void { + $this->httpResponse->setExpiration(null); + $this->redirect('this'); + }); + } + } diff --git a/site/app/Www/Presenters/templates/@layout.latte b/site/app/Www/Presenters/templates/@layout.latte index 2f1c163da..05f89a461 100644 --- a/site/app/Www/Presenters/templates/@layout.latte +++ b/site/app/Www/Presenters/templates/@layout.latte @@ -88,14 +88,16 @@