Skip to content

Commit

Permalink
Merge pull request #36 from sdassow/fix/lint-errors
Browse files Browse the repository at this point in the history
  • Loading branch information
andydotxyz authored Jan 30, 2025
2 parents b42a036 + 9b1b3fd commit 287e6fd
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 25 deletions.
23 changes: 17 additions & 6 deletions internal/guibuilder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,15 @@ func (b *Builder) Run() {
}

w, err := storage.Writer(goURI)
if err != nil {
fyne.LogError("Failed get storage writer", err)
return
}
err = gui.ExportGoPreview(b.root, b.meta, w)
if err != nil {
fyne.LogError("Failed to export go preview", err)
return
}

pwd, _ := os.Getwd()
os.Chdir(path)
Expand Down Expand Up @@ -115,6 +123,9 @@ func (b *Builder) Save() error {
return err
}
err = gui.ExportGo(b.root, b.meta, name, w)
if err != nil {
return err
}

_ = w.Close()

Expand Down Expand Up @@ -201,13 +212,13 @@ func (b *Builder) buildLibrary() fyne.CanvasObject {
b.choose(c)
}
return
} else {
class := reflect.TypeOf(b.current).String()
if wid := guidefs.Lookup(class); wid != nil && wid.IsContainer() {
wid.AddChild(b.current, selected.Create())
}

return
}
class := reflect.TypeOf(b.current).String()
if wid := guidefs.Lookup(class); wid != nil && wid.IsContainer() {
wid.AddChild(b.current, selected.Create())

return
}

dialog.ShowInformation("Selected not a container", "Please select a container to add items", b.win)
Expand Down
2 changes: 1 addition & 1 deletion internal/guibuilder/overlay.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (o *overlay) Tapped(pe *fyne.PointEvent) {
obj := findObject(o.b.root, pos)

// TODO update when an item is removed, inserted, or if the UI resizes
o.indicator.StrokeColor = theme.PrimaryColor()
o.indicator.StrokeColor = theme.Color(theme.ColorNamePrimary)
objAbsPos := fyne.CurrentApp().Driver().AbsolutePositionForObject(obj)
objPos := objAbsPos.Subtract(rootPos)
o.indicator.Move(objPos)
Expand Down
1 change: 1 addition & 0 deletions internal/guidefs/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fyne.io/fyne/v2"
)

