-
I read the code and demo related to the gti repository and structview, but I still can't find a way to get the value of the struct field, for example, after setStruct, all the fields are displayed on the ui, and then you need to get the information entered by the user from the first field of the ui for other operations. According to the reflection logic, there should be a traversal of the field and the valueof operation to take out the field value after setting the struct, unfortunately the relevant code was not found in the struct view ... |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
You can just directly access the field of the underlying struct. For example: type Person struct {
Name string
Age int
}
p := &Person{}
giv.NewStructView(par).SetStruct(p)
...
// when you want to access the data of the struct
fmt.Println(p.Name) If you want to be notified when the struct view changes, just add an type Person struct {
Name string
Age int
}
p := &Person{}
sv := giv.NewStructView(par).SetStruct(p)
sv.OnChange(func(e events.Event) {
fmt.Println(p.Name)
}) |
Beta Was this translation helpful? Give feedback.
-
I will try, thx you type Object struct { //gti:add
MidEntry string `width:"50"`
RegCode string `width:"50"`
Version string `width:"50"`
Website string `width:"50"`
SimpleMid string `width:"50"`
}
func main() {
gimain.Run(func() {
b := gi.NewAppBody("keygen")
gi.NewLabel(b).SetText("KeyGen for data Recovery")
Choose := gi.NewChooser(b).SetStrings(keygen.InvalidAppNameKindTreeNodeKind.Keys(), true, 200).SetIcons(icons.All())
o := &Object{
MidEntry: "",
RegCode: "",
Version: "",
Website: "",
SimpleMid: "",
}
v := giv.NewStructView(b).SetStruct(o)
log := texteditor.NewEditor(b).SetPlaceholder("log view")
mid := ""
v.OnChange(func(e events.Event) {
mid = o.MidEntry
println(mid)
})
Choose.OnChange(func(e events.Event) {
val := Choose.CurVal
switch val {
case keygen.SuperRecovery4Kind.String():
SuperRecovery4 := keygen.NewSuperRecovery4()
if !SuperRecovery4.GenRegCode(mid) {
o = &Object{
MidEntry: "",
RegCode: "",
Version: "",
Website: "",
SimpleMid: SuperRecovery4.SimpleMid(),
}
v.SetStruct(o)
log.SetBuf(texteditor.NewBuf().SetText([]byte(mylog.Body())))
return
}
o = &Object{
MidEntry: mid,
RegCode: SuperRecovery4.RegCode(),
Version: SuperRecovery4.Version(),
Website: SuperRecovery4.Website(),
SimpleMid: "",
}
v.SetStruct(o) I got the logic right by adding a variable and the onchange event you said. However, I found that onchange or oninput have to press the enter key to make the event take effect, and you can't simply enter it into the edit box to finish, which is still a bit inconvenient, but the layout has become simpler, compared to other UI frameworks. Take the actual scene code I posted as an example, it would be perfect if the onchange event of the struct view could be triggered without pressing the enter key when the onchange of the chooser is triggered. |
Beta Was this translation helpful? Give feedback.
You can just directly access the field of the underlying struct. For example:
If you want to be notified when the struct view changes, just add an
OnChange
event handler: