diff --git a/examples/profilebtn/main.go b/examples/profilebtn/main.go index e89cfab..c70d389 100644 --- a/examples/profilebtn/main.go +++ b/examples/profilebtn/main.go @@ -30,6 +30,8 @@ func main() { } b.SetProfileImage(i) b.OutlineWidth = 5 + b.Disable() + b.OutlineColor = color.RGBA{0x1a, 0x23, 0xce, 0xff} b2 := profilebtn.NewProfileBtn() b2.SetProfileResource(resourceProfilepicdoesnotexistJpg) b2.OutlineWidth = 3 diff --git a/pkg/profilebtn/profilebtn.go b/pkg/profilebtn/profilebtn.go index 812fc3c..6ec5870 100644 --- a/pkg/profilebtn/profilebtn.go +++ b/pkg/profilebtn/profilebtn.go @@ -23,6 +23,7 @@ type ProfileBtn struct { profileImage image.Image minSizeOverridden bool minSizeRequested fyne.Size + disabled bool OnTapped func() `json:"-"` } @@ -54,8 +55,16 @@ func (b *ProfileBtn) Resize(s fyne.Size) { b.BaseWidget.Resize(s) } +func (b *ProfileBtn) Disable() { + b.disabled = true +} + +func (b *ProfileBtn) Ensable() { + b.disabled = false +} + func (b *ProfileBtn) Disabled() bool { - return false + return b.disabled } func (b *ProfileBtn) Tapped(*fyne.PointEvent) { @@ -91,6 +100,7 @@ func NewProfileBtn() *ProfileBtn { w.OutlineColor = color.White w.SetProfileResource(nil) w.minSizeOverridden = false + w.disabled = false return w } @@ -142,11 +152,26 @@ func (r *profileBtnRenderer) Render(w, h int) image.Image { i := image.NewRGBA(image.Rect(0, 0, w, h)) if r.w.profileImage != nil { src := r.w.profileImage + // convert src to srcRGBA + srcb := src.Bounds() + srcRGBA := image.NewRGBA(image.Rect(0, 0, srcb.Dx(), srcb.Dy())) + draw.Draw(srcRGBA, srcRGBA.Bounds(), src, srcb.Min, draw.Src) + b := src.Bounds().Size() wh := image.NewUniform(r.w.OutlineColor) dst := image.NewRGBA(image.Rect(0, 0, b.X, b.Y)) draw.DrawMask(dst, dst.Bounds(), wh, image.ZP, &circle{image.Point{b.X / 2, b.Y / 2}, (min(b.Y, b.Y) / 2)}, image.ZP, draw.Over) - draw.DrawMask(dst, dst.Bounds(), src, image.ZP, &circle{image.Point{b.X / 2, b.Y / 2}, (min(b.Y, b.Y) / 2) - r.w.OutlineWidth}, image.ZP, draw.Over) + if r.w.Disabled() { + for x := 0; x < srcRGBA.Bounds().Dx(); x++ { + for y := 0; y < srcRGBA.Bounds().Dx(); y++ { + c := srcRGBA.RGBA64At(x, y) + avg := (c.R / 3) + (c.G / 3) + (c.B / 3) + c = color.RGBA64{avg, avg, avg, c.A} + srcRGBA.SetRGBA64(x, y, c) + } + } + } + draw.DrawMask(dst, dst.Bounds(), srcRGBA, image.ZP, &circle{image.Point{b.X / 2, b.Y / 2}, (min(b.Y, b.Y) / 2) - r.w.OutlineWidth}, image.ZP, draw.Over) return dst } else { return i