// GoString generates Go code for the given type and object
func GoString(clazz string, obj fyne.CanvasObject, props map[fyne.CanvasObject]map[string]string, defs map[string]string) string {
info := Lookup(clazz)
if info == nil {
Expand Down
1 change: 1 addition & 0 deletions internal/guidefs/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var (
// layoutNames is an array with the list of names of all the Layouts
layoutNames = extractLayoutNames()

// Layouts maps container names to layout information to create and edit containers, and generate code
Layouts = map[string]layoutInfo{
"Border": {
func(c *fyne.Container, props map[string]string) fyne.Layout {
Expand Down
2 changes: 2 additions & 0 deletions internal/guidefs/res.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ func (r *jsonResource) MarshalJSON() ([]byte, error) {
return []byte(icon), nil
}

// WrapResource wraps a fyne.Resource for integration with JSON
func WrapResource(r fyne.Resource) fyne.Resource {
return &jsonResource{r}
}

// IconName returns the name for an icon
func IconName(res fyne.Resource) string {
name := res.Name()
// strip prefix numbers to unwrap
Expand Down
39 changes: 26 additions & 13 deletions internal/guidefs/widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,24 @@ const noIconLabel = "(No Icon)"

var (
// WidgetNames is an array with the list of names of all the Widgets
WidgetNames, CollectionNames, ContainerNames []string

Widgets, Collections, Containers map[string]WidgetInfo
once sync.Once
WidgetNames []string
// CollectionNames is an array with collection type names
CollectionNames []string
// ContainerNames is an array of known container type names
ContainerNames []string

// Widgets maps names to widget information and corresponding functions
Widgets map[string]WidgetInfo
// Collections maps names to widget information and corresponding functions
Collections map[string]WidgetInfo
// Containers maps names to widget information and corresponding functions
Containers map[string]WidgetInfo
once sync.Once

importances = []string{"Medium", "High", "Low", "Danger", "Warning", "Success"}
)

// WidgetInfo contains the name and corresponding functions for the widget type
type WidgetInfo struct {
Name string
Children func(o fyne.CanvasObject) []fyne.CanvasObject
Expand All @@ -39,6 +49,7 @@ type WidgetInfo struct {
Packages func(object fyne.CanvasObject) []string
}

// IsContainer indicates wether a widget children or not
func (w WidgetInfo) IsContainer() bool {
return w.Children != nil
}
Expand Down Expand Up @@ -116,19 +127,19 @@ func initWidgets() {
if b.Icon == nil {
if b.Importance == widget.MediumImportance && b.Alignment == widget.ButtonAlignCenter {
return widgetRef(props[obj], defs, fmt.Sprintf("widget.NewButton(\"%s\", %s)", escapeLabel(b.Text), action))
} else {
return widgetRef(props[obj], defs, fmt.Sprintf("&widget.Button{Text: \"%s\", Importance: %d, Alignment: %d, OnTapped: %s}",
escapeLabel(b.Text), b.Importance, b.Alignment, action))
}

return widgetRef(props[obj], defs, fmt.Sprintf("&widget.Button{Text: \"%s\", Importance: %d, Alignment: %d, OnTapped: %s}",
escapeLabel(b.Text), b.Importance, b.Alignment, action))
}

icon := "theme." + IconName(b.Icon) + "()"
if b.Importance == widget.MediumImportance && b.Alignment == widget.ButtonAlignCenter {
return widgetRef(props[obj], defs, fmt.Sprintf("widget.NewButtonWithIcon(\"%s\", %s, %s)", escapeLabel(b.Text), icon, action))
} else {
return widgetRef(props[obj], defs, fmt.Sprintf("&widget.Button{Text: \"%s\", Importance: %d, Icon: %s, Alignment: %d, OnTapped: %s}",
escapeLabel(b.Text), b.Importance, icon, b.Alignment, action))
}

return widgetRef(props[obj], defs, fmt.Sprintf("&widget.Button{Text: \"%s\", Importance: %d, Icon: %s, Alignment: %d, OnTapped: %s}",
escapeLabel(b.Text), b.Importance, icon, b.Alignment, action))
},
Packages: func(obj fyne.CanvasObject) []string {
b := obj.(*widget.Button)
Expand Down Expand Up @@ -479,10 +490,10 @@ func initWidgets() {
if s.Selected == "" {
return widgetRef(props[obj], defs,
fmt.Sprintf("widget.NewSelect([]string{%s}, func(s string) {})", optionString))
} else {
format := "&widget.Select{Options: []string{%s}, Selected: \"%s\", OnChanged: func(s string) {}}"
return widgetRef(props[obj], defs, fmt.Sprintf(format, optionString, s.Selected))
}

format := "&widget.Select{Options: []string{%s}, Selected: \"%s\", OnChanged: func(s string) {}}"
return widgetRef(props[obj], defs, fmt.Sprintf(format, optionString, s.Selected))
},
},
"*layout.Spacer": {
Expand Down Expand Up @@ -896,6 +907,7 @@ func widgetRef(props map[string]string, defs map[string]string, code string) str
return code
}

// InitOnce initializes icons, graphics, containers, and widgets one time
func InitOnce() {
once.Do(func() {
initIcons()
Expand All @@ -905,6 +917,7 @@ func InitOnce() {
})
}

// Lookup returns the [WidgetInfo] for the given widget type
func Lookup(clazz string) *WidgetInfo {
if match, ok := Widgets[clazz]; ok {
return &match
Expand Down
4 changes: 4 additions & 0 deletions pkg/gui/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/fyne-io/defyne/internal/guidefs"
)

// CreateNew returns a new instance of the given widget type
func CreateNew(name string) fyne.CanvasObject {
guidefs.InitOnce()

Expand All @@ -18,6 +19,7 @@ func CreateNew(name string) fyne.CanvasObject {
return nil
}

// EditorFor returns an array of FormItem to edit the given widget
func EditorFor(o fyne.CanvasObject, props map[string]string, refresh func([]*widget.FormItem)) []*widget.FormItem {
guidefs.InitOnce()

Expand All @@ -30,6 +32,7 @@ func EditorFor(o fyne.CanvasObject, props map[string]string, refresh func([]*wid
return nil
}

// GoStringFor generates the Go code for the given widget
func GoStringFor(o fyne.CanvasObject, props map[fyne.CanvasObject]map[string]string, defs map[string]string) string {
guidefs.InitOnce()

Expand All @@ -49,6 +52,7 @@ func getTypeOf(o fyne.CanvasObject) (string, string) {
return name, class
}

// NameOf returns the name for a given object
func NameOf(o fyne.CanvasObject) string {
typeName := reflect.TypeOf(o).Elem().Name()
l := reflect.ValueOf(o).Elem()
Expand Down
2 changes: 2 additions & 0 deletions pkg/gui/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"fyne.io/fyne/v2"
)

// ExportGo generates a full Go package for the given object and writes it to the provided file handle
func ExportGo(obj fyne.CanvasObject, meta map[fyne.CanvasObject]map[string]string, name string, w io.Writer) error {
guidefs.InitOnce()

Expand All @@ -23,6 +24,7 @@ func ExportGo(obj fyne.CanvasObject, meta map[fyne.CanvasObject]map[string]strin
return err
}

// ExportGoPreview generates a preview version of the Go code with a `main()` method for the given object and writes it to the file handle
func ExportGoPreview(obj fyne.CanvasObject, meta map[fyne.CanvasObject]map[string]string, w io.Writer) error {
guidefs.InitOnce()

Expand Down
6 changes: 1 addition & 5 deletions pkg/gui/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (
"fyne.io/fyne/v2/widget"
)

const jsonKeyObject = "Object"

type canvObj struct {
Type string
Name string `json:",omitempty"`
Expand Down Expand Up @@ -131,9 +129,7 @@ func DecodeMap(m map[string]interface{}, meta map[fyne.CanvasObject]map[string]s
}
}
item.Content, _ = DecodeMap(data["Content"].(map[string]interface{}), meta)
if item != nil {
obj.Append(item)
}
obj.Append(item)
}
}

Expand Down
1 change: 1 addition & 0 deletions pkg/gui/widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func WidgetClassList() []string {
return guidefs.WidgetNames
}

// DropZonesForObject returns the children of a container that can be used as drag and drop target zones
func DropZonesForObject(o fyne.CanvasObject) []fyne.CanvasObject {
class := reflect.TypeOf(o).String()
info := guidefs.Lookup(class)
Expand Down

0 comments on commit 287e6fd

Please sign in to comment.