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

cocoa: add standard app menu items #1262

Merged

Conversation

layday
Copy link
Contributor

@layday layday commented May 21, 2021

Added "Hide ", "Hide Others" and "Show All" in a separate
section immediately preceding "Quit". Also added Cmd+, as
a shortcut for "Preferences" and sectioned it off.

Closes #1261.

Signed-off-by: layday [email protected]

PR Checklist:

  • All new features have been tested
  • All new features have been documented
  • I have read the CONTRIBUTING.md file
  • I will abide by the code of conduct

Added "Hide <app>", "Hide Others" and "Show All" in a separate
section immediately preceding "Quit".  Also added Cmd+, as
a shortcut for "Preferences" and sectioned it off.

Closes beeware#1261.

Signed-off-by: layday <[email protected]>
@layday
Copy link
Contributor Author

layday commented May 21, 2021

I don't know if this is the correct approach to using rubicon - happy to shift things around.

Copy link
Member

@freakboy3742 freakboy3742 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome - thanks for the patch!

I'll merge this as-is, as it's a significant improvement. However, if you're looking for a small place to tinker and tweak, I noticed that the "Show All" option is always active. On other apps, it's disabled unless there are hidden apps. I'm not sure what mechanism is driving that - I'm guessing it has something to do with NSWorkSpaceDidHideApplicationNotification - but it seems weird that every app would need to implement logic to determine if there are currently hidden apps.

@freakboy3742 freakboy3742 merged commit 0447a36 into beeware:master May 23, 2021
@samschott
Copy link
Member

I suspect that the automatic enabling and disabling is typically driven by the responder implementing validateUserInterfaceItem:, as described in Apple's docs for automatically enabling or disabling menu items.

In this case, the responder should be NSApplication and the selector should be unhideAllApplications: but toga currently wires all menu items up to the selector selectMenuItem:. To get automatic enabling enabling of menu items (useful also for copy / paste commands), we would probably need to set NSMenu.autoenablesItems to True and set the appropriate selectors at least for such "native" commands.

@layday
Copy link
Contributor Author

layday commented May 23, 2021

Thanks for merging.

I tried hooking it up to unhideAllApplications directly using a selector, thought that might trigger validateMenuItem but no luck.

@samschott
Copy link
Member

samschott commented May 23, 2021

Strange, I've just tried this out myself and it works both with hideOtherApplications and unhideAllApplications. Remember to set submenu.setAutoenablesItems(True) for that submenu.

Edit: I'm not sure if this actually calls validateMenuItem or validateUserInterfaceItem but the menu items certainly get enabled / disabled automatically as appropriate.

@layday
Copy link
Contributor Author

layday commented May 23, 2021

That does work, I was doing it on the menu bar 🤦

When I tried it on the app delegate it was validateMenuItem: being called. I see that sub-menus currently have autoenablesItems set to false, I assume that's for Command.enabled to be picked up? If validateMenuItem: sets the enabled flag on NSMenuItem's then we could implement that and bypass the Command proxy? For instance:

class AppDelegate(NSObject):
    @objc_method
    def validateMenuItem_(self, sender) -> bool:
        cmd = self.interface._impl._menu_items[sender]
        return cmd.enabled

@samschott
Copy link
Member

I see that sub-menus currently have autoenablesItems set to false, I assume that's for Command.enabled to be picked up?

Yes, exactly.

If validateMenuItem: sets the enabled flag on NSMenuItem's then we could implement that and bypass the Command proxy?

I like that approach!

@layday
Copy link
Contributor Author

layday commented May 23, 2021

#1267

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make app respond to Cmd-H, Cmd-Opt-H on macOS; add menu bar items
3 participants