Skip to content

Replace MainMenu.xib with a pure Swift + SwiftUI app #46

Description

@hisaac

Replace MainMenu.xib with a pure Swift + SwiftUI app, dropping the AppKit template scaffolding the
project has carried since it was created.

It does work

Worth stating up front, because it is easy to assume the xib is load-bearing: a SwiftUI App with no
xib at all synthesises the complete menu bar. A bundle containing only an Info.plist and a
binary produces Apple, App, Edit, View, Window, Help with no nib involved. The xib is template
residue, not a requirement.

What has to move

  • AppDelegate currently builds an NSWindow + NSWindowController + NSHostingView by hand. A
    Settings scene replaces all of it and gets ⌘, and the menu item for free.
  • INFOPLIST_KEY_NSMainNibFile=MainMenu in Config/CenterMouse.xcconfig must go, along with the xib
    itself.
  • Menu customisation moves to .commands { }.

Watch out for

The xib encodes an App Review accommodation. From AppDelegate:

// Excluding this from the Window menu so that it can be manually added.
// This is being done to comply with an App Review requirement.
window.isExcludedFromWindowsMenu = true

A reviewer required this once. Re-derive it deliberately in the SwiftUI version rather than
discovering during review that it was lost — this is the single riskiest part of the change.

Some things have no SwiftUI equivalent and still need an @NSApplicationDelegateAdaptor:

  • The launchedAsLogInItem check, which reads the launch Apple Event via NSAppleEventManager.
  • NSApp.setActivationPolicy for the Dock icon toggle.

The About panel needs no special handling as long as the credits stay a bundled resource — the
standard menu action picks them up on its own. Only reach for CommandGroup(replacing: .appInfo) if
you want behaviour beyond that.

Notes

MenuBarExtra is available (macOS 13+) if a status item is ever wanted, but note that menu bar items
were deliberately removed in 6480ee9 — this issue is not a reason to bring them back.

Verify

  • Full menu bar present, all standard items functional.
  • ⌘, opens settings; the app menu's Settings item works.
  • The settings window is still excluded from the Window menu as above.
  • Launching as a login item still shows no window and does not steal focus.
  • Hiding the Dock icon still works, and the window can still be reopened afterwards.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions