Skip to content

Commit

Permalink
Improved window management (#391)
Browse files Browse the repository at this point in the history
* consistency

* Use the Window class

* Sensible default

* Allow setting title and URL

* Use setter

* Add closable

* Add webPreferences support

* Dynamic DevTools

* Type hint

* Use passed ID

* Fix dialog loading
  • Loading branch information
simonhamp authored Oct 30, 2024
1 parent 27cc28b commit 2c89fba
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/Concerns/HasUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 1 addition & 5 deletions src/Dialog.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
92 changes: 86 additions & 6 deletions src/Windows/Window.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
class Window
{
use HasDimensions;
use HasUrl;
use HasUrl {
HasUrl::url as defaultUrl;
}
use HasVibrancy;

protected bool $autoHideMenuBar = false;
Expand All @@ -28,6 +30,8 @@ class Window

protected bool $showDevTools = false;

protected bool $devToolsOpen = false;

protected bool $resizable = true;

protected bool $movable = true;
Expand All @@ -40,6 +44,8 @@ class Window

protected bool $focusable = true;

protected bool $focused = false;

protected bool $hasShadow = true;

protected bool $frame = true;
Expand All @@ -56,6 +62,8 @@ class Window

protected array $afterOpenCallbacks = [];

protected array $webPreferences = [];

public function __construct(string $id)
{
$this->id = $id;
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 [
Expand Down Expand Up @@ -273,6 +343,7 @@ public function toArray()
'kiosk' => $this->kiosk,
'autoHideMenuBar' => $this->autoHideMenuBar,
'transparent' => $this->transparent,
'webPreferences' => $this->webPreferences,
];
}

Expand All @@ -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;
}
}
19 changes: 16 additions & 3 deletions src/Windows/WindowManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 2c89fba

Please sign in to comment.