glimmer-dsl-opal 0.0.9 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (131) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +50 -0
  3. data/README.md +999 -177
  4. data/VERSION +1 -1
  5. data/lib/glimmer-dsl-opal.rb +49 -10
  6. data/lib/glimmer-dsl-opal/ext/date.rb +13 -0
  7. data/lib/glimmer-dsl-opal/ext/exception.rb +5 -0
  8. data/lib/{samples → glimmer-dsl-opal/samples}/elaborate/contact_manager.rb +1 -1
  9. data/lib/{samples → glimmer-dsl-opal/samples}/elaborate/contact_manager/contact.rb +0 -0
  10. data/lib/{samples → glimmer-dsl-opal/samples}/elaborate/contact_manager/contact_manager_presenter.rb +0 -0
  11. data/lib/{samples → glimmer-dsl-opal/samples}/elaborate/contact_manager/contact_repository.rb +24 -99
  12. data/lib/{samples → glimmer-dsl-opal/samples}/elaborate/login.rb +0 -0
  13. data/lib/{samples → glimmer-dsl-opal/samples}/elaborate/tic_tac_toe.rb +0 -0
  14. data/lib/{samples → glimmer-dsl-opal/samples}/elaborate/tic_tac_toe/board.rb +0 -0
  15. data/lib/{samples → glimmer-dsl-opal/samples}/elaborate/tic_tac_toe/cell.rb +0 -0
  16. data/lib/{samples → glimmer-dsl-opal/samples}/hello/hello_browser.rb +0 -0
  17. data/lib/glimmer-dsl-opal/samples/hello/hello_checkbox.rb +85 -0
  18. data/lib/glimmer-dsl-opal/samples/hello/hello_checkbox_group.rb +68 -0
  19. data/lib/glimmer-dsl-opal/samples/hello/hello_combo.rb +63 -0
  20. data/lib/{samples → glimmer-dsl-opal/samples}/hello/hello_computed.rb +19 -19
  21. data/lib/{samples → glimmer-dsl-opal/samples}/hello/hello_computed/contact.rb +0 -0
  22. data/lib/glimmer-dsl-opal/samples/hello/hello_custom_shell.rb +155 -0
  23. data/lib/glimmer-dsl-opal/samples/hello/hello_custom_widget.rb +86 -0
  24. data/lib/glimmer-dsl-opal/samples/hello/hello_group.rb +104 -0
  25. data/lib/{samples → glimmer-dsl-opal/samples}/hello/hello_list_multi_selection.rb +0 -0
  26. data/lib/{samples → glimmer-dsl-opal/samples}/hello/hello_list_single_selection.rb +0 -0
  27. data/lib/glimmer-dsl-opal/samples/hello/hello_radio.rb +108 -0
  28. data/lib/glimmer-dsl-opal/samples/hello/hello_radio_group.rb +84 -0
  29. data/lib/glimmer-dsl-opal/samples/hello/hello_tab.rb +50 -0
  30. data/lib/glimmer-dsl-opal/samples/hello/hello_world.rb +29 -0
  31. data/lib/glimmer-dsl-opal/vendor/jquery.js +2 -0
  32. data/lib/glimmer-dsl-swt.rb +37 -0
  33. data/lib/glimmer/data_binding/element_binding.rb +1 -0
  34. data/lib/glimmer/data_binding/ext/observable_model.rb +5 -5
  35. data/lib/glimmer/data_binding/list_selection_binding.rb +1 -1
  36. data/lib/glimmer/data_binding/table_items_binding.rb +3 -3
  37. data/lib/glimmer/dsl/opal/async_exec_expression.rb +23 -7
  38. data/lib/glimmer/dsl/opal/checkbox_group_selection_data_binding_expression.rb +61 -0
  39. data/lib/glimmer/dsl/opal/color_expression.rb +38 -0
  40. data/lib/glimmer/dsl/opal/column_properties_expression.rb +2 -2
  41. data/lib/glimmer/dsl/opal/combo_selection_data_binding_expression.rb +2 -2
  42. data/lib/glimmer/dsl/opal/custom_widget_expression.rb +94 -0
  43. data/lib/glimmer/dsl/opal/display_expression.rb +40 -0
  44. data/lib/glimmer/dsl/opal/dsl.rb +18 -16
  45. data/lib/glimmer/dsl/opal/exec_expression.rb +55 -0
  46. data/lib/glimmer/dsl/opal/font_expression.rb +47 -0
  47. data/lib/glimmer/dsl/opal/layout_data_expression.rb +2 -2
  48. data/lib/glimmer/dsl/opal/layout_expression.rb +22 -0
  49. data/lib/glimmer/dsl/opal/list_selection_data_binding_expression.rb +2 -3
  50. data/lib/glimmer/dsl/opal/message_box_expression.rb +2 -2
  51. data/lib/glimmer/dsl/opal/property_expression.rb +6 -2
  52. data/lib/glimmer/dsl/opal/radio_group_selection_data_binding_expression.rb +61 -0
  53. data/lib/glimmer/dsl/opal/rgb_expression.rb +32 -0
  54. data/lib/glimmer/dsl/opal/rgba_expression.rb +32 -0
  55. data/lib/glimmer/dsl/opal/shell_expression.rb +20 -3
  56. data/lib/glimmer/dsl/opal/swt_expression.rb +46 -0
  57. data/lib/glimmer/dsl/opal/sync_exec_expression.rb +33 -0
  58. data/lib/glimmer/dsl/opal/table_expression.rb +2 -2
  59. data/lib/glimmer/dsl/opal/table_items_data_binding_expression.rb +2 -2
  60. data/lib/glimmer/dsl/opal/widget_expression.rb +24 -0
  61. data/lib/glimmer/dsl/opal/widget_listener_expression.rb +16 -3
  62. data/lib/glimmer/swt.rb +499 -0
  63. data/lib/glimmer/swt/browser_proxy.rb +27 -0
  64. data/lib/glimmer/swt/button_proxy.rb +54 -0
  65. data/lib/glimmer/swt/checkbox_proxy.rb +80 -0
  66. data/lib/glimmer/swt/color_proxy.rb +119 -0
  67. data/lib/glimmer/{opal/select_proxy.rb → swt/combo_proxy.rb} +23 -18
  68. data/lib/glimmer/swt/composite_proxy.rb +31 -0
  69. data/lib/glimmer/swt/custom/checkbox_group.rb +142 -0
  70. data/lib/glimmer/swt/custom/radio_group.rb +143 -0
  71. data/lib/glimmer/swt/display_proxy.rb +79 -0
  72. data/lib/glimmer/{opal → swt}/event_listener_proxy.rb +1 -1
  73. data/lib/glimmer/swt/fill_layout_proxy.rb +84 -0
  74. data/lib/glimmer/swt/font_proxy.rb +79 -0
  75. data/lib/glimmer/swt/grid_layout_proxy.rb +82 -0
  76. data/lib/glimmer/swt/group_proxy.rb +38 -0
  77. data/lib/glimmer/swt/label_proxy.rb +54 -0
  78. data/lib/glimmer/swt/layout_data_proxy.rb +105 -0
  79. data/lib/glimmer/swt/layout_proxy.rb +63 -0
  80. data/lib/glimmer/{opal → swt}/list_proxy.rb +30 -22
  81. data/lib/glimmer/swt/make_shift_shell_proxy.rb +38 -0
  82. data/lib/glimmer/swt/message_box_proxy.rb +143 -0
  83. data/lib/glimmer/{opal → swt}/point.rb +1 -1
  84. data/lib/glimmer/{opal → swt}/property_owner.rb +1 -1
  85. data/lib/glimmer/swt/radio_proxy.rb +81 -0
  86. data/lib/glimmer/swt/row_layout_proxy.rb +128 -0
  87. data/lib/glimmer/swt/scrolled_composite_proxy.rb +20 -0
  88. data/lib/glimmer/swt/shell_proxy.rb +260 -0
  89. data/lib/glimmer/swt/style_constantizable.rb +154 -0
  90. data/lib/glimmer/swt/styled_text_proxy.rb +44 -0
  91. data/lib/glimmer/swt/swt_proxy.rb +53 -0
  92. data/lib/glimmer/{opal/tab_folder.rb → swt/tab_folder_proxy.rb} +20 -21
  93. data/lib/glimmer/swt/tab_item_proxy.rb +84 -0
  94. data/lib/glimmer/{opal/table_column.rb → swt/table_column_proxy.rb} +13 -7
  95. data/lib/glimmer/{opal/table_item.rb → swt/table_item_proxy.rb} +59 -48
  96. data/lib/glimmer/{opal → swt}/table_proxy.rb +66 -56
  97. data/lib/glimmer/swt/text_proxy.rb +46 -0
  98. data/lib/glimmer/swt/widget_proxy.rb +511 -0
  99. data/lib/glimmer/ui/custom_shell.rb +92 -0
  100. data/lib/glimmer/ui/custom_widget.rb +292 -0
  101. data/lib/glimmer/util/proc_tracker.rb +39 -0
  102. data/lib/net/http.rb +17 -0
  103. data/lib/uri.rb +64 -0
  104. metadata +142 -67
  105. data/lib/glimmer/dsl/opal/browser_expression.rb +0 -17
  106. data/lib/glimmer/dsl/opal/button_expression.rb +0 -18
  107. data/lib/glimmer/dsl/opal/combo_expression.rb +0 -17
  108. data/lib/glimmer/dsl/opal/composite_expression.rb +0 -17
  109. data/lib/glimmer/dsl/opal/grid_layout_expression.rb +0 -17
  110. data/lib/glimmer/dsl/opal/label_expression.rb +0 -17
  111. data/lib/glimmer/dsl/opal/list_expression.rb +0 -17
  112. data/lib/glimmer/dsl/opal/tab_folder_expression.rb +0 -17
  113. data/lib/glimmer/dsl/opal/tab_item_expression.rb +0 -17
  114. data/lib/glimmer/dsl/opal/text_expression.rb +0 -22
  115. data/lib/glimmer/opal/display_proxy.rb +0 -23
  116. data/lib/glimmer/opal/div_proxy.rb +0 -29
  117. data/lib/glimmer/opal/document_proxy.rb +0 -187
  118. data/lib/glimmer/opal/element_proxy.rb +0 -304
  119. data/lib/glimmer/opal/grid_layout_proxy.rb +0 -54
  120. data/lib/glimmer/opal/iframe_proxy.rb +0 -23
  121. data/lib/glimmer/opal/input_proxy.rb +0 -45
  122. data/lib/glimmer/opal/label_proxy.rb +0 -25
  123. data/lib/glimmer/opal/layout_data_proxy.rb +0 -52
  124. data/lib/glimmer/opal/modal.rb +0 -94
  125. data/lib/glimmer/opal/tab_item.rb +0 -98
  126. data/lib/samples/elaborate/launch +0 -6
  127. data/lib/samples/hello/hello_combo.rb +0 -34
  128. data/lib/samples/hello/hello_tab.rb +0 -24
  129. data/lib/samples/hello/hello_world.rb +0 -8
  130. data/lib/samples/hello/launch +0 -10
  131. data/lib/samples/launch +0 -4
