![](/img/trans.png)
[英]I'm uploading data from my Swift app to Amazon S3 and it drains battery like nothing else. How can this be avoided?
[英]How can I make my app behave like I'm changing Application is agent(UIElement) at runtime using Swift?
我正在編碼一個Mac應用程序,它是菜單欄右側的NSPopover
( Application is agent(UIElement)
設置為YES
)。 我允許用戶通過單擊並向下拖動彈出窗口來分離彈出窗口,這會將應用程序置於窗口內。 這很好。 但是,當將應用程序從菜單欄拖到窗口中時,我希望我的應用程序圖標出現在停靠欄中,並且希望在菜單欄的左側顯示特定於應用程序的菜單,就像Application is agent(UIElement)
設置為NO
。 相反,當關閉窗口並將應用程序返回到菜單欄中的彈出窗口時,我希望我的應用程序圖標從停靠區中消失,並且不再在菜單欄的左側顯示特定於Application is agent(UIElement)
的菜單( Application is agent(UIElement)
設置回YES
)。
從這個問題 ,我知道在運行時更改Application is agent(UIElement)
是不可能的。 但是,給出的答案是在Objective-C中進行的,自OS X 10.9起,最后一個功能似乎已被淘汰。 如何使我的應用程序具有與在運行時使用Swift更改Application is agent(UIElement)
相同的行為?
我知道,顯示的應用程序圖標/菜單欄菜單中會發生windowDidBecomeMain
和隱藏應用程序圖標/菜單欄菜單中會發生windowWillClose
。
謝謝。
經過大量的反復試驗,但我終於弄明白了。 您可以使用NSApp.setActivationPolicy
來代替Application is agent(UIElement)
使用NSApp.setActivationPolicy
。 現在這是我的代碼。 在應用程序委托中:
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
}
}
在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)
}
}
}
此外,如果在切換為常規菜單后無法顯示菜單和停靠圖標,則可能需要執行以下操作。 對於較舊的Mac OS版本,這絕對是必需的。
// needed to activate menu
NSArray *dockAppA = [NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.apple.loginwindow"];
NSRunningApplication *dock = [dockAppA firstObject];
[dock activateWithOptions:NSApplicationActivateIgnoringOtherApps];
NSArray *dockAppB = [NSRunningApplication runningApplicationsWithBundleIdentifier:@"myAppBundleIdentifier"];
NSRunningApplication *myApp = [dockAppB firstObject];
[myApp activateWithOptions:NSApplicationActivateIgnoringOtherApps];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.