glimmer-dsl-opal 0.1.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (97) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +48 -0
  3. data/README.md +948 -169
  4. data/VERSION +1 -1
  5. data/lib/glimmer-dsl-opal.rb +31 -7
  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 +0 -0
  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 +0 -0
  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 +1 -1
  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/{jquery.js → glimmer-dsl-opal/vendor/jquery.js} +0 -0
  32. data/lib/glimmer-dsl-swt.rb +37 -0
  33. data/lib/glimmer/data_binding/element_binding.rb +2 -1
  34. data/lib/glimmer/data_binding/ext/observable_model.rb +1 -1
  35. data/lib/glimmer/dsl/opal/async_exec_expression.rb +23 -7
  36. data/lib/glimmer/dsl/opal/checkbox_group_selection_data_binding_expression.rb +61 -0
  37. data/lib/glimmer/dsl/opal/color_expression.rb +38 -0
  38. data/lib/glimmer/dsl/opal/custom_widget_expression.rb +94 -0
  39. data/lib/glimmer/dsl/opal/display_expression.rb +40 -0
  40. data/lib/glimmer/dsl/opal/dsl.rb +14 -0
  41. data/lib/glimmer/dsl/opal/exec_expression.rb +55 -0
  42. data/lib/glimmer/dsl/opal/font_expression.rb +47 -0
  43. data/lib/glimmer/dsl/opal/layout_expression.rb +1 -1
  44. data/lib/glimmer/dsl/opal/property_expression.rb +6 -2
  45. data/lib/glimmer/dsl/opal/radio_group_selection_data_binding_expression.rb +61 -0
  46. data/lib/glimmer/dsl/opal/rgb_expression.rb +32 -0
  47. data/lib/glimmer/dsl/opal/rgba_expression.rb +32 -0
  48. data/lib/glimmer/dsl/opal/shell_expression.rb +19 -2
  49. data/lib/glimmer/dsl/opal/swt_expression.rb +46 -0
  50. data/lib/glimmer/dsl/opal/sync_exec_expression.rb +33 -0
  51. data/lib/glimmer/dsl/opal/widget_expression.rb +2 -1
  52. data/lib/glimmer/dsl/opal/widget_listener_expression.rb +16 -3
  53. data/lib/glimmer/swt.rb +499 -0
  54. data/lib/glimmer/swt/browser_proxy.rb +1 -1
  55. data/lib/glimmer/swt/button_proxy.rb +17 -3
  56. data/lib/glimmer/swt/checkbox_proxy.rb +80 -0
  57. data/lib/glimmer/swt/color_proxy.rb +119 -0
  58. data/lib/glimmer/swt/combo_proxy.rb +13 -12
  59. data/lib/glimmer/swt/composite_proxy.rb +8 -8
  60. data/lib/glimmer/swt/custom/checkbox_group.rb +142 -0
  61. data/lib/glimmer/swt/custom/radio_group.rb +143 -0
  62. data/lib/glimmer/swt/display_proxy.rb +79 -0
  63. data/lib/glimmer/swt/fill_layout_proxy.rb +84 -0
  64. data/lib/glimmer/swt/font_proxy.rb +79 -0
  65. data/lib/glimmer/swt/grid_layout_proxy.rb +45 -4
  66. data/lib/glimmer/swt/group_proxy.rb +38 -0
  67. data/lib/glimmer/swt/label_proxy.rb +28 -4
  68. data/lib/glimmer/swt/layout_data_proxy.rb +59 -6
  69. data/lib/glimmer/swt/layout_proxy.rb +17 -14
  70. data/lib/glimmer/swt/list_proxy.rb +19 -14
  71. data/lib/glimmer/swt/make_shift_shell_proxy.rb +38 -0
  72. data/lib/glimmer/swt/message_box_proxy.rb +5 -8
  73. data/lib/glimmer/swt/radio_proxy.rb +81 -0
  74. data/lib/glimmer/swt/row_layout_proxy.rb +128 -0
  75. data/lib/glimmer/swt/scrolled_composite_proxy.rb +20 -0
  76. data/lib/glimmer/swt/shell_proxy.rb +51 -26
  77. data/lib/glimmer/swt/style_constantizable.rb +154 -0
  78. data/lib/glimmer/swt/styled_text_proxy.rb +44 -0
  79. data/lib/glimmer/swt/swt_proxy.rb +53 -0
  80. data/lib/glimmer/swt/tab_folder_proxy.rb +8 -8
  81. data/lib/glimmer/swt/tab_item_proxy.rb +15 -32
  82. data/lib/glimmer/swt/table_proxy.rb +0 -18
  83. data/lib/glimmer/swt/widget_proxy.rb +173 -54
  84. data/lib/glimmer/ui/custom_shell.rb +92 -0
  85. data/lib/glimmer/ui/custom_widget.rb +292 -0
  86. data/lib/glimmer/util/proc_tracker.rb +39 -0
  87. data/lib/net/http.rb +17 -0
  88. data/lib/uri.rb +64 -0
  89. metadata +108 -57
  90. data/lib/glimmer/opal/display_proxy.rb +0 -23
  91. data/lib/glimmer/opal/element_proxy.rb +0 -312
  92. data/lib/samples/elaborate/launch +0 -6
  93. data/lib/samples/hello/hello_combo.rb +0 -34
  94. data/lib/samples/hello/hello_tab.rb +0 -24
  95. data/lib/samples/hello/hello_world.rb +0 -8
  96. data/lib/samples/hello/launch +0 -10
  97. data/lib/samples/launch +0 -4
