Class: RubyTUI::Widgets::Block
- Inherits:
-
Object
- Object
- RubyTUI::Widgets::Block
- Includes:
- RubyTUI::Widget
- Defined in:
- lib/rubytui/widgets/block.rb
Overview
Block is the most fundamental widget: a rectangular area with optional borders, title, and padding. Other widgets are typically rendered inside a Block's inner area.
Constant Summary collapse
- NONE =
No borders.
:none- ALL =
All four borders.
:all- TOP =
Top border.
:top- BOTTOM =
Bottom border.
:bottom- LEFT =
Left border.
:left- RIGHT =
Right border.
:right
Instance Attribute Summary collapse
-
#border_set ⇒ Object
readonly
Returns the value of attribute border_set.
-
#border_style ⇒ Object
readonly
Returns the value of attribute border_style.
-
#borders ⇒ Object
readonly
Returns the value of attribute borders.
-
#padding ⇒ Object
readonly
Returns the value of attribute padding.
-
#style ⇒ Object
readonly
Returns the value of attribute style.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
-
#title_alignment ⇒ Object
readonly
Returns the value of attribute title_alignment.
Instance Method Summary collapse
-
#initialize(title: nil, title_alignment: :left, title_style: nil, borders: ALL, border_set: Symbols::ROUNDED, border_style: Style::DEFAULT, style: Style::DEFAULT, padding: nil) ⇒ Block
constructor
A new instance of Block.
-
#inner(area) ⇒ Rect
Calculate the inner area (area minus borders and padding).
-
#render(area, buf) ⇒ void
Render the block's background, borders and title.
Constructor Details
#initialize(title: nil, title_alignment: :left, title_style: nil, borders: ALL, border_set: Symbols::ROUNDED, border_style: Style::DEFAULT, style: Style::DEFAULT, padding: nil) ⇒ Block
Returns a new instance of Block.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/rubytui/widgets/block.rb', line 58 def initialize( title: nil, title_alignment: :left, title_style: nil, borders: ALL, border_set: Symbols::ROUNDED, border_style: Style::DEFAULT, style: Style::DEFAULT, padding: nil # [top, right, bottom, left] or single integer ) @title = title @title_alignment = title_alignment @title_style = title_style || border_style @borders = normalize_borders(borders) @border_set = border_set @border_style = border_style @style = style @padding = normalize_padding(padding) end |
Instance Attribute Details
#border_set ⇒ Object (readonly)
Returns the value of attribute border_set.
37 38 39 |
# File 'lib/rubytui/widgets/block.rb', line 37 def border_set @border_set end |
#border_style ⇒ Object (readonly)
Returns the value of attribute border_style.
37 38 39 |
# File 'lib/rubytui/widgets/block.rb', line 37 def border_style @border_style end |
#borders ⇒ Object (readonly)
Returns the value of attribute borders.
37 38 39 |
# File 'lib/rubytui/widgets/block.rb', line 37 def borders @borders end |
#padding ⇒ Object (readonly)
Returns the value of attribute padding.
37 38 39 |
# File 'lib/rubytui/widgets/block.rb', line 37 def padding @padding end |
#style ⇒ Object (readonly)
Returns the value of attribute style.
37 38 39 |
# File 'lib/rubytui/widgets/block.rb', line 37 def style @style end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
37 38 39 |
# File 'lib/rubytui/widgets/block.rb', line 37 def title @title end |
#title_alignment ⇒ Object (readonly)
Returns the value of attribute title_alignment.
37 38 39 |
# File 'lib/rubytui/widgets/block.rb', line 37 def title_alignment @title_alignment end |
Instance Method Details
#inner(area) ⇒ Rect
Calculate the inner area (area minus borders and padding)
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/rubytui/widgets/block.rb', line 83 def inner(area) return Rect::ZERO if area.empty? x = area.x y = area.y w = area.width h = area.height if @borders.include?(:left) x += 1 w -= 1 end if @borders.include?(:right) w -= 1 end if @borders.include?(:top) y += 1 h -= 1 end if @borders.include?(:bottom) h -= 1 end # Apply padding x += @padding[3] # left y += @padding[0] # top w -= @padding[1] + @padding[3] # right + left h -= @padding[0] + @padding[2] # top + bottom Rect.new(x: x, y: y, width: [w, 0].max, height: [h, 0].max) end |
#render(area, buf) ⇒ void
This method returns an undefined value.
Render the block's background, borders and title.
The background style is patched onto every cell of area unless it is
Style::DEFAULT. Child content is not rendered; draw it into #inner.
123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/rubytui/widgets/block.rb', line 123 def render(area, buf) return if area.empty? # Fill background buf.set_style(area, @style) if @style != Style::DEFAULT # Draw borders render_borders(area, buf) # Draw title render_title(area, buf) if @title end |