-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbuilder.rb
More file actions
196 lines (163 loc) · 4.5 KB
/
builder.rb
File metadata and controls
196 lines (163 loc) · 4.5 KB
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
require "ui/widget"
module UI
module Builder
TOPLEVEL_ELEMENTS = [:main_dialog, :popup_dialog]
CONTAINER_ELEMENTS = [:vbox, :hbox,
:hsquash, :vsquash,
:hvsquash, :frame, :replace_point,
:left, :right, :top, :bottom, :hcenter, :vcenter,
:hvcenter, :margin_box, :radio_button_group]
LEAF_ELEMENTS = [:push_button, :input_field, :check_box, :radio_button,
:hspacing, :vspacing, :hstretch, :vstretch,
:label, :progress_bar, :rich_text, :selection_box]
# @visibility private
def initialize_widget(el, opts)
el.id = opts[:id] if opts.has_key?(:id)
properties = el.properties
opts.each do |k,v|
el[k] = v if properties.include? k
if UI::Widget::CALLBACKS.include? k
el.send(k,&v);
end
end
end
[TOPLEVEL_ELEMENTS, CONTAINER_ELEMENTS, LEAF_ELEMENTS].flatten.each do |element|
define_method(element) do |*args, &block|
File.write("/tmp/io.calls", "call #{element} with \#{args.inspect}")
opts = {}
# If last element is a Hash
# we assume they are options
if args.last.is_a?(Hash)
opts = args.pop
end
# add parent if needed
unless TOPLEVEL_ELEMENTS.include?(element)
args.unshift(@__ui_builder_parent)
end
el = Builder.send("create_#{element}", *args)
initialize_widget(el, opts)
unless LEAF_ELEMENTS.include?(element)
old_parent = @__ui_builder_parent
@__ui_builder_parent = el
block.call
@__ui_builder_parent = old_parent
end
el
end
end
# @!group Top level elements
# @!method main_dialog(&block)
# @return [Dialog] a main dialog
# @example
# dlg = UI.main_dialog {
# ...
# }
#def main_dialog(opts={}, &block)
# el = Builder.create_main_dialog
#
#end
# @!method popup_dialog(&block)
# @return [Dialog] a popup dialog
# @example
# dlg = UI.popup_dialog {
# ...
# }
# @!endgroup
# @!group Container elements
# @!method vbox(&block)
# Creates a vertically alligned container
# @return [LayoutBox]
# @example
# UI.main_dialog {
# vbox {
# push_button "Ok"
# }
# }
# @!method hbox(&block)
# Horizontally aligned container
# @see vbox
# @see LayoutBox
# @!method hstretch(&block)
# @!method vstretch(&block)
# @!method hspacing(&block)
# @!method vspacing(&block)
# @!method hsquash(&block)
# @!method vsquash(&block)
# @!method hvsquash(&block)
# @!method left(&block)
# @!method right(&block)
# @!method top(&block)
# @!method bottom(&block)
# @!method hcenter(&block)
# @!method vcenter(&block)
# @!method hvcenter(&block)
# @!method margin_box(&block)
# @!method radio_button_group(&block)
# @!method frame(label, &block)
# Creates a frame
# @param [String] label
# @param [Fixnum] max_value Maximum progress value
# @return [Frame]
# @example
# UI.main_dialog {
# vbox {
# frame "Settings" {
# ...
# }
# }
# }
# @!method replace_point(&block)
# @!endgroup
# @!group Elements
# @!method push_button(text, &block)
# Creates a push button
# @return [Button]
# @example
# UI.main_dialog {
# vbox {
# push_button "Ok"
# }
# }
# @!method progress_bar(label, max_value = 100)
# Creates a progress bar
# @param [String] label The caption above the progress bar
# @param [Fixnum] max_value Maximum progress value
# @return [ProgressBar]
# @example
# UI.main_dialog {
# vbox {
# progress_bar "Current progress"
# }
# }
# @!method input_field(value)
# @!method check_box(label)
# @!method rich_text(text)
# @!method label(text)
# @!endgroup
end
extend Builder
class Dialog
include Builder
end
class LayoutBox
include Builder
end
class Spacing
include Builder
end
class Squash
include Builder
end
class Frame
include Builder
end
class RadioButtonGroup
include Builder
end
class ReplacePoint
include Builder
end
class Alignment
include Builder
end
end