Skip to content

Commit a3a8936

Browse files
committed
Extract MenuBuilder
1 parent 1df3c34 commit a3a8936

File tree

3 files changed

+184
-81
lines changed

3 files changed

+184
-81
lines changed

src/Facades/Menu.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Native\Laravel\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
/**
8+
* @method static void create()
9+
*/
10+
class Menu extends Facade
11+
{
12+
protected static function getFacadeAccessor()
13+
{
14+
return \Native\Laravel\Menu\MenuBuilder::class;
15+
}
16+
}

src/Menu/Menu.php

Lines changed: 7 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,10 @@ class Menu implements MenuItem
1919

2020
protected array $items = [];
2121

22-
protected string $prepend = '';
22+
protected string $label = '';
2323

2424
public function __construct(protected Client $client) {}
2525

26-
public static function new(): static
27-
{
28-
return new static(new Client);
29-
}
30-
3126
public function register(): void
3227
{
3328
$items = $this->toArray()['submenu'];
@@ -37,81 +32,11 @@ public function register(): void
3732
]);
3833
}
3934

40-
public function prepend(string $prepend): self
41-
{
42-
$this->prepend = $prepend;
43-
44-
return $this;
45-
}
46-
47-
public function submenu(string $header, Menu $submenu): static
48-
{
49-
return $this->add($submenu->prepend($header));
50-
}
51-
52-
public function separator(): static
53-
{
54-
return $this->add(new Separator);
55-
}
56-
57-
public function quit(): static
58-
{
59-
return $this->add(new Role(RolesEnum::QUIT));
60-
}
61-
6235
public function label(string $label): self
6336
{
64-
return $this->add(new Label($label));
65-
}
66-
67-
public function checkbox(string $label, bool $checked = false, ?string $hotkey = null): self
68-
{
69-
return $this->add(new Checkbox($label, $checked, $hotkey));
70-
}
71-
72-
public function event(string $event, string $text, ?string $hotkey = null): self
73-
{
74-
return $this->add(new Event($event, $text, $hotkey));
75-
}
37+
$this->label = $label;
7638

77-
public function link(string $url, string $text, ?string $hotkey = null): self
78-
{
79-
return $this->add(new Link($url, $text, $hotkey));
80-
}
81-
82-
public function appMenu(): static
83-
{
84-
return $this->add(new Role(RolesEnum::APP_MENU));
85-
}
86-
87-
public function fileMenu($label = 'File'): static
88-
{
89-
return $this->add(new Role(RolesEnum::FILE_MENU, $label));
90-
}
91-
92-
public function editMenu($label = 'Edit'): static
93-
{
94-
return $this->add(new Role(RolesEnum::EDIT_MENU, $label));
95-
}
96-
97-
public function viewMenu($label = 'View'): static
98-
{
99-
return $this->add(new Role(RolesEnum::VIEW_MENU, $label));
100-
}
101-
102-
public function windowMenu($label = 'Window'): static
103-
{
104-
return $this->add(new Role(RolesEnum::WINDOW_MENU, $label));
105-
}
106-
107-
public function toggleFullscreen(): static
108-
{
109-
return $this->add(new Role(RolesEnum::TOGGLE_FULL_SCREEN));
110-
}
111-
112-
public function toggleDevTools(): static
113-
{
114-
return $this->add(new Role(RolesEnum::TOGGLE_DEV_TOOLS));
39+
return $this;
11540
}
11641

11742
public function add(MenuItem $item): self
@@ -123,11 +48,12 @@ public function add(MenuItem $item): self
12348

12449
public function toArray(): array
12550
{
126-
$items = collect($this->items)->map(fn (MenuItem $item) => $item->toArray())->toArray();
127-
$label = $this->prepend;
51+
$items = collect($this->items)
52+
->map(fn (MenuItem $item) => $item->toArray())
53+
->toArray();
12854

12955
return [
130-
'label' => $label,
56+
'label' => $this->label,
13157
'submenu' => $items,
13258
];
13359
}

