NL
apps/winzik / src / Form / OptionsForm.php · commit 6576d6f
<?phpnamespace App\Form;use App\Scene\AboutScene;use JumPHP\Gui\Button;use JumPHP\Gui\CheckBox;use JumPHP\Gui\ComboBox;use JumPHP\Gui\Label;use JumPHP\Gui\TextBox;use JumPHP\Native\FilePicker;use JumPHP\Native\Native;/** * Form17 of WinZiK 0.9.85 (found in its WinZik.exe: the splash, the unrolling windows, the skin chosen at the start, * the task bar, the joystick, the plug-ins) and the secondary folder of the lyrics * (rep2, chosen with BrowseForFolder in 2001). * * @property CheckBox $splash * @property CheckBox $splashSteps * @property CheckBox $unroll * @property CheckBox $unrollQuit * @property CheckBox $autoSkin * @property CheckBox $taskbar * @property CheckBox $joystick * @property Button $plugins * @property CheckBox $ytTools * @property CheckBox $whisperClaude * @property CheckBox $lrclib * @property CheckBox $autostart * @property CheckBox $autoUpdate * @property CheckBox $marketLyrics * @property CheckBox $snap * @property CheckBox $autoLyrics * @property CheckBox $autoTranslate * @property CheckBox $lyricsShowTranslation * @property CheckBox $lyricsClaude * @property ComboBox $translateLang * @property ComboBox $zoom * @property TextBox $lyricsDir * @property TextBox $spotifyId * @property Button $reset * @property TextBox $spotifySecret * @property \App\Gui\JogDial $jogX * @property \App\Gui\JogDial $jogY * @property Button $spotifyHelp * @property TextBox $spotifyGuide * @property Button $spotifyOpen * @property Button $spotifyCopy * @property \JumPHP\Gui\Container $spotifyLinks * @property Button $browse * @property Label $lyricsInfo * @property Button $about * @property Button $cancel * @property Button $ok */final class OptionsForm extends WinZikDialog{    private const CHECKS = ['splash', 'splashSteps', 'unroll', 'unrollQuit', 'autoSkin', 'taskbar', 'joystick', 'snap', 'autoLyrics', 'autoUpdate', 'marketLyrics', 'ytTools', 'whisperClaude', 'lrclib', 'autostart',        'autoTranslate', 'lyricsShowTranslation', 'lyricsClaude'];    private const ZOOMS = ['auto', '1', '1.5', '2', '2.5', '3', '4'];    protected function loaded(array $params = []): void    {        $this->resetAsked = 0.0;        $o = $this->wz->options;        foreach (self::CHECKS as $key) {            $this->$key->checked = (bool)($o[$key] ?? false);        }        $this->zoom->selected = max(0, (int)array_search((string)($o['zoom'] ?? 'auto'), self::ZOOMS, true));        // the language of the translations of the lyrics        $this->translateLang->items = array_values(\App\Model\Language::NAMES);        $this->translateLang->selected = max(0, (int)array_search($this->wz->translationLang(), array_keys(\App\Model\Language::NAMES), true));        $this->lyricsDir->text = (string)($o['lyricsDir'] ?? '');        $this->spotifyId->text = (string)($o['spotifyId'] ?? '');        $this->spotifySecret->text = (string)($o['spotifySecret'] ?? '');        $this->compactBefore = [(int)($o['compactDx'] ?? 0), (int)($o['compactDy'] ?? 0)];        $this->showNudge();        $this->browse->enabled = Native::supports('folder');        $this->browse->tooltip = $this->browse->enabled ? '' : 'Pas de choix de dossier ici : tapez le chemin dans la case';        $this->showLyricsInfo();        // the phone: nothing of the computer (its task bar, starting with it, the size of its windows, the mini bar on        // its windows, yt-dlp / ffmpeg / Whisper it downloads or runs, a joystick, a folder chooser it has not)        $this->hideOnPhone('taskbar', 'joystick', 'snap', 'autostart', 'whisperClaude', 'ytTools', 'zoomRow', 'jogRow', 'browse', 'lyricsClaude');        $this->setFocus($this->splash);    }    private function showLyricsInfo(): void    {        $this->lyricsInfo->text = 'Toujours cherchées aussi : à côté du morceau, et dans ' . $this->wz->storagePath('lyr');        $this->lyricsInfo->props['color'] = '';    }    public function onLyricsDirChange(TextBox $sender, string $text): void    {        $this->showLyricsInfo();    }    public function onBrowseClick(Button $sender): void    {        FilePicker::folder(['title' => 'Choisissez un répertoire'])            ->then(fn (mixed $dir) => is_string($dir) && $dir !== '' && $this->lyricsDir->text = $dir);    }    public function onOkClick(Button $sender): void    {        $dir = trim($this->lyricsDir->text);        if ($dir !== '' && !is_dir($dir)) {            // nothing saved: the user corrects the folder, or empties it            $this->lyricsInfo->text = "Ce répertoire n'existe pas : " . $dir . '. Corrigez-le (ou « Parcourir… »), ou videz la case.';            $this->lyricsInfo->props['color'] = '#ff8080';            $this->setFocus($this->lyricsDir);            return;        }        foreach (self::CHECKS as $key) {            $this->wz->options[$key] = (bool)$this->$key->checked;        }        $this->wz->options['zoom'] = self::ZOOMS[max(0, (int)$this->zoom->selected)] ?? 'auto';        $this->wz->options['translateLang'] = array_keys(\App\Model\Language::NAMES)[max(0, (int)$this->translateLang->selected)] ?? 'fr';        $this->wz->applyAutostart();        $this->wz->options['spotifyId'] = trim($this->spotifyId->text);        $this->wz->options['spotifySecret'] = trim($this->spotifySecret->text);        if ($dir !== $this->wz->options['lyricsDir']) {            $this->wz->options['lyricsDir'] = $dir;            $this->wz->refreshLyrics();        }        $this->wz->save();        $this->close();    }    /** @var array{0: int, 1: int} the offset of the compact bar when the window opened (Annuler puts it back) */    private array $compactBefore = [0, 0];    /** A jog dial of x / y turned: the compact bar moves at once (on the active window's title bar). */    public function onJogChange(\App\Gui\JogDial $sender, int $value): void    {        $key = $sender === $this->jogX ? 'compactDx' : 'compactDy';        $this->wz->options[$key] = $value;        $this->wz->touch();    }    private function showNudge(): void    {        $this->jogX->props['value'] = (int)($this->wz->options['compactDx'] ?? 0);        $this->jogY->props['value'] = (int)($this->wz->options['compactDy'] ?? 0);    }    private const SPOTIFY_DASHBOARD = 'https://developer.spotify.com/dashboard';    /** what Spotify accepts as a redirect: a loopback address with its port (http://localhost and no port are refused) */    private const SPOTIFY_REDIRECT = 'http://127.0.0.1:8888/callback';    /** Spotify: where to get one's own identifiers (the developer site opens), and the steps (text that can be copied). */    public function onSpotifyHelpClick(Button $sender): void    {        $this->spotifyGuide->visible = true;        $this->spotifyLinks->visible = true;        $this->spotifyGuide->text = "1. Ouvrez " . self::SPOTIFY_DASHBOARD . " et connectez-vous (compte Spotify gratuit), acceptez les conditions du développeur.\n"            . "2. « Create app » : un nom (WinZik), une description, Redirect URI : " . self::SPOTIFY_REDIRECT . " (Add), cochez « Web API », Save.\n"            . "3. Dans l'app : « Settings » : copiez le Client ID, puis « View client secret » et copiez-le.\n"            . "4. Collez-les dans les deux cases ci-dessus, puis Ok : la recherche « Spotify » de la fenêtre YouTube marche.";        $this->openSpotify();    }    public function onSpotifyOpenClick(Button $sender): void    {        $this->openSpotify();    }    public function onSpotifyCopyClick(Button $sender): void    {        $done = \JumPHP\Native\Clipboard::copy(self::SPOTIFY_REDIRECT);        $this->wz->notice($done ? 'Copié : ' . self::SPOTIFY_REDIRECT : 'Copiez-la dans le texte : ' . self::SPOTIFY_REDIRECT, 3);    }    private function openSpotify(): void    {        if (\JumPHP\Native\Native::supports('openUrl')) {            \JumPHP\Native\Shell::openUrl(self::SPOTIFY_DASHBOARD);        }    }    public function onPluginsClick(Button $sender): void    {        $this->scenes()->push(PluginsForm::class);    }    public function onCancelClick(Button $sender): void    {        // Annuler: the compact bar back where it was        [$this->wz->options['compactDx'], $this->wz->options['compactDy']] = $this->compactBefore;        $this->wz->touch();        $this->close();    }    /** seconds left to confirm the reset (0: not asked) */    private float $resetAsked = 0.0;    protected function tick(float $dt): void    {        parent::tick($dt);        if ($this->resetAsked > 0) {            $this->resetAsked -= $dt;            if ($this->resetAsked <= 0) {                $this->resetAsked = 0.0;                $this->reset->text = 'Réinitialiser les options et redémarrer';     // not confirmed: nothing done            }        }    }    /** Réinitialiser: a second click within 5 s confirms (everything of WinZik's settings is erased). */    public function onResetClick(Button $sender): void    {        if ($this->resetAsked > 0) {            $this->resetAsked = 0.0;            $this->wz->resetAndRestart();            $this->reset->text = 'Options effacées : WinZik redémarre...';            return;        }        $this->resetAsked = 5.0;        $this->reset->text = 'Confirmer : tout effacer et redémarrer ?';    }    public function onAboutClick(Button $sender): void    {        $this->scenes()->push(AboutScene::class);    }}