Skip to content

Commit

Permalink
Merge pull request #876 from justMaku/mirrored
Browse files Browse the repository at this point in the history
"True" Mirrored Mode
  • Loading branch information
glouel committed Dec 6, 2019
2 parents d453a2b + 9293568 commit 59bf700
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Aerial/Source/Controllers/Preferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ final class Preferences {
}

enum NewViewingMode: Int {
case independent, mirrored, spanned
case independent, cloned, spanned, mirrored
}

enum BetaCheckFrequency: Int {
Expand Down
18 changes: 16 additions & 2 deletions Aerial/Source/Views/AerialView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,17 @@ final class AerialView: ScreenSaverView, CAAnimationDelegate {
// Mirrored viewing mode and Spanned viewing mode share the same player for sync & ressource saving
static var sharingPlayers: Bool {
let preferences = Preferences.sharedInstance
return (preferences.newViewingMode == Preferences.NewViewingMode.mirrored.rawValue) ||
(preferences.newViewingMode == Preferences.NewViewingMode.spanned.rawValue)

switch preferences.newViewingMode {
case
Preferences.NewViewingMode.cloned.rawValue,
Preferences.NewViewingMode.mirrored.rawValue,
Preferences.NewViewingMode.spanned.rawValue:

return true
default:
return false
}
}

static var sharedViews: [AerialView] = []
Expand Down Expand Up @@ -404,6 +413,11 @@ final class AerialView: ScreenSaverView, CAAnimationDelegate {
}
} else {
playerLayer.frame = layer.bounds

let index = AerialView.instanciatedViews.firstIndex(of: self) ?? 0
if index % 2 == 1 && preferences.newViewingMode == Preferences.NewViewingMode.mirrored.rawValue {
playerLayer.transform = CATransform3DMakeAffineTransform(CGAffineTransform(scaleX: -1, y: 1))
}
}
layer.addSublayer(playerLayer)

Expand Down
35 changes: 31 additions & 4 deletions Aerial/Source/Views/DisplayView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,27 @@ class DisplayPreview: NSObject {
}
}

extension NSImage {
func flipped(flipHorizontally: Bool = false, flipVertically: Bool = false) -> NSImage {
let flippedImage = NSImage(size: size)

flippedImage.lockFocus()

NSGraphicsContext.current?.imageInterpolation = .high

let transform = NSAffineTransform()
transform.translateX(by: flipHorizontally ? size.width : 0, yBy: flipVertically ? size.height : 0)
transform.scaleX(by: flipHorizontally ? -1 : 1, yBy: flipVertically ? -1 : 1)
transform.concat()

draw(at: .zero, from: NSRect(origin: .zero, size: size), operation: .sourceOver, fraction: 1)

flippedImage.unlockFocus()

return flippedImage
}
}

class DisplayView: NSView {
// We store our computed previews here
var displayPreviews = [DisplayPreview]()
Expand Down Expand Up @@ -115,13 +136,19 @@ class DisplayView: NSView {

let sInRect = sRect.insetBy(dx: 1, dy: 1)

if preferences.newViewingMode == Preferences.NewViewingMode.independent.rawValue ||
preferences.newViewingMode == Preferences.NewViewingMode.mirrored.rawValue {
func rawValue(for mode: Preferences.NewViewingMode) -> Int { return mode.rawValue }
let viewMode = preferences.newViewingMode

if viewMode == rawValue(for: .independent) || viewMode == rawValue(for: .cloned) || viewMode == rawValue(for: .mirrored) {
if displayDetection.isScreenActive(id: screen.id) {
let bundle = Bundle(for: PreferencesWindowController.self)
if let imagePath = bundle.path(forResource: "screen"+String(idx), ofType: "jpg") {
let image = NSImage(contentsOfFile: imagePath)
//image!.draw(in: sInRect)
var image = NSImage(contentsOfFile: imagePath)

if preferences.newViewingMode == Preferences.NewViewingMode.mirrored.rawValue && screen.id % 2 == 1 {
image = image?.flipped(flipHorizontally: true, flipVertically: false)
}

image!.draw(in: sInRect, from: calcScreenshotRect(src: sInRect), operation: NSCompositingOperation.copy, fraction: 1.0)
} else {
errorLog("\(#file) screenshot is missing!!!")
Expand Down
10 changes: 8 additions & 2 deletions Resources/PreferencesWindow.xib
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ is disabled
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<buttonCell key="cell" type="roundRect" bezelStyle="roundedRect" image="NSBookmarksTemplate" imagePosition="only" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="kGs-X6-tPb">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>

<font key="font" metaFont="system" size="12"/>
</buttonCell>
<connections>
Expand Down Expand Up @@ -577,7 +578,7 @@ is disabled
</view>
</tabViewItem>
<tabViewItem label="Displays" identifier="" id="P6g-Pf-AmY">
<view key="view" id="65A-Ae-cVB">
<view key="view" ambiguous="YES" id="65A-Ae-cVB">
<rect key="frame" x="10" y="33" width="614" height="381"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
Expand Down Expand Up @@ -660,10 +661,15 @@ is disabled
<menu key="menu" id="3Bi-za-9u6">
<items>
<menuItem title="Independent" state="on" id="MKb-uC-u8v"/>
<menuItem title="Mirrored" id="mW5-ZK-gX1"/>
<menuItem title="Cloned" id="mW5-ZK-gX1" userLabel="Cloned">
<modifierMask key="keyEquivalentModifierMask"/>
</menuItem>
<menuItem title="Spanned" id="1hP-Py-Qcq">
<modifierMask key="keyEquivalentModifierMask"/>
</menuItem>
<menuItem title="Mirrored" id="3ls-5j-yRy" userLabel="Mirrored">
<modifierMask key="keyEquivalentModifierMask"/>
</menuItem>
</items>
</menu>
</popUpButtonCell>
Expand Down

0 comments on commit 59bf700

Please sign in to comment.