Class: Tuile::Component::Window
- Inherits:
-
Tuile::Component
- Object
- Tuile::Component
- Tuile::Component::Window
- Includes:
- HasContent
- Defined in:
- lib/tuile/component/window.rb
Overview
A window with a frame, a #caption and a content Tuile::Component. Doesn’t support overlapping with other windows: it paints its entire contents and doesn’t clip if there are other overlapping windows.
The window’s ‘content` is unset by default; assign one via HasContent#content=.
Window is considered invisible if #rect is empty or one of left/top is negative. The window won’t draw when invisible.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#caption ⇒ String
The current caption, empty by default.
-
#footer ⇒ Component?
Optional component overlaying the bottom border row.
Attributes included from HasContent
Attributes inherited from Tuile::Component
Instance Method Summary collapse
- #children ⇒ Array<Component>
-
#content_size ⇒ Size
The size needed to fit the window’s content, footer (width only — footer overlays the bottom border), and caption, plus the 2-character border.
- #focusable? ⇒ Boolean
- #handle_key(key) ⇒ Boolean
- #handle_mouse(event) ⇒ void
-
#initialize(caption = "") ⇒ Window
constructor
A new instance of Window.
- #key_shortcut=(key) ⇒ void
- #rect=(new_rect) ⇒ void
-
#repaint ⇒ void
Fully repaints the window: both frame and contents.
- #scrollbar=(value) ⇒ void
-
#visible? ⇒ Boolean
True if #rect is off screen and the window won’t paint.
Methods included from HasContent
Methods inherited from Tuile::Component
#active=, #active?, #attached?, #cursor_position, #depth, #find_shortcut_component, #focus, #keyboard_hint, #on_child_removed, #on_focus, #on_tree, #root, #screen
Constructor Details
#initialize(caption = "") ⇒ Window
Returns a new instance of Window.
17 18 19 20 21 22 23 24 |
# File 'lib/tuile/component/window.rb', line 17 def initialize(caption = "") super() @border_right = 1 @caption = caption # Optional bottom-row chrome that overlays the bottom border (e.g. a # search field). @footer = nil end |
Instance Attribute Details
#caption ⇒ String
Returns the current caption, empty by default.
105 106 107 |
# File 'lib/tuile/component/window.rb', line 105 def caption @caption end |
#footer ⇒ Component?
Returns optional component overlaying the bottom border row.
30 31 32 |
# File 'lib/tuile/component/window.rb', line 30 def @footer end |
Instance Method Details
#children ⇒ Array<Component>
61 62 63 |
# File 'lib/tuile/component/window.rb', line 61 def children @footer.nil? ? super : super + [@footer] end |
#content_size ⇒ Size
Returns the size needed to fit the window’s content, footer (width only — footer overlays the bottom border), and caption, plus the 2-character border. Returns Size‘.new(2, 2)` when the window has no content, footer, or caption.
118 119 120 121 122 123 124 125 126 |
# File 'lib/tuile/component/window.rb', line 118 def content_size inner_w = [ content&.content_size&.width || 0, @footer&.content_size&.width || 0, frame_caption.length ].max inner_h = content&.content_size&.height || 0 Size.new(inner_w + 2, inner_h + 2) end |
#focusable? ⇒ Boolean
26 |
# File 'lib/tuile/component/window.rb', line 26 def focusable? = true |
#handle_key(key) ⇒ Boolean
67 68 69 70 71 |
# File 'lib/tuile/component/window.rb', line 67 def handle_key(key) return @footer.handle_key(key) if @footer&.active? super end |
#handle_mouse(event) ⇒ void
This method returns an undefined value.
75 76 77 78 79 80 81 |
# File 'lib/tuile/component/window.rb', line 75 def handle_mouse(event) if @footer&.rect&.contains?(event.point) @footer.handle_mouse(event) else super end end |
#key_shortcut=(key) ⇒ void
This method returns an undefined value.
146 147 148 149 150 |
# File 'lib/tuile/component/window.rb', line 146 def key_shortcut=(key) super # The shortcut key is shown in the caption — repaint. invalidate end |
#rect=(new_rect) ⇒ void
This method returns an undefined value.
85 86 87 88 |
# File 'lib/tuile/component/window.rb', line 85 def rect=(new_rect) super end |
#repaint ⇒ void
This method returns an undefined value.
Fully repaints the window: both frame and contents.
136 137 138 139 140 141 142 |
# File 'lib/tuile/component/window.rb', line 136 def repaint super repaint_border # Border paints over content: invalidate the content to have it # repainted. content&.invalidate end |
#scrollbar=(value) ⇒ void
This method returns an undefined value.
92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/tuile/component/window.rb', line 92 def (value) unless content.is_a?(Component::List) raise Tuile::Error, "scrollbar= requires a Component::List as content, got #{content.inspect}" end content. = value ? :visible : :gone @border_right = value ? 0 : 1 invalidate layout(content) end |
#visible? ⇒ Boolean
Returns true if Tuile::Component#rect is off screen and the window won’t paint.
130 131 132 |
# File 'lib/tuile/component/window.rb', line 130 def visible? !@rect.empty? && !@rect.top.negative? && !@rect.left.negative? end |