Module: SugarCube::Repl

Defined in:
lib/ios/sugarcube-repl/repl.rb,
lib/osx/sugarcube-repl/repl.rb,
lib/cocoa/sugarcube-repl/repl.rb

Class Method Summary collapse

Class Method Details

.adjust(view = nil) ⇒ Object Also known as: a



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/cocoa/sugarcube-repl/repl.rb', line 9

def adjust(view=nil)
  return @sugarcube_view unless view

  if view.is_a? Fixnum
    @sugarcube_items ||= SugarCube::Repl::build_tree(window, :subviews)
    view = @sugarcube_items[view]
  end

  # a/adjust will return this object
  @sugarcube_view = view

  adjust_init(view)

  view
end

.adjust_init(view) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/ios/sugarcube-repl/repl.rb', line 17

def adjust_init(view)
  if view.is_a?(UIView)
    @sugarcube_restore = {
      frame: SugarCube::Repl.frame,
      shadow: SugarCube::Repl.shadow,
    }
  end
end


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ios/sugarcube-repl/repl.rb', line 60

def blink(color=nil)
  return unless check_sugarcube_view

  blinking_view = UIView.alloc.initWithFrame([[0,0], @sugarcube_view.frame.size])
  color = color.uicolor if color.respond_to?(:uicolor)
  blinking_view.backgroundColor = color
  blinking_view.alpha = 0
  if @sugarcube_view.window
    blinking_view.frame = @sugarcube_view.convertRect([[0, 0], @sugarcube_view.frame.size], toView: @sugarcube_view.window)
    @sugarcube_view.window.addSubview(blinking_view)
  else
    @sugarcube_view.addSubview(blinking_view)
  end

  duration = 0.2
  NSView.animateWithDuration(duration * 2, animations: ->{ blinking_view.alpha = 1 }, completion: ->(finished) do
    NSView.animateWithDuration(duration, animations: ->{ blinking_view.alpha = 0 }, completion: ->(finished) do
      NSView.animateWithDuration(duration, animations: ->{ blinking_view.alpha = 1 }, completion: ->(finished) do
        NSView.animateWithDuration(duration, animations: ->{ blinking_view.alpha = 0 }, completion: ->(finished) do
          blinking_view.removeFromSuperview
        end)
      end)
    end)
  end)
end

.build_tree(item, selector) ⇒ Object



304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/cocoa/sugarcube-repl/repl.rb', line 304

def build_tree(item, selector)
  if selector.is_a? Proc
    items = selector.call(item)
  else
    items = item.send(selector)
  end
  items ||= []

  ret = [item]
  items.each_with_index { |subview, index|
    ret.concat SugarCube::Repl::build_tree(subview, selector)
  }
  ret
end

.center(*args) ⇒ Object Also known as: c



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/cocoa/sugarcube-repl/repl.rb', line 157

def center(*args)
  return unless check_sugarcube_view

  element = nil
  total = nil
  direction = 'h'
  args.each do |option|
    case option
    when String, Symbol  # accept string or symbol
      direction = option.to_s
    when Numeric
      unless total
        total = option
      elsunless element
        element = option
      else
        raise "I don't know what to do with #{option.inspect}"
      end
    else
      raise "I don't know what to do with #{option.inspect}"
    end
  end
  element = 1 unless element
  total = 1 unless total

  view = @sugarcube_view

  left = view.origin.x
  top = view.origin.y

  if /h|x/.match(direction.downcase)
    swidth = view.frame.size.width
    pwidth = view.superview.frame.size.width / total
    left = (pwidth - swidth) / 2 + pwidth * (element - 1)
  end
  if /v|y/.match(direction.downcase)
    sheight = view.frame.size.height
    pheight = view.superview.frame.size.height / total
    top = (pheight - sheight) / 2 + pheight * (element - 1)
  end

  self.origin left, top
end

.check_sugarcube_viewObject



319
320
321
322
323
# File 'lib/cocoa/sugarcube-repl/repl.rb', line 319

def check_sugarcube_view
  raise 'no view has been assigned to SugarCube::Repl::adjust' unless @sugarcube_view

  true
end

.down(val = 1) ⇒ Object Also known as: d



63
64
65
66
67
68
69
70
71
72
# File 'lib/cocoa/sugarcube-repl/repl.rb', line 63

def down(val=1)
  return unless check_sugarcube_view

  f = @sugarcube_view.frame
  f.origin.y += val
  @sugarcube_view.frame = f
  puts format_frame(f)

  @sugarcube_view
end

.draw_tree(item, selector, tab = nil, is_last = true, items_index = 0) ⇒ Object

