class Frameit::Screenshot

Represents one screenshot

Attributes

color[RW]
device[RW]
path[RW]
size[RW]

Public Class Methods

new(path, color, config, platform_command) click to toggle source

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

# File frameit/lib/frameit/screenshot.rb, line 17
def initialize(path, color, config, platform_command)
  UI.user_error!("Couldn't find file at path '#{path}'") unless File.exist?(path)
  @color = color
  @path = path
  @size = FastImage.size(path)

  # There are three ways how we can get settings to Frameit:
  # - options.rb
  #   - gets parameters via CLI (e. g. fastlane run frameit use_platform:"android") or fastfile (Fastlane's global
  #     settings for a given project)
  #   - see Parameters in the doc
  #   - contains default values and validates values
  #   - accessed via Frameit.config[:key]
  #   - default value is either general platform from fastfile or IOS if run directly
  #   - lowest priority
  # - commands_generator.rb
  #   - commands entered directly to CLI (e. g. fastlane frameit android)
  #   - they are passed via constructors to other classes
  #   - higher priority than options.rb (user may enter a command to override fastfile's global setting)
  # - config_parser.rb
  #   - gets key / values from Framefile.json
  #   - see Advanced usage in the doc
  #   - both default and specific values can be entered in the file (filtered by file name)
  #   - accessed via ConfigParser.fetch_value(screenshot.path)[key] (the ConfigParser's instance is passed
  #     to Screenshot's constructor as config, i.e. we call config[key])
  #   - should have the highest priority, because user might set a specific value for a specific screenshot which
  #     should override CLI parameters and fastfile global setting
  platform = config['use_platform'] || platform_command || Frameit.config[:use_platform]
  @device = Device.find_device_by_id_or_name(config['force_device_type'] || Frameit.config[:force_device_type]) || Device.detect_device(path, platform)
end

Public Instance Methods

default_color() click to toggle source
# File frameit/lib/frameit/screenshot.rb, line 54
def default_color
  @device.default_color
end
deliver_screen_id() click to toggle source
# File frameit/lib/frameit/screenshot.rb, line 58
def deliver_screen_id
  @device.deliver_screen_id
end
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 49
def device_name
  @device.formatted_name
  # rubocop:enable Require/MissingRequireStatement
end
frame_orientation() click to toggle source
# File frameit/lib/frameit/screenshot.rb, line 82
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 111
def landscape?
  return self.landscape_left? || self.landscape_right
end
landscape_left?() click to toggle source
# File frameit/lib/frameit/screenshot.rb, line 103
def landscape_left?
  return (frame_orientation == :landscape_left)
end
landscape_right?() click to toggle source
# File frameit/lib/frameit/screenshot.rb, line 107
def landscape_right?
  return (frame_orientation == :landscape_right)
end
language() click to toggle source
# File frameit/lib/frameit/screenshot.rb, line 126
def language
  @language ||= Pathname.new(path).parent.basename.to_s
end
mac?() click to toggle source
# File frameit/lib/frameit/screenshot.rb, line 72
def mac?
  device_name == 'MacBook'
end
mini?() click to toggle source

Super old devices (iPhone 4)

# File frameit/lib/frameit/screenshot.rb, line 68
def mini?
  !device.density_ppi.nil? && device.density_ppi < 300
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 77
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 121
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 115
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 99
def portrait?
  return (frame_orientation == :portrait)
end
to_s() click to toggle source
# File frameit/lib/frameit/screenshot.rb, line 130
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 63
def triple_density?
  !device.density_ppi.nil? && device.density_ppi > 400
end