Golang Fyne v2 custom entry widget can not overrule the FocusGained() function?

Issue

Im currently working in Golang Fyne V2 framework. I want to call a custom function when my entry widget has been clicked/selected. My approach was to make a custom entry widget.
https://developer.fyne.io/api/v2.0/widget/entry.html

Problem is that my custom widget is not overruling the following function:

func (e *Entry) Tapped(ev *fyne.PointEvent)

OR

func (e *Entry) FocusGained()

My custom input field looks basicly like this. I see the inputField rendered in a Gui container so thats great. But when I click on it the "EXTRA FUNCTION CALL" is not being printed.

package gui

import (
    "fmt"

    "fyne.io/fyne/v2"
    "fyne.io/fyne/v2/driver/desktop"
    "fyne.io/fyne/v2/widget"
)

type InputField struct {
    widget.BaseWidget
    inputText      *widget.Entry
    customFunction func()
}

func NewInputField() *InputField {

    i := &InputField{
        BaseWidget: widget.BaseWidget{},
        inputText:  widget.NewEntry(),
    }

    i.inputText.Resize(i.MinSize())


    i.customFunction = func() {
        fmt.Println("EXTRA FUNCTION CALL")
    }

    i.ExtendBaseWidget(i)

    return i
}

func (i *InputField) MinSize() fyne.Size {
    return fyne.NewSize(600, 30)
}

func (i *InputField) Tapped(ev *fyne.PointEvent) {
    fmt.Println("I have been tapped")
    i.customFunction()
}

func (i *InputField) FocusGained() {
    fmt.Println("I have been focussed")
    i.customFunction()
}

func (i *InputField) FocusLost() {
    fmt.Println("Focus lost")
}

func (i *InputField) MouseDown(m *desktop.MouseEvent) {
    fmt.Println("MouseDown")
    i.customFunction()
}

func (t *InputField) CreateRenderer() fyne.WidgetRenderer {
    return NewBaseRenderer([]fyne.CanvasObject{t.inputText})
}

Solution

Don’t extend BaseWidget and Entry, just extend widget.Entry directly.
With that change it should work as you expect.

Answered By – andy.xyz

Answer Checked By – David Marino (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.