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.
Files changed (119) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +56 -0
  3. data/README.md +815 -76
  4. data/VERSION +1 -1
  5. data/lib/glimmer-dsl-opal.rb +45 -8
  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/glimmer-dsl-opal/missing/net/http.rb +17 -0
  9. data/lib/glimmer-dsl-opal/missing/uri.rb +64 -0
  10. data/lib/{samples → glimmer-dsl-opal/samples}/elaborate/contact_manager.rb +2 -3
  11. data/lib/{samples → glimmer-dsl-opal/samples}/elaborate/contact_manager/contact.rb +0 -0
  12. data/lib/{samples → glimmer-dsl-opal/samples}/elaborate/contact_manager/contact_manager_presenter.rb +0 -0
  13. data/lib/{samples → glimmer-dsl-opal/samples}/elaborate/contact_manager/contact_repository.rb +24 -99
  14. data/lib/{samples → glimmer-dsl-opal/samples}/elaborate/login.rb +0 -0
  15. data/lib/{samples → glimmer-dsl-opal/samples}/elaborate/tic_tac_toe.rb +0 -0
  16. data/lib/{samples → glimmer-dsl-opal/samples}/elaborate/tic_tac_toe/board.rb +0 -0
  17. data/lib/{samples → glimmer-dsl-opal/samples}/elaborate/tic_tac_toe/cell.rb +0 -0
  18. data/lib/{samples → glimmer-dsl-opal/samples}/hello/hello_browser.rb +0 -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/{samples → glimmer-dsl-opal/samples}/hello/hello_list_multi_selection.rb +0 -0
  25. data/lib/{samples → glimmer-dsl-opal/samples}/hello/hello_list_single_selection.rb +0 -0
  26. data/lib/glimmer-dsl-opal/samples/hello/hello_tab.rb +50 -0
  27. data/lib/glimmer-dsl-opal/samples/hello/hello_world.rb +29 -0
  28. data/lib/glimmer-dsl-opal/vendor/jquery.js +2 -0
  29. data/lib/glimmer/data_binding/element_binding.rb +1 -1
  30. data/lib/glimmer/data_binding/ext/observable_model.rb +6 -6
  31. data/lib/glimmer/data_binding/list_selection_binding.rb +1 -1
  32. data/lib/glimmer/data_binding/table_items_binding.rb +70 -0
  33. data/lib/glimmer/dsl/opal/async_exec_expression.rb +2 -2
  34. data/lib/glimmer/dsl/opal/color_expression.rb +38 -0
  35. data/lib/glimmer/dsl/opal/column_properties_expression.rb +22 -0
  36. data/lib/glimmer/dsl/opal/combo_selection_data_binding_expression.rb +2 -2
  37. data/lib/glimmer/dsl/opal/custom_widget_expression.rb +77 -0
  38. data/lib/glimmer/dsl/opal/dsl.rb +15 -14
  39. data/lib/glimmer/dsl/opal/font_expression.rb +47 -0
  40. data/lib/glimmer/dsl/opal/layout_data_expression.rb +2 -2
  41. data/lib/glimmer/dsl/opal/layout_expression.rb +22 -0
  42. data/lib/glimmer/dsl/opal/list_selection_data_binding_expression.rb +2 -3
  43. data/lib/glimmer/dsl/opal/message_box_expression.rb +2 -2
  44. data/lib/glimmer/dsl/opal/property_expression.rb +5 -2
  45. data/lib/glimmer/dsl/opal/rgb_expression.rb +32 -0
  46. data/lib/glimmer/dsl/opal/rgba_expression.rb +32 -0
  47. data/lib/glimmer/dsl/opal/shell_expression.rb +18 -2
  48. data/lib/glimmer/dsl/opal/swt_expression.rb +46 -0
  49. data/lib/glimmer/dsl/opal/{composite_expression.rb → table_column_expression.rb} +3 -3
  50. data/lib/glimmer/dsl/opal/{list_expression.rb → table_expression.rb} +3 -3
  51. data/lib/glimmer/dsl/opal/table_items_data_binding_expression.rb +29 -0
  52. data/lib/glimmer/dsl/opal/widget_expression.rb +24 -0
  53. data/lib/glimmer/dsl/opal/widget_listener_expression.rb +16 -3
  54. data/lib/glimmer/swt.rb +499 -0
  55. data/lib/glimmer/swt/browser_proxy.rb +27 -0
  56. data/lib/glimmer/swt/button_proxy.rb +40 -0
  57. data/lib/glimmer/swt/color_proxy.rb +119 -0
  58. data/lib/glimmer/{opal/select_proxy.rb → swt/combo_proxy.rb} +24 -19
  59. data/lib/glimmer/swt/composite_proxy.rb +31 -0
  60. data/lib/glimmer/{opal → swt}/display_proxy.rb +6 -4
  61. data/lib/glimmer/{opal → swt}/event_listener_proxy.rb +1 -1
  62. data/lib/glimmer/swt/fill_layout_proxy.rb +84 -0
  63. data/lib/glimmer/swt/font_proxy.rb +79 -0
  64. data/lib/glimmer/swt/grid_layout_proxy.rb +71 -0
  65. data/lib/glimmer/swt/label_proxy.rb +34 -0
  66. data/lib/glimmer/swt/layout_data_proxy.rb +87 -0
  67. data/lib/glimmer/swt/layout_proxy.rb +63 -0
  68. data/lib/glimmer/{opal → swt}/list_proxy.rb +31 -23
  69. data/lib/glimmer/swt/message_box_proxy.rb +143 -0
  70. data/lib/glimmer/{opal → swt}/point.rb +1 -1
  71. data/lib/glimmer/{opal → swt}/property_owner.rb +1 -1
  72. data/lib/glimmer/swt/row_layout_proxy.rb +105 -0
  73. data/lib/glimmer/swt/shell_proxy.rb +246 -0
  74. data/lib/glimmer/swt/style_constantizable.rb +154 -0
  75. data/lib/glimmer/swt/swt_proxy.rb +53 -0
  76. data/lib/glimmer/{opal/tab_folder.rb → swt/tab_folder_proxy.rb} +21 -15
  77. data/lib/glimmer/swt/tab_item_proxy.rb +84 -0
  78. data/lib/glimmer/swt/table_column_proxy.rb +56 -0
  79. data/lib/glimmer/swt/table_item_proxy.rb +147 -0
  80. data/lib/glimmer/swt/table_proxy.rb +159 -0
  81. data/lib/glimmer/swt/text_proxy.rb +46 -0
  82. data/lib/glimmer/swt/widget_proxy.rb +490 -0
  83. data/lib/glimmer/ui/custom_shell.rb +91 -0
  84. data/lib/glimmer/ui/custom_widget.rb +291 -0
  85. data/lib/glimmer/util/proc_tracker.rb +39 -0
  86. metadata +131 -76
  87. data/lib/glimmer/config.rb +0 -22
  88. data/lib/glimmer/dsl/engine.rb +0 -193
  89. data/lib/glimmer/dsl/expression.rb +0 -42
  90. data/lib/glimmer/dsl/expression_handler.rb +0 -48
  91. data/lib/glimmer/dsl/opal/browser_expression.rb +0 -17
  92. data/lib/glimmer/dsl/opal/button_expression.rb +0 -18
  93. data/lib/glimmer/dsl/opal/combo_expression.rb +0 -17
  94. data/lib/glimmer/dsl/opal/grid_layout_expression.rb +0 -17
  95. data/lib/glimmer/dsl/opal/label_expression.rb +0 -17
  96. data/lib/glimmer/dsl/opal/tab_folder_expression.rb +0 -17
  97. data/lib/glimmer/dsl/opal/tab_item_expression.rb +0 -17
  98. data/lib/glimmer/dsl/opal/text_expression.rb +0 -22
  99. data/lib/glimmer/dsl/parent_expression.rb +0 -12
  100. data/lib/glimmer/dsl/static_expression.rb +0 -36
  101. data/lib/glimmer/dsl/top_level_expression.rb +0 -7
  102. data/lib/glimmer/error.rb +0 -6
  103. data/lib/glimmer/invalid_keyword_error.rb +0 -6
  104. data/lib/glimmer/opal/div_proxy.rb +0 -22
  105. data/lib/glimmer/opal/document_proxy.rb +0 -178
  106. data/lib/glimmer/opal/element_proxy.rb +0 -287
  107. data/lib/glimmer/opal/grid_layout_proxy.rb +0 -54
  108. data/lib/glimmer/opal/iframe_proxy.rb +0 -23
  109. data/lib/glimmer/opal/input_proxy.rb +0 -45
  110. data/lib/glimmer/opal/label_proxy.rb +0 -25
  111. data/lib/glimmer/opal/layout_data_proxy.rb +0 -31
  112. data/lib/glimmer/opal/modal.rb +0 -94
  113. data/lib/glimmer/opal/tab_item.rb +0 -98
  114. data/lib/samples/elaborate/launch +0 -6
  115. data/lib/samples/hello/hello_combo.rb +0 -34
  116. data/lib/samples/hello/hello_tab.rb +0 -24
  117. data/lib/samples/hello/hello_world.rb +0 -8
  118. data/lib/samples/hello/launch +0 -10
  119. 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,40 @@
