Swift for Xojo™ Developers (cont'd) : SCREEN

These methods/class replicate the Xojo™ SCREEN methods/class.

  • screenCount : Returns the number of screens (monitors) attached to the host computer
  • screen(x) : Returns the information about a specific monitor. If you provide a screen number out of bounds, a fatal exception will occur

values for screen(x) (all of which are read-only)

  • availableHeight [Int]
  • availableLeft [Int]
  • availableTop [Int]
  • availableWidth [Int]
  • left [Int]
  • top [Int]
  • height [Int]
  • width [Int]
  • depth [Int]
  • scaleFactor [CGFloat]
//
//  ds$SCREEN.swift
//
//  This replicates the Xojo™ SCREEN methods
//
//  Created by David Sisemore on 24-Feb-2022
//
import Cocoa
import Foundation
/// Used to determine the number of screens connected to the user’s computer.
var screenCount : Int { return NSScreen.screens.count  }

public func screen(_ index:Int) -> ds$SCREEN {
  if index >= 0 && index < screenCount { return  ds$SCREEN(screen : index) }
  fatalError("ERROR: Screen(index:\(index)) is out of range.")
}

public class ds$SCREEN : NSObject {
  private var thisScreen  : NSScreen?
  private var mainScreen  : NSScreen?

  init(screen: Int) {
    super.init()
    thisScreen = NSScreen.screens[screen]
    mainScreen = NSScreen.screens[0]
  }
  public var availableHeight : Int     { return Int(thisScreen!.visibleFrame.height) }
  public var availableLeft   : Int     { return Int(thisScreen!.visibleFrame.minX) }
  public var availableTop    : Int     { return Int(mainScreen!.frame.height-(thisScreen!.visibleFrame.minY+thisScreen!.visibleFrame.height)) }
  public var availableWidth  : Int     { return Int(thisScreen!.visibleFrame.width) }
  public var left            : Int     { return Int(thisScreen!.frame.minX) }
  public var top             : Int     { return Int(mainScreen!.frame.height-(thisScreen!.frame.minY+thisScreen!.frame.height)) }
  public var height          : Int     { return Int(thisScreen!.frame.height) }
  public var width           : Int     { return Int(thisScreen!.frame.width) }
  public var depth           : Int     { return thisScreen!.depth.bitsPerPixel }
  public var scaleFactor     : CGFloat { return thisScreen!.backingScaleFactor }

  public func show() {
    print("  Availble Left : \(availableLeft)")
    print("   Availble Top : \(availableTop)")
    print(" Availble Width : \(availableWidth)")
    print("Availble Height : \(availableHeight)")
    print("           Left : \(left)")
    print("            Top : \(top)")
    print("          Width : \(width)")
    print("         Height : \(height)")
    print("          Depth : \(depth)")
    print("    Scalefactor : \(scaleFactor)")
  }
}

This is part of my dsFRAMEWORK project