glimmer-dsl-opal 0.0.7 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +56 -0
- data/README.md +815 -76
- data/VERSION +1 -1
- data/lib/glimmer-dsl-opal.rb +45 -8
- data/lib/glimmer-dsl-opal/ext/date.rb +13 -0
- data/lib/glimmer-dsl-opal/ext/exception.rb +5 -0
- data/lib/glimmer-dsl-opal/missing/net/http.rb +17 -0
- data/lib/glimmer-dsl-opal/missing/uri.rb +64 -0
- data/lib/{samples → glimmer-dsl-opal/samples}/elaborate/contact_manager.rb +2 -3
- data/lib/{samples → glimmer-dsl-opal/samples}/elaborate/contact_manager/contact.rb +0 -0
- data/lib/{samples → glimmer-dsl-opal/samples}/elaborate/contact_manager/contact_manager_presenter.rb +0 -0
- data/lib/{samples → glimmer-dsl-opal/samples}/elaborate/contact_manager/contact_repository.rb +24 -99
- data/lib/{samples → glimmer-dsl-opal/samples}/elaborate/login.rb +0 -0
- data/lib/{samples → glimmer-dsl-opal/samples}/elaborate/tic_tac_toe.rb +0 -0
- data/lib/{samples → glimmer-dsl-opal/samples}/elaborate/tic_tac_toe/board.rb +0 -0
- data/lib/{samples → glimmer-dsl-opal/samples}/elaborate/tic_tac_toe/cell.rb +0 -0
- data/lib/{samples → glimmer-dsl-opal/samples}/hello/hello_browser.rb +0 -0
- data/lib/glimmer-dsl-opal/samples/hello/hello_combo.rb +63 -0
- data/lib/{samples → glimmer-dsl-opal/samples}/hello/hello_computed.rb +19 -19
- data/lib/{samples → glimmer-dsl-opal/samples}/hello/hello_computed/contact.rb +0 -0
- data/lib/glimmer-dsl-opal/samples/hello/hello_custom_shell.rb +155 -0
- data/lib/glimmer-dsl-opal/samples/hello/hello_custom_widget.rb +86 -0
- data/lib/{samples → glimmer-dsl-opal/samples}/hello/hello_list_multi_selection.rb +0 -0
- data/lib/{samples → glimmer-dsl-opal/samples}/hello/hello_list_single_selection.rb +0 -0
- data/lib/glimmer-dsl-opal/samples/hello/hello_tab.rb +50 -0
- data/lib/glimmer-dsl-opal/samples/hello/hello_world.rb +29 -0
- data/lib/glimmer-dsl-opal/vendor/jquery.js +2 -0
- data/lib/glimmer/data_binding/element_binding.rb +1 -1
- data/lib/glimmer/data_binding/ext/observable_model.rb +6 -6
- data/lib/glimmer/data_binding/list_selection_binding.rb +1 -1
- data/lib/glimmer/data_binding/table_items_binding.rb +70 -0
- data/lib/glimmer/dsl/opal/async_exec_expression.rb +2 -2
- data/lib/glimmer/dsl/opal/color_expression.rb +38 -0
- data/lib/glimmer/dsl/opal/column_properties_expression.rb +22 -0
- data/lib/glimmer/dsl/opal/combo_selection_data_binding_expression.rb +2 -2
- data/lib/glimmer/dsl/opal/custom_widget_expression.rb +77 -0
- data/lib/glimmer/dsl/opal/dsl.rb +15 -14
- data/lib/glimmer/dsl/opal/font_expression.rb +47 -0
- data/lib/glimmer/dsl/opal/layout_data_expression.rb +2 -2
- data/lib/glimmer/dsl/opal/layout_expression.rb +22 -0
- data/lib/glimmer/dsl/opal/list_selection_data_binding_expression.rb +2 -3
- data/lib/glimmer/dsl/opal/message_box_expression.rb +2 -2
- data/lib/glimmer/dsl/opal/property_expression.rb +5 -2
- data/lib/glimmer/dsl/opal/rgb_expression.rb +32 -0
- data/lib/glimmer/dsl/opal/rgba_expression.rb +32 -0
- data/lib/glimmer/dsl/opal/shell_expression.rb +18 -2
- data/lib/glimmer/dsl/opal/swt_expression.rb +46 -0
- data/lib/glimmer/dsl/opal/{composite_expression.rb → table_column_expression.rb} +3 -3
- data/lib/glimmer/dsl/opal/{list_expression.rb → table_expression.rb} +3 -3
- data/lib/glimmer/dsl/opal/table_items_data_binding_expression.rb +29 -0
- data/lib/glimmer/dsl/opal/widget_expression.rb +24 -0
- data/lib/glimmer/dsl/opal/widget_listener_expression.rb +16 -3
- data/lib/glimmer/swt.rb +499 -0
- data/lib/glimmer/swt/browser_proxy.rb +27 -0
- data/lib/glimmer/swt/button_proxy.rb +40 -0
- data/lib/glimmer/swt/color_proxy.rb +119 -0
- data/lib/glimmer/{opal/select_proxy.rb → swt/combo_proxy.rb} +24 -19
- data/lib/glimmer/swt/composite_proxy.rb +31 -0
- data/lib/glimmer/{opal → swt}/display_proxy.rb +6 -4
- data/lib/glimmer/{opal → swt}/event_listener_proxy.rb +1 -1
- data/lib/glimmer/swt/fill_layout_proxy.rb +84 -0
- data/lib/glimmer/swt/font_proxy.rb +79 -0
- data/lib/glimmer/swt/grid_layout_proxy.rb +71 -0
- data/lib/glimmer/swt/label_proxy.rb +34 -0
- data/lib/glimmer/swt/layout_data_proxy.rb +87 -0
- data/lib/glimmer/swt/layout_proxy.rb +63 -0
- data/lib/glimmer/{opal → swt}/list_proxy.rb +31 -23
- data/lib/glimmer/swt/message_box_proxy.rb +143 -0
- data/lib/glimmer/{opal → swt}/point.rb +1 -1
- data/lib/glimmer/{opal → swt}/property_owner.rb +1 -1
- data/lib/glimmer/swt/row_layout_proxy.rb +105 -0
- data/lib/glimmer/swt/shell_proxy.rb +246 -0
- data/lib/glimmer/swt/style_constantizable.rb +154 -0
- data/lib/glimmer/swt/swt_proxy.rb +53 -0
- data/lib/glimmer/{opal/tab_folder.rb → swt/tab_folder_proxy.rb} +21 -15
- data/lib/glimmer/swt/tab_item_proxy.rb +84 -0
- data/lib/glimmer/swt/table_column_proxy.rb +56 -0
- data/lib/glimmer/swt/table_item_proxy.rb +147 -0
- data/lib/glimmer/swt/table_proxy.rb +159 -0
- data/lib/glimmer/swt/text_proxy.rb +46 -0
- data/lib/glimmer/swt/widget_proxy.rb +490 -0
- data/lib/glimmer/ui/custom_shell.rb +91 -0
- data/lib/glimmer/ui/custom_widget.rb +291 -0
- data/lib/glimmer/util/proc_tracker.rb +39 -0
- metadata +131 -76
- data/lib/glimmer/config.rb +0 -22
- data/lib/glimmer/dsl/engine.rb +0 -193
- data/lib/glimmer/dsl/expression.rb +0 -42
- data/lib/glimmer/dsl/expression_handler.rb +0 -48
- data/lib/glimmer/dsl/opal/browser_expression.rb +0 -17
- data/lib/glimmer/dsl/opal/button_expression.rb +0 -18
- data/lib/glimmer/dsl/opal/combo_expression.rb +0 -17
- data/lib/glimmer/dsl/opal/grid_layout_expression.rb +0 -17
- data/lib/glimmer/dsl/opal/label_expression.rb +0 -17
- data/lib/glimmer/dsl/opal/tab_folder_expression.rb +0 -17
- data/lib/glimmer/dsl/opal/tab_item_expression.rb +0 -17
- data/lib/glimmer/dsl/opal/text_expression.rb +0 -22
- data/lib/glimmer/dsl/parent_expression.rb +0 -12
- data/lib/glimmer/dsl/static_expression.rb +0 -36
- data/lib/glimmer/dsl/top_level_expression.rb +0 -7
- data/lib/glimmer/error.rb +0 -6
- data/lib/glimmer/invalid_keyword_error.rb +0 -6
- data/lib/glimmer/opal/div_proxy.rb +0 -22
- data/lib/glimmer/opal/document_proxy.rb +0 -178
- data/lib/glimmer/opal/element_proxy.rb +0 -287
- data/lib/glimmer/opal/grid_layout_proxy.rb +0 -54
- data/lib/glimmer/opal/iframe_proxy.rb +0 -23
- data/lib/glimmer/opal/input_proxy.rb +0 -45
- data/lib/glimmer/opal/label_proxy.rb +0 -25
- data/lib/glimmer/opal/layout_data_proxy.rb +0 -31
- data/lib/glimmer/opal/modal.rb +0 -94
- data/lib/glimmer/opal/tab_item.rb +0 -98
- data/lib/samples/elaborate/launch +0 -6
- data/lib/samples/hello/hello_combo.rb +0 -34
- data/lib/samples/hello/hello_tab.rb +0 -24
- data/lib/samples/hello/hello_world.rb +0 -8
- data/lib/samples/hello/launch +0 -10
- data/lib/samples/launch +0 -4
@@ -0,0 +1,105 @@
|
|
1
|
+
require 'glimmer/swt/layout_proxy'
|
2
|
+
|
3
|
+
module Glimmer
|
4
|
+
module SWT
|
5
|
+
class RowLayoutProxy < LayoutProxy
|
6
|
+
include Glimmer
|
7
|
+
|
8
|
+
STYLE = <<~CSS
|
9
|
+
.row-layout {
|
10
|
+
display: flex;
|
11
|
+
}
|
12
|
+
|
13
|
+
.row-layout-pack {
|
14
|
+
display: initial;
|
15
|
+
}
|
16
|
+
|
17
|
+
.row-layout-horizontal {
|
18
|
+
flex-direction: row;
|
19
|
+
}
|
20
|
+
|
21
|
+
.row-layout-horizontal.row-layout-pack {
|
22
|
+
flex-direction: none;
|
23
|
+
}
|
24
|
+
|
25
|
+
.row-layout-vertical {
|
26
|
+
flex-direction: column;
|
27
|
+
}
|
28
|
+
|
29
|
+
.row-layout-vertical.row-layout-pack {
|
30
|
+
flex-direction: none;
|
31
|
+
}
|
32
|
+
CSS
|
33
|
+
|
34
|
+
attr_reader :type, :margin_width, :margin_height, :spacing, :pack
|
35
|
+
|
36
|
+
def initialize(parent, args)
|
37
|
+
super(parent, args)
|
38
|
+
@type = @args.first || :horizontal
|
39
|
+
@marign_width = 15
|
40
|
+
@margin_height = 15
|
41
|
+
self.pack = true
|
42
|
+
@parent.dom_element.add_class('row-layout')
|
43
|
+
@parent.dom_element.add_class(horizontal? ? 'row-layout-horizontal' : 'row-layout-vertical')
|
44
|
+
end
|
45
|
+
|
46
|
+
def horizontal?
|
47
|
+
@type == :horizontal
|
48
|
+
end
|
49
|
+
|
50
|
+
def vertical?
|
51
|
+
@type == :vertical
|
52
|
+
end
|
53
|
+
|
54
|
+
def dom(widget_dom)
|
55
|
+
dom_result = widget_dom
|
56
|
+
dom_result += '<br />' if vertical? && @pack
|
57
|
+
dom_result
|
58
|
+
end
|
59
|
+
|
60
|
+
def pack=(value)
|
61
|
+
@pack = value
|
62
|
+
if @pack
|
63
|
+
@parent.dom_element.add_class('row-layout-pack')
|
64
|
+
else
|
65
|
+
@parent.dom_element.remove_class('row-layout-pack')
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def margin_width=(pixels)
|
70
|
+
@margin_width = pixels
|
71
|
+
# Using padding for width since margin-right isn't getting respected with width 100%
|
72
|
+
@parent.dom_element.css('padding-left', @margin_width)
|
73
|
+
@parent.dom_element.css('padding-right', @margin_width)
|
74
|
+
end
|
75
|
+
|
76
|
+
def margin_height=(pixels)
|
77
|
+
@margin_height = pixels
|
78
|
+
@parent.dom_element.css('margin-top', @margin_height)
|
79
|
+
@parent.dom_element.css('margin-bottom', @margin_height)
|
80
|
+
end
|
81
|
+
|
82
|
+
def spacing=(spacing)
|
83
|
+
@spacing = spacing.to_i
|
84
|
+
# TODO implement changes to accomodate layout_data in the future
|
85
|
+
@parent.style_element.html css {
|
86
|
+
s("##{@parent.id} > *") {
|
87
|
+
if horizontal?
|
88
|
+
margin_right "#{@spacing}px"
|
89
|
+
elsif vertical?
|
90
|
+
margin_bottom "#{@spacing}px"
|
91
|
+
end
|
92
|
+
}
|
93
|
+
s("##{@parent.id} > :last-child") {
|
94
|
+
if horizontal?
|
95
|
+
margin_right 0
|
96
|
+
elsif vertical?
|
97
|
+
margin_bottom 0
|
98
|
+
end
|
99
|
+
}
|
100
|
+
}.to_s
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
@@ -0,0 +1,246 @@
|
|
1
|
+
require 'glimmer/swt/widget_proxy'
|
2
|
+
require 'glimmer/swt/point'
|
3
|
+
|
4
|
+
module Glimmer
|
5
|
+
module SWT
|
6
|
+
class ShellProxy < CompositeProxy
|
7
|
+
# TODO consider renaming to ShellProxy to match SWT API
|
8
|
+
attr_reader :minimum_size
|
9
|
+
|
10
|
+
WIDTH_MIN = 130
|
11
|
+
HEIGHT_MIN = 0
|
12
|
+
|
13
|
+
def initialize(args)
|
14
|
+
@args = args
|
15
|
+
@children = []
|
16
|
+
# Document.ready? do end # TODO consider embedding this jQuery call in so outside consumers don't have to use it
|
17
|
+
Document.find('body').empty unless ENV['RUBY_ENV'] == 'test'
|
18
|
+
render
|
19
|
+
@layout = FillLayoutProxy.new(self, [])
|
20
|
+
@layout.margin_width = 0
|
21
|
+
@layout.margin_height = 0
|
22
|
+
self.minimum_size = Point.new(WIDTH_MIN, HEIGHT_MIN)
|
23
|
+
end
|
24
|
+
|
25
|
+
def element
|
26
|
+
'div'
|
27
|
+
end
|
28
|
+
|
29
|
+
def parent_path
|
30
|
+
'body'
|
31
|
+
end
|
32
|
+
|
33
|
+
def text
|
34
|
+
$document.title
|
35
|
+
end
|
36
|
+
|
37
|
+
def text=(value)
|
38
|
+
Document.title = value
|
39
|
+
end
|
40
|
+
|
41
|
+
def minimum_size=(width_or_minimum_size, height = nil)
|
42
|
+
@minimum_size = height.nil? ? width_or_minimum_size : Point.new(width_or_minimum_size, height)
|
43
|
+
return if @minimum_size.nil?
|
44
|
+
dom_element.css('min-width', "#{@minimum_size.x}px")
|
45
|
+
dom_element.css('min-height', "#{@minimum_size.y}px")
|
46
|
+
end
|
47
|
+
|
48
|
+
def style_dom_css
|
49
|
+
<<~CSS
|
50
|
+
.hide {
|
51
|
+
display: none !important;
|
52
|
+
}
|
53
|
+
.selected, .tabs .tab.selected {
|
54
|
+
background: rgb(80, 116, 211);
|
55
|
+
color: white;
|
56
|
+
}
|
57
|
+
CSS
|
58
|
+
end
|
59
|
+
|
60
|
+
def style_dom_shell_css
|
61
|
+
<<~CSS
|
62
|
+
html {
|
63
|
+
width: 100%;
|
64
|
+
height: 100%;
|
65
|
+
}
|
66
|
+
body {
|
67
|
+
width: 100%;
|
68
|
+
height: 100%;
|
69
|
+
margin: 0;
|
70
|
+
}
|
71
|
+
.shell {
|
72
|
+
height: 100%;
|
73
|
+
margin: 0;
|
74
|
+
}
|
75
|
+
.shell iframe {
|
76
|
+
width: 100%;
|
77
|
+
height: 100%;
|
78
|
+
}
|
79
|
+
CSS
|
80
|
+
end
|
81
|
+
|
82
|
+
def style_dom_list_css
|
83
|
+
<<~CSS
|
84
|
+
ul {
|
85
|
+
list-style: none;
|
86
|
+
padding: 0;
|
87
|
+
}
|
88
|
+
li {
|
89
|
+
cursor: default;
|
90
|
+
padding-left: 10px;
|
91
|
+
padding-right: 10px;
|
92
|
+
}
|
93
|
+
li.empty-list-item {
|
94
|
+
color: transparent;
|
95
|
+
}
|
96
|
+
CSS
|
97
|
+
end
|
98
|
+
|
99
|
+
def style_dom_tab_css
|
100
|
+
<<~CSS
|
101
|
+
.tabs .tab {
|
102
|
+
background-color: inherit;
|
103
|
+
float: left;
|
104
|
+
border: none;
|
105
|
+
outline: none;
|
106
|
+
cursor: pointer;
|
107
|
+
padding: 14px 16px;
|
108
|
+
transition: 0.3s;
|
109
|
+
font-size: 17px;
|
110
|
+
}
|
111
|
+
.tabs {
|
112
|
+
overflow: hidden;
|
113
|
+
border: 1px solid #ccc;
|
114
|
+
background-color: #f1f1f1;
|
115
|
+
}
|
116
|
+
CSS
|
117
|
+
end
|
118
|
+
|
119
|
+
def style_dom_tab_item_css
|
120
|
+
<<~CSS
|
121
|
+
/* Create an selected/current tablink class */
|
122
|
+
.tabs .tab.selected {
|
123
|
+
background-color: #ccc;
|
124
|
+
}
|
125
|
+
/* Change background color of buttons on hover */
|
126
|
+
.tabs .tab:hover {
|
127
|
+
background-color: #ddd;
|
128
|
+
}
|
129
|
+
/* Style the tab content */
|
130
|
+
.tab-item {
|
131
|
+
padding: 6px 12px;
|
132
|
+
border: 1px solid #ccc;
|
133
|
+
border-top: none;
|
134
|
+
}
|
135
|
+
CSS
|
136
|
+
end
|
137
|
+
|
138
|
+
def style_dom_modal_css
|
139
|
+
<<~CSS
|
140
|
+
/* The Modal (background) */
|
141
|
+
.modal {
|
142
|
+
position: fixed; /* Stay in place */
|
143
|
+
z-index: 1; /* Sit on top */
|
144
|
+
padding-top: 100px; /* Location of the box */
|
145
|
+
left: 0;
|
146
|
+
top: 0;
|
147
|
+
width: 100%; /* Full width */
|
148
|
+
height: 100%; /* Full height */
|
149
|
+
overflow: auto; /* Enable scroll if needed */
|
150
|
+
background-color: rgb(0,0,0); /* Fallback color */
|
151
|
+
background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
|
152
|
+
text-align: center;
|
153
|
+
}
|
154
|
+
|
155
|
+
/* Modal Content */
|
156
|
+
.modal-content {
|
157
|
+
background-color: #fefefe;
|
158
|
+
margin: auto;
|
159
|
+
border: 1px solid #888;
|
160
|
+
display: inline-block;
|
161
|
+
min-width: 200px;
|
162
|
+
}
|
163
|
+
|
164
|
+
.modal-content .text {
|
165
|
+
background: rgb(80, 116, 211);
|
166
|
+
color: white;
|
167
|
+
padding: 5px;
|
168
|
+
}
|
169
|
+
|
170
|
+
.modal-content .message {
|
171
|
+
padding: 20px;
|
172
|
+
}
|
173
|
+
|
174
|
+
/* The Close Button */
|
175
|
+
.close {
|
176
|
+
color: #aaaaaa;
|
177
|
+
float: right;
|
178
|
+
font-weight: bold;
|
179
|
+
margin: 5px;
|
180
|
+
}
|
181
|
+
|
182
|
+
.close:hover,
|
183
|
+
.close:focus {
|
184
|
+
color: #000;
|
185
|
+
text-decoration: none;
|
186
|
+
cursor: pointer;
|
187
|
+
}
|
188
|
+
CSS
|
189
|
+
end
|
190
|
+
|
191
|
+
def style_dom_table_css
|
192
|
+
<<~CSS
|
193
|
+
table {
|
194
|
+
border-spacing: 0;
|
195
|
+
}
|
196
|
+
|
197
|
+
table tr th,td {
|
198
|
+
cursor: default;
|
199
|
+
}
|
200
|
+
CSS
|
201
|
+
end
|
202
|
+
|
203
|
+
def dom
|
204
|
+
i = 0
|
205
|
+
body_id = id
|
206
|
+
body_class = ([name] + css_classes.to_a).join(' ')
|
207
|
+
@dom ||= html {
|
208
|
+
div(id: body_id, class: body_class) {
|
209
|
+
style(class: 'common-style') {
|
210
|
+
style_dom_css
|
211
|
+
}
|
212
|
+
style(class: 'shell-style') {
|
213
|
+
style_dom_shell_css
|
214
|
+
}
|
215
|
+
style(class: 'list-style') {
|
216
|
+
style_dom_list_css
|
217
|
+
}
|
218
|
+
style(class: 'tab-style') {
|
219
|
+
style_dom_tab_css
|
220
|
+
}
|
221
|
+
# style(class: 'tab-item-style') {
|
222
|
+
# style_dom_tab_item_css
|
223
|
+
# }
|
224
|
+
# style(class: 'modal-style') {
|
225
|
+
# style_dom_modal_css
|
226
|
+
# }
|
227
|
+
style(class: 'table-style') {
|
228
|
+
style_dom_table_css
|
229
|
+
}
|
230
|
+
style(class: 'fill-layout-style') {
|
231
|
+
Glimmer::SWT::FillLayoutProxy::STYLE
|
232
|
+
}
|
233
|
+
style(class: 'row-layout-style') {
|
234
|
+
Glimmer::SWT::RowLayoutProxy::STYLE
|
235
|
+
}
|
236
|
+
}
|
237
|
+
}.to_s
|
238
|
+
end
|
239
|
+
|
240
|
+
def open
|
241
|
+
# TODO consider the idea of delaying rendering till the open method
|
242
|
+
# TODO make it start as hidden and show shell upon open
|
243
|
+
end
|
244
|
+
end
|
245
|
+
end
|
246
|
+
end
|
@@ -0,0 +1,154 @@
|
|
1
|
+
# Copyright (c) 2020 Andy Maleh
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
# a copy of this software and associated documentation files (the
|
5
|
+
# "Software"), to deal in the Software without restriction, including
|
6
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
# the following conditions:
|
10
|
+
#
|
11
|
+
# The above copyright notice and this permission notice shall be
|
12
|
+
# included in all copies or substantial portions of the Software.
|
13
|
+
#
|
14
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
21
|
+
|
22
|
+
require 'glimmer/error'
|
23
|
+
|
24
|
+
module Glimmer
|
25
|
+
module SWT
|
26
|
+
# Mixin for all proxy classes that manage style constants (e.g. SWT, DND, etc...)
|
27
|
+
module StyleConstantizable
|
28
|
+
module ClassMethods
|
29
|
+
REGEX_SYMBOL_NEGATIVITY = /^([^!]+)(!)?$/
|
30
|
+
|
31
|
+
def constant_source_class
|
32
|
+
raise 'Not implemented! Mixer must implement!'
|
33
|
+
end
|
34
|
+
|
35
|
+
def constant_value_none
|
36
|
+
# TODO instead of raising error try a convention instead like CLASSNAME::NONE by default
|
37
|
+
raise 'Not implemented! Mixer must implement!'
|
38
|
+
end
|
39
|
+
|
40
|
+
# hash of extra styles (i.e. new style combinations)
|
41
|
+
def extra_styles
|
42
|
+
raise 'Not implemented! Mixer must implement!'
|
43
|
+
end
|
44
|
+
|
45
|
+
def error_message_invalid_style
|
46
|
+
" is an invalid #{constant_source_class.name.split(':').last} style! Please choose a style from #{constant_source_class.name} class constants." # TODO parameterize
|
47
|
+
end
|
48
|
+
|
49
|
+
# Gets constants (e.g. SWT::CONSTANT) where constant is
|
50
|
+
# passed in as a lower case symbol
|
51
|
+
def [](*symbols)
|
52
|
+
symbols = symbols.first if symbols.size == 1 && symbols.first.is_a?(Array)
|
53
|
+
result = symbols.compact.map do |symbol|
|
54
|
+
constant(symbol).tap do |constant_value|
|
55
|
+
raise Glimmer::Error, symbol.to_s + error_message_invalid_style unless constant_value.is_a?(Integer)
|
56
|
+
end
|
57
|
+
end.reduce do |output, constant_value|
|
58
|
+
if constant_value < 0
|
59
|
+
output & constant_value
|
60
|
+
else
|
61
|
+
output | constant_value
|
62
|
+
end
|
63
|
+
end
|
64
|
+
result.nil? ? constant_value_none : result
|
65
|
+
end
|
66
|
+
|
67
|
+
# Returns style integer value for passed in symbol or allows
|
68
|
+
# passed in object to pass through (e.g. Integer). This makes is convenient
|
69
|
+
# to use symbols or actual style integers in Glimmer
|
70
|
+
# Does not raise error for invalid values. Just lets them pass as is.
|
71
|
+
# (look into [] operator if you want an error raised on invalid values)
|
72
|
+
def constant(symbol)
|
73
|
+
return symbol unless symbol.is_a?(Symbol) || symbol.is_a?(String)
|
74
|
+
symbol_string, negative = extract_symbol_string_negativity(symbol)
|
75
|
+
swt_constant_symbol = symbol_string.downcase == symbol_string ? symbol_string.upcase.to_sym : symbol_string.to_sym
|
76
|
+
bit_value = constant_source_class.const_get(swt_constant_symbol)
|
77
|
+
negative ? ~bit_value : bit_value
|
78
|
+
rescue => e
|
79
|
+
begin
|
80
|
+
# Glimmer::Config.logger.debug {e.full_message}
|
81
|
+
alternative_swt_constant_symbol = constant_source_class.constants.find {|c| c.to_s.upcase == swt_constant_symbol.to_s.upcase}
|
82
|
+
bit_value = constant_source_class.const_get(alternative_swt_constant_symbol)
|
83
|
+
negative ? ~bit_value : bit_value
|
84
|
+
rescue => e
|
85
|
+
# Glimmer::Config.logger.debug {e.full_message}
|
86
|
+
bit_value = extra_styles[swt_constant_symbol]
|
87
|
+
if bit_value
|
88
|
+
negative ? ~bit_value : bit_value
|
89
|
+
else
|
90
|
+
symbol
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def extract_symbol_string_negativity(symbol)
|
96
|
+
if symbol.is_a?(Symbol) || symbol.is_a?(String)
|
97
|
+
symbol_negativity_match = symbol.to_s.match(REGEX_SYMBOL_NEGATIVITY)
|
98
|
+
symbol = symbol_negativity_match[1]
|
99
|
+
negative = !!symbol_negativity_match[2]
|
100
|
+
[symbol, negative]
|
101
|
+
else
|
102
|
+
negative = symbol < 0
|
103
|
+
[symbol, negative]
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def negative?(symbol)
|
108
|
+
extract_symbol_string_negativity(symbol)[1]
|
109
|
+
end
|
110
|
+
|
111
|
+
def has_constant?(symbol)
|
112
|
+
return false unless symbol.is_a?(Symbol) || symbol.is_a?(String)
|
113
|
+
constant(symbol).is_a?(Integer)
|
114
|
+
end
|
115
|
+
|
116
|
+
def constantify_args(args)
|
117
|
+
args.map {|arg| constant(arg)}
|
118
|
+
end
|
119
|
+
|
120
|
+
# Deconstructs a style integer into symbols
|
121
|
+
# Useful for debugging
|
122
|
+
def deconstruct(integer)
|
123
|
+
constant_source_class.constants.reduce([]) do |found, c|
|
124
|
+
constant_value = constant_source_class.const_get(c) rescue -1
|
125
|
+
is_found = constant_value.is_a?(Integer) && (integer & constant_value) == integer
|
126
|
+
is_found ? found += [c] : found
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
# Reverse engineer a style integer into a symbol
|
131
|
+
# Useful for debugging
|
132
|
+
def reverse_lookup(integer)
|
133
|
+
constant_source_class.constants.reduce([]) do |found, c|
|
134
|
+
constant_value = constant_source_class.const_get(c) rescue -1
|
135
|
+
is_found = constant_value.is_a?(Integer) && integer == constant_value
|
136
|
+
is_found ? found += [c] : found
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
def include?(swt_constant, *symbols)
|
141
|
+
swt_constant & self[symbols] == self[symbols]
|
142
|
+
end
|
143
|
+
|
144
|
+
end
|
145
|
+
|
146
|
+
def self.included(klass)
|
147
|
+
klass.extend(ClassMethods)
|
148
|
+
end
|
149
|
+
|
150
|
+
end
|
151
|
+
|
152
|
+
end
|
153
|
+
|
154
|
+
end
|