StatusItem.text only shows when clicked, then disappears

Hi everyone.

I wanted to make a Menu Bar application to count my influencer projects and I finished the app, it shows everything I need. But, of course, I have to click the Menu Bar item. I want to make it visible directly from menubar. How can I achieve this?

(e.g. iStat Menus, LyricX)

Thank you!

Edit : I nearly achieved what I trying to do. The app shows Title and Menu Bar Icon for a moment when I click the menubar item, but then disappears.

2019-11-11%2013_52_24

ViewController.swift

import Cocoa

class ViewController: NSViewController {

@IBOutlet weak var dosyaSayisi: NSTextField!
@IBOutlet weak var gelenKazanc: NSTextField!


@IBAction func quit(_ sender: Any) {
    NSApplication.shared.terminate(sender)
}

func applicationDidFinishLaunching(_ aNotification: Notification) {
    
}

let numberOfItems = try! FileManager.default.contentsOfDirectory(atPath: "/Users/oguzhanvarsak/Documents/APPS24").count

override func viewDidLoad() {
    super.viewDidLoad()
    dosyaSayisi.stringValue = String(numberOfItems.self - 2)
    gelenKazanc.stringValue = String(numberOfItems.self * 20) + " ₺"
    let statusItem2 = AppDelegate().statusItem
    statusItem2.button!.title = dosyaSayisi.stringValue + " uygulama, " + gelenKazanc.stringValue
    statusItem2.length = NSStatusItem.variableLength
}

override var representedObject: Any? {
    didSet {
    // Update the view, if already loaded.
    }
}
}

AppDelegate.swift

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
    let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
    let popoverView = NSPopover()

func applicationDidFinishLaunching(_ aNotification: Notification) {
    statusItem.button?.image = NSImage(named:NSImage.Name("StatusBarButtonImage"))
    statusItem.button?.target = self
    if let button = statusItem.button {
        button.action = #selector(self.doSomeAction(sender:))
        button.sendAction(on: [.leftMouseUp, .rightMouseUp])
    }
}

func applicationWillTerminate(_ aNotification: Notification) {
    // Insert code here to tear down your application
}

func constructMenu() {
  let menu = NSMenu()
  menu.addItem(NSMenuItem(title: "Quit Quotes", action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q"))

  statusItem.menu = menu
}

func closePopover(sender: Any?) {
      popoverView.performClose(sender)
}

func showPopover(sender: Any?) {
  if let button = statusItem.button {
    
    popoverView.show(relativeTo: button.bounds, of: button, preferredEdge: .maxY)
  }
}

@objc func doSomeAction(sender: NSStatusItem) {
let event = NSApp.currentEvent!
    
if event.type == NSEvent.EventType.rightMouseUp {

} else {
    let storyboard = NSStoryboard(name: "Main", bundle: nil)
    
    guard let vc = storyboard.instantiateController(withIdentifier: "ViewController") as? ViewController else {
            fatalError("Unable to find ViewController in the storyboard.")
    }
    
    guard statusItem.button != nil else {
        fatalError("Couldn't find status item button.")
    }
    
    popoverView.contentViewController = vc
    popoverView.behavior = .transient
    
    if popoverView.isShown {
        closePopover(sender: sender)
    } else {
        showPopover(sender: sender)
    }
}
}
}

Not sure if this is the right forum for this kind of question since it's more about macOS Development than it is about using Swift.

1 Like
Terms of Service

Privacy Policy

Cookie Policy