From a3a8936fca6a5f40648180f99249fa278f1b296e Mon Sep 17 00:00:00 2001 From: Simon Hamp Date: Wed, 20 Nov 2024 00:06:40 +0000 Subject: [PATCH 01/22] Extract MenuBuilder --- src/Facades/Menu.php | 16 ++++ src/Menu/Menu.php | 88 ++------------------- src/Menu/MenuBuilder.php | 161 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 184 insertions(+), 81 deletions(-) create mode 100644 src/Facades/Menu.php create mode 100644 src/Menu/MenuBuilder.php diff --git a/src/Facades/Menu.php b/src/Facades/Menu.php new file mode 100644 index 0000000..020683c --- /dev/null +++ b/src/Facades/Menu.php @@ -0,0 +1,16 @@ +toArray()['submenu']; @@ -37,81 +32,11 @@ public function register(): void ]); } - public function prepend(string $prepend): self - { - $this->prepend = $prepend; - - return $this; - } - - public function submenu(string $header, Menu $submenu): static - { - return $this->add($submenu->prepend($header)); - } - - public function separator(): static - { - return $this->add(new Separator); - } - - public function quit(): static - { - return $this->add(new Role(RolesEnum::QUIT)); - } - public function label(string $label): self { - return $this->add(new Label($label)); - } - - public function checkbox(string $label, bool $checked = false, ?string $hotkey = null): self - { - return $this->add(new Checkbox($label, $checked, $hotkey)); - } - - public function event(string $event, string $text, ?string $hotkey = null): self - { - return $this->add(new Event($event, $text, $hotkey)); - } + $this->label = $label; - public function link(string $url, string $text, ?string $hotkey = null): self - { - return $this->add(new Link($url, $text, $hotkey)); - } - - public function appMenu(): static - { - return $this->add(new Role(RolesEnum::APP_MENU)); - } - - public function fileMenu($label = 'File'): static - { - return $this->add(new Role(RolesEnum::FILE_MENU, $label)); - } - - public function editMenu($label = 'Edit'): static - { - return $this->add(new Role(RolesEnum::EDIT_MENU, $label)); - } - - public function viewMenu($label = 'View'): static - { - return $this->add(new Role(RolesEnum::VIEW_MENU, $label)); - } - - public function windowMenu($label = 'Window'): static - { - return $this->add(new Role(RolesEnum::WINDOW_MENU, $label)); - } - - public function toggleFullscreen(): static - { - return $this->add(new Role(RolesEnum::TOGGLE_FULL_SCREEN)); - } - - public function toggleDevTools(): static - { - return $this->add(new Role(RolesEnum::TOGGLE_DEV_TOOLS)); + return $this; } public function add(MenuItem $item): self @@ -123,11 +48,12 @@ public function add(MenuItem $item): self public function toArray(): array { - $items = collect($this->items)->map(fn (MenuItem $item) => $item->toArray())->toArray(); - $label = $this->prepend; + $items = collect($this->items) + ->map(fn (MenuItem $item) => $item->toArray()) + ->toArray(); return [ - 'label' => $label, + 'label' => $this->label, 'submenu' => $items, ]; } diff --git a/src/Menu/MenuBuilder.php b/src/Menu/MenuBuilder.php new file mode 100644 index 0000000..cd00dea --- /dev/null +++ b/src/Menu/MenuBuilder.php @@ -0,0 +1,161 @@ +client); + + foreach ($items as $item) { + $menu->add($item); + } + + return $menu; + } + + public function create(MenuItem ...$items): void + { + $this->make(...$items) + ->register(); + } + + public function default(): void + { + $this->create( + $this->app(), + $this->file(), + $this->edit(), + $this->view(), + $this->window(), + ); + } + + public function label(): Items\Label + { + return new Items\Label($label); + } + + public function goToUrl(string $url, string $label = null, ?string $hotkey = null): Items\GoToUrl + { + return new Items\GoToUrl($url, $label, $hotkey); + } + + public function goToRoute(string $route, string $label = null, ?string $hotkey = null): Items\GoToUrl + { + return new Items\GoToUrl(route($route), $label, $hotkey); + } + + public function checkbox(string $label, bool $checked = false, ?string $hotkey = null): Items\Checkbox + { + return new Items\Checkbox($label, $checked, $hotkey); + } + + public function event(string $event, string $label = null, ?string $hotkey = null): Items\Event + { + return new Items\Event($event, $label, $hotkey); + } + + public function link(string $url, string $label = null, ?string $hotkey = null): Items\Link + { + return new Items\Link($url, $label, $hotkey); + } + + public function app(): Items\Role + { + return new Items\Role(RolesEnum::APP_MENU); + } + + public function file($label = 'File'): Items\Role + { + return new Items\Role(RolesEnum::FILE_MENU, $label); + } + + public function edit($label = 'Edit'): Items\Role + { + return new Items\Role(RolesEnum::EDIT_MENU, $label); + } + + public function view($label = 'View'): Items\Role + { + return new Items\Role(RolesEnum::VIEW_MENU, $label); + } + + public function window($label = 'Window'): Items\Role + { + return new Items\Role(RolesEnum::WINDOW_MENU, $label); + } + + public function separator(): Items\Separator + { + return new Items\Separator; + } + + public function fullscreen(): Items\Role + { + return new Items\Role(RolesEnum::TOGGLE_FULL_SCREEN); + } + + public function devTools(): Items\Role + { + return new Items\Role(RolesEnum::TOGGLE_DEV_TOOLS); + } + + public function undo(): Items\Role + { + return new Items\Role(RolesEnum::UNDO); + } + + public function redo(): Items\Role + { + return new Items\Role(RolesEnum::REDO); + } + + public function cut(): Items\Role + { + return new Items\Role(RolesEnum::CUT); + } + + public function copy(): Items\Role + { + return new Items\Role(RolesEnum::COPY); + } + + public function paste(): Items\Role + { + return new Items\Role(RolesEnum::PASTE); + } + + public function pasteAndMatchStyle(): Items\Role + { + return new Items\Role(RolesEnum::PASTE_STYLE); + } + + public function reload(): Items\Role + { + return new Items\Role(RolesEnum::RELOAD); + } + + public function minimize(): Items\Role + { + return new Items\Role(RolesEnum::MINIMIZE); + } + + public function close(): Items\Role + { + return new Items\Role(RolesEnum::PASTE_STYLE); + } + + public function quit(): Items\Role + { + return new Items\Role(RolesEnum::QUIT); + } +} From bff79d741651db263718e53cea93fbd14995b63d Mon Sep 17 00:00:00 2001 From: Simon Hamp Date: Wed, 20 Nov 2024 00:07:05 +0000 Subject: [PATCH 02/22] Add more Electron MenuItem roles --- src/Enums/RolesEnum.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Enums/RolesEnum.php b/src/Enums/RolesEnum.php index 337c479..372ed93 100644 --- a/src/Enums/RolesEnum.php +++ b/src/Enums/RolesEnum.php @@ -9,7 +9,17 @@ enum RolesEnum: string case EDIT_MENU = 'editMenu'; case VIEW_MENU = 'viewMenu'; case WINDOW_MENU = 'windowMenu'; + case UNDO = 'undo'; + case REDO = 'redo'; + case CUT = 'cut'; + case COPY = 'copy'; + case PASTE = 'paste'; + case PASTE_STYLE = 'pasteAndMatchStyle'; + case RELOAD = 'reload'; + case MINIMIZE = 'minimize'; + case CLOSE = 'close'; case QUIT = 'quit'; case TOGGLE_FULL_SCREEN = 'togglefullscreen'; case TOGGLE_DEV_TOOLS = 'toggleDevTools'; + case ABOUT = 'about'; } From 9d407ce527806a5c1d9a857733984a1569a30b65 Mon Sep 17 00:00:00 2001 From: Simon Hamp Date: Wed, 20 Nov 2024 00:07:44 +0000 Subject: [PATCH 03/22] Allow MenuItems to have submenus --- src/Menu/Items/MenuItem.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Menu/Items/MenuItem.php b/src/Menu/Items/MenuItem.php index 34d41ba..c1c52ee 100644 --- a/src/Menu/Items/MenuItem.php +++ b/src/Menu/Items/MenuItem.php @@ -3,6 +3,8 @@ namespace Native\Laravel\Menu\Items; use Native\Laravel\Contracts\MenuItem as MenuItemContract; +use Native\Laravel\Facades\Menu as MenuFacade; +use Native\Laravel\Menu\Menu; abstract class MenuItem implements MenuItemContract { @@ -18,6 +20,8 @@ abstract class MenuItem implements MenuItemContract protected ?string $toolTip = null; + protected ?Menu $submenu = null; + protected bool $isEnabled = true; protected bool $isVisible = true; @@ -80,6 +84,13 @@ public function toolTip(string $toolTip): self return $this; } + public function submenu(MenuItemContract ...$items): self + { + $this->submenu = MenuFacade::make(...$items); + + return $this; + } + public function toArray(): array { return array_filter([ @@ -92,6 +103,7 @@ public function toArray(): array 'checked' => $this->isChecked, 'accelerator' => $this->accelerator, 'icon' => $this->icon, + 'submenu' => $this->submenu?->toArray(), ], fn ($value) => $value !== null); } } From 97bc40b58f1423f04187794a14cd50e129cd96d9 Mon Sep 17 00:00:00 2001 From: Simon Hamp Date: Wed, 20 Nov 2024 00:07:53 +0000 Subject: [PATCH 04/22] enabled/disabled --- src/Menu/Items/MenuItem.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Menu/Items/MenuItem.php b/src/Menu/Items/MenuItem.php index c1c52ee..d5d3e5f 100644 --- a/src/Menu/Items/MenuItem.php +++ b/src/Menu/Items/MenuItem.php @@ -28,9 +28,16 @@ abstract class MenuItem implements MenuItemContract protected bool $isChecked = false; - public function enabled($enabled = true): self + public function enabled(): self { - $this->isEnabled = $enabled; + $this->isEnabled = true; + + return $this; + } + + public function disabled(): self + { + $this->isEnabled = false; return $this; } From 0e2429f0af49b210861cdf1abc1a227e6a62065d Mon Sep 17 00:00:00 2001 From: Simon Hamp Date: Wed, 20 Nov 2024 00:09:29 +0000 Subject: [PATCH 05/22] Add GoToUrl convenience item --- src/Menu/Items/GoToUrl.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/Menu/Items/GoToUrl.php diff --git a/src/Menu/Items/GoToUrl.php b/src/Menu/Items/GoToUrl.php new file mode 100644 index 0000000..4e232a9 --- /dev/null +++ b/src/Menu/Items/GoToUrl.php @@ -0,0 +1,21 @@ + 'goto', + 'url' => $this->url, + 'label' => $this->label, + ]); + } +} From 428819b360897db8e06009d85b2a3c582546caab Mon Sep 17 00:00:00 2001 From: simonhamp Date: Wed, 20 Nov 2024 00:30:25 +0000 Subject: [PATCH 06/22] Fix styling --- src/Facades/Menu.php | 4 ++-- src/Menu/Menu.php | 7 ------- src/Menu/MenuBuilder.php | 11 +++++------ 3 files changed, 7 insertions(+), 15 deletions(-) diff --git a/src/Facades/Menu.php b/src/Facades/Menu.php index 020683c..e3a6256 100644 --- a/src/Facades/Menu.php +++ b/src/Facades/Menu.php @@ -5,8 +5,8 @@ use Illuminate\Support\Facades\Facade; /** -* @method static void create() -*/ + * @method static void create() + */ class Menu extends Facade { protected static function getFacadeAccessor() diff --git a/src/Menu/Menu.php b/src/Menu/Menu.php index 16298f2..7710a7d 100644 --- a/src/Menu/Menu.php +++ b/src/Menu/Menu.php @@ -5,13 +5,6 @@ use Illuminate\Support\Traits\Conditionable; use Native\Laravel\Client\Client; use Native\Laravel\Contracts\MenuItem; -use Native\Laravel\Enums\RolesEnum; -use Native\Laravel\Menu\Items\Checkbox; -use Native\Laravel\Menu\Items\Event; -use Native\Laravel\Menu\Items\Label; -use Native\Laravel\Menu\Items\Link; -use Native\Laravel\Menu\Items\Role; -use Native\Laravel\Menu\Items\Separator; class Menu implements MenuItem { diff --git a/src/Menu/MenuBuilder.php b/src/Menu/MenuBuilder.php index cd00dea..a93b534 100644 --- a/src/Menu/MenuBuilder.php +++ b/src/Menu/MenuBuilder.php @@ -5,7 +5,6 @@ use Native\Laravel\Client\Client; use Native\Laravel\Contracts\MenuItem; use Native\Laravel\Enums\RolesEnum; -use Native\Laravel\Menu\Items; class MenuBuilder { @@ -21,7 +20,7 @@ public function make(MenuItem ...$items): Menu return $menu; } - + public function create(MenuItem ...$items): void { $this->make(...$items) @@ -44,12 +43,12 @@ public function label(): Items\Label return new Items\Label($label); } - public function goToUrl(string $url, string $label = null, ?string $hotkey = null): Items\GoToUrl + public function goToUrl(string $url, ?string $label = null, ?string $hotkey = null): Items\GoToUrl { return new Items\GoToUrl($url, $label, $hotkey); } - public function goToRoute(string $route, string $label = null, ?string $hotkey = null): Items\GoToUrl + public function goToRoute(string $route, ?string $label = null, ?string $hotkey = null): Items\GoToUrl { return new Items\GoToUrl(route($route), $label, $hotkey); } @@ -59,12 +58,12 @@ public function checkbox(string $label, bool $checked = false, ?string $hotkey = return new Items\Checkbox($label, $checked, $hotkey); } - public function event(string $event, string $label = null, ?string $hotkey = null): Items\Event + public function event(string $event, ?string $label = null, ?string $hotkey = null): Items\Event { return new Items\Event($event, $label, $hotkey); } - public function link(string $url, string $label = null, ?string $hotkey = null): Items\Link + public function link(string $url, ?string $label = null, ?string $hotkey = null): Items\Link { return new Items\Link($url, $label, $hotkey); } From e066697ba90f3fe424657a007a832bc05ce67634 Mon Sep 17 00:00:00 2001 From: Simon Hamp Date: Wed, 20 Nov 2024 00:39:59 +0000 Subject: [PATCH 07/22] Add help and hide roles --- src/Enums/RolesEnum.php | 4 +++- src/Menu/MenuBuilder.php | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Enums/RolesEnum.php b/src/Enums/RolesEnum.php index 372ed93..0039d72 100644 --- a/src/Enums/RolesEnum.php +++ b/src/Enums/RolesEnum.php @@ -4,11 +4,12 @@ enum RolesEnum: string { - case APP_MENU = 'appMenu'; + case APP_MENU = 'appMenu'; // macOS case FILE_MENU = 'fileMenu'; case EDIT_MENU = 'editMenu'; case VIEW_MENU = 'viewMenu'; case WINDOW_MENU = 'windowMenu'; + case HELP = 'help'; // macOS case UNDO = 'undo'; case REDO = 'redo'; case CUT = 'cut'; @@ -16,6 +17,7 @@ enum RolesEnum: string case PASTE = 'paste'; case PASTE_STYLE = 'pasteAndMatchStyle'; case RELOAD = 'reload'; + case HIDE = 'hide'; // macOS case MINIMIZE = 'minimize'; case CLOSE = 'close'; case QUIT = 'quit'; diff --git a/src/Menu/MenuBuilder.php b/src/Menu/MenuBuilder.php index a93b534..a6beed9 100644 --- a/src/Menu/MenuBuilder.php +++ b/src/Menu/MenuBuilder.php @@ -157,4 +157,14 @@ public function quit(): Items\Role { return new Items\Role(RolesEnum::QUIT); } + + public function help(): Items\Role + { + return new Items\Role(RolesEnum::HELP); + } + + public function hide(): Items\Role + { + return new Items\Role(RolesEnum::HIDE); + } } From 4ad23a6df938a9ed10c26b66aaeca2be905fb268 Mon Sep 17 00:00:00 2001 From: Simon Hamp Date: Wed, 20 Nov 2024 02:23:30 +0000 Subject: [PATCH 08/22] Receive combo key data --- src/Events/Menu/MenuItemClicked.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Events/Menu/MenuItemClicked.php b/src/Events/Menu/MenuItemClicked.php index d9daa74..961ed1c 100644 --- a/src/Events/Menu/MenuItemClicked.php +++ b/src/Events/Menu/MenuItemClicked.php @@ -12,7 +12,7 @@ class MenuItemClicked implements ShouldBroadcastNow { use Dispatchable, InteractsWithSockets, SerializesModels; - public function __construct(public array $item) {} + public function __construct(public array $item, public array $combo = []) {} public function broadcastOn() { From c1874eeeb646db2c1dc51125ea0cf31c92e10ef4 Mon Sep 17 00:00:00 2001 From: Simon Hamp Date: Wed, 20 Nov 2024 02:25:14 +0000 Subject: [PATCH 09/22] Add id's to menu items --- src/Menu/Items/MenuItem.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Menu/Items/MenuItem.php b/src/Menu/Items/MenuItem.php index d5d3e5f..bbabd87 100644 --- a/src/Menu/Items/MenuItem.php +++ b/src/Menu/Items/MenuItem.php @@ -10,6 +10,8 @@ abstract class MenuItem implements MenuItemContract { protected string $type = 'normal'; + protected ?string $id = null; + protected ?string $label = null; protected ?string $sublabel = null; @@ -42,6 +44,13 @@ public function disabled(): self return $this; } + public function id(string $id): self + { + $this->id = $id; + + return $this; + } + public function label(string $label): self { $this->label = $label; @@ -84,7 +93,7 @@ public function checked($checked = true): self return $this; } - public function toolTip(string $toolTip): self + public function tooltip(string $toolTip): self { $this->toolTip = $toolTip; @@ -102,6 +111,7 @@ public function toArray(): array { return array_filter([ 'type' => $this->type, + 'id' => $this->id, 'label' => $this->label, 'sublabel' => $this->sublabel, 'toolTip' => $this->toolTip, From 37f0040bc2aa7dff5ee6a3a7babce8d555027874 Mon Sep 17 00:00:00 2001 From: Simon Hamp Date: Wed, 20 Nov 2024 02:25:32 +0000 Subject: [PATCH 10/22] Support radio items --- src/Menu/Items/Radio.php | 2 +- src/Menu/MenuBuilder.php | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Menu/Items/Radio.php b/src/Menu/Items/Radio.php index 63587a2..16753c7 100644 --- a/src/Menu/Items/Radio.php +++ b/src/Menu/Items/Radio.php @@ -6,7 +6,7 @@ class Radio extends MenuItem { protected string $type = 'radio'; - public function __construct(string $label) + public function __construct(string $label, protected bool $isChecked = false, protected ?string $accelerator = null) { $this->label = $label; } diff --git a/src/Menu/MenuBuilder.php b/src/Menu/MenuBuilder.php index a6beed9..a24ab86 100644 --- a/src/Menu/MenuBuilder.php +++ b/src/Menu/MenuBuilder.php @@ -5,6 +5,7 @@ use Native\Laravel\Client\Client; use Native\Laravel\Contracts\MenuItem; use Native\Laravel\Enums\RolesEnum; +use Native\Laravel\Menu\Items; class MenuBuilder { @@ -58,6 +59,11 @@ public function checkbox(string $label, bool $checked = false, ?string $hotkey = return new Items\Checkbox($label, $checked, $hotkey); } + public function radio(string $label, bool $checked = false, ?string $hotkey = null): Items\Radio + { + return new Items\Radio($label, $checked, $hotkey); + } + public function event(string $event, ?string $label = null, ?string $hotkey = null): Items\Event { return new Items\Event($event, $label, $hotkey); From 23c33638a36b42cc31859a2faca7e9230789efec Mon Sep 17 00:00:00 2001 From: Simon Hamp Date: Wed, 20 Nov 2024 02:35:31 +0000 Subject: [PATCH 11/22] Style --- src/Menu/Items/Checkbox.php | 7 +++++-- src/Menu/Items/Radio.php | 7 +++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Menu/Items/Checkbox.php b/src/Menu/Items/Checkbox.php index 7482170..afc550e 100644 --- a/src/Menu/Items/Checkbox.php +++ b/src/Menu/Items/Checkbox.php @@ -6,8 +6,11 @@ class Checkbox extends MenuItem { protected string $type = 'checkbox'; - public function __construct(string $label, protected bool $isChecked = false, protected ?string $accelerator = null) - { + public function __construct( + string $label, + protected bool $isChecked = false, + protected ?string $accelerator = null + ) { $this->label = $label; } } diff --git a/src/Menu/Items/Radio.php b/src/Menu/Items/Radio.php index 16753c7..0751522 100644 --- a/src/Menu/Items/Radio.php +++ b/src/Menu/Items/Radio.php @@ -6,8 +6,11 @@ class Radio extends MenuItem { protected string $type = 'radio'; - public function __construct(string $label, protected bool $isChecked = false, protected ?string $accelerator = null) - { + public function __construct( + string $label, + protected bool $isChecked = false, + protected ?string $accelerator = null + ) { $this->label = $label; } } From 7c1cca5b8df2d1924cc46ca596a07b3854e70a04 Mon Sep 17 00:00:00 2001 From: Simon Hamp Date: Wed, 20 Nov 2024 02:55:30 +0000 Subject: [PATCH 12/22] Remove custom event menu item type --- src/Menu/Items/Event.php | 17 ----------------- src/Menu/MenuBuilder.php | 5 ----- 2 files changed, 22 deletions(-) delete mode 100644 src/Menu/Items/Event.php diff --git a/src/Menu/Items/Event.php b/src/Menu/Items/Event.php deleted file mode 100644 index 02af55e..0000000 --- a/src/Menu/Items/Event.php +++ /dev/null @@ -1,17 +0,0 @@ - 'event', - 'event' => $this->event, - 'label' => $this->label, - ]); - } -} diff --git a/src/Menu/MenuBuilder.php b/src/Menu/MenuBuilder.php index a24ab86..871c0a7 100644 --- a/src/Menu/MenuBuilder.php +++ b/src/Menu/MenuBuilder.php @@ -64,11 +64,6 @@ public function radio(string $label, bool $checked = false, ?string $hotkey = nu return new Items\Radio($label, $checked, $hotkey); } - public function event(string $event, ?string $label = null, ?string $hotkey = null): Items\Event - { - return new Items\Event($event, $label, $hotkey); - } - public function link(string $url, ?string $label = null, ?string $hotkey = null): Items\Link { return new Items\Link($url, $label, $hotkey); From f8579389b89120752960619dcd0c45f84f346c52 Mon Sep 17 00:00:00 2001 From: Simon Hamp Date: Wed, 20 Nov 2024 02:55:54 +0000 Subject: [PATCH 13/22] Support custom event firing on all menu items --- src/Menu/Items/MenuItem.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Menu/Items/MenuItem.php b/src/Menu/Items/MenuItem.php index bbabd87..d9cf74d 100644 --- a/src/Menu/Items/MenuItem.php +++ b/src/Menu/Items/MenuItem.php @@ -30,6 +30,8 @@ abstract class MenuItem implements MenuItemContract protected bool $isChecked = false; + protected ?string $event = null; + public function enabled(): self { $this->isEnabled = true; @@ -107,12 +109,20 @@ public function submenu(MenuItemContract ...$items): self return $this; } + public function event(string $event): self + { + $this->event = $event; + + return $this; + } + public function toArray(): array { return array_filter([ 'type' => $this->type, 'id' => $this->id, 'label' => $this->label, + 'event' => $this->event, 'sublabel' => $this->sublabel, 'toolTip' => $this->toolTip, 'enabled' => $this->isEnabled, From 1c8931e80f8c81c59931cf8db7e5dc1d66590097 Mon Sep 17 00:00:00 2001 From: Simon Hamp Date: Wed, 20 Nov 2024 02:56:01 +0000 Subject: [PATCH 14/22] Fix label --- src/Menu/MenuBuilder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Menu/MenuBuilder.php b/src/Menu/MenuBuilder.php index 871c0a7..ddac245 100644 --- a/src/Menu/MenuBuilder.php +++ b/src/Menu/MenuBuilder.php @@ -39,7 +39,7 @@ public function default(): void ); } - public function label(): Items\Label + public function label(string $label): Items\Label { return new Items\Label($label); } From c6cc7e083a14fe9fe735f7a2eade560a53e8564a Mon Sep 17 00:00:00 2001 From: Simon Hamp Date: Wed, 20 Nov 2024 03:06:29 +0000 Subject: [PATCH 15/22] Type hints and consistency --- src/Facades/Menu.php | 39 +++++++++++++++++++++++- src/Menu/MenuBuilder.php | 64 ++++++++++++++++++++-------------------- 2 files changed, 70 insertions(+), 33 deletions(-) diff --git a/src/Facades/Menu.php b/src/Facades/Menu.php index e3a6256..ff9be32 100644 --- a/src/Facades/Menu.php +++ b/src/Facades/Menu.php @@ -3,9 +3,46 @@ namespace Native\Laravel\Facades; use Illuminate\Support\Facades\Facade; +use Native\Laravel\Menu\Items\Checkbox; +use Native\Laravel\Menu\Items\GoToUrl; +use Native\Laravel\Menu\Items\Label; +use Native\Laravel\Menu\Items\Link; +use Native\Laravel\Menu\Items\Radio; +use Native\Laravel\Menu\Items\Role; +use Native\Laravel\Menu\Items\Separator; /** - * @method static void create() +* @method static \Native\Laravel\Menu\Menu make(MenuItem ...$items) +* @method static Checkbox checkbox(string $label, bool $checked = false, ?string $hotkey = null) +* @method static GoToLink goToUrl(string $url, string $label = null, ?string $hotkey = null) +* @method static GoToLink goToRoute(string $url, string $label = null, ?string $hotkey = null) +* @method static Label label(string $label) +* @method static Link link(string $url, string $label = null, ?string $hotkey = null) +* @method static Radio radio(string $label, bool $checked = false, ?string $hotkey = null) +* @method static Role app() +* @method static Role file() +* @method static Role edit() +* @method static Role view() +* @method static Role window() +* @method static Role help() +* @method static Role window() +* @method static Role fullscreen() +* @method static Role separator() +* @method static Role devTools() +* @method static Role undo() +* @method static Role redo() +* @method static Role cut() +* @method static Role copy() +* @method static Role paste() +* @method static Role pasteAndMatchStyle() +* @method static Role reload() +* @method static Role minimize() +* @method static Role close() +* @method static Role quit() +* @method static Role help() +* @method static Role hide() +* @method static void create(MenuItem ...$items) +* @method static void default() */ class Menu extends Facade { diff --git a/src/Menu/MenuBuilder.php b/src/Menu/MenuBuilder.php index ddac245..e23020b 100644 --- a/src/Menu/MenuBuilder.php +++ b/src/Menu/MenuBuilder.php @@ -74,22 +74,22 @@ public function app(): Items\Role return new Items\Role(RolesEnum::APP_MENU); } - public function file($label = 'File'): Items\Role + public function file(?string $label = null): Items\Role { return new Items\Role(RolesEnum::FILE_MENU, $label); } - public function edit($label = 'Edit'): Items\Role + public function edit(?string $label = null): Items\Role { return new Items\Role(RolesEnum::EDIT_MENU, $label); } - public function view($label = 'View'): Items\Role + public function view(?string $label = null): Items\Role { return new Items\Role(RolesEnum::VIEW_MENU, $label); } - public function window($label = 'Window'): Items\Role + public function window(?string $label = null): Items\Role { return new Items\Role(RolesEnum::WINDOW_MENU, $label); } @@ -99,73 +99,73 @@ public function separator(): Items\Separator return new Items\Separator; } - public function fullscreen(): Items\Role + public function fullscreen(?string $label = null): Items\Role { - return new Items\Role(RolesEnum::TOGGLE_FULL_SCREEN); + return new Items\Role(RolesEnum::TOGGLE_FULL_SCREEN, $label); } - public function devTools(): Items\Role + public function devTools(?string $label = null): Items\Role { - return new Items\Role(RolesEnum::TOGGLE_DEV_TOOLS); + return new Items\Role(RolesEnum::TOGGLE_DEV_TOOLS, $label); } - public function undo(): Items\Role + public function undo(?string $label = null): Items\Role { - return new Items\Role(RolesEnum::UNDO); + return new Items\Role(RolesEnum::UNDO, $label); } - public function redo(): Items\Role + public function redo(?string $label = null): Items\Role { - return new Items\Role(RolesEnum::REDO); + return new Items\Role(RolesEnum::REDO, $label); } - public function cut(): Items\Role + public function cut(?string $label = null): Items\Role { - return new Items\Role(RolesEnum::CUT); + return new Items\Role(RolesEnum::CUT, $label); } - public function copy(): Items\Role + public function copy(?string $label = null): Items\Role { - return new Items\Role(RolesEnum::COPY); + return new Items\Role(RolesEnum::COPY, $label); } - public function paste(): Items\Role + public function paste(?string $label = null): Items\Role { - return new Items\Role(RolesEnum::PASTE); + return new Items\Role(RolesEnum::PASTE, $label); } - public function pasteAndMatchStyle(): Items\Role + public function pasteAndMatchStyle(?string $label = null): Items\Role { - return new Items\Role(RolesEnum::PASTE_STYLE); + return new Items\Role(RolesEnum::PASTE_STYLE, $label); } - public function reload(): Items\Role + public function reload(?string $label = null): Items\Role { - return new Items\Role(RolesEnum::RELOAD); + return new Items\Role(RolesEnum::RELOAD, $label); } - public function minimize(): Items\Role + public function minimize(?string $label = null): Items\Role { - return new Items\Role(RolesEnum::MINIMIZE); + return new Items\Role(RolesEnum::MINIMIZE, $label); } - public function close(): Items\Role + public function close(?string $label = null): Items\Role { - return new Items\Role(RolesEnum::PASTE_STYLE); + return new Items\Role(RolesEnum::CLOSE, $label); } - public function quit(): Items\Role + public function quit(?string $label = null): Items\Role { - return new Items\Role(RolesEnum::QUIT); + return new Items\Role(RolesEnum::QUIT, $label); } - public function help(): Items\Role + public function help(?string $label = null): Items\Role { - return new Items\Role(RolesEnum::HELP); + return new Items\Role(RolesEnum::HELP, $label); } - public function hide(): Items\Role + public function hide(?string $label = null): Items\Role { - return new Items\Role(RolesEnum::HIDE); + return new Items\Role(RolesEnum::HIDE, $label); } } From a180a16c4250e1ab217e7bf0f19c4dbaf20c8e3e Mon Sep 17 00:00:00 2001 From: simonhamp Date: Wed, 20 Nov 2024 03:09:20 +0000 Subject: [PATCH 16/22] Fix styling --- src/Facades/Menu.php | 62 ++++++++++++++++++++-------------------- src/Menu/MenuBuilder.php | 1 - 2 files changed, 31 insertions(+), 32 deletions(-) diff --git a/src/Facades/Menu.php b/src/Facades/Menu.php index ff9be32..caf08bf 100644 --- a/src/Facades/Menu.php +++ b/src/Facades/Menu.php @@ -12,37 +12,37 @@ use Native\Laravel\Menu\Items\Separator; /** -* @method static \Native\Laravel\Menu\Menu make(MenuItem ...$items) -* @method static Checkbox checkbox(string $label, bool $checked = false, ?string $hotkey = null) -* @method static GoToLink goToUrl(string $url, string $label = null, ?string $hotkey = null) -* @method static GoToLink goToRoute(string $url, string $label = null, ?string $hotkey = null) -* @method static Label label(string $label) -* @method static Link link(string $url, string $label = null, ?string $hotkey = null) -* @method static Radio radio(string $label, bool $checked = false, ?string $hotkey = null) -* @method static Role app() -* @method static Role file() -* @method static Role edit() -* @method static Role view() -* @method static Role window() -* @method static Role help() -* @method static Role window() -* @method static Role fullscreen() -* @method static Role separator() -* @method static Role devTools() -* @method static Role undo() -* @method static Role redo() -* @method static Role cut() -* @method static Role copy() -* @method static Role paste() -* @method static Role pasteAndMatchStyle() -* @method static Role reload() -* @method static Role minimize() -* @method static Role close() -* @method static Role quit() -* @method static Role help() -* @method static Role hide() -* @method static void create(MenuItem ...$items) -* @method static void default() + * @method static \Native\Laravel\Menu\Menu make(MenuItem ...$items) + * @method static Checkbox checkbox(string $label, bool $checked = false, ?string $hotkey = null) + * @method static GoToLink goToUrl(string $url, string $label = null, ?string $hotkey = null) + * @method static GoToLink goToRoute(string $url, string $label = null, ?string $hotkey = null) + * @method static Label label(string $label) + * @method static Link link(string $url, string $label = null, ?string $hotkey = null) + * @method static Radio radio(string $label, bool $checked = false, ?string $hotkey = null) + * @method static Role app() + * @method static Role file() + * @method static Role edit() + * @method static Role view() + * @method static Role window() + * @method static Role help() + * @method static Role window() + * @method static Role fullscreen() + * @method static Role separator() + * @method static Role devTools() + * @method static Role undo() + * @method static Role redo() + * @method static Role cut() + * @method static Role copy() + * @method static Role paste() + * @method static Role pasteAndMatchStyle() + * @method static Role reload() + * @method static Role minimize() + * @method static Role close() + * @method static Role quit() + * @method static Role help() + * @method static Role hide() + * @method static void create(MenuItem ...$items) + * @method static void default() */ class Menu extends Facade { diff --git a/src/Menu/MenuBuilder.php b/src/Menu/MenuBuilder.php index e23020b..14c0769 100644 --- a/src/Menu/MenuBuilder.php +++ b/src/Menu/MenuBuilder.php @@ -5,7 +5,6 @@ use Native\Laravel\Client\Client; use Native\Laravel\Contracts\MenuItem; use Native\Laravel\Enums\RolesEnum; -use Native\Laravel\Menu\Items; class MenuBuilder { From a6a6f30e5f458d8745db53bfaa3a702de801d724 Mon Sep 17 00:00:00 2001 From: Simon Hamp Date: Wed, 20 Nov 2024 09:44:26 +0000 Subject: [PATCH 17/22] Get rid of the yucky GoTo* stuff Fold it all into Link instead --- src/Facades/Menu.php | 4 +--- src/Menu/Items/GoToUrl.php | 21 --------------------- src/Menu/Items/Link.php | 10 ++++++++++ src/Menu/MenuBuilder.php | 15 +++++---------- 4 files changed, 16 insertions(+), 34 deletions(-) delete mode 100644 src/Menu/Items/GoToUrl.php diff --git a/src/Facades/Menu.php b/src/Facades/Menu.php index caf08bf..cd65027 100644 --- a/src/Facades/Menu.php +++ b/src/Facades/Menu.php @@ -4,7 +4,6 @@ use Illuminate\Support\Facades\Facade; use Native\Laravel\Menu\Items\Checkbox; -use Native\Laravel\Menu\Items\GoToUrl; use Native\Laravel\Menu\Items\Label; use Native\Laravel\Menu\Items\Link; use Native\Laravel\Menu\Items\Radio; @@ -14,10 +13,9 @@ /** * @method static \Native\Laravel\Menu\Menu make(MenuItem ...$items) * @method static Checkbox checkbox(string $label, bool $checked = false, ?string $hotkey = null) - * @method static GoToLink goToUrl(string $url, string $label = null, ?string $hotkey = null) - * @method static GoToLink goToRoute(string $url, string $label = null, ?string $hotkey = null) * @method static Label label(string $label) * @method static Link link(string $url, string $label = null, ?string $hotkey = null) + * @method static Link route(string $url, string $label = null, ?string $hotkey = null) * @method static Radio radio(string $label, bool $checked = false, ?string $hotkey = null) * @method static Role app() * @method static Role file() diff --git a/src/Menu/Items/GoToUrl.php b/src/Menu/Items/GoToUrl.php deleted file mode 100644 index 4e232a9..0000000 --- a/src/Menu/Items/GoToUrl.php +++ /dev/null @@ -1,21 +0,0 @@ - 'goto', - 'url' => $this->url, - 'label' => $this->label, - ]); - } -} diff --git a/src/Menu/Items/Link.php b/src/Menu/Items/Link.php index ec5a1cd..3655574 100644 --- a/src/Menu/Items/Link.php +++ b/src/Menu/Items/Link.php @@ -6,12 +6,22 @@ class Link extends MenuItem { protected string $type = 'link'; + protected bool $openInBrowser = false; + public function __construct(protected string $url, protected ?string $label, protected ?string $accelerator = null) {} + public function openInBrowser(bool $openInBrowser = true): self + { + $this->openInBrowser = $openInBrowser; + + return $this; + } + public function toArray(): array { return array_merge(parent::toArray(), [ 'url' => $this->url, + 'openInBrowser' => $this->openInBrowser, ]); } } diff --git a/src/Menu/MenuBuilder.php b/src/Menu/MenuBuilder.php index 14c0769..52ca3c9 100644 --- a/src/Menu/MenuBuilder.php +++ b/src/Menu/MenuBuilder.php @@ -43,16 +43,6 @@ public function label(string $label): Items\Label return new Items\Label($label); } - public function goToUrl(string $url, ?string $label = null, ?string $hotkey = null): Items\GoToUrl - { - return new Items\GoToUrl($url, $label, $hotkey); - } - - public function goToRoute(string $route, ?string $label = null, ?string $hotkey = null): Items\GoToUrl - { - return new Items\GoToUrl(route($route), $label, $hotkey); - } - public function checkbox(string $label, bool $checked = false, ?string $hotkey = null): Items\Checkbox { return new Items\Checkbox($label, $checked, $hotkey); @@ -68,6 +58,11 @@ public function link(string $url, ?string $label = null, ?string $hotkey = null) return new Items\Link($url, $label, $hotkey); } + public function route(string $route, ?string $label = null, ?string $hotkey = null): Items\Link + { + return new Items\Link(route($route), $label, $hotkey); + } + public function app(): Items\Role { return new Items\Role(RolesEnum::APP_MENU); From 382563594f0dd0652038baf887aa4f966f739ccf Mon Sep 17 00:00:00 2001 From: Simon Hamp Date: Wed, 20 Nov 2024 12:33:24 +0000 Subject: [PATCH 18/22] Fix test --- tests/MenuBar/MenuBarTest.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/MenuBar/MenuBarTest.php b/tests/MenuBar/MenuBarTest.php index ec1c022..4b86aa9 100644 --- a/tests/MenuBar/MenuBarTest.php +++ b/tests/MenuBar/MenuBarTest.php @@ -1,7 +1,7 @@ set('nativephp-internal.api_url', 'https://jsonplaceholder.typicode.com/todos/1'); @@ -13,7 +13,10 @@ ->icon('nativephp.png') ->url('https://github.com/milwad-dev') ->withContextMenu( - Menu::new()->label('My Application')->quit(), + Menu::make( + Menu::label('My Application'), + Menu::quit(), + ), ); $menuBarArray = $menuBar->toArray(); From 2f4d01f64f0e5dc4798a078b1a801c5c399c6cf2 Mon Sep 17 00:00:00 2001 From: Simon Hamp Date: Sun, 1 Dec 2024 23:01:06 +0000 Subject: [PATCH 19/22] Add hotkey alias method --- src/Menu/Items/MenuItem.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Menu/Items/MenuItem.php b/src/Menu/Items/MenuItem.php index d9cf74d..7fe9051 100644 --- a/src/Menu/Items/MenuItem.php +++ b/src/Menu/Items/MenuItem.php @@ -88,6 +88,11 @@ public function accelerator(string $accelerator): self return $this; } + public function hotkey(string $hotkey): self + { + return $this->accelerator($hotkey); + } + public function checked($checked = true): self { $this->isChecked = $checked; From 2afe2e4e3b4477bbba93914db070f5b028a9c17e Mon Sep 17 00:00:00 2001 From: Simon Hamp Date: Sun, 1 Dec 2024 23:03:16 +0000 Subject: [PATCH 20/22] Update docblock --- src/Facades/Menu.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Facades/Menu.php b/src/Facades/Menu.php index cd65027..e12c2cb 100644 --- a/src/Facades/Menu.php +++ b/src/Facades/Menu.php @@ -11,7 +11,7 @@ use Native\Laravel\Menu\Items\Separator; /** - * @method static \Native\Laravel\Menu\Menu make(MenuItem ...$items) + * @method static \Native\Laravel\Menu\Menu make(\Native\Laravel\Menu\Items\MenuItem ...$items) * @method static Checkbox checkbox(string $label, bool $checked = false, ?string $hotkey = null) * @method static Label label(string $label) * @method static Link link(string $url, string $label = null, ?string $hotkey = null) From 9fe62d4ff014f46383944fe8767d58a2ef4ace3f Mon Sep 17 00:00:00 2001 From: Simon Hamp Date: Sun, 1 Dec 2024 23:32:02 +0000 Subject: [PATCH 21/22] Make Menu JsonSerializable --- src/Menu/Menu.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Menu/Menu.php b/src/Menu/Menu.php index 7710a7d..329aac9 100644 --- a/src/Menu/Menu.php +++ b/src/Menu/Menu.php @@ -3,10 +3,11 @@ namespace Native\Laravel\Menu; use Illuminate\Support\Traits\Conditionable; +use JsonSerializable; use Native\Laravel\Client\Client; use Native\Laravel\Contracts\MenuItem; -class Menu implements MenuItem +class Menu implements MenuItem, JsonSerializable { use Conditionable; @@ -50,4 +51,9 @@ public function toArray(): array 'submenu' => $items, ]; } + + public function jsonSerialize(): array + { + return $this->toArray(); + } } From d19389ea226923dc094743081c78091427677fa3 Mon Sep 17 00:00:00 2001 From: simonhamp Date: Sun, 1 Dec 2024 23:32:46 +0000 Subject: [PATCH 22/22] Fix styling --- src/Menu/Menu.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Menu/Menu.php b/src/Menu/Menu.php index 329aac9..c97a30d 100644 --- a/src/Menu/Menu.php +++ b/src/Menu/Menu.php @@ -7,7 +7,7 @@ use Native\Laravel\Client\Client; use Native\Laravel\Contracts\MenuItem; -class Menu implements MenuItem, JsonSerializable +class Menu implements JsonSerializable, MenuItem { use Conditionable;