Skip to content

steventroughtonsmith/CatalystMenuBarStatusWindow

main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 

CatalystMenuBarStatusWindow

This sample code showcases a method of presenting UIKit (or Catalyst-specific SwiftUI) content from the status menu area on macOS.

WARNING: In lieu of a public API to do this, we have to rely on swizzling. I've tried to make this as 'safe' as it can possibly be, but as with all swizzling and non-public API, please be careful of side-effects and potential breakage in future versions of the OS. This is to be considered an advanced topic for people who know what they're doing.

Realistically, all you're really doing is manually setting the size and position of an NSPopover when it's created, so the potential failure states should be fairly innocuous.

Overview

  • Uses an AppKit bridging bundle
  • Adds a status menu item
  • Clicking the status menu item fires a Notification
  • In the UIKit side of the app, the Notification receiver presents a view controller (in this case, with a SwiftUI content view) as a popover with a nil source view
  • Swizzles showRelativeToRect:ofView:preferredEdge: in AppKit to redirect the popover source view to the status menu item
  • As the popover is now 'attached' to the status menu item, it will move with it when rearranged
  • Process has been tested to work on both macOS 11 and macOS 12

Screenshots

https://hccdata.s3.amazonaws.com/gh_statusbar_uikit.jpg

About

This sample code showcases a method of presenting UIKit (or Catalyst-specific SwiftUI) content from the status menu area on macOS.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published