Class: RubyTUI::Widgets::Popup
- Inherits:
-
Object
- Object
- RubyTUI::Widgets::Popup
- Includes:
- RubyTUI::Widget
- Defined in:
- lib/rubytui/widgets/popup.rb
Overview
Popup renders an overlay on top of existing content. It clears its area, optionally draws a drop shadow, and draws its block; popup content is rendered separately into #inner.
Instance Method Summary collapse
-
#initialize(width: nil, height: nil, width_percent: 60, height_percent: 60, x: nil, y: nil, block: nil, style: Style::DEFAULT, shadow: false, shadow_style: nil) ⇒ Popup
constructor
A new instance of Popup.
-
#inner(parent_area) ⇒ Rect
Get the inner area (inside the popup's block).
-
#popup_area(parent_area) ⇒ Rect
Calculate the popup area within the given parent area.
-
#render(area, buf) ⇒ void
Draw the shadow (if enabled), clear the popup area with
style, then draw the block.
Constructor Details
#initialize(width: nil, height: nil, width_percent: 60, height_percent: 60, x: nil, y: nil, block: nil, style: Style::DEFAULT, shadow: false, shadow_style: nil) ⇒ Popup
Returns a new instance of Popup.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/rubytui/widgets/popup.rb', line 35 def initialize( width: nil, # fixed width or nil for percentage height: nil, # fixed height or nil for percentage width_percent: 60, # percentage of parent width height_percent: 60, # percentage of parent height x: nil, # fixed x position or nil for centered y: nil, # fixed y position or nil for centered block: nil, # Block widget for border/title style: Style::DEFAULT, shadow: false, shadow_style: nil ) @fixed_width = width @fixed_height = height @width_percent = width_percent @height_percent = height_percent @fixed_x = x @fixed_y = y @block = block || Widgets::Block.new(borders: :all, border_set: Symbols::ROUNDED) @style = style @shadow = shadow @shadow_style = shadow_style || Style.new.fg(Color::BRIGHT_BLACK).bg(Color::BLACK) end |
Instance Method Details
#inner(parent_area) ⇒ Rect
Get the inner area (inside the popup's block)
82 83 84 |
# File 'lib/rubytui/widgets/popup.rb', line 82 def inner(parent_area) @block.inner(popup_area(parent_area)) end |
#popup_area(parent_area) ⇒ Rect
Calculate the popup area within the given parent area
The size is clamped to the parent size. A fixed x or y is used as is and
is not clamped to the parent area.
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/rubytui/widgets/popup.rb', line 66 def popup_area(parent_area) w = @fixed_width || (parent_area.width * @width_percent / 100) h = @fixed_height || (parent_area.height * @height_percent / 100) w = [w, parent_area.width].min h = [h, parent_area.height].min px = @fixed_x || (parent_area.x + (parent_area.width - w) / 2) py = @fixed_y || (parent_area.y + (parent_area.height - h) / 2) Rect.new(x: px, y: py, width: w, height: h) end |
#render(area, buf) ⇒ void
This method returns an undefined value.
Draw the shadow (if enabled), clear the popup area with style, then draw
the block.
area is the parent area; the popup is placed within it by #popup_area.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/rubytui/widgets/popup.rb', line 94 def render(area, buf) return if area.empty? pa = popup_area(area) return if pa.empty? # Draw shadow first (offset by 1,1) if @shadow render_shadow(pa, buf) end # Clear the popup area with background style clear_area(pa, buf) # Draw the block (border + title) @block.render(pa, buf) end |