cura 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +45 -21
  3. data/cura.gemspec +1 -1
  4. data/examples/hello_world/lib/hello_world.rb +10 -10
  5. data/examples/mruby-examples/mrbgem.rake +5 -6
  6. data/examples/todo_list/data.db +0 -0
  7. data/examples/todo_list/lib/todo_list/application.rb +24 -18
  8. data/lib/cura/adapter.rb +13 -20
  9. data/lib/cura/application.rb +47 -51
  10. data/lib/cura/attributes/has_ancestry.rb +4 -8
  11. data/lib/cura/attributes/has_application.rb +3 -7
  12. data/lib/cura/attributes/has_attributes.rb +1 -9
  13. data/lib/cura/attributes/has_children.rb +14 -20
  14. data/lib/cura/attributes/has_colors.rb +14 -18
  15. data/lib/cura/attributes/has_coordinates.rb +9 -15
  16. data/lib/cura/attributes/has_dimensions.rb +12 -18
  17. data/lib/cura/attributes/has_events.rb +10 -18
  18. data/lib/cura/attributes/has_focusability.rb +5 -11
  19. data/lib/cura/attributes/has_initialize.rb +1 -5
  20. data/lib/cura/attributes/has_offsets.rb +16 -20
  21. data/lib/cura/attributes/has_orientation.rb +12 -18
  22. data/lib/cura/attributes/has_relative_coordinates.rb +4 -8
  23. data/lib/cura/attributes/has_root.rb +18 -22
  24. data/lib/cura/attributes/has_side_attributes.rb +18 -24
  25. data/lib/cura/attributes/has_windows.rb +13 -19
  26. data/lib/cura/borders.rb +0 -4
  27. data/lib/cura/color.rb +84 -91
  28. data/lib/cura/component/base.rb +29 -33
  29. data/lib/cura/component/button.rb +10 -16
  30. data/lib/cura/component/group.rb +14 -18
  31. data/lib/cura/component/label.rb +44 -48
  32. data/lib/cura/component/listbox.rb +24 -28
  33. data/lib/cura/component/pack.rb +14 -18
  34. data/lib/cura/component/scrollbar.rb +41 -45
  35. data/lib/cura/component/textbox.rb +21 -25
  36. data/lib/cura/cursor.rb +15 -23
  37. data/lib/cura/error/base.rb +0 -3
  38. data/lib/cura/error/invalid_adapter.rb +1 -7
  39. data/lib/cura/error/invalid_application.rb +1 -7
  40. data/lib/cura/error/invalid_color.rb +1 -7
  41. data/lib/cura/error/invalid_component.rb +1 -7
  42. data/lib/cura/error/invalid_middleware.rb +1 -7
  43. data/lib/cura/event.rb +4 -8
  44. data/lib/cura/event/base.rb +17 -24
  45. data/lib/cura/event/click.rb +1 -6
  46. data/lib/cura/event/dispatcher.rb +20 -26
  47. data/lib/cura/event/focus.rb +1 -6
  48. data/lib/cura/event/handler.rb +16 -24
  49. data/lib/cura/event/key_down.rb +11 -17
  50. data/lib/cura/event/middleware/aimer/base.rb +4 -10
  51. data/lib/cura/event/middleware/aimer/dispatcher_target.rb +2 -8
  52. data/lib/cura/event/middleware/aimer/mouse_focus.rb +6 -11
  53. data/lib/cura/event/middleware/aimer/target_option.rb +4 -10
  54. data/lib/cura/event/middleware/base.rb +0 -4
  55. data/lib/cura/event/middleware/dispatch.rb +0 -4
  56. data/lib/cura/event/middleware/translator/base.rb +4 -10
  57. data/lib/cura/event/middleware/translator/mouse_click.rb +4 -8
  58. data/lib/cura/event/mouse.rb +5 -11
  59. data/lib/cura/event/mouse_button.rb +21 -27
  60. data/lib/cura/event/mouse_wheel_down.rb +1 -6
  61. data/lib/cura/event/mouse_wheel_up.rb +1 -6
  62. data/lib/cura/event/resize.rb +0 -4
  63. data/lib/cura/event/selected.rb +1 -6
  64. data/lib/cura/event/unfocus.rb +1 -6
  65. data/lib/cura/focus_controller.rb +19 -23
  66. data/lib/cura/key.rb +277 -283
  67. data/lib/cura/margins.rb +0 -4
  68. data/lib/cura/offsets.rb +14 -18
  69. data/lib/cura/padding.rb +0 -4
  70. data/lib/cura/pencil.rb +3 -7
  71. data/lib/cura/version.rb +1 -3
  72. data/lib/cura/window.rb +11 -16
  73. data/spec/cura/attributes/has_ancestry_spec.rb +39 -39
  74. data/spec/cura/attributes/has_application_spec.rb +20 -20
  75. data/spec/cura/attributes/has_attributes_spec.rb +26 -26
  76. data/spec/cura/attributes/has_children_spec.rb +54 -54
  77. data/spec/cura/attributes/has_colors_spec.rb +4 -4
  78. data/spec/cura/attributes/has_coordinates_spec.rb +4 -4
  79. data/spec/cura/attributes/has_dimensions_spec.rb +4 -4
  80. data/spec/cura/attributes/has_events_spec.rb +4 -4
  81. data/spec/cura/attributes/has_focusability_spec.rb +18 -18
  82. data/spec/cura/attributes/has_offsets_spec.rb +4 -4
  83. data/spec/cura/attributes/has_orientation_spec.rb +38 -38
  84. data/spec/cura/attributes/has_relative_coordinates_spec.rb +4 -4
  85. data/spec/cura/attributes/has_side_attributes_spec.rb +4 -4
  86. data/spec/spec_helper.rb +1 -1
  87. data/spec/support/shared_examples_for_attributes.rb +41 -41
  88. metadata +1 -1
