-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add form to gui translator (not fully working)
- Loading branch information
Showing
5 changed files
with
145 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |