lacci 0.2.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (78) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +2 -0
  3. data/Gemfile.lock +8 -1
  4. data/lib/lacci/scarpe_cli.rb +2 -2
  5. data/lib/lacci/scarpe_core.rb +2 -1
  6. data/lib/lacci/version.rb +1 -1
  7. data/lib/scarpe/niente/app.rb +23 -0
  8. data/lib/scarpe/niente/display_service.rb +66 -0
  9. data/lib/scarpe/niente/drawable.rb +59 -0
  10. data/lib/scarpe/niente/shoes_spec.rb +93 -0
  11. data/lib/scarpe/niente.rb +32 -0
  12. data/lib/shoes/app.rb +111 -72
  13. data/lib/shoes/background.rb +2 -2
  14. data/lib/shoes/border.rb +2 -2
  15. data/lib/shoes/builtins.rb +63 -0
  16. data/lib/shoes/changelog.rb +52 -0
  17. data/lib/shoes/colors.rb +3 -1
  18. data/lib/shoes/constants.rb +41 -2
  19. data/lib/shoes/display_service.rb +80 -18
  20. data/lib/shoes/download.rb +2 -2
  21. data/lib/shoes/drawable.rb +654 -0
  22. data/lib/shoes/drawables/arc.rb +27 -0
  23. data/lib/shoes/drawables/arrow.rb +21 -0
  24. data/lib/shoes/drawables/border.rb +28 -0
  25. data/lib/shoes/drawables/button.rb +57 -0
  26. data/lib/shoes/drawables/check.rb +33 -0
  27. data/lib/shoes/drawables/document_root.rb +20 -0
  28. data/lib/shoes/{widgets → drawables}/edit_box.rb +9 -8
  29. data/lib/shoes/{widgets → drawables}/edit_line.rb +8 -7
  30. data/lib/shoes/drawables/flow.rb +20 -0
  31. data/lib/shoes/drawables/font_helper.rb +62 -0
  32. data/lib/shoes/{widgets → drawables}/image.rb +7 -7
  33. data/lib/shoes/drawables/line.rb +17 -0
  34. data/lib/shoes/drawables/link.rb +31 -0
  35. data/lib/shoes/drawables/list_box.rb +59 -0
  36. data/lib/shoes/drawables/oval.rb +48 -0
  37. data/lib/shoes/drawables/para.rb +206 -0
  38. data/lib/shoes/drawables/progress.rb +15 -0
  39. data/lib/shoes/drawables/radio.rb +35 -0
  40. data/lib/shoes/drawables/rect.rb +18 -0
  41. data/lib/shoes/{widgets → drawables}/shape.rb +8 -8
  42. data/lib/shoes/drawables/slot.rb +178 -0
  43. data/lib/shoes/drawables/stack.rb +21 -0
  44. data/lib/shoes/drawables/star.rb +28 -0
  45. data/lib/shoes/drawables/subscription_item.rb +93 -0
  46. data/lib/shoes/drawables/text_drawable.rb +122 -0
  47. data/lib/shoes/drawables/video.rb +17 -0
  48. data/lib/shoes/drawables/widget.rb +74 -0
  49. data/lib/shoes/drawables.rb +32 -0
  50. data/lib/shoes/errors.rb +38 -0
  51. data/lib/shoes/log.rb +2 -2
  52. data/lib/shoes/margin_helper.rb +79 -0
  53. data/lib/shoes/ruby_extensions.rb +15 -0
  54. data/lib/shoes-spec.rb +93 -0
  55. data/lib/shoes.rb +31 -10
  56. metadata +58 -31
  57. data/lib/shoes/spacing.rb +0 -9
  58. data/lib/shoes/widget.rb +0 -218
  59. data/lib/shoes/widgets/alert.rb +0 -19
  60. data/lib/shoes/widgets/arc.rb +0 -51
  61. data/lib/shoes/widgets/button.rb +0 -35
  62. data/lib/shoes/widgets/check.rb +0 -28
  63. data/lib/shoes/widgets/document_root.rb +0 -20
  64. data/lib/shoes/widgets/flow.rb +0 -22
  65. data/lib/shoes/widgets/font.rb +0 -14
  66. data/lib/shoes/widgets/line.rb +0 -18
  67. data/lib/shoes/widgets/link.rb +0 -25
  68. data/lib/shoes/widgets/list_box.rb +0 -25
  69. data/lib/shoes/widgets/para.rb +0 -68
  70. data/lib/shoes/widgets/radio.rb +0 -35
  71. data/lib/shoes/widgets/slot.rb +0 -75
  72. data/lib/shoes/widgets/span.rb +0 -26
  73. data/lib/shoes/widgets/stack.rb +0 -24
  74. data/lib/shoes/widgets/star.rb +0 -44
  75. data/lib/shoes/widgets/subscription_item.rb +0 -60
  76. data/lib/shoes/widgets/text_widget.rb +0 -51
  77. data/lib/shoes/widgets/video.rb +0 -15
  78. data/lib/shoes/widgets.rb +0 -29
