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

Skip to content

Menu improvements #139

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 9 commits into from
Dec 1, 2024
Merged
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
Prev Previous commit
Next Next commit
Fix triggering menu item events
  • Loading branch information
simonhamp committed Nov 20, 2024
commit f08b0de3eaa8c244dc6b5674cdb4641be254449e
75 changes: 49 additions & 26 deletions resources/js/electron-plugin/src/server/api/helper/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ 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,
},
});
}

Expand All @@ -25,31 +26,53 @@ export function compileMenu (item) {
}

if (item.type === 'link') {
return {
click() {
triggerMenuItemEvent(item);
shell.openExternal(item.url);
}
};
item.type = 'normal';

item.click = (menuItem, focusedWindow, combo) => {
triggerMenuItemEvent(item, combo);
shell.openExternal(item.url);
}

return item;
}

if (item.type === 'checkbox') {
item.click = () => {
if (item.type === 'checkbox' || item.type === 'radio') {
item.click = (menuItem, focusedWindow, combo) => {
item.checked = !item.checked;
triggerMenuItemEvent(item);
triggerMenuItemEvent(item, combo);
};

return item;
}

if (item.type === 'event') {
return {
label: item.label,
accelerator: item.accelerator,
click() {
notifyLaravel('events', {
event: item.event
});
},
item.type = 'normal';

item.click = (menuItem, focusedWindow, combo) => {
triggerMenuItemEvent(item, combo);
};

return item;
}

if (item.type === 'goto') {
item.type = 'normal';

item.click = (menuItem, focusedWindow, combo) => {
triggerMenuItemEvent(item, combo);

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 === 'role') {
Expand All @@ -66,8 +89,8 @@ export function compileMenu (item) {

// Default click event
if (! item.click) {
item.click = () => {
triggerMenuItemEvent(item);
item.click = (menuItem, focusedWindow, combo) => {
triggerMenuItemEvent(item, combo);
}
}

Expand Down
Loading
0