{"id":17506222,"url":"https://github.com/orchetect/menubarextraaccess","last_synced_at":"2025-04-05T15:05:52.558Z","repository":{"id":90389337,"uuid":"607148286","full_name":"orchetect/MenuBarExtraAccess","owner":"orchetect","description":"Show/hide SwiftUI MenuBarExtra menu using Bindings","archived":false,"fork":false,"pushed_at":"2025-01-27T22:34:13.000Z","size":129,"stargazers_count":145,"open_issues_count":0,"forks_count":8,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-03-29T14:09:16.887Z","etag":null,"topics":["macos","macos-ventura","menubar","menubarextra","nsstatusitem","statusitem","swift","swiftui"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/orchetect.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"orchetect"}},"created_at":"2023-02-27T12:17:01.000Z","updated_at":"2025-03-28T11:56:28.000Z","dependencies_parsed_at":null,"dependency_job_id":"1378cdae-ba67-4e3a-aa10-870cfc5b9f4b","html_url":"https://github.com/orchetect/MenuBarExtraAccess","commit_stats":{"total_commits":21,"total_committers":1,"mean_commits":21.0,"dds":0.0,"last_synced_commit":"5af5a7d6768862b47d64a51f7343b0a6c81aba86"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/orchetect%2FMenuBarExtraAccess","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/orchetect%2FMenuBarExtraAccess/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/orchetect%2FMenuBarExtraAccess/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/orchetect%2FMenuBarExtraAccess/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/orchetect","download_url":"https://codeload.github.com/orchetect/MenuBarExtraAccess/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247353730,"owners_count":20925329,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["macos","macos-ventura","menubar","menubarextra","nsstatusitem","statusitem","swift","swiftui"],"created_at":"2024-10-20T03:34:07.635Z","updated_at":"2025-04-05T15:05:52.538Z","avatar_url":"https://github.com/orchetect.png","language":"Swift","funding_links":["https://github.com/sponsors/orchetect"],"categories":[],"sub_categories":[],"readme":"# MenuBarExtraAccess\n\n[![Platforms - macOS 13.0](https://img.shields.io/badge/platforms-macOS%2013.0-blue.svg?style=flat)](https://developer.apple.com/swift) [![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Forchetect%2FMenuBarExtraAccess%2Fbadge%3Ftype%3Dswift-versions)](https://swiftpackageindex.com/orchetect/MenuBarExtraAccess) [![Xcode 14](https://img.shields.io/badge/Xcode-14-blue.svg?style=flat)](https://developer.apple.com/swift) [![License: MIT](http://img.shields.io/badge/license-MIT-lightgrey.svg?style=flat)](https://github.com/orchetect/MenuBarExtraAccess/blob/main/LICENSE)\n\n#### **Gives you *Extra* access to SwiftUI `MenuBarExtra`.**\n\n- Programmatically hide, show, or toggle the menu (by way of a Bool binding)\n- Access to the underlying `NSStatusItem`\n- Access to the underlying `NSWindow` (when using the `.window` style)\n- Works with one or [multiple](#Multiple-MenuBarExtra) `MenuBarExtra`\n- Works with both [`menu`](#Standard-Menu-Style) and [`window`](#Window-Style) based styles (see [Known Issues](#Known-Issues))\n\n#### Why?\n\nThere is no 1st-party MenuBarExtra API to get or set the menu presentation state, access the status item, or access the popup's NSWindow. (Still as of Xcode 16.1)\n\n#### Library Features\n\n- A new `.menuBarExtraAccess(isPresented:) { statusItem in }` scene modifier with\n  - a binding to hide/show/toggle the menu, and\n  - direct access to the `NSStatusItem` if needed\n- A new `.introspectMenuBarExtraWindow { window in }` view modifier passing in the `NSWindow` reference\n- Window-based menu extra status items now remain highlighted while the window is open so it feels more like a native menu\n- No private API used, so it's Mac App Store safe\n\n## Getting Started\n\nThe library is available as a Swift Package Manager (SPM) package.\n\nUse the URL `https://github.com/orchetect/MenuBarExtraAccess` when adding the library to a project or Swift package.\n\nThen import the library:\n\n```swift\nimport SwiftUI\nimport MenuBarExtraAccess\n```\n\n### Standard Menu Style\n\nAn example of showing the menu extra menu by clicking a button in a window:\n\n```swift \n@main struct MyApp: App {\n    @State var isMenuPresented: Bool = false\n    \n    var body: some Scene {\n        WindowGroup {\n            Button(\"Show Menu\") { isMenuPresented = true }\n        }\n        \n        MenuBarExtra(\"MyApp Menu\", systemImage: \"folder\") {\n            Button(\"Menu Item 1\") { print(\"Menu Item 1\") }\n            Button(\"Menu Item 2\") { print(\"Menu Item 2\") }\n        }\n        .menuBarExtraStyle(.menu)\n        .menuBarExtraAccess(isPresented: $isMenuPresented) { statusItem in // \u003c-- the magic ✨\n             // access status item or store it in a @State var\n        }\n    }\n}\n```\n\n### Window Style\n\nAn example of a button in the popup window dismissing the popup and performing an action:\n\n```swift \n@main struct MyApp: App {\n    @State var isMenuPresented: Bool = false\n    \n    var body: some Scene {\n        MenuBarExtra(\"MyApp Menu\", systemImage: \"folder\") {\n            MyMenu(isMenuPresented: $isMenuPresented)\n            \t.introspectMenuBarExtraWindow { window in // \u003c-- the magic ✨\n                    window.animationBehavior = .alertPanel\n                }\n        }\n        .menuBarExtraStyle(.window)\n        .menuBarExtraAccess(isPresented: $isMenuPresented) { statusItem in // \u003c-- the magic ✨\n             // access status item or store it in a @State var\n        }\n    }\n}\n\nstruct MyMenu: View {\n    @Binding var isMenuPresented: Bool\n\n    var body: some View {\n        Button(\"Perform Action\") { \n            isMenuPresented = false \n            performSomeAction()\n        }\n    }\n}\n```\n\n### Multiple MenuBarExtra\n\nMenuBarExtraAccess is fully compatible with one or multiple MenuBarExtra in an app.\n\nJust add an index number parameter to `.menuBarExtraAccess()` and `.introspectMenuBarExtraWindow()` that reflects the order of `MenuBarExtra` declarations.\n\n```swift\nvar body: some Scene {\n    MenuBarExtra(\"MyApp Menu A\", systemImage: \"folder\") {\n        MyMenu(isMenuPresented: $isMenuPresented)\n            .introspectMenuBarExtraWindow(index: 0) { window in // \u003c-- add index 0\n                // ...\n            }\n    }\n    .menuBarExtraStyle(.window)\n    .menuBarExtraAccess(index: 0, isPresented: $isMenuPresented) // \u003c-- add index 0\n    \n    MenuBarExtra(\"MyApp Menu B\", systemImage: \"folder\") {\n        MyMenu(isMenuPresented: $isMenuPresented)\n            .introspectMenuBarExtraWindow(index: 1) { window in // \u003c-- add index 1\n                // ...\n            }\n    }\n    .menuBarExtraStyle(.window)\n    .menuBarExtraAccess(index: 1, isPresented: $isMenuPresented) // \u003c-- add index 1\n}\n```\n\n## Future\n\nThe hope is that Apple implements native versions of these features (and more) in future iterations of SwiftUI!\n\nUntil then, a radar has been filed as a feature request: [FB11984872](https://github.com/feedback-assistant/reports/issues/383)\n\n## Menu Builder\n\nCheck out [MacControlCenterUI](https://github.com/orchetect/MacControlCenterUI), a SwiftUI package built on MenuBarExtraAccess for easily building Control Center style menus.\n\n## Known Issues\n\n- When using `.menuBarExtraStyle(.menu)`, SwiftUI causes the popup menu to block the runloop while the menu is open, which means:\n  - Observing the `isPresented` binding will not work as expected.\n  - Setting the `isPresented` binding to `false` while the menu is presented has no effect.\n  - The user must dismiss the menu themself to allow event flow to continue. We have no control over this until Apple decides to change the MenuBarExtra behavior.\n\n## Author\n\nCoded by a bunch of 🐹 hamsters in a trenchcoat that calls itself [@orchetect](https://github.com/orchetect).\n\n## License\n\nLicensed under the MIT license. See [LICENSE](https://github.com/orchetect/MenuBarExtraAccess/blob/master/LICENSE) for details.\n\n## Sponsoring\n\nIf you enjoy using MenuBarExtraAccess and want to contribute to open-source financially, GitHub sponsorship is much appreciated. Feedback and code contributions are also welcome.\n\n## Contributions\n\nContributions are welcome. Posting in [Discussions](https://github.com/orchetect/MenuBarExtraAccess/discussions) first prior to new submitting PRs for features or modifications is encouraged.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Forchetect%2Fmenubarextraaccess","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Forchetect%2Fmenubarextraaccess","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Forchetect%2Fmenubarextraaccess/lists"}