@@ -0,0 +1,27 @@
1
+ require 'glimmer/swt/widget_proxy'
2
+
3
+ module Glimmer
4
+ module SWT
5
+ class BrowserProxy < WidgetProxy
6
+ attr_reader :url
7
+
8
+ def url=(value)
9
+ @url = value
10
+ dom_element.attr('src', @url)
11
+ end
12
+
13
+ def element
14
+ 'iframe'
15
+ end
16
+
17
+ def dom
18
+ iframe_id = id
19
+ iframe_url = url
20
+ @dom ||= html {
21
+ iframe(id: iframe_id, class: name, src: iframe_url, frameBorder: 0) {
22
+ }
23
+ }.to_s
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,54 @@
1
+ require 'glimmer/swt/widget_proxy'
2
+ require 'glimmer/swt/radio_proxy'
3
+ require 'glimmer/swt/checkbox_proxy'
4
+
5
+ module Glimmer
6
+ module SWT
7
+ class ButtonProxy < WidgetProxy
8
+ class << self
9
+ def create(parent, args)
10
+ if args.to_a.include?(:radio)
11
+ RadioProxy.new(parent, args)
12
+ elsif args.to_a.include?(:check)
13
+ CheckboxProxy.new(parent, args)
14
+ else
15
+ new(parent, args)
16
+ end
17
+ end
18
+ end
19
+
20
+ attr_reader :text
21
+
22
+ def text=(value)
23
+ @text = value
24
+ dom_element.html(@text)
25
+ end
26
+
27
+ def element
28
+ 'button'
29
+ end
30
+
31
+ def observation_request_to_event_mapping
32
+ {
33
+ 'on_widget_selected' => {
34
+ event: 'click'
35
+ },
36
+ }
37
+ end
38
+
39
+ def dom
40
+ input_text = @text
41
+ input_id = id
42
+ input_style = css
43
+ input_args = {}
44
+ input_disabled = @enabled ? {} : {'disabled': 'disabled'}
45
+ input_args = input_args.merge(type: 'password') if has_style?(:password)
46
+ @dom ||= html {
47
+ button(input_args.merge(id: input_id, class: name, style: input_style, style: 'min-width: 27px; min-height: 27px;').merge(input_disabled)) {
48
+ input_text.to_s == '' ? '&nbsp;' : input_text
49
+ }
50
+ }.to_s
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,80 @@
1
+ require 'glimmer/swt/widget_proxy'
2
+
3
+ module Glimmer
4
+ module SWT
5
+ class CheckboxProxy < WidgetProxy
6
+ STYLE=<<~CSS
7
+ .checkbox {
8
+ display: inline;
9
+ }
10
+ .checkbox-label {
11
+ display: inline;
12
+ }
13
+ CSS
14
+ # TODO consider reuse of logic in Radioproxy
15
+ attr_reader :text
16
+
17
+ def text=(value)
18
+ @text = value
19
+ dom_element.val(@text)
20
+ label_dom_element.html(@text)
21
+ end
22
+
23
+ def selection
24
+ dom_element.prop('checked')
25
+ end
26
+
27
+ def selection=(value)
28
+ @selection = value
29
+ dom_element.prop('checked', @selection)
30
+ end
31
+
32
+ def element
33
+ 'input'
34
+ end
35
+
36
+ def observation_request_to_event_mapping
37
+ {
38
+ 'on_widget_selected' => {
39
+ event: 'change'
40
+ },
41
+ }
42
+ end
43
+
44
+ def label_id
45
+ "#{id}-label"
46
+ end
47
+
48
+ def label_class
49
+ "#{name}-label"
50
+ end
51
+
52
+ def label_dom_element
53
+ Element.find("##{label_id}")
54
+ end
55
+
56
+ def dom
57
+ check_text = @text
58
+ check_id = id
59
+ check_style = css
60
+ check_class = name
61
+ check_selection = @selection
62
+ options = {type: 'checkbox', id: check_id, name: parent.id, style: check_style, class: check_class, value: check_text, style: 'min-width: 27px;'}
63
+ options[checked: 'checked'] if check_selection
64
+ @dom ||= html {
65
+ span {
66
+ input(options) {
67
+ }
68
+ label(id: label_id, class: label_class, for: check_id) {
69
+ check_text
70
+ }
71
+ }
72
+ }.to_s
73
+ end
74
+
75
+ end
76
+
77
+ CheckProxy = CheckboxProxy # alias
78
+ end
79
+
80
+ end
@@ -0,0 +1,119 @@
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
+ module Glimmer
23
+ module SWT
24
+ # Proxy for org.eclipse.swt.graphics.Color
25
+ #
26
+ # Invoking `#swt_color` returns the SWT Color object wrapped by this proxy
27
+ #
28
+ # Follows the Proxy Design Pattern
29
+ class ColorProxy
30
+ SWT_COLOR_TRANSLATION = {
31
+ "widget_foreground" => [0, 0, 0],
32
+ "blue" => [0, 0, 255],
33
+ "widget_dark_shadow" => [0, 0, 0],
34
+ "title_foreground" => [0, 0, 0],
35
+ "yellow" => [255, 255, 0],
36
+ "widget_highlight_shadow" => [255, 255, 255],
37
+ "dark_cyan" => [0, 128, 128],
38
+ "list_foreground" => [0, 0, 0],
39
+ "dark_blue" => [0, 0, 128],
40
+ "dark_yellow" => [128, 128, 0],
41
+ "cyan" => [0, 255, 255],
42
+ "info_background" => [236, 235, 236],
43
+ "link_foreground" => [0, 104, 218],
44
+ "title_inactive_foreground" => [0, 0, 0],
45
+ "title_background_gradient" => [179, 215, 255],
46
+ "red" => [255, 0, 0],
47
+ "title_inactive_background_gradient" => [220, 220, 220],
48
+ "transparent" => [255, 255, 255],
49
+ "widget_light_shadow" => [232, 232, 232],
50
+ "dark_magenta" => [128, 0, 128],
51
+ "white" => [255, 255, 255],
52
+ "list_selection" => [179, 215, 255],
53
+ "gray" => [192, 192, 192],
54
+ "widget_border" => [0, 0, 0],
55
+ "widget_background" => [236, 236, 236],
56
+ "info_foreground" => [0, 0, 0],
57
+ "title_inactive_background" => [220, 220, 220],
58
+ "widget_disabled_foreground" => [220, 220, 220],
59
+ "list_background" => [255, 255, 255],
60
+ "magenta" => [255, 0, 255],
61
+ "title_background" => [0, 99, 225],
62
+ "text_disabled_background" => [255, 255, 255],
63
+ "black" => [0, 0, 0],
64
+ "dark_gray" => [128, 128, 128],
65
+ "list_selection_text" => [0, 0, 0],
66
+ "dark_red" => [128, 0, 0],
67
+ "widget_normal_shadow" => [159, 159, 159],
68
+ "dark_green" => [0, 128, 0],
69
+ "green" => [0, 255, 0]
70
+ }
71
+
72
+ attr_reader :args, :red, :green, :blue, :alpha
73
+
74
+ # Initializes a proxy for an SWT Color object
75
+ #
76
+ # Takes a standard color single argument, rgba 3 args, or rgba 4 args
77
+ #
78
+ # A standard color is a string/symbol representing one of the
79
+ # SWT.COLOR_*** constants like SWT.COLOR_RED, but in underscored string
80
+ # format (e.g :color_red).
81
+ # Glimmer can also accept standard color names without the color_ prefix,
82
+ # and it will automatically figure out the SWT.COLOR_*** constant
83
+ # (e.g. :red)
84
+ #
85
+ # rgb is 3 arguments representing Red, Green, Blue numeric values
86
+ #
87
+ # rgba is 4 arguments representing Red, Green, Blue, and Alpha numeric values
88
+ #
89
+ def initialize(*args)
90
+ @args = args
91
+ case @args.size
92
+ when 1
93
+ @alpha = nil
94
+ if @args.first.is_a?(String) || @args.first.is_a?(Symbol)
95
+ standard_color = @args.first.to_s.downcase.sub('COLOR_', '')
96
+ @red, @green, @blue = SWT_COLOR_TRANSLATION[standard_color]
97
+ else
98
+ @red, @green, @blue = [0, 0, 0]
99
+ end
100
+ when 3..4
101
+ @red, @green, @blue, @alpha = @args
102
+ end
103
+ end
104
+
105
+ def to_css
106
+ if alpha.nil?
107
+ "rgb(#{red}, #{green}, #{blue})"
108
+ else
109
+ "rgba(#{red}, #{green}, #{blue}, #{alpha_css})"
110
+ end
111
+ end
112
+
113
+ def alpha_css
114
+ alpha.to_f / 255
115
+ end
116
+
117
+ end
118
+ end
119
+ end
@@ -1,10 +1,9 @@
1
1
  require 'glimmer/data_binding/observable_element'
