Skip to content

Commit

Permalink
feat: add form to gui translator (not fully working)
Browse files Browse the repository at this point in the history
  • Loading branch information
virus-rpi committed May 22, 2024
1 parent ef2499e commit 9227cf5
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 28 deletions.
37 changes: 11 additions & 26 deletions gui/setupDialog.go
Original file line number Diff line number Diff line change
@@ -1,45 +1,30 @@
package gui

import (
"CUBUS-core/shared"
"CUBUS-core/shared/forms"
"CUBUS-core/shared/translation"
"CUBUS-core/shared/types"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/widget"
"fyne.io/fyne/v2/layout"
"github.com/google/uuid"
)

func setupDialog(window fyne.Window) shared.CubeConfigType { // TODO: use the shared dialog tree to create the setup dialog form and return the cube config
func setupDialog(window fyne.Window) types.CubeConfig { // TODO: add a on change listener that puts the values into the cubeConfig
T := translation.T

cubeConfig := shared.CubeConfigType{
cubeConfig := types.CubeConfig{
Id: uuid.New().String(),
CubeType: shared.CubeTypes["generic-worker"],
CubeType: types.CubeTypes.GenericWorker,
PublicKey: nil,
}

cubeTypeLabels := make([]string, len(shared.CubeTypes))
counter := 0
for _, cubeType := range shared.CubeTypes {
cubeTypeLabels[counter] = cubeType.Label
counter++
}

locationItem := widget.NewSelect([]string{T("Local"), T("Remote")}, func(string) {})
typeItem := widget.NewSelect(cubeTypeLabels, func(string) {})
publicKeyItem := widget.NewMultiLineEntry()

form := &widget.Form{
Items: []*widget.FormItem{
{Text: T("Location"), Widget: locationItem},
{Text: T("Type"), Widget: typeItem},
{Text: T("Public Key"), Widget: publicKeyItem},
},
OnSubmit: func() {
},
}
cubeSetupForm := forms.GetCubeSetupForm()
box := container.New(layout.NewVBoxLayout())
forms.FormToFyneForm(cubeSetupForm, box)

dialog.NewCustom(T("Setup"), "OK", form, window).Show()
dialog.NewCustom(T("Setup"), "OK", box, window).Show()

return cubeConfig
}
2 changes: 1 addition & 1 deletion shared/forms/cubeSetupForm.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var CubeTypes = map[string]Option{
},
}

func getCubeSetupForm() *Form {
func GetCubeSetupForm() *Form {
queenCubeSetupFields := NewFieldGroup(
"queenCubeSetup",
[]DisplayCondition{&HasValueDisplayCondition{fieldId: "cubeType", value: "queen"}},
Expand Down
23 changes: 22 additions & 1 deletion shared/forms/forms.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,16 @@ type FieldGroup struct {
Fields []Field
}

func (f *FieldGroup) GetFieldsToDisplay() []Field {
var fieldsToDisplay []Field
for _, field := range f.Fields {
if field.ShouldDisplay() {
fieldsToDisplay = append(fieldsToDisplay, field)
}
}
return fieldsToDisplay
}

// Defining the Form Type

type Form struct {
Expand Down Expand Up @@ -346,7 +356,18 @@ func (f *Form) SetOnChangeCallback(onChange func()) {
func NewForm(fields ...Field) *Form {
form := &Form{Fields: fields, onChange: func() {}}
for _, field := range fields {
field.(*FieldBaseType).form = form
switch v := field.(type) {
case *FieldBaseType:
v.form = form
case *TextField:
v.FieldBaseType.form = form
case *NumberField:
v.TextField.FieldBaseType.form = form
case *MultipleChoiceField:
v.TextField.FieldBaseType.form = form
case *FieldGroup:
v.FieldBaseType.form = form
}
}
return form
}
Expand Down
74 changes: 74 additions & 0 deletions shared/forms/gui.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package forms

import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/widget"
)

func refreshForm(form *Form, box *fyne.Container) {
FormToFyneForm(form, box)
}

func fieldsToFyneForm(fields []Field, form *Form, box *fyne.Container) *widget.Form {
fyneForm := widget.NewForm()

for _, field := range fields { // TODO: Fix the issue that only one field is displayed
switch field := field.(type) {
case *FieldBaseType:
// Do nothing
case *TextField:
entry := widget.NewEntry()
entry.SetText(field.GetValue())
entry.SetPlaceHolder(field.GetPlaceholder())
entry.OnChanged = func(text string) {
field.SetValue(text)
refreshForm(form, box)
}
fyneForm.Append(field.GetId(), entry)
case *MultipleChoiceField: // TODO: Use label instead of key for the options
options := make([]string, 0, len(field.Options))
for key := range field.Options {
options = append(options, key)
}
selectWidget := widget.NewSelect(options, func(value string) { // TODO: translate label back to key
field.SetValue(value)
})
selectWidget.SetSelected(field.GetValue())
selectWidget.OnChanged = func(value string) {
field.SetValue(value)
refreshForm(form, box)
}
fyneForm.Append(field.GetId(), selectWidget)
case *Message:
label := widget.NewLabel(field.GetValue())
fyneForm.Append(field.GetId(), label)
case *NumberField:
entry := widget.NewEntry()
entry.SetText(field.GetValue())
entry.SetPlaceHolder(field.GetPlaceholder())
entry.OnChanged = func(text string) {
field.SetValue(text)
refreshForm(form, box)
}
fyneForm.Append(field.GetId(), entry)
case *FieldGroup:
if field.GetValue() != "" {
label := widget.NewLabel(field.GetValue())
fyneForm.Append(field.GetId(), label)
}
subForm := fieldsToFyneForm(field.GetFieldsToDisplay(), form, box)
fyneForm.Append(field.GetId(), subForm)
default:
panic("Unknown field type")
}
}

return fyneForm
}

func FormToFyneForm(form *Form, box *fyne.Container) {
fields := form.GetFieldsToDisplay()
box.RemoveAll()
box.Add(fieldsToFyneForm(fields, form, box))
box.Refresh()
}
37 changes: 37 additions & 0 deletions shared/types/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package types

import "crypto"

type CubeType struct {
Value string
}

type CubeTypesStruct struct {
Queen CubeType
Security CubeType
Database CubeType
Api CubeType
CubusMod CubeType
DiscordBot CubeType
Web CubeType
Drone CubeType
GenericWorker CubeType
}

var CubeTypes = CubeTypesStruct{
Queen: CubeType{Value: "queen"},
Security: CubeType{Value: "security"},
Database: CubeType{Value: "database"},
Api: CubeType{Value: "api"},
CubusMod: CubeType{Value: "cubus-mod"},
DiscordBot: CubeType{Value: "discord-bot"},
Web: CubeType{Value: "web"},
Drone: CubeType{Value: "drone"},
GenericWorker: CubeType{Value: "generic-worker"},
}

type CubeConfig struct {
Id string
CubeType CubeType
PublicKey crypto.PublicKey
}

0 comments on commit 9227cf5

Please sign in to comment.