glimmer-dsl-libui 0.3.0 → 0.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +35 -0
  3. data/README.md +1229 -1210
  4. data/VERSION +1 -1
  5. data/examples/area_gallery.rb +28 -28
  6. data/examples/area_gallery2.rb +113 -110
  7. data/examples/area_gallery3.rb +28 -28
  8. data/examples/area_gallery4.rb +112 -110
  9. data/examples/basic_area.rb +1 -3
  10. data/examples/basic_area2.rb +1 -3
  11. data/examples/basic_area3.rb +17 -0
  12. data/examples/basic_area4.rb +19 -0
  13. data/examples/basic_scrolling_area.rb +79 -0
  14. data/examples/basic_table_color.rb +1 -11
  15. data/examples/basic_table_color2.rb +39 -0
  16. data/examples/basic_table_image.rb +2 -14
  17. data/examples/basic_table_image2.rb +44 -0
  18. data/examples/basic_table_image_text.rb +2 -13
  19. data/examples/basic_table_image_text2.rb +44 -0
  20. data/examples/basic_transform.rb +3 -6
  21. data/examples/basic_transform2.rb +34 -0
  22. data/examples/color_the_circles.rb +1 -3
  23. data/examples/dynamic_area.rb +1 -3
  24. data/examples/dynamic_area2.rb +5 -7
  25. data/examples/form_table.rb +4 -0
  26. data/examples/grid.rb +4 -4
  27. data/examples/histogram.rb +4 -8
  28. data/examples/meta_example.rb +50 -10
  29. data/examples/snake.rb +1 -3
  30. data/examples/tetris.rb +15 -18
  31. data/examples/tic_tac_toe/board.rb +4 -2
  32. data/examples/tic_tac_toe.rb +1 -3
  33. data/glimmer-dsl-libui.gemspec +0 -0
  34. data/lib/glimmer/dsl/libui/shape_expression.rb +6 -1
  35. data/lib/glimmer/libui/control_proxy/area_proxy/scrolling_area_proxy.rb +35 -0
  36. data/lib/glimmer/libui/control_proxy/area_proxy.rb +1 -0
  37. data/lib/glimmer/libui/control_proxy/column.rb +2 -2
  38. data/lib/glimmer/libui/control_proxy/image_proxy.rb +42 -35
  39. data/lib/glimmer/libui/control_proxy/table_proxy.rb +15 -2
  40. data/lib/glimmer/libui/control_proxy/window_proxy.rb +20 -0
  41. data/lib/glimmer/libui/shape.rb +49 -1
  42. metadata +12 -19
@@ -45,9 +45,7 @@ class TicTacToe
45
45
 
46
46
  3.times.map do |column|
