Display: review r3 fixes
This commit is contained in:
6 files changed
+197
-89
No files matched your search
@@ -30,77 +30,100 @@ type ImageView struct {
|
||||
Retry bool
|
||||
}
|
||||
|
||||
// Image shows a bitmap or SVG; nil shows a placeholder.
|
||||
func Image(c *ui.Context, src ui.ImageSource, opts ImageOptions) ImageView {
|
||||
if opts.Width < 0 || opts.Height < 0 {
|
||||
panic("mujica: Image Width and Height must not be negative")
|
||||
}
|
||||
if opts.Err == nil && !opts.Loading && src != nil {
|
||||
e := ui.Image(c, src).Radius(theme.ControlRadius)
|
||||
if opts.Crop {
|
||||
e.Fit(ui.Cover)
|
||||
}
|
||||
if opts.Width > 0 {
|
||||
e.Width(opts.Width)
|
||||
}
|
||||
if opts.Height > 0 {
|
||||
e.Height(opts.Height)
|
||||
}
|
||||
if opts.Alt != "" {
|
||||
e.Label(opts.Alt)
|
||||
}
|
||||
return ImageView{Element: e}
|
||||
}
|
||||
k := Tokens(c)
|
||||
u := Density(c).Unit()
|
||||
w, h := imageBox(src, opts.Width, opts.Height)
|
||||
var v ImageView
|
||||
// Same size rules as the loaded image.
|
||||
frame := ui.Row(c).AspectRatio(w/h).Center().Gap(u*2).Padding(0, u).Clip().
|
||||
Radius(theme.ControlRadius).Background(k.SurfaceHover).Border(theme.BorderWidth, k.Border)
|
||||
if opts.Width > 0 || opts.Height == 0 {
|
||||
frame.Width(w)
|
||||
}
|
||||
if opts.Height > 0 {
|
||||
frame.Height(h)
|
||||
}
|
||||
v.Element = frame
|
||||
room := frame.Bounds()
|
||||
// Build again when the box changed, settling the error text.
|
||||
if last := ui.Local(frame, imageRoomKey{}, func() ui.Rect { return ui.Rect{W: -1} }); *last != room {
|
||||
*last = room
|
||||
c.AnimationFrame()
|
||||
}
|
||||
frame.Children(func() {
|
||||
switch {
|
||||
case opts.Err != nil:
|
||||
frame.Role(ui.RoleGroup).Label(msg(c, "image.error")).Description(opts.Alt).Error(opts.Err.Error())
|
||||
if imageRetry(c, k, opts.Err.Error()).Clicked() {
|
||||
v.Retry = true
|
||||
slog.Debug("mujica: image retry", "alt", opts.Alt)
|
||||
}
|
||||
if room.W >= theme.MinHitSize+u*4+imageErrorTextWidth {
|
||||
ui.Text(c, msg(c, "image.error")).FontSize(FontSize(c, theme.CaptionSize)).TextColor(k.Danger).SingleLine()
|
||||
}
|
||||
case opts.Loading:
|
||||
frame.Role(ui.RoleImage).Label(msg(c, "image.loading")).Description(opts.Alt)
|
||||
Spinner(c, SpinnerOptions{Size: min(20, room.H), Label: msg(c, "image.loading")})
|
||||
default:
|
||||
frame.Role(ui.RoleImage).Label(msg(c, "image.placeholder")).Description(opts.Alt)
|
||||
Icon(c, "image", IconOptions{Size: 24, Tone: IconMuted})
|
||||
}
|
||||
})
|
||||
return v
|
||||
}
|
||||
// imageBlank sizes the box of a nil src: 160×120.
|
||||
var imageBlank = ui.MustParseSVG([]byte(`<svg xmlns="http://www.w3.org/2000/svg" width="160" height="120"/>`))
|
||||
|
||||
type imageRoomKey struct{}
|
||||
|
||||
// imageErrorTextWidth is the room the error text needs.
|
||||
const imageErrorTextWidth = 120
|
||||
|
||||
// Image shows a bitmap or SVG; nil shows a placeholder.
|
||||
func Image(c *ui.Context, src ui.ImageSource, opts ImageOptions) ImageView {
|
||||
if opts.Width < 0 || opts.Height < 0 {
|
||||
panic("mujica: Image Width and Height must not be negative")
|
||||
}
|
||||
loaded := opts.Err == nil && !opts.Loading && src != nil
|
||||
var v ImageView
|
||||
v.Element = imageSized(c, src, opts, func(box, pic *ui.Element) {
|
||||
if loaded {
|
||||
if opts.Alt != "" {
|
||||
pic.Label(opts.Alt)
|
||||
}
|
||||
return
|
||||
}
|
||||
pic.Invisible()
|
||||
v.Retry = imageLayers(c, box, opts)
|
||||
})
|
||||
return v
|
||||
}
|
||||
|
||||
// imageLayers builds the state layers over the hidden picture.
|
||||
func imageLayers(c *ui.Context, box *ui.Element, opts ImageOptions) (retry bool) {
|
||||
k := Tokens(c)
|
||||
u := Density(c).Unit()
|
||||
room := box.Bounds()
|
||||
// Build again when the box changed, settling the error text.
|
||||
if last := ui.Local(box, imageRoomKey{}, func() ui.Rect { return ui.Rect{W: -1} }); *last != room {
|
||||
*last = room
|
||||
c.AnimationFrame()
|
||||
}
|
||||
{
|
||||
// The clipped backdrop; Retry sits above it, unclipped.
|
||||
back := ui.Box(c).Absolute().Left(0).Top(0).Right(0).Bottom(0).Center().Clip().
|
||||
Radius(theme.ControlRadius).Background(k.SurfaceHover).Border(theme.BorderWidth, k.Border)
|
||||
switch {
|
||||
case opts.Err != nil:
|
||||
box.Role(ui.RoleGroup).Label(msg(c, "image.error")).Description(opts.Alt).Error(opts.Err.Error())
|
||||
ui.Row(c).Absolute().Left(0).Top(0).Right(0).Bottom(0).Center().Gap(u * 2).Children(func() {
|
||||
if imageRetry(c, k, opts.Err.Error()).Clicked() {
|
||||
retry = true
|
||||
slog.Debug("mujica: image retry", "alt", opts.Alt)
|
||||
}
|
||||
if room.W >= theme.MinHitSize+u*4+imageErrorTextWidth && room.H >= theme.MinHitSize {
|
||||
ui.Text(c, msg(c, "image.error")).FontSize(FontSize(c, theme.CaptionSize)).TextColor(k.Danger).SingleLine()
|
||||
}
|
||||
})
|
||||
case opts.Loading:
|
||||
box.Role(ui.RoleImage).Label(msg(c, "image.loading")).Description(opts.Alt)
|
||||
back.Children(func() {
|
||||
Spinner(c, SpinnerOptions{Size: max(8, min(20, room.H-4)), Label: msg(c, "image.loading")})
|
||||
})
|
||||
default:
|
||||
box.Role(ui.RoleImage).Label(msg(c, "image.placeholder")).Description(opts.Alt)
|
||||
back.Children(func() { Icon(c, "image", IconOptions{Size: 24, Tone: IconMuted}) })
|
||||
}
|
||||
}
|
||||
return retry
|
||||
}
|
||||
|
||||
// imageSized builds the outer box and its picture, sized alike in every state.
|
||||
func imageSized(c *ui.Context, src ui.ImageSource, opts ImageOptions, layers func(box, pic *ui.Element)) *ui.Element {
|
||||
if src == nil {
|
||||
src = imageBlank
|
||||
}
|
||||
box := ui.Box(c)
|
||||
box.Children(func() {
|
||||
pic := ui.Image(c, src).Radius(theme.ControlRadius)
|
||||
if opts.Crop {
|
||||
pic.Fit(ui.Cover)
|
||||
}
|
||||
if opts.Width > 0 {
|
||||
box.Width(opts.Width)
|
||||
pic.Width(opts.Width)
|
||||
}
|
||||
if opts.Height > 0 {
|
||||
box.Height(opts.Height)
|
||||
pic.Height(opts.Height)
|
||||
}
|
||||
layers(box, pic)
|
||||
})
|
||||
return box
|
||||
}
|
||||
|
||||
// imageRetry is the 28 DIP icon-only retry button.
|
||||
func imageRetry(c *ui.Context, k theme.Tokens, problem string) *ui.Element {
|
||||
b := ui.ButtonBase(c).Size(theme.MinHitSize, theme.MinHitSize).Radius(theme.ControlRadius).
|
||||
b := ui.ButtonBase(c).Size(theme.MinHitSize, theme.MinHitSize).Shrink(0).Radius(theme.ControlRadius).
|
||||
Label(msg(c, "image.retry")).Description(problem).Tooltip(problem)
|
||||
bg := k.Surface
|
||||
if b.Pressed() {
|
||||
@@ -114,27 +137,3 @@ func imageRetry(c *ui.Context, k theme.Tokens, problem string) *ui.Element {
|
||||
b.Children(func() { Icon(c, "refresh-cw", IconOptions{Size: 16}) })
|
||||
return b
|
||||
}
|
||||
|
||||
// imageBox fills unset sides from src's aspect, else 160×120.
|
||||
func imageBox(src ui.ImageSource, w, h float32) (float32, float32) {
|
||||
var sw, sh float32
|
||||
switch s := src.(type) {
|
||||
case *ui.Bitmap:
|
||||
iw, ih := s.Size()
|
||||
sw, sh = float32(iw), float32(ih)
|
||||
case *ui.SVG:
|
||||
sw, sh = s.Size()
|
||||
}
|
||||
if sw <= 0 || sh <= 0 {
|
||||
sw, sh = 160, 120
|
||||
}
|
||||
switch {
|
||||
case w == 0 && h == 0:
|
||||
return sw, sh
|
||||
case w == 0:
|
||||
return h * sw / sh, h
|
||||
case h == 0:
|
||||
return w, w * sh / sw
|
||||
}
|
||||
return w, h
|
||||
}
|
||||
Reference in new issue
Block a user