forked from fyne-io/fyne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
card.go
234 lines (197 loc) · 5.69 KB
/
card.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package widget
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/internal/widget"
"fyne.io/fyne/v2/theme"
)
// Card widget groups title, subtitle with content and a header image
//
// Since: 1.4
type Card struct {
BaseWidget
Title, Subtitle string
Image *canvas.Image
Content fyne.CanvasObject
}
// NewCard creates a new card widget with the specified title, subtitle and content (all optional).
//
// Since: 1.4
func NewCard(title, subtitle string, content fyne.CanvasObject) *Card {
card := &Card{
Title: title,
Subtitle: subtitle,
Content: content,
}
card.ExtendBaseWidget(card)
return card
}
// CreateRenderer is a private method to Fyne which links this widget to its renderer
func (c *Card) CreateRenderer() fyne.WidgetRenderer {
c.ExtendBaseWidget(c)
header := canvas.NewText(c.Title, theme.ForegroundColor())
header.TextStyle.Bold = true
subHeader := canvas.NewText(c.Subtitle, header.Color)
objects := []fyne.CanvasObject{header, subHeader}
if c.Image != nil {
objects = append(objects, c.Image)
}
if c.Content != nil {
objects = append(objects, c.Content)
}
r := &cardRenderer{widget.NewShadowingRenderer(objects, widget.CardLevel),
header, subHeader, c}
r.applyTheme()
return r
}
// MinSize returns the size that this widget should not shrink below
func (c *Card) MinSize() fyne.Size {
c.ExtendBaseWidget(c)
return c.BaseWidget.MinSize()
}
// SetContent changes the body of this card to have the specified content.
func (c *Card) SetContent(obj fyne.CanvasObject) {
c.Content = obj
c.Refresh()
}
// SetImage changes the image displayed above the title for this card.
func (c *Card) SetImage(img *canvas.Image) {
c.Image = img
c.Refresh()
}
// SetSubTitle updates the secondary title for this card.
func (c *Card) SetSubTitle(text string) {
c.Subtitle = text
c.Refresh()
}
// SetTitle updates the main title for this card.
func (c *Card) SetTitle(text string) {
c.Title = text
c.Refresh()
}
type cardRenderer struct {
*widget.ShadowingRenderer
header, subHeader *canvas.Text
card *Card
}
const (
cardMediaHeight = 128
)
// Layout the components of the card container.
func (c *cardRenderer) Layout(size fyne.Size) {
padding := theme.Padding()
pos := fyne.NewSquareOffsetPos(padding / 2)
size = size.Subtract(fyne.NewSquareSize(padding))
c.LayoutShadow(size, pos)
if c.card.Image != nil {
c.card.Image.Move(pos)
c.card.Image.Resize(fyne.NewSize(size.Width, cardMediaHeight))
pos.Y += cardMediaHeight
}
if c.card.Title != "" || c.card.Subtitle != "" {
titlePad := padding * 2
size.Width -= titlePad * 2
pos.X += titlePad
pos.Y += titlePad
if c.card.Title != "" {
height := c.header.MinSize().Height
c.header.Move(pos)
c.header.Resize(fyne.NewSize(size.Width, height))
pos.Y += height + padding
}
if c.card.Subtitle != "" {
height := c.subHeader.MinSize().Height
c.subHeader.Move(pos)
c.subHeader.Resize(fyne.NewSize(size.Width, height))
pos.Y += height + padding
}
size.Width = size.Width + titlePad*2
pos.X = pos.X - titlePad
pos.Y += titlePad
}
size.Width -= padding * 2
pos.X += padding
if c.card.Content != nil {
height := size.Height - padding*2 - (pos.Y - padding/2) // adjust for content and initial offset
if c.card.Title != "" || c.card.Subtitle != "" {
height += padding
pos.Y -= padding
}
c.card.Content.Move(pos.Add(fyne.NewPos(0, padding)))
c.card.Content.Resize(fyne.NewSize(size.Width, height))
}
}
// MinSize calculates the minimum size of a card.
// This is based on the contained text, image and content.
func (c *cardRenderer) MinSize() fyne.Size {
hasHeader := c.card.Title != ""
hasSubHeader := c.card.Subtitle != ""
hasImage := c.card.Image != nil
hasContent := c.card.Content != nil
padding := theme.Padding()
if !hasHeader && !hasSubHeader && !hasContent { // just image, or nothing
if c.card.Image == nil {
return fyne.NewSize(padding, padding) // empty, just space for border
}
return fyne.NewSize(c.card.Image.MinSize().Width+padding, cardMediaHeight+padding)
}
min := fyne.NewSize(padding, padding)
if hasImage {
min = fyne.NewSize(min.Width, min.Height+cardMediaHeight)
}
if hasHeader || hasSubHeader {
titlePad := padding * 2
min = min.Add(fyne.NewSize(0, titlePad*2))
if hasHeader {
headerMin := c.header.MinSize()
min = fyne.NewSize(fyne.Max(min.Width, headerMin.Width+titlePad*2+padding),
min.Height+headerMin.Height)
if hasSubHeader {
min.Height += padding
}
}
if hasSubHeader {
subHeaderMin := c.subHeader.MinSize()
min = fyne.NewSize(fyne.Max(min.Width, subHeaderMin.Width+titlePad*2+padding),
min.Height+subHeaderMin.Height)
}
}
if hasContent {
contentMin := c.card.Content.MinSize()
min = fyne.NewSize(fyne.Max(min.Width, contentMin.Width+padding*3),
min.Height+contentMin.Height+padding*2)
}
return min
}
func (c *cardRenderer) Refresh() {
c.header.Text = c.card.Title
c.header.Refresh()
c.subHeader.Text = c.card.Subtitle
c.subHeader.Refresh()
objects := []fyne.CanvasObject{c.header, c.subHeader}
if c.card.Image != nil {
objects = append(objects, c.card.Image)
}
if c.card.Content != nil {
objects = append(objects, c.card.Content)
}
c.ShadowingRenderer.SetObjects(objects)
c.applyTheme()
c.Layout(c.card.Size())
c.ShadowingRenderer.RefreshShadow()
canvas.Refresh(c.card.super())
}
// applyTheme updates this button to match the current theme
func (c *cardRenderer) applyTheme() {
if c.header != nil {
c.header.TextSize = theme.TextHeadingSize()
c.header.Color = theme.ForegroundColor()
}
if c.subHeader != nil {
c.subHeader.TextSize = theme.TextSize()
c.subHeader.Color = theme.ForegroundColor()
}
if c.card.Content != nil {
c.card.Content.Refresh()
}
}