Skip to content

Commit

Permalink
widget: Adapt to use square size and pos
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacalz committed Jun 18, 2023
1 parent 2a1b415 commit 0618a14
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 42 deletions.
8 changes: 4 additions & 4 deletions widget/button.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (b *Button) CreateRenderer() fyne.WidgetRenderer {
seg := &TextSegment{Text: b.Text, Style: RichTextStyleStrong}
seg.Style.Alignment = fyne.TextAlignCenter
text := NewRichText(seg)
text.inset = fyne.NewSize(theme.InnerPadding(), theme.InnerPadding())
text.inset = fyne.NewSquareSize(theme.InnerPadding())

b.background = canvas.NewRectangle(theme.ButtonColor())
b.background.CornerRadius = theme.InputRadiusSize()
Expand Down Expand Up @@ -299,7 +299,7 @@ func (r *buttonRenderer) Layout(size fyne.Size) {
// Nothing to layout
return
}
iconSize := fyne.NewSize(theme.IconInlineSize(), theme.IconInlineSize())
iconSize := fyne.NewSquareSize(theme.IconInlineSize())
labelSize := r.label.MinSize()
padding := r.padding()
if hasLabel {
Expand Down Expand Up @@ -336,7 +336,7 @@ func (r *buttonRenderer) Layout(size fyne.Size) {
func (r *buttonRenderer) MinSize() (size fyne.Size) {
hasIcon := r.icon != nil
hasLabel := r.label.Segments[0].(*TextSegment).Text != ""
iconSize := fyne.NewSize(theme.IconInlineSize(), theme.IconInlineSize())
iconSize := fyne.NewSquareSize(theme.IconInlineSize())
labelSize := r.label.MinSize()
if hasLabel {
size.Width = labelSize.Width
Expand Down Expand Up @@ -394,7 +394,7 @@ func (r *buttonRenderer) applyTheme() {
}

func (r *buttonRenderer) padding() fyne.Size {
return fyne.NewSize(theme.InnerPadding()*2, theme.InnerPadding()*2)
return fyne.NewSquareSize(theme.InnerPadding() * 2)
}

func (r *buttonRenderer) updateIconAndText() {
Expand Down
46 changes: 23 additions & 23 deletions widget/card.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (c *Card) CreateRenderer() fyne.WidgetRenderer {

header := canvas.NewText(c.Title, theme.ForegroundColor())
header.TextStyle.Bold = true
subHeader := canvas.NewText(c.Subtitle, theme.ForegroundColor())
subHeader := canvas.NewText(c.Subtitle, header.Color)

objects := []fyne.CanvasObject{header, subHeader}
if c.Image != nil {
Expand Down Expand Up @@ -100,8 +100,9 @@ const (

// Layout the components of the card container.
func (c *cardRenderer) Layout(size fyne.Size) {
pos := fyne.NewPos(theme.Padding()/2, theme.Padding()/2)
size = size.Subtract(fyne.NewSize(theme.Padding(), theme.Padding()))
padding := theme.Padding()
pos := fyne.NewSquarePos(padding / 2)
size = size.Subtract(fyne.NewSquareSize(padding))
c.LayoutShadow(size, pos)

if c.card.Image != nil {
Expand All @@ -110,9 +111,8 @@ func (c *cardRenderer) Layout(size fyne.Size) {
pos.Y += cardMediaHeight
}

contentPad := theme.Padding()
if c.card.Title != "" || c.card.Subtitle != "" {
titlePad := theme.Padding() * 2
titlePad := padding * 2
size.Width -= titlePad * 2
pos.X += titlePad
pos.Y += titlePad
Expand All @@ -121,30 +121,30 @@ func (c *cardRenderer) Layout(size fyne.Size) {
height := c.header.MinSize().Height
c.header.Move(pos)
c.header.Resize(fyne.NewSize(size.Width, height))
pos.Y += height + theme.Padding()
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 + theme.Padding()
pos.Y += height + padding
}

size.Width = size.Width + titlePad*2
pos.X = pos.X - titlePad
pos.Y += titlePad
}

size.Width -= contentPad * 2
pos.X += contentPad
size.Width -= padding * 2
pos.X += padding
if c.card.Content != nil {
height := size.Height - contentPad*2 - (pos.Y - theme.Padding()/2) // adjust for content and initial offset
height := size.Height - padding*2 - (pos.Y - padding/2) // adjust for content and initial offset
if c.card.Title != "" || c.card.Subtitle != "" {
height += contentPad
pos.Y -= contentPad
height += padding
pos.Y -= padding
}
c.card.Content.Move(pos.Add(fyne.NewPos(0, contentPad)))
c.card.Content.Move(pos.Add(fyne.NewPos(0, padding)))
c.card.Content.Resize(fyne.NewSize(size.Width, height))
}
}
Expand All @@ -157,41 +157,41 @@ func (c *cardRenderer) MinSize() fyne.Size {
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(theme.Padding(), theme.Padding()) // empty, just space for border
return fyne.NewSize(padding, padding) // empty, just space for border
}
return fyne.NewSize(c.card.Image.MinSize().Width+theme.Padding(), cardMediaHeight+theme.Padding())
return fyne.NewSize(c.card.Image.MinSize().Width+padding, cardMediaHeight+padding)
}

contentPad := theme.Padding()
min := fyne.NewSize(theme.Padding(), theme.Padding())
min := fyne.NewSize(padding, padding)
if hasImage {
min = fyne.NewSize(min.Width, min.Height+cardMediaHeight)
}

if hasHeader || hasSubHeader {
titlePad := theme.Padding() * 2
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+theme.Padding()),
min = fyne.NewSize(fyne.Max(min.Width, headerMin.Width+titlePad*2+padding),
min.Height+headerMin.Height)
if hasSubHeader {
min.Height += theme.Padding()
min.Height += padding
}
}
if hasSubHeader {
subHeaderMin := c.subHeader.MinSize()
min = fyne.NewSize(fyne.Max(min.Width, subHeaderMin.Width+titlePad*2+theme.Padding()),
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+contentPad*2+theme.Padding()),
min.Height+contentMin.Height+contentPad*2)
min = fyne.NewSize(fyne.Max(min.Width, contentMin.Width+padding*3),
min.Height+contentMin.Height+padding*2)
}

return min
Expand Down
4 changes: 2 additions & 2 deletions widget/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (c *checkRenderer) MinSize() fyne.Size {

// Layout the components of the check widget
func (c *checkRenderer) Layout(size fyne.Size) {
focusIndicatorSize := fyne.NewSize(theme.IconInlineSize()+theme.InnerPadding(), theme.IconInlineSize()+theme.InnerPadding())
focusIndicatorSize := fyne.NewSquareSize(theme.IconInlineSize() + theme.InnerPadding())
c.focusIndicator.Resize(focusIndicatorSize)
c.focusIndicator.Move(fyne.NewPos(theme.InputBorderSize(), (size.Height-focusIndicatorSize.Height)/2))

Expand All @@ -44,7 +44,7 @@ func (c *checkRenderer) Layout(size fyne.Size) {
c.label.Move(fyne.NewPos(xOff, 0))

iconPos := fyne.NewPos(theme.InnerPadding()/2+theme.InputBorderSize(), (size.Height-theme.IconInlineSize())/2)
iconSize := fyne.NewSize(theme.IconInlineSize(), theme.IconInlineSize())
iconSize := fyne.NewSquareSize(theme.IconInlineSize())
c.bg.Move(iconPos)
c.bg.Resize(iconSize)
c.icon.Resize(iconSize)
Expand Down
12 changes: 6 additions & 6 deletions widget/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -1332,21 +1332,21 @@ func (r *entryRenderer) Layout(size fyne.Size) {
// 0.5 is removed so on low DPI it rounds down on the trailing edge
r.border.Resize(fyne.NewSize(size.Width-theme.InputBorderSize()-.5, size.Height-theme.InputBorderSize()-.5))
r.border.StrokeWidth = theme.InputBorderSize()
r.border.Move(fyne.NewPos(theme.InputBorderSize()/2, theme.InputBorderSize()/2))
r.box.Resize(size.Subtract(fyne.NewSize(theme.InputBorderSize()*2, theme.InputBorderSize()*2)))
r.box.Move(fyne.NewPos(theme.InputBorderSize(), theme.InputBorderSize()))
r.border.Move(fyne.NewSquarePos(theme.InputBorderSize() / 2))
r.box.Resize(size.Subtract(fyne.NewSquareSize(theme.InputBorderSize() * 2)))
r.box.Move(fyne.NewSquarePos(theme.InputBorderSize()))

actionIconSize := fyne.NewSize(0, 0)
if r.entry.ActionItem != nil {
actionIconSize = fyne.NewSize(theme.IconInlineSize(), theme.IconInlineSize())
actionIconSize = fyne.NewSquareSize(theme.IconInlineSize())

r.entry.ActionItem.Resize(actionIconSize)
r.entry.ActionItem.Move(fyne.NewPos(size.Width-actionIconSize.Width-theme.InnerPadding(), theme.InnerPadding()))
}

validatorIconSize := fyne.NewSize(0, 0)
if r.entry.Validator != nil {
validatorIconSize = fyne.NewSize(theme.IconInlineSize(), theme.IconInlineSize())
validatorIconSize = fyne.NewSquareSize(theme.IconInlineSize())

r.ensureValidationSetup()
r.entry.validationStatus.Resize(validatorIconSize)
Expand Down Expand Up @@ -1401,7 +1401,7 @@ func (r *entryRenderer) MinSize() fyne.Size {
}

charMin := r.entry.placeholderProvider().charMinSize(r.entry.Password, r.entry.TextStyle)
minSize := charMin.Add(fyne.NewSize(theme.InnerPadding(), theme.InnerPadding()))
minSize := charMin.Add(fyne.NewSquareSize(theme.InnerPadding()))

if r.entry.MultiLine {
count := r.entry.multiLineRows
Expand Down
3 changes: 1 addition & 2 deletions widget/fileicon.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@ type fileIconRenderer struct {
}

func (s *fileIconRenderer) MinSize() fyne.Size {
size := theme.IconInlineSize()
return fyne.NewSize(size, size)
return fyne.NewSquareSize(theme.IconInlineSize())
}

func (s *fileIconRenderer) Layout(size fyne.Size) {
Expand Down
3 changes: 1 addition & 2 deletions widget/icon.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ type iconRenderer struct {
}

func (i *iconRenderer) MinSize() fyne.Size {
size := theme.IconInlineSize()
return fyne.NewSize(size, size)
return fyne.NewSquareSize(theme.IconInlineSize())
}

func (i *iconRenderer) Layout(size fyne.Size) {
Expand Down
4 changes: 2 additions & 2 deletions widget/radio_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ type radioItemRenderer struct {
}

func (r *radioItemRenderer) Layout(size fyne.Size) {
focusIndicatorSize := fyne.NewSize(theme.IconInlineSize()+theme.InnerPadding(), theme.IconInlineSize()+theme.InnerPadding())
focusIndicatorSize := fyne.NewSquareSize(theme.IconInlineSize() + theme.InnerPadding())
r.focusIndicator.Resize(focusIndicatorSize)
r.focusIndicator.Move(fyne.NewPos(theme.InputBorderSize(), (size.Height-focusIndicatorSize.Height)/2))

Expand All @@ -167,7 +167,7 @@ func (r *radioItemRenderer) Layout(size fyne.Size) {
r.label.Move(fyne.NewPos(focusIndicatorSize.Width+theme.Padding(), 0))

iconPos := fyne.NewPos(theme.InnerPadding()/2+theme.InputBorderSize(), (size.Height-theme.IconInlineSize())/2)
iconSize := fyne.NewSize(theme.IconInlineSize(), theme.IconInlineSize())
iconSize := fyne.NewSquareSize(theme.IconInlineSize())
r.icon.Resize(iconSize)
r.icon.Move(iconPos)
r.over.Resize(iconSize)
Expand Down
2 changes: 1 addition & 1 deletion widget/slider.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ func (s *sliderRenderer) Layout(size fyne.Size) {
s.thumb.Move(thumbPos)
s.thumb.Resize(fyne.NewSize(diameter, diameter))

focusIndicatorSize := fyne.NewSize(theme.IconInlineSize()+theme.InnerPadding(), theme.IconInlineSize()+theme.InnerPadding())
focusIndicatorSize := fyne.NewSquareSize(theme.IconInlineSize() + theme.InnerPadding())
delta := (focusIndicatorSize.Width - diameter) / 2
s.focusIndicator.Resize(focusIndicatorSize)
s.focusIndicator.Move(thumbPos.SubtractXY(delta, delta))
Expand Down

0 comments on commit 0618a14

Please sign in to comment.