@@ -0,0 +1,143 @@
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 radios generated via data-binding
28
+ class RadioGroup
29
+ include Glimmer::UI::CustomWidget
30
+
31
+ body {
32
+ composite # just an empty composite to hold radios upon data-binding `selection`
33
+ }
34
+
35
+ def items=(text_array)
36
+ selection_value = selection
37
+ @items = Array[*text_array]
38
+ build_radios
39
+ end
40
+
41
+ def items
42
+ @items || []
43
+ end
44
+
45
+ def selection=(text)
46
+ radios.count.times do |index|
47
+ radio = radios[index]
48
+ item = items[index]
49
+ radio.selection = item == text
50
+ end
51
+ end
52
+
53
+ def selection
54
+ selection_value = radios[selection_index]&.text unless selection_index == -1
55
+ selection_value.to_s
56
+ end
57
+
58
+ def selection_index=(index)
59
+ self.selection=(items[index])
60
+ end
61
+ alias select selection_index=
62
+
63
+ def selection_index
64
+ radios.index(radios.detect(&:selection)) || -1
65
+ end
66
+
67
+ def radios
68
+ @radios ||= []
69
+ end
70
+
71
+ def observation_request_to_event_mapping
72
+ # TODO method might not be needed
73
+ {
74
+ 'on_widget_selected' => {
75
+ event: 'change'
76
+ },
77
+ }
78
+ end
79
+
80
+ def can_handle_observation_request?(observation_request)
81
+ radios.first&.can_handle_observation_request?(observation_request) || super(observation_request)
82
+ end
83
+
84
+ def handle_observation_request(observation_request, &block)
85
+ observation_requests << [observation_request, block]
86
+ delegate_observation_request_to_radios(observation_request, &block)
87
+ super
88
+ end
89
+
90
+ def delegate_observation_request_to_radios(observation_request, &block)
91
+ if observation_request != 'on_widget_disposed'
92
+ radios.count.times do |index|
93
+ radio = radios[index]
94
+ radio.handle_observation_request(observation_request, &block) if radio.can_handle_observation_request?(observation_request)
95
+ end
96
+ end
97
+ end
98
+
99
+ def observation_requests
100
+ @observation_requests ||= Set.new
101
+ end
102
+
103
+ def has_attribute?(attribute_name, *args)
104
+ @radios.to_a.map do |widget_proxy|
105
+ return true if widget_proxy.has_attribute?(attribute_name, *args)
106
+ end
107
+ super
108
+ end
109
+
110
+ def set_attribute(attribute_name, *args)
111
+ excluded_attributes = ['selection']
112
+ unless excluded_attributes.include?(attribute_name.to_s)
113
+ @radios.to_a.each do |widget_proxy|
114
+ widget_proxy.set_attribute(attribute_name, *args) if widget_proxy.has_attribute?(attribute_name, *args)
115
+ end
116
+ end
117
+ super
118
+ end
119
+
120
+ private
121
+
122
+ def build_radios
123
+ current_selection = selection
124
+ @radios = []
125
+ items.each do |item|
126
+ body_root.content {
127
+ radios << radio { |radio_proxy|
128
+ text item
129
+ on_widget_selected {
130
+ self.selection = items[radios.index(radio_proxy)]
131
+ }
132
+ }
133
+ }
134
+ end
135
+ observation_requests.to_a.each do |observation_request, block|
136
+ delegate_observation_request_to_radios(observation_request, &block)
137
+ end
138
+ self.selection = current_selection
139
+ end
140
+ end
141
+ end
142
+ end
143
+ end
@@ -0,0 +1,79 @@
1
+ module Glimmer
2
+ module SWT
3
+ class DisplayProxy < WidgetProxy
4
+ class << self
5
+ def instance
6
+ @instance ||= new
7
+ end
8
+ end
9
+
10
+ def initialize
11
+ # Do not call super
12
+ end
13
+
14
+ def path
15
+ "html body"
16
+ end
17
+
18
+ # Root element representing widget. Must be overridden by subclasses if different from div
19
+ def element
20
+ 'body'
21
+ end
22
+
23
+ def listener_dom_element
24
+ Document
25
+ end
26
+
27
+ def render
28
+ # No rendering as body is rendered as part of ShellProxy.. this class only serves as an SWT Display utility
29
+ end
30
+
31
+ def async_exec(&block)
32
+ executer = lambda do
33
+ if Document.find('.modal').to_a.empty?
34
+ block.call
35
+ else
36
+ sleep(0.05)
37
+ Async::Task.new(&executer)
38
+ end
39
+ end
40
+ Async::Task.new(&executer)
41
+ end
42
+ # sync_exec kept for API compatibility reasons
43
+ alias sync_exec async_exec
44
+
45
+ def observation_request_to_event_mapping
46
+ {
47
+ 'on_swt_keydown' => [
48
+ {
49
+ event: 'keypress',
50
+ event_handler: -> (event_listener) {
51
+ -> (event) {
52
+ event.singleton_class.define_method(:character) do
53
+ which || key_code
54
+ end
55
+ event_listener.call(event)
56
+ }
57
+ }
58
+ },
59
+ {
60
+ event: 'keydown',
61
+ event_handler: -> (event_listener) {
62
+ -> (event) {
63
+ event.singleton_class.define_method(:character) do
64
+ which || key_code
65
+ end
66
+ event_listener.call(event) if event.key_code != 13 && (event.key_code == 127 || event.key_code <= 31)
67
+ }
68
+ }
69
+ }
70
+ ]
71
+ }
72
+ end
73
+
74
+ def shells
75
+ @shells ||= []
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,84 @@
1
+ require 'glimmer/swt/layout_proxy'
2
+
3
+ module Glimmer
4
+ module SWT
5
+ class FillLayoutProxy < LayoutProxy
6
+ include Glimmer
7
+
8
+ STYLE = <<~CSS
9
+ .fill-layout {
10
+ display: flex;
11
+ }
12
+
13
+ .fill-layout > * {
14
+ width: 100%;
15
+ height: 100%;
16
+ }
17
+
18
+ .fill-layout-horizontal {
19
+ flex-direction: row;
20
+ }
21
+
22
+ .fill-layout-vertical {
23
+ flex-direction: column;
24
+ }
25
+ CSS
26
+
27
+ attr_reader :type, :margin_width, :margin_height, :spacing
28
+
29
+ def initialize(parent, args)
30
+ super(parent, args)
31
+ @type = @args.first || :horizontal
32
+ self.margin_width = 15
33
+ self.margin_height = 15
34
+ @parent.css_classes << 'fill-layout'
35
+ @parent.css_classes << (horizontal? ? 'fill-layout-horizontal' : 'fill-layout-vertical')
36
+ @parent.dom_element.add_class('fill-layout')
37
+ @parent.dom_element.add_class(horizontal? ? 'fill-layout-horizontal' : 'fill-layout-vertical')
38
+ end
39
+
40
+ def horizontal?
41
+ @type == :horizontal
42
+ end
43
+
44
+ def vertical?
45
+ @type == :vertical
46
+ end
47
+
48
+ def margin_width=(pixels)
49
+ @margin_width = pixels
50
+ # Using padding for width since margin-right isn't getting respected with width 100%
51
+ @parent.dom_element.css('padding-left', @margin_width)
52
+ @parent.dom_element.css('padding-right', @margin_width)
53
+ end
54
+
55
+ def margin_height=(pixels)
56
+ @margin_height = pixels
57
+ @parent.dom_element.css('padding-top', @margin_height)
58
+ @parent.dom_element.css('padding-bottom', @margin_height)
59
+ end
60
+
61
+ def spacing=(spacing)
62
+ @spacing = spacing.to_i
63
+ # TODO implement changes to accomodate layout_data in the future
64
+ @parent.style_element.html css {
65
+ s("##{@parent.id} > *") {
66
+ if horizontal?
67
+ margin_right "#{@spacing}px"
68
+ elsif vertical?
69
+ margin_bottom "#{@spacing}px"
70
+ end
71
+ }
72
+ s("##{@parent.id} > :last-child") {
73
+ if horizontal?
74
+ margin_right 0
75
+ elsif vertical?
76
+ margin_bottom 0
77
+ end
78
+ }
79
+ }.to_s
80
+ end
81
+
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,79 @@
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
+ # Proxy for org.eclipse.swt.graphics.Font
27
+ #
28
+ # This class can be optionally used with WidgetProxy to manipulate
29
+ # an SWT widget font (reusing its FontData but building a new Font)
30
+ #
31
+ # Otherwise, if no WidgetProxy is passed to constructor, it builds new FontData
32
+ #
33
+ # Invoking `#swt_font` returns the SWT Font object wrapped by this proxy
34
+ #
35
+ # Follows the Proxy Design Pattern
36
+ class FontProxy
37
+ ERROR_INVALID_FONT_STYLE = " is an invalid font style! Valid values are :normal, :bold, and :italic"
38
+ FONT_STYLES = [:normal, :bold, :italic]
39
+
40
+ attr_reader :widget_proxy, :font_properties
41
+
42
+ # Builds a new font proxy from passed in widget_proxy and font_properties hash,
43
+ #
44
+ # It begins with existing SWT widget font and amends it with font properties.
45
+ #
46
+ # Font properties consist of: :name, :height, and :style (one needed minimum)
47
+ #
48
+ # Style (:style value) can only be one of FontProxy::FONT_STYLES values:
49
+ # that is :normal, :bold, or :italic
50
+ def initialize(widget_proxy = nil, font_properties)
51
+ @widget_proxy = widget_proxy
52
+ @font_properties = font_properties.symbolize_keys
53
+ detect_invalid_font_property(font_properties)
54
+ end
55
+
56
+ def name
57
+ font_properties[:name]
58
+ end
59
+
60
+ def height
61
+ font_properties[:height]
62
+ end
63
+
64
+ def style
65
+ font_properties[:style]
66
+ end
67
+
68
+ private
69
+
70
+ def detect_invalid_font_property(font_properties)
71
+ [font_properties[:style]].flatten.select do |style|
72
+ style.is_a?(Symbol) || style.is_a?(String)
73
+ end.each do |style|
74
+ raise Error, style.to_s + ERROR_INVALID_FONT_STYLE if !FONT_STYLES.include?(style.to_sym)
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
@@ -3,18 +3,28 @@ require 'glimmer/swt/layout_proxy'
3
3
  module Glimmer