2
- require 'glimmer/opal/event_listener_proxy'
3
- require 'glimmer/opal/element_proxy'
2
+ require 'glimmer/swt/widget_proxy'
4
3
 
5
4
  module Glimmer
6
- module Opal
7
- class SelectProxy < ElementProxy
5
+ module SWT
6
+ class ComboProxy < WidgetProxy
8
7
  include Glimmer::DataBinding::ObservableElement
9
8
  attr_reader :text, :items
10
9
 
@@ -13,14 +12,27 @@ module Glimmer
13
12
  @items = []
14
13
  end
15
14
 
15
+ def element
16
+ 'select'
17
+ end
18
+
16
19
  def text=(value)
17
20
  @text = value
18
- redraw
21
+ Document.find(path).value = value
19
22
  end
20
23
 
21
24
  def items=(the_items)
22
25
  @items = the_items
23
- redraw
26
+ items_dom = items.to_a.map do |item|
27
+ option_hash = {value: item}
28
+ option_hash[:selected] = 'selected' if @text == item
29
+ html {
30
+ option(option_hash) {
31
+ item
32
+ }
33
+ }.to_s
34
+ end
35
+ dom_element.html(items_dom)
24
36
  end
25
37
 
26
38
  def observation_request_to_event_mapping
@@ -30,7 +42,7 @@ module Glimmer
30
42
  event_handler: -> (event_listener) {
31
43
  -> (event) {
32
44
  @text = event.target.value
33
- event_listener.call(event)
45
+ event_listener.call(event)
34
46
  }
35
47
  }
