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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: aab735aab6fb64d87496b2ebefac1235a3858b1e14f176a2b165bf0c82c10260
4
- data.tar.gz: 961b0b461164093bf51bf2ab7f79ae95e626a561548c7421bdcc75efe199e4e1
3
+ metadata.gz: 3bf9fa7d8c25eece259eb65e02ac680c3004762fa86951597ae1721010a8af52
4
+ data.tar.gz: b997f33fc1f63480f38144f89c54af16a3f99c7eafaf960b53770ea4894b51e6
5
5
  SHA512:
6
- metadata.gz: 3e3bffa1f9f822b082ed7268458f0a93c49614a3b6169f4648fe57c126d77a4131d5113a15ca2a7421fe3271746ba36eb10faed83a326c4eb9514ec0447eaecf
7
- data.tar.gz: 7f2c9d2e4672faea8f3f5613a9b4bdc4c78b65e9443a669c166a76d95cee2c305095eda4688418bc4b8a3d804e9ea2662fa09f0a906b61435a9aa3d019367cce
6
+ metadata.gz: 7eb784a865dfd9add9ade9861a62cfa2192e407d1371e49cdd23eb6205c55c36f67d6a31347775ee6b3d43c6e025ea1788e6c51dedf4251a562dcb62f2cda1e5
7
+ data.tar.gz: ea5b4aef895ee41ea78699bac827f8404ebbef97f0d7e8ed93b41382076c3188538e70aa32016b5ff90bd81b05feca8effc977f5452efe30744f25a7d3fe845f
data/Gemfile CHANGED
@@ -4,6 +4,8 @@ source "https://rubygems.org"
4
4
 
5
5
  gemspec
6
6
 
7
+ gem "scarpe-components", path: "../scarpe-components"
8
+
7
9
  gem "rake", "~> 13.0"
8
10
 
9
11
  group :test do
data/Gemfile.lock CHANGED
@@ -1,7 +1,13 @@
1
+ PATH
2
+ remote: ../scarpe-components
3
+ specs:
4
+ scarpe-components (0.3.0)
5
+
1
6
  PATH
2
7
  remote: .
3
8
  specs:
4
- lacci (0.2.1)
9
+ lacci (0.3.0)
10
+ scarpe-components
5
11
 
6
12
  GEM
7
13
  remote: https://rubygems.org/
@@ -66,6 +72,7 @@ DEPENDENCIES
66
72
  redcarpet
67
73
  rubocop (~> 1.21)
68
74
  rubocop-shopify
75
+ scarpe-components!
69
76
  yard
70
77
 
71
78
  BUNDLED WITH
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class Scarpe
3
+ module Scarpe
4
4
  module CLI
5
5
  DEFAULT_USAGE = <<~'USAGE'
6
6
  Usage: scarpe_core [OPTIONS] <scarpe app file> # Same as "scarpe run"
@@ -23,11 +23,11 @@ class Scarpe
23
23
  end
24
24
 
25
25
  def default_env_categories
