Class: Motion::Xray::SaveUIPlugin

Inherits:
Plugin
  • Object
show all
Defined in:
lib/motion-xray/plugins/save_ui_plugin.rb

Instance Attribute Summary

Attributes inherited from Plugin

#name, #target, #view

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Plugin

#get_plugin_view, #hide, name, #xray_name

Constructor Details

#initializeSaveUIPlugin


6
7
8
# File 'lib/motion-xray/plugins/save_ui_plugin.rb', line 6

def initialize(type=nil)
  @type = type
end

Class Method Details

.encode(format, object) ⇒ Object


46
47
48
49
50
51
52
53
# File 'lib/motion-xray/plugins/save_ui_plugin.rb', line 46

def encode(format, object)
  handler = encoders(format, object.class)
  if handler
    handler.call(object)
  else
    nil
  end
end

.encoders(format, type = nil) ⇒ Object


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/motion-xray/plugins/save_ui_plugin.rb', line 25

def encoders(format, type=nil)
  @encoders ||= {}
  @encoders[format] ||= {}
  unless @did_startup
    @did_startup = true
    startup
  end

  if type
    retval = nil
    while type
      retval = @encoders[format][type]
      break if retval
      type = type.superclass
    end
    return retval
  else
    return @encoders[format]
  end
end

.register(format, type, &handler) ⇒ Object


19
20
21
22
23
# File 'lib/motion-xray/plugins/save_ui_plugin.rb', line 19

def register(format, type, &handler)
  # don't care about the return - the side effect is to establish
  # @encoders
  encoders(format)[type] = handler
end

.startupObject


55
56
57
58
59
60
61
62
63
# File 'lib/motion-xray/plugins/save_ui_plugin.rb', line 55

def startup
  register(:teacup, CGRect) { |rect| "[[#{rect.origin.x}, #{rect.origin.y}], [#{rect.size.width}, #{rect.size.height}]]"}
  register(:teacup, CGPoint) { |rect| "[#{rect.origin.x}, #{rect.origin.y}]"}
  register(:teacup, CGSize) { |rect| "[#{rect.size.width}, #{rect.size.height}]"}
  register(:teacup, true.class) { |t| 'true' }
  register(:teacup, false.class) { |t| 'false' }
  # fall back
  register(:teacup, NSObject) { |v| v.inspect }
end

Instance Method Details

#edit(target) ⇒ Object


94
95
96
97
98
# File 'lib/motion-xray/plugins/save_ui_plugin.rb', line 94

def edit(target)
  super
  XrayTargetDidChangeNotification.remove_observer(self)
  XrayTargetDidChangeNotification.add_observer(self, :'save_changes:', @target)
end

#plugin_view(canvas) ⇒ Object


72
73
74
75
76
77
78
79
# File 'lib/motion-xray/plugins/save_ui_plugin.rb', line 72

def plugin_view(canvas)
  @log = UITextView.alloc.initWithFrame(canvas.bounds)
  @log.editable = false
  @log.font = :monospace.uifont
  @log.textColor = 0xBCBEAB.uicolor
  @log.backgroundColor = 0x2b2b2b.uicolor
  return @log
end

#save_changes(notification) ⇒ Object


81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/motion-xray/plugins/save_ui_plugin.rb', line 81

def save_changes(notification)
  @changes[@target] ||= {}
  property = notification.userInfo['property']
  value = notification.userInfo['value']
  original = notification.userInfo['original']

  if value == original
    @changes[@target].delete(property)
  else
    @changes[@target][property] = notification.userInfo['value']
  end
end

#showObject


100
101
102
103
104
# File 'lib/motion-xray/plugins/save_ui_plugin.rb', line 100

def show
  if type
    @log.text = send("#{type}_text")
  end
end

#teacup_textObject


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
# File 'lib/motion-xray/plugins/save_ui_plugin.rb', line 106

def teacup_text
  apply = {}
  @changes.each do |view, properties|
    properties.each do |property, value|
      encoded = SaveUIPlugin.encode(:teacup, value)
      if encoded
        apply[view] ||= []
        apply[view] << [property, encoded]
      end
    end
  end

  text = ''
  apply.each do |view, stuff|
    first_line = true
    if view.stylesheet && view.stylename
      name = "Teacup::Stylesheet[#{view.stylesheet.name.inspect}].style #{view.stylename.inspect},\n  "
    else
      name = "#{view.class.name.downcase}.style "
    end

    text << "#{name}"
    stuff.each do |property, encoded|
      unless first_line
        text << ",\n  "
      end
      text << "#{property}: #{encoded}"
      first_line = false
    end
    text << "\n\n"
  end
  return text
end

#typeObject


10
11
12
# File 'lib/motion-xray/plugins/save_ui_plugin.rb', line 10

def type
  @type || :teacup
end