36
48
  }
@@ -38,21 +50,14 @@ module Glimmer
38
50
  end
39
51
 
40
52
  def dom
41
- select_text = @text
42
53
  items = @items
43
54
  select_id = id
44
55
  select_style = css
45
- @dom ||= DOM {
46
- select(id: select_id, style: select_style) {
47
- items.to_a.each do |item|
48
- option_hash = {value: item}
49
- option_hash[:selected] = 'selected' if select_text == item
50
- option(option_hash) {
51
- item
52
- }
53
- end
56
+ select_class = name
57
+ @dom ||= html {
58
+ select(id: select_id, class: select_class, style: select_style) {
54
59
  }
55
- }
60
+ }.to_s
56
61
  end
57
62
  end
58
63
  end
@@ -0,0 +1,31 @@
1
+ require 'glimmer/swt/grid_layout_proxy'
2
+ require 'glimmer/swt/widget_proxy'
3
+
4
+ module Glimmer
5
+ module SWT
6
+ class CompositeProxy < WidgetProxy
7
+ attr_reader :layout
8
+
9
+ def initialize(parent, args)
10
+ super(parent, args)
11
+ @layout = GridLayoutProxy.new(self, [])
12
+ end
13
+
14
+ def dom
15
+ div_id = id
16
+ div_style = css
17
+ div_class = name
18
+ @dom ||= html {
19
+ div(id: div_id, class: div_class, style: div_style)
20
+ }.to_s
21
+ end
22
+
23
+ def layout=(the_layout)
24
+ @layout = the_layout
25
+ end
26
+
27
+ end
28
+
29
+ end
30
+
31
+ end
@@ -0,0 +1,142 @@
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/ui/custom_widget'
23
+
24
+ module Glimmer
25
+ module SWT
26
+ module Custom
27
+ # A custom widget rendering a group of checkboxes generated via data-binding
28
+ class CheckboxGroup
29
+ include Glimmer::UI::CustomWidget
30
+
31
+ body {
32
+ composite # just an empty composite to hold checkboxs upon data-binding `selection`
33
+ }
34
+
35
+ def items=(text_array)
36
+ selection_value = selection
37
+ @items = Array[*text_array]
38
+ build_checkboxes
39
+ end
40
+
41
+ def items
42
+ @items || []
43
+ end
44
+
45
+ def selection=(selection_texts)
46
+ items.count.times do |index|
47
+ checkbox = checkboxes[index]
48
+ item = items[index]
49
+ checkbox_text = checkbox&.text
50
+ checkbox.selection = selection_texts.to_a.include?(checkbox_text)
51
+ end
52
+ selection_texts
53
+ end
54
+
55
+ def selection
56
+ selection_indices.map do |selection_index|
57
+ checkboxes[selection_index]&.text
58
+ end
59
+ end
60
+
61
+ def selection_indices=(indices)
62
+ self.selection=(indices.to_a.map {|index| items[index]})
63
+ end
64
+ alias select selection_indices=
65
+
66
+ def selection_indices
67
+ checkboxes.each_with_index.map do |checkbox, index|
68
+ index if checkbox.selection
69
+ end.to_a.compact
70
+ end
71
+
72
+ def checkboxes
73
+ @checkboxes ||= []
74
+ end
75
+ alias checks checkboxes
76
+
77
+ def can_handle_observation_request?(observation_request)
78
+ checkboxes.first&.can_handle_observation_request?(observation_request) || super(observation_request)
79
+ end
80
+
81
+ def handle_observation_request(observation_request, &block)
82
+ observation_requests << [observation_request, block]
83
+ delegate_observation_request_to_checkboxes(observation_request, &block)
84
+ super
85
+ end
86
+
87
+ def delegate_observation_request_to_checkboxes(observation_request, &block)
88
+ if observation_request != 'on_widget_disposed'
89
+ checkboxes.count.times do |index|
90
+ checkbox = checkboxes[index]
91
+ checkbox.handle_observation_request(observation_request, &block) if checkbox.can_handle_observation_request?(observation_request)
92
+ end
93
+ end
94
+ end
95
+
96
+ def observation_requests
97
+ @observation_requests ||= Set.new
98
+ end
99
+
100
+ def has_attribute?(attribute_name, *args)
101
+ @checkboxes.to_a.map do |widget_proxy|
102
+ return true if widget_proxy.has_attribute?(attribute_name, *args)
103
+ end
104
+ super
105
+ end
106
+
107
+ def set_attribute(attribute_name, *args)
108
+ excluded_attributes = ['selection']
109
+ unless excluded_attributes.include?(attribute_name.to_s)
110
+ @checkboxes.to_a.each do |widget_proxy|
111
+ widget_proxy.set_attribute(attribute_name, *args) if widget_proxy.has_attribute?(attribute_name, *args)
112
+ end
113
+ end
114
+ super
115
+ end
116
+
117
+ private
118
+
119
+ def build_checkboxes
120
+ current_selection = selection
121
+ @checkboxes = []
122
+ items.each do |item|
123
+ body_root.content {
124
+ checkboxes << checkbox { |checkbox_proxy|
125
+ text item
126
+ on_widget_selected {
127
+ self.selection_indices = checkboxes.each_with_index.map {|cb, i| i if cb.selection}.to_a.compact
128
+ }
129
+ }
130
+ }
131
+ end
132
+ observation_requests.to_a.each do |observation_request, block|
133
+ delegate_observation_request_to_checkboxes(observation_request, &block)
134
+ end
135
+ self.selection = current_selection
136
+ end
137
+ end
138
+
139
+ CheckGroup = CheckboxGroup # alias
140
+ end
141
+ end
142
+ end