@@ -1,15 +1,11 @@
1
- if Kernel.respond_to?(:require)
2
- require "cura/event/middleware/aimer/base"
3
- end
1
+ require "cura/event/middleware/aimer/base" if Kernel.respond_to?(:require)
4
2
 
5
3
  module Cura
6
4
  module Event
7
5
  module Middleware
8
6
  module Aimer
9
-
10
7
  # Sets the event's target to the component passed by an optional :target option.
11
8
  class TargetOption < Base
12
-
13
9
  # Call this middleware.
14
10
  #
15
11
  # @param [#to_h] options
@@ -19,19 +15,17 @@ module Cura
19
15
  def call(options={})
20
16
  super # Only here for documentation
21
17
  end
22
-
18
+
23
19
  protected
24
-
20
+
25
21
  def should_aim?(options={})
26
22
  options.key?(:target)
27
23
  end
28
-
24
+
29
25
  def set_target(options={})
30
26
  options[:event].target = options[:target]
31
27
  end
32
-
33
28
  end
34
-
35
29
  end
36
30
  end
37
31
  end
@@ -1,10 +1,8 @@
1
1
  module Cura
2
2
  module Event
3
3
  module Middleware
4
-
5
4
  # The base class for event middleware.
6
5
  class Base
7
-
8
6
  # Call this middleware.
9
7
  #
10
8
  # @param [#to_h] options
@@ -13,9 +11,7 @@ module Cura
13
11
  def call(_options={})
14
12
  # Does nothing on purpose
15
13
  end
16
-
17
14
  end
18
-
19
15
  end
20
16
  end
21
17
  end
@@ -1,10 +1,8 @@
1
1
  module Cura
2
2
  module Event
3
3
  module Middleware
4
-
5
4
  # Dispatches the event.
6
5
  class Dispatch < Base
7
-
8
6
  # Dispatch the event.
9
7
  #
10
8
  # @param [#to_h] options
@@ -12,9 +10,7 @@ module Cura
12
10
  def call(options={})
13
11
  options[:dispatch_queue] << options[:event]
14
12
  end
15
-
16
13
  end
17
-
18
14
  end
19
15
  end
20
16
  end
