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, config, platform_command) ⇒ Screenshot

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



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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

Instance Attribute Details

#colorObject

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



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

def color
  @color
end

#deviceObject

device detected according to resolution, priority and settings



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

def device
  @device
end

#pathObject

path to the screenshot



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

def path
  @path
end

#sizeObject

size in px array of 2 elements: height and width



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

def size
  @size
end

Instance Method Details

#default_colorObject



54
55
56
# File 'frameit/lib/frameit/screenshot.rb', line 54

def default_color
  @device.default_color
end

#deliver_screen_idObject



58
59
60
# File 'frameit/lib/frameit/screenshot.rb', line 58

def deliver_screen_id
  @device.deliver_screen_id
end

#device_nameObject

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



49
50
51
52
# File 'frameit/lib/frameit/screenshot.rb', line 49

def device_name
  @device.formatted_name
  # rubocop:enable Require/MissingRequireStatement
end

#frame_orientationObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# 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?Boolean

Returns:



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

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

#landscape_left?Boolean

Returns:



103
104
105
# File 'frameit/lib/frameit/screenshot.rb', line 103

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

#landscape_right?Boolean

Returns:



107
108
109
# File 'frameit/lib/frameit/screenshot.rb', line 107

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

#languageObject



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

def language
  @language ||= Pathname.new(path).parent.basename.to_s
end

#mac?Boolean

Returns:



72
73
74
# File 'frameit/lib/frameit/screenshot.rb', line 72

def mac?
  device_name == 'MacBook'
end

#mini?Boolean

Super old devices (iPhone 4)

Returns:



68
69
70
# File 'frameit/lib/frameit/screenshot.rb', line 68

def mini?
  !device.density_ppi.nil? && device.density_ppi < 300
end

#orientation_nameObject

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



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

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:



121
122
123
124
# 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_pathObject



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

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

#portrait?Boolean

Returns:



99
100
101
# File 'frameit/lib/frameit/screenshot.rb', line 99

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

#to_sObject



130
131
132
# File 'frameit/lib/frameit/screenshot.rb', line 130

def to_s
  self.path
end

#triple_density?Boolean

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

Returns:



63
64
65
# File 'frameit/lib/frameit/screenshot.rb', line 63

def triple_density?
  !device.density_ppi.nil? && device.density_ppi > 400
end