Class: Muxr::Window

Inherits:
Object
  • Object
show all
Defined in:
lib/muxr/window.rb

Overview

A Window is a single logical “screen” containing an ordered list of panes, a focused-index, a master-index (which pane is the master for tall/grid layouts), and the currently-selected layout.

Constant Summary collapse

LAYOUTS =
LayoutManager::LAYOUTS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name: "main") ⇒ Window

Returns a new instance of Window.



11
12
13
14
15
16
17
18
# File 'lib/muxr/window.rb', line 11

def initialize(name: "main")
  @name = name
  @panes = []
  @focused_index = 0
  @last_focused_pane = nil
  @master_index = 0
  @layout = :tall
end

Instance Attribute Details

#focused_indexObject

Returns the value of attribute focused_index.



9
10
11
# File 'lib/muxr/window.rb', line 9

def focused_index
  @focused_index
end

#layoutObject

Returns the value of attribute layout.



8
9
10
# File 'lib/muxr/window.rb', line 8

def layout
  @layout
end

#master_indexObject

Returns the value of attribute master_index.



8
9
10
# File 'lib/muxr/window.rb', line 8

def master_index
  @master_index
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/muxr/window.rb', line 8

def name
  @name
end

#panesObject (readonly)

Returns the value of attribute panes.



9
10
11
# File 'lib/muxr/window.rb', line 9

def panes
  @panes
end

Instance Method Details

#add_pane(pane) ⇒ Object



29
30
31
32
# File 'lib/muxr/window.rb', line 29

def add_pane(pane)
  @panes << pane
  pane
end

#clamp_indices!Object



100
101
102
103
104
105
106
107
108
# File 'lib/muxr/window.rb', line 100

def clamp_indices!
  if @panes.empty?
    @focused_index = 0
    @master_index = 0
  else
    @focused_index = @focused_index.clamp(0, @panes.length - 1)
    @master_index  = @master_index.clamp(0, @panes.length - 1)
  end
end

#cycle_layoutObject



89
90
91
92
# File 'lib/muxr/window.rb', line 89

def cycle_layout
  i = LAYOUTS.index(@layout) || 0
  @layout = LAYOUTS[(i + 1) % LAYOUTS.length]
end

#focus_index(idx) ⇒ Object



66
67
68
69
70
# File 'lib/muxr/window.rb', line 66

def focus_index(idx)
  return if @panes.empty?
  return unless idx.is_a?(Integer) && idx >= 0 && idx < @panes.length
  self.focused_index = idx
end

#focus_lastObject



59
60
61
62
63
64
# File 'lib/muxr/window.rb', line 59

def focus_last
  return unless @last_focused_pane
  idx = @panes.index(@last_focused_pane)
  return unless idx
  self.focused_index = idx
end

#focus_nextObject



49
50
51
52
# File 'lib/muxr/window.rb', line 49

def focus_next
  return if @panes.empty?
  self.focused_index = (@focused_index + 1) % @panes.length
end

#focus_prevObject



54
55
56
57
# File 'lib/muxr/window.rb', line 54

def focus_prev
  return if @panes.empty?
  self.focused_index = (@focused_index - 1) % @panes.length
end

#focused_paneObject



44
45
46
47
# File 'lib/muxr/window.rb', line 44

def focused_pane
  return nil if @panes.empty?
  @panes[@focused_index]
end

#promote_to_masterObject



72
73
74
75
76
77
78
79
80
81
# File 'lib/muxr/window.rb', line 72

def promote_to_master
  return if @panes.empty?
  return if @master_index == @focused_index
  # Move the focused pane into the master slot and shift the others down,
  # preserving relative order so tall/grid layouts stay stable.
  pane = @panes.delete_at(@focused_index)
  @panes.unshift(pane)
  @master_index = 0
  @focused_index = 0
end

#remove_pane(pane) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/muxr/window.rb', line 34

def remove_pane(pane)
  idx = @panes.index(pane)
  return false unless idx
  pane.close
  @panes.delete_at(idx)
  @last_focused_pane = nil if @last_focused_pane == pane
  clamp_indices!
  true
end

#set_layout(layout) ⇒ Object

Raises:

  • (ArgumentError)


94
95
96
97
98
# File 'lib/muxr/window.rb', line 94

def set_layout(layout)
  layout = layout.to_sym
  raise ArgumentError, "Unknown layout: #{layout}" unless LAYOUTS.include?(layout)
  @layout = layout
end

#swap_panes(i, j) ⇒ Object



83
84
85
86
87
# File 'lib/muxr/window.rb', line 83

def swap_panes(i, j)
  return if i == j
  return unless @panes[i] && @panes[j]
  @panes[i], @panes[j] = @panes[j], @panes[i]
end