How can I make my app behave like I'm changing Application is agent(UIElement) at runtime using Swift?

TonyStark4ever

I am coding a Mac app that is an NSPopover on the right side of the menu bar (Application is agent(UIElement) is set to YES). I allow the user to detach the popover by clicking and dragging it downward, which puts the app inside of a window. This is working fine; however, when the app is dragged off of the menu bar and made into a window, I would like my app's icon to come up in the dock, as well as show app-specific menus on the left side of the menu bar, as if Application is agent(UIElement) is set to NO. Conversely, when the window is closed and the app is returned to a popover in the menu bar, I would like my app's icon to disappear from the dock and no longer show app-specific menus on the left side of the menu bar (Application is agent(UIElement) is set back to YES).

From this question, I understand that changing Application is agent(UIElement) at runtime isn't possible. However, the answer given is in Objective-C, and the last function appears to be depreciated since OS X 10.9. How can I make my app have the same behavior as changing Application is agent(UIElement) at runtime using Swift?

I know that showing the app icon/menu bar menus would happen in windowDidBecomeMain and hiding the app icon/menu bar menus would happen in windowWillClose.

Thanks.

TonyStark4ever

It took a lot of trial and error, but I finally figured it out. Instead of using Application is agent(UIElement), you use NSApp.setActivationPolicy. This is now my code. In the App Delegate:

var isWindow = false

class AppDelegate: NSObject, NSApplicationDelegate, NSPopoverDelegate {

    let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength)
    let popover = NSPopover()

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        // Insert code here to initialize your application

        NSApp.setActivationPolicy(.accessory)

        if let button = statusItem.button {
            button.image = NSImage(named: "StatusBarImage")
            button.action = #selector(togglePopover(_:))
        }
        popover.contentViewController = MainViewController.loadController()
        popover.delegate = self
        popover.animates = false
        popover.behavior = .transient
    }

    @objc func togglePopover(_ sender: Any?) {
        if popover.isShown == true {
            popover.performClose(sender)
        } else if detachedWindowController.window!.isVisible {
            detachedWindowController.window?.setIsVisible(false)
            isWindow = true
        } else if isWindow == true {
            detachedWindowController.window?.setIsVisible(true)
        } else {
            if let button = statusItem.button {
                popover.show(relativeTo: button.bounds, of: button, preferredEdge: NSRectEdge.minY)
            }
        }
    }

    lazy var detachedWindowController: DetachedWindowController = {
        let detachedWindowController = DetachedWindowController(windowNibName: "DetachedWindowController")
        detachedWindowController.contentViewController = MainViewController.loadController()
        return detachedWindowController
    }()

    func popoverShouldDetach(_ popover: NSPopover) -> Bool {
        return true
    }

    func detachableWindow(for popover: NSPopover) -> NSWindow? {
        return detachedWindowController.window
    }
}

In the DetachedWindowController:

class DetachedWindowController: NSWindowController, NSWindowDelegate {

    @IBOutlet var detachedWindow: NSWindow!

    override func windowDidLoad() {
        super.windowDidLoad()

        detachedWindow.delegate = self
    }

    func windowWillClose(_ notification: Notification) {
        isWindow = false
        NSApp.setActivationPolicy(.accessory)
    }

    func windowDidBecomeMain(_ notification: Notification) {
        if NSApp.activationPolicy() == .accessory {
            NSApp.setActivationPolicy(.regular)
        }
    }
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How can i make my springboot app websocket app get connected via a "ws://" url while using stomp

分類Dev

How can I configure my Laravel application to load using MAMP?

分類Dev

How can I allow background music to continue playing while my app still plays its sounds while using Swift

分類Dev

How can i add music settings for my app?? Like Android games

分類Dev

How can I make AutoLayout crash my app when it logs unsatisfiable constraints in debug console?

分類Dev

Swift, macos - Can I detect or make other app to be in the foreground?

分類Dev

How can I embed a Facebook Like Box in my website?

分類Dev

How can I treat my disk like one big file?

分類Dev

Magento - How can I tell if I'm on a "My Account" page?

分類Dev

How can I find why system can not run my application?

分類Dev

How can I integrate FusionChart in my SPA application developed using Durandal and Knockout?

分類Dev

How can I make eclipse detect my new java class in my m2e maven project?

分類Dev

How can I bundle a command line utility in os x application on Mac App Store (using sandbox entitlement)

分類Dev

How can I connect my Flutter app to local sql server database using API?

分類Dev

How do I include my app as a module in a Swift Playground?

分類Dev

How can i make my database method more future proof?

分類Dev

How can I make my glob work for cogs on Linux

分類Dev

How can I make my trie more efficient?

分類Dev

How can I make a table inside my shiny box with RandomIcon()

分類Dev

How can I make a triangle drawable on the top left of my view?

分類Dev

How can I make my button call a rotation of GIFs?

分類Dev

How can I make my sprite not go crosswise in pygame?

分類Dev

How can I make my div's appear in a horizontal way?

分類Dev

How can I make the pixel value of the height in my code dynamic?

分類Dev

How can I make my bot delete its own message?

分類Dev

My Prestashop is slow - how can I make it faster?

分類Dev

How can I embed CEF3 into my OSX application?

分類Dev

How can I make url rewrite on google cloud app engine?

分類Dev

In swift, how can i remove an object from my tableview?

Related 関連記事

  1. 1

    How can i make my springboot app websocket app get connected via a "ws://" url while using stomp

  2. 2

    How can I configure my Laravel application to load using MAMP?

  3. 3

    How can I allow background music to continue playing while my app still plays its sounds while using Swift

  4. 4

    How can i add music settings for my app?? Like Android games

  5. 5

    How can I make AutoLayout crash my app when it logs unsatisfiable constraints in debug console?

  6. 6

    Swift, macos - Can I detect or make other app to be in the foreground?

  7. 7

    How can I embed a Facebook Like Box in my website?

  8. 8

    How can I treat my disk like one big file?

  9. 9

    Magento - How can I tell if I'm on a "My Account" page?

  10. 10

    How can I find why system can not run my application?

  11. 11

    How can I integrate FusionChart in my SPA application developed using Durandal and Knockout?

  12. 12

    How can I make eclipse detect my new java class in my m2e maven project?

  13. 13

    How can I bundle a command line utility in os x application on Mac App Store (using sandbox entitlement)

  14. 14

    How can I connect my Flutter app to local sql server database using API?

  15. 15

    How do I include my app as a module in a Swift Playground?

  16. 16

    How can i make my database method more future proof?

  17. 17

    How can I make my glob work for cogs on Linux

  18. 18

    How can I make my trie more efficient?

  19. 19

    How can I make a table inside my shiny box with RandomIcon()

  20. 20

    How can I make a triangle drawable on the top left of my view?

  21. 21

    How can I make my button call a rotation of GIFs?

  22. 22

    How can I make my sprite not go crosswise in pygame?

  23. 23

    How can I make my div's appear in a horizontal way?

  24. 24

    How can I make the pixel value of the height in my code dynamic?

  25. 25

    How can I make my bot delete its own message?

  26. 26

    My Prestashop is slow - how can I make it faster?

  27. 27

    How can I embed CEF3 into my OSX application?

  28. 28

    How can I make url rewrite on google cloud app engine?

  29. 29

    In swift, how can i remove an object from my tableview?

ホットタグ

アーカイブ