@@ -1,15 +1,11 @@
1
- if Kernel.respond_to?(:require)
2
- require "cura/event/middleware/base"
3
- end
1
+ require "cura/event/middleware/base" if Kernel.respond_to?(:require)
4
2
 
5
3
  module Cura
6
4
  module Event
7
5
  module Middleware
8
6
  module Translator
9
-
10
7
  # The base class for event middleware which detects multiple events and dispatches a new one when found.
11
8
  class Base
12
-
13
9
  # Call this middleware.
14
10
  #
15
11
  # @param [#to_h] options
@@ -18,19 +14,17 @@ module Cura
18
14
  def call(options={})
19
15
  translate_event(options) if should_translate?(options)
20
16
  end
21
-
17
+
22
18
  protected
23
-
19
+
24
20
  def should_translate?(_options={})
25
21
  false
26
22
  end
27
-
23
+
28
24
  def translate_event(_options={})
29
25
  # Does nothing on purpose
30
26
  end
31
-
32
27
  end
33
-
34
28
  end
35
29
  end
36
30
  end
@@ -7,14 +7,12 @@ module Cura
7
7
  module Event
8
8
  module Middleware
9
9
  module Translator
10
-
11
10
  # Translates MouseDown and MouseUp events into a MouseClick event.
12
11
  class MouseClick < Base
13
-
14
12
  def initialize
15
13
  @last_mouse_down_at = Time.now
16
14
  end
17
-
15
+
18
16
  # Call this middleware.
19
17
  #
20
18
  # @param [#to_h] options
@@ -22,22 +20,20 @@ module Cura
22
20
  # @option options [Event::Base] :event
23
21
  def call(options={})
24
22
  event = options[:event]
25
-
23
+
26
24
  return unless event.is_a?(Event::Mouse)
27
-
25
+
28
26
  if event.down?
29
27
  @last_mouse_down_at = event.created_at
30
28
  @last_target = event.target
31
29
  elsif event.up? && event.created_at > @last_mouse_down_at && @last_target.respond_to?(:contains_coordinates) && @last_target.contains_coordinates?(x: event.x, y: event.y)
32
30
  @last_mouse_down_at = nil
33
31
  @last_target = nil
34
-
32
+
35
33
  options[:dispatch_queue] << Event.new_from_name(:mouse_button, state: :click, target: event.target, x: event.x, y: event.y) # TODO: Left? Right? Termbox can't tell..
36
34
  end
37
35
  end
38
-
39
36
  end
40
-
41
37
  end
42
38
  end
43
39
  end
@@ -1,34 +1,28 @@
1
- if Kernel.respond_to?(:require)
2
- require "cura/event/base"
3
- end
1
+ require "cura/event/base" if Kernel.respond_to?(:require)
4
2
 
5
3
  module Cura
6
4
  module Event
7
-
8
5
  # Dispatched when a mouse's button state changes.
9
6
  class Mouse < Base
10
-
11
7
  # Get the X coordinate where the mouse button was pressed.
12
8
  #
13
9
  # @return [Integer]
14
10
  attr_reader :x
15
-
11
+
16
12
  # Get the Y coordinate where the mouse button was pressed.
17
13
  #
18
14
  # @return [Integer]
19
15
  attr_reader :y
20
-
16
+
21
17
  protected
22
-
18
+
23
19
  def x=(value)
24
20
  @x = value.to_i
25
21
  end
26
-
22
+
27
23
  def y=(value)
28
24
  @y = value.to_i
29
25
  end
30
-
31
26
  end
32
-
33
27
  end
34
28
  end
@@ -1,103 +1,97 @@
1
- if Kernel.respond_to?(:require)
2
- require "cura/event/mouse"
3
- end
1
+ require "cura/event/mouse" if Kernel.respond_to?(:require)
4
2
 
5
3
  module Cura
6
4
  module Event
7
-
8
5
  # Dispatched when a mouse's button state changes.
9
6
  class MouseButton < Mouse
10
-
11
7
  VALID_NAMES = [:left, :middle, :right]
