Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Menu improvements #423

Merged
merged 22 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Contributor

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(\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)
simonhamp marked this conversation as resolved.
Show resolved Hide resolved
* @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
simonhamp marked this conversation as resolved.
Show resolved Hide resolved
{
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