Skip to content
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = []) {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add some PHP Doc here to represent:

array:2 [▼
  "item" => array:2 [▼
    "label" => "Settings"
    "checked" => false
  ]
  "combo" => array:5 [▼
    "shiftKey" => false
    "ctrlKey" => false
    "altKey" => false
    "metaKey" => false
    "triggeredByAccelerator" => false
  ]
]

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comes straight from Electron and I don't want the docblock structure to be wrong if/when they change things, as that would be more confusing imo. Perhaps at some point we create our own abstraction around this? 🤷🏻‍♂️


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(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,
]);
}
}
45 changes: 42 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 = $enabled;
$this->isEnabled = false;

return $this;
}

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

return $this;
}
Expand Down Expand Up @@ -73,25 +95,42 @@ public function checked($checked = true): self
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;
}
}
95 changes: 7 additions & 88 deletions src/Menu/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,17 @@
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
{
use Conditionable;

protected array $items = [];

protected string $prepend = '';
protected string $label = '';

public function __construct(protected Client $client) {}

public static function new(): static
{
return new static(new Client);
}

public function register(): void
{
$items = $this->toArray()['submenu'];
Expand All @@ -37,81 +25,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
Expand All @@ -123,11 +41,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,
];
}
Expand Down
Loading
Loading