In the beginning, I used Election + Vue to build a MAC taskbar calendar, but it was packaged up to be bulky and Election was not available on the MAC Store.

So, today came up with the use of Swift native to write another, right as learning Swift language.

Today, the focus is on simulating a notification bar.

start

I have always wanted to learn SwiftUI and now I have an excuse. To create a new application, select Interface: SwiftUI, Language: Swift.

In fanlymenuApp, we removed the main page:

var body: some Scene {
    Settings {
        EmptyView()
    }
}
Copy the code

This will remove the home page layout directly and place the focus on the notification bar.

At that time, you need to use NSApplicationDelegateAdaptor:

@NSApplicationDelegateAdaptor(AppDelegate.self) var delegate
Copy the code

createAppDelegateClass:

class AppDelegate: NSObject, NSApplicationDelegate {}
Copy the code

The NSStatusItem class creates an NSStatusItem and clicking on the status bar pops up an NSPopover:

// Status Bar Item...
var statusItem: NSStatusItem?
// PopOver...
var popOver = NSPopover()
Copy the code

In func applicationDidFinishLaunching (_ notification: notification) method, we initialize them:

// Menu View... let menuView = ContentView() // Creating PopOver... popOver.behavior = .transient popOver.animates = true // Setting Empty View Controller... // And Setting View as SwiftUI View... // with the help of Hosting Controller... popOver.contentViewController = NSViewController() popOver.contentViewController? .view = NSHostingView(rootView: menuView)Copy the code

This is a little bit easier to understand, basically loading the ContentView in popOver.

This ContentView is the Demo from the beginning of the project.

Think of this View as the main page:

// also Making View as Main View... popOver.contentViewController? .view.window? .makeKey()Copy the code

The next step is to initialize NSStatusItem:

// Creating Status Bar Button... statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength) // Safe Check if status Button is Available or not... if let MenuButton = statusItem? .button { MenuButton.image = NSImage(systemSymbolName: "icloud.and.arrow.up.fill", accessibilityDescription: nil) MenuButton.imagePosition = NSControl.ImagePosition.imageLeft MenuButton.title = "demo" MenuButton.action = #selector(MenuButtonToggle) }Copy the code

After creation, add its button event:

// Button Action @objc func MenuButtonToggle(sender: AnyObject) { // For Safer Sice... if popOver.isShown { popOver.performClose(sender) } else { // Showing PopOver if let menuButton = statusItem? .button { // Top Get Button Location For Popover Arrow... self.popOver.show(relativeTo: menuButton.bounds, of: menuButton, preferredEdge: NSRectEdge.maxY) } } }Copy the code

Click on the notification bar to pop up the PopOver page below, as shown at the beginning.

Now that the status bar is basically simulated, the next step is to hide the app icon in the dock, which is a simple notification bar application.

Add Application is Agent (UIElement) to YES in Info

conclusion

After Election + Vue, Swift, who has been reading Rust’s book. Learn as much as you can. It’s fun to learn the essence of different languages.

To be continued