Class: SugarCube::Repl

Inherits:
Object show all
Defined in:
lib/all/sugarcube-repl/repl.rb

Class Method Summary collapse

Class Method Details

.adjust(item = nil) ⇒ Object



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

def adjust(item=nil)
  return @adjust_item unless item

  if item.is_a? Fixnum
    @tree_items ||= build_default_tree
    item = @tree_items[item]
  end

  # a/adjust will return this object
  @adjust_item = item

  adjust_init(item)

  item
end

.adjust_init(item) ⇒ Object



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

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


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

def blink(color=nil)
  return unless check_sugarcube_view

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

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

.build_default_treeObject



4
5
6
# File 'lib/cocoa/sugarcube-repl/repl.rb', line 4

def build_default_tree
  build_tree(window)
end

.build_tree(item) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/all/sugarcube-repl/repl.rb', line 159

def build_tree(item)
  items = call_item_selector(item)

  ret = [item]
  return ret if @collapsed_items && @collapsed_items.include?(item)

  index = 0
  items.each do |subitem|
    ret.concat build_tree(subitem)
    index += 1
  end
  ret
end

.call_item_selector(item) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/all/sugarcube-repl/repl.rb', line 173

def call_item_selector(item)
  selector = nil
  klass = item.class
  while klass && !selector
    selector = tree_selectors(klass)
    klass = klass.superclass
  end

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

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

  return items || []
end

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



149
150
151
152
153
154
155
156
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
# File 'lib/cocoa/sugarcube-repl/repl.rb', line 149

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 = @adjust_item

  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



212
213
214
215
216
# File 'lib/cocoa/sugarcube-repl/repl.rb', line 212

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

  true
end

.clear_defaultObject



52
53
54
# File 'lib/all/sugarcube-repl/repl.rb', line 52

def clear_default
  @default = nil
end

.collapse(item) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/all/sugarcube-repl/repl.rb', line 26

def collapse(item)
  if item.is_a? Fixnum
    @tree_items ||= build_default_tree
    item = @tree_items[item]
  end

  @collapsed_items ||= []
  if @collapsed_items.include?(item)
    @collapsed_items.delete(item)
  else
    @collapsed_items << item
  end

  retval = tree

  if @collapsed_items
    @collapsed_items.keep_if { |v| @tree_items.include? v }
  end

  retval
end

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



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

def down(val=1)
  return unless check_sugarcube_view

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

  @adjust_item
end

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



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/all/sugarcube-repl/repl.rb', line 102

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

  if tab
    line << tab
    if @collapsed_items && @collapsed_items.include?(item)
      line << '<<< '
    else
      if is_last
        line << '`-- '
        tab += '    '
      else
        line << '+-- '
        tab += '|   '
      end
    end
  else
    line << '. '
    tab = ''
  end

  if self == item || @adjust_item == item
    line << "\033[1m"
  end
  line << draw_tree_item(item)
  if self == item || @adjust_item == item
    line << "\033[0m"
  end
  puts line

  items = call_item_selector(item)

  unless @collapsed_items && @collapsed_items.include?(item)
    index = 0
    items.each do |subitem|
      items_index += 1
      if self.respond_to? :draw_tree
        items_index = draw_tree(subitem, tab, index == items.length - 1, items_index)
      else
        items_index = draw_tree(subitem, tab, index == items.length - 1, items_index)
      end
      index += 1
    end
  end

  return items_index
end

.draw_tree_item(item) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/ios/sugarcube-repl/repl.rb', line 98

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



218
219
220
# File 'lib/cocoa/sugarcube-repl/repl.rb', line 218

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



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

def frame(f=nil)
  return unless check_sugarcube_view

  return @adjust_item.frame unless f

  if defined?(SugarCube::CoreGraphics)
    f = SugarCube::CoreGraphics::Rect(f)
  end
  @adjust_item.frame = f
  puts format_frame(f)

  @adjust_item
end

.get_tree_item(item) ⇒ Object



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

def get_tree_item(item)
  if item.nil? && @default
    @default
  elsif item.nil? || item.is_a?(Fixnum)
    window(item)
  else
    item
  end
end

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

| ORIGIN



