Class: Echoes::PaneTree

Inherits:
Object
  • Object
show all
Defined in:
lib/echoes/pane_tree.rb

Defined Under Namespace

Classes: PaneNode, SplitNode

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pane) ⇒ PaneTree

Returns a new instance of PaneTree.



30
31
32
33
# File 'lib/echoes/pane_tree.rb', line 30

def initialize(pane)
  @root = PaneNode.new(pane)
  @active_pane = pane
end

Instance Attribute Details

#active_paneObject

Returns the value of attribute active_pane.



28
29
30
# File 'lib/echoes/pane_tree.rb', line 28

def active_pane
  @active_pane
end

#rootObject (readonly)

Returns the value of attribute root.



27
28
29
# File 'lib/echoes/pane_tree.rb', line 27

def root
  @root
end

Instance Method Details

#layout(x, y, w, h) ⇒ Object

Calculate layout rectangles for all panes Returns [x:, y:, w:, h:, …]



72
73
74
# File 'lib/echoes/pane_tree.rb', line 72

def layout(x, y, w, h)
  layout_node(@root, x, y, w, h)
end

#next_pane(current) ⇒ Object

Cycle to next pane



82
83
84
85
86
87
# File 'lib/echoes/pane_tree.rb', line 82

def next_pane(current)
  list = panes
  idx = list.index(current)
  return list.first unless idx
  list[(idx + 1) % list.size]
end

#pane_countObject



101
102
103
# File 'lib/echoes/pane_tree.rb', line 101

def pane_count
  panes.size
end

#panesObject

Flat list of all panes (in-order traversal)



77
78
79
# File 'lib/echoes/pane_tree.rb', line 77

def panes
  collect_panes(@root)
end

#prev_pane(current) ⇒ Object

Cycle to previous pane



90
91
92
93
94
95
# File 'lib/echoes/pane_tree.rb', line 90

def prev_pane(current)
  list = panes
  idx = list.index(current)
  return list.last unless idx
  list[(idx - 1) % list.size]
end

#remove(pane) ⇒ Object

Remove a pane, promoting its sibling



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/echoes/pane_tree.rb', line 50

def remove(pane)
  return nil if single_pane?

  parent = find_parent(@root, pane)
  return nil unless parent

  sibling = if pane_in_subtree?(parent.left, pane)
              parent.right
            else
              parent.left
            end

  replace_node(parent, sibling)

  if @active_pane == pane
    @active_pane = panes.first
  end
  pane
end

#single_pane?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/echoes/pane_tree.rb', line 97

def single_pane?
  @root.is_a?(PaneNode)
end

#split(pane, direction, new_pane) ⇒ Object

Split the active pane in the given direction, returning the new pane



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/echoes/pane_tree.rb', line 36

def split(pane, direction, new_pane)
  node = find_node(@root, pane)
  return nil unless node

  old_node = PaneNode.new(pane)
  new_node = PaneNode.new(new_pane)
  split_node = SplitNode.new(direction: direction, left: old_node, right: new_node)

  replace_node(node, split_node)
  @active_pane = new_pane
  new_pane
end