How do you make the Application window open when the dock icon is clicked?

后端 未结 6 2056
余生分开走
余生分开走 2020-12-07 22:50

I\'m surprised this doesn\'t happen automatically, but I would like my applications window to open automatically when the Dock icon is clicked.

Just to clarify, when

6条回答
  •  不知归路
    2020-12-07 23:28

    As others pointed, using applicationShouldHandleReopen method for reopening closed windows in non-document apps is the right way. The only change I want to add is a more flexible way to check what window must be re-displayed, by iterating through the NSApplication's list of visible and invisible .windows and checking for the required window.

    func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
    
        if flag == false {
    
            for window in sender.windows {
    
                if (window.delegate?.isKind(of: WelcomeWindowController.self)) == true {
                    window.makeKeyAndOrderFront(self)
                }
            }
        }
    
        return true
    }
    

    Appendix

    a) If window was hidden then it will be showed automatically when user will click on app's Dock icon, so no need to implement applicationShouldHandleReopen method.

    b) Checked "Release when closed" option doesn't affect in any way the above behaviour.

提交回复
热议问题