Class: TerminalNotes::Widget

Inherits:
Object
  • Object
show all
Defined in:
lib/terminal-notes/widget.rb

Direct Known Subclasses

FileList, InfoBar, SearchField

Constant Summary collapse

TITLE_PADDING =
4

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent:, title:, width: 1, height: 1, x: 0, y: 0, align: :center, valign: :center, border: true) ⇒ Widget

Returns a new instance of Widget.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/terminal-notes/widget.rb', line 7

def initialize(parent:, title:, width: 1, height: 1,
               x: 0, y: 0, align: :center, valign: :center,
               border: true)
  @parent = parent
  @title  = title || ""

  @width  = width
  @height = height

  parent_size = @parent.size

  # Relative layout calculation
  remaining_width = @width
  if @width <= 1
    remaining_width = parent_size[:columns] - x
    @width = (@width * remaining_width).to_i
  end

  remaining_height = @height
  if @height <= 1
    remaining_height = parent_size[:lines] - y
    @height = (@height * remaining_height).to_i
  end

  if align == :center
    pos_x = x + (remaining_width - @width) / 2
    @title_x = (@width - @title.size) / 2
  elsif align == :right
    pos_x = (remaining_width - @width)
    @title_x = @width - @title.size - TITLE_PADDING
  else
    pos_x = x # do nothing
    @title_x = TITLE_PADDING
  end

  if valign == :center
    pos_y = y + (remaining_height - @height) / 2
  elsif valign == :bottom
    pos_y = y + (remaining_height - @height)
  else
    pos_y = y # do nothing
  end

  @position = Cursor.new(x: pos_x, y: pos_y)

  @window = Rurses::Window.new(
    lines: @height, columns: @width,
    x: @position.x, y: @position.y)

  @has_border = border

  # @window.refresh_in_memory
end

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height.



3
4
5
# File 'lib/terminal-notes/widget.rb', line 3

def height
  @height
end

#widthObject (readonly)

Returns the value of attribute width.



3
4
5
# File 'lib/terminal-notes/widget.rb', line 3

def width
  @width
end

#windowObject (readonly)

Returns the value of attribute window.



3
4
5
# File 'lib/terminal-notes/widget.rb', line 3

def window
  @window
end

Instance Method Details

#drawObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/terminal-notes/widget.rb', line 66

def draw
  old_cursor = @window.cursor_xy

  # Draw title
  if @has_border || !@title.empty?
    @window.draw_border
    @window.move_cursor(x: @title_x, y: 0)
    @window.draw_string(" #{@title} ")
  end

  yield if block_given?

  @window.move_cursor(old_cursor)
  @window.refresh_in_memory
end

#focusObject



82
83
# File 'lib/terminal-notes/widget.rb', line 82

def focus
end

#redrawObject



61
62
63
64
# File 'lib/terminal-notes/widget.rb', line 61

def redraw
  @window.clear
  draw
end

#resizeObject



85
86
87
# File 'lib/terminal-notes/widget.rb', line 85

def resize
  calculate_and_draw
end