| 1 |
|
| 2 |
n |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package mauview |
| 9 |
|
| 10 |
import ( |
| •11 |
"github.com/gdamore/tcell/v2" |
| 12 |
) |
| 13 |
|
| 14 |
type FlexDirection int |
| 15 |
|
| 16 |
const ( |
| 17 |
FlexRow FlexDirection = iota |
| 18 |
FlexColumn |
| 19 |
) |
| 20 |
|
| 21 |
type flexChild struct { |
| 22 |
genericChild |
| 23 |
size int |
| 24 |
} |
| 25 |
|
| 26 |
type Flex struct { |
| 27 |
direction FlexDirection |
| 28 |
children []flexChild |
| 29 |
focused *flexChild |
| 30 |
} |
| 31 |
|
| 32 |
func NewFlex() *Flex { |
| 33 |
return &Flex{ |
| 34 |
children: []flexChild{}, |
| 35 |
focused: nil, |
| 36 |
direction: FlexColumn, |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
func (flex *Flex) SetDirection(direction FlexDirection) *Flex { |
| 41 |
flex.direction = direction |
| 42 |
return flex |
| 43 |
} |
| 44 |
|
| 45 |
func (flex *Flex) AddFixedComponent(comp Component, size int) *Flex { |
| 46 |
flex.AddProportionalComponent(comp, -size) |
| 47 |
return flex |
| 48 |
} |
| 49 |
|
| 50 |
func (flex *Flex) AddProportionalComponent(comp Component, size int) *Flex { |
| 51 |
flex.children = append(flex.children, flexChild{ |
| 52 |
genericChild: genericChild{ |
| 53 |
target: comp, |
| 54 |
screen: &ProxyScreen{Style: tcell.StyleDefault}, |
| 55 |
}, |
| 56 |
size: -size, |
| 57 |
}) |
| 58 |
return flex |
| 59 |
} |
| 60 |
|
| 61 |
func (flex *Flex) RemoveComponent(comp Component) *Flex { |
| 62 |
for index := len(flex.children) - 1; index >= 0; index-- { |
| 63 |
if flex.children[index].target == comp { |
| 64 |
flex.children = append(flex.children[:index], flex.children[index+1:]...) |
| 65 |
} |
| 66 |
} |
| 67 |
return flex |
| 68 |
} |
| 69 |
|
| 70 |
func (flex *Flex) Draw(screen Screen) { |
| 71 |
width, height := screen.Size() |
| 72 |
screen.Clear() |
| 73 |
relTotalSize := width |
| 74 |
if flex.direction == FlexRow { |
| 75 |
relTotalSize = height |
| 76 |
} |
| 77 |
relParts := 0 |
| 78 |
for _, child := range flex.children { |
| 79 |
if child.size > 0 { |
| 80 |
relTotalSize -= child.size |
| 81 |
} else { |
| 82 |
relParts -= child.size |
| 83 |
} |
| 84 |
|
| 85 |
} |
| 86 |
offset := 0 |
| 87 |
for _, child := range flex.children { |
| 88 |
child.screen.Parent = screen |
| 89 |
size := child.size |
| 90 |
if size < 0 { |
| 91 |
size = relTotalSize * (-size) / relParts |
| 92 |
} |
| 93 |
if flex.direction == FlexRow { |
| 94 |
child.screen.Height = size |
| 95 |
child.screen.Width = width |
| 96 |
child.screen.OffsetY = offset |
| 97 |
child.screen.OffsetX = 0 |
| 98 |
} else { |
| 99 |
child.screen.Height = height |
| 100 |
child.screen.Width = size |
| 101 |
child.screen.OffsetY = 0 |
| 102 |
child.screen.OffsetX = offset |
| 103 |
} |
| 104 |
offset += size |
| 105 |
if flex.focused == nil || child != *flex.focused { |
| 106 |
child.target.Draw(child.screen) |
| 107 |
} |
| 108 |
} |
| 109 |
if flex.focused != nil { |
| 110 |
flex.focused.target.Draw(flex.focused.screen) |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
func (flex *Flex) OnKeyEvent(event KeyEvent) bool { |
| 115 |
if flex.focused != nil { |
| 116 |
return flex.focused.target.OnKeyEvent(event) |
| 117 |
} |
| 118 |
return false |
| 119 |
} |
| 120 |
|
| 121 |
func (flex *Flex) OnPasteEvent(event PasteEvent) bool { |
| 122 |
if flex.focused != nil { |
| 123 |
return flex.focused.target.OnPasteEvent(event) |
| 124 |
} |
| 125 |
return false |
| 126 |
} |
| 127 |
|
| 128 |
func (flex *Flex) SetFocused(comp Component) { |
| 129 |
for _, child := range flex.children { |
| 130 |
if child.target == comp { |
| 131 |
flex.focused = &child |
| 132 |
flex.focused.Focus() |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
func (flex *Flex) OnMouseEvent(event MouseEvent) bool { |
| 138 |
if flex.focused != nil && flex.focused.screen.IsInArea(event.Position()) { |
| 139 |
screen := flex.focused.screen |
| 140 |
return flex.focused.target.OnMouseEvent(OffsetMouseEvent(event, -screen.OffsetX, -screen.OffsetY)) |
| 141 |
} |
| 142 |
for _, child := range flex.children { |
| 143 |
if child.screen.IsInArea(event.Position()) { |
| 144 |
focusChanged := false |
| 145 |
if event.Buttons() == tcell.Button1 && !event.HasMotion() { |
| 146 |
if flex.focused != nil { |
| 147 |
flex.focused.Blur() |
| 148 |
} |
| 149 |
flex.focused = &child |
| 150 |
flex.focused.Focus() |
| 151 |
focusChanged = true |
| 152 |
} |
| 153 |
return child.target.OnMouseEvent(OffsetMouseEvent(event, -child.screen.OffsetX, -child.screen.OffsetY)) || |
| 154 |
focusChanged |
| 155 |
|
| 156 |
} |
| 157 |
} |
| 158 |
if event.Buttons() == tcell.Button1 && flex.focused != nil && !event.HasMotion() { |
| 159 |
flex.focused.Blur() |
| 160 |
flex.focused = nil |
| 161 |
return true |
| 162 |
} |
| 163 |
return false |
| 164 |
} |
| 165 |
|
| 166 |
func (flex *Flex) Focus() {} |
| 167 |
|
| 168 |
func (flex *Flex) Blur() { |
| 169 |
if flex.focused != nil { |
| 170 |
flex.focused.Blur() |
| 171 |
flex.focused = nil |
| 172 |
} |
| 173 |
} |