Skip to content

Commit b7fd944

Browse files
Jianbe-03Jibbe Winandsgwleuverink
authored
Add method to programmatically open context menu (#57)
* feat: add showContextMenu method * Build new electron --------- Co-authored-by: Jibbe Winands <j.winands@creacoon.nl> Co-authored-by: Willem Leuverink <willem@leuver.ink>
1 parent 8e0a61f commit b7fd944

File tree

5 files changed

+30
-0
lines changed

5 files changed

+30
-0
lines changed

resources/electron/electron-plugin/dist/server/api/menuBar.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ router.post("/context-menu", (req, res) => {
3131
const { contextMenu } = req.body;
3232
(_a = state.tray) === null || _a === void 0 ? void 0 : _a.setContextMenu(buildMenu(contextMenu));
3333
});
34+
router.post("/show-context-menu", (req, res) => {
35+
var _a;
36+
res.sendStatus(200);
37+
(_a = state.tray) === null || _a === void 0 ? void 0 : _a.popUpContextMenu();
38+
});
3439
router.post("/show", (req, res) => {
3540
res.sendStatus(200);
3641
state.activeMenuBar.showWindow();

resources/electron/electron-plugin/src/server/api/menuBar.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ router.post("/context-menu", (req, res) => {
4242
state.tray?.setContextMenu(buildMenu(contextMenu));
4343
});
4444

45+
router.post("/show-context-menu", (req, res) => {
46+
res.sendStatus(200);
47+
48+
state.tray?.popUpContextMenu();
49+
});
50+
4551
router.post("/show", (req, res) => {
4652
res.sendStatus(200);
4753

src/Facades/MenuBar.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* @method static void icon(string $icon)
1515
* @method static void resize(int $width, int $height)
1616
* @method static void contextMenu(Menu $contextMenu)
17+
* @method static void showContextMenu()
1718
* @method static void webPreferences(array $preferences)
1819
*/
1920
class MenuBar extends Facade

src/MenuBar/MenuBarManager.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,9 @@ public function contextMenu(Menu $contextMenu)
5959
'contextMenu' => $contextMenu->toArray()['submenu'],
6060
]);
6161
}
62+
63+
public function showContextMenu()
64+
{
65+
$this->client->post('menu-bar/show-context-menu');
66+
}
6267
}

tests/MenuBar/MenuBarTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use Illuminate\Support\Facades\Http;
34
use Native\Desktop\Facades\Menu;
45
use Native\Desktop\Facades\MenuBar;
56

@@ -30,3 +31,15 @@
3031
$this->assertEquals('trayCenter', $menuBarArray['windowPosition']);
3132
$this->assertIsArray($menuBarArray['contextMenu']);
3233
});
34+
35+
it('can show context menu programmatically', function () {
36+
Http::fake([
37+
'*/menu-bar/show-context-menu' => Http::response([], 200),
38+
]);
39+
40+
MenuBar::showContextMenu();
41+
42+
Http::assertSent(function ($request) {
43+
return str_contains($request->url(), 'menu-bar/show-context-menu');
44+
});
45+
});

0 commit comments

Comments
 (0)