12
8
  VALID_STATES = [:up, :down, :click, :double_click]
13
-
9
+
14
10
  def initialize(attributes={})
15
11
  super
16
-
12
+
17
13
  # raise ArgumentError, "name must be set" if @name.nil? # TODO: Termbox doesn't support which button was released yet
18
14
  raise ArgumentError, "state must be set" if @state.nil?
19
15
  end
20
-
16
+
21
17
  # @method state
22
18
  # Get the mouse button state.
23
19
  # Will return `:up`, `:down`, `:click`, or `:double_click`.
24
20
  #
25
21
  # @return [Symbol]
26
-
22
+
27
23
  # @method state=(value)
28
24
  # Set the mouse button state.
29
25
  #
30
26
  # @param [#to_sym] value
31
27
  # @return [Symbol]
32
-
28
+
33
29
  attribute(:state) { |value| validate_list(value, VALID_STATES) }
34
-
30
+
35
31
  # @method name
36
32
  # Get the mouse button name.
37
33
  # Will return `:left`, `:middle`, or `:right`.
38
34
  #
39
35
  # @return [Symbol]
40
-
36
+
41
37
  # @method name=(value)
42
38
  # Set the mouse button name.
43
39
  #
44
40
  # @param [#to_sym] value
45
41
  # @return [Symbol]
46
-
42
+
47
43
  attribute(:name) { |value| validate_list(value, VALID_NAMES) }
48
-
44
+
49
45
  # @method left?
50
46
  # Get whether the mouse button state occurred the left button.
51
47
  #
52
48
  # @return [Boolean]
53
-
49
+
54
50
  # @method middle?
55
51
  # Get whether the mouse button state occurred the middle button.
56
52
  #
57
53
  # @return [Boolean]
58
-
54
+
59
55
  # @method right?
60
56
  # Get whether the mouse button state occurred the right button.
61
57
  #
62
58
  # @return [Boolean]
63
-
59
+
64
60
  VALID_NAMES.each do |name|
65
61
  define_method("#{name}?") { @name == name }
66
62
  end
67
-
63
+
68
64
  # @method up?
69
65
  # Get whether the mouse button state is up.
70
66
  #
71
67
  # @return [Boolean]
72
-
68
+
73
69
  # @method down?
74
70
  # Get whether the mouse button state is down.
75
71
  #
76
72
  # @return [Boolean]
77
-
73
+
78
74
  # @method click?
79
75
  # Get whether the mouse button state is click.
80
76
  #
81
77
  # @return [Boolean]
82
-
78
+
83
79
  # @method click?
84
80
  # Get whether the mouse button state is double_click.
85
81
  #
86
82
  # @return [Boolean]
87
-
83
+
88
84
  VALID_STATES.each do |state|
89
85
  define_method("#{state}?") { @state == state }
90
86
  end
91
-
87
+
92
88
  protected
93
-
89
+
94
90
  def validate_list(value, list)
95
91
  raise ArgumentError, "must be one of #{list.join(', ')}" unless list.include?(value)
96
-
92
+
97
93
  value.to_sym
98
94
  end
99
-
100
95
  end
101
-
102
96
  end
103
97
  end
@@ -1,14 +1,9 @@
1
- if Kernel.respond_to?(:require)
2
- require "cura/event/mouse"
3
- end
1
+ require "cura/event/mouse" if Kernel.respond_to?(:require)
4
2
 
5
3
  module Cura
6
4
  module Event
7
-
8
5
  # Dispatched when a mouse's wheel is scrolled down.
9
6
  class MouseWheelDown < Mouse
10
-
11
7
  end
12
-
13
8
  end
14
9
  end
@@ -1,14 +1,9 @@
1
- if Kernel.respond_to?(:require)
2
- require "cura/event/mouse"
3
- end
1
+ require "cura/event/mouse" if Kernel.respond_to?(:require)
4
2
 
5
3
  module Cura
6
4
  module Event
7
-
8
5
  # Dispatched when a mouse's wheel is scrolled up.
9
6
  class MouseWheelUp < Mouse
10
-
11
7
  end
