Skip to main content
No need to say thanks
Source Link
Marc-Andre
  • 6.7k
  • 5
  • 38
  • 65

The code bellow seems to do the same with less code. I found the original solution to be a bit cryptic and language specific. The provided code bellow is also easier to modulate, one could imagine getting the backMostWinOfType with a single char change in the sort algorithm. One could argue that running a sort algorithm is superfluous to this task, but it's unlikely to hamper performance as you never have that many windows anyway. Thoughts and corrections are more than welcome. Thanks.

/**
 * Returns the front most window in NSApp of a spedific class or protocol type
 */
class func frontMostWinOfType<T:NSWindow>(type:T.Type)-> T?{
    var windows:Array<T> = []
    for window : NSWindow in NSApp.windows { if(window as? T != nil) {windows.append(window as! T)}}
    windows.sortInPlace { (a, b) -> Bool in return a.orderedIndex > b.orderedIndex}
    return windows[0]
}

The code bellow seems to do the same with less code. I found the original solution to be a bit cryptic and language specific. The provided code bellow is also easier to modulate, one could imagine getting the backMostWinOfType with a single char change in the sort algorithm. One could argue that running a sort algorithm is superfluous to this task, but it's unlikely to hamper performance as you never have that many windows anyway. Thoughts and corrections are more than welcome. Thanks.

/**
 * Returns the front most window in NSApp of a spedific class or protocol type
 */
class func frontMostWinOfType<T:NSWindow>(type:T.Type)-> T?{
    var windows:Array<T> = []
    for window : NSWindow in NSApp.windows { if(window as? T != nil) {windows.append(window as! T)}}
    windows.sortInPlace { (a, b) -> Bool in return a.orderedIndex > b.orderedIndex}
    return windows[0]
}

The code bellow seems to do the same with less code. I found the original solution to be a bit cryptic and language specific. The provided code bellow is also easier to modulate, one could imagine getting the backMostWinOfType with a single char change in the sort algorithm. One could argue that running a sort algorithm is superfluous to this task, but it's unlikely to hamper performance as you never have that many windows anyway. Thoughts and corrections are more than welcome.

/**
 * Returns the front most window in NSApp of a spedific class or protocol type
 */
class func frontMostWinOfType<T:NSWindow>(type:T.Type)-> T?{
    var windows:Array<T> = []
    for window : NSWindow in NSApp.windows { if(window as? T != nil) {windows.append(window as! T)}}
    windows.sortInPlace { (a, b) -> Bool in return a.orderedIndex > b.orderedIndex}
    return windows[0]
}
added 504 characters in body
Source Link

I might be missing something but thisThe code bellow seems to do the same with less code. I found the original solution to be a bit cryptic and language specific. The provided code bellow is also easier to modulate, no? :one could imagine getting the backMostWinOfType with a single char change in the sort algorithm. One could argue that running a sort algorithm is superfluous to this task, but it's unlikely to hamper performance as you never have that many windows anyway. Thoughts and corrections are more than welcome. Thanks.

/**
 * Returns the firstfront most window in NSApp of a spedific class or protocol type
 * NOTE: there is also window.isMemberOfClass which could work
 */
class func firstWindow<T>frontMostWinOfType<T:NSWindow>(type:T.Type)-> T? {
    var windows:Array<T> = []
    for window : NSWindow in NSApp.windows { if(window as? T != nil) {return windows.append(window as?! T)}}
    windows.sortInPlace { (a, b) -> Bool in return a.orderedIndex > b.orderedIndex}
    return nilwindows[0]
}

I might be missing something but this seems to do the same with less code, no? :

/**
 * Returns the first window in NSApp of a spedific class or protocol type
 * NOTE: there is also window.isMemberOfClass which could work
 */
class func firstWindow<T>(type:T.Type)-> T? {
    for window : NSWindow in NSApp.windows { if(window as? T != nil) {return window as? T}}
    return nil
}

The code bellow seems to do the same with less code. I found the original solution to be a bit cryptic and language specific. The provided code bellow is also easier to modulate, one could imagine getting the backMostWinOfType with a single char change in the sort algorithm. One could argue that running a sort algorithm is superfluous to this task, but it's unlikely to hamper performance as you never have that many windows anyway. Thoughts and corrections are more than welcome. Thanks.

/**
 * Returns the front most window in NSApp of a spedific class or protocol type
 */
class func frontMostWinOfType<T:NSWindow>(type:T.Type)-> T?{
    var windows:Array<T> = []
    for window : NSWindow in NSApp.windows { if(window as? T != nil) {windows.append(window as! T)}}
    windows.sortInPlace { (a, b) -> Bool in return a.orderedIndex > b.orderedIndex}
    return windows[0]
}
added 47 characters in body
Source Link

Here is my Utility code forI might be missing something but this seems to do the same with less code, no? :

/**
 * Returns the first window in NSApp of a spedific class or protocol type
 * NOTE: there is also window.isMemberOfClass which could work
 */
class func firstWindow<T>(type:T.Type)-> T? {
    for window : NSWindow in NSApp.windows { if(window as? T != nil) {return window as? T}}
    return nil
}

Here is my Utility code for this :

/**
 * Returns the first window in NSApp of a spedific class or protocol type
 * NOTE: there is also window.isMemberOfClass which could work
 */
class func firstWindow<T>(type:T.Type)-> T? {
    for window : NSWindow in NSApp.windows { if(window as? T != nil) {return window as? T}}
    return nil
}

I might be missing something but this seems to do the same with less code, no? :

/**
 * Returns the first window in NSApp of a spedific class or protocol type
 * NOTE: there is also window.isMemberOfClass which could work
 */
class func firstWindow<T>(type:T.Type)-> T? {
    for window : NSWindow in NSApp.windows { if(window as? T != nil) {return window as? T}}
    return nil
}
Source Link
Loading