src/Menu/MenuBuilder.php

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
<?php
2+
3+
namespace Native\Laravel\Menu;
4+
5+
use Native\Laravel\Client\Client;
6+
use Native\Laravel\Contracts\MenuItem;
7+
use Native\Laravel\Enums\RolesEnum;
8+
use Native\Laravel\Menu\Items;
9+
10+
class MenuBuilder
11+
{
12+
public function __construct(protected Client $client) {}
13+
14+
public function make(MenuItem ...$items): Menu
15+
{
16+
$menu = new Menu($this->client);
17+
18+
foreach ($items as $item) {
19+
$menu->add($item);
20+
}
21+
22+
return $menu;
23+
}
24+
25+
public function create(MenuItem ...$items): void
26+
{
27+
$this->make(...$items)
28+
->register();
29+
}
30+
31+
public function default(): void
32+
{
33+
$this->create(
34+
$this->app(),
35+
$this->file(),
36+
$this->edit(),
37+
$this->view(),
38+
$this->window(),
39+
);
40+
}
41+
42+
public function label(): Items\Label
43+
{
44+
return new Items\Label($label);
45+
}
46+
47+
public function goToUrl(string $url, string $label = null, ?string $hotkey = null): Items\GoToUrl
48+
{
49+
return new Items\GoToUrl($url, $label, $hotkey);
50+
}
51+
52+
public function goToRoute(string $route, string $label = null, ?string $hotkey = null): Items\GoToUrl
53+
{
54+
return new Items\GoToUrl(route($route), $label, $hotkey);
55+
}
56+
57+
public function checkbox(string $label, bool $checked = false, ?string $hotkey = null): Items\Checkbox
58+
{
59+
return new Items\Checkbox($label, $checked, $hotkey);
60+
}
61+
62+
public function event(string $event, string $label = null, ?string $hotkey = null): Items\Event
63+
{
64+
return new Items\Event($event, $label, $hotkey);
65+
}
66+
67+
public function link(string $url, string $label = null, ?string $hotkey = null): Items\Link
68+
{
69+
return new Items\Link($url, $label, $hotkey);
70+
}
71+
72+
public function app(): Items\Role
73+
{
74+
return new Items\Role(RolesEnum::APP_MENU);
75+
}
76+
77+
public function file($label = 'File'): Items\Role
78+
{
79+
return new Items\Role(RolesEnum::FILE_MENU, $label);
80+
}
81+
82+
public function edit($label = 'Edit'): Items\Role
83+
{
84+
return new Items\Role(RolesEnum::EDIT_MENU, $label);
85+
}
86+
87+
public function view($label = 'View'): Items\Role
88+
{
89+
return new Items\Role(RolesEnum::VIEW_MENU, $label);
90+
}
91+
92+
public function window($label = 'Window'): Items\Role
93+
{
94+
return new Items\Role(RolesEnum::WINDOW_MENU, $label);
95+
}
96+
97+
public function separator(): Items\Separator
98+
{
99+
return new Items\Separator;
100+
}
101+
102+
public function fullscreen(): Items\Role
103+
{
104+
return new Items\Role(RolesEnum::TOGGLE_FULL_SCREEN);
105+
}
106+
107+
public function devTools(): Items\Role
108+
{
109+
return new Items\Role(RolesEnum::TOGGLE_DEV_TOOLS);
110+
}
111+
112+
public function undo(): Items\Role
113+
{
114+
return new Items\Role(RolesEnum::UNDO);
115+
}
116+
117+
public function redo(): Items\Role
118+
{
119+
return new Items\Role(RolesEnum::REDO);
120+
}
121+
122+
public function cut(): Items\Role
123+
{
124+
return new Items\Role(RolesEnum::CUT);
125+
}
126+
127+
public function copy(): Items\Role
128+
{
129+
return new Items\Role(RolesEnum::COPY);
130+
}
131+
132+
public function paste(): Items\Role
133+
{
134+
return new Items\Role(RolesEnum::PASTE);
135+
}
136+
137+
public function pasteAndMatchStyle(): Items\Role
138+
{
139+
return new Items\Role(RolesEnum::PASTE_STYLE);
140+
}
141+
142+
public function reload(): Items\Role
143+
{
144+
return new Items\Role(RolesEnum::RELOAD);
145+
}
146+
147+
public function minimize(): Items\Role
148+
{
149+
return new Items\Role(RolesEnum::MINIMIZE);
150+
}
151+
152+
public function close(): Items\Role
153+
{
154+
return new Items\Role(RolesEnum::PASTE_STYLE);
155+
}
156+
157+
public function quit(): Items\Role
158+
{
159+
return new Items\Role(RolesEnum::QUIT);
160+
}
161+
}

0 commit comments

Comments
 (0)