Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listen for window resize event in Swift / Objective-C

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.

like image 417
Wojciech Danilo Avatar asked Dec 09 '15 01:12

Wojciech Danilo


1 Answers

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.

like image 149
Peter Hornsby Avatar answered Sep 18 '22 19:09

Peter Hornsby