25
26
27
# File 'lib/cocoa/sugarcube-repl/repl.rb', line 25

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

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



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

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

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

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

  @adjust_item
end

.register_default_tree_selectorsObject



194
195
196
197
198
199
200
# File 'lib/cocoa/sugarcube-repl/repl.rb', line 194

def register_default_tree_selectors
  register_tree_selector(CALayer, :sublayers)
  if defined?(SKNode)
    register_tree_selector(SKNode, :children)
  end
  register_platform_tree_selectors
end

.register_platform_tree_selectorsObject



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

def register_platform_tree_selectors
  register_tree_selector(UIView, :subviews)
  register_tree_selector(UIViewController) do |ctlr|
    ret = Array.new ctlr.childViewControllers
    if ctlr.presentedViewController && ctlr.presentedViewController.presentingViewController == ctlr
      ret << ctlr.presentedViewController
    end
    ret
  end
end

.register_tree_selector(klass, selector = nil, &sel_blk) ⇒ Object



56
57
58
59
60
61
# File 'lib/all/sugarcube-repl/repl.rb', line 56

def register_tree_selector(klass, selector=nil, &sel_blk)
  if selector && sel_blk
    raise "You can't hand me a block AND a selector.  I don't know what to do with both!"
  end
  tree_selectors[klass] = selector || sel_blk
end

.restoreObject

| RESTORE



141
142
143
144
145
146
147
# File 'lib/cocoa/sugarcube-repl/repl.rb', line 141

def restore
  return unless check_sugarcube_view

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

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



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

def right(val=1)
  return unless check_sugarcube_view

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

  @adjust_item
end

.rootObject



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

def root
  window.rootViewController
end

.set_default(item) ⇒ Object



48
49
50
# File 'lib/all/sugarcube-repl/repl.rb', line 48

def set_default(item)
  @default = item
end

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

| SHADOW



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

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
        @adjust_item.layer.send(msg, value)
        @adjust_item.layer.masksToBounds = false
        @adjust_item.layer.shouldRasterize = true
      end
    }
    @adjust_item
  else
    {
      opacity: @adjust_item.layer.shadowOpacity,
      radius: @adjust_item.layer.shadowRadius,
      offset: @adjust_item.layer.shadowOffset,
      color: @adjust_item.layer.shadowColor,
      path: @adjust_item.layer.shadowPath,
    }
  end
end

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



100
101
102
# File 'lib/cocoa/sugarcube-repl/repl.rb', line 100

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

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



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/cocoa/sugarcube-repl/repl.rb', line 117

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

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

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

  @adjust_item
end

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



105
106
107
108
109
110
111
112
113
114
# File 'lib/cocoa/sugarcube-repl/repl.rb', line 105

def taller(val=1)
  return unless check_sugarcube_view

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

  @adjust_item
end

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

| SIZE



83
84
85
# File 'lib/cocoa/sugarcube-repl/repl.rb', line 83

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



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

def tree(item=nil, selector=nil, &sel_blk)
  item = get_tree_item(item)
  if selector || sel_blk
    register_tree_selector(item.class, selector, &sel_blk)
  end

  @tree_items = build_tree(item)
  if @collapsed_items
    @collapsed_items.keep_if { |v| @tree_items.include? v }
  end

  draw_tree(item)
  puts ''

  return item
end

.tree_selectors(klass = nil) ⇒ Object



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

def tree_selectors(klass=nil)
  if ! @tree_selectors
    @tree_selectors ||= {}
    @tree_selectors[nil.class] = -> (foo) { nil }
    register_default_tree_selectors
  end

  if klass
    @tree_selectors[klass]
  else
    @tree_selectors
  end
end

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



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

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

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



88
89
90
91
92
93
94
95
96
97
# File 'lib/cocoa/sugarcube-repl/repl.rb', line 88

def wider(val=1)
  return unless check_sugarcube_view

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

  @adjust_item
end

.window(index = nil) ⇒ Object



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

def window(index=nil)
  if index
    UIApplication.sharedApplication.windows[index]
  elsif UIApplication.sharedApplication.keyWindow
    UIApplication.sharedApplication.keyWindow
  else
    UIApplication.sharedApplication.windows.select { |window| window.subviews.count > 0 }.first
  end
end