1
+ require 'glimmer/swt/widget_proxy'
2
+
3
+ module Glimmer
4
+ module SWT
5
+ class ButtonProxy < WidgetProxy
6
+ attr_reader :text
7
+
8
+ def text=(value)
9
+ @text = value
10
+ dom_element.html(@text)
11
+ end
12
+
13
+ def element
14
+ 'button'
15
+ end
16
+
17
+ def observation_request_to_event_mapping
18
+ {
19
+ 'on_widget_selected' => {
20
+ event: 'click'
21
+ },
22
+ }
23
+ end
24
+
25
+ def dom
26
+ input_text = @text
27
+ input_id = id
28
+ input_style = css
29
+ input_args = {}
30
+ input_disabled = @enabled ? {} : {'disabled': 'disabled'}
31
+ input_args = input_args.merge(type: 'password') if has_style?(:password)
32
+ @dom ||= html {
33
+ button(input_args.merge(id: input_id, class: name, style: input_style, style: 'min-width: 27px; min-height: 27px;').merge(input_disabled)) {
34
+ input_text.to_s == '' ? '&nbsp;' : input_text
35
+ }
36
+ }.to_s
37
+ end
38
+ end
39
+ end
40
+ 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,22 +12,35 @@ 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
- def observation_request_to_event_mapping
38
+ def observation_request_to_event_mapping
27
39
  {
28
40
  'on_widget_selected' => {
29
41
  event: 'change',
30
42
  event_handler: -> (event_listener) {
31
- -> (event) {
43
+ -> (event) {
32
44
  @text = event.target.value
33
45
  event_listener.call(event)
34
46
  }
@@ -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
@@ -1,5 +1,5 @@
1
1
  module Glimmer
2
- module Opal
2
+ module SWT
3
3
  class DisplayProxy
4
4
  class << self
5
5
  def instance
@@ -9,15 +9,17 @@ module Glimmer
9
9
 
10
10
  def async_exec(&block)
11
11
  executer = lambda do
12
- if $document.at_css('.modal')
12
+ if Document.find('.modal').to_a.empty?
13
+ block.call
14
+ else
13
15
  sleep(0.05)
14
16
  Async::Task.new(&executer)
15
- else
16
- block.call
17
17
  end
18
18
  end
19
19
  Async::Task.new(&executer)
20
20
  end
21
+ # sync_exec kept for API compatibility reasons
22
+ alias sync_exec async_exec
21
23
  end
22
24
  end
23
25
  end
@@ -1,5 +1,5 @@
1
1
  module Glimmer
2
- module Opal
2
+ module SWT
3
3
  class EventListenerProxy
4
4
  attr_reader :element_proxy, :event, :selector, :delegate
5
5
 
@@ -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('margin-top', @margin_height)
58
+ @parent.dom_element.css('margin-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