class Chamomile::Viewport
Scrollable content pane with key binding and mouse wheel support.
Constants
- DEFAULT_KEY_MAP
Attributes
Public Class Methods
Source
# 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
Public Instance Methods
Source
# File lib/chamomile/components/viewport.rb, line 133 def at_bottom? @y_offset >= max_scroll end
Source
# File lib/chamomile/components/viewport.rb, line 129 def at_top? @y_offset <= 0 end
Queries
Source
# File lib/chamomile/components/viewport.rb, line 50 def content @lines.join("\n") end
Source
# 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
Source
# 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
Source
# File lib/chamomile/components/viewport.rb, line 92 def goto_bottom self.y_offset = max_scroll end
Source
# File lib/chamomile/components/viewport.rb, line 88 def goto_top self.y_offset = 0 end
Source
# File lib/chamomile/components/viewport.rb, line 84 def half_page_down scroll_down(@height / 2) end
Source
# File lib/chamomile/components/viewport.rb, line 80 def half_page_up scroll_up(@height / 2) end
Source
# 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
Handle an incoming event.
Also aliased as: update
Source
# File lib/chamomile/components/viewport.rb, line 30 def height=(new_height) @height = new_height clamp_offset end
Also aliased as: set_height
Source
# 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
Source
# File lib/chamomile/components/viewport.rb, line 76 def page_down scroll_down(@height) end
Source
# File lib/chamomile/components/viewport.rb, line 72 def page_up scroll_up(@height) end
Source
# File lib/chamomile/components/viewport.rb, line 68 def scroll_down(n = 1) self.y_offset = @y_offset + n end
Source
# File lib/chamomile/components/viewport.rb, line 102 def scroll_left(n = 1) return if @soft_wrap self.x_offset = @x_offset - n end
Horizontal scrolling
Source
# 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
Source
# File lib/chamomile/components/viewport.rb, line 108 def scroll_right(n = 1) return if @soft_wrap self.x_offset = @x_offset + n end
Source
# File lib/chamomile/components/viewport.rb, line 64 def scroll_up(n = 1) self.y_offset = @y_offset - n end
Vertical scrolling
Source
# File lib/chamomile/components/viewport.rb, line 45 def set_content(s) self.content = s self end
Backward compat — preserves the return-self convention
Source
# File lib/chamomile/components/viewport.rb, line 54 def total_line_count @soft_wrap ? wrapped_lines.length : @lines.length end
Source
# File lib/chamomile/components/viewport.rb, line 167 def view if @soft_wrap render_soft_wrap else render_normal end end
Source
# File lib/chamomile/components/viewport.rb, line 58 def visible_line_count [total_line_count, @height].min end
Source
# File lib/chamomile/components/viewport.rb, line 21 def width=(new_width) @width = new_width clamp_offset @x_offset = @x_offset.clamp(0, [max_horizontal_scroll, 0].max) unless @soft_wrap end
Also aliased as: set_width
Source
# File lib/chamomile/components/viewport.rb, line 114 def x_offset=(n) return if @soft_wrap @x_offset = n.clamp(0, [max_horizontal_scroll, 0].max) end
Source
# File lib/chamomile/components/viewport.rb, line 96 def y_offset=(n) @y_offset = n.clamp(0, max_scroll) end