Skip to content

Commit 5e47725

Browse files
committed
Add webview manager
1 parent 1d2489a commit 5e47725

26 files changed

Lines changed: 918 additions & 391 deletions

resources/stubs/saucer.stub.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,39 @@
1515
final class CSaucerWebViewEventsStruct extends CData
1616
{
1717
/**
18-
* @var \Closure(CData):void
18+
* @var \Closure(CData, CData):void
1919
*/
2020
public \Closure $onDomReady;
2121

2222
/**
23-
* @var \Closure(CData, string):void
23+
* @var \Closure(CData, string, CData):void
2424
*/
2525
public \Closure $onNavigated;
2626

2727
/**
28-
* @var \Closure(CData, CData):void
28+
* @var \Closure(CData, CData, CData):void
2929
*/
3030
public \Closure $onNavigating;
3131

3232
/**
33-
* @var \Closure(CData, CData):void
33+
* @var \Closure(CData, CData, CData):void
3434
*/
3535
public \Closure $onFaviconChanged;
3636

3737
/**
38-
* @var \Closure(CData, string):void
38+
* @var \Closure(CData, string, CData):void
3939
*/
4040
public \Closure $onTitleChanged;
4141

4242
/**
43-
* @var \Closure(CData, array{State::SAUCER_STATE_*}):void
43+
* @var \Closure(CData, State::SAUCER_STATE_*, CData):void
4444
*/
4545
public \Closure $onLoad;
46+
47+
/**
48+
* @var \Closure(CData, string, int<0, max>, CData):void
49+
*/
50+
public \Closure $onMessage;
4651
}
4752

4853
}

