glimmer-dsl-swt 4.18.0.0 → 4.18.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (147) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +52 -0
  3. data/LICENSE.txt +1 -1
  4. data/README.md +571 -55
  5. data/RUBY_VERSION +1 -1
  6. data/VERSION +1 -1
  7. data/bin/girb +1 -1
  8. data/bin/girb_runner.rb +1 -1
  9. data/bin/glimmer +1 -1
  10. data/glimmer-dsl-swt.gemspec +26 -17
  11. data/lib/ext/glimmer.rb +1 -1
  12. data/lib/ext/glimmer/config.rb +7 -7
  13. data/lib/glimmer-dsl-swt.rb +2 -1
  14. data/lib/glimmer/Rakefile +1 -1
  15. data/lib/glimmer/data_binding/list_selection_binding.rb +1 -1
  16. data/lib/glimmer/data_binding/observable_widget.rb +1 -1
  17. data/lib/glimmer/data_binding/shine.rb +1 -1
  18. data/lib/glimmer/data_binding/table_items_binding.rb +1 -1
  19. data/lib/glimmer/data_binding/tree_items_binding.rb +1 -1
  20. data/lib/glimmer/data_binding/widget_binding.rb +1 -1
  21. data/lib/glimmer/dsl/swt/animation_expression.rb +43 -0
  22. data/lib/glimmer/dsl/swt/async_exec_expression.rb +1 -1
  23. data/lib/glimmer/dsl/swt/bind_expression.rb +1 -1
  24. data/lib/glimmer/dsl/swt/block_property_expression.rb +1 -1
  25. data/lib/glimmer/dsl/swt/checkbox_group_selection_data_binding_expression.rb +1 -1
  26. data/lib/glimmer/dsl/swt/color_expression.rb +1 -1
  27. data/lib/glimmer/dsl/swt/column_properties_expression.rb +1 -1
  28. data/lib/glimmer/dsl/swt/combo_selection_data_binding_expression.rb +1 -1
  29. data/lib/glimmer/dsl/swt/cursor_expression.rb +1 -1
  30. data/lib/glimmer/dsl/swt/custom_widget_expression.rb +1 -1
  31. data/lib/glimmer/dsl/swt/data_binding_expression.rb +1 -1
  32. data/lib/glimmer/dsl/swt/dialog_expression.rb +1 -1
  33. data/lib/glimmer/dsl/swt/directory_dialog_expression.rb +1 -1
  34. data/lib/glimmer/dsl/swt/display_expression.rb +1 -1
  35. data/lib/glimmer/dsl/swt/dnd_expression.rb +1 -1
  36. data/lib/glimmer/dsl/swt/dsl.rb +5 -4
  37. data/lib/glimmer/dsl/swt/exec_expression.rb +5 -5
  38. data/lib/glimmer/dsl/swt/expand_item_expression.rb +1 -1
  39. data/lib/glimmer/dsl/swt/file_dialog_expression.rb +1 -1
  40. data/lib/glimmer/dsl/swt/font_expression.rb +8 -6
  41. data/lib/glimmer/dsl/swt/image_expression.rb +1 -1
  42. data/lib/glimmer/dsl/swt/layout_data_expression.rb +1 -1
  43. data/lib/glimmer/dsl/swt/layout_expression.rb +1 -1
  44. data/lib/glimmer/dsl/swt/list_selection_data_binding_expression.rb +1 -1
  45. data/lib/glimmer/dsl/swt/menu_bar_expression.rb +1 -1
  46. data/lib/glimmer/dsl/swt/menu_expression.rb +1 -1
  47. data/lib/glimmer/dsl/swt/message_box_expression.rb +1 -1
  48. data/lib/glimmer/dsl/swt/observe_expression.rb +1 -1
  49. data/lib/glimmer/dsl/swt/property_expression.rb +4 -4
  50. data/lib/glimmer/dsl/swt/radio_group_selection_data_binding_expression.rb +1 -1
  51. data/lib/glimmer/dsl/swt/rgb_expression.rb +1 -1
  52. data/lib/glimmer/dsl/swt/rgba_expression.rb +1 -1
  53. data/lib/glimmer/dsl/swt/shape_expression.rb +56 -0
  54. data/lib/glimmer/dsl/swt/shell_expression.rb +1 -1
  55. data/lib/glimmer/dsl/swt/swt_expression.rb +1 -1
  56. data/lib/glimmer/dsl/swt/sync_exec_expression.rb +1 -1
  57. data/lib/glimmer/dsl/swt/tab_item_expression.rb +1 -1
  58. data/lib/glimmer/dsl/swt/table_items_data_binding_expression.rb +1 -1
  59. data/lib/glimmer/dsl/swt/tree_items_data_binding_expression.rb +1 -1
  60. data/lib/glimmer/dsl/swt/tree_properties_expression.rb +1 -1
  61. data/lib/glimmer/dsl/swt/widget_expression.rb +7 -3
  62. data/lib/glimmer/dsl/swt/widget_listener_expression.rb +1 -1
  63. data/lib/glimmer/launcher.rb +1 -1
  64. data/lib/glimmer/rake_task.rb +1 -1
  65. data/lib/glimmer/rake_task/list.rb +6 -6
  66. data/lib/glimmer/rake_task/package.rb +1 -1
  67. data/lib/glimmer/rake_task/scaffold.rb +33 -34
  68. data/lib/glimmer/swt/color_proxy.rb +1 -1
  69. data/lib/glimmer/swt/cursor_proxy.rb +1 -1
  70. data/lib/glimmer/swt/custom/animation.rb +243 -0
  71. data/lib/glimmer/swt/custom/checkbox_group.rb +1 -1
  72. data/lib/glimmer/swt/custom/code_text.rb +2 -1
  73. data/lib/glimmer/swt/custom/drawable.rb +43 -0
  74. data/lib/glimmer/swt/custom/radio_group.rb +1 -1
  75. data/lib/glimmer/swt/custom/shape.rb +193 -0
  76. data/lib/glimmer/swt/date_time_proxy.rb +1 -1
  77. data/lib/glimmer/swt/directory_dialog_proxy.rb +1 -1
  78. data/lib/glimmer/swt/display_proxy.rb +17 -1
  79. data/lib/glimmer/swt/dnd_proxy.rb +1 -1
  80. data/lib/glimmer/swt/expand_item_proxy.rb +1 -1
  81. data/lib/glimmer/swt/file_dialog_proxy.rb +1 -1
  82. data/lib/glimmer/swt/font_proxy.rb +4 -4
  83. data/lib/glimmer/swt/image_proxy.rb +1 -1
  84. data/lib/glimmer/swt/layout_data_proxy.rb +1 -1
  85. data/lib/glimmer/swt/layout_proxy.rb +1 -1
  86. data/lib/glimmer/swt/menu_proxy.rb +1 -1
  87. data/lib/glimmer/swt/message_box_proxy.rb +1 -1
  88. data/lib/glimmer/swt/packages.rb +1 -1
  89. data/lib/glimmer/swt/properties.rb +49 -0
  90. data/lib/glimmer/swt/sash_form_proxy.rb +1 -1
  91. data/lib/glimmer/swt/scrolled_composite_proxy.rb +1 -1
  92. data/lib/glimmer/swt/shell_proxy.rb +3 -2
  93. data/lib/glimmer/swt/style_constantizable.rb +1 -1
  94. data/lib/glimmer/swt/styled_text_proxy.rb +1 -1
  95. data/lib/glimmer/swt/swt_proxy.rb +1 -1
  96. data/lib/glimmer/swt/tab_item_proxy.rb +1 -1
  97. data/lib/glimmer/swt/table_column_proxy.rb +1 -1
  98. data/lib/glimmer/swt/table_proxy.rb +1 -1
  99. data/lib/glimmer/swt/tree_proxy.rb +26 -22
  100. data/lib/glimmer/swt/widget_listener_proxy.rb +1 -1
  101. data/lib/glimmer/swt/widget_proxy.rb +5 -23
  102. data/lib/glimmer/ui/custom_shell.rb +1 -1
  103. data/lib/glimmer/ui/custom_widget.rb +1 -1
  104. data/lib/glimmer/util/proc_tracker.rb +1 -1
  105. data/samples/elaborate/contact_manager.rb +1 -1
  106. data/samples/elaborate/contact_manager/contact.rb +1 -1
  107. data/samples/elaborate/contact_manager/contact_manager_presenter.rb +1 -1
  108. data/samples/elaborate/contact_manager/contact_repository.rb +1 -1
  109. data/samples/elaborate/login.rb +1 -1
  110. data/samples/elaborate/meta_sample.rb +3 -1
  111. data/samples/elaborate/tic_tac_toe.rb +2 -2
  112. data/samples/elaborate/tic_tac_toe/board.rb +1 -1
  113. data/samples/elaborate/tic_tac_toe/cell.rb +1 -1
  114. data/samples/elaborate/user_profile.rb +1 -1
  115. data/samples/hello/hello_browser.rb +1 -1
  116. data/samples/hello/hello_button.rb +1 -1
  117. data/samples/hello/hello_canvas.rb +63 -0
  118. data/samples/hello/hello_canvas_animation.rb +66 -0
  119. data/samples/hello/hello_checkbox.rb +1 -1
  120. data/samples/hello/hello_checkbox_group.rb +1 -1
  121. data/samples/hello/hello_combo.rb +1 -1
  122. data/samples/hello/hello_computed.rb +1 -1
  123. data/samples/hello/hello_computed/contact.rb +1 -1
  124. data/samples/hello/hello_custom_shell.rb +1 -1
  125. data/samples/hello/hello_custom_widget.rb +1 -1
  126. data/samples/hello/hello_date_time.rb +1 -1
  127. data/samples/hello/hello_dialog.rb +78 -0
  128. data/samples/hello/hello_directory_dialog.rb +1 -1
  129. data/samples/hello/hello_drag_and_drop.rb +1 -1
  130. data/samples/hello/hello_expand_bar.rb +1 -1
  131. data/samples/hello/hello_file_dialog.rb +1 -1
  132. data/samples/hello/hello_group.rb +1 -1
  133. data/samples/hello/hello_link.rb +1 -1
  134. data/samples/hello/hello_list_multi_selection.rb +1 -1
  135. data/samples/hello/hello_list_single_selection.rb +1 -1
  136. data/samples/hello/hello_menu_bar.rb +1 -1
  137. data/samples/hello/hello_message_box.rb +1 -1
  138. data/samples/hello/hello_pop_up_context_menu.rb +1 -1
  139. data/samples/hello/hello_radio.rb +1 -1
  140. data/samples/hello/hello_radio_group.rb +1 -1
  141. data/samples/hello/hello_sash_form.rb +1 -1
  142. data/samples/hello/hello_spinner.rb +1 -1
  143. data/samples/hello/hello_styled_text.rb +1 -1
  144. data/samples/hello/hello_tab.rb +1 -1
  145. data/samples/hello/hello_table.rb +1 -1
  146. data/samples/hello/hello_world.rb +1 -1
  147. metadata +37 -13
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2007-2020 Andy Maleh
1
+ # Copyright (c) 2007-2021 Andy Maleh
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining
4
4
  # a copy of this software and associated documentation files (the
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2007-2020 Andy Maleh
1
+ # Copyright (c) 2007-2021 Andy Maleh
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining
4
4
  # a copy of this software and associated documentation files (the
