I implemented simple widgets for my GUI project, but I feel like there's a much better way to do this

Hi,

I have the following Window/Widget structs:

 Window struct {
         *Widget
         Children  []*Widget
}

 Widget struct {
         X, Y int
         Parent     *Window
         Type       WidgetType

         OnKeyPress func(KeyEvent)

         This interface{} // This is very ugly, I think
}

TextBox struct {
    *Widget
    Text string
}

And in my event processing code I have:

for _, child := range window.Children {
         if child.Type == TextBox && child.IsFocused() {
                 child.This.(*TextBox).Text += string(char)
         }
}

This works but feels really wrong. Is there a better way to do this?

Thanks

You may get some inspiration from how it is solved in this project

https://github.com/andlabs/ui/blob/master/control.go defines an interface Control and a struct ControlBase implementing this interface. And then is the interface used on every place where you would have several different types of controls in a slice for example.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.