Skip to content

Commit 2c89fba

Browse files
authored
Improved window management (#391)
* 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
1 parent 27cc28b commit 2c89fba

File tree

4 files changed

+104
-15
lines changed

4 files changed

+104
-15
lines changed

src/Concerns/HasUrl.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function url(string $url): self
1515

1616
public function route(string $route, array $parameters = []): self
1717
{
18-
$this->url = route($route, $parameters);
18+
$this->url(route($route, $parameters));
1919

2020
return $this;
2121
}

src/Dialog.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,7 @@ public function properties(array $properties): self
110110

111111
public function asSheet(?string $windowId = null): self
112112
{
113-
if (is_null($windowId)) {
114-
$this->windowReference = Window::current()->id;
115-
} else {
116-
$this->windowReference = $windowId;
117-
}
113+
$this->windowReference = $windowId ?? Window::current()->getId();
118114

119115
return $this;
120116
}

src/Windows/Window.php

Lines changed: 86 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
class Window
1212
{
1313
use HasDimensions;
14-
use HasUrl;
14+
use HasUrl {
15+
HasUrl::url as defaultUrl;
16+
}
1517
use HasVibrancy;
1618

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

2931
protected bool $showDevTools = false;
3032

33+
protected bool $devToolsOpen = false;
34+
3135
protected bool $resizable = true;
3236

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

4145
protected bool $focusable = true;
4246

47+
protected bool $focused = false;
48+
4349
protected bool $hasShadow = true;
4450

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

5763
protected array $afterOpenCallbacks = [];
5864

65+
protected array $webPreferences = [];
66+
5967
public function __construct(string $id)
6068
{
6169
$this->id = $id;
@@ -71,10 +79,36 @@ public function id(string $id = 'main'): self
7179
return $this;
7280
}
7381

82+
public function getId(): string
83+
{
84+
return $this->id;
85+
}
86+
7487
public function title(string $title): self
7588
{
7689
$this->title = $title;
7790

91+
if (! $this instanceof PendingOpenWindow) {
92+
$this->client->post('window/title', [
93+
'id' => $this->id,
94+
'title' => $title,
95+
]);
96+
}
97+
98+
return $this;
99+
}
100+
101+
public function url(string $url)
102+
{
103+
$this->defaultUrl($url);
104+
105+
if (! $this instanceof PendingOpenWindow) {
106+
$this->client->post('window/url', [
107+
'id' => $this->id,
108+
'url' => $url,
109+
]);
110+
}
111+
78112
return $this;
79113
}
80114

@@ -142,21 +176,43 @@ public function setClient(Client $client): self
142176
return $this;
143177
}
144178

145-
public function alwaysOnTop($alwaysOnTop = true): self
179+
public function alwaysOnTop(bool $alwaysOnTop = true): self
146180
{
147181
$this->alwaysOnTop = $alwaysOnTop;
148182

149183
return $this;
150184
}
151185

152-
public function showDevTools($showDevTools = true): self
186+
public function showDevTools(bool $showDevTools = true): self
153187
{
154188
$this->showDevTools = $showDevTools;
155189

190+
if (! $this instanceof PendingOpenWindow) {
191+
$this->client->post('window/show-dev-tools', [
192+
'id' => $this->id,
193+
]);
194+
}
195+
196+
return $this;
197+
}
198+
199+
public function hideDevTools(): self
200+
{
201+
if (! $this instanceof PendingOpenWindow) {
202+
$this->client->post('window/hide-dev-tools', [
203+
'id' => $this->id,
204+
]);
205+
}
206+
156207
return $this;
157208
}
158209

159-
public function resizable($resizable = true): static
210+
public function devToolsOpen(): bool
211+
{
212+
return $this->devToolsOpen;
213+
}
214+
215+
public function resizable(bool $resizable = true): static
160216
{
161217
$this->resizable = $resizable;
162218

@@ -194,10 +250,17 @@ public function maximized(): static
194250
return $this->afterOpen(fn () => WindowFacade::maximize($this->id));
195251
}
196252

197-
public function closable($closable = true): static
253+
public function closable(bool $closable = true): static
198254
{
199255
$this->closable = $closable;
200256

257+
if (! $this instanceof PendingOpenWindow) {
258+
$this->client->post('window/closable', [
259+
'id' => $this->id,
260+
'closable' => $closable,
261+
]);
262+
}
263+
201264
return $this;
202265
}
203266

@@ -231,13 +294,20 @@ public function fullscreenable($fullscreenable = true): static
231294
return $this;
232295
}
233296

234-
public function kiosk($kiosk = false): static
297+
public function kiosk($kiosk = true): static
235298
{
236299
$this->kiosk = $kiosk;
237300

238301
return $this;
239302
}
240303

304+
public function webPreferences(array $preferences): static
305+
{
306+
$this->webPreferences = $preferences;
307+
308+
return $this;
309+
}
310+
241311
public function toArray()
242312
{
243313
return [
@@ -273,6 +343,7 @@ public function toArray()
273343
'kiosk' => $this->kiosk,
274344
'autoHideMenuBar' => $this->autoHideMenuBar,
275345
'transparent' => $this->transparent,
346+
'webPreferences' => $this->webPreferences,
276347
];
277348
}
278349

@@ -282,4 +353,13 @@ public function afterOpen(callable $cb): static
282353

283354
return $this;
284355
}
356+
357+
public function fromRuntimeWindow(object $window): static
358+
{
359+
foreach ($window as $key => $value) {
360+
$this->{$key} = $value;
361+
}
362+
363+
return $this;
364+
}
285365
}

src/Windows/WindowManager.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,27 @@ public function close($id = null)
2525

2626
public function hide($id = null)
2727
{
28-
return $this->client->post('window/hide', [
28+
$this->client->post('window/hide', [
2929
'id' => $id ?? $this->detectId(),
3030
]);
3131
}
3232

33-
public function current()
33+
public function current(): Window
3434
{
35-
return (object) $this->client->get('window/current')->json();
35+
$window = (object) $this->client->get('window/current')->json();
36+
37+
return (new Window($window->id))
38+
->setClient($this->client)
39+
->fromRuntimeWindow($window);
40+
}
41+
42+
public function get(string $id): Window
43+
{
44+
$window = (object) $this->client->get("window/get/{$id}")->json();
45+
46+
return (new Window($id))
47+
->setClient($this->client)
48+
->fromRuntimeWindow($window);
3649
}
3750

3851
public function resize($width, $height, $id = null)

0 commit comments

Comments
 (0)