8000 Menu improvements by simonhamp · Pull Request #423 · NativePHP/laravel · GitHub
[go: up one dir, main page]

Skip to content

Menu improvements #423

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

Merged
merged 22 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Extract MenuBuilder
  • Loading branch information
simonhamp committed Nov 20, 2024
commit a3a8936fca6a5f40648180f99249fa278f1b296e
16 changes: 16 additions & 0 deletions src/Facades/Menu.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Native\Laravel\Facades;

use Illuminate\Support\Facades\Facade;

/**
* @method static void create()
*/
class Menu extends Facade
{
protected static function getFacadeAccessor()
{
return \Native\Laravel\Menu\MenuBuilder::class;
}
}
88 changes: 7 additions & 81 deletions src/Menu/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,10 @@ class Menu implements MenuItem

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 +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
Expand All @@ -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,
];
}
Expand Down
161 changes: 161 additions & 0 deletions src/Menu/MenuBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<?php

namespace Native\Laravel\Menu;

use Native\Laravel\Client\Client;
use Native\Laravel\Contracts\MenuItem;
use Native\Laravel\Enums\RolesEnum;
use Native\Laravel\Menu\Items;

class MenuBuilder
{
public function __construct(protected Client $client) {}

public function make(MenuItem ...$items): Menu
{
$menu = new Menu($this->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);
}
}
0