4
4
  module SWT
5
5
  class GridLayoutProxy < LayoutProxy
6
- attr_reader :num_columns, :make_columns_equal_width, :horizontal_spacing, :vertical_spacing
6
+ STYLE = <<~CSS
7
+ .grid-layout {
8
+ display: grid;
9
+ grid-template-rows: min-content;
10
+ justify-content: start;
11
+ place-content: start;
12
+ align-items: stretch;
13
+ }
14
+ CSS
15
+ attr_reader :num_columns, :make_columns_equal_width, :horizontal_spacing, :vertical_spacing, :margin_width, :margin_height
7
16
 
8
17
  def initialize(parent, args)
9
18
  super(parent, args)
10
- @horizontal_spacing = 10
11
- @vertical_spacing = 10
12
- @num_columns = @args.first || 1
19
+ self.horizontal_spacing = 10
20
+ self.vertical_spacing = 10
21
+ self.num_columns = @args.first || 1
13
22
  reapply
14
23
  end
15
24
 
16
25
  def num_columns=(columns)
17
26
  @num_columns = columns
27
+ # TODO do the following instead of reapply
18
28
  # @parent.add_css_class("num-columns-#{@num_columns}")
19
29
  reapply
20
30
  end
@@ -36,6 +46,37 @@ module Glimmer
36
46
  # @parent.add_css_class("vertical-spacing-#{@vertical_spacing}")