Draws the tree items



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/cocoa/sugarcube-repl/repl.rb', line 251

def draw_tree(item, selector, tab=nil, is_last=true, items_index=0)
  space = ' '
  if items_index < 10
    print '  '
  elsif items_index < 100
    print ' '
  elsif items_index > 999  # good god, man!
    space = ''
  end
  print items_index.to_s + ":" + space

  if tab
    print tab
    if is_last
      print '`-- '
      tab += '    '
    else
      print '+-- '
      tab += '|   '
    end
  else
    print '. '
    tab = ''
  end

  if self == item || @sugarcube_view == item
    print "\033[1m"
  end
  print draw_tree_item(item)
  if self == item || @sugarcube_view == item
    print "\033[0m"
  end
  puts

  if selector.is_a? Proc
    items = selector.call(item)
  else
    items = item.send(selector)
  end
  items ||= []

  items.each_with_index { |subview, index|
    items_index += 1
    if self.respond_to? :draw_tree
      items_index = draw_tree(subview, selector, tab, index == items.length - 1, items_index)
    else
      items_index = SugarCube::Repl::draw_tree(subview, selector, tab, index == items.length - 1, items_index)
    end
  }

  return items_index
end

.draw_tree_item(item) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/ios/sugarcube-repl/repl.rb', line 102

def draw_tree_item(item)
  if item.is_a?(UIView)
    item.sugarcube_to_s superview: false
  else
    item.to_s
  end
end

.format_frame(frame) ⇒ Object



325
326
327
# File 'lib/cocoa/sugarcube-repl/repl.rb', line 325

def format_frame(frame)
  "[[#{frame.origin.x}, #{frame.origin.y}], [#{frame.size.width}, #{frame.size.height}]]"
end

.frame(f = nil) ⇒ Object Also known as: f

| FRAME



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/cocoa/sugarcube-repl/repl.rb', line 27

def frame(f=nil)
  return unless check_sugarcube_view

  return @sugarcube_view.frame unless f

  f = SugarCube::CoreGraphics::Rect(f)
  @sugarcube_view.frame = f
  puts format_frame(f)

  @sugarcube_view
end

.get_tree_item(item) ⇒ Object



202
203
204
205
206
207
208
# File 'lib/cocoa/sugarcube-repl/repl.rb', line 202

def get_tree_item(item)
  if item.nil? || item.is_a?(Fixnum)
    window(item)
  else
    item
  end
end

.get_tree_selector(item) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ios/sugarcube-repl/repl.rb', line 86

def get_tree_selector(item)
  if item.is_a? UIView
    return :subviews
  elsif item.is_a? UIViewController
    return -> (ctlr) do
      ret = Array.new ctlr.childViewControllers
      if ctlr.presentedViewController && ctlr.presentedViewController.presentingViewController == ctlr
        ret << ctlr.presentedViewController
      end
      ret
    end
  else
    nil
  end
end

.left(val = 1) ⇒ Object Also known as: l

| ORIGIN



41
42
43
# File 'lib/cocoa/sugarcube-repl/repl.rb', line 41

def left(val=1)
  SugarCube::Repl::right -val
end

.origin(x = nil, y = nil) ⇒ Object Also known as: o



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/cocoa/sugarcube-repl/repl.rb', line 75

def origin x=nil, y=nil
  return unless check_sugarcube_view

  f = @sugarcube_view.frame
  return f.origin unless x

  if y
    f.origin.x = x
    f.origin.y = y
  else
    f.origin = SugarCube::CoreGraphics::Point(x)
  end
  @sugarcube_view.frame = f
  puts format_frame(f)

  @sugarcube_view
end

.restoreObject

| RESTORE



149
150
151
152
153
154
155
# File 'lib/cocoa/sugarcube-repl/repl.rb', line 149

def restore
  return unless check_sugarcube_view

  @sugarcube_restore.each do |msg, value|
    SugarCube::Repl.send(msg, value)
  end
end

.right(val = 1) ⇒ Object Also known as: r



46
47
48
49
50
51
52
53
54
55
# File 'lib/cocoa/sugarcube-repl/repl.rb', line 46

def right(val=1)
  return unless check_sugarcube_view

  f = @sugarcube_view.frame
  f.origin.x += val
  @sugarcube_view.frame = f
  puts format_frame(f)

  @sugarcube_view
end

.rootObject



13
14
15
# File 'lib/ios/sugarcube-repl/repl.rb', line 13

def root
  window.rootViewController
end

.shadow(shadow = nil) ⇒ Object Also known as: h

| SHADOW



27
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
# File 'lib/ios/sugarcube-repl/repl.rb', line 27

