Class: VER::PanedLayout

Inherits:
Tk::Tile::LabelFrame
  • Object
show all
Defined in:
lib/ver/layout/paned.rb

Overview

A Layout containing frames in a master and slave pane.

Constant Summary collapse

OPTIONS =

default options

{masters: 1, slaves: 3, center: 0.5}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, options = {}) ⇒ PanedLayout

Create a new PanedLayout for given parent and options. The options may contain :masters and :slaves, which will be merged with the default [OPTIONS].



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ver/layout/paned.rb', line 25

def initialize(parent, options = {})
  @options = OPTIONS.dup
  @options[:masters] = options.delete(:masters) if options.key?(:masters)
  @options[:slaves]  = options.delete(:slaves)  if options.key?(:slaves)

  super

  pack(fill: :both, expand: true)

  @frames = []

  @layout_pane = Tk::Tile::PanedWindow.new(self, orient: :horizontal)
  @master_pane = Tk::Tile::PanedWindow.new(@layout_pane, orient: :vertical)
  @slave_pane  = Tk::Tile::PanedWindow.new(@layout_pane, orient: :vertical)
  @layout_pane.pack fill: :both, expand: true
  @layout_pane.add(@master_pane, weight: 1)
  @layout_pane.add(@slave_pane, weight: 1)

  @layout_pane.bind('<Map>'){ apply }
end

Instance Attribute Details

#framesObject (readonly)

contains the frames in the order they are displayed



5
6
7
# File 'lib/ver/layout/paned.rb', line 5

def frames
  @frames
end

#layout_paneObject (readonly)

contains the master and slave pane



8
9
10
# File 'lib/ver/layout/paned.rb', line 8

def layout_pane
  @layout_pane
end

#master_paneObject (readonly)

contains master frames



11
12
13
# File 'lib/ver/layout/paned.rb', line 11

def master_pane
  @master_pane
end

#optionsObject (readonly)

options specific for the paned layout



17
18
19
# File 'lib/ver/layout/paned.rb', line 17

def options
  @options
end

#slave_paneObject (readonly)

contains slave frames



14
15
16
# File 'lib/ver/layout/paned.rb', line 14

def slave_pane
  @slave_pane
end

Instance Method Details

#add_buffer(buffer) ⇒ Object

Add the given buffer.frame to the #frames, apply, and focus buffer.



47
48
49
50
51
# File 'lib/ver/layout/paned.rb', line 47

def add_buffer(buffer)
  frames.unshift(buffer.frame)
  apply
  buffer.focus
end

#applyObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/ver/layout/paned.rb', line 85

def apply
  masters, slaves = masters_slaves

  (master_pane.panes - masters.map{|bff| bff.tk_pathname }).each do |bff|
    master_pane.forget(bff)
  end

  (slave_pane.panes - slaves.map{|bff| bff.tk_pathname }).each do |bff|
    slave_pane.forget(bff)
  end

  masters.each{|bff| master_pane.insert(:end, bff, weight: 1) }
  slaves.each{|bff| slave_pane.insert(:end, bff, weight: 1) }

  if slaves.empty?
    layout_pane.forget(slave_pane) rescue nil
  else
    layout_pane.add(slave_pane, weight: 1) rescue nil

    width = layout_pane.winfo_width
    center = (width * options[:center]).to_i

    # layout_pane.sashpos(0, center)
  end
end

#close_buffer(buffer) ⇒ Object

Forget and destroy the given buffer.

See Also:



74
75
76
77
78
79
80
81
82
83
# File 'lib/ver/layout/paned.rb', line 74

def close_buffer(buffer)
  forget_buffer(buffer)
  buffer.frame.destroy

  if previous = visible.first
    previous.focus
  else
    Methods::Save.quit(nil)
  end
end

#create_buffer(options = {}) {|buffer| ... } ⇒ Object

