Skip to content

Commit

Permalink
Menu improvements (#423)
Browse files Browse the repository at this point in the history
* 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 <simonhamp@users.noreply.github.com>
  • Loading branch information
simonhamp and simonhamp authored Dec 1, 2024
1 parent a30505f commit 53c7fa2
Show file tree
Hide file tree
Showing 11 changed files with 316 additions and 117 deletions.
14 changes: 13 additions & 1 deletion src/Enums/RolesEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,24 @@

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';
case COPY = 'copy';
case PASTE = 'paste';
case PASTE_STYLE = 'pasteAndMatchStyle';
case RELOAD = 'reload';
case HIDE = 'hide'; // macOS
case MINIMIZE = 'minimize';
case CLOSE = 'close';
case QUIT = 'quit';
case TOGGLE_FULL_SCREEN = 'togglefullscreen';
case TOGGLE_DEV_TOOLS = 'toggleDevTools';
case ABOUT = 'about';
}
2 changes: 1 addition & 1 deletion src/Events/Menu/MenuItemClicked.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
51 changes: 51 additions & 0 deletions src/Facades/Menu.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Native\Laravel\Facades;

use Illuminate\Support\Facades\Facade;
use Native\Laravel\Menu\Items\Checkbox;
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 \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)
* @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()
* @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
{
protected static function getFacadeAccessor()
{
return \Native\Laravel\Menu\MenuBuilder::class;
}
}
7 changes: 5 additions & 2 deletions src/Menu/Items/Checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
17 changes: 0 additions & 17 deletions src/Menu/Items/Event.php

This file was deleted.

10 changes: 10 additions & 0 deletions src/Menu/Items/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);
}
}
50 changes: 47 additions & 3 deletions src/Menu/Items/MenuItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
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
{
protected string $type = 'normal';

protected ?string $id = null;

protected ?string $label = null;

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

protected ?string $toolTip = null;

protected ?Menu $submenu = null;

protected bool $isEnabled = true;

protected bool $isVisible = true;

protected bool $isChecked = false;

public function enabled($enabled = true): self
protected ?string $event = null;

public function enabled(): self
{
$this->isEnabled = true;

return $this;
}

public function disabled(): self
{
$this->isEnabled = false;

return $this;
}

public function id(string $id): self
{
$this->isEnabled = $enabled;
$this->id = $id;

return $this;
}
Expand Down Expand Up @@ -66,32 +88,54 @@ 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;

return $this;
}

public function toolTip(string $toolTip): self
public function tooltip(string $toolTip): self
{
$this->toolTip = $toolTip;

return $this;
}

public function submenu(MenuItemContract ...$items): self
{
$this->submenu = MenuFacade::make(...$items);

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,
'visible' => $this->isVisible,
'checked' => $this->isChecked,
'accelerator' => $this->accelerator,
'icon' => $this->icon,
'submenu' => $this->submenu?->toArray(),
], fn ($value) => $value !== null);
}
}
7 changes: 5 additions & 2 deletions src/Menu/Items/Radio.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ 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;
}
}
Loading

0 comments on commit 53c7fa2

Please sign in to comment.