-
Notifications
You must be signed in to change notification settings - Fork 6
/
observers.rb
206 lines (150 loc) · 5.33 KB
/
observers.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
47
48
49
50
51
52
53
54
55
56
57
58
59
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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
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
200
201
202
203
204
205
206
require 'sketchup.rb'
# TODO: rename select into update, also in attribute_inspector.rb
# TODO: inverse dependency, use observer pattern?
# These observers currently have an reference to an attribute_inspector and call inspect on it.
# Instead, create a common observer interface on which attribute_inspector registers on(:selected){ |*selected| inspect(*selected) }
module AE
module AttributeInspector
module Observers
class SelectionObserver < Sketchup::SelectionObserver
def initialize(instance)
@instance = instance
@is_selection_cleared = false
end
# When an entity was selected and another entity is selected instead,
# onSelectionCleared triggers followed by onSelectionBulkChange.
# onSelectionCleared propagates a wrong information that is immediately not
# up-to-date anymore. Because of this, we filter out double triggers by waiting
# in onSelectionCleared and checking whether it is followed by onSelectionBulkChange.
def onSelectionBulkChange(selection)
Utils.catch_errors{
@is_selection_cleared = false unless selection.empty?
@instance.select(*selection.to_a)
}
end
def onSelectionCleared(selection)
@is_selection_cleared = true
UI.start_timer(0, false) {
Utils.catch_errors{
@instance.select(*selection.to_a) if @is_selection_cleared
}
}
end
end
class DefinitionsSelectionObserver < Sketchup::SelectionObserver
def initialize(instance)
@instance = instance
@is_selection_cleared = false
end
# Since active/selected definition in DefinitionsList can not be observed,
# we workaround it and use the definition of the selected DrawingElement.
def onSelectionBulkChange(selection)
Utils.catch_errors{
@is_selection_cleared = false unless selection.empty?
definitions = selection.to_a.select{ |e| e.respond_to?(:definition) }.map{ |e| e.definition }
@instance.select(*definitions)
}
end
def onSelectionCleared(selection)
@is_selection_cleared = true
UI.start_timer(0, false) {
Utils.catch_errors{
if @is_selection_cleared
definitions = selection.to_a.select{ |e| e.respond_to?(:definition) }.map{ |e| e.definition }
@instance.select(*definitions)
end
}
}
end
end
class MaterialsObserver < Sketchup::MaterialsObserver
def initialize(instance)
@instance = instance
end
def onMaterialSetCurrent(materials, material)
# TODO: This should trigger when Materials#current changes.
Utils.catch_errors{
@instance.select(material)
}
end
end
class LayersObserver < Sketchup::LayersObserver
def initialize(instance)
@instance = instance
end
def onCurrentLayerChanged(layers, layer)
Utils.catch_errors{
@instance.select(layer)
}
end
end
class PagesObserver < Sketchup::PagesObserver
def initialize(instance)
@instance = instance
end
# TODO: This does not trigger when Pages#selected_page changes, only when a page is modified.
def onContentsModified(pages)
Utils.catch_errors{
@instance.select(pages.selected_page)
}
end
end
class RenderingOptionsStylesObserver < Sketchup::RenderingOptionsObserver
def initialize(instance)
@instance = instance
@selected_style = nil
end
# Since there is no StylesObserver and EntityObserver (Styles is an Entity)
# is not triggered for changed active/selected style,
# we workaround it and observe the rendering options.
def onRenderingOptionsChanged(rendering_options, type)
Utils.catch_errors{
styles = rendering_options.model.styles
if styles.selected_style != @selected_style
@active_style = styles.selected_style
@instance.select(styles.selected_style)
end
}
end
end
class ModelObserver < Sketchup::ModelObserver
def initialize(instance)
@instance = instance
end
def onTransactionUndo(model)
Utils.catch_errors{
@instance.refresh
}
end
def onTransactionRedo(model)
Utils.catch_errors{
@instance.refresh
}
end
end
class NewModelObserver < Sketchup::AppObserver
def initialize(instance)
@instance = instance
end
def expectsStartupModelNotifications
return true
end
def onNewModel(model)
Utils.catch_errors{
@instance.model = model
}
end
def onOpenModel(model)
Utils.catch_errors{
@instance.model = model
}
end
def onActivateModel(model)
Utils.catch_errors{
@instance.model = model
}
end
end
end # module Observers
end # class AttributeInspector
end # module AE