{"id":17688655,"url":"https://github.com/boybeak/tray","last_synced_at":"2025-05-13T00:48:30.611Z","repository":{"id":242708359,"uuid":"810225471","full_name":"boybeak/Tray","owner":"boybeak","description":"Build a menubar app easily on macOS. Support both SwiftUI and AppKit.","archived":false,"fork":false,"pushed_at":"2024-12-18T03:34:36.000Z","size":61,"stargazers_count":8,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-13T00:48:20.987Z","etag":null,"topics":["macos-menubar"],"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/boybeak.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"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}},"created_at":"2024-06-04T09:36:00.000Z","updated_at":"2025-04-30T06:27:14.000Z","dependencies_parsed_at":"2024-10-24T14:25:46.764Z","dependency_job_id":"59052070-7eca-4dae-8259-045e2755109f","html_url":"https://github.com/boybeak/Tray","commit_stats":null,"previous_names":["boybeak/tray"],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boybeak%2FTray","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boybeak%2FTray/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boybeak%2FTray/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boybeak%2FTray/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/boybeak","download_url":"https://codeload.github.com/boybeak/Tray/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253850882,"owners_count":21973672,"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-menubar"],"created_at":"2024-10-24T11:44:59.571Z","updated_at":"2025-05-13T00:48:30.590Z","avatar_url":"https://github.com/boybeak.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Tray\nBuild a menubar app easily on macOS. Support both **SwiftUI** and **AppKit**.\n\n## Install\nOpen **XCode**, then **File** -\u003e **Add Package Dependencies...**, on the new window, copy `https://github.com/boybeak/Tray.git` and paste it to search input.\n\n## Usage\nImport `Tray` before use it.\n```swift\nimport Tray\n```\nThen, on your `AppDelegate` class, declare a class variable `private let tray = Tray()`. Then set arguments to the tray in `applicationDidFinishLaunching`, after that, install it.\n```swift\nclass AppDelegate: NSObject, NSApplicationDelegate {\n    \n    private let tray: Tray!\n    \n    func applicationDidFinishLaunching(_ notification: Notification) {\n        tray = Tray.install(named: \"TrayIcon\") { tray in \n            // config Tray here\n            // 1. setView\n            tray.setView(content: ContentView()) \n            // tray.setView(view: ) or tray.setView(viewController: )\n            // Some other optional parameters:\n            // - behavior: NSPopover behavior, default is .transient;\n            // - level: NSPopover window level, default is .floating;\n            // - size: Size of NSPopover, default is nil, depends on the view you passed in.\n\n            // 2. If you do not need a view, you can set a left click event\n            tray.setOnLeftClick(onClick: {\n                self.onNewNoteAction()\n                return true // return true, If you handled event and prevent default action, the default action is show popover view if you set.\n            })\n\n            // 3. And also you can set a right click evnet\n            tray.setOnRightClick(onClick: {\n                return true // return true, If you handled event and prevent default action, the default action is show menu if you set.\n            })\n\n            // 4. If you need a menu for right click.\n            let menu = NSMenu()\n        \n            let newNoteMenuItem = NSMenuItem(title: NSLocalizedString(\"Menu_item_new_note\", comment: \"\"), action: #selector(onNewNoteAction), keyEquivalent: \"\")\n            let quitMenuItem = NSMenuItem(title: NSLocalizedString(\"Menu_item_quit\", comment: \"\"), action: #selector(onQuitAction), keyEquivalent: \"\")\n            \n            menu.addItem(newNoteMenuItem)\n            menu.addItem(quitMenuItem)\n            tray.setMenu(menu: menu)\n\n        }\n    }\n    \n    @objc func onQuitAction() {\n        NSApplication.shared.terminate(nil)\n    }\n}\n```\n\n## Suggestions\n\n### 1. Tray icon size\nThe tray icon size: 18\\*18 for 1x, 36\\*36 for 2x and 54\\*54 for 3x.\n\n### 2. Work with SwiftUI\nIf you are using SwiftUI, you can init AppDelegate like below:\n```swift\n@main\nstruct MyApp: App {\n    @NSApplicationDelegateAdaptor(AppDelegate.self) var app: AppDelegate\n\n    var body: some Scene {\n        Settings {}\n    }\n}\n```\n~~The code `Settings {}` will hide main window if your app is a tray only app.~~\n\nThis doesn't work for macOS 15 and later. Use [NoLaunchWin](https://github.com/boybeak/NoLaunchWin) instead.\n\n#### 2.1 Anchor triangle is not in the middle of the window?\n![anchor-triangle-issue](./art/anchor-triangle-issue.jpg)\nIf you are using swiftUI, you'd better both set size in tray and swiftUI's view.\n```swift\n// Tray install\nTray.install(content: ContentView(), size: CGSize(width: 320, height: 320))\n```\n```swift\nstruct ContentView: View {\n    var body: some View {\n        VStack {\n            Text(\"Hello, world!\")\n        }\n        .frame(width: 320, height: 320)\n    }\n}\n```\nAfter config size, it looks normal.\n\n![anchor-triangle-normal](./art/anchor-triangle-normal.jpg)\n\n### 3. Hide app's docker icon\nIn Info.plist add an options: **Application is agent(UIElement)** - **YES**\n\n## More\nWho use this library?\n1. [Translator](https://github.com/boybeak/TranslatorDocs);\n2. [JustTodo](https://github.com/boybeak/JustTodo);\n3. [DeskNote](https://github.com/boybeak/DeskNote);\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fboybeak%2Ftray","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fboybeak%2Ftray","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fboybeak%2Ftray/lists"}