12
-
13
8
  end
14
9
  end
@@ -5,13 +5,9 @@ end
5
5
 
6
6
  module Cura
7
7
  module Event
8
-
9
8
  # Dispatched when an object is resized.
10
9
  class Resize < Base
11
-
12
10
  include Attributes::HasDimensions
13
-
14
11
  end
15
-
16
12
  end
17
13
  end
@@ -1,14 +1,9 @@
1
- if Kernel.respond_to?(:require)
2
- require "cura/event/base"
3
- end
1
+ require "cura/event/base" if Kernel.respond_to?(:require)
4
2
 
5
3
  module Cura
6
4
  module Event
7
-
8
5
  # Dispatched when a component is selected.
9
6
  class Selected < Base
10
-
11
7
  end
12
-
13
8
  end
14
9
  end
@@ -1,14 +1,9 @@
1
- if Kernel.respond_to?(:require)
2
- require "cura/event/base"
3
- end
1
+ require "cura/event/base" if Kernel.respond_to?(:require)
4
2
 
5
3
  module Cura
6
4
  module Event
7
-
8
5
  # Dispatched when a component is unfocused.
9
6
  class Unfocus < Base
10
-
11
7
  end
12
-
13
8
  end
14
9
  end
@@ -5,85 +5,81 @@ if Kernel.respond_to?(:require)
5
5
  end
6
6
 
7
7
  module Cura
8
-
9
8
  class FocusController
10
-
11
9
  include Attributes::HasInitialize
12
10
  include Attributes::HasAttributes
13
11
  include Attributes::HasApplication
14
-
12
+
15
13
  def initialize(attributes={})
16
14
  @index = 0
17
-
15
+
18
16
  super
19
-
17
+
20
18
  # TODO: raise error if window or application is nil
21
19
  end
22
-
20
+
23
21
  # @method window
24
22
  # Get the window of the currently focused component.
25
23
  #
26
24
  # @return [Window]
27
-
25
+
28
26
  # @method window=(value)
29
27
  # Set the window of the currently focused component.
30
28
  #
31
29
  # @param [#to_i] value
32
30
  # @return [Window]
33
-
31
+
34
32
  attribute(:window) { |value| validate_window(value) }
35
-
33
+
36
34
  # @method index
37
35
  # Get the index of the currently focused component.
38
36
  #
39
37
  # @return [Integer]
40
-
38
+
41
39
  # @method index=(value)
42
40
  # Set the index of the currently focused component.
43
41
  # This will dispatch a Event::Focus instance to the object.
44
42
  #
45
43
  # @param [#to_i] value
46
44
  # @return [Integer]
47
-
45
+
48
46
  attribute(:index) { |value| set_index(value) }
49
-
47
+
50
48
  protected
51
-
49
+
52
50
  def validate_window(window)
53
51
  raise TypeError, "must be a Cura::Window" unless window.is_a?(Window)
54
-
52
+
55
53
  window
56
54
  end
57
-
55
+
58
56
  def set_index(value)
59
57
  index = value.to_i
60
58
  focusable_children = focusable_children_of(@window.root)
61
59
  index %= focusable_children.length
62
-
60
+
63
61
  @window.application.dispatcher.target = focusable_children[index]
64
-
62
+
65
63
  index
66
64
  end
67
-
65
+
68
66
  # TODO: When on a focusable component, set the index. When not on a focusable component, find nearest focusable component and set the index.
69
67
  # def update_focused_index(component)
70
68
  # focusable_children = focusable_children_of(@window.root)
71
69
  #
72
70
  # @index = focusable_children.index(component)
73
71
  # end
74
-
72
+
75
73
  # Recursively find all children which are focusable.
76
74
  def focusable_children_of(component)
77
75
  result = []
78
-
76
+
79
77
  component.children.each do |child|
80
78
  result << child if child.focusable?
81
79
  result << focusable_children_of(child) if child.respond_to?(:children)
82
80
  end
83
-
81
+
84
82
  result.flatten
85
83
  end
86
-
87
84
  end
88
-
89
85
  end