From f1fc721894fccec4afdb922c182a410fcedff126 Mon Sep 17 00:00:00 2001 From: Thomas Peterson Date: Mon, 28 Oct 2024 20:47:01 +0100 Subject: [PATCH] Fixes --- src/PHPNative/Framework/src/App.php | 3 +++ src/PHPNative/Framework/src/Application/Gui.php | 1 + .../Framework/src/Lifecycle/Lifecycle.php | 16 ++++++++++++++++ .../Framework/src/Loop/OrderedEventLoop.php | 8 +------- 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/PHPNative/Framework/src/App.php b/src/PHPNative/Framework/src/App.php index 7c6686d..09dac2c 100644 --- a/src/PHPNative/Framework/src/App.php +++ b/src/PHPNative/Framework/src/App.php @@ -2,9 +2,12 @@ namespace PHPNative\Framework; +use PHPNative\UI\TrayIcon; + interface App { public function getName(): string; public function getStartWindow(): string; + public function getTrayIcon(): ?TrayIcon; } \ No newline at end of file diff --git a/src/PHPNative/Framework/src/Application/Gui.php b/src/PHPNative/Framework/src/Application/Gui.php index f1011d0..26978c2 100644 --- a/src/PHPNative/Framework/src/Application/Gui.php +++ b/src/PHPNative/Framework/src/Application/Gui.php @@ -42,6 +42,7 @@ final readonly class Gui implements Application try { $lifeCycle->show($application->getStartWindow()); + $lifeCycle->addTrayIcon($application->getTrayIcon()); $lifeCycle->run(); } catch (ArgumentCountError $e) { var_dump($e->getMessage()); diff --git a/src/PHPNative/Framework/src/Lifecycle/Lifecycle.php b/src/PHPNative/Framework/src/Lifecycle/Lifecycle.php index 00c2084..b6edad5 100644 --- a/src/PHPNative/Framework/src/Lifecycle/Lifecycle.php +++ b/src/PHPNative/Framework/src/Lifecycle/Lifecycle.php @@ -10,10 +10,13 @@ use PHPNative\Framework\Application\Window; use PHPNative\Framework\Loop\OrderedEventLoop; use PHPNative\Framework\Loop\WorkerInterface; use PHPNative\Renderer\Thread; +use PHPNative\UI\TrayIcon; #[Singleton] class Lifecycle implements WorkerInterface { + private ?\Tray $trayIcon = null; + public function __construct( private OrderedEventLoop $loop, private ContextCollection $contextCollection, @@ -22,6 +25,18 @@ class Lifecycle implements WorkerInterface $this->loop->use($this); } + private function is_windows() { + return strncasecmp(PHP_OS, "WIN", 3) === 0; + } + + public function addTrayIcon(?TrayIcon $trayIcon):void + { + if($trayIcon) { + $this->trayIcon = new \Tray($trayIcon->name, $this->is_windows()? $trayIcon->iconWindows: $trayIcon->iconLinux); + $this->trayIcon->init(); + } + } + public function show(string $window, array $arguments = [], ?callable $onClose = null): void { @@ -44,6 +59,7 @@ class Lifecycle implements WorkerInterface public function onUpdate(float $delta): void { $this->contextCollection->map(fn(Context $context) => $context->update($delta)); + $this->trayIcon?->poll(); } public function onRender(float $delta): void diff --git a/src/PHPNative/Framework/src/Loop/OrderedEventLoop.php b/src/PHPNative/Framework/src/Loop/OrderedEventLoop.php index a061668..7931c96 100644 --- a/src/PHPNative/Framework/src/Loop/OrderedEventLoop.php +++ b/src/PHPNative/Framework/src/Loop/OrderedEventLoop.php @@ -11,6 +11,7 @@ class OrderedEventLoop extends EventLoop public Timer $updates; + public function __construct(private Driver $eventDriver) { $this->render = new Timer(self::DEFAULT_FRAME_RATE); @@ -21,8 +22,6 @@ class OrderedEventLoop extends EventLoop { $this->render->rate($frameRate)->touch(); $this->updates->rate($updateRate)->touch(); - $tray = new \Tray("Demo App", __DIR__ .'/../../../../../../app/app/Assets/icon.png'); - $tray->init(); while ($this->running) { $now = \microtime(true); @@ -33,10 +32,6 @@ class OrderedEventLoop extends EventLoop if (($delta = $this->render->next($now)) !== null) { $this->render($delta); - $poll = $tray->poll(); - if($poll != null) { - echo sprintf("%s", $poll).PHP_EOL; - } } while ($event = $this->eventDriver->pollEvent()) { @@ -44,6 +39,5 @@ class OrderedEventLoop extends EventLoop $this->poll($event); } } - $tray->close(); } } \ No newline at end of file