diff --git a/src/Concerns/HasUrl.php b/src/Concerns/HasUrl.php index bb98b01..0a642c2 100644 --- a/src/Concerns/HasUrl.php +++ b/src/Concerns/HasUrl.php @@ -15,7 +15,7 @@ public function url(string $url): self public function route(string $route, array $parameters = []): self { - $this->url = route($route, $parameters); + $this->url(route($route, $parameters)); return $this; } diff --git a/src/Dialog.php b/src/Dialog.php index 223c853..6188efa 100644 --- a/src/Dialog.php +++ b/src/Dialog.php @@ -110,11 +110,7 @@ public function properties(array $properties): self public function asSheet(?string $windowId = null): self { - if (is_null($windowId)) { - $this->windowReference = Window::current()->id; - } else { - $this->windowReference = $windowId; - } + $this->windowReference = $windowId ?? Window::current()->getId(); return $this; } diff --git a/src/Windows/Window.php b/src/Windows/Window.php index cc430ca..17b3ecb 100644 --- a/src/Windows/Window.php +++ b/src/Windows/Window.php @@ -11,7 +11,9 @@ class Window { use HasDimensions; - use HasUrl; + use HasUrl { + HasUrl::url as defaultUrl; + } use HasVibrancy; protected bool $autoHideMenuBar = false; @@ -28,6 +30,8 @@ class Window protected bool $showDevTools = false; + protected bool $devToolsOpen = false; + protected bool $resizable = true; protected bool $movable = true; @@ -40,6 +44,8 @@ class Window protected bool $focusable = true; + protected bool $focused = false; + protected bool $hasShadow = true; protected bool $frame = true; @@ -56,6 +62,8 @@ class Window protected array $afterOpenCallbacks = []; + protected array $webPreferences = []; + public function __construct(string $id) { $this->id = $id; @@ -71,10 +79,36 @@ public function id(string $id = 'main'): self return $this; } + public function getId(): string + { + return $this->id; + } + public function title(string $title): self { $this->title = $title; + if (! $this instanceof PendingOpenWindow) { + $this->client->post('window/title', [ + 'id' => $this->id, + 'title' => $title, + ]); + } + + return $this; + } + + public function url(string $url) + { + $this->defaultUrl($url); + + if (! $this instanceof PendingOpenWindow) { + $this->client->post('window/url', [ + 'id' => $this->id, + 'url' => $url, + ]); + } + return $this; } @@ -142,21 +176,43 @@ public function setClient(Client $client): self return $this; } - public function alwaysOnTop($alwaysOnTop = true): self + public function alwaysOnTop(bool $alwaysOnTop = true): self { $this->alwaysOnTop = $alwaysOnTop; return $this; } - public function showDevTools($showDevTools = true): self + public function showDevTools(bool $showDevTools = true): self { $this->showDevTools = $showDevTools; + if (! $this instanceof PendingOpenWindow) { + $this->client->post('window/show-dev-tools', [ + 'id' => $this->id, + ]); + } + + return $this; + } + + public function hideDevTools(): self + { + if (! $this instanceof PendingOpenWindow) { + $this->client->post('window/hide-dev-tools', [ + 'id' => $this->id, + ]); + } + return $this; } - public function resizable($resizable = true): static + public function devToolsOpen(): bool + { + return $this->devToolsOpen; + } + + public function resizable(bool $resizable = true): static { $this->resizable = $resizable; @@ -194,10 +250,17 @@ public function maximized(): static return $this->afterOpen(fn () => WindowFacade::maximize($this->id)); } - public function closable($closable = true): static + public function closable(bool $closable = true): static { $this->closable = $closable; + if (! $this instanceof PendingOpenWindow) { + $this->client->post('window/closable', [ + 'id' => $this->id, + 'closable' => $closable, + ]); + } + return $this; } @@ -231,13 +294,20 @@ public function fullscreenable($fullscreenable = true): static return $this; } - public function kiosk($kiosk = false): static + public function kiosk($kiosk = true): static { $this->kiosk = $kiosk; return $this; } + public function webPreferences(array $preferences): static + { + $this->webPreferences = $preferences; + + return $this; + } + public function toArray() { return [ @@ -273,6 +343,7 @@ public function toArray() 'kiosk' => $this->kiosk, 'autoHideMenuBar' => $this->autoHideMenuBar, 'transparent' => $this->transparent, + 'webPreferences' => $this->webPreferences, ]; } @@ -282,4 +353,13 @@ public function afterOpen(callable $cb): static return $this; } + + public function fromRuntimeWindow(object $window): static + { + foreach ($window as $key => $value) { + $this->{$key} = $value; + } + + return $this; + } } diff --git a/src/Windows/WindowManager.php b/src/Windows/WindowManager.php index 9378c5f..9890708 100644 --- a/src/Windows/WindowManager.php +++ b/src/Windows/WindowManager.php @@ -25,14 +25,27 @@ public function close($id = null) public function hide($id = null) { - return $this->client->post('window/hide', [ + $this->client->post('window/hide', [ 'id' => $id ?? $this->detectId(), ]); } - public function current() + public function current(): Window { - return (object) $this->client->get('window/current')->json(); + $window = (object) $this->client->get('window/current')->json(); + + return (new Window($window->id)) + ->setClient($this->client) + ->fromRuntimeWindow($window); + } + + public function get(string $id): Window + { + $window = (object) $this->client->get("window/get/{$id}")->json(); + + return (new Window($id)) + ->setClient($this->client) + ->fromRuntimeWindow($window); } public function resize($width, $height, $id = null)