Skip to content

Commit

Permalink
Fix support for mac
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrkstr committed Apr 24, 2022
1 parent 90a0345 commit 69b6079
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
3 changes: 1 addition & 2 deletions frameit/lib/frameit/mac_editor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ def prepare_image
end

def put_device_into_background(background)
self.top_space_above_device = offset['titleHeight'] # needed for centering the title

@image = background.composite(image, "png") do |c|
c.compose("Over")
Expand All @@ -29,7 +28,7 @@ def is_complex_framing_mode?
end

def generate_background
MiniMagick::Image.open(fetch_config['background']) # no resizing on the Mac
MiniMagick::Image.open(@config['background']) # no resizing on the Mac
end
end
end
2 changes: 1 addition & 1 deletion frameit/lib/frameit/screenshot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def mini?
end

def mac?
device_name == 'MacBook'
device_name == 'Apple MacBook'
end

# The name of the orientation of a screenshot. Used to find the correct template
Expand Down
44 changes: 28 additions & 16 deletions snapshot/lib/assets/SnapshotHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ func snapshot(_ name: String, waitForLoadingIndicator: Bool) {
/// - Parameters:
/// - name: The name of the snapshot
/// - timeout: Amount of seconds to wait until the network loading indicator disappears. Pass `0` if you don't want to wait.
func snapshot(_ name: String, timeWaitingForIdle timeout: TimeInterval = 20) {
Snapshot.snapshot(name, timeWaitingForIdle: timeout)
/// - screenshotProvider: The provider to screenshot. Otherwise `XCUIScreen.main` is screenshotted. This is useful when taking screenshots for macOS and you'd prefer to screenshot the window and not the screen.
func snapshot(_ name: String, timeWaitingForIdle timeout: TimeInterval = 20, screenshotProvider: XCUIScreenshotProviding = XCUIScreen.main) {
Snapshot.snapshot(name, timeWaitingForIdle: timeout, screenshotProvider: screenshotProvider)

}

enum SnapshotError: Error, CustomDebugStringConvertible {
Expand Down Expand Up @@ -139,7 +141,7 @@ open class Snapshot: NSObject {
}
}

open class func snapshot(_ name: String, timeWaitingForIdle timeout: TimeInterval = 20) {
open class func snapshot(_ name: String, timeWaitingForIdle timeout: TimeInterval = 20, screenshotProvider: XCUIScreenshotProviding = XCUIScreen.main) {
if timeout > 0 {
waitForLoadingIndicatorToDisappear(within: timeout)
}
Expand All @@ -149,22 +151,30 @@ open class Snapshot: NSObject {
if Snapshot.waitForAnimations {
sleep(1) // Waiting for the animation to be finished (kind of)
}

guard self.app != nil else {
NSLog("XCUIApplication is not set. Please call setupSnapshot(app) before snapshot().")
return
}

let screenshot = screenshotProvider.screenshot()

#if os(OSX)
guard let app = self.app else {
NSLog("XCUIApplication is not set. Please call setupSnapshot(app) before snapshot().")
return
}

app.typeKey(XCUIKeyboardKeySecondaryFn, modifierFlags: [])

let image = screenshot.pngRepresentation
guard let screenshotsDir = screenshotsDirectory else { return }
let path = screenshotsDir.appendingPathComponent("MacBook-\(name).png", isDirectory: false)

do {
try FileManager.default.createDirectory(at: screenshotsDir, withIntermediateDirectories: true, attributes: nil)
try image.write(to: path, options: .atomic)
} catch let error {
NSLog("Problem writing screenshot: \(name) to \(path)")
NSLog(error.localizedDescription)
}

#else

guard self.app != nil else {
NSLog("XCUIApplication is not set. Please call setupSnapshot(app) before snapshot().")
return
}

let screenshot = XCUIScreen.main.screenshot()
#if os(iOS) && !targetEnvironment(macCatalyst)
let image = XCUIDevice.shared.orientation.isLandscape ? fixLandscapeOrientation(image: screenshot.image) : screenshot.image
#else
Expand All @@ -191,7 +201,8 @@ open class Snapshot: NSObject {
}
#endif
}


#if os(iOS) || os(watchOS) || os(tvOS)
class func fixLandscapeOrientation(image: UIImage) -> UIImage {
#if os(watchOS)
return image
Expand All @@ -208,6 +219,7 @@ open class Snapshot: NSObject {
}
#endif
}
#endif

class func waitForLoadingIndicatorToDisappear(within timeout: TimeInterval) {
#if os(tvOS)
Expand Down

0 comments on commit 69b6079

Please sign in to comment.