recalc recalculates all dimensions and position from inside out
()
| 206 | |
| 207 | // recalc recalculates all dimensions and position from inside out |
| 208 | func (b *Button) recalc() { |
| 209 | |
| 210 | // Current width and height of button content area |
| 211 | width := b.Panel.ContentWidth() |
| 212 | height := b.Panel.ContentHeight() |
| 213 | |
| 214 | // Image or icon width |
| 215 | imgWidth := float32(0) |
| 216 | spacing := float32(4) |
| 217 | if b.image != nil { |
| 218 | imgWidth = b.image.Width() |
| 219 | } else if b.icon != nil { |
| 220 | imgWidth = b.icon.Width() |
| 221 | } |
| 222 | if imgWidth == 0 { |
| 223 | spacing = 0 |
| 224 | } |
| 225 | |
| 226 | // If the label is empty and an icon of image was defined ignore the label widthh |
| 227 | // to centralize the icon/image in the button |
| 228 | labelWidth := spacing + b.Label.Width() |
| 229 | if b.Label.Text() == "" && imgWidth > 0 { |
| 230 | labelWidth = 0 |
| 231 | } |
| 232 | |
| 233 | // Sets new content width and height if necessary |
| 234 | minWidth := imgWidth + labelWidth |
| 235 | minHeight := b.Label.Height() |
| 236 | resize := false |
| 237 | if width < minWidth { |
| 238 | width = minWidth |
| 239 | resize = true |
| 240 | } |
| 241 | if height < minHeight { |
| 242 | height = minHeight |
| 243 | resize = true |
| 244 | } |
| 245 | if resize { |
| 246 | b.SetContentSize(width, height) |
| 247 | } |
| 248 | |
| 249 | // Centralize horizontally |
| 250 | px := (width - minWidth) / 2 |
| 251 | |
| 252 | // Set label position |
| 253 | ly := (height - b.Label.Height()) / 2 |
| 254 | b.Label.SetPosition(px+imgWidth+spacing, ly) |
| 255 | |
| 256 | // Image/icon position |
| 257 | if b.image != nil { |
| 258 | iy := (height - b.image.height) / 2 |
| 259 | b.image.SetPosition(px, iy) |
| 260 | } else if b.icon != nil { |
| 261 | b.icon.SetPosition(px, ly) |
| 262 | } |
| 263 | } |
no test coverage detected