Class: Motion::LaunchImages

Inherits:
Object
  • Object
show all
Defined in:
lib/motion/devices.rb,
lib/motion/launch_images.rb

Class Method Summary collapse

Class Method Details

.capture!(orientation) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/motion/launch_images.rb', line 28

def self.capture!(orientation)
  # sanity check the device values
  screen = UIScreen.mainScreen
  height = screen.bounds.size.height.to_i
  scale = screen.respondsToSelector('displayLinkWithTarget:selector:') ? screen.scale.to_i : 1
  device = get_device(device_name)
  expected_height = (orientation == :portrait ? device.height : device.width) / device.scale

  if expected_height != height || device.scale != scale
    puts "In #{orientation.to_s}, height was expected to be #{expected_height} but was #{height} and/or scale was expected to be #{device.scale} but was #{scale}"
    exit
  end

  if scale > 1
    UIGraphicsBeginImageContextWithOptions(window.bounds.size, false, screen.scale)
  else # TODO not sure if this is necessary anymore?
    UIGraphicsBeginImageContext(window.bounds.size)
  end

  filename = screenshot_path + device.filename(orientation)
  window.layer.renderInContext(UIGraphicsGetCurrentContext())
  image = UIGraphicsGetImageFromCurrentImageContext()
  UIGraphicsEndImageContext()
  data = UIImagePNGRepresentation(image)
  data.writeToFile(filename, atomically:true)
  puts "Wrote to #{filename}"

  if orientation == :portrait && device.landscape
    take!(:landscape)
  else
    exit
  end
end

.device_nameObject



7
8
9
# File 'lib/motion/launch_images.rb', line 7

def self.device_name
  NSBundle.mainBundle.objectForInfoDictionaryKey('device_name')
end

.get_device(name) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/motion/devices.rb', line 54

def self.get_device(name)
  if iphones.has_key?(name)
    iphones[name]
  elsif ipads.has_key?(name)
    ipads[name]
  else
    puts "Unable to find device #{name}"
    exit
  end
end

.ipadsObject



46
47
48
49
50
51
52
# File 'lib/motion/devices.rb', line 46

def self.ipads
  {
    'iPad Pro (9.7-inch)'  => Device.new(1536, 2048, 2, true, true),
    'iPad Pro (10.5-inch)' => Device.new(1668, 2224, 2, true, false),
    'iPad Pro (12.9-inch)' => Device.new(2048, 2732, 2, true, false),
  }
end

.iphonesObject



37
38
39
40
41
42
43
44
# File 'lib/motion/devices.rb', line 37

def self.iphones
  {
    'iPhone SE'     => Device.new(640,  1136, 2, false, false), # also iPhone 5s
    'iPhone 8'      => Device.new(750,  1334, 2, false, false), # also iPhone 6/7
    'iPhone 8 Plus' => Device.new(1242, 2208, 3, false, false), # also iPhone 6/7 Plus
    'iPhone X'      => Device.new(1125, 2436, 3, false, false),
  }
end

.needs_rotation?(orientation) ⇒ Boolean



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/motion/launch_images.rb', line 85

def self.needs_rotation?(orientation)
  status = UIApplication.sharedApplication.statusBarOrientation
  if orientation == :portrait && status == UIInterfaceOrientationPortrait
    false
  elsif orientation == :landscape && (status == UIInterfaceOrientationLandscapeLeft ||
                                      status == UIInterfaceOrientationLandscapeRight)
    false
  else
    true
  end
end

.rotate(to) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/motion/launch_images.rb', line 75

def self.rotate(to)
  if to == :portrait
    value = NSNumber.numberWithInt(UIInterfaceOrientationPortrait)
  else
    value = NSNumber.numberWithInt(UIInterfaceOrientationLandscapeLeft)
  end

  UIDevice.currentDevice.setValue(value, forKey:'orientation')
end

.screenshot_pathObject



3
4
5
# File 'lib/motion/launch_images.rb', line 3

def self.screenshot_path
  NSBundle.mainBundle.objectForInfoDictionaryKey('screenshot_path')
end

.take!(orientation = :portrait) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/motion/launch_images.rb', line 15

def self.take!(orientation = :portrait)
  return unless taking?

  Dispatch::Queue.main.async do
    if needs_rotation?(orientation)
      rotate(orientation)
      sleep 1.0
    end

    Dispatch::Queue.main.async { capture!(orientation) }
  end
end

.taking?Boolean



11
12
13
# File 'lib/motion/launch_images.rb', line 11

def self.taking?
  screenshot_path != nil
end

.windowObject

ported from BubbleWrap to reduce dependencies



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/motion/launch_images.rb', line 63

def self.window
  normal_windows = UIApplication.sharedApplication.windows.select { |w|
    w.windowLevel == UIWindowLevelNormal
  }

  key_window = normal_windows.select { |w|
    w == UIApplication.sharedApplication.keyWindow
  }.first

  key_window || normal_windows.first
end