-
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Extract methods to utils * Refactor mapMenu * Change/remove imports/calls to mapMenu * Code style * Allow main app menu to be modified * Fix triggering menu item events * Scrap custom event menu item We can get enough custom event data through on other types, making this redundant * Fold 'goto' items into 'link'
- Loading branch information
Showing
10 changed files
with
138 additions
and
117 deletions.
There are no files selected for viewing
28 changes: 15 additions & 13 deletions
28
resources/js/electron-plugin/src/server/api/contextMenu.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,37 @@ | ||
import express from 'express' | ||
import {app, Menu} from 'electron' | ||
import {mapMenu} from "./helper"; | ||
import express from 'express'; | ||
import { app, Menu } from 'electron'; | ||
import { compileMenu } from "./helper"; | ||
import contextMenu from "electron-context-menu"; | ||
|
||
const router = express.Router(); | ||
|
||
let contextMenuDisposable = null | ||
|
||
router.delete('/', (req, res) => { | ||
res.sendStatus(200) | ||
res.sendStatus(200); | ||
|
||
if (contextMenuDisposable) { | ||
contextMenuDisposable() | ||
contextMenuDisposable = null | ||
contextMenuDisposable(); | ||
contextMenuDisposable = null; | ||
} | ||
}); | ||
|
||
router.post('/', (req, res) => { | ||
res.sendStatus(200) | ||
res.sendStatus(200); | ||
|
||
if (contextMenuDisposable) { | ||
contextMenuDisposable() | ||
contextMenuDisposable = null | ||
contextMenuDisposable(); | ||
contextMenuDisposable = null; | ||
} | ||
|
||
contextMenuDisposable = contextMenu({ | ||
showLookUpSelection: false, | ||
showSearchWithGoogle: false, | ||
showInspectElement: false, | ||
prepend: (defaultActions, parameters, browserWindow) => { | ||
return req.body.entries.map(mapMenu) | ||
} | ||
}) | ||
}) | ||
return req.body.entries.map(compileMenu); | ||
}, | ||
}); | ||
}); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 56 additions & 47 deletions
103
resources/js/electron-plugin/src/server/api/helper/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,82 @@ | ||
import {shell} from "electron"; | ||
import {notifyLaravel} from "../../utils"; | ||
import { shell } from 'electron'; | ||
import { notifyLaravel, goToUrl } from '../../utils'; | ||
import state from '../../state'; | ||
|
||
function triggerMenuItemEvent(menuItem) { | ||
function triggerMenuItemEvent(menuItem, combo) { | ||
notifyLaravel('events', { | ||
event: '\\Native\\Laravel\\Events\\Menu\\MenuItemClicked', | ||
payload: [ | ||
{ | ||
event: menuItem.event || '\\Native\\Laravel\\Events\\Menu\\MenuItemClicked', | ||
payload: { | ||
item: { | ||
id: menuItem.id, | ||
label: menuItem.label, | ||
checked: menuItem.checked | ||
} | ||
] | ||
}) | ||
checked: menuItem.checked, | ||
}, | ||
combo, | ||
}, | ||
}); | ||
} | ||
|
||
const mapMenu = (menu) => { | ||
if (menu.submenu) { | ||
menu.submenu = menu.submenu.map(mapMenu) | ||
} | ||
|
||
if (menu.type === 'link') { | ||
menu.type = 'normal' | ||
menu.click = () => { | ||
triggerMenuItemEvent(menu) | ||
shell.openExternal(menu.url) | ||
export function compileMenu (item) { | ||
if (item.submenu) { | ||
if (Array.isArray(item.submenu)) { | ||
item.submenu = item.submenu?.map(compileMenu); | ||
} else { | ||
item.submenu = item.submenu.submenu?.map(compileMenu); | ||
} | ||
return menu | ||
} | ||
|
||
if (menu.type === 'checkbox') { | ||
menu.click = () => { | ||
menu.checked = !menu.checked | ||
triggerMenuItemEvent(menu) | ||
} | ||
} | ||
if (item.type === 'link') { | ||
item.type = 'normal'; | ||
|
||
if (menu.type === 'event') { | ||
return { | ||
label: menu.label, | ||
accelerator: menu.accelerator, | ||
click() { | ||
notifyLaravel('events', { | ||
event: menu.event | ||
}) | ||
item.click = (menuItem, focusedWindow, combo) => { | ||
triggerMenuItemEvent(item, combo); | ||
|
||
if (item.openInBrowser) { | ||
shell.openExternal(item.url); | ||
return; | ||
} | ||
|
||
if (! focusedWindow) { | ||
// TODO: Bring a window to the front? | ||
return; | ||
} | ||
|
||
const id = Object.keys(state.windows) | ||
.find(key => state.windows[key] === focusedWindow); | ||
|
||
goToUrl(item.url, id); | ||
} | ||
|
||
return item; | ||
} | ||
|
||
if (item.type === 'checkbox' || item.type === 'radio') { | ||
item.click = (menuItem, focusedWindow, combo) => { | ||
item.checked = !item.checked; | ||
triggerMenuItemEvent(item, combo); | ||
}; | ||
|
||
return item; | ||
} | ||
|
||
if (menu.type === 'role') { | ||
if (item.type === 'role') { | ||
let menuItem = { | ||
role: menu.role | ||
role: item.role | ||
}; | ||
|
||
if (menu.label) { | ||
menuItem['label'] = menu.label; | ||
if (item.label) { | ||
menuItem['label'] = item.label; | ||
} | ||
|
||
return menuItem; | ||
} | ||
|
||
if (! menu.click) { | ||
menu.click = () => { | ||
triggerMenuItemEvent(menu) | ||
// Default click event | ||
if (! item.click) { | ||
item.click = (menuItem, focusedWindow, combo) => { | ||
triggerMenuItemEvent(item, combo); | ||
} | ||
} | ||
|
||
return menu | ||
} | ||
|
||
export { | ||
mapMenu, | ||
return item; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,19 @@ | ||
import express from 'express' | ||
import {Menu} from 'electron' | ||
import {mapMenu} from "./helper"; | ||
import express from 'express'; | ||
import { Menu } from 'electron'; | ||
import { compileMenu } from './helper'; | ||
|
||
const router = express.Router(); | ||
|
||
router.post('/', (req, res) => { | ||
const menuEntries = req.body.items.map(mapMenu) | ||
Menu.setApplicationMenu(null); | ||
|
||
const menuEntries = req.body.items.map(compileMenu); | ||
|
||
const menu = Menu.buildFromTemplate(menuEntries); | ||
|
||
Menu.setApplicationMenu(menu); | ||
|
||
const menu = Menu.buildFromTemplate(menuEntries) | ||
Menu.setApplicationMenu(menu) | ||
res.sendStatus(200) | ||
}) | ||
res.sendStatus(200); | ||
}); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.