Tapped method of custom widget.PopUp doesn't work

61 views Asked by At

I tried to extend PopUp widget to add new functionality with new Tapped method.

Here's example, where I created CustomLabel and CustomPopUp. Both with new method Tapped:

package main

import (
    "fmt"

    "fyne.io/fyne/v2"
    "fyne.io/fyne/v2/app"
    "fyne.io/fyne/v2/container"
    "fyne.io/fyne/v2/widget"
)

func main() {
    a := app.New()
    w := a.NewWindow("Bug0")
    w.Resize(fyne.NewSize(700, 700))
    w.SetContent(container.NewCenter(newCustomLabel("Custom Label")))
    w.ShowAndRun()
}


// Extending basic Label works perfectly fine

type CustomLabel struct {
    widget.Label
}

func newCustomLabel(name string) *CustomLabel {
    lb := &CustomLabel{}
    lb.ExtendBaseWidget(lb)
    lb.SetText(name)
    return lb
}

func (t *CustomLabel) Tapped(pe *fyne.PointEvent) {
    entryPos := fyne.CurrentApp().Driver().AbsolutePositionForObject(t)
    popUpPos := entryPos.Add(pe.Position)

    label := widget.NewLabel("Hello")
    con := container.NewCenter(label)
    c := fyne.CurrentApp().Driver().CanvasForObject(t)

    pop := newCustomPopUp(con, c)
    pop.Resize(fyne.NewSize(200, 200))
    pop.ShowAtPosition(popUpPos)
}


// Same thing with PopUp widget doesn't work

type CustomPopUp struct {
    widget.PopUp
}

func newCustomPopUp(content fyne.CanvasObject, canvas fyne.Canvas) *CustomPopUp {
    p := &CustomPopUp{}
    p.ExtendBaseWidget(p)
    p.Content = content
    p.Canvas = canvas
    return p
}

func (p *CustomPopUp) Tapped(_ *fyne.PointEvent) {
    fmt.Println("Custom PopUp Tapped")
}

On click on the label new CustomPopUp opens. So label's Tapped is working. But Tapped method of CustomPopUp is ignored. Original method of widget.PopUp is called instead. What did I miss?

0

There are 0 answers