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
# 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_58
    return 'iPhone X'
  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::MAC
    return 'MacBook'
  else
    UI.error("Unknown device type for size #{@screen_size} for path '#{path}'")
  end
  # rubocop:enable Require/MissingRequireStatement
end

#frame!Object

Add the device frame, this will also call the method that adds the background + title



121
122
123
124
125
126
127
# File 'frameit/lib/frameit/screenshot.rb', line 121

def frame!
  if self.mac?
    MacEditor.new.frame!(self)
  else
    Editor.new.frame!(self)
  end
end

#frame_orientationObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'frameit/lib/frameit/screenshot.rb', line 83

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:

  • (Boolean)


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

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

#landscape_left?Boolean

Returns:

  • (Boolean)


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

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

#landscape_right?Boolean

Returns:

  • (Boolean)


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

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

#mac?Boolean

Returns:

  • (Boolean)


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

def mac?
  return device_name == 'MacBook'
end

#mini?Boolean

Super old devices (iPhone 4)

Returns:

  • (Boolean)


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

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



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

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

#portrait?Boolean

Returns:

  • (Boolean)


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

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

#to_sObject



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

def to_s
  self.path
end

#triple_density?Boolean

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

Returns:

  • (Boolean)


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

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