Class: RubyRich::FocusManager
- Inherits:
-
Object
- Object
- RubyRich::FocusManager
- Defined in:
- lib/ruby_rich/focus_manager.rb
Instance Attribute Summary collapse
-
#focused_name ⇒ Object
readonly
Returns the value of attribute focused_name.
Instance Method Summary collapse
- #attach(root_layout, priority: 500) ⇒ Object
- #focus(name) ⇒ Object
- #focus_next ⇒ Object
- #focused?(name) ⇒ Boolean
-
#initialize ⇒ FocusManager
constructor
A new instance of FocusManager.
- #register(name, layout, target) ⇒ Object
Constructor Details
#initialize ⇒ FocusManager
Returns a new instance of FocusManager.
7 8 9 10 |
# File 'lib/ruby_rich/focus_manager.rb', line 7 def initialize @entries = [] @focused_name = nil end |
Instance Attribute Details
#focused_name ⇒ Object (readonly)
Returns the value of attribute focused_name.
5 6 7 |
# File 'lib/ruby_rich/focus_manager.rb', line 5 def focused_name @focused_name end |
Instance Method Details
#attach(root_layout, priority: 500) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/ruby_rich/focus_manager.rb', line 18 def attach(root_layout, priority: 500) root_layout.key(:tab, priority) do |_event_data, _live| current = @entries.find { |entry| entry[:name] == @focused_name } if current && current[:target].respond_to?(:wants_tab?) && current[:target].wants_tab? false else next_entry = focus_next target = next_entry && next_entry[:target] target.ignore_next_tab if target.respond_to?(:ignore_next_tab) false end end root_layout.key(:mouse_down, priority) do |event_data, _live| entry = entry_at(event_data[:x], event_data[:y]) if entry focus(entry[:name]) false else false end end self end |
#focus(name) ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/ruby_rich/focus_manager.rb', line 45 def focus(name) entry = @entries.find { |item| item[:name] == name } return nil unless entry blur_current @focused_name = entry[:name] entry[:target].focus if entry[:target].respond_to?(:focus) entry end |
#focus_next ⇒ Object
55 56 57 58 59 60 |
# File 'lib/ruby_rich/focus_manager.rb', line 55 def focus_next return nil if @entries.empty? index = @entries.index { |entry| entry[:name] == @focused_name } || -1 focus(@entries[(index + 1) % @entries.length][:name]) end |
#focused?(name) ⇒ Boolean
62 63 64 |
# File 'lib/ruby_rich/focus_manager.rb', line 62 def focused?(name) @focused_name == name end |
#register(name, layout, target) ⇒ Object
12 13 14 15 16 |
# File 'lib/ruby_rich/focus_manager.rb', line 12 def register(name, layout, target) @entries << { name: name, layout: layout, target: target } focus(name) unless @focused_name self end |