Create a new Buffer inside the VER::PanedLayout, options are passed to Buffer.new. The new Buffer will be at top of the #frames.

Yields:

  • (buffer)

See Also:



64
65
66
67
68
69
# File 'lib/ver/layout/paned.rb', line 64

def create_buffer(options = {})
  buffer = Buffer.new(self, options)
  yield buffer
  add_buffer(buffer)
  buffer
end

#cycle_next(frame) ⇒ Object



182
183
184
185
186
187
# File 'lib/ver/layout/paned.rb', line 182

def cycle_next(frame)
  return unless index = frames.index(frame)
  frames.push(frames.shift)
  apply
  frames[index].focus
end

#cycle_prev(frame) ⇒ Object



189
190
191
192
193
194
# File 'lib/ver/layout/paned.rb', line 189

def cycle_prev(frame)
  return unless index = frames.index(frame)
  frames.unshift(frames.pop)
  apply
  frames[index].focus
end

#focus_next(frame) ⇒ Object



137
138
139
140
141
142
# File 'lib/ver/layout/paned.rb', line 137

def focus_next(frame)
  return unless index = visible.index(frame)

  found = visible[index + 1] || visible[0]
  found.focus
end

#focus_prev(frame) ⇒ Object



144
145
146
147
148
149
# File 'lib/ver/layout/paned.rb', line 144

def focus_prev(frame)
  return unless index = visible.index(frame)

  found = visible[index - 1]
  found.focus
end

#forget_buffer(buffer) ⇒ Object

Remove given buffer from #frames and apply.



54
55
56
57
# File 'lib/ver/layout/paned.rb', line 54

def forget_buffer(buffer)
  frames.delete(buffer.frame)
  apply
end

#hide(frame) ⇒ Object



132
133
134
135
# File 'lib/ver/layout/paned.rb', line 132

def hide(frame)
  frame.shown = false
  apply
end

#masters_slavesObject



111
112
113
114
115
116
# File 'lib/ver/layout/paned.rb', line 111

def masters_slaves
  masters_max, slaves_max = options.values_at(:masters, :slaves)
  frames.compact!
  possible = frames.select(&:shown?)
  return [*possible[0, masters_max]], [*possible[masters_max, slaves_max]]
end

#push_bottom(frame) ⇒ Object



176
177
178
179
180
# File 'lib/ver/layout/paned.rb', line 176

def push_bottom(frame)
  frames.push(frames.delete(frame))
  apply
  frames.last.focus unless visible?(frame)
end

#push_down(frame) ⇒ Object



161
162
163
164
165
166
167
168
169
# File 'lib/ver/layout/paned.rb', line 161

def push_down(frame)
  return unless index = frames.index(frame)
  following = frames[index + 1] || frames[0]
  frame.raise(following)
  frames[frames.index(following)], frames[index] = frame, following

  apply
  following.focus unless visible?(frame)
end

#push_top(frame) ⇒ Object



171
172
173
174
# File 'lib/ver/layout/paned.rb', line 171

def push_top(frame)
  frames.unshift(frames.delete(frame))
  apply
end

#push_up(frame) ⇒ Object



151
152
153
154
155
156
157
158
159
# File 'lib/ver/layout/paned.rb', line 151

def push_up(frame)
  return unless index = frames.index(frame)
  previous = frames[index - 1]
  frame.raise(previous)
  frames[index - 1], frames[index] = frame, previous

  apply
  previous.focus unless visible?(frame)
end

#show(frame) ⇒ Object



126
127
128
129
130
# File 'lib/ver/layout/paned.rb', line 126

def show(frame)
  frame.shown = true
  push_up(frame) until visible?(frame)
  frame.focus
end

#visibleObject



122
123
124
# File 'lib/ver/layout/paned.rb', line 122

def visible
  masters_slaves.flatten
end

#visible?(frame) ⇒ Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/ver/layout/paned.rb', line 118

def visible?(frame)
  visible.index(frame)
end