-
Notifications
You must be signed in to change notification settings - Fork 0
/
splitter.go
138 lines (112 loc) · 4.04 KB
/
splitter.go
1
2
3
4
5
6
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
type AdaptativeSplit struct {
widget.BaseWidget
menu *widget.Button
title *widget.Label
separator fyne.CanvasObject
background fyne.CanvasObject
leftContent fyne.CanvasObject
rightContent fyne.CanvasObject
collapsed bool
}
var _ fyne.Widget = (*AdaptativeSplit)(nil)
func NewSplit(title string, leftContent fyne.CanvasObject, rightContent fyne.CanvasObject) *AdaptativeSplit {
var l *AdaptativeSplit
var menu *widget.Button
menu = widget.NewButtonWithIcon("", theme.CancelIcon(), func() {
if l.collapsed {
l.collapsed = false
menu.Icon = theme.CancelIcon()
} else {
l.collapsed = true
menu.Icon = theme.MenuIcon()
}
menu.Refresh()
l.Refresh()
})
l = &AdaptativeSplit{
menu: menu,
title: widget.NewLabel(title),
separator: widget.NewSeparator(),
background: canvas.NewRectangle(theme.MenuBackgroundColor()),
leftContent: leftContent,
rightContent: rightContent,
}
l.title.TextStyle.Bold = true
l.title.TextStyle.Italic = true
l.BaseWidget.ExtendBaseWidget(l)
return l
}
func (s *AdaptativeSplit) CreateRenderer() fyne.WidgetRenderer {
return &AdaptativeSplitRenderer{
split: s,
}
}
func (s *AdaptativeSplit) MinSize() fyne.Size {
menuMinSize := s.menu.MinSize()
leftMinSize := s.leftContent.MinSize()
rightMinSize := s.rightContent.MinSize()
separatorMinSize := s.separator.MinSize()
width := fyne.Max(menuMinSize.Width+rightMinSize.Width+separatorMinSize.Width+theme.Padding()*2, leftMinSize.Width+separatorMinSize.Width+theme.Padding())
height := fyne.Max(menuMinSize.Height+leftMinSize.Height, rightMinSize.Height)
return fyne.NewSize(width, height)
}
type AdaptativeSplitRenderer struct {
split *AdaptativeSplit
}
func (r *AdaptativeSplitRenderer) Layout(size fyne.Size) {
s := r.split
s.menu.Move(fyne.NewPos(0, 0))
s.menu.Resize(s.menu.MinSize())
if s.collapsed {
s.title.Hide()
s.separator.Move(fyne.NewPos(s.menu.Position().X+s.menu.Size().Width+theme.Padding(), 0))
s.separator.Resize(fyne.NewSize(1, size.Height))
s.leftContent.Hide()
s.rightContent.Move(fyne.NewPos(s.separator.Position().X+s.separator.Size().Width+theme.Padding(), 0))
s.rightContent.Resize(fyne.NewSize(size.Width-s.rightContent.Position().X, size.Height))
} else {
s.title.Move(fyne.NewPos(s.menu.Position().X+s.menu.Size().Width+theme.Padding(), 0))
s.title.Resize(fyne.NewSize(s.leftContent.MinSize().Width-s.title.Position().X, s.menu.Size().Height))
s.title.Show()
s.separator.Move(fyne.NewPos(s.leftContent.Position().X+s.leftContent.Size().Width+theme.Padding(), 0))
s.separator.Resize(fyne.NewSize(1, size.Height))
s.leftContent.Move(fyne.NewPos(0, s.menu.Position().Y+s.menu.Size().Height+theme.Padding()))
s.leftContent.Resize(fyne.NewSize(s.leftContent.MinSize().Width, size.Height-s.leftContent.Position().Y))
s.leftContent.Show()
rightContentMinSize := s.rightContent.MinSize()
if rightContentMinSize.Width > size.Width-s.separator.Position().X-s.separator.Size().Width-theme.Padding()*2 {
s.rightContent.Move(fyne.NewPos(s.menu.Position().X+s.menu.Size().Width+theme.Padding()+s.separator.Size().Width, 0))
s.rightContent.Resize(fyne.NewSize(size.Width-s.rightContent.Position().X, size.Height))
} else {
s.rightContent.Move(fyne.NewPos(s.separator.Position().X+s.separator.Size().Width+theme.Padding(), 0))
s.rightContent.Resize(fyne.NewSize(size.Width-s.rightContent.Position().X, size.Height))
}
}
s.background.Move(fyne.NewPos(0, 0))
s.background.Resize(fyne.NewSize(s.separator.Position().X, size.Height))
}
func (r *AdaptativeSplitRenderer) MinSize() fyne.Size {
return r.split.MinSize()
}
func (r *AdaptativeSplitRenderer) Objects() []fyne.CanvasObject {
return []fyne.CanvasObject{
r.split.rightContent,
r.split.background,
r.split.menu,
r.split.title,
r.split.leftContent,
r.split.separator,
}
}
func (r *AdaptativeSplitRenderer) Refresh() {
r.Layout(r.split.Size())
}
func (r *AdaptativeSplitRenderer) Destroy() {
}