def shadow(shadow=nil)
  return unless check_sugarcube_view

  if shadow
    {
      opacity: :'shadowOpacity=',
      radius: :'shadowRadius=',
      offset: :'shadowOffset=',
      color: :'shadowColor=',
      path: :'shadowPath=',
    }.each { |key, msg|
      if value = shadow[key]
        if key == :color and [Symbol, Fixnum, NSString, UIImage, UIColor].any?{|klass| value.is_a? klass}
          value = value.uicolor.CGColor
        end
        @sugarcube_view.layer.send(msg, value)
        @sugarcube_view.layer.masksToBounds = false
        @sugarcube_view.layer.shouldRasterize = true
      end
    }
    @sugarcube_view
  else
    {
      opacity: @sugarcube_view.layer.shadowOpacity,
      radius: @sugarcube_view.layer.shadowRadius,
      offset: @sugarcube_view.layer.shadowOffset,
      color: @sugarcube_view.layer.shadowColor,
      path: @sugarcube_view.layer.shadowPath,
    }
  end
end

.shorter(val = 1) ⇒ Object Also known as: s



112
113
114
# File 'lib/cocoa/sugarcube-repl/repl.rb', line 112

def shorter(val=1)
  SugarCube::Repl::taller -val
end

.size(w = nil, h = nil) ⇒ Object Also known as: z



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/cocoa/sugarcube-repl/repl.rb', line 129

def size(w=nil, h=nil)
  return unless check_sugarcube_view

  f = @sugarcube_view.frame
  return f.size unless w

  if h
    f.size.width = w
    f.size.height = h
  else
    f.size = SugarCube::CoreGraphics::Size(w)
  end
  @sugarcube_view.frame = f
  puts format_frame(f)

  @sugarcube_view
end

.taller(val = 1) ⇒ Object Also known as: t



117
118
119
120
121
122
123
124
125
126
# File 'lib/cocoa/sugarcube-repl/repl.rb', line 117

def taller(val=1)
  return unless check_sugarcube_view

  f = @sugarcube_view.frame
  f.size.height += val
  @sugarcube_view.frame = f
  puts format_frame(f)

  @sugarcube_view
end

.thinner(val = 1) ⇒ Object Also known as: n

| SIZE



95
96
97
# File 'lib/cocoa/sugarcube-repl/repl.rb', line 95

def thinner(val=1)
  SugarCube::Repl::wider -val
end

.tree(item = nil, selector = nil, &sel_blk) ⇒ Object

Parameters:

  • item (defaults to: nil)

    this can be a tree-like item (View, ViewController, CALayer, Menu) or an integer, in which case it will select that window. Defalt is to display the keyWindow

  • selector (defaults to: nil)

    If you pass an unsupported object to tree, you will need to pass a selector as well - this method should return an array of items which are passed recursively to tree



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/cocoa/sugarcube-repl/repl.rb', line 218

def tree(item=nil, selector=nil, &sel_blk)
  item = get_tree_item(item)

  if sel_blk && selector
    raise "You can't hand me a block AND a selector.  I don't know what to do with them!"
  elsif sel_blk
    selector = sel_blk
  elsif ! selector
    if item.is_a? CALayer
      selector = :sublayers
    elsif defined?(SKNode) && item.is_a?(SKNode)
      selector = :children
    else
      selector = get_tree_selector(item)
    end

    unless selector
      raise "Unable to determine a SugarCube::Repl::tree selector for #{item.class.to_s}"
    end
  end

  @sugarcube_items = SugarCube::Repl::build_tree(item, selector)
  if self.respond_to? :draw_tree
    draw_tree(item, selector)
  else
    SugarCube::Repl::draw_tree(item, selector)
  end
  puts ''

  return item
end

.up(val = 1) ⇒ Object Also known as: u



58
59
60
# File 'lib/cocoa/sugarcube-repl/repl.rb', line 58

def up(val=1)
  SugarCube::Repl::down -val
end

.wider(val = 1) ⇒ Object Also known as: w



100
101
102
103
104
105
106
107
108
109
# File 'lib/cocoa/sugarcube-repl/repl.rb', line 100

def wider(val=1)
  return unless check_sugarcube_view

  f = @sugarcube_view.frame
  f.size.width += val
  @sugarcube_view.frame = f
  puts format_frame(f)

  @sugarcube_view
end

.window(index = nil) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/ios/sugarcube-repl/repl.rb', line 5

def window(index=nil)
  if index
    UIApplication.sharedApplication.windows[index]
  else
    UIApplication.sharedApplication.keyWindow
  end
end