src/Application.php

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
use Boson\Event\ApplicationStarting;
1616
use Boson\Event\ApplicationStopped;
1717
use Boson\Event\ApplicationStopping;
18+
use Boson\Exception\ApplicationException;
1819
use Boson\Exception\NoDefaultWindowException;
1920
use Boson\Extension\Exception\ExtensionNotFoundException;
2021
use Boson\Extension\Registry;
2122
use Boson\Internal\Poller\SaucerPoller;
22-
use Boson\Internal\ThreadsCountResolver;
2323
use Boson\Poller\PollerInterface;
2424
use Boson\Shared\Marker\BlockingOperation;
2525
use Boson\Shared\Marker\RequiresDealloc;
@@ -200,7 +200,7 @@ public function __construct(
200200

201201
// Kernel initialization
202202
$this->saucer = $this->createLibSaucer($info->library);
203-
$this->id = $this->createApplicationId($this->saucer, $this->info->name, $this->info->threads);
203+
$this->id = $this->createApplicationId($this->saucer, $this->info->name);
204204
$this->windows = $this->createWindowManager($this->saucer, $this, $info, $this->listener);
205205
$this->poller = $this->createApplicationPoller($this->saucer);
206206

@@ -323,7 +323,7 @@ protected function createWindowManager(
323323
private function registerSchemes(): void
324324
{
325325
foreach ($this->info->schemes as $scheme) {
326-
$this->saucer->saucer_register_scheme($scheme);
326+
$this->saucer->saucer_webview_register_scheme($scheme);
327327
}
328328
}
329329

@@ -358,8 +358,7 @@ private function onWindowClose(): void
358358
*/
359359
private function onApplicationStarted(): void
360360
{
361-
// Resolve main window lazy proxy (facade)
362-
$_ = $this->window->isClosed;
361+
$this->windows->boot();
363362
}
364363

365364
/**
@@ -379,13 +378,12 @@ protected function createEventListener(?EventDispatcherInterface $dispatcher): E
379378
* Creates a new application ID
380379
*
381380
* @param non-empty-string $name
382-
* @param int<1, max>|null $threads
383381
*/
384-
protected function createApplicationId(SaucerInterface $api, string $name, ?int $threads): ApplicationId
382+
protected function createApplicationId(SaucerInterface $api, string $name): ApplicationId
385383
{
386384
return ApplicationId::fromAppHandle(
387385
api: $api,
388-
handle: $this->createApplicationPointer($api, $name, $threads),
386+
handle: $this->createApplicationPointer($api, $name),
389387
name: $name,
390388
);
391389
}
@@ -394,38 +392,36 @@ protected function createApplicationId(SaucerInterface $api, string $name, ?int
394392
* Creates a new application instance pointer.
395393
*
396394
* @param non-empty-string $name
397-
* @param int<1, max>|null $threads
398395
*/
399396
#[RequiresDealloc]
400-
protected function createApplicationPointer(SaucerInterface $api, string $name, ?int $threads): CData
397+
protected function createApplicationPointer(SaucerInterface $api, string $name): CData
401398
{
402-
$options = $this->createApplicationOptionsPointer($api, $name, $threads);
399+
$options = $this->createApplicationOptionsPointer($api, $name);
403400

404401
try {
405-
return $api->saucer_application_init($options);
402+
$result = $api->saucer_application_new($options, \FFI::addr(
403+
$error = $this->saucer->new('int'),
404+
));
405+
406+
if ($error->cdata !== 0) {
407+
throw new ApplicationException('An internal error occurred while creating application', $error->cdata);
408+
}
409+
410+
return $result;
406411
} finally {
407-
$api->saucer_options_free($options);
412+
$api->saucer_application_options_free($options);
408413
}
409414
}
410415

411416
/**
412417
* Creates a new application options pointer.
413418
*
414419
* @param non-empty-string $name
415-
* @param int<1, max>|null $threads
416420
*/
417421
#[RequiresDealloc]
418-
protected function createApplicationOptionsPointer(SaucerInterface $api, string $name, ?int $threads): CData
422+
protected function createApplicationOptionsPointer(SaucerInterface $api, string $name): CData
419423
{
420-
$options = $api->saucer_options_new($name);
421-
422-
$threads = ThreadsCountResolver::resolve($threads);
423-
424-
if ($threads !== null) {
425-
$api->saucer_options_set_threads($options, $threads);
426-
}
427-
428-
return $options;
424+
return $api->saucer_application_options_new($name);
429425
}
430426

431427
/**

src/ApplicationCreateInfo.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,6 @@ public function __construct(
7474
*/
7575
public string $name = self::DEFAULT_APPLICATION_NAME,
7676
iterable $schemes = [],
77-
/**
78-
* An application threads count.
79-
*
80-
* The number of threads will be determined automatically if it
81-
* is not explicitly specified (defined as {@see null}).
82-
*
83-
* @var int<1, max>|null
84-
*/
85-
public ?int $threads = null,
8677
/**
8778
* Automatically detects debug environment if {@see null},
8879
* otherwise it forcibly enables or disables it.

src/Internal/Poller/SaucerPoller.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ final class SaucerPoller implements PollerInterface
4040
private readonly CData $ptr;
4141

4242
public function __construct(
43-
private readonly ApplicationId $id,
43+
ApplicationId $id,
4444
private readonly SaucerInterface $saucer,
4545
) {
4646
$this->ids = $this->createIdValueGenerator();
47-
$this->ptr = $this->id->ptr;
47+
$this->ptr = $this->saucer->saucer_loop_new($id->ptr);
4848
}
4949

5050
/**
@@ -89,7 +89,7 @@ public function next(): void
8989

9090
private function executeInternalTask(): void
9191
{
92-
$this->saucer->saucer_application_run_once($this->ptr);
92+
$this->saucer->saucer_loop_iteration($this->ptr);
9393
}
9494

9595
private function executePeriodicTask(): void
@@ -156,4 +156,9 @@ public function cancel(int|string $taskId): void
156156
{
157157
unset($this->periodicMicroTasks[$taskId], $this->microTasks[$taskId]);
158158
}
159+
160+
public function __destruct()
161+
{
162+
$this->saucer->saucer_loop_free($this->ptr);
163+
}
159164
}

src/Internal/PositiveIntId.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Boson\Internal;
6+
7+
use Boson\Contracts\Id\IntIdInterface;
8+
9+
/**
10+
* @template-implements IntIdInterface<int<0, max>>
11+
*/
12+
abstract readonly class PositiveIntId implements IntIdInterface
13+
{
14+
final protected function __construct(
15+
/**
16+
* @var int<0, max>
17+
*/
18+
protected int $id,
19+
) {}
20+
21+
/**
22+
* @param int<0, max> $id
23+
*/
24+
public static function new(int $id): static
25+
{
26+
return new static($id);
27+
}
28+
29+
public function toInteger(): int
30+
{
31+
return $this->id;
32+
}
33+
34+
public function equals(mixed $other): bool
35+
{
36+
return $other === $this
37+
|| ($other instanceof self
38+
&& $this->id === $other->id);
39+
}
40+
41+
public function __debugInfo(): array
42+
{
43+
return [
44+
'id' => $this->id,
45+
];
46+
}
47+
48+
/**
49+
* @return non-empty-string
50+
*/
51+
public function __toString(): string
52+
{
53+
return \sprintf('%s(%d)', static::class, $this->id);
54+
}
55+
}

src/Internal/ThreadsCountResolver.php

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
 (0)