@@ -0,0 +1,243 @@
1
+ # Copyright (c) 2007-2021 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/swt/properties'
23
+
24
+ module Glimmer
25
+ module SWT
26
+ module Custom
27
+ # Represents an animation declaratively
28
+ class Animation
29
+ include Properties # TODO rename to Properties
30
+
31
+ class << self
32
+ def schedule_frame_animation(animation, &frame_animation_block)
33
+ frame_animation_queue(animation).prepend(frame_animation_block)
34
+ swt_display.async_exec do
35
+ frame_animation_queue(next_animation)&.pop&.call
36
+ end
37
+ end
38
+
39
+ def next_animation
40
+ animation = nil
41
+ while frame_animation_queues.values.reduce(:+)&.any? && (animation.nil? || frame_animation_queue(animation).last.nil?)
42
+ animation = frame_animation_queues.keys[next_animation_index]
43
+ frame_animation_queues.delete(animation) if frame_animation_queues.values.reduce(:+)&.any? && !animation.nil? && frame_animation_queue(animation).empty?
44
+ end
45
+ animation
46
+ end
47
+
48
+ def next_animation_index
49
+ next_schedule_index % frame_animation_queues.keys.size
50
+ end
51
+
52
+ def next_schedule_index
53
+ unless defined? @@next_schedule_index
54
+ @@next_schedule_index = 0
55
+ else
56
+ @@next_schedule_index += 1
57
+ end
58
+ end
59
+
60
+ def frame_animation_queues
61
+ unless defined? @@frame_animation_queues
62
+ @@frame_animation_queues = {}
63
+ end
64
+ @@frame_animation_queues
65
+ end
66
+
67
+ def frame_animation_queue(animation)
68
+ frame_animation_queues[animation] ||= []
69
+ end
70
+
71
+ def swt_display
72
+ unless defined? @@swt_display
73
+ @@swt_display = DisplayProxy.instance.swt_display
74
+ end
75
+ @@swt_display
76
+ end
77
+ end
78
+
79
+ attr_reader :parent, :options, :frame_index, :cycle
80
+ alias current_frame_index frame_index
81
+ attr_accessor :frame_block, :every, :cycle_count, :frame_count, :started, :duration_limit
82
+ alias started? started
83
+ # TODO consider supporting an async: false option
84
+
85
+ def initialize(parent)
86
+ @parent = parent
87
+ @started = true
88
+ @frame_index = 0
89
+ @cycle_count_index = 0
90
+ @start_number = 0 # denotes the number of starts (increments on every start)
91
+ self.class.swt_display # ensures initializing variable to set from GUI thread
92
+ end
93
+
94
+ def post_add_content
95
+ @parent.on_widget_disposed { stop }
96
+ start if started?
97
+ end
98
+
99
+ # Starts an animation that is indefinite or has never been started before (i.e. having `started: false` option).
100
+ # Otherwise, resumes a stopped animation that has not been completed.
101
+ def start
102
+ return if @start_number > 0 && started?
103
+ @start_number += 1
104
+ @started = true
105
+ @start_time = Time.now
106
+ @original_start_time = @start_time if @duration.nil?
107
+ # TODO track when finished in a variable for finite animations (whether by frame count, cycle count, or duration limit)
108
+ Thread.new do
109
+ start_number = @start_number
110
+ if cycle_count.is_a?(Integer) && cycle.is_a?(Array)
111
+ (cycle_count * cycle.length).times do
112
+ break unless draw_frame(start_number)
113
+ end
114
+ else
115
+ loop do
116
+ # this code has to be duplicated to break from a loop (break keyword only works when literally in a loop block)
117
+ break unless draw_frame(start_number)
118
+ end
119
+ end
120
+ end
121
+ end
122
+
123
+ def stop
124
+ return if stopped?
125
+ @started = false
126
+ @duration = (Time.now - @start_time) + @duration.to_f if duration_limited? && !@start_time.nil?
127
+ end
128
+
129
+ # Restarts an animation (whether indefinite or not and whether stopped or not)
130
+ def restart
131
+ @original_start_time = @start_time = nil
132
+ @duration = nil
133
+ @frame_index = 0
134
+ @cycle_count_index = 0
135
+ stop
136
+ start
137
+ end
138
+
139
+ def stopped?
140
+ !started?
141
+ end
142
+
143
+ def finite?
144
+ frame_count_limited? || cycle_limited? || duration_limited?
145
+ end
146
+
147
+ def infinite?
148
+ !finite?
149
+ end
150
+ alias indefinite? infinite?
151
+
152
+ def has_attribute?(attribute_name, *args)
153
+ respond_to?(ruby_attribute_setter(attribute_name)) && respond_to?(ruby_attribute_getter(attribute_name))
154
+ end
155
+
156
+ def set_attribute(attribute_name, *args)
157
+ send(ruby_attribute_setter(attribute_name), *args)
158
+ end
159
+
160
+ def get_attribute(attribute_name)
161
+ send(ruby_attribute_getter(attribute_name))
162
+ end
163
+
164
+ def cycle=(*args)
165
+ if args.size == 1
166
+ if args.first.is_a?(Array)
167
+ @cycle = args.first
168
+ else
169
+ @cycle = [args.first]
170
+ end
171
+ elsif args.size > 1
172
+ @cycle = args
173
+ end
174
+ end
175
+
176
+ def cycle_enabled?
177
+ @cycle.is_a?(Array)
178
+ end
179
+
180
+ def cycle_limited?
181
+ cycle_enabled? && @cycle_count.is_a?(Integer)
182
+ end
183
+
184
+ def duration_limited?
185
+ @duration_limit.is_a?(Integer)
186
+ end
187
+
188
+ def frame_count_limited?
189
+ @frame_count.is_a?(Integer)
190
+ end
191
+
192
+ def surpassed_duration_limit?
193
+ duration_limited? && ((Time.now - @start_time) > (@duration_limit - @duration.to_f))
194
+ end
195
+
196
+ def within_duration_limit?
197
+ !surpassed_duration_limit?
198
+ end
199
+
200
+ private
201
+
202
+ # Returns true on success of painting a frame and false otherwise
203
+ def draw_frame(start_number)
204
+ return false if stopped? ||
205
+ start_number != @start_number ||
206
+ (frame_count_limited? && @frame_index == @frame_count) ||
207
+ (cycle_limited? && @cycle_count_index == @cycle_count) ||
208
+ surpassed_duration_limit?
209
+ block_args = [@frame_index]
210
+ block_args << @cycle[@frame_index % @cycle.length] if cycle_enabled?
211
+ current_frame_index = @frame_index
212
+ current_cycle_count_index = @cycle_count_index
213
+ self.class.schedule_frame_animation(self) do
214
+ if started? && start_number == @start_number && within_duration_limit?
215
+ @parent.clear_shapes
216
+ @parent.content {
217
+ frame_block.call(*block_args)
218
+ }
219
+ @parent.redraw
220
+ else
221
+ if stopped? && @frame_index > current_frame_index
222
+ @started = false
223
+ @frame_index = current_frame_index
224
+ @cycle_count_index = current_cycle_count_index
225
+ end
226
+ end
227
+ end
228
+ @frame_index += 1
229
+ @cycle_count_index += 1 if cycle_limited? && (@frame_index % @cycle&.length&.to_i) == 0
230
+ sleep(every) if every.is_a?(Numeric)
231
+ true
232
+ rescue => e
233
+ Glimmer::Config.logger.error {e}
234
+ false
235
+ end
236
+
237
+ end
238
+
239
+ end
240
+
241
+ end
242
+
243
+ end
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2007-2020 Andy Maleh
1
+ # Copyright (c) 2007-2021 Andy Maleh
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining
4
4
  # a copy of this software and associated documentation files (the
