Set menubar icon from ViewController - swift

How to change the menubar icon of a MacOS app from another ViewController?
AppDelegate.swift (inits menubar icon)
ViewController.swift (tries to set menubar icon ❌)
I found this but this isn't changing the menubar icon for me:
Mac: How to save alternate app icon in dock OSX xcode
let image = NSImage.init(named: NSImage.Name(rawValue: "AltAppIcon"))
NSApp.applicationIconImage = image
See how the BOINC icon has the little custom pause symbol/badge in the bottom right of it's menubar? This app's icon changes. Are they writing over the name of that file and changing it to the "paused icon" image maybe?
✅UPDATE*
A AppDelegate.swift function that set the menubar icon worked:
AppDelegate.swift
func setIcon() {
let onIcon = NSImage(named: "fv-mini-icon-green")
statusItem.button?.image = onIcon
}
ViewController.swift
func taskOnIcon() {
DispatchQueue.main.async(execute: {
let appDele = NSApplication.shared.delegate as! AppDelegate
appDele.setIcon()
})
}

Here is a way...
class AppDelegate: NSObject, NSApplicationDelegate {
var statusBarItem: NSStatusItem!
func applicationDidFinishLaunching(_ aNotification: Notification) {
let statusBar = NSStatusBar.system
statusBarItem = statusBar.statusItem(withLength: 16)
let button = statusBarItem.button
button?.image = NSImage(named: "fv-mini-icon-green")
// .. other code here

Related

How to make icon button change colors on dark or light backgrounds with Swift macOS status bar app

I have a status bar application. The problem is that my icon stay white at all times. Is there a way to make it go white on dark background and black on light backgrounds. I notice that must status bar apps have this feature
class AppDelegate: NSObject, NSApplicationDelegate {
static private(set) var instance: AppDelegate!
lazy var statusBarItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
let menu = ApplicationMenu()
func applicationDidFinishLaunching(_ notification: Notification) {
AppDelegate.instance = self
statusBarItem.button?.image = NSImage(named: NSImage.Name("E22"))
statusBarItem.button?.imagePosition = .imageLeading
statusBarItem.menu = menu.createMenu()
}
}

NSStatusItem icon randomly dissappearing

I'm trying to figure out a weird problem that's happening with my menu bar app. It would run perfectly fine for a couple of hours or even days but would randomly disappear from the menu bar.
In activity monitor, it's still running in the background. There's a global keyboard shortcut in the app to show the window and it brings out the app no problem but the menu bar icon is still missing.
I'm on macOS Monterery 12.2.1
StatusBarController
class StatusBarController {
private var statusBar: NSStatusBar
private var statusItem: NSStatusItem
public var popover: NSPopover
private var eventMonitor: EventMonitor?
init(_ popover: NSPopover)
{
self.popover = popover
statusBar = NSStatusBar.init()
statusItem = statusBar.statusItem(withLength: 28.0)
if let statusBarButton = statusItem.button {
statusBarButton.image = #imageLiteral(resourceName: "link")
statusBarButton.image?.size = NSSize(width: 18.0, height: 18.0)
statusBarButton.image?.isTemplate = true
statusBarButton.action = #selector(togglePopover(sender:))
statusBarButton.target = self
}
}
.......}
App Delegate
#NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
var popover = NSPopover.init()
var statusBar: StatusBarController?
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Create the SwiftUI view that provides the contents
let contentView = ContentView()
// Set the SwiftUI's ContentView to the Popover's ContentViewController
popover.contentViewController = MainViewController()
popover.contentViewController?.view = NSHostingView(rootView: contentView)
popover.animates = false
KeyboardShortcuts.onKeyUp(for: .triggerPopover, action: {
self.statusBar?.togglePopover(sender: self)
})
// Create the Status Bar Item with the Popover
statusBar = StatusBarController.init(popover)
}
}
From Status Bar Programming Topics:
A system-wide status bar resides at the right side of the menu bar and is the only status bar currently available.
NSStatusBar.init() doesn't return the system status bar. Use NSStatusBar.system instead.

Request key for TextField in macOS status bar app

