forked from fyne-io/fyne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileitem.go
128 lines (102 loc) · 3 KB
/
fileitem.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
package dialog
import (
"path/filepath"
"github.com/unix-world/smart-fyne"
"github.com/unix-world/smart-fyne/theme"
"github.com/unix-world/smart-fyne/widget"
)
const (
fileIconSize = 64
fileInlineIconSize = 24
fileIconCellWidth = fileIconSize * 1.25
)
type fileDialogItem struct {
widget.BaseWidget
picker *fileDialog
name string
location fyne.URI
dir bool
}
func (i *fileDialogItem) CreateRenderer() fyne.WidgetRenderer {
text := widget.NewLabelWithStyle(i.name, fyne.TextAlignCenter, fyne.TextStyle{})
text.Truncation = fyne.TextTruncateEllipsis
text.Wrapping = fyne.TextWrapBreak
icon := widget.NewFileIcon(i.location)
return &fileItemRenderer{
item: i,
icon: icon,
text: text,
objects: []fyne.CanvasObject{icon, text},
fileTextSize: widget.NewLabel("M\nM").MinSize().Height, // cache two-line label height,
}
}
func (i *fileDialogItem) setLocation(l fyne.URI, dir, up bool) {
i.dir = dir
i.location = l
i.name = l.Name()
if i.picker.view == gridView {
ext := filepath.Ext(i.name[1:])
i.name = i.name[:len(i.name)-len(ext)]
}
if up {
i.name = "(Parent)"
}
i.Refresh()
}
func (f *fileDialog) newFileItem(location fyne.URI, dir, up bool) *fileDialogItem {
item := &fileDialogItem{
picker: f,
location: location,
name: location.Name(),
dir: dir,
}
if f.view == gridView {
ext := filepath.Ext(item.name[1:])
item.name = item.name[:len(item.name)-len(ext)]
}
if up {
item.name = "(Parent)"
}
item.ExtendBaseWidget(item)
return item
}
type fileItemRenderer struct {
item *fileDialogItem
fileTextSize float32
icon *widget.FileIcon
text *widget.Label
objects []fyne.CanvasObject
}
func (s *fileItemRenderer) Layout(size fyne.Size) {
if s.item.picker.view == gridView {
s.icon.Resize(fyne.NewSize(fileIconSize, fileIconSize))
s.icon.Move(fyne.NewPos((size.Width-fileIconSize)/2, 0))
s.text.Alignment = fyne.TextAlignCenter
s.text.Resize(fyne.NewSize(size.Width, s.fileTextSize))
s.text.Move(fyne.NewPos(0, size.Height-s.fileTextSize))
} else {
s.icon.Resize(fyne.NewSize(fileInlineIconSize, fileInlineIconSize))
s.icon.Move(fyne.NewPos(theme.Padding(), (size.Height-fileInlineIconSize)/2))
s.text.Alignment = fyne.TextAlignLeading
textMin := s.text.MinSize()
s.text.Resize(fyne.NewSize(size.Width, textMin.Height))
s.text.Move(fyne.NewPos(fileInlineIconSize, (size.Height-textMin.Height)/2))
}
}
func (s *fileItemRenderer) MinSize() fyne.Size {
if s.item.picker.view == gridView {
return fyne.NewSize(fileIconCellWidth, fileIconSize+s.fileTextSize)
}
textMin := s.text.MinSize()
return fyne.NewSize(fileInlineIconSize+textMin.Width+theme.Padding(), textMin.Height)
}
func (s *fileItemRenderer) Refresh() {
s.fileTextSize = widget.NewLabel("M\nM").MinSize().Height // cache two-line label height
s.text.SetText(s.item.name)
s.icon.SetURI(s.item.location)
}
func (s *fileItemRenderer) Objects() []fyne.CanvasObject {
return s.objects
}
func (s *fileItemRenderer) Destroy() {
}