card.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. package widget
  2. import (
  3. "fyne.io/fyne/v2"
  4. "fyne.io/fyne/v2/canvas"
  5. "fyne.io/fyne/v2/internal/widget"
  6. "fyne.io/fyne/v2/theme"
  7. )
  8. // Card widget groups title, subtitle with content and a header image
  9. //
  10. // Since: 1.4
  11. type Card struct {
  12. BaseWidget
  13. Title, Subtitle string
  14. Image *canvas.Image
  15. Content fyne.CanvasObject
  16. }
  17. // NewCard creates a new card widget with the specified title, subtitle and content (all optional).
  18. //
  19. // Since: 1.4
  20. func NewCard(title, subtitle string, content fyne.CanvasObject) *Card {
  21. card := &Card{
  22. Title: title,
  23. Subtitle: subtitle,
  24. Content: content,
  25. }
  26. card.ExtendBaseWidget(card)
  27. return card
  28. }
  29. // CreateRenderer is a private method to Fyne which links this widget to its renderer
  30. func (c *Card) CreateRenderer() fyne.WidgetRenderer {
  31. c.ExtendBaseWidget(c)
  32. header := canvas.NewText(c.Title, theme.ForegroundColor())
  33. header.TextStyle.Bold = true
  34. subHeader := canvas.NewText(c.Subtitle, header.Color)
  35. objects := []fyne.CanvasObject{header, subHeader}
  36. if c.Image != nil {
  37. objects = append(objects, c.Image)
  38. }
  39. if c.Content != nil {
  40. objects = append(objects, c.Content)
  41. }
  42. r := &cardRenderer{widget.NewShadowingRenderer(objects, widget.CardLevel),
  43. header, subHeader, c}
  44. r.applyTheme()
  45. return r
  46. }
  47. // MinSize returns the size that this widget should not shrink below
  48. func (c *Card) MinSize() fyne.Size {
  49. c.ExtendBaseWidget(c)
  50. return c.BaseWidget.MinSize()
  51. }
  52. // SetContent changes the body of this card to have the specified content.
  53. func (c *Card) SetContent(obj fyne.CanvasObject) {
  54. c.Content = obj
  55. c.Refresh()
  56. }
  57. // SetImage changes the image displayed above the title for this card.
  58. func (c *Card) SetImage(img *canvas.Image) {
  59. c.Image = img
  60. c.Refresh()
  61. }
  62. // SetSubTitle updates the secondary title for this card.
  63. func (c *Card) SetSubTitle(text string) {
  64. c.Subtitle = text
  65. c.Refresh()
  66. }
  67. // SetTitle updates the main title for this card.
  68. func (c *Card) SetTitle(text string) {
  69. c.Title = text
  70. c.Refresh()
  71. }
  72. type cardRenderer struct {
  73. *widget.ShadowingRenderer
  74. header, subHeader *canvas.Text
  75. card *Card
  76. }
  77. const (
  78. cardMediaHeight = 128
  79. )
  80. // Layout the components of the card container.
  81. func (c *cardRenderer) Layout(size fyne.Size) {
  82. padding := theme.Padding()
  83. pos := fyne.NewSquareOffsetPos(padding / 2)
  84. size = size.Subtract(fyne.NewSquareSize(padding))
  85. c.LayoutShadow(size, pos)
  86. if c.card.Image != nil {
  87. c.card.Image.Move(pos)
  88. c.card.Image.Resize(fyne.NewSize(size.Width, cardMediaHeight))
  89. pos.Y += cardMediaHeight
  90. }
  91. if c.card.Title != "" || c.card.Subtitle != "" {
  92. titlePad := padding * 2
  93. size.Width -= titlePad * 2
  94. pos.X += titlePad
  95. pos.Y += titlePad
  96. if c.card.Title != "" {
  97. height := c.header.MinSize().Height
  98. c.header.Move(pos)
  99. c.header.Resize(fyne.NewSize(size.Width, height))
  100. pos.Y += height + padding
  101. }
  102. if c.card.Subtitle != "" {
  103. height := c.subHeader.MinSize().Height
  104. c.subHeader.Move(pos)
  105. c.subHeader.Resize(fyne.NewSize(size.Width, height))
  106. pos.Y += height + padding
  107. }
  108. size.Width = size.Width + titlePad*2
  109. pos.X = pos.X - titlePad
  110. pos.Y += titlePad
  111. }
  112. size.Width -= padding * 2
  113. pos.X += padding
  114. if c.card.Content != nil {
  115. height := size.Height - padding*2 - (pos.Y - padding/2) // adjust for content and initial offset
  116. if c.card.Title != "" || c.card.Subtitle != "" {
  117. height += padding
  118. pos.Y -= padding
  119. }
  120. c.card.Content.Move(pos.Add(fyne.NewPos(0, padding)))
  121. c.card.Content.Resize(fyne.NewSize(size.Width, height))
  122. }
  123. }
  124. // MinSize calculates the minimum size of a card.
  125. // This is based on the contained text, image and content.
  126. func (c *cardRenderer) MinSize() fyne.Size {
  127. hasHeader := c.card.Title != ""
  128. hasSubHeader := c.card.Subtitle != ""
  129. hasImage := c.card.Image != nil
  130. hasContent := c.card.Content != nil
  131. padding := theme.Padding()
  132. if !hasHeader && !hasSubHeader && !hasContent { // just image, or nothing
  133. if c.card.Image == nil {
  134. return fyne.NewSize(padding, padding) // empty, just space for border
  135. }
  136. return fyne.NewSize(c.card.Image.MinSize().Width+padding, cardMediaHeight+padding)
  137. }
  138. min := fyne.NewSize(padding, padding)
  139. if hasImage {
  140. min = fyne.NewSize(min.Width, min.Height+cardMediaHeight)
  141. }
  142. if hasHeader || hasSubHeader {
  143. titlePad := padding * 2
  144. min = min.Add(fyne.NewSize(0, titlePad*2))
  145. if hasHeader {
  146. headerMin := c.header.MinSize()
  147. min = fyne.NewSize(fyne.Max(min.Width, headerMin.Width+titlePad*2+padding),
  148. min.Height+headerMin.Height)
  149. if hasSubHeader {
  150. min.Height += padding
  151. }
  152. }
  153. if hasSubHeader {
  154. subHeaderMin := c.subHeader.MinSize()
  155. min = fyne.NewSize(fyne.Max(min.Width, subHeaderMin.Width+titlePad*2+padding),
  156. min.Height+subHeaderMin.Height)
  157. }
  158. }
  159. if hasContent {
  160. contentMin := c.card.Content.MinSize()
  161. min = fyne.NewSize(fyne.Max(min.Width, contentMin.Width+padding*3),
  162. min.Height+contentMin.Height+padding*2)
  163. }
  164. return min
  165. }
  166. func (c *cardRenderer) Refresh() {
  167. c.header.Text = c.card.Title
  168. c.header.Refresh()
  169. c.subHeader.Text = c.card.Subtitle
  170. c.subHeader.Refresh()
  171. objects := []fyne.CanvasObject{c.header, c.subHeader}
  172. if c.card.Image != nil {
  173. objects = append(objects, c.card.Image)
  174. }
  175. if c.card.Content != nil {
  176. objects = append(objects, c.card.Content)
  177. }
  178. c.ShadowingRenderer.SetObjects(objects)
  179. c.applyTheme()
  180. c.Layout(c.card.Size())
  181. c.ShadowingRenderer.RefreshShadow()
  182. canvas.Refresh(c.card.super())
  183. }
  184. // applyTheme updates this button to match the current theme
  185. func (c *cardRenderer) applyTheme() {
  186. if c.header != nil {
  187. c.header.TextSize = theme.TextHeadingSize()
  188. c.header.Color = theme.ForegroundColor()
  189. }
  190. if c.subHeader != nil {
  191. c.subHeader.TextSize = theme.TextSize()
  192. c.subHeader.Color = theme.ForegroundColor()
  193. }
  194. if c.card.Content != nil {
  195. c.card.Content.Refresh()
  196. }
  197. }