This commit is contained in:
Thomas Peterson 2024-10-28 20:47:01 +01:00
parent 3654670954
commit f1fc721894
4 changed files with 21 additions and 7 deletions

View File

@ -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;
}

View File

@ -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());

View File

@ -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

View File

@ -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();
}
}