Thursday, July 1, 2021


Tags

Ohanaware AppKit Icon

Let the macOS manage your Window menu

How to activate App Kit's Window Menu from inside a Xojo made Mac app

The Window Menu under macOS Big Sur provides the options to make a window fill half the screen or when multiple displays are connected, the option to move the window between the screens.

There is actually very little code involved in activating this feature and once configured, the macOS will even automatically add and remove windows to the menu as needed.

For this code to work, find your "Window" menu in the Xojo IDE. Make sure that it is named "WindowMenu", then switch to the Open event of your application.

Ohanaware App Kit customers

Append the following code to the open event of the App object, after you’ve called OAK.init.

#if targetMacOS then windowMenu.makeWindowMenu( true ) #endif

This function will also create the Minimize and Zoom / Center menuitems for you, which are then auto managed (by OAK) so you don’t need to add code to each window to respond to these items.

Without Ohanaware's App Kit

If you don’t have the Ohanaware App Kit, you can still get the macOS to manage the Window menu for you. You’ll have to handle the Minimize and Zoom / Center menuitems yourself. Add the following block of code to the Open event of your application.

#if targetMacOS then // --- This code snippet was made from the Ohanaware App Kit 2021 - ohanaware.com // --- Basically, we grab the NSMenu instance of the windowMenu item, tell Apple's App Kit to use that for the Window menu. declare Function NSClassFromString lib

"Foundation"

( className as CFStringRef ) as integer declare Function NSMenuitem_Submenu lib

"AppKit"

selector

"submenu"

( NSMenuitemInstance as integer ) as integer declare Sub NSApplication_setWindowsMenu lib

"AppKit"

selector

"setWindowsMenu:"

( NSApplicationInstance as integer, inNSMenuitem as integer ) declare function NSApplication_sharedApplication lib

"AppKit"

selector

"sharedApplication"

( NSApplicationClass as integer ) as integer Dim subMenuRef as integer = NSMenuitem_Submenu( windowMenu.handle( menuitem.handleType.CocoaNSMenuItem ) ) NSApplication_setWindowsMenu( NSApplication_sharedApplication( NSClassFromString(

"NSApplication"

) ), subMenuRef ) #endif

The Ohanaware App Kit 2021 includes more code like this that will help you to build better Mac apps using the Xojo development tool. Get the new 2021 version as part of the Omegabundle 2021.

To see what's new for 2021, check out https://ohanaware.com/appkit/ or https://www.omegabundle.net/whats-included-in-this-omegabundle