Class: Chamomile::Viewport
- Inherits:
-
Object
- Object
- Chamomile::Viewport
- Defined in:
- lib/chamomile/components/viewport.rb,
lib/chamomile/components/viewport/key_map.rb
Overview
Scrollable content pane with key binding and mouse wheel support.
Constant Summary collapse
- DEFAULT_KEY_MAP =
KeyBinding.normalize({ up: [[:up, []], ["k", []]], down: [[:down, []], ["j", []]], page_up: [[:page_up, []], ["b", []]], page_down: [[:page_down, []], ["f", []], [" ", []]], half_page_up: [["u", [:ctrl]]], half_page_down: [["d", [:ctrl]]], goto_top: [["g", []]], goto_bottom: [["G", [:shift]]], left: [[:left, []], ["h", [:alt]]], right: [[:right, []], ["l", [:alt]]], })
Instance Attribute Summary collapse
-
#height ⇒ Object
Returns the value of attribute height.
-
#key_map ⇒ Object
Returns the value of attribute key_map.
-
#mouse_wheel_delta ⇒ Object
Returns the value of attribute mouse_wheel_delta.
-
#mouse_wheel_enabled ⇒ Object
Returns the value of attribute mouse_wheel_enabled.
-
#soft_wrap ⇒ Object
Returns the value of attribute soft_wrap.
-
#width ⇒ Object
Returns the value of attribute width.
-
#x_offset ⇒ Object
Returns the value of attribute x_offset.
-
#y_offset ⇒ Object
Returns the value of attribute y_offset.
Instance Method Summary collapse
- #at_bottom? ⇒ Boolean
-
#at_top? ⇒ Boolean
Queries.
- #content ⇒ Object
- #content=(s) ⇒ Object
- #ensure_visible(line) ⇒ Object
- #goto_bottom ⇒ Object
- #goto_top ⇒ Object
- #half_page_down ⇒ Object
- #half_page_up ⇒ Object
-
#handle(msg) ⇒ Object
(also: #update)
Handle an incoming event.
-
#initialize(width: 80, height: 24, key_map: DEFAULT_KEY_MAP) ⇒ Viewport
constructor
A new instance of Viewport.
- #max_horizontal_scroll ⇒ Object
- #page_down ⇒ Object
- #page_up ⇒ Object
- #scroll_down(n = 1) ⇒ Object
-
#scroll_left(n = 1) ⇒ Object
Horizontal scrolling.
- #scroll_percent ⇒ Object
- #scroll_right(n = 1) ⇒ Object
-
#scroll_up(n = 1) ⇒ Object
Vertical scrolling.
-
#set_content(s) ⇒ Object
Backward compat — preserves the return-self convention.
- #total_line_count ⇒ Object
- #view ⇒ Object
- #visible_line_count ⇒ Object
Constructor Details
#initialize(width: 80, height: 24, key_map: DEFAULT_KEY_MAP) ⇒ Viewport
Returns a new instance of Viewport.
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/chamomile/components/viewport.rb', line 9 def initialize(width: 80, height: 24, key_map: DEFAULT_KEY_MAP) @width = width @height = height @key_map = key_map @lines = [] @y_offset = 0 @x_offset = 0 @mouse_wheel_enabled = true @mouse_wheel_delta = 3 @soft_wrap = false end |
Instance Attribute Details
#height ⇒ Object
Returns the value of attribute height.
6 7 8 |
# File 'lib/chamomile/components/viewport.rb', line 6 def height @height end |
#key_map ⇒ Object
Returns the value of attribute key_map.
7 8 9 |
# File 'lib/chamomile/components/viewport.rb', line 7 def key_map @key_map end |
#mouse_wheel_delta ⇒ Object
Returns the value of attribute mouse_wheel_delta.
7 8 9 |
# File 'lib/chamomile/components/viewport.rb', line 7 def mouse_wheel_delta @mouse_wheel_delta end |
#mouse_wheel_enabled ⇒ Object
Returns the value of attribute mouse_wheel_enabled.
7 8 9 |
# File 'lib/chamomile/components/viewport.rb', line 7 def mouse_wheel_enabled @mouse_wheel_enabled end |
#soft_wrap ⇒ Object
Returns the value of attribute soft_wrap.
7 8 9 |
# File 'lib/chamomile/components/viewport.rb', line 7 def soft_wrap @soft_wrap end |
#width ⇒ Object
Returns the value of attribute width.
6 7 8 |
# File 'lib/chamomile/components/viewport.rb', line 6 def width @width end |
#x_offset ⇒ Object
Returns the value of attribute x_offset.
6 7 8 |
# File 'lib/chamomile/components/viewport.rb', line 6 def x_offset @x_offset end |
#y_offset ⇒ Object
Returns the value of attribute y_offset.
6 7 8 |
# File 'lib/chamomile/components/viewport.rb', line 6 def y_offset @y_offset end |
Instance Method Details
#at_bottom? ⇒ Boolean
133 134 135 |
# File 'lib/chamomile/components/viewport.rb', line 133 def at_bottom? @y_offset >= max_scroll end |
#at_top? ⇒ Boolean
Queries
129 130 131 |
# File 'lib/chamomile/components/viewport.rb', line 129 def at_top? @y_offset <= 0 end |
#content ⇒ Object
50 51 52 |
# File 'lib/chamomile/components/viewport.rb', line 50 def content @lines.join("\n") end |
#content=(s) ⇒ Object
38 39 40 41 42 |
# File 'lib/chamomile/components/viewport.rb', line 38 def content=(s) @lines = s.to_s.split("\n", -1) clamp_offset @x_offset = @x_offset.clamp(0, [max_horizontal_scroll, 0].max) unless @soft_wrap end |
#ensure_visible(line) ⇒ Object
143 144 145 146 147 148 149 150 |
# File 'lib/chamomile/components/viewport.rb', line 143 def ensure_visible(line) if line < @y_offset self.y_offset = line elsif line >= @y_offset + @height self.y_offset = line - @height + 1 end self end |
#goto_bottom ⇒ Object
92 93 94 |
# File 'lib/chamomile/components/viewport.rb', line 92 def goto_bottom self.y_offset = max_scroll end |
#goto_top ⇒ Object
88 89 90 |
# File 'lib/chamomile/components/viewport.rb', line 88 def goto_top self.y_offset = 0 end |
#half_page_down ⇒ Object
84 85 86 |
# File 'lib/chamomile/components/viewport.rb', line 84 def half_page_down scroll_down(@height / 2) end |
#half_page_up ⇒ Object
80 81 82 |
# File 'lib/chamomile/components/viewport.rb', line 80 def half_page_up scroll_up(@height / 2) end |
#handle(msg) ⇒ Object Also known as: update
Handle an incoming event.
153 154 155 156 157 158 159 160 161 162 |
# File 'lib/chamomile/components/viewport.rb', line 153 def handle(msg) case msg when Chamomile::KeyEvent handle_key(msg) when Chamomile::MouseEvent handle_mouse(msg) end nil end |
#max_horizontal_scroll ⇒ Object
120 121 122 123 124 125 |
# File 'lib/chamomile/components/viewport.rb', line 120 def max_horizontal_scroll return 0 if @lines.empty? longest = @lines.map(&:length).max || 0 [longest - @width, 0].max end |
#page_down ⇒ Object
76 77 78 |
# File 'lib/chamomile/components/viewport.rb', line 76 def page_down scroll_down(@height) end |
#page_up ⇒ Object
72 73 74 |
# File 'lib/chamomile/components/viewport.rb', line 72 def page_up scroll_up(@height) end |
#scroll_down(n = 1) ⇒ Object
68 69 70 |
# File 'lib/chamomile/components/viewport.rb', line 68 def scroll_down(n = 1) self.y_offset = @y_offset + n end |
#scroll_left(n = 1) ⇒ Object
Horizontal scrolling
102 103 104 105 106 |
# File 'lib/chamomile/components/viewport.rb', line 102 def scroll_left(n = 1) return if @soft_wrap self.x_offset = @x_offset - n end |
#scroll_percent ⇒ Object
137 138 139 140 141 |
# File 'lib/chamomile/components/viewport.rb', line 137 def scroll_percent return 1.0 if max_scroll <= 0 @y_offset.to_f / max_scroll end |
#scroll_right(n = 1) ⇒ Object
108 109 110 111 112 |
# File 'lib/chamomile/components/viewport.rb', line 108 def scroll_right(n = 1) return if @soft_wrap self.x_offset = @x_offset + n end |
#scroll_up(n = 1) ⇒ Object
Vertical scrolling
64 65 66 |
# File 'lib/chamomile/components/viewport.rb', line 64 def scroll_up(n = 1) self.y_offset = @y_offset - n end |
#set_content(s) ⇒ Object
Backward compat — preserves the return-self convention
45 46 47 48 |
# File 'lib/chamomile/components/viewport.rb', line 45 def set_content(s) self.content = s self end |
#total_line_count ⇒ Object
54 55 56 |
# File 'lib/chamomile/components/viewport.rb', line 54 def total_line_count @soft_wrap ? wrapped_lines.length : @lines.length end |
#view ⇒ Object
167 168 169 170 171 172 173 |
# File 'lib/chamomile/components/viewport.rb', line 167 def view if @soft_wrap render_soft_wrap else render_normal end end |
#visible_line_count ⇒ Object
58 59 60 |
# File 'lib/chamomile/components/viewport.rb', line 58 def visible_line_count [total_line_count, @height].min end |