Class: Frameit::Screenshot

Inherits:
Object
  • Object
show all
Defined in:
frameit/lib/frameit/screenshot.rb

Overview

Represents one screenshot

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, color) ⇒ Screenshot

path: Path to screenshot color: Color to use for the frame



18
19
20
21
22
23
24
25
# File 'frameit/lib/frameit/screenshot.rb', line 18

def initialize(path, color)
  UI.user_error!("Couldn't find file at path '#{path}'") unless File.exist?(path)
  @color = color
  @path = path
  @size = FastImage.size(path)

  @screen_size = ENV["FRAMEIT_FORCE_DEVICE_TYPE"] || Deliver::AppScreenshot.calculate_screen_size(path)
end

Instance Attribute Details

#colorObject

the color to use for the frame (from Frameit::Color)



14
15
16
# File 'frameit/lib/frameit/screenshot.rb', line 14

def color
  @color
end

#pathObject

path to the screenshot



11
12
13
# File 'frameit/lib/frameit/screenshot.rb', line 11

def path
  @path
end

#screen_sizeObject

deliver screen size type, is unique per device type, used in device_name



13
14
15
# File 'frameit/lib/frameit/screenshot.rb', line 13

def screen_size
  @screen_size
end

#sizeObject

size in px array of 2 elements: height and width



12
13
14
# File 'frameit/lib/frameit/screenshot.rb', line 12

def size
  @size
end

Instance Method Details

#device_nameObject

Device name for a given screen size. Used to use the correct template



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
61
62
# File 'frameit/lib/frameit/screenshot.rb', line 28

def device_name
  # rubocop:disable Require/MissingRequireStatement
  sizes = Deliver::AppScreenshot::ScreenSize
  case @screen_size
  when sizes::IOS_65
    return 'iPhone XS Max'
  when sizes::IOS_61
    return 'iPhone XR'
  when sizes::IOS_58
    return Frameit.config[:use_legacy_iphonex] ? 'iPhone X' : 'iPhone XS'
  when sizes::IOS_55
    return Frameit.config[:use_legacy_iphone6s] ? 'iPhone 6s Plus' : 'iPhone 7 Plus'
  when sizes::IOS_47
    return Frameit.config[:use_legacy_iphone6s] ? 'iPhone 6s' : 'iPhone 7'
  when sizes::IOS_40
    return Frameit.config[:use_legacy_iphone5s] ? 'iPhone 5s' : 'iPhone SE'
  when sizes::IOS_35
    return 'iPhone 4'
  when sizes::IOS_IPAD
    return 'iPad Air 2'
  when sizes::IOS_IPAD_10_5
    return 'iPad Pro (10.5-inch)'
  when sizes::IOS_IPAD_11
    return 'iPad Pro (11-inch)'
  when sizes::IOS_IPAD_PRO
    return 'iPad Pro'
  when sizes::IOS_IPAD_PRO_12_9
    return 'iPad Pro (12.9-inch) (3rd generation)'
  when sizes::MAC
    return 'MacBook'
  else
    UI.error("Unknown device type for size #{@screen_size} for path '#{path}'")
  end
  # rubocop:enable Require/MissingRequireStatement
end

#frame_orientationObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'frameit/lib/frameit/screenshot.rb', line 93

def frame_orientation
  filename = File.basename(self.path, ".*")
  block = Frameit.config[:force_orientation_block]

  unless block.nil?
    orientation = block.call(filename)
    valid = [:landscape_left, :landscape_right, :portrait, nil]
    UI.user_error("orientation_block must return #{valid[0..-2].join(', ')} or nil") unless valid.include?(orientation)
  end

  puts("Forced orientation: #{orientation}") unless orientation.nil?

  return orientation unless orientation.nil?
  return :portrait if self.orientation_name == Orientation::PORTRAIT
  return :landscape_right # Default landscape orientation
end

#landscape?Boolean

Returns:



122
123
124
# File 'frameit/lib/frameit/screenshot.rb', line 122

def landscape?
  return self.landscape_left? || self.landscape_right
end

#landscape_left?Boolean

Returns:



114
115
116
# File 'frameit/lib/frameit/screenshot.rb', line 114

def landscape_left?
  return (frame_orientation == :landscape_left)
end

#landscape_right?Boolean

Returns:



118
119
120
# File 'frameit/lib/frameit/screenshot.rb', line 118

def landscape_right?
  return (frame_orientation == :landscape_right)
end

#mac?Boolean

Returns:



83
84
85
# File 'frameit/lib/frameit/screenshot.rb', line 83

def mac?
  return device_name == 'MacBook'
end

#mini?Boolean

Super old devices (iPhone 4)

Returns:



79
80
81
# File 'frameit/lib/frameit/screenshot.rb', line 79

def mini?
  (screen_size == Deliver::AppScreenshot::ScreenSize::IOS_35)
end

#orientation_nameObject

The name of the orientation of a screenshot. Used to find the correct template



88
89
90
91
# File 'frameit/lib/frameit/screenshot.rb', line 88

def orientation_name
  return Orientation::PORTRAIT if size[0] < size[1]
  return Orientation::LANDSCAPE
end

#outdated?Boolean

If the framed screenshot was generated before the screenshot file, then we must be outdated.

Returns:



132
133
134
135
# File 'frameit/lib/frameit/screenshot.rb', line 132

def outdated?
  return true unless File.exist?(output_path)
  return File.mtime(path) > File.mtime(output_path)
end

#output_pathObject



126
127
128
# File 'frameit/lib/frameit/screenshot.rb', line 126

def output_path
  path.gsub('.png', '_framed.png').gsub('.PNG', '_framed.png')
end

#portrait?Boolean

Returns:



110
111
112
# File 'frameit/lib/frameit/screenshot.rb', line 110

def portrait?
  return (frame_orientation == :portrait)
end

#to_sObject



137
138
139
# File 'frameit/lib/frameit/screenshot.rb', line 137

def to_s
  self.path
end

#triple_density?Boolean

Is the device a 3x device? (e.g. iPhone 6 Plus, iPhone X)

Returns:



74
75
76
# File 'frameit/lib/frameit/screenshot.rb', line 74

def triple_density?
  (screen_size == Deliver::AppScreenshot::ScreenSize::IOS_55 || screen_size == Deliver::AppScreenshot::ScreenSize::IOS_58 || screen_size == Deliver::AppScreenshot::ScreenSize::IOS_65)
end