37
47
  reapply
38
48
  end
49
+
50
+ def margin_width=(pixels)
51
+ @margin_width = pixels
52
+ # Using padding for width since margin-right isn't getting respected with width 100%
53
+ @parent.dom_element.css('padding-left', @margin_width)
54
+ @parent.dom_element.css('padding-right', @margin_width)
55
+ end
56
+
57
+ def margin_height=(pixels)
58
+ @margin_height = pixels
59
+ @parent.dom_element.css('padding-top', @margin_height)
60
+ @parent.dom_element.css('padding-bottom', @margin_height)
61
+ end
62
+
63
+ def reapply
64
+ # TODO get rid of this method
65
+ layout_css = <<~CSS
66
+ grid-template-columns: #{'auto ' * @num_columns.to_i};
67
+ grid-row-gap: #{@vertical_spacing}px;
68
+ grid-column-gap: #{@horizontal_spacing}px;
69
+ CSS
70
+ if @parent.css_classes.include?('grid-layout')
71
+ layout_css.split(";").map(&:strip).map {|l| l.split(':').map(&:strip)}.each do |key, value|
72
+ @parent.dom_element.css(key, value) unless key.nil?
73
+ end
74
+ else
75
+ layout_css.split(";").map(&:strip).map {|l| l.split(':').map(&:strip)}.each do |key, value|
76
+ @parent.dom_element.css(key, 'initial') unless key.nil?
77
+ end
78
+ end
79
+ end
39
80
  end
40
81
  end
41
82
  end