TR
apps/winzik / src / Form / MainForm.php · commit 6576d6f
<?phpnamespace App\Form;use App\Core\WinZik;use App\Gui\Desktop;use App\Gui\Host;use App\Scene\AboutScene;use JumPHP\Gui\Form;use JumPHP\Kernel\Platform;use JumPHP\Native\Shell;/** * The desktop of WinZik (Form1 and its friends): one Desktop widget draws the skinned windows; the dialogs are pushed * over it. Params: demo (the smoke test: it plays), fresh (start from the defaults, tests), phone (tests: the layout of * the phone app in a portrait column of the view; a number = its width / its height). * * On a phone it is "WinZik original" (option phoneUi = 'original'): the same windows, upright, one under the other; * otherwise a phone has its own screens (MobileForm). * * @property Desktop $desktop */final class MainForm extends Form implements Host{    public const SITE = 'https://web.archive.org/web/2002/http://www.winzik.com/';    public function __construct(private WinZik $wz)    {        parent::__construct();    }    public function winzik(): WinZik    {        return $this->wz;    }    public function enter(array $params = []): void    {        $this->wz->attach($this->engine);        if (!empty($params['fresh'])) {            $this->engine->storage->delete(WinZik::SETTINGS);            $this->wz->reset();        }        parent::enter($params);    }    /** the phone shows this interface (the windows of 2001), not its own screens */    public static function originalOnPhone(WinZik $wz): bool    {        return ($wz->options['phoneUi'] ?? 'mobile') === 'original';    }    protected function loaded(array $params = []): void    {        if (Platform::isPhoneApp() && !self::originalOnPhone($this->wz)) {            $this->scenes()->switchTo(MobileForm::class, [], false);     // the phone app has its own screens (src/Mobile)            return;        }        if (!empty($params['phone'])) {            // tests: the phone simulated in a column of the view (a number: its width / its height; else 9 / 19.5)            WinZikDialog::$phoneAspect = is_numeric($params['phone']) && (float)$params['phone'] > 0 ? (float)$params['phone'] : 9 / 19.5;        } elseif (!empty($params['fresh'])) {            WinZikDialog::$phoneAspect = 0.0;        }        if ($this->wz->native !== null) {            $this->wz->native->attach($this->desktop);        }        $this->wz->greet();        if (($this->wz->options['autoSkin'] ?? false) && !$this->wz->skinAsked && empty($params['fresh'])) {            // Choix du skin automatique au démarage: the skin viewer, once, at the start            $this->wz->skinAsked = true;            $this->scenes()->push(SkinForm::class);        }        $this->setFocus($this->desktop);        $this->desktop->phoneAspect = WinZikDialog::$phoneAspect;     // tests: a phone simulated (back from an editor too)        $this->invalidate();        if (!empty($params['phone']) || Platform::isPhoneApp()) {            $this->wz->notice('Appui long ou bouton Menu : le menu de WinZik', 6);        }        if (!empty($params['demo'])) {            // the smoke test of bin/jumphp test: the player plays, the lyrics show, the menu opens and closes            $this->wz->ui['lyrics']['visible'] = true;            $this->wz->playIndex(0);        }        // pictures of the site (tools/site/shots.sh): a moment of the song, another skin, the window opened        if (isset($params['market'])) {            $this->wz->useMarket((string)$params['market']);        }        if (isset($params['add'])) {            $this->wz->addFiles([(string)$params['add']]);     // tests, measures: a file of the computer        }        if (isset($params['play'])) {            $this->wz->playIndex((int)$params['play']);        }        if (isset($params['seek'])) {            $this->wz->seek((float)$params['seek']);        }        if (isset($params['skin'])) {            $this->wz->useSkin('assets/skins/' . $params['skin']);        }        if (!empty($params['eq'])) {            $this->wz->ui['eq']['visible'] = true;            $this->wz->eq->preset((string)$params['eq']);            $this->wz->eqChanged();        }        if (!empty($params['expanded'])) {            $this->wz->ui['main']['expanded'] = true;        }    }    public function resume(mixed $result = null): void    {        $this->setFocus($this->desktop);        // lyrics: "Ouvrir dans l'éditeur" of the search: the song plays (the editor works on the song playing), then the editor        if (is_array($result) && is_string($result['openEditor'] ?? null)) {            $file = $result['openEditor'];            if ($this->wz->playlist->current() !== $file) {                $index = array_search($file, $this->wz->playlist->items, true);                if ($index !== false) {                    $this->wz->playIndex((int)$index);                }            }            $this->openLyricsEditor();        }    }    protected function tick(float $dt): void    {        $this->wz->tick($dt);    }    public function exit(): void    {        $this->wz->save();    }    // Host: the dialogs ------------------------------------------------------------------------------------------    public function openYoutube(): void    {        $this->scenes()->push(YouTubeForm::class);    }    public function openSpotify(): void    {        $this->scenes()->push(SpotifyForm::class);    }    public function openTags(): void    {        $this->scenes()->push(TagForm::class);    }    public function openFiles(): void    {        $this->scenes()->push(FilesForm::class);    }    public function openFls(bool $save): void    {        $this->scenes()->push(FlsForm::class, ['save' => $save]);    }    public function openSkins(): void    {        $this->scenes()->push(SkinForm::class);    }    public function openLyricsEditor(): void    {        $this->wz->save();        $this->scenes()->switchTo(LyricsEditorForm::class, [], false);    }    public function openLyricsSearch(string $file): void    {        $this->scenes()->push(LyricsSearchForm::class, ['song' => $file]);    }    public function openMarket(string $kind): void    {        $this->scenes()->push(MarketForm::class, ['kind' => $kind]);    }    public function openCloud(): void    {        $this->scenes()->push(CloudForm::class);    }    public function openPlugins(): void    {        $this->scenes()->push(PluginsForm::class);    }    public function openSkinEditor(): void    {        $this->wz->save();        $this->scenes()->switchTo(SkinEditorForm::class, [], false);    }    public function openOptions(): void    {        $this->scenes()->push(OptionsForm::class);    }    public function openControls(): void    {        $this->scenes()->push(ControlsForm::class);    }    public function downloadLyrics(): void    {        $this->scenes()->push(LyricsForm::class);    }    public function openLock(): void    {        $this->scenes()->push(LockForm::class);    }    public function openAbout(): void    {        $this->scenes()->push(AboutScene::class);    }    public function openWebsite(): void    {        if (!Shell::openUrl(self::SITE)) {            $this->wz->notice('Le site : ' . self::SITE, 8);        }    }    /** The phone: back to its own screens (the option is kept: the next start opens them). */    public function openMobileUi(): void    {        $this->wz->options['phoneUi'] = 'mobile';        $this->wz->touch();        $this->wz->save();        $this->desktop->close();        // tests: the phone simulated here is simulated there too        $this->scenes()->switchTo(MobileForm::class, $this->desktop->phoneAspect > 0 ? ['phone' => true] : [], false);    }    /** Command5_Click: the settings written, then the end. A browser tab does not close itself: WinZik goes to the tray. */    public function quit(): void    {        $this->wz->save();        if (Platform::isDesktop() && !Platform::isHeadless()) {            $this->wz->quit();            return;        }        $this->wz->stop();        $this->desktop->toTray();    }}