【问题标题】:Swift Mac Os Application - NSSavePanel does not behave correctly with a 'background only' applicationSwift Mac Os 应用程序 - NSSavePanel 在“仅后台”应用程序中的行为不正确
【发布时间】:2020-02-11 14:05:56
【问题描述】:

我正在开发一个 swift Mac OS 应用程序,它只在状态栏(带有弹出框)中工作,Application is background only 设置为 true

在弹出框内,用户可以与按钮交互以保存图像,更具体地说,触发了以下功能:

@IBAction func saveImage(sender: NSButton) {
    self.closePopover()

    let dialog = NSSavePanel()

    let date = Date()
    let formattedDate = getFormattedDate(date: date, format: "yyyy-MM-dd")
    let formattedTime = getFormattedDate(date: date, format: "HH.mm.ss")
    dialog.level = .modalPanel
    dialog.title                    = "Choose a destination"
    dialog.nameFieldStringValue     = "Screenshot \(formattedDate) at \(formattedTime)"
    dialog.showsResizeIndicator     = true
    dialog.showsHiddenFiles         = false
    dialog.canCreateDirectories     = true
    dialog.allowedFileTypes         = ["png"]

    if (dialog.runModal() == .OK) {
        // Pathname of the file
        if let result = dialog.url {
            self.screenshot!.savePNGRepresentationToURL(url: result)
        }
    } else {
        // User clicked on "Cancel"
        return
    }
}

问题是:

  • 如果我把Application is background only = true:
    我无法与 NSSavePanel 交互(无法更改 save as 字段),如果我在该框中写入,则文本将写入我正在交互的最后一个应用程序中(焦点并不真正在模式上)。
  • 如果我把Application is background only = false:
    NSSavePanel 工作得很好,但这不是一个好的解决方案,因为我不想让图标出现在 Dock 中

所以问题显然与Application is background only 有关,但我没有找到同时拥有两者的方法:

  • Dock 中没有应用图标
  • 与 NSSavePanel 的良好交互

【问题讨论】:

  • 您可能需要启动另一个应用程序。看看 Dropbox 菜单的工作原理。它只是后台,但有时它会启动它的应用程序。
  • 对于保管箱“同步问题”部分,您是对的。这可能是解决它的一种方法。我正在寻找一种以编程方式仅关闭背景的方法,然后在选择完成后再次打开它......但您的建议可能更好。
  • 使用NSApplication.shared.setActivationPolicy 成功了,我会发布我的答案

标签: swift macos panel nsopenpanel nssavepanel


【解决方案1】:

我使用NSApplication.shared.setActivationPolicy 方法解决了这样的问题:

@IBAction func saveImage(sender: NSButton) {
    self.closePopover()

    NSApplication.shared.setActivationPolicy(.regular)

    let dialog = NSSavePanel()

    let date = Date()
    let formattedDate = getFormattedDate(date: date, format: "yyyy-MM-dd")
    let formattedTime = getFormattedDate(date: date, format: "HH.mm.ss")
    dialog.level = .modalPanel
    dialog.title                    = "Choose a destination"
    dialog.nameFieldStringValue     = "Screenshot \(formattedDate) at \(formattedTime)"
    dialog.showsResizeIndicator     = true
    dialog.showsHiddenFiles         = false
    dialog.canCreateDirectories     = true
    dialog.allowedFileTypes         = ["png"]

    if (dialog.runModal() == .OK) {
        // Pathname of the file
        if let result = dialog.url {
            self.screenshot!.savePNGRepresentationToURL(url: result)
        }
    } else {
        // User clicked on "Cancel"
    }

    NSApplication.shared.setActivationPolicy(.accessory)
}

【讨论】:

    猜你喜欢
    • 2016-06-07
    • 2017-03-28
    • 1970-01-01
    • 1970-01-01
    • 2013-04-11
    • 1970-01-01
    • 1970-01-01
    • 2012-06-17
    • 2019-06-22
    相关资源
    最近更新 更多