I am writing a little status bar app, which includes a text input.
The app is added to the status bar as a NSMenuItem which holds a NSHostingViewController with the SwiftUI-View. The status bar menu is added through an App delegate inside SwiftUIs #main scene. I've set "Application is agent" in the info.plist to true and I have an empty scene as #main in SwiftUI.
Problem: When I try to edit text, in the status bar app, the text field is not clickable and does not receive text input. When I add a window, the text field in the status bar app works as expected as long as the window is in foreground.
From my understanding, this is caused by the text field not being inside a window marked as key window. Is there any workaround to make the text field work as expected without the need for an additional app window?
Minimal example for the app delegate:
class AppDelegate: NSObject, NSApplicationDelegate {
private var statusItem: NSStatusItem?
func applicationDidFinishLaunching(_ notification: Notification) {
self.statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
if let statusBarButton = statusItem?.button {
statusBarButton.image = NSImage(systemSymbolName: "6.circle", accessibilityDescription: "")
statusBarButton.image?.isTemplate = true
}
let menuItem = NSMenuItem()
menuItem.view = createView()
let menu = NSMenu()
menu.addItem(menuItem)
statusItem?.menu = menu
}
private func createView() -> NSView {
let view = HostingView(rootView: ContentView())
view.frame = NSRect(x: 0, y: 0, width: 520, height: 260)
return view
}
}
SwiftUI #main
import SwiftUI
import AppKit
#main
struct MacApp: App {
#NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
Settings {
EmptyView()
}
}
}
Edit: I figured out, that there are two windows registered for my app. One for the status bar icon and one for the menu. However, the code below does not result in the menu window to become the key window. (although it is in fact the only window which returns true for .canBecomeKey)
.onAppear {
let window = NSApp.windows.first { $0.canBecomeKey }
window?.makeKey()
}
Second Edit: Apparently, the problem is not, that the window is not able to become key, but that the app is not set as the active app when the status bar menu is opened. I came up with this rather ugly hack, which causes the menu to close and re-open when another app was in focus before, but at least buttons and textfields work as expected with this:
func applicationDidFinishLaunching(_ notification: Notification) {
...
// Also let AppDelegate implement NSMenuDelegate
menu.delegate = self
...
}
func menuWillOpen(_ menu: NSMenu) {
#warning("This is a hack to activate the app when the menu is shown. This will cause the menu to close and re-open when another window was focused before.")
if !NSApp.isActive {
NSApp.activate(ignoringOtherApps: true)
DispatchQueue.main.async {
self.statusItem?.button?.performClick(nil)
}
}
}
I am still looking for a proper solution.

Status button highlight when popover is active (Swift, macOS)

I am making a status bar macOS app. On clicking on the status bar icon, I am showing an NSPopover (not an NSMenu). However, when my NSPopover is shown, my status menu icon is not highlighted. It is only highlighted for a moment when I click it. I want it to stay highlighted, much like how it behaves with the wifi status bar icon.
I know that if I use a NSMenu instead of a NSPopover, it can probably be fixed. But the requirement is such that I need to use a NSPopover.
I have tried the following approaches, but to no avail:
1.
let statusItem = NSStatusBar.system.statusItem(withLength:NSStatusItem.squareLength)
if let button = statusItem.button {
button.setButtonType(.pushOnPushOff)
}
statusItem.highlightMode = true
statusItem.button?.highlight(true)
statusItem.button?.isHighlighted = true
I am not very experienced with status bar apps. So I am not really sure about which approach to take here.
The left most icon is my status bar app icon. The popover is currently active but the icon is not highlighted. I had to crop out the popover.
This can be done, but to do it reliably requires some tight coupling. In this example, I assume you have a NSViewController named PopoverController that has a togglePopover() method and that you've set this controller as the target of the status bar button.
Step 0: Context
Here's the basic setup of the class that controls the popover:
final class PopoverController: NSViewController
{
private(set) var statusItem: NSStatusItem!
private(set) var popover: NSPopover
// You can set up your StatusItem and Popover anywhere; but here's an example with -awakeFromNib()
override func awakeFromNib()
{
super.awakeFromNib()
statusItem = NSStatusBar.system.statusItem(withLength: 20.0)
statusItem.button?.sendAction(on: [.leftMouseDown, .rightMouseDown])
statusItem.button?.image = NSImage(named: "statusBar-icon")
statusItem.button?.target = self
statusItem.button?.action = #selector(togglePopover)
popover = NSPopover()
popover.behavior = .transient
popover.delegate = self
popover.contentViewController = self
popover.setValue(true, forKeyPath: "shouldHideAnchor") // Private API
}
#objc func togglePopover()
{
if popover.isShown
{
popover.performClose(nil)
}
else if !popover.isShown,
let button: NSButton = statusItem.button,
button.window?.contentView != nil, // Exception thrown if view is nil or not part of a window.
button.superview != nil
{
popover.show(relativeTo: .zero, of: button, preferredEdge: .minY)
}
}
}
Step 1: Override the Status Button
extension NSStatusBarButton
{
public override func mouseDown(with event: NSEvent)
{
if event.modifierFlags.contains(.control)
{
self.rightMouseDown(with: event)
return
}
if let controller: PopoverController = self.target as? PopoverController
{
controller.togglePopover()
self.highlight(controller.popover.isShown)
}
}
}
Step 2: Handle Popover Closing
Make sure PopoverController conforms to NSPopoverDelegate and implement this delegate method:
func popoverDidClose(_ notification: Notification)
{
statusItem.button?.highlight(false)
}
Outcome
With all of that in place, the button highlighting now works just as it does for Apple's system status bar items like Control Center, Wifi, Battery, etc.
Note: you'll also need to add a global event monitor to listen for clicks that happen outside of your popover to ensure that it closes properly when the user clicks away from it. But that's outside the scope of this question and available elsewhere on SO.

Pop a View when click the StatusBar Item

I expect to present a View when click the StatusBar Item like this:
And I have set up the StatusItem
import Cocoa
#NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
#IBOutlet weak var imageManager: NSMenu!
let statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(-1)
func applicationDidFinishLaunching(aNotification: NSNotification) {
let icon = NSImage(named: "statusIcon")
icon?.template = true
statusItem.image = icon
statusItem.menu = imageManager
statusItem.action = nil
}
What should I do next?
The simplest approach would be to create a NSPopover object and attach it to the status item. There are multiple tutorials online for this. Here is a good one!
Although, if you need a more custom appearance, you need to write your own. For this you will need a borderless window with transparent background. Its contentView will draw the frame of the speech-bubble ish design..