Skip to content

Commit

Permalink
Add horizontal margin control (simplified) for spanned mode
Browse files Browse the repository at this point in the history
  • Loading branch information
glouel committed May 13, 2019
1 parent e8a3501 commit 9300380
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 12 deletions.
11 changes: 11 additions & 0 deletions Aerial/Source/Controllers/Preferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ final class Preferences {
case newViewingMode = "newViewingMode"
case newDisplayDict = "newDisplayDict"
case logMilliseconds = "logMilliseconds"
case horizontalMargin = "horizontalMargin"
}

enum NewDisplayMode: Int {
Expand Down Expand Up @@ -215,6 +216,7 @@ final class Preferences {
defaultValues[.newViewingMode] = NewViewingMode.independent
defaultValues[.newDisplayDict] = [String: Bool]()
defaultValues[.logMilliseconds] = false
defaultValues[.horizontalMargin] = 0

// Set today's date as default
let dateFormatter = DateFormatter()
Expand Down Expand Up @@ -251,6 +253,15 @@ final class Preferences {
}
}

var horizontalMargin: Double? {
get {
return optionalValue(forIdentifier: .horizontalMargin)
}
set {
setValue(forIdentifier: .horizontalMargin, value: newValue)
}
}

var newDisplayMode: Int? {
get {
return optionalValue(forIdentifier: .newDisplayMode)
Expand Down
14 changes: 14 additions & 0 deletions Aerial/Source/Controllers/PreferencesWindowController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ final class PreferencesWindowController: NSWindowController, NSOutlineViewDataSo
@IBOutlet var quitConfirmationPanel: NSPanel!

@IBOutlet var logMillisecondsButton: NSButton!
@IBOutlet var displayMarginBox: NSBox!
@IBOutlet var horizontalDisplayMarginTextfield: NSTextField!
var player: AVPlayer = AVPlayer()

var videos: [AerialVideo]?
Expand Down Expand Up @@ -421,6 +423,7 @@ final class PreferencesWindowController: NSWindowController, NSOutlineViewDataSo
if !preferences.allowSkips {
rightArrowKeyPlaysNextCheckbox.state = .off
}
horizontalDisplayMarginTextfield.doubleValue = preferences.horizontalMargin!

// Advanced panel
if preferences.debugMode {
Expand Down Expand Up @@ -886,6 +889,17 @@ final class PreferencesWindowController: NSWindowController, NSOutlineViewDataSo
debugLog("UI newViewingModeClick: \(sender.indexOfSelectedItem)")
preferences.newViewingMode = sender.indexOfSelectedItem
displayView.needsDisplay = true

if preferences.newViewingMode == Preferences.NewViewingMode.spanned.rawValue {
displayMarginBox.isHidden = false
} else {
displayMarginBox.isHidden = true
}
}

@IBAction func horizontalDisplayMarginChange(_ sender: NSTextField) {
debugLog("UI horizontalDisplayMarginChange \(sender.stringValue)")
preferences.horizontalMargin = sender.doubleValue
}

// MARK: - Text panel
Expand Down
38 changes: 34 additions & 4 deletions Aerial/Source/Models/DisplayDetection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ final class DisplayDetection: NSObject {
static let sharedInstance = DisplayDetection()

var screens = [Screen]()
var cmInPoints: CGFloat = 40
var maxLeftScreens: CGFloat = 0
var maxBelowScreens: CGFloat = 0

// MARK: - Lifecycle
override init() {
Expand Down Expand Up @@ -72,8 +75,13 @@ final class DisplayDetection: NSObject {
var rect = CGDisplayBounds(currentDisplay)
if isMain == 0 {
rect = convertTopLeftToBottomLeft(rect: rect)
} else {
// We calculate the equivalent of a centimeter in points on the main screen as a reference
let mmsize = CGDisplayScreenSize(currentDisplay)
let wide = CGDisplayPixelsWide(currentDisplay)
cmInPoints = CGFloat(wide) / CGFloat(mmsize.width) * 10
debugLog("1cm = \(cmInPoints) points")
}

screens.append(Screen(id: currentDisplay,
width: CGDisplayPixelsWide(currentDisplay),
height: CGDisplayPixelsHigh(currentDisplay),
Expand All @@ -97,6 +105,7 @@ final class DisplayDetection: NSObject {
for screen in screens {
debugLog("\(screen)")
}

debugLog("\(getGlobalScreenRect())")
debugLog("***Display Detection Done***")
}
Expand All @@ -106,12 +115,33 @@ final class DisplayDetection: NSObject {
func calculateZeroedOrigins() {
let orect = getGlobalScreenRect()

// First we check for the screen relative position and calculate how many screens we have horizontally
// TODO Vertical + H/V mix
for screen in screens {
screen.zeroedOrigin = CGPoint(x: screen.bottomLeftFrame.origin.x - orect.origin.x,
debugLog("src orig : \(screen.bottomLeftFrame.origin)")
var leftScreens: CGFloat = 0
// Very rough, horizontal spans only
for otherScreen in screens {
if otherScreen.bottomLeftFrame.origin.x != screen.bottomLeftFrame.origin.x &&
otherScreen.bottomLeftFrame.origin.x < screen.bottomLeftFrame.origin.x {
leftScreens += 1
}
}

if leftScreens > maxLeftScreens {
maxLeftScreens = leftScreens
}

screen.zeroedOrigin = CGPoint(x: screen.bottomLeftFrame.origin.x - orect.origin.x + (leftScreens * leftMargin()),
y: screen.bottomLeftFrame.origin.y - orect.origin.y)
}
}

func leftMargin() -> CGFloat {
let preferences = Preferences.sharedInstance
return cmInPoints * CGFloat(preferences.horizontalMargin!)
}

func findScreenWith(frame: CGRect) -> Screen? {
for screen in screens where frame == screen.bottomLeftFrame {
return screen
Expand Down Expand Up @@ -146,7 +176,7 @@ final class DisplayDetection: NSObject {
}
}

return CGRect(x: minX, y: minY, width: maxX-minX, height: maxY-minY)
return CGRect(x: minX, y: minY, width: maxX-minX+(maxLeftScreens*leftMargin()), height: maxY-minY)
}

func getZeroedActiveSpannedRect() -> CGRect {
Expand All @@ -172,7 +202,7 @@ final class DisplayDetection: NSObject {
let orect = getGlobalScreenRect()
minX -= orect.origin.x
minY -= orect.origin.y
return CGRect(x: minX, y: minY, width: width, height: height)
return CGRect(x: minX, y: minY, width: width+(maxLeftScreens*leftMargin()), height: height)
}

// NSScreen coordinates are with a bottom left origin, whereas CGDisplay
Expand Down
17 changes: 11 additions & 6 deletions Aerial/Source/Views/AerialView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ final class AerialView: ScreenSaverView {
playerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
}
playerLayer.autoresizingMask = [CAAutoresizingMask.layerWidthSizable, CAAutoresizingMask.layerHeightSizable]

// In case of span mode we need to compute the size of our layer
if preferences.newViewingMode == Preferences.NewViewingMode.spanned.rawValue && !isPreview {
let zRect = displayDetection.getZeroedActiveSpannedRect()
Expand All @@ -371,6 +371,7 @@ final class AerialView: ScreenSaverView {
y: zRect.origin.y - scr.zeroedOrigin.y,
width: zRect.width,
height: zRect.height)
debugLog("tRect : \(tRect)")
playerLayer.frame = tRect
//playerLayer.bounds = layer.bounds
} else {
Expand Down Expand Up @@ -583,7 +584,7 @@ final class AerialView: ScreenSaverView {
debugLog("\(self.description) streaming video (not fully available offline) : \(video.url)")
} else {
let localurl = URL(fileURLWithPath: VideoCache.cachePath(forVideo: video)!)
var localitem = AVPlayerItem(url: localurl)
let localitem = AVPlayerItem(url: localurl)
player.replaceCurrentItem(with: localitem)
debugLog("\(self.description) playing video (OFFLINE MODE) : \(localurl)")
}
Expand Down Expand Up @@ -631,10 +632,14 @@ final class AerialView: ScreenSaverView {

if preferences.allowSkips {
if event.keyCode == 124 {
//playNextVideo()
// We need to skip forward all our views
for view in AerialView.instanciatedViews {
view.playNextVideo()
// If we share, just call this on our main view
if AerialView.sharingPlayers {
playNextVideo()
} else {
// If we do independant playback we have to skip all views
for view in AerialView.instanciatedViews {
view.playNextVideo()
}
}
} else {
self.nextResponder!.keyDown(with: event)
Expand Down
Loading

0 comments on commit 9300380

Please sign in to comment.