2017-05-06 18 views
0

사용자가 빨간색 버튼을 통해 마지막 창을 닫으려고하면 응용 프로그램 호출을 숨기려고합니다.NSWindow close와 Multiple-Tab-NSWindow를 구분합니까?

참고 : 아래 언급 된 탭은 sierra 자동 탭 기능의 일부입니다.

나는 응용 프로그램을 NSApplication.shared().hide()으로 숨길 수 있다는 것을 알고 있지만 사용자가 마지막으로 열린 창을 닫으려고 할 때만이 작업을 수행하려고합니다. 즉, 해당 창에 대한 모든 탭을 닫을 수있는 빨간색 버튼). 그러나 탭의 닫기 단추가 정상적으로 수행되도록하고 탭을 닫고 싶습니다.

지금까지 탭 클로저와 윈도우 클로저가 API에서 동일하게 나타나며 원하는 동작을 달성하는 데 어려움을 겪고 있습니다. 빨간색 닫기 버튼 또는 탭 닫기 버튼을 통해 닫히는 지 확인하는 방법이 있습니까?

+0

사용자가 마지막 창을 닫을 때 숨기기를 호출하지 마십시오. 닫기 버튼은 숨기지 말고 닫아야합니다. 마지막 창을 닫은 후 종료하거나 마지막 창을 닫지 않도록하거나 앱이 가장 앞선 앱이 아닌 경우 창을 숨 깁니다. – Willeke

+0

Finder의 행동에 영감을 받았습니다. 마지막 찾기 창에 여러 개의 탭이있는 경우 해당 창을 닫았다가 다시 열면 다시 열 때 탭에 영향을주지 않는 것 같습니다. 그러나, 페이지 및 사파리에서 모든 탭이 닫힙니다 .... bah! – orion

+0

Finder는 창을 닫을 때 자체를 숨기지 않습니다. 그것은 활성 상태이고 가장 앞쪽에있는 앱입니다. Finder는 활성화 될 때 창을 복원합니다. Finder에서 창을 다시 여는 방법은 무엇입니까? – Willeke

답변

0

면책 조항 : 나는 단지 몇 주 동안 스위프트/코코아와 함께 일한지

  • .
  • 아마이 방법을 사용하는 것이 좋습니다.

이렇게 쉬운 방법을 찾을 수 없습니다. 내 구현의 배경은 창을 닫을 때 디 바운스 (debounced) 함수를 호출하는 것이다. 모서리에있는 "X"를 클릭하면 모든 창을 닫으므로 디 바운싱을 통해 모든 작업이 완료되면 한 번 호출 할 수 있습니다.

다른 것들도 있습니다 : 탭에있는 창 수를 캐시해야합니다. 닫혀있는 창 목록을 유지해야합니다.

하지만 결국, 당신은 구별 할 수 있습니다

  • 의 창에 여러 탭 닫기 ("다른 탭 닫기"를)
  • 전체 창을 닫으면 창
  • 에 하나의 탭 닫기 어쨌든

탭없이 창을 닫으면 탭

  • 의, 여기 내 구현입니다. 당신은 debounce from here가 필요합니다

    // Stands for TabbedWindowCloseDebouncer 
    class TWCDebouncer { 
        private static var Debouncers = [NSWindowTabGroup:() -> Void]() 
        private static var TabStartCounts = [NSWindowTabGroup: Int]() 
        private static var Windows = [NSWindowTabGroup: [NSWindow]]() 
        private static var LastTabGroup: NSWindowTabGroup? 
    
        func handleClose(window: NSWindow) { 
         // This handles a window without tabs. 
         // Check presence in Debouncers, otherwise it will also catch the last 
         // window of a tabbed window closing. 
         if window.tabbedWindows == nil && TWCDebouncer.Debouncers[window.tabGroup!] == nil { 
          // You can consider this to be the same as closing a whole window. 
          return 
         } 
    
         // This could probably lead to problems. 
         TWCDebouncer.LastTabGroup = window.tabGroup 
    
         // Store the initial tab count. 
         if TWCDebouncer.TabStartCounts[TWCDebouncer.LastTabGroup!] == nil { 
          TWCDebouncer.TabStartCounts[TWCDebouncer.LastTabGroup!] = window.tabbedWindows?.count 
         } 
    
         // Initialize the list of windows closing. 
         if TWCDebouncer.Windows[TWCDebouncer.LastTabGroup!] == nil { 
          TWCDebouncer.Windows[TWCDebouncer.LastTabGroup!] = [] 
         } 
    
         // Set up the debounced function. 
         if TWCDebouncer.Debouncers[TWCDebouncer.LastTabGroup!] == nil { 
          TWCDebouncer.Debouncers[TWCDebouncer.LastTabGroup!] = debounce(delay: .milliseconds(20), action: { 
           let countAfter = TWCDebouncer.LastTabGroup?.windows.count ?? 0 
    
           print(TWCDebouncer.Windows[TWCDebouncer.LastTabGroup!]) 
           if countAfter == 0 { 
            // All windows were closed. 
           } else { 
            // One or more windows were closed in the tab group 
           } 
    
           // Reset. 
           TWCDebouncer.Debouncers[TWCDebouncer.LastTabGroup!] = nil 
           TWCDebouncer.TabStartCounts[TWCDebouncer.LastTabGroup!] = nil 
           TWCDebouncer.Windows[TWCDebouncer.LastTabGroup!] = nil 
           TWCDebouncer.LastTabGroup = nil 
          }) 
         } 
    
         // Store the window. 
         TWCDebouncer.Windows[TWCDebouncer.LastTabGroup!]?.append(window) 
         // Call the debounced function. 
         TWCDebouncer.Debouncers[window.tabGroup!]!() 
        } 
    } 
    

    를 사용하려면, 내 WindowController에 넣어했습니다. 나는 이 호출 될 때까지 에 넣어야했다. window.tabbedWindowsnil이다.

    class WindowController: NSWindowController { 
        var closeDebouncer = TWCDebouncer() 
    
        func windowShouldClose(_ sender: NSWindow) -> Bool { 
         self.closeDebouncer.handleClose(window: self.window!) 
         return true 
        } 
    }