47
47
  area {
48
- path {
49
- square(0, 0, 60)
50
-
48
+ square(0, 0, 60) {
51
49
  stroke :black, thickness: 2
52
50
  }
53
51
  text(23, 19) {
Binary file
@@ -21,6 +21,9 @@
21
21
 
22
22
  require 'glimmer/dsl/expression'
23
23
  require 'glimmer/dsl/parent_expression'
24
+ require 'glimmer/libui/control_proxy/path_proxy'
25
+ require 'glimmer/libui/shape'
26
+ require 'glimmer/libui/control_proxy/area_proxy'
24
27
 
25
28
  module Glimmer
26
29
  module DSL
@@ -32,7 +35,9 @@ module Glimmer
32
35
  Glimmer::LibUI::Shape.exists?(keyword) and
33
36
  (
34
37
  parent.is_a?(Glimmer::LibUI::ControlProxy::PathProxy) or
35
- parent.is_a?(Glimmer::LibUI::Shape)
38
+ parent.is_a?(Glimmer::LibUI::Shape) or
39
+ parent.is_a?(Glimmer::LibUI::ControlProxy::AreaProxy) or
40
+ (parent.nil? && Glimmer::LibUI::ControlProxy::AreaProxy.current_area_draw_params)
36
41
  )
37
42
  end
38
43
 
@@ -31,8 +31,43 @@ module Glimmer
31
31
  class ScrollingAreaProxy < AreaProxy
32
32
  def build_control
33
33
  @area_handler = ::LibUI::FFI::AreaHandler.malloc
34
+ @args[0] ||= Glimmer::LibUI::ControlProxy.main_window_proxy.width
35
+ @args[1] ||= Glimmer::LibUI::ControlProxy.main_window_proxy.height
34
36
  @libui = ::LibUI.new_scrolling_area(@area_handler, *@args)
35
37
  end
38
+
39
+ def width(value = nil)
40
+ if value.nil?
41
+ @args[0]
42
+ else
43
+ @args[0] = value
44
+ set_size(width, height)
45
+ end
46
+ end
47
+ alias width= width
48
+ alias set_width width
49
+
50
+ def height(value = nil)
51
+ if value.nil?
52
+ @args[1]
53
+ else
54
+ @args[1] = value
55
+ set_size(width, height)
56
+ end
57
+ end
58
+ alias height= height
59
+ alias set_height height
60
+
61
+ def set_size(width, height)
62
+ @args[0] = width
63
+ @args[1] = height
64
+ super(width, height)
65
+ end
66
+
67
+ def scroll_to(scroll_x, scroll_y, scroll_width = nil, scroll_height = nil)
68
+ scroll_width, scroll_height = Glimmer::LibUI::ControlProxy.main_window_proxy.content_size
69
+ super(scroll_x, scroll_y, scroll_width, scroll_height)
70
+ end
36
71
  end
37
72
  end
38
73
  end
@@ -218,6 +218,7 @@ module Glimmer
218
218
  {
219
219
  key: key_to_char(area_key_event.Key, modifiers),
220
220
  key_value: area_key_event.Key,
221
+ key_code: area_key_event.Key,
221
222
  ext_key: ext_key_to_symbol(area_key_event.ExtKey),
222
223
  ext_key_value: area_key_event.ExtKey,
223
224
  modifier: modifiers_to_symbols(area_key_event.Modifier).first,
@@ -43,9 +43,9 @@ module Glimmer
43
43
  @column_index ||= @parent_proxy.send(:next_column_index)
44
44
  end
45
45
 
46
- # actual index within table columns (disregarding nil fillings that account for DualColumn instances)
46
+ # actual index within table columns (disregarding extra fillings that account for DualColumn instances)
47
47
  def index
48
- @parent_proxy.columns.compact.index(self)
48
+ @parent_proxy.columns.select {|c| c.is_a?(Column)}.index(self)
49
49
  end
50
50
  end
51
51
  end
@@ -20,6 +20,7 @@
20
20
  # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
21
 
22
22
  require 'glimmer/libui/control_proxy'
23
+ require 'glimmer/libui/control_proxy/image_part_proxy'
23
24
  require 'glimmer/libui/image_path_renderer'
24
25
  require 'glimmer/data_binding/observer'
25
26
  require 'glimmer/libui/control_proxy/transformable'
@@ -63,15 +64,13 @@ module Glimmer
63
64
  end
64
65
 
65
66
  def file(value = nil)
66
- if area_image?
67
- if value.nil?
68
- @args[0]
69
- else
70
- @args[0] = value
71
- if @content_added
72
- post_add_content
73
- request_auto_redraw
74
- end
67
+ if value.nil?
68
+ @args[0]
69
+ else
70
+ @args[0] = value
71
+ if @content_added
72
+ post_add_content
73
+ request_auto_redraw
75
74
  end
76
75
  end
77
76
  end
@@ -80,16 +79,12 @@ module Glimmer
80
79
 
81
80
  def width(value = nil)
82
81
  if value.nil?
83
- area_image? ? @args[1] : @args[0]
82
+ @args[1]
84
83
  else
85
- if area_image?
86
- @args[1] = value
87
- if @content_added
88
- post_add_content
89
- request_auto_redraw
90
- end
91
- else
92
- @args[0] = value
84
+ @args[1] = value
85
+ if area_image? && @content_added
86
+ post_add_content
87
+ request_auto_redraw
93
88
  end
94
89
  end
95
90
  end
@@ -98,16 +93,12 @@ module Glimmer
98
93
 
99
94
  def height(value = nil)
100
95
  if value.nil?
101
- area_image? ? @args[2] : @args[1]
96
+ @args[2]
102
97
  else
103
- if area_image?
104
- @args[2] = value
105
- if @content_added
106
- post_add_content
107
- request_auto_redraw
108
- end
109
- else
110
- @args[1] = value
98
+ @args[2] = value
99
+ if area_image? && @content_added
100
+ post_add_content
101
+ request_auto_redraw
111
102
  end
112
103
  end
113
104
  end
@@ -132,9 +123,7 @@ module Glimmer
132
123
  end
133
124
 
134
125
  def area_image?
135
- @parent_proxy&.is_a?(AreaProxy) or
136
- AreaProxy.current_area_draw_params or
137
- @args[0].is_a?(String) # first arg is file
126
+ @area_image ||= !!(@parent_proxy&.is_a?(AreaProxy) || AreaProxy.current_area_draw_params)
138
127
  end
139
128
 
140
129
  def destroy
@@ -146,23 +135,41 @@ module Glimmer
146
135
 
147
136
  def build_control
148
137
  unless area_image? # image object
149
- @args = [@children.first.args[1], @children.first.args[2]] if @children.size == 1 && (@args[0].nil? || @args[1].nil?)
150
- super
138
+ if file
139
+ load_image
140
+ ImagePartProxy.new('image_part', self, [@data, width, height, width * 4])
141
+ end
142
+ @args[1] = @children.first.args[1] if @children.size == 1 && @args[1].nil?
143
+ @args[2] = @children.first.args[2] if @children.size == 1 && @args[2].nil?
144
+ @libui = ControlProxy.new_control(@keyword, [width, height])
151
145
  @libui.tap do
152
146
  @children.each {|child| child&.send(:build_control) }
153
147
  end
154
148
  end
149
+ rescue => e
150
+ Glimmer::Config.logger.error {"Failed to load image file: #{file}"}
151
+ Glimmer::Config.logger.error {e.full_message}
152
+ raise e
155
153
  end
156
154
 
157
155
  def load_image
158
156
  require 'chunky_png'
159
- f = File.open(file)
160
- canvas = ChunkyPNG::Canvas.from_io(f)
161
- f.close
157
+ canvas = nil
158
+ if file.start_with?('http')
159
+ require 'net/http'
160
+ require 'open-uri'
161
+ uri = URI(file)
162
+ canvas = ChunkyPNG::Canvas.from_string(Net::HTTP.get(uri))
163
+ else
164
+ f = File.open(file)
165
+ canvas = ChunkyPNG::Canvas.from_io(f)
166
+ f.close
167
+ end
162
168
  canvas.resample_nearest_neighbor!(width, height) if width && height
163
169
  @data = canvas.to_rgba_stream
164
170
  self.width = canvas.width
165
171
  self.height = canvas.height
172
+ [@data, width, height]
166
173
  end
167
174
 
168
175
  def parse_pixels
@@ -21,6 +21,7 @@
21
21
 
22
22
  require 'glimmer/libui/control_proxy'
23
23
  require 'glimmer/libui/control_proxy/dual_column'
24
+ require 'glimmer/libui/control_proxy/triple_column'
24
25
  require 'glimmer/data_binding/observer'
25
26
  require 'glimmer/fiddle_consumer'
26
27
 
@@ -137,7 +138,7 @@ module Glimmer
137
138
 
138
139
  def build_control
139
140
  @model_handler = ::LibUI::FFI::TableModelHandler.malloc
140
- @model_handler.NumColumns = fiddle_closure_block_caller(4) { @columns.map {|c| c.is_a?(DualColumn) ? 2 : 1}.sum }
141
+ @model_handler.NumColumns = fiddle_closure_block_caller(4) { @columns.map {|c| c.is_a?(DualColumn) ? 2 : (c.is_a?(TripleColumn) ? 3 : 1)}.sum }
141
142
  @model_handler.ColumnType = fiddle_closure_block_caller(4, [1, 1, 4]) do |_, _, column|
142
143
  # TODO consider refactoring to use Glimmer::LibUI.enum_symbols(:table_value_type)
143
144
  case @columns[column]
@@ -193,15 +194,27 @@ module Glimmer
193
194
  column = @columns[column].index
194
195
  @cell_rows[row] ||= []
195
196
  @cell_rows[row][column] = ::LibUI.table_value_string(val).to_s
197
+ when Column::TextColorColumnProxy
198
+ column = @columns[column].index
199
+ @cell_rows[row] ||= []
200
+ @cell_rows[row][column] ||= []
201
+ @cell_rows[row][column][0] = ::LibUI.table_value_string(val).to_s
196
202
  when :text
197
203
  column = @columns[column - 1].index
204
+ @cell_rows[row] ||= []
205
+ @cell_rows[row][column] ||= []
198
206
  @cell_rows[row][column][1] = ::LibUI.table_value_string(val).to_s
199
207
  when Column::ButtonColumnProxy
200
208
  @columns[column].notify_listeners(:on_clicked, row)
201
- when Column::CheckboxColumnProxy, Column::CheckboxTextColumnProxy
209
+ when Column::CheckboxColumnProxy
202
210
  column = @columns[column].index
203
211
  @cell_rows[row] ||= []
204
212
  @cell_rows[row][column] = ::LibUI.table_value_int(val).to_i == 1
213
+ when Column::CheckboxTextColumnProxy
214
+ column = @columns[column].index
215
+ @cell_rows[row] ||= []
216
+ @cell_rows[row][column] ||= []
217
+ @cell_rows[row][column][0] = ::LibUI.table_value_int(val).to_i == 1
205
218
  end
206
219
  on_edited.each {|listener| listener.call(row, @cell_rows[row])}
207
220
  end
@@ -113,6 +113,26 @@ module Glimmer
113
113
  alias content_size= content_size
114
114
  alias set_content_size content_size
115
115
 
116
+ def width(value = nil)
117
+ if value.nil?
118
+ content_size.first
119
+ else
120
+ set_content_size(value, height)
121
+ end
122
+ end
123
+ alias width= width
124
+ alias set_width width
125
+
126
+ def height(value = nil)
127
+ if value.nil?
128
+ content_size.last
129
+ else
130
+ set_content_size(width, value)
131
+ end
132
+ end
133
+ alias height= height
134
+ alias set_height height
135
+
116
136
  def resizable(value = nil)
117
137
  if value.nil?
118
138
  @resizable = true if @resizable.nil?
@@ -73,12 +73,27 @@ module Glimmer
73
73
  @args = args
74
74
  @block = block
75
75
  set_parameter_defaults
76
+ build_control if implicit_path?
76
77
  post_add_content if @block.nil?
77
78
  end
78
79
 
79
80
  # Subclasses may override to perform post add_content work (normally must call super)
80
81
  def post_add_content
81
82
  @parent&.post_initialize_child(self)
83
+ @parent.post_add_content if implicit_path? && dynamic?
84
+ end
85
+
86
+ def post_initialize_child(child, add_child: true)
87
+ if child.is_a?(ControlProxy::MatrixProxy)
88
+ path_proxy.post_initialize_child(child, add_child: add_child)
89
+ else
90
+ super(child, add_child: add_child)
91
+ end
92
+ end
93
+
94
+ def content(&block)
95
+ Glimmer::DSL::Engine.add_content(self, Glimmer::DSL::Libui::ShapeExpression.new, @keyword, &block)
96
+ request_auto_redraw
82
97
  end
83
98
 
84
99
  # Subclasses must override to perform draw work and call super afterwards to ensure calling destroy when semi-declarative in an on_draw method
@@ -105,6 +120,24 @@ module Glimmer
105
120
  def path_proxy
106
121
  find_parent_in_ancestors { |parent| parent.nil? || parent.is_a?(ControlProxy::PathProxy) }
107
122
  end
123
+
124
+ def fill(*args)
125
+ path_proxy.fill(*args)
126
+ end
127
+ alias fill= fill
128
+ alias set_fill fill
129
+
130
+ def stroke(*args)
131
+ path_proxy.stroke(*args)
132
+ end
133
+ alias stroke= stroke
134
+ alias set_stroke stroke
135
+
136
+ def transform(matrix = nil)
137
+ path_proxy.transform(matrix)
138
+ end
139
+ alias transform= transform
140
+ alias set_transform transform
108
141
 
109
142
  def respond_to?(method_name, *args, &block)
110
143
  self.class.parameters.include?(method_name.to_s.sub(/=$/, '').sub(/^set_/, '').to_sym) or
@@ -117,6 +150,7 @@ module Glimmer
117
150
  method_name = method_name.to_s
118
151
  parameter_index = self.class.parameters.index(method_name_parameter)
119
152
  if method_name.start_with?('set_') || method_name.end_with?('=') || !args.empty?
153
+ args = [args] if args.size > 1
120
154
  if args.first != @args[parameter_index]
121
155
  @args[parameter_index] = args.first
122
156
  request_auto_redraw
@@ -124,13 +158,27 @@ module Glimmer
124
158
  else
125
159
  @args[parameter_index]
126
160
  end
127
- else
161
+ else # TODO consider if there is a need to redirect anything to path proxy or delete this TODO
128
162
  super
129
163
  end
130
164
  end
131
165
 
132
166
  private
133
167
 
168
+ def build_control
169
+ block = Proc.new {} if dynamic?
170
+ @parent = Glimmer::LibUI::ControlProxy::PathProxy.new('path', @parent, [], &block)
171
+ end
172
+
173
+ # indicates if nested directly under area or on_draw event (having an implicit path not an explicit path parent)
174
+ def implicit_path?
175
+ @implicit_path ||= !!(@parent.is_a?(Glimmer::LibUI::ControlProxy::AreaProxy) || (@parent.nil? && Glimmer::LibUI::ControlProxy::AreaProxy.current_area_draw_params))
176
+ end
177
+
178
+ def dynamic?
179
+ ((@parent.nil? || (@parent.is_a?(ControlProxy::PathProxy) && @parent.parent_proxy.nil?)) && Glimmer::LibUI::ControlProxy::AreaProxy.current_area_draw_params)
180
+ end
181
+
134
182
  def set_parameter_defaults
135
183
  self.class.parameter_defaults.each_with_index do |default, i|
136
184
  @args[i] ||= default
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: glimmer-dsl-libui
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Maleh
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-11-16 00:00:00.000000000 Z
11
+ date: 2021-11-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: glimmer
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 2.4.1
19
+ version: 2.5.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 2.4.1
26
+ version: 2.5.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: os
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -190,20 +190,6 @@ dependencies:
190
190
  - - "~>"
191
191
  - !ruby/object:Gem::Version
192
192
  version: 0.7.0
193
- - !ruby/object:Gem::Dependency
194
- name: chunky_png
195
- requirement: !ruby/object:Gem::Requirement
196
- requirements:
197
- - - "~>"
198
- - !ruby/object:Gem::Version
199
- version: 1.4.0
200
- type: :development
201
- prerelease: false
202
- version_requirements: !ruby/object:Gem::Requirement
203
- requirements:
204
- - - "~>"
205
- - !ruby/object:Gem::Version
206
- version: 1.4.0
207
193
  description: Glimmer DSL for LibUI (Prerequisite-Free Ruby Desktop Development GUI
208
194
  Library) - No need to pre-install any prerequisites. Just install the gem and have
209
195
  platform-independent native GUI that just works! Glimmer DSL for LibUI aims to provide
@@ -231,6 +217,8 @@ files:
231
217
  - examples/area_gallery4.rb
232
218
  - examples/basic_area.rb
233
219
  - examples/basic_area2.rb
220
+ - examples/basic_area3.rb
221
+ - examples/basic_area4.rb
234
222
  - examples/basic_button.rb
235
223
  - examples/basic_draw_text.rb
236
224
  - examples/basic_draw_text2.rb
@@ -240,15 +228,20 @@ files:
240
228
  - examples/basic_image3.rb
241
229
  - examples/basic_image4.rb
242
230
  - examples/basic_image5.rb
231
+ - examples/basic_scrolling_area.rb
243
232
  - examples/basic_table.rb
244
233
  - examples/basic_table_button.rb
245
234
  - examples/basic_table_checkbox.rb
246
235
  - examples/basic_table_checkbox_text.rb
247
236
  - examples/basic_table_color.rb
237
+ - examples/basic_table_color2.rb
248
238
  - examples/basic_table_image.rb
239
+ - examples/basic_table_image2.rb
249
240
  - examples/basic_table_image_text.rb
241
+ - examples/basic_table_image_text2.rb
250
242
  - examples/basic_table_progress_bar.rb
251
243
  - examples/basic_transform.rb
244
+ - examples/basic_transform2.rb
252
245
  - examples/basic_window.rb
253
246
  - examples/basic_window2.rb
254
247
  - examples/color_button.rb
@@ -405,7 +398,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
405
398
  - !ruby/object:Gem::Version
406
399
  version: '0'
407
400
  requirements: []
408
- rubygems_version: 3.2.22
401
+ rubygems_version: 3.2.31
409
402
  signing_key:
410
403
  specification_version: 4
411
404
  summary: Glimmer DSL for LibUI