Skip to content

Commit 53c7fa2

Browse files
authored
Menu improvements (#423)
* Extract MenuBuilder * Add more Electron MenuItem roles * Allow MenuItems to have submenus * enabled/disabled * Add GoToUrl convenience item * Fix styling * Add help and hide roles * Receive combo key data * Add id's to menu items * Support radio items * Style * Remove custom event menu item type * Support custom event firing on all menu items * Fix label * Type hints and consistency * Fix styling * Get rid of the yucky GoTo* stuff Fold it all into Link instead * Fix test * Add hotkey alias method * Update docblock * Make Menu JsonSerializable * Fix styling --------- Co-authored-by: simonhamp <[email protected]>
1 parent a30505f commit 53c7fa2

File tree

11 files changed

+316
-117
lines changed

11 files changed

+316
-117
lines changed

src/Enums/RolesEnum.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,24 @@
44

55
enum RolesEnum: string
66
{
7-
case APP_MENU = 'appMenu';
7+
case APP_MENU = 'appMenu'; // macOS
88
case FILE_MENU = 'fileMenu';
99
case EDIT_MENU = 'editMenu';
1010
case VIEW_MENU = 'viewMenu';
1111
case WINDOW_MENU = 'windowMenu';
12+
case HELP = 'help'; // macOS
13+
case UNDO = 'undo';
14+
case REDO = 'redo';
15+
case CUT = 'cut';
16+
case COPY = 'copy';
17+
case PASTE = 'paste';
18+
case PASTE_STYLE = 'pasteAndMatchStyle';
19+
case RELOAD = 'reload';
20+
case HIDE = 'hide'; // macOS
21+
case MINIMIZE = 'minimize';
22+
case CLOSE = 'close';
1223
case QUIT = 'quit';
1324
case TOGGLE_FULL_SCREEN = 'togglefullscreen';
1425
case TOGGLE_DEV_TOOLS = 'toggleDevTools';
26+
case ABOUT = 'about';
1527
}

src/Events/Menu/MenuItemClicked.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class MenuItemClicked implements ShouldBroadcastNow
1212
{
1313
use Dispatchable, InteractsWithSockets, SerializesModels;
1414

15-
public function __construct(public array $item) {}
15+
public function __construct(public array $item, public array $combo = []) {}
1616

1717
public function broadcastOn()
1818
{

src/Facades/Menu.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Native\Laravel\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
use Native\Laravel\Menu\Items\Checkbox;
7+
use Native\Laravel\Menu\Items\Label;
8+
use Native\Laravel\Menu\Items\Link;
9+
use Native\Laravel\Menu\Items\Radio;
10+
use Native\Laravel\Menu\Items\Role;
11+
use Native\Laravel\Menu\Items\Separator;
12+
13+
/**
14+
* @method static \Native\Laravel\Menu\Menu make(\Native\Laravel\Menu\Items\MenuItem ...$items)
15+
* @method static Checkbox checkbox(string $label, bool $checked = false, ?string $hotkey = null)
16+
* @method static Label label(string $label)
17+
* @method static Link link(string $url, string $label = null, ?string $hotkey = null)
18+
* @method static Link route(string $url, string $label = null, ?string $hotkey = null)
19+
* @method static Radio radio(string $label, bool $checked = false, ?string $hotkey = null)
20+
* @method static Role app()
21+
* @method static Role file()
22+
* @method static Role edit()
23+
* @method static Role view()
24+
* @method static Role window()
25+
* @method static Role help()
26+
* @method static Role window()
27+
* @method static Role fullscreen()
28+
* @method static Role separator()
29+
* @method static Role devTools()
30+
* @method static Role undo()
31+
* @method static Role redo()
32+
* @method static Role cut()
33+
* @method static Role copy()
34+
* @method static Role paste()
35+
* @method static Role pasteAndMatchStyle()
36+
* @method static Role reload()
37+
* @method static Role minimize()
38+
* @method static Role close()
39+
* @method static Role quit()
40+
* @method static Role help()
41+
* @method static Role hide()
42+
* @method static void create(MenuItem ...$items)
43+
* @method static void default()
44+
*/
45+
class Menu extends Facade
46+
{
47+
protected static function getFacadeAccessor()
48+
{
49+
return \Native\Laravel\Menu\MenuBuilder::class;
50+
}
51+
}

src/Menu/Items/Checkbox.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ class Checkbox extends MenuItem
66
{
77
protected string $type = 'checkbox';
88

9-
public function __construct(string $label, protected bool $isChecked = false, protected ?string $accelerator = null)
10-
{
9+
public function __construct(
10+
string $label,
11+
protected bool $isChecked = false,
12+
protected ?string $accelerator = null
13+
) {
1114
$this->label = $label;
1215
}
1316
}

src/Menu/Items/Event.php

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

src/Menu/Items/Link.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,22 @@ class Link extends MenuItem
66
{
77
protected string $type = 'link';
88

9+
protected bool $openInBrowser = false;
10+
911
public function __construct(protected string $url, protected ?string $label, protected ?string $accelerator = null) {}
1012

13+
public function openInBrowser(bool $openInBrowser = true): self
14+
{
15+
$this->openInBrowser = $openInBrowser;
16+
17+
return $this;
18+
}
19+
1120
public function toArray(): array
1221
{
1322
return array_merge(parent::toArray(), [
1423
'url' => $this->url,
24+
'openInBrowser' => $this->openInBrowser,
1525
]);
1626
}
1727
}

src/Menu/Items/MenuItem.php

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
namespace Native\Laravel\Menu\Items;
44

55
use Native\Laravel\Contracts\MenuItem as MenuItemContract;
6+
use Native\Laravel\Facades\Menu as MenuFacade;
7+
use Native\Laravel\Menu\Menu;
68

79
abstract class MenuItem implements MenuItemContract
810
{
911
protected string $type = 'normal';
1012

13+
protected ?string $id = null;
14+
1115
protected ?string $label = null;
1216

1317
protected ?string $sublabel = null;
@@ -18,15 +22,33 @@ abstract class MenuItem implements MenuItemContract
1822

1923
protected ?string $toolTip = null;
2024

25+
protected ?Menu $submenu = null;
26+
2127
protected bool $isEnabled = true;
2228

2329
protected bool $isVisible = true;
2430

2531
protected bool $isChecked = false;
2632

27-
public function enabled($enabled = true): self
33+
protected ?string $event = null;
34+
35+
public function enabled(): self
36+
{
37+
$this->isEnabled = true;
38+
39+
return $this;
40+
}
41+
42+
public function disabled(): self
43+
{
44+
$this->isEnabled = false;
45+
46+
return $this;
47+
}
48+
49+
public function id(string $id): self
2850
{
29-
$this->isEnabled = $enabled;
51+
$this->id = $id;
3052

3153
return $this;
3254
}
@@ -66,32 +88,54 @@ public function accelerator(string $accelerator): self
6688
return $this;
6789
}
6890

91+
public function hotkey(string $hotkey): self
92+
{
93+
return $this->accelerator($hotkey);
94+
}
95+
6996
public function checked($checked = true): self
7097
{
7198
$this->isChecked = $checked;
7299

73100
return $this;
74101
}
75102

76-
public function toolTip(string $toolTip): self
103+
public function tooltip(string $toolTip): self
77104
{
78105
$this->toolTip = $toolTip;
79106

80107
return $this;
81108
}
82109

110+
public function submenu(MenuItemContract ...$items): self
111+
{
112+
$this->submenu = MenuFacade::make(...$items);
113+
114+
return $this;
115+
}
116+
117+
public function event(string $event): self
118+
{
119+
$this->event = $event;
120+
121+
return $this;
122+
}
123+
83124
public function toArray(): array
84125
{
85126
return array_filter([
86127
'type' => $this->type,
128+
'id' => $this->id,
87129
'label' => $this->label,
130+
'event' => $this->event,
88131
'sublabel' => $this->sublabel,
89132
'toolTip' => $this->toolTip,
90133
'enabled' => $this->isEnabled,
91134
'visible' => $this->isVisible,
92135
'checked' => $this->isChecked,
93136
'accelerator' => $this->accelerator,
94137
'icon' => $this->icon,
138+
'submenu' => $this->submenu?->toArray(),
95139
], fn ($value) => $value !== null);
96140
}
97141
}

src/Menu/Items/Radio.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ class Radio extends MenuItem
66
{
77
protected string $type = 'radio';
88

9-
public function __construct(string $label)
10-
{
9+
public function __construct(
10+
string $label,
11+
protected bool $isChecked = false,
12+
protected ?string $accelerator = null
13+
) {
1114
$this->label = $label;
1215
}
1316
}

0 commit comments

Comments
 (0)