26
+ require "shoes"
26
27
  {
27
28
  "Lacci" => [
28
29
  env_or_default("SCARPE_DISPLAY_SERVICE", "(none)"),
29
30
  env_or_default("SCARPE_LOG_CONFIG", "(default)#{Shoes::Log::DEFAULT_LOG_CONFIG.inspect}"),
30
- env_or_default("SCARPE_APP_TEST", "(none)"),
31
31
  ],
32
32
  "Ruby and Shell" => [
33
33
  ["RUBY_DESCRIPTION", RUBY_DESCRIPTION],
@@ -12,9 +12,10 @@ if RUBY_VERSION[0..2] < "3.2"
12
12
  exit(-1)
13
13
  end
14
14
 
15
- class Scarpe; end
15
+ module Scarpe; end
16
16
 
17
17
  # The base error class for Scarpe errors, but not necessarily {Shoes::Error}s
18
+ class Shoes::Error < StandardError; end
18
19
  class Scarpe::Error < StandardError; end
19
20
 
20
21
  require "lacci/version"
data/lib/lacci/version.rb CHANGED
@@ -9,5 +9,5 @@
9
9
  # mostly invisible. Instead, look at the {Shoes} module
10
10
  # to see what's in Lacci.
11
11
  module Lacci
12
- VERSION = "0.2.2"
12
+ VERSION = "0.4.0"
13
13
  end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Niente
4
+ class App < Drawable
5
+ def initialize(properties)
6
+ super
7
+
8
+ bind_shoes_event(event_name: "init") { init }
9
+ bind_shoes_event(event_name: "run") { run }
10
+ bind_shoes_event(event_name: "destroy") { destroy }
11
+ end
12
+
13
+ def init
14
+ end
15
+
16
+ def run
17
+ send_shoes_event("wait", event_name: "custom_event_loop")
18
+ end
19
+
20
+ def destroy
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Niente
4
+ # This is a "null" DisplayService, doing as little as it
5
+ # can get away with.
6
+ class DisplayService < Shoes::DisplayService
7
+ include Shoes::Log
8
+
9
+ class << self
10
+ attr_accessor :instance
11
+ end
12
+
13
+ def initialize
14
+ if Niente::DisplayService.instance
15
+ raise Shoes::SingletonError, "ERROR! This is meant to be a singleton!"
16
+ end
17
+
18
+ Niente::DisplayService.instance = self
19
+
20
+ log_init("Niente::DisplayService")
21
+ super()
22
+ end
23
+
24
+ # Create a fake display drawable for a specific Shoes drawable, and pair it with
25
+ # the linkable ID for this Shoes drawable.
26
+ #
27
+ # @param drawable_class_name [String] The class name of the Shoes drawable, e.g. Shoes::Button
28
+ # @param drawable_id [String] the linkable ID for drawable events
29
+ # @param properties [Hash] a JSON-serialisable Hash with the drawable's Shoes styles
30
+ # @param is_widget [Boolean] whether the class is a user-defined Shoes::Widget subclass
31
+ # @return [Webview::Drawable] the newly-created Webview drawable
32
+ def create_display_drawable_for(drawable_class_name, drawable_id, properties, parent_id:, is_widget:)
33
+ existing = query_display_drawable_for(drawable_id, nil_ok: true)
34
+ if existing
35
+ @log.warn("There is already a display drawable for #{drawable_id.inspect}! Returning #{existing.class.name}.")
36
+ return existing
37
+ end
38
+
39
+ if drawable_class_name == "App"
40
+ @app = Niente::App.new(properties)
41
+ set_drawable_pairing(drawable_id, @app)
42
+
43
+ return @app
44
+ end
45
+
46
+ display_drawable = Niente::Drawable.new(properties)
47
+ display_drawable.shoes_type = drawable_class_name
48
+ set_drawable_pairing(drawable_id, display_drawable)
49
+
50
+ # Nil parent is okay for DocumentRoot and TextDrawables, so we have to specify it.
51
+ parent = DisplayService.instance.query_display_drawable_for(parent_id, nil_ok: true)
52
+ display_drawable.set_parent(parent)
53
+
54
+ return display_drawable
55
+ end
56
+
57
+ # Destroy the display service and the app. Quit the process (eventually.)
58
+ #
59
+ # @return [void]
60
+ def destroy
61
+ @app.destroy
62
+ DisplayService.instance = nil
63
+ end
64
+ end
65
+ end
66
+
@@ -0,0 +1,59 @@
1
+ module Niente
2
+ class Drawable < Shoes::Linkable
3
+ attr_reader :shoes_linkable_id
4
+ attr_reader :parent
5
+ attr_reader :children
6
+
7
+ attr_accessor :shoes_type
8
+
9
+ def initialize(props)
10
+ @shoes_linkable_id = props.delete("shoes_linkable_id") || props.delete(:shoes_linkable_id)
11
+ @data = props
12
+
13
+ super(linkable_id: @shoes_linkable_id)
14
+
15
+ # This should only be used for reparenting after a drawable was initially created.
16
+ bind_shoes_event(event_name: "parent", target: shoes_linkable_id) do |new_parent_id|
17
+ display_parent = DisplayService.instance.query_display_drawable_for(new_parent_id)
18
+ if @parent != display_parent
19
+ set_parent(display_parent)
20
+ end
21
+ end
22
+
23
+ # When Shoes drawables change properties, we get a change notification here
24
+ bind_shoes_event(event_name: "prop_change", target: shoes_linkable_id) do |prop_changes|
25
+ prop_changes.each do |k, v|
26
+ instance_variable_set("@" + k, v)
27
+ end
28
+ properties_changed(prop_changes) if respond_to?(:properties_changed)
29
+ end
30
+
31
+ bind_shoes_event(event_name: "destroy", target: shoes_linkable_id) do
32
+ set_parent(nil)
33
+ end
34
+ end
35
+
36
+ def set_parent(new_parent)
37
+ @parent&.remove_child(self)
38
+ new_parent&.add_child(self)
39
+ @parent = new_parent
40
+ end
41
+
42
+ # Do not call directly, use set_parent
43
+ def remove_child(child)
44
+ @children ||= []
45
+ unless @children.include?(child)
46
+ STDERR.puts("remove_child: no such child(#{child.inspect}) for"\
47
+ " parent(#{parent.inspect})!")
48
+ end
49
+ @children.delete(child)
50
+ end
51
+
52
+ # Do not call directly, use set_parent
53
+ def add_child(child)
54
+ @children ||= []
55
+ @children << child
56
+ end
57
+ end
58
+ end
59
+
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "minitest"
4
+ require "scarpe/components/string_helpers"
5
+
6
+ module Niente; end
7
+
8
+ class Niente::Test
9
+ def self.run_shoes_spec_test_code(code, class_name: nil, test_name: nil)
10
+ if @shoes_spec_init
11
+ raise Shoes::Errors::MultipleShoesSpecRunsError, "Scarpe-Webview can only run a single Shoes spec per process!"
12
+ end
13
+ @shoes_spec_init = true
14
+
15
+ require "scarpe/components/minitest_export_reporter"
16
+ Minitest::Reporters::ShoesExportReporter.activate!
17
+
18
+ class_name ||= ENV["SHOES_MINITEST_CLASS_NAME"] || "TestShoesSpecCode"
19
+ test_name ||= ENV["SHOES_MINITEST_METHOD_NAME"] || "test_shoes_spec"
20
+
21
+ Shoes::DisplayService.subscribe_to_event("heartbeat", nil) do
22
+ unless @hb_init
23
+ Minitest.run []
24
+ Shoes::App.instance.destroy
25
+ end
26
+ @hb_init = true
27
+ end
28
+
29
+ test_class = Class.new(Niente::ShoesSpecTest)
30
+ Object.const_set(Scarpe::Components::StringHelpers.camelize(class_name), test_class)
31
+ test_name = "test_" + test_name unless test_name.start_with?("test_")
32
+ test_class.define_method(test_name) do
33
+ eval(code)
34
+ end
35
+ end
36
+ end
37
+
38
+ class Niente::ShoesSpecTest < Minitest::Test
39
+ Shoes::Drawable.drawable_classes.each do |drawable_class|
40
+ finder_name = drawable_class.dsl_name
41
+
42
+ define_method(finder_name) do |*args|
43
+ app = Shoes::App.instance
44
+
45
+ drawables = app.find_drawables_by(drawable_class, *args)
46
+ raise Shoes::Errors::MultipleDrawablesFoundError, "Found more than one #{finder_name} matching #{args.inspect}!" if drawables.size > 1
47
+ raise Shoes::Errors::NoDrawablesFoundError, "Found no #{finder_name} matching #{args.inspect}!" if drawables.empty?
48
+
49
+ Niente::ShoesSpecProxy.new(drawables[0])
50
+ end
51
+ end
52
+
53
+ def drawable(*specs)
54
+ drawables = Shoes::App.instance.find_drawables_by(*specs)
55
+ raise Shoes::Errors::MultipleDrawablesFoundError, "Found more than one #{finder_name} matching #{args.inspect}!" if drawables.size > 1
56
+ raise Shoes::Errors::NoDrawablesFoundError, "Found no #{finder_name} matching #{args.inspect}!" if drawables.empty?
57
+ Niente::ShoesSpecProxy.new(drawables[0])
58
+ end
59
+ end
60
+
61
+ class Niente::ShoesSpecProxy
62
+ attr_reader :obj
63
+ attr_reader :linkable_id
64
+ attr_reader :display
65
+
66
+ def initialize(obj)
67
+ @obj = obj
68
+ @linkable_id = obj.linkable_id
69
+ @display = ::Shoes::DisplayService.display_service.query_display_drawable_for(obj.linkable_id)
70
+ end
71
+
72
+ def method_missing(method, ...)
73
+ if @obj.respond_to?(method)
74
+ self.singleton_class.define_method(method) do |*args, **kwargs, &block|
75
+ @obj.send(method, *args, **kwargs, &block)
76
+ end
77
+ send(method, ...)
78
+ else
79
+ super # raise an exception
80
+ end
81
+ end
82
+
83
+ JS_EVENTS = [:click, :hover, :leave]
84
+ JS_EVENTS.each do |event|
85
+ define_method("trigger_#{event}") do |*args, **kwargs|
86
+ ::Shoes::DisplayService.dispatch_event(event.to_s, @linkable_id, *args, **kwargs)
87
+ end
88
+ end
89
+
90
+ def respond_to_missing?(method_name, include_private = false)
91
+ @obj.respond_to_missing?(method_name, include_private)
92
+ end
93
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Niente -- Italian for "nothing" -- is a null display service
4
+ # that doesn't display anything. It does very little, while
5
+ # keeping a certain amount of "mental model" or schema of
6
+ # how a real display service should act.
7
+ module Niente; end
8
+
9
+ require "scarpe/components/print_logger"
10
+ Shoes::Log.instance = Scarpe::Components::PrintLogImpl.new
11
+ if ENV["NIENTE_LOG_LEVEL"]
12
+ pl = Scarpe::Components::PrintLogImpl::PrintLogger
13
+ level = ENV["NIENTE_LOG_LEVEL"].strip.downcase.to_sym
14
+ unless pl::LEVELS.key?(level)
15
+ raise "Unrecognized Niente log level: #{level.inspect}!"
16
+ end
17
+ pl.min_level = pl::LEVELS[level]
18
+ end
19
+
20
+ require_relative "niente/drawable"
21
+ require_relative "niente/app"
22
+ require_relative "niente/display_service"
23
+
24
+ require_relative "niente/shoes_spec"
25
+ Shoes::Spec.instance = Niente::Test
26
+
27
+ require "scarpe/components/segmented_file_loader"
28
+ loader = Scarpe::Components::SegmentedFileLoader.new
29
+ Shoes.add_file_loader loader
30
+
31
+ Shoes::DisplayService.set_display_service_class(Niente::DisplayService)
32
+
data/lib/shoes/app.rb CHANGED
@@ -1,61 +1,81 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Shoes
4
- class App < Shoes::Widget
3
+ class Shoes
4
+ class App < Shoes::Drawable
5
5
  include Shoes::Log
6
6
 
7
7
  class << self
8
8
  attr_accessor :instance
9
9
  end
10
10
 
11
+ # The Shoes root of the drawable tree
11
12
  attr_reader :document_root
12
13
 
13
- display_properties :title, :width, :height, :resizable
14
+ # The application directory for this app. Often this will be the directory
15
+ # containing the launched application file.
16
+ attr_reader :dir
17
+
18
+ shoes_styles :title, :width, :height, :resizable, :features
19
+
20
+ # This is defined to avoid the linkable-id check in the Shoes-style method_missing def'n
21
+ def features
22
+ @features
23
+ end
14
24
 
15
25
  CUSTOM_EVENT_LOOP_TYPES = ["displaylib", "return", "wait"]
16
26
 
27
+ init_args
17
28
  def initialize(
18
29
  title: "Shoes!",
19
30
  width: 480,
20
31
  height: 420,
21
32
  resizable: true,
33
+ features: [],
22
34
  &app_code_body
23
35
  )
24
36
  log_init("Shoes::App")
25
37
 
26
38
  if Shoes::App.instance
27
39
  @log.error("Trying to create a second Shoes::App in the same process! Fail!")
28
- raise "Cannot create multiple Shoes::App objects!"
40
+ raise Shoes::Errors::TooManyInstancesError, "Cannot create multiple Shoes::App objects!"
29
41
  else
30
42
  Shoes::App.instance = self
31
43
  end
32
44
 
45
+ # We cd to the app's containing dir when running the app
46
+ @dir = Dir.pwd
47
+
33
48
  @do_shutdown = false
34
49
  @event_loop_type = "displaylib" # the default
35
50
 
36
- super
51
+ @features = features
37
52
 
38
- # The draw context tracks current settings like fill and stroke,
39
- # plus potentially other current state that changes from widget
40
- # to widget and slot to slot.
41
- @draw_context = {
42
- "fill" => "",
43
- "stroke" => "",
44
- }
53
+ unknown_ext = features - Shoes::FEATURES - Shoes::EXTENSIONS
54
+ unsupported_features = unknown_ext & Shoes::KNOWN_FEATURES
55
+ unless unsupported_features.empty?
56
+ @log.error("Shoes app requires feature(s) not supported by this display service: #{unsupported_features.inspect}!")
57
+ raise Shoes::Errors::UnsupportedFeatureError, "Shoes app needs features: #{unsupported_features.inspect}"
58
+ end
59
+ unless unknown_ext.empty?
60
+ @log.warn("Shoes app requested unknown features #{unknown_ext.inspect}! Known: #{(Shoes::FEATURES + Shoes::EXTENSIONS).inspect}")
61
+ end
45
62
 
46
- # This creates the DocumentRoot, including its corresponding display widget
63
+ @slots = []
64
+
65
+ super
66
+
67
+ # This creates the DocumentRoot, including its corresponding display drawable
47
68
  @document_root = Shoes::DocumentRoot.new
48
69
 
49
- @slots = []
70
+ # Now create the App display drawable
71
+ create_display_drawable
50
72
 
51
- # Now create the App display widget
52
- create_display_widget
73
+ # Set up testing *after* Display Service basic objects exist
53
74
 
54
- # Set up testing events *after* Display Service basic objects exist
55
- if ENV["SCARPE_APP_TEST"]
56
- test_code = File.read ENV["SCARPE_APP_TEST"]
57
- if test_code != ""
58
- self.instance_eval test_code
75
+ if ENV["SHOES_SPEC_TEST"]
76
+ test_code = File.read ENV["SHOES_SPEC_TEST"]
77
+ unless test_code.empty?
78
+ Shoes::Spec.instance.run_shoes_spec_test_code test_code
59
79
  end
60
80
  end
61
81
 
@@ -70,7 +90,7 @@ module Shoes
70
90
  end
71
91
 
72
92
  @watch_for_event_loop = bind_shoes_event(event_name: "custom_event_loop") do |loop_type|
73
- raise("Unknown event loop type: #{loop_type.inspect}!") unless CUSTOM_EVENT_LOOP_TYPES.include?(loop_type)
93
+ raise(Shoes::Errors::InvalidAttributeValueError, "Unknown event loop type: #{loop_type.inspect}!") unless CUSTOM_EVENT_LOOP_TYPES.include?(loop_type)
74
94
 
75
95
  @event_loop_type = loop_type
76
96
  end
@@ -89,9 +109,9 @@ module Shoes
89
109
  ::Shoes::App.instance.with_slot(@document_root, &@app_code_body)
90
110
  end
91
111
 
92
- # "Container" widgets like flows, stacks, masks and the document root
112
+ # "Container" drawables like flows, stacks, masks and the document root
93
113
  # are considered "slots" in Shoes parlance. When a new slot is created,
94
- # we push it here in order to track what widgets are found in that slot.
114
+ # we push it here in order to track what drawables are found in that slot.
95
115
  def push_slot(slot)
96
116
  @slots.push(slot)
97
117
  end
@@ -115,8 +135,26 @@ module Shoes
115
135
  pop_slot
116
136
  end
117
137
 
138
+ # We use method_missing for drawable-creating methods like "button".
139
+ # The parent's method_missing will auto-create Shoes style getters and setters.
140
+ # This is similar to the method_missing in Shoes::Slot, but different in
141
+ # where the new drawable appears.
142
+ def method_missing(name, *args, **kwargs, &block)
143
+ klass = ::Shoes::Drawable.drawable_class_by_name(name)
144
+ return super unless klass
145
+
146
+ ::Shoes::App.define_method(name) do |*args, **kwargs, &block|
147
+ klass.new(*args, **kwargs, &block)
148
+ end
149
+
150
+ send(name, *args, **kwargs, &block)
151
+ end
152
+
153
+ # Get the current draw context for the current slot
154
+ #
155
+ # @return [Hash] a hash of Shoes styles for the current draw context
118
156
  def current_draw_context
119
- @draw_context.dup
157
+ current_slot&.current_draw_context
120
158
  end
121
159
 
122
160
  # This usually doesn't return. The display service may take control
@@ -136,7 +174,9 @@ module Shoes
136
174
  case @event_loop_type
137
175
  when "wait"
138
176
  # Display lib wants us to busy-wait instead of it.
139
- sleep 0.1 until @do_shutdown
177
+ until @do_shutdown
178
+ Shoes::DisplayService.dispatch_event("heartbeat", nil)
179
+ end
140
180
  when "displaylib"
141
181
  # If run event returned, that means we're done.
142
182
  destroy
@@ -144,7 +184,7 @@ module Shoes
144
184
  # We can just return to the main event loop. But we shouldn't call destroy.
145
185
  # Presumably some event loop *outside* our event loop is handling things.
146
186
  else
147
- raise "Internal error! Incorrect event loop type: #{@event_loop_type.inspect}!"
187
+ raise Shoes::Errors::InvalidAttributeValueError, "Internal error! Incorrect event loop type: #{@event_loop_type.inspect}!"
148
188
  end
149
189
  end
150
190
 
@@ -153,10 +193,10 @@ module Shoes
153
193
  send_shoes_event(event_name: "destroy") if send_event
154
194
  end
155
195
 
156
- def all_widgets
196
+ def all_drawables
157
197
  out = []
158
198
 
159
- to_add = @document_root.children
199
+ to_add = [@document_root, @document_root.children]
160
200
  until to_add.empty?
161
201
  out.concat(to_add)
162
202
  to_add = to_add.flat_map { |w| w.respond_to?(:children) ? w.children : [] }.compact
@@ -165,13 +205,15 @@ module Shoes
165
205
  out
166
206
  end
167
207
 
168
- # We can add various ways to find widgets here.
208
+ # We can add various ways to find drawables here.
169
209
  # These are sort of like Shoes selectors, used for testing.
170
- def find_widgets_by(*specs)
171
- widgets = all_widgets
210
+ def find_drawables_by(*specs)
211
+ drawables = all_drawables
172
212
  specs.each do |spec|
173
- if spec.is_a?(Class)
174
- widgets.select! { |w| spec === w }
213
+ if spec == Shoes::App
214
+ drawables = [Shoes::App.instance]
215
+ elsif spec.is_a?(Class)
216
+ drawables.select! { |w| spec === w }
175
217
  elsif spec.is_a?(Symbol) || spec.is_a?(String)
176
218
  s = spec.to_s
177
219
  case s[0]
@@ -179,71 +221,68 @@ module Shoes
179
221
  begin
180
222
  # I'm not finding a global_variable_get or similar...
181
223
  global_value = eval s
182
- widgets &= [global_value]
224
+ drawables &= [global_value]
183
225
  rescue
184
- raise "Error getting global variable: #{spec.inspect}"
226
+ raise Shoes::Errors::InvalidAttributeValueError, "Error getting global variable: #{spec.inspect}"
185
227
  end
186
228
  when "@"
187
229
  if Shoes::App.instance.instance_variables.include?(spec.to_sym)
188
- widgets &= [self.instance_variable_get(spec)]
230
+ drawables &= [self.instance_variable_get(spec)]
189
231
  else
190
- raise "Can't find top-level instance variable: #{spec.inspect}!"
232
+ raise Shoes::Errors::InvalidAttributeValueError, "Can't find top-level instance variable: #{spec.inspect}!"
191
233
  end
192
234
  else
235
+ if s.start_with?("id:")
236
+ find_id = Integer(s[3..-1])
237
+ drawable = Shoes::Drawable.drawable_by_id(find_id)
238
+ drawables &= [drawable]
239
+ else
240
+ raise Shoes::Errors::InvalidAttributeValueError, "Don't know how to find drawables by #{spec.inspect}!"
241
+ end
193
242
  end
194
243
  else
195
- raise("Don't know how to find widgets by #{spec.inspect}!")
244
+ raise(Shoes::Errors::InvalidAttributeValueError, "Don't know how to find drawables by #{spec.inspect}!")
196
245
  end
197
246
  end
198
- widgets
247
+ drawables
199
248
  end
200
249
  end
201
250
  end
202
251
 
203
- # DSL methods
204
- class Shoes::App
252
+ # Event handler DSLs get defined in both App and Slot - same code, slightly different results
253
+ events = [:motion, :hover, :leave, :click, :release, :keypress, :animate, :every, :timer]
254
+ events.each do |event|
255
+ Shoes::App.define_method(event) do |*args, &block|
256
+ subscription_item(args:, shoes_api_name: event.to_s, &block)
257
+ end
258
+ Shoes::Slot.define_method(event) do |*args, &block|
259
+ subscription_item(args:, shoes_api_name: event.to_s, &block)
260
+ end
261
+ end
262
+
263
+ # These methods will need to be defined on Slots too, but probably need a rework in general.
264
+ class Shoes::App < Shoes::Drawable
265
+ # This is going to go away. See issue #496
205
266
  def background(...)
206
267
  current_slot.background(...)
207
268
  end
208
269
 
270
+ # This is going to go away. See issue #498
209
271
  def border(...)
210
272
  current_slot.border(...)
211
273
  end
212
274
 
213
- def motion(&block)
214
- subscription_item(shoes_api_name: "motion", &block)
215
- end
216
-
217
- def hover(&block)
218
- subscription_item(shoes_api_name: "hover", &block)
219
- end
220
-
221
- def click(&block)
222
- subscription_item(shoes_api_name: "click", &block)
223
- end
224
-
225
- # Draw context methods
226
-
227
- def fill(color)
228
- @draw_context["fill"] = color
229
- end
230
-
231
- def nofill
232
- @draw_context["fill"] = ""
233
- end
234
-
235
- def stroke(color)
236
- @draw_context["stroke"] = color
237
- end
238
-
239
- def nostroke
240
- @draw_context["stroke"] = ""
275
+ # Draw Context methods -- forward to the current slot
276
+ [:fill, :nofill, :stroke, :strokewidth, :nostroke, :rotate].each do |dc_method|
277
+ define_method(dc_method) do |*args|
278
+ current_slot.send(dc_method, *args)
279
+ end
241
280
  end
242
281
 
243
282
  # Shape DSL methods
244
283
 
245
284
  def move_to(x, y)
246
- raise("Pass only Numeric arguments to move_to!") unless x.is_a?(Numeric) && y.is_a?(Numeric)
285
+ raise(Shoes::Errors::InvalidAttributeValueError, "Pass only Numeric arguments to move_to!") unless x.is_a?(Numeric) && y.is_a?(Numeric)
247
286
 
248
287
  if current_slot.is_a?(::Shoes::Shape)
249
288
  current_slot.add_shape_command(["move_to", x, y])
@@ -251,7 +290,7 @@ class Shoes::App
251
290
  end
252
291
 
253
292
  def line_to(x, y)
254
- raise("Pass only Numeric arguments to line_to!") unless x.is_a?(Numeric) && y.is_a?(Numeric)
293
+ raise(Shoes::Errors::InvalidAttributeValueError, "Pass only Numeric arguments to line_to!") unless x.is_a?(Numeric) && y.is_a?(Numeric)
255
294
 
256
295
  if current_slot.is_a?(::Shoes::Shape)
257
296
  current_slot.add_shape_command(["line_to", x, y])