Is it possible to iterate over all open windows of any application in OSX and listen to their resize events in Swift? I want to create custom window manager that would move and resize events based on user behavior – so if user resizes a window, other windows get automatically re-arranged.
I'm Haskell, not a Swift developer, so I would be very thanful for any code snippet / example showing how to achieve this effect. (In fact I'll use it as a kind of Haskell -> Cocoa binding.)
Edit:
I would be very interested in seing the solution in Objective-C also, but Swift is more important for me here.
Is it possible to iterate over all open windows of any application in OSX and listen to their resize events in Swift?
It is possible to do this, a better way would be to have your custom window manager class implement the NSWindowDelegate protocol and set all window's delegate to your custom window manager. This will give you all the resize and move information you require.
I'm Haskell, not a Swift developer, so I would be very thanful for any code snippet / example showing how to achieve this effect.
Implementing protocol methods:
class CustomWindowManager: NSWindowController, NSWindowDelegate {
// Resize Methods
func windowWillResize(sender: NSWindow,toSize frameSize: NSSize) -> NSSize {
// Your code goes here
}
func windowDidResize(notification: NSNotification) {
// Your code goes here
}
}
You can then just chose which protocol methods you want to implement as all methods in the protocol are optional.
You will have to be able to identify which window is calling the delegate method and track any windows you move or resize due to reacting to the initial window change. See this Stack Overflow question for help with this.
For getting a list of all windows currently on screen see this Stack Overflow question.
Edit
For getting a list of all windows of your application currently on screen see this Stack Overflow question.
first step,you should add NSWindowDelegate and function windowWillResize,full code like this:
class MainWindowController: NSWindowController, NSWindowDelegate {
override var windowNibName: String? {
return "MainWindowController"
}
override func windowDidLoad() {
super.windowDidLoad()
}
func windowDidResize(notification: NSNotification) {
//listen the event
}
}
then you should set the delegate property,set MainWindowController.xib include file window's delegate property to File's Owner.
With Swift 3 the windowDidResize method should look like the following:
func windowDidResize(_ notification: Notification) {
// Listen to a window resize event
}
- create an NSWindowController Class
- Open your storyboard and find your window controller then set the Class as custom Class (identity inspect tab)
- create an NSWindowDelegate Class with windowWillResize method
- set the NSWindowDelegate instance as the window.delegate property of your NSWindowController instance
When user change window size,the windowWillResize method will be called.