@@ -61,11 +61,12 @@ module Glimmer
61
61
 
62
62
  before_body {
63
63
  @swt_style = swt_style == 0 ? [:border, :multi, :v_scroll, :h_scroll] : swt_style
64
+ @font_name = display.get_font_list(nil, true).map(&:name).include?('Consolas') ? 'Consolas' : 'Courier'
64
65
  }
65
66
 
66
67
  body {
67
68
  styled_text(swt_style) {
68
- font name: 'Consolas', height: 15
69
+ font name: @font_name, height: 15
69
70
  foreground rgb(75, 75, 75)
70
71
  left_margin 5
71
72
  top_margin 5
@@ -0,0 +1,43 @@
1
+ # Copyright (c) 2007-2021 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
+ module Glimmer
23
+ module SWT
24
+ module Custom
25
+ # Represents SWT drawable controls (widgets like canvas) and display
26
+ module Drawable
27
+ def shapes
28
+ @shapes ||= []
29
+ end
30
+
31
+ def clear_shapes
32
+ shapes.dup.each do |shape|
33
+ shape.paint_listener_proxy&.unregister
34
+ shapes.delete(shape)
35
+ end
36
+ end
37
+ end
38
+
39
+ end
40
+
41
+ end
42
+
43
+ end
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2007-2020 Andy Maleh
1
+ # Copyright (c) 2007-2021 Andy Maleh
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining
4
4
  # a copy of this software and associated documentation files (the
@@ -0,0 +1,193 @@
1
+ # Copyright (c) 2007-2021 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/swt/properties'
23
+ require 'glimmer/swt/swt_proxy'
24
+ require 'glimmer/swt/display_proxy'
25
+ require 'glimmer/swt/color_proxy'
26
+ require 'glimmer/swt/font_proxy'
27
+
28
+ module Glimmer
29
+ module SWT
30
+ module Custom
31
+ # Represents a shape (graphics) to be drawn on a control/widget/canvas/display
32
+ # swt_widget returns the parent (e.g. a `canvas` WidgetProxy), equivalent to `parent.swt_widget`
33
+ # That is because Shape is drawn on a parent as graphics and doesn't have an SWT widget for itself
34
+ class Shape
35
+ include Properties
36
+ # TODO support textExtent sized shapes nested within text/string
37
+ # TODO support a Pattern DSL for methods that take Pattern arguments
38
+
39
+ class << self
40
+ def valid?(parent, keyword, *args, &block)
41
+ gc_instance_methods.include?(method_name(keyword, args))
42
+ end
43
+
44
+ def gc_instance_methods
45
+ org.eclipse.swt.graphics.GC.instance_methods.map(&:to_s)
46
+ end
47
+
48
+ def arg_options(args, extract: false)
49
+ arg_options_method = extract ? :pop : :last
50
+ options = args.send(arg_options_method) if args.last.is_a?(Hash)
51
+ options.nil? ? {} : options.symbolize_keys
52
+ end
53
+
54
+ def method_name(keyword, args)
55
+ keyword = keyword.to_s
56
+ gradient = 'gradient_' if arg_options(args)[:gradient]
57
+ round = 'round_' if arg_options(args)[:round]
58
+ gc_instance_method_name_prefix = !['polyline', 'point', 'image', 'focus'].include?(keyword) && (arg_options(args)[:fill] || arg_options(args)[:gradient]) ? 'fill_' : 'draw_'
59
+ "#{gc_instance_method_name_prefix}#{gradient}#{round}#{keyword}"
60
+ end
61
+ end
62
+
63
+ attr_reader :parent, :name, :args, :options, :swt_widget, :paint_listener_proxy
64
+
65
+ def initialize(parent, keyword, *args, &property_block)
66
+ @parent = parent
67
+ @name = keyword
68
+ @method_name = self.class.method_name(keyword, args)
69
+ @options = self.class.arg_options(args, extract: true)
70
+ @args = args
71
+ @swt_widget = parent.respond_to?(:swt_display) ? parent.swt_display : parent.swt_widget
72
+ @properties = {}
73
+ @parent.shapes << self
74
+ post_add_content if property_block.nil?
75
+ end
76
+
77
+ def draw?
78
+ !fill?
79
+ end
80
+
81
+ def fill?
82
+ @options[:fill]
83
+ end
84
+
85
+ def gradient?
86
+ @options[:gradient]
87
+ end
88
+
89
+ def round?
90
+ @options[:round]
91
+ end
92
+
93
+ def post_add_content
94
+ event_handler = lambda do |event|
95
+ @properties['background'] = [@parent.background] if fill? && !@properties.keys.map(&:to_s).include?('background')
96
+ @properties['foreground'] = [@parent.foreground] if draw? && !@properties.keys.map(&:to_s).include?('foreground')
97
+ @properties.each do |property, args|
98
+ method_name = attribute_setter(property)
99
+ apply_property_arg_conversions(method_name, args)
100
+ event.gc.send(method_name, *args)
101
+ end
102
+ apply_shape_arg_conversions(@method_name, @args)
103
+ apply_shape_arg_defaults(@method_name, @args)
104
+ tolerate_shape_extra_args(@method_name, @args)
105
+ event.gc.send(@method_name, *@args)
106
+ end
107
+ if parent.respond_to?(:swt_display)
108
+ @paint_listener_proxy = @parent.on_swt_paint(&event_handler)
109
+ else
110
+ @paint_listener_proxy = @parent.on_paint_control(&event_handler)
111
+ end
112
+ end
113
+
114
+ def apply_property_arg_conversions(method_name, args)
115
+ the_java_method = org.eclipse.swt.graphics.GC.java_class.declared_instance_methods.detect {|m| m.name == method_name}
116
+ if (args.first.is_a?(Symbol) || args.first.is_a?(String))
117
+ if the_java_method.parameter_types.first == Color.java_class
118
+ args[0] = ColorProxy.new(args[0])
119
+ end
120
+ if the_java_method.parameter_types.first == Java::int.java_class
121
+ args[0] = SWTProxy.constant(args[0])
122
+ end
123
+ end
124
+ if args.first.is_a?(ColorProxy)
125
+ args[0] = args[0].swt_color
126
+ end
127
+ if args.first.is_a?(Hash) && the_java_method.parameter_types.first == Font.java_class
128
+ args[0] = FontProxy.new(args[0])
129
+ end
130
+ if args.first.is_a?(FontProxy)
131
+ args[0] = args[0].swt_font
132
+ end
133
+ if ['setBackgroundPattern', 'setForegroundPattern'].include?(method_name.to_s)
134
+ args.each_with_index do |arg, i|
135
+ if arg.is_a?(Symbol) || arg.is_a?(String)
136
+ args[i] = ColorProxy.new(arg).swt_color
137
+ elsif arg.is_a?(ColorProxy)
138
+ args[i] = arg.swt_color
139
+ end
140
+ end
141
+ new_args = [DisplayProxy.instance.swt_display] + args
142
+ args[0] = org.eclipse.swt.graphics.Pattern.new(*new_args)
143
+ args[1..-1] = []
144
+ end
145
+ end
146
+
147
+ def apply_shape_arg_conversions(method_name, args)
148
+ if args.size > 1 && (method_name.include?('polygon') || method_name.include?('polyline'))
149
+ args[0] = args.dup
150
+ args[1..-1] = []
151
+ end
152
+ end
153
+
154
+ def apply_shape_arg_defaults(method_name, args)
155
+ if method_name.include?('round_rectangle') && args.size.between?(4, 5)
156
+ (6 - args.size).times {args << 60}
157
+ elsif method_name.include?('rectangle') && gradient? && args.size == 4
158
+ args << true
159
+ elsif (method_name.include?('text') || method_name.include?('string')) && !@properties.keys.map(&:to_s).include?('background') && args.size == 3
160
+ args << true
161
+ end
162
+ end
163
+
164
+ # Tolerates shape extra args added by user by mistake
165
+ # (e.g. happens when switching from round rectangle to a standard one without removing all extra args)
166
+ def tolerate_shape_extra_args(method_name, args)
167
+ the_java_method_arg_count = org.eclipse.swt.graphics.GC.java_class.declared_instance_methods.select do |m|
168
+ m.name == method_name.camelcase(:lower)
169
+ end.map(&:parameter_types).map(&:size).max
170
+ if args.size > the_java_method_arg_count
171
+ args[the_java_method_arg_count..-1] = []
172
+ end
173
+ end
174
+
175
+ def has_attribute?(attribute_name, *args)
176
+ self.class.gc_instance_methods.include?(attribute_setter(attribute_name))
177
+ end
178
+
179
+ def set_attribute(attribute_name, *args)
180
+ @properties[attribute_name] = args
181
+ end
182
+
183
+ def get_attribute(attribute_name)
184
+ @properties.symbolize_keys[attribute_name.to_s.to_sym]
185
+ end
186
+
187
+ end
188
+
189
+ end
190
+
191
+ end
192
+
193
+ end