class Frameit::Screenshot

Represents one screenshot

Attributes

color[RW]
path[RW]
screen_size[RW]
size[RW]

Public Class Methods

new(path, color) click to toggle source

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

# 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

Public Instance Methods

device_name() click to toggle source

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

# 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_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_orientation() click to toggle source
# File frameit/lib/frameit/screenshot.rb, line 89
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?() click to toggle source
# File frameit/lib/frameit/screenshot.rb, line 118
def landscape?
  return self.landscape_left? || self.landscape_right
end
landscape_left?() click to toggle source
# File frameit/lib/frameit/screenshot.rb, line 110
def landscape_left?
  return (frame_orientation == :landscape_left)
end
landscape_right?() click to toggle source
# File frameit/lib/frameit/screenshot.rb, line 114
def landscape_right?
  return (frame_orientation == :landscape_right)
end
mac?() click to toggle source
# File frameit/lib/frameit/screenshot.rb, line 79
def mac?
  return device_name == 'MacBook'
end
mini?() click to toggle source

Super old devices (iPhone 4)

# File frameit/lib/frameit/screenshot.rb, line 75
def mini?
  (screen_size == Deliver::AppScreenshot::ScreenSize::IOS_35)
end
orientation_name() click to toggle source

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

# File frameit/lib/frameit/screenshot.rb, line 84
def orientation_name
  return Orientation::PORTRAIT if size[0] < size[1]
  return Orientation::LANDSCAPE
end
outdated?() click to toggle source

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

# File frameit/lib/frameit/screenshot.rb, line 128
def outdated?
  return true unless File.exist?(output_path)
  return File.mtime(path) > File.mtime(output_path)
end
output_path() click to toggle source
# File frameit/lib/frameit/screenshot.rb, line 122
def output_path
  path.gsub('.png', '_framed.png').gsub('.PNG', '_framed.png')
end
portrait?() click to toggle source
# File frameit/lib/frameit/screenshot.rb, line 106
def portrait?
  return (frame_orientation == :portrait)
end
to_s() click to toggle source
# File frameit/lib/frameit/screenshot.rb, line 133
def to_s
  self.path
end
triple_density?() click to toggle source

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

# File frameit/lib/frameit/screenshot.rb, line 70
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