Pop a View when click the StatusBar Item - swift

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..

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()
}
}

Set menubar icon from ViewController

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

Swift Access Objects In View Controller From Window Controller

I'm just getting into development on mac os and I made a simple app for the touch bar that allows you to change the color (with a nscolorpicker) of a label that is also on the touch bar.
Now I would like to get the same effect on the actual window like so: I change the color using the picker on the touch bar and the color of the colorwell in the window changes as well.
This is the code that I currently have for the touch bar actions:
import Cocoa
#available(OSX 10.12.2, *)
class MainWindowController: NSWindowController {
#IBOutlet weak var cptHello: NSColorPickerTouchBarItem!
#IBOutlet var lblHello: NSTextField!
override func windowDidLoad() {
super.windowDidLoad()
// Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
cptHello.color = NSColor.white
setCol()
}
func setCol(){
lblHello.textColor = cptHello.color
}
#IBAction func colorPicked(_ sender: Any) {
setCol()
}
}
This piece of code resides in MainWindowController.swift which is paired with the window controller.
In the view controller, I have a single NSColorWell that I would like to change the color for inside the function "setCol()". I created an outlet in the view controller for it like so:
#IBOutlet var cwHello: NSColorWell!
So ideally what I want to achieve is something like this:
func setCol(){
lblHello.textColor = cptHello.color
ViewController.cwHello.color = cptHello.color
}
Can this be done at all?

How do you reference the view's window in Swift 3.x using Storyboards/Cocoa

with all the changes in Xcode and Swift, I can't figure out how to address the view's window in a stotyboard-driven project in the way I'm doing it in projects using XIBs. New to macOS programming so apologies in advance if this is basic stuff:
When using a storyboard, how do I change the view's window state using for instance:
window.titleVisibility = .hidden
window.setContentSize(size)
In an xib-driven project, I'm using
#IBOutlet weak var window: NSWindow!
but this doesn't seem to work the same way with storyboards. How do I do the same tihing using storyboards? Any help appreciated!
You can get a reference of your view window accessing its window property. Note that it can not be done inside view did load but you can create a window property for your view controller using a lazy initializer:
lazy var window: NSWindow! = self.view.window
And you can do your window customization inside the method viewWillAppear:
import Cocoa
class ViewController: NSViewController {
lazy var window: NSWindow! = self.view.window
override func viewWillAppear() {
super.viewWillAppear()
window.titleVisibility = .hidden
}
}
This seems to work:
if let window = NSApplication.shared().mainWindow {
window.titleVisibility = .hidden
}
Is this an ok way to do it?

How can I use NSVisualEffectView to blend window with background

There seem to be a bunch of questions on this for old versions of Swift/Xcode, but for some reason it hasn't been working with the latest update. I created a NSVisualEffectView, blurryView, and added the subview to my main view:
class ViewController: NSViewController {
#IBOutlet weak var blurryView: NSVisualEffectView!
override func viewDidLoad() {
super.viewDidLoad()
//background styling
blurryView.wantsLayer = true
blurryView.blendingMode = NSVisualEffectBlendingMode.behindWindow
blurryView.material = NSVisualEffectMaterial.dark
blurryView.state = NSVisualEffectState.active
self.view.addSubview(blurryView, positioned: NSWindowOrderingMode.above, relativeTo: nil)
// Do any additional setup after loading the view.
}
...
}
But when I run it, there is no effect on the window. (when I set it to within window, and layer it on top of my other view, the blur works correctly, but I only want the window to blur.) I also tried doing the same thing in my App Delegate class, but I can't connect my window as an outlet, and therefore can't add the blurry view to the window. Here's what the code would look like:
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
blurryView.wantsLayer = true
blurryView.blendingMode = NSVisualEffectBlendingMode.withinWindow
blurryView.material = NSVisualEffectMaterial.dark
blurryView.state = NSVisualEffectState.active
self.window.contentView?.addSubview(blurryView)
}
...
}
To get an idea if what I'm looking for: NSVisualEffectView Vibrancy
It works quite easy:
In Interface Builder drag a NSVisualEffectView directly as a subview of the main view of your scene.
In the Properties Inspector set Blending Mode to Behind Window
Add the rest of the views you need as subviews of the NSVisualEffectView
That's it, you're done
Here's an example:
Panel 1 View Controller is my blurred view, Background View is the first (non-blurred) view in my "real"view hierarchy.
Swift 5:
Simply add this to your viewWillAppear and it should work:
override func viewWillAppear() {
super.viewWillAppear()
//Adds transparency to the app
view.window?.isOpaque = false
view.window?.alphaValue = 0.98 //you can remove this line but it adds a nice effect to it
let blurView = NSVisualEffectView(frame: view.bounds)
blurView.blendingMode = .behindWindow
blurView.material = .fullScreenUI
blurView.state = .active
view.window?.contentView?.addSubview(blurView)
}