@@ -0,0 +1,654 @@
1
+ # frozen_string_literal: true
2
+ require_relative 'margin_helper'
3
+ class Shoes
4
+ # Shoes::Drawable
5
+ #
6
+ # This is the display-service portable Shoes Drawable interface. Visible Shoes
7
+ # drawables like buttons inherit from this. Compound drawables made of multiple
8
+ # different smaller Drawables inherit from it in their various apps or libraries.
9
+ # The Shoes Drawable helps build a Shoes-side drawable tree, with parents and
10
+ # children. Any API that applies to all drawables (e.g. remove) should be
11
+ # defined here.
12
+ #
13
+ class Drawable < Shoes::Linkable
14
+ include Shoes::Log
15
+ include Shoes::Colors
16
+
17
+ # All Drawables have these so they go in Shoes::Drawable and are inherited
18
+ @shoes_events = ["parent", "destroy", "prop_change", "hover", "leave", "motion"]
19
+
20
+ class << self
21
+ attr_accessor :drawable_classes
22
+ attr_accessor :drawable_default_styles
23
+ attr_accessor :widget_classes
24
+
25
+ def inherited(subclass)
26
+ Shoes::Drawable.drawable_classes ||= []
27
+ Shoes::Drawable.drawable_classes << subclass
28
+
29
+ Shoes::Drawable.drawable_default_styles ||= {}
30
+ Shoes::Drawable.drawable_default_styles[subclass] = {}
31
+
32
+ Shoes::Drawable.widget_classes ||= []
33
+ if subclass < Shoes::Widget
34
+ Shoes::Drawable.widget_classes << subclass.name
35
+ end
36
+
37
+ super
38
+ end
39
+
40
+ def dsl_name
41
+ n = name.split("::").last.chomp("Drawable")
42
+ n.gsub(/(.)([A-Z])/, '\1_\2').downcase
43
+ end
44
+
45
+ def drawable_class_by_name(name)
46
+ name = name.to_s
47
+ drawable_classes.detect { |k| k.dsl_name == name }
48
+ end
49
+
50
+ def is_widget_class?(name)
51
+ !!Shoes::Drawable.widget_classes.intersect?([name.to_s])
52
+ end
53
+
54
+ def validate_as(prop_name, value)
55
+ prop_name = prop_name.to_s
56
+ hashes = shoes_style_hashes
57
+
58
+ h = hashes.detect { |hash| hash[:name] == prop_name }
59
+ raise(Shoes::Errors::NoSuchStyleError, "Can't find property #{prop_name.inspect} in #{self} property list: #{hashes.inspect}!") unless h
60
+
61
+ return value if h[:validator].nil?
62
+
63
+ # Pass both the property name and value to the validator block
64
+ h[:validator].call(value,prop_name)
65
+ end
66
+
67
+ # Return a list of Shoes events for this class.
68
+ #
69
+ # @return Array[String] the list of event names
70
+ def get_shoes_events
71
+ if @shoes_events.nil?
72
+ raise Shoes::Errors::UnknownEventsForClassError, "Drawable type #{self} hasn't defined its list of Shoes events!"
73
+ end
74
+
75
+ @shoes_events
76
+ end
77
+
78
+ # Return whether Shoes events have already been registered for this class
79
+ #
80
+ # @return [Boolean] true if events have been registered, false if not
81
+ def registered_shoes_events?
82
+ !@shoes_events.nil?
83
+ end
84
+
85
+ # Set the list of Shoes event names that are allowed for this class.
86
+ #
87
+ # @param args [Array] an array of event names, which will be coerced to Strings
88
+ # @return [void]
89
+ def shoes_events(*args)
90
+ if @shoes_events
91
+ raise Shoes::Errors::DoubleRegisteredShoesEventError, "Registering shoes events #{args.inspect} for class #{self} but already registered events as #{@shoes_events.inspect}!"
92
+ end
93
+ @shoes_events = args.map(&:to_s) + self.superclass.get_shoes_events
94
+ end
95
+
96
+ # Require supplying these Shoes style values as positional arguments to
97
+ # initialize. Initialize will get the arg list, then set the specified styles
98
+ # if args are given for them. @see opt_init_args for additional non-required
99
+ # init args.
100
+ #
101
+ # @param args [Array<String,Symbol>] an array of Shoes style names
102
+ # @return [void]
103
+ def init_args(*args)
104
+ raise Shoes::Errors::BadArgumentListError, "Positional init args already set for #{self}!" if @required_init_args
105
+ @required_init_args = args.map(&:to_s)
106
+ end
107
+
108
+ # Allow supplying these Shoes style values as optional positional arguments to
109
+ # initialize after the mandatory args. @see init_args for setting required
110
+ # init args.
111
+ #
112
+ # @param args [Array<String,Symbol>] an array of Shoes style names
113
+ # @return [void]
114
+ def opt_init_args(*args)
115
+ raise Shoes::Errors::BadArgumentListError, "Positional init args already set for #{self}!" if @opt_init_args
116
+ @opt_init_args = args.map(&:to_s)
117
+ end
118
+
119
+ # Return the list of style names for required init args for this class
120
+ #
121
+ # @return [Array<String>] the array of style names as strings
122
+ def required_init_args
123
+ @required_init_args ||= [] # TODO: eventually remove the ||= here
124
+ end
125
+
126
+ # Return the list of style names for optional init args for this class
127
+ #
128
+ # @return [Array<String>] the array of style names as strings
129
+ def optional_init_args
130
+ @opt_init_args ||= []
131
+ end
132
+
133
+ # Assign a new Shoes Drawable ID number, starting from 1.
134
+ # This allows non-overlapping small integer IDs for Shoes
135
+ # linkable IDs - the number part of making it clear what
136
+ # widget you're talking about.
137
+ def allocate_drawable_id
138
+ @drawable_id_counter ||= 0
139
+ @drawable_id_counter += 1
140
+ @drawable_id_counter
141
+ end
142
+
143
+ def register_drawable_id(id, drawable)
144
+ @drawables_by_id ||= {}
145
+ @drawables_by_id[id] = drawable
146
+ end
147
+
148
+ def unregister_drawable_id(id)
149
+ @drawables_by_id ||= {}
150
+ @drawables_by_id.delete(id)
151
+ end
152
+
153
+ def drawable_by_id(id, none_ok: false)
154
+ val = @drawables_by_id[id]
155
+ unless val || none_ok
156
+ raise "No Drawable Found! #{@drawables_by_id.inspect}"
157
+ end
158
+
159
+ val
160
+ end
161
+
162
+ private
163
+
164
+ def linkable_properties
165
+ @linkable_properties ||= []
166
+ end
167
+
168
+ def linkable_properties_hash
169
+ @linkable_properties_hash ||= {}
170
+ end
171
+
172
+ public
173
+
174
+ # Shoes styles in Shoes Linkables are automatically sync'd with the display side objects.
175
+ # If a block is passed to shoes_style, that's the validation for the property. It should
176
+ # convert a given value to a valid value for the property or throw an exception.
177
+ #
178
+ # If feature is non-nil, it's the feature that an app must request in order to see this
179
+ # property.
180
+ #
181
+ # @param name [String,Symbol] the style name
182
+ # @param feature [Symbol,NilClass] the feature that must be defined for an app to request this style, or nil
183
+ # @block if block is given, call it to map the given style value to a valid value, or raise an exception
184
+ def shoes_style(name, feature: nil, &validator)
185
+ name = name.to_s
186
+
187
+ return if linkable_properties_hash[name]
188
+
189
+ linkable_properties << { name: name, validator:, feature: }
190
+ linkable_properties_hash[name] = true
191
+ end
192
+
193
+ # Add these names as Shoes styles with the given validator and feature, if any
194
+ def shoes_styles(*names, feature: nil, &validator)
195
+ names.each { |n| shoes_style(n, feature:, &validator) }
196
+ end
197
+
198
+ # Query what feature, if any, is required to use a specific shoes_style.
199
+ # If no specific feature is needed, nil will be returned.
200
+ def feature_for_shoes_style(style_name)
201
+ style_name = style_name.to_s
202
+ lp = linkable_properties.detect { |prop| prop[:name] == style_name }
203
+ return lp[:feature] if lp
204
+
205
+ # If we get to the top of the superclass tree and we didn't find it, it's not here
206
+ if self.class == ::Shoes::Drawable
207
+ raise Shoes::Errors::NoSuchStyleError, "Can't find information for style #{style_name.inspect}!"
208
+ end
209
+
210
+ super
211
+ end
212
+
213
+ # Return a list of shoes_style names with the given features. If with_features is nil,
214
+ # return them with a list of features for the current Shoes::App. For the list of
215
+ # styles available with no features requested, pass nil to with_features.
216
+ def shoes_style_names(with_features: nil)
217
+ # No with_features given? Use the ones requested by this Shoes::App
218
+ with_features ||= Shoes::App.instance.features
219
+ parent_prop_names = self != Shoes::Drawable ? self.superclass.shoes_style_names(with_features:) : []
220
+
221
+ if with_features == :all
222
+ subclass_props = linkable_properties
223
+ else
224
+ subclass_props = linkable_properties.select { |prop| !prop[:feature] || with_features.include?(prop[:feature]) }
225
+ end
226
+ parent_prop_names | subclass_props.map { |prop| prop[:name] }
227
+ end
228
+
229
+ def shoes_style_hashes
230
+ parent_hashes = self != Shoes::Drawable ? self.superclass.shoes_style_hashes : []
231
+
232
+ parent_hashes + linkable_properties
233
+ end
234
+
235
+ def shoes_style_name?(name)
236
+ linkable_properties_hash[name.to_s] ||
237
+ (self != Shoes::Drawable && superclass.shoes_style_name?(name))
238
+ end
239
+ end
240
+
241
+ # Every Shoes drawable has positioning properties
242
+ shoes_styles :top, :left, :width, :height
243
+
244
+ # Margins around drawable
245
+ shoes_styles :margin, :margin_top, :margin_bottom, :margin_left, :margin_right
246
+
247
+ # Padding around drawable
248
+ shoes_styles :padding, :padding_top, :padding_bottom, :padding_left, :padding_right
249
+
250
+ # Shoes uses a "hidden" style property for hide/show
251
+ shoes_style :hidden
252
+
253
+ attr_reader :debug_id
254
+
255
+ # These styles can be set to a current per-slot value and inherited from parent slots.
256
+ # Their value is set at drawable-create time.
257
+ DRAW_CONTEXT_STYLES = [:fill, :stroke, :strokewidth, :rotate, :transform, :translate]
258
+
259
+ include MarginHelper
260
+
261
+ def initialize(*args, **kwargs)
262
+ kwargs = margin_parse(kwargs)
263
+ log_init("Shoes::#{self.class.name}") unless @log
264
+
265
+ # First, get the list of allowed and disallowed styles for the given features
266
+ # and make sure no disallowed styles were given.
267
+
268
+ app_features = Shoes::App.instance.features
269
+ this_app_styles = self.class.shoes_style_names.map(&:to_sym)
270
+ not_this_app_styles = self.class.shoes_style_names(with_features: :all).map(&:to_sym) - this_app_styles
271
+
272
+ bad_styles = kwargs.keys & not_this_app_styles
273
+ unless bad_styles.empty?
274
+ features_needed = bad_styles.map { |s| self.class.feature_for_shoes_style(s) }.uniq
275
+ raise Shoes::Errors::UnsupportedFeatureError, "The style(s) #{bad_styles.inspect} are only defined for applications that request specific features: #{features_needed.inspect} (you requested #{app_features.inspect})!"
276
+ end
277
+
278
+ # Next, check positional arguments and make sure the correct number and type
279
+ # were passed and match positional args with style names.
280
+
281
+ supplied_args = kwargs.keys
282
+
283
+ req_args = self.class.required_init_args
284
+ opt_args = self.class.optional_init_args
285
+ pos_args = req_args + opt_args
286
+ if req_args != ["any"]
287
+ if args.size > pos_args.size
288
+ raise Shoes::Errors::BadArgumentListError, "Too many arguments given for #{self.class}#initialize! #{args.inspect}"
289
+ end
290
+
291
+ if args.size == 0
292
+ # It's fine to use keyword args instead, but we should make sure they're actually there
293
+ needed_args = req_args.map(&:to_sym) - kwargs.keys
294
+ unless needed_args.empty?
295
+ raise Shoes::Errors::BadArgumentListError, "Keyword arguments for #{self.class}#initialize should also supply #{needed_args.inspect}! #{args.inspect}"
296
+ end
297
+ elsif args.size < req_args.size
298
+ raise Shoes::Errors::BadArgumentListError, "Too few arguments given for #{self.class}#initialize! #{args.inspect}"
299
+ end
300
+
301
+ # Set each positional argument
302
+ args.each.with_index do |val, idx|
303
+ style_name = pos_args[idx]
304
+ next if style_name.nil? || style_name == "" # It's possible to have non-style positional args
305
+
306
+ val = self.class.validate_as(style_name, args[idx])
307
+ instance_variable_set("@#{style_name}", val)
308
+ supplied_args << style_name.to_sym
309
+ end
310
+ end
311
+
312
+ this_drawable_styles = self.class.shoes_style_names.map(&:to_sym)
313
+ dc = Shoes::App.instance.current_draw_context || {}
314
+
315
+ # Styles not passed as arguments can come from the draw context
316
+
317
+ # What styles are in the draw context, are used by this drawable, and weren't
318
+ # given as positional or keyword arguments?
319
+ draw_context_styles = (DRAW_CONTEXT_STYLES & this_drawable_styles) - supplied_args
320
+ unless draw_context_styles.empty?
321
+ # When we first call this, there is no parent. We don't want to set the parent
322
+ # yet because that will send a notification, and *that* should wait until after
323
+ # we've told the display service that this drawable was created. So instead
324
+ # we'll query the parent object's draw context directly.
325
+
326
+ draw_context_styles.each do |style|
327
+ dc_val = dc[style.to_s]
328
+ next if dc_val.nil?
329
+
330
+ val = self.class.validate_as(style, dc[style.to_s])
331
+ instance_variable_set("@#{style}", val)
332
+ supplied_args << style
333
+ end
334
+ end
335
+
336
+ # Styles that were *not* passed should be set to defaults
337
+
338
+ default_styles = Shoes::Drawable.drawable_default_styles[self.class]
339
+
340
+ # No arg specified for a property with a default value? Set it to default.
341
+ (default_styles.keys - supplied_args).each do |key|
342
+ val = self.class.validate_as(key, default_styles[key])
343
+ instance_variable_set("@#{key}", val)
344
+ end
345
+
346
+ # If we have a keyword arg for a style, set it as specified.
347
+ (this_drawable_styles & kwargs.keys).each do |key|
348
+ val = self.class.validate_as(key, kwargs[key])
349
+ instance_variable_set("@#{key}", val)
350
+ end
351
+
352
+ # We'd like to avoid unexpected keywords. But we're not disciplined enough to
353
+ # raise an error by default yet. Non-style keywords passed to Drawable#initialize
354
+ # are deprecated at this point, but I need to hunt down the last of them
355
+ # and prevent them.
356
+ unexpected = (kwargs.keys - this_drawable_styles)
357
+ unless unexpected.empty?
358
+ STDERR.puts "Unexpected non-style keyword(s) in #{self.class} initialize: #{unexpected.inspect}"
359
+ end
360
+
361
+ super(linkable_id: Shoes::Drawable.allocate_drawable_id)
362
+ Shoes::Drawable.register_drawable_id(self.linkable_id, self)
363
+
364
+ generate_debug_id
365
+
366
+ parent = ::Shoes::App.instance.current_slot
367
+ if self.class.expects_parent?
368
+ set_parent(parent, notify: false)
369
+ end
370
+
371
+ unless self.class.registered_shoes_events?
372
+ # No Shoes events declared and we're creating an instance?
373
+ # Default to no class-specific events.
374
+ self.class.shoes_events
375
+ end
376
+
377
+ # Binding the motion events here isn't perfect.
378
+ # What about drawables like SubscriptionItem that
379
+ # have no motion events? With the current Lacci
380
+ # implementation, the answer is that those events
381
+ # will never be sent. Calling .hover on one will
382
+ # be useless, harmless, and allowed. If you want
383
+ # to make it disallowed, you can do something like
384
+ # define a SubscriptionItem#hover that raises an
385
+ # exception instead.
386
+
387
+ bind_self_event("hover") do
388
+ @hover&.call
389
+ end
390
+
391
+ bind_self_event("leave") do
392
+ @leave&.call
393
+ end
394
+
395
+ bind_self_event("motion") do |x, y|
396
+ @motion&.call(x, y)
397
+ end
398
+ end
399
+
400
+ def self.expects_parent?
401
+ return false if [::Shoes::App, ::Shoes::DocumentRoot].include?(self)
402
+ return false if self < ::Shoes::TextDrawable
403
+
404
+ true
405
+ end
406
+
407
+ # Calling stack.app or drawable.app will execute the block
408
+ # with the Shoes::App as self, and with that stack or
409
+ # flow as the current slot.
410
+ #
411
+ # @incompatibility In Shoes Classic this is the only way
412
+ # to change self, while Scarpe will also change self
413
+ # with the other Slot Manipulation methods: #clear,
414
+ # #append, #prepend, #before and #after.
415
+ #
416
+ # @return [Shoes::App] the Shoes app
417
+ # @yield the block to call with the Shoes App as self
418
+ def app(&block)
419
+ Shoes::App.instance.with_slot(self, &block) if block_given?
420
+ Shoes::App.instance
421
+ end
422
+
423
+ private
424
+
425
+ def generate_debug_id
426
+ cl = caller_locations(3)
427
+ da = cl.detect { |loc| !loc.path.include?("lacci/lib/shoes") }
428
+ @drawable_defined_at = "#{File.basename(da.path)}:#{da.lineno}"
429
+
430
+ class_name = self.class.name.split("::")[-1]
431
+
432
+ @debug_id = "#{class_name}##{@linkable_id}(#{@drawable_defined_at})"
433
+ end
434
+
435
+ public
436
+
437
+ def inspect
438
+ "#<#{debug_id} " +
439
+ " @parent=#{@parent ? @parent.debug_id : "(none)"} " +
440
+ "@children=#{@children ? @children.map(&:debug_id) : "(none)"} properties=#{shoes_style_values.inspect}>"
441
+ end
442
+
443
+ private
444
+
445
+ def validate_event_name(event_name)
446
+ events = self.class.get_shoes_events
447
+ unless events.include?(event_name.to_s)
448
+ raise Shoes::Errors::UnregisteredShoesEventError, "Drawable #{self.inspect} tried to bind Shoes event #{event_name}, which is not in #{events.inspect}!"
449
+ end
450
+ end
451
+
452
+ def bind_self_event(event_name, &block)
453
+ raise(Shoes::Errors::NoSuchLinkableIdError, "Drawable has no linkable_id! #{inspect}") unless linkable_id
454
+
455
+ validate_event_name(event_name)
456
+
457
+ bind_shoes_event(event_name: event_name, target: linkable_id, &block)
458
+ end
459
+
460
+ def bind_no_target_event(event_name, &block)
461
+ validate_event_name(event_name)
462
+
463
+ bind_shoes_event(event_name:, &block)
464
+ end
465
+
466
+ public
467
+
468
+ def event(event_name, *args, **kwargs)
469
+ validate_event_name(event_name)
470
+
471
+ send_shoes_event(*args, **kwargs, event_name:, target: linkable_id)
472
+ end
473
+
474
+ def shoes_style_values
475
+ all_property_names = self.class.shoes_style_names
476
+
477
+ properties = {}
478
+ all_property_names.each do |prop|
479
+ properties[prop] = instance_variable_get("@" + prop)
480
+ end
481
+ properties["shoes_linkable_id"] = self.linkable_id
482
+ properties
483
+ end
484
+
485
+ def style(*args, **kwargs)
486
+ if args.empty? && kwargs.empty?
487
+ # Just called as .style()
488
+ shoes_style_values
489
+ elsif args.empty?
490
+ # This is called to set one or more Shoes styles
491
+ prop_names = self.class.shoes_style_names
492
+ unknown_styles = kwargs.keys.select { |k| !prop_names.include?(k.to_s) }
493
+ unless unknown_styles.empty?
494
+ raise Shoes::Errors::NoSuchStyleError, "Unknown styles for drawable type #{self.class.name}: #{unknown_styles.join(", ")}"
495
+ end
496
+
497
+ kwargs.each do |name, val|
498
+ instance_variable_set("@#{name}", val)
499
+ end
500
+ elsif args.length == 1 && args[0] < Shoes::Drawable
501
+ # Shoes supports calling .style with a Shoes class, e.g. .style(Shoes::Button, displace_left: 5)
502
+ kwargs.each do |name, val|
503
+ Shoes::Drawable.drawable_default_styles[args[0]][name.to_sym] = val
504
+ end
505
+ else
506
+ raise Shoes::Errors::InvalidAttributeValueError, "Unexpected arguments to style! args: #{args.inspect}, keyword args: #{kwargs.inspect}"
507
+ end
508
+ end
509
+
510
+ private
511
+
512
+ def create_display_drawable
513
+ klass_name = self.class.name.delete_prefix("Scarpe::").delete_prefix("Shoes::")
514
+
515
+ is_widget = Shoes::Drawable.is_widget_class?(klass_name)
516
+ parent_id = @parent&.linkable_id
517
+
518
+ # Should we send an event so this can be discovered from someplace other than
519
+ # the DisplayService?
520
+ ::Shoes::DisplayService.display_service.create_display_drawable_for(klass_name, self.linkable_id, shoes_style_values, parent_id:, is_widget:)
521
+ end
522
+
523
+ public
524
+
525
+ attr_reader :parent
526
+ attr_reader :destroyed
527
+
528
+ # Set the Drawable's parent drawable. Notify the display service
529
+ # that the parent has changed unless instructed not to.
530
+ # We don't notify when first creating the Drawable. The create
531
+ # event that is first sent will include the parent.
532
+ def set_parent(new_parent, notify: true)
533
+ @parent&.remove_child(self)
534
+ new_parent&.add_child(self)
535
+ @parent = new_parent
536
+ return unless notify
537
+
538
+ send_shoes_event(new_parent&.linkable_id, event_name: "parent", target: linkable_id)
539
+ end
540
+
541
+ # Removes the element from the Shoes::Drawable tree and removes all event subscriptions
542
+ def destroy
543
+ @parent&.remove_child(self)
544
+ @parent = nil
545
+ @destroyed = true
546
+ unsub_all_shoes_events
547
+ send_shoes_event(event_name: "destroy", target: linkable_id)
548
+ Shoes::Drawable.unregister_drawable_id(linkable_id)
549
+ end
550
+ alias_method :remove, :destroy
551
+
552
+ # Hide the drawable.
553
+ def hide
554
+ self.hidden = true
555
+ end
556
+
557
+ # Show the drawable.
558
+ def show
559
+ self.hidden = false
560
+ end
561
+
562
+ # Hide the drawable if it is currently shown. Show it if it is currently hidden.
563
+ def toggle
564
+ self.hidden = !self.hidden
565
+ end
566
+
567
+ # Set the hover handler. Not every drawable may do something useful with this.
568
+ #
569
+ # @yield A block to be called when the cursor moves to be over the drawable.
570
+ def hover(&block)
571
+ @hover = block
572
+ end
573
+
574
+ # Set the leave handler. Not every drawable may do something useful with this.
575
+ #
576
+ # @yield A block to be called when the cursor moves to be off of the drawable.
577
+ def leave(&block)
578
+ @leave = block
579
+ end
580
+
581
+ # Set the motion handler, called with x and y coordinates as params.
582
+ # Not every drawable may do something useful with this.
583
+ #
584
+ # @yield A block to be called when the cursor moves around over the drawable.
585
+ def motion(&block)
586
+ @motion = block
587
+ end
588
+
589
+ # We use method_missing to auto-create Shoes style getters and setters.
590
+ def method_missing(name, *args, **kwargs, &block)
591
+ name_s = name.to_s
592
+
593
+ if name_s[-1] == "="
594
+ prop_name = name_s[0..-2]
595
+ if self.class.shoes_style_name?(prop_name)
596
+ self.class.define_method(name) do |new_value|
597
+ raise(Shoes::Errors::NoSuchLinkableIdError, "Trying to set Shoes styles in a #{self.class} with no linkable ID!") unless linkable_id
598
+
599
+ new_value = self.class.validate_as(prop_name, new_value)
600
+ instance_variable_set("@" + prop_name, new_value)
601
+ send_shoes_event({ prop_name => new_value }, event_name: "prop_change", target: linkable_id)
602
+ end
603
+
604
+ return self.send(name, *args, **kwargs, &block)
605
+ end
606
+ end
607
+
608
+ if self.class.shoes_style_name?(name_s)
609
+ self.class.define_method(name) do
610
+ raise(Shoes::Errors::NoSuchLinkableIdError, "Trying to get Shoes styles in an object with no linkable ID! #{inspect}") unless linkable_id
611
+
612
+ instance_variable_get("@" + name_s)
613
+ end
614
+
615
+ return self.send(name, *args, **kwargs, &block)
616
+ end
617
+
618
+ super(name, *args, **kwargs, &block)
619
+ end
620
+
621
+ def respond_to_missing?(name, include_private = false)
622
+ name_s = name.to_s
623
+ return true if self.class.shoes_style_name?(name_s)
624
+ return true if self.class.shoes_style_name?(name_s[0..-2]) && name_s[-1] == "="
625
+ return true if Drawable.drawable_class_by_name(name_s)
626
+
627
+ super
628
+ end
629
+
630
+ def self.convert_to_integer(value, attribute_name)
631
+ begin
632
+ value = Integer(value)
633
+ raise Shoes::Errors::InvalidAttributeValueError, "Negative number '#{value}' not allowed for attribute '#{attribute_name}'" if value < 0
634
+
635
+ value
636
+ rescue ArgumentError
637
+ error_message = "Invalid value '#{value}' provided for attribute '#{attribute_name}'. The value should be a number."
638
+ raise Shoes::Errors::InvalidAttributeValueError, error_message
639
+ end
640
+ end
641
+
642
+ def self.convert_to_float(value, attribute_name)
643
+ begin
644
+ value = Float(value)
645
+ raise Shoes::Errors::InvalidAttributeValueError, "Negative number '#{value}' not allowed for attribute '#{attribute_name}'" if value < 0
646
+
647
+ value
648
+ rescue ArgumentError
649
+ error_message = "Invalid value '#{value}' provided for attribute '#{attribute_name}'. The value should be a number."
650
+ raise Shoes::Errors::InvalidAttributeValueError, error_message
651
+ end
652
+ end
653
+ end
654
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Shoes
4
+ class Arc < Shoes::Drawable
5
+ shoes_style :draw_context
6
+ shoes_events # No Arc-specific events yet
7
+
8
+ [:left, :top, :width, :height].each do |prop|
9
+ shoes_style(prop) { |val| convert_to_integer(val, prop) }
10
+ end
11
+
12
+ [:angle1, :angle2].each do |prop|
13
+ shoes_style(prop) { |val| convert_to_float(val, prop) }
14
+ end
15
+
16
+ init_args :left, :top, :width, :height, :angle1, :angle2
17
+ def initialize(*args, **kwargs)
18
+ @draw_context = Shoes::App.instance.current_draw_context
19
+
20
+ super
21
+
22
+ create_display_drawable
23
+ end
24
+
25
+
26
+ end
27
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Shoes
4
+ class Arrow < Shoes::Drawable
5
+ shoes_style :draw_context
6
+ shoes_events # No Arrow-specific events yet
7
+
8
+ [:left, :top, :width].each do |prop|
9
+ shoes_style(prop) { |val| val.is_a?(Hash) ? val : convert_to_integer(val, prop) }
10
+ end
11
+
12
+ init_args :left, :top, :width
13
+ def initialize(*args, **kwargs)
14
+ @draw_context = Shoes::App.instance.current_draw_context
15
+
16
+ super
17
+
18
+ create_display_drawable
19
+ end
20
+ end
21
+ end