Class: Tuile::Component::Window

Inherits:
Tuile::Component show all
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

InfoWindow, LogWindow, PickerWindow

Instance Attribute Summary collapse

Attributes included from HasContent

#content

Attributes inherited from Tuile::Component

#key_shortcut, #parent, #rect

Instance Method Summary collapse

Methods included from HasContent

#on_focus

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.

Parameters:

  • caption (String) (defaults to: "")


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

#captionString

Returns the current caption, empty by default.

Returns:

  • (String)

    the current caption, empty by default.



105
106
107
# File 'lib/tuile/component/window.rb', line 105

def caption
  @caption
end

Returns optional component overlaying the bottom border row.

Returns:

  • (Component, nil)

    optional component overlaying the bottom border row.



30
31
32
# File 'lib/tuile/component/window.rb', line 30

def footer
  @footer
end

Instance Method Details

#childrenArray<Component>

Returns:



61
62
63
# File 'lib/tuile/component/window.rb', line 61

def children
  @footer.nil? ? super : super + [@footer]
end

#content_sizeSize

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.

Returns:

  • (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. 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

Returns:

  • (Boolean)


26
# File 'lib/tuile/component/window.rb', line 26

def focusable? = true

#handle_key(key) ⇒ Boolean

Parameters:

  • key (String)

Returns:

  • (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.

Parameters:



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.

Parameters:

  • key (String, nil)


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.

Parameters:



85
86
87
88
# File 'lib/tuile/component/window.rb', line 85

def rect=(new_rect)
  super
  layout_footer
end

#repaintvoid

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.

Parameters:

  • value (Boolean)


92
93
94
95
96
97
98
99
100
101
102
# File 'lib/tuile/component/window.rb', line 92

def scrollbar=(value)
  unless content.is_a?(Component::List)
    raise Tuile::Error,
          "scrollbar= requires a Component::List as content, got #{content.inspect}"
  end

  content.scrollbar_visibility = 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.

Returns:



130
131
132
# File 'lib/tuile/component/window.rb', line 130

def visible?
  !@rect.empty? && !@rect.top.negative? && !@rect.left.negative?
end