918 lines
28 KiB
Go
918 lines
28 KiB
Go
// +build gui
|
|
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"image/color"
|
|
"net/url"
|
|
"reflect"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"fyne.io/fyne/v2"
|
|
"fyne.io/fyne/v2/app"
|
|
"fyne.io/fyne/v2/container"
|
|
"fyne.io/fyne/v2/dialog"
|
|
"fyne.io/fyne/v2/layout"
|
|
"fyne.io/fyne/v2/theme"
|
|
"fyne.io/fyne/v2/widget"
|
|
)
|
|
|
|
type GUI struct {
|
|
gui fyne.App
|
|
windows map[string]fyne.Window
|
|
}
|
|
|
|
func (g GUI) String() string {
|
|
return "GUI-Root"
|
|
}
|
|
|
|
type guiWidget fyne.Widget
|
|
|
|
type guiContainer fyne.CanvasObject
|
|
|
|
type guiObject interface{}
|
|
|
|
type slopeTheme struct{}
|
|
|
|
func (t slopeTheme) String() string {
|
|
return "GUI-Theme"
|
|
}
|
|
|
|
// Dev note: no memory of why this is
|
|
// done this way.
|
|
var _ fyne.Theme = (*slopeTheme)(nil)
|
|
|
|
func (m slopeTheme) Color(name fyne.ThemeColorName, variant fyne.ThemeVariant) color.Color {
|
|
if name == theme.ColorNameBackground {
|
|
if variant == theme.VariantLight {
|
|
return color.RGBA{200, 200, 200, 255}
|
|
}
|
|
return color.RGBA{60, 60, 60, 255}
|
|
}
|
|
|
|
return theme.DefaultTheme().Color(name, variant)
|
|
}
|
|
|
|
func (m slopeTheme) Icon(name fyne.ThemeIconName) fyne.Resource {
|
|
return theme.DefaultTheme().Icon(name)
|
|
}
|
|
|
|
func (m slopeTheme) Font(style fyne.TextStyle) fyne.Resource {
|
|
return theme.DefaultTheme().Font(style)
|
|
}
|
|
|
|
func (m slopeTheme) Size(name fyne.ThemeSizeName) float32 {
|
|
return theme.DefaultTheme().Size(name) * 0.9
|
|
}
|
|
|
|
var guiLib = vars{
|
|
"gui-create": func(a ...expression) expression {
|
|
app := &GUI{app.New(), make(map[string]fyne.Window)}
|
|
app.gui.Settings().SetTheme(&slopeTheme{})
|
|
return app
|
|
},
|
|
"gui-add-window": func(a ...expression) expression {
|
|
if len(a) < 2 {
|
|
return exception("'gui-add-window' expects a GUI and a string as arguments, too few arguments were given")
|
|
}
|
|
g, ok := a[0].(*GUI)
|
|
if !ok {
|
|
return exception("'gui-add-window' was given a non-GUI value as its first argument")
|
|
}
|
|
s := String(a[1], false)
|
|
g.windows[s] = g.gui.NewWindow(s)
|
|
return true
|
|
},
|
|
"gui-list-windows": func(a ...expression) expression {
|
|
if len(a) == 0 {
|
|
return exception("'gui-list-windows' expects a GUI as an argument, no arguments were given")
|
|
}
|
|
g, ok := a[0].(*GUI)
|
|
if !ok {
|
|
return exception("'gui-list-windows' was given a non-GUI argument")
|
|
}
|
|
out := make([]expression, 0, len(g.windows))
|
|
for k := range g.windows {
|
|
out = append(out, k)
|
|
}
|
|
return out
|
|
},
|
|
"gui-use-light-theme": func(a ...expression) expression {
|
|
if len(a) == 0 {
|
|
return exception("'gui-use-light-theme' expects a gui-root as its first argument and, optionally, a bool as its second argument, no arguments were given")
|
|
}
|
|
|
|
app, ok := a[0].(*GUI)
|
|
if !ok {
|
|
return exception("'gui-use-light-theme' expects a gui-root as its first argument, a non-gui-root value was given")
|
|
}
|
|
|
|
// If the current theme is == to the light theme return true, else false
|
|
if len(a) == 1 {
|
|
return reflect.DeepEqual(app.gui.Settings().Theme(), theme.LightTheme())
|
|
}
|
|
|
|
b := AnythingToBool(a[1]).(bool)
|
|
if b {
|
|
app.gui.Settings().SetTheme(theme.LightTheme())
|
|
} else {
|
|
app.gui.Settings().SetTheme(theme.DarkTheme())
|
|
}
|
|
return b
|
|
},
|
|
"window-set-content": func(a ...expression) expression {
|
|
// (window-set-content window-id content...)
|
|
if len(a) < 3 {
|
|
return exception("'window-set-content' expects a gui-root and a string as arguments, too few arguments were given")
|
|
}
|
|
g, ok := a[0].(*GUI)
|
|
if !ok {
|
|
return exception("'window-set-content' was given a non-gui-root value as its first argument")
|
|
}
|
|
s := String(a[1], false)
|
|
w, ok := g.windows[s]
|
|
if !ok {
|
|
return exception("'window-set-content' was given a window-id that does not exist: " + s)
|
|
}
|
|
content, ok := a[2].(fyne.CanvasObject)
|
|
if !ok {
|
|
return exception("'window-set-content' was given invalid content")
|
|
}
|
|
w.SetContent(content)
|
|
return true
|
|
},
|
|
"window-resize": func(a ...expression) expression {
|
|
if len(a) < 4 {
|
|
return exception("'window-resize' expects a GUI, a string, and two numbers as arguments; too few arguments were given")
|
|
}
|
|
g, ok := a[0].(*GUI)
|
|
if !ok {
|
|
return exception("'window-resize' was given a non-GUI value as its first argument")
|
|
}
|
|
s := String(a[1], false)
|
|
w, ok := g.windows[s]
|
|
if !ok {
|
|
return exception("'window-resize' was given a window-id that does not exist: " + s)
|
|
}
|
|
width, ok := a[2].(number)
|
|
height, ok2 := a[3].(number)
|
|
if !ok || !ok2 {
|
|
return exception("'window-resize' was given a non-number value for height or width")
|
|
}
|
|
w.Resize(fyne.NewSize(float32(width), float32(height)))
|
|
return true
|
|
},
|
|
"window-show": func(a ...expression) expression {
|
|
if len(a) < 2 {
|
|
return exception("'window-show' expects a GUI and a string; too few arguments were given")
|
|
}
|
|
g, ok := a[0].(*GUI)
|
|
if !ok {
|
|
return exception("'window-show' was given a non-GUI value as its first argument")
|
|
}
|
|
s := String(a[1], false)
|
|
w, ok := g.windows[s]
|
|
if !ok {
|
|
return exception("'window-show' was given a window-id that does not exist: " + s)
|
|
}
|
|
w.Show()
|
|
return true
|
|
},
|
|
"window-hide": func(a ...expression) expression {
|
|
if len(a) < 2 {
|
|
return exception("'window-hide' expects a GUI and a string; too few arguments were given")
|
|
}
|
|
g, ok := a[0].(*GUI)
|
|
if !ok {
|
|
return exception("'window-hide' was given a non-GUI value as its first argument")
|
|
}
|
|
s := String(a[1], false)
|
|
w, ok := g.windows[s]
|
|
if !ok {
|
|
return exception("'window-hide' was given a window-id that does not exist: " + s)
|
|
}
|
|
w.Hide()
|
|
return true
|
|
},
|
|
"window-close": func(a ...expression) expression {
|
|
if len(a) < 2 {
|
|
return exception("'window-close' expects a GUI and a string; too few arguments were given")
|
|
}
|
|
g, ok := a[0].(*GUI)
|
|
if !ok {
|
|
return exception("'window-close' was given a non-GUI value as its first argument")
|
|
}
|
|
s := String(a[1], false)
|
|
w, ok := g.windows[s]
|
|
if !ok {
|
|
return exception("'window-close' was given a window-id that does not exist: " + s)
|
|
}
|
|
w.Close()
|
|
return true
|
|
},
|
|
"window-set-title": func(a ...expression) expression {
|
|
if len(a) < 3 {
|
|
return exception("'window-set-title' expects a GUI and two strings; too few arguments were given")
|
|
}
|
|
g, ok := a[0].(*GUI)
|
|
if !ok {
|
|
return exception("'window-set-title' was given a non-GUI value as its first argument")
|
|
}
|
|
s := String(a[1], false)
|
|
w, ok := g.windows[s]
|
|
if !ok {
|
|
return exception("'window-set-title' was given a window-id that does not exist: " + s)
|
|
}
|
|
w.SetTitle(String(a[2], false))
|
|
return true
|
|
},
|
|
"window-set-fullscreen": func(a ...expression) expression {
|
|
if len(a) < 3 {
|
|
return exception("'window-set-fullscreen' expects a GUI, a string, and a bool; too few arguments were given")
|
|
}
|
|
g, ok := a[0].(*GUI)
|
|
if !ok {
|
|
return exception("'window-set-fullscreen' was given a non-GUI value as its first argument")
|
|
}
|
|
s := String(a[1], false)
|
|
w, ok := g.windows[s]
|
|
if !ok {
|
|
return exception("'window-set-fullscreen' was given a window-id that does not exist: " + s)
|
|
}
|
|
w.SetFullScreen(AnythingToBool(a[2]).(bool))
|
|
return true
|
|
},
|
|
"window-show-and-run": func(a ...expression) expression {
|
|
// (window-show-and-run window-id)
|
|
if len(a) < 2 {
|
|
return exception("'window-show-and-run' expects a GUI and a string as arguments, too few arguments were given")
|
|
}
|
|
g, ok := a[0].(*GUI)
|
|
if !ok {
|
|
return exception("'window-show-and-run' was given a non-GUI value as its first argument")
|
|
}
|
|
s := String(a[1], false)
|
|
w, ok := g.windows[s]
|
|
if !ok {
|
|
return exception("'window-show-and-run' was given a window-id that does not exist: " + s)
|
|
}
|
|
w.ShowAndRun()
|
|
return true
|
|
},
|
|
"window-center": func(a ...expression) expression {
|
|
if len(a) < 2 {
|
|
return exception("'window-center' expects a GUI and a string as arguments, too few arguments were given")
|
|
}
|
|
g, ok := a[0].(*GUI)
|
|
if !ok {
|
|
return exception("'window-center' was given a non-GUI value as its first argument")
|
|
}
|
|
s := String(a[1], false)
|
|
w, ok := g.windows[s]
|
|
if !ok {
|
|
return exception("'window-center' was given a window-id that does not exist: " + s)
|
|
}
|
|
w.CenterOnScreen()
|
|
return true
|
|
},
|
|
"window-allow-resize": func(a ...expression) expression {
|
|
if len(a) < 3 {
|
|
return exception("'window-allow-resize' expects a GUI, a string, and a bool; too few arguments were given")
|
|
}
|
|
g, ok := a[0].(*GUI)
|
|
if !ok {
|
|
return exception("'window-allow-resize' was given a non-GUI value as its first argument")
|
|
}
|
|
s := String(a[1], false)
|
|
w, ok := g.windows[s]
|
|
if !ok {
|
|
return exception("'window-allow-resize' was given a window-id that does not exist: " + s)
|
|
}
|
|
w.SetFixedSize(!AnythingToBool(a[2]).(bool))
|
|
return true
|
|
},
|
|
"widget-show": func(a ...expression) expression {
|
|
if len(a) == 0 {
|
|
return exception("'widget-show' expects a widget, no value was given")
|
|
}
|
|
wid, ok := a[0].(guiWidget)
|
|
if !ok {
|
|
return exception("'widget-show' expects a widget, a non-widget value was given")
|
|
}
|
|
wid.Show()
|
|
return true
|
|
},
|
|
"widget-hide": func(a ...expression) expression {
|
|
if len(a) == 0 {
|
|
return exception("'widget-hide' expects a widget, no value was given")
|
|
}
|
|
wid, ok := a[0].(guiWidget)
|
|
if !ok {
|
|
return exception("'widget-hide' expects a widget, a non-widget value was given")
|
|
}
|
|
wid.Hide()
|
|
return true
|
|
},
|
|
"widget-resize": func(a ...expression) expression {
|
|
if len(a) < 3 {
|
|
return exception("'widget-rsize' expects a widget and two numbers, insufficient values were given")
|
|
}
|
|
wid, ok := a[0].(guiWidget)
|
|
if !ok {
|
|
return exception("'widget-resize' expects a widget, a non-widget value was given")
|
|
}
|
|
width, ok := a[1].(number)
|
|
if !ok {
|
|
return exception("'widget-resize' expects a width number, a non-number value was given")
|
|
}
|
|
height, ok := a[2].(number)
|
|
if !ok {
|
|
return exception("'widget-resize' expects a height number, a non-number value was given")
|
|
}
|
|
wid.Resize(fyne.NewSize(float32(width), float32(height)))
|
|
return guiWidget(wid)
|
|
},
|
|
"widget-size": func(a ...expression) expression {
|
|
if len(a) == 0 {
|
|
return exception("'widget-sze' expects a widget, no value was given")
|
|
}
|
|
wid, ok := a[0].(guiWidget)
|
|
if !ok {
|
|
return exception("'widget-size' expects a widget, a non-widget value was given")
|
|
}
|
|
s := wid.Size()
|
|
return []expression{number(s.Width), number(s.Height)}
|
|
},
|
|
"widget-add-to-size": func(a ...expression) expression {
|
|
if len(a) < 3 {
|
|
return exception("'widget-add-to-size' expects a widget and two numbers, insufficient values were given")
|
|
}
|
|
wid, ok := a[0].(guiWidget)
|
|
if !ok {
|
|
return exception("'widget-add-to-size' expects a widget, a non-widget value was given")
|
|
}
|
|
width, ok := a[1].(number)
|
|
if !ok {
|
|
return exception("'widget-add-to-size' expects a width number, a non-number value was given")
|
|
}
|
|
height, ok := a[2].(number)
|
|
if !ok {
|
|
return exception("'widget-add-to-size' expects a height number, a non-number value was given")
|
|
}
|
|
s := wid.Size()
|
|
wid.Resize(s.Add(fyne.NewSize(float32(width), float32(height))))
|
|
return guiWidget(wid)
|
|
},
|
|
"widget-make-entry": func(a ...expression) expression {
|
|
if len(a) == 0 {
|
|
return exception("'widget-make-entry' expects at least one argument, a string representing the text of the label, no arguments were given")
|
|
}
|
|
var e guiWidget = widget.NewEntry()
|
|
|
|
// Set placeholder
|
|
if len(a) > 0 {
|
|
e.(*widget.Entry).SetPlaceHolder(String(a[0], false))
|
|
}
|
|
|
|
// Set wrapping
|
|
if len(a) > 1 && AnythingToBool(a[1]).(bool) {
|
|
e.(*widget.Entry).Wrapping = fyne.TextWrapWord
|
|
} else {
|
|
e.(*widget.Entry).Wrapping = fyne.TextTruncate
|
|
}
|
|
|
|
// Handle validation
|
|
if len(a) > 2 {
|
|
cb, ok := a[2].(proc)
|
|
if !ok {
|
|
return exception("'widget-make-entry' expects a lambdaas its optional third argument, a non-lambda value was given")
|
|
}
|
|
e.(*widget.Entry).Validator = func(in string) error {
|
|
result := apply(cb, []expression{in})
|
|
switch t := result.(type) {
|
|
case string:
|
|
return fmt.Errorf(t)
|
|
default:
|
|
return nil
|
|
}
|
|
}
|
|
}
|
|
return e
|
|
},
|
|
"widget-make-password": func(a ...expression) expression {
|
|
if len(a) == 0 {
|
|
return exception("'widget-make-password' expects at least one argument, a string representing the text of the label, no arguments were given")
|
|
}
|
|
var e guiWidget = widget.NewPasswordEntry()
|
|
|
|
// Set placeholder
|
|
if len(a) > 0 {
|
|
e.(*widget.Entry).SetPlaceHolder(String(a[0], false))
|
|
}
|
|
|
|
// Set wrapping
|
|
if len(a) > 1 && AnythingToBool(a[1]).(bool) {
|
|
e.(*widget.Entry).Wrapping = fyne.TextWrapWord
|
|
} else {
|
|
e.(*widget.Entry).Wrapping = fyne.TextTruncate
|
|
}
|
|
|
|
// Handle validation
|
|
if len(a) > 2 {
|
|
cb, ok := a[2].(proc)
|
|
if !ok {
|
|
return exception("'widget-make-entry' expects a callback as its optional third argument, a non-callback value was given")
|
|
}
|
|
e.(*widget.Entry).Validator = func(in string) error {
|
|
result := apply(cb, []expression{in})
|
|
switch t := result.(type) {
|
|
case string:
|
|
return fmt.Errorf(t)
|
|
default:
|
|
return nil
|
|
}
|
|
}
|
|
}
|
|
return e
|
|
},
|
|
"widget-make-multiline": func(a ...expression) expression {
|
|
if len(a) == 0 {
|
|
return exception("'widget-make-multiline' expects at least one argument, a string representing the text of the label, no arguments were given")
|
|
}
|
|
var e guiWidget = widget.NewMultiLineEntry()
|
|
|
|
// Set placeholder
|
|
if len(a) > 0 {
|
|
e.(*widget.Entry).SetPlaceHolder(String(a[0], false))
|
|
}
|
|
|
|
// Set wrapping
|
|
if len(a) > 1 && AnythingToBool(a[1]).(bool) {
|
|
e.(*widget.Entry).Wrapping = fyne.TextWrapWord
|
|
} else {
|
|
e.(*widget.Entry).Wrapping = fyne.TextTruncate
|
|
}
|
|
|
|
// Handle validation
|
|
if len(a) > 2 {
|
|
cb, ok := a[2].(proc)
|
|
if !ok {
|
|
return exception("'widget-make-entry' expects a callback as its optional third argument, a non-callback value was given")
|
|
}
|
|
e.(*widget.Entry).Validator = func(in string) error {
|
|
result := apply(cb, []expression{in})
|
|
switch t := result.(type) {
|
|
case string:
|
|
return fmt.Errorf(t)
|
|
default:
|
|
return nil
|
|
}
|
|
}
|
|
}
|
|
return e
|
|
},
|
|
"widget-make-label": func(a ...expression) expression {
|
|
// (widget-label text:string alignment:number wrap:bool)
|
|
if len(a) == 0 {
|
|
return exception("'widget-make-label' expects at least one argument, a string representing the text of the label, no arguments were given")
|
|
}
|
|
var l guiWidget = widget.NewLabel(String(a[0], false))
|
|
if len(a) >= 2 {
|
|
align, ok := a[1].(number)
|
|
if !ok {
|
|
return exception("'widget-make-label' expects a number (-1, 0, or 1) as its optional second argument (alignment), a non-number value was given")
|
|
}
|
|
switch int(align) {
|
|
case -1:
|
|
l.(*widget.Label).Alignment = fyne.TextAlignLeading
|
|
case 0:
|
|
l.(*widget.Label).Alignment = fyne.TextAlignCenter
|
|
case 1:
|
|
l.(*widget.Label).Alignment = fyne.TextAlignTrailing
|
|
default:
|
|
return exception("'widget-make-label' expects a number (-1, 0, or 1) as its optional second argument (alignment), a number outside of the range was given")
|
|
}
|
|
}
|
|
if len(a) >= 3 {
|
|
wrap := AnythingToBool(a[0]).(bool)
|
|
if wrap {
|
|
l.(*widget.Label).Wrapping = fyne.TextWrapWord
|
|
}
|
|
}
|
|
return l
|
|
},
|
|
"widget-make-select": func(a ...expression) expression {
|
|
if len(a) < 2 {
|
|
return exception("'widget-make-select' expects a list of options as strings, a callback (lambda), and an optional alignment number too few arguments were given")
|
|
}
|
|
elist, ok := a[0].([]expression)
|
|
if !ok {
|
|
return exception("'widget-make-select' expected a list as its first argument, but was given a non-list value")
|
|
}
|
|
|
|
list := ExpressionSliceToStringSlice(elist)
|
|
|
|
var b guiWidget
|
|
switch f := a[1].(type) {
|
|
case func(...expression) expression:
|
|
b = widget.NewSelect(list, func(in string) { f(in) })
|
|
case proc:
|
|
b = widget.NewSelect(list, func(in string) { apply(f, []expression{in}) })
|
|
default:
|
|
return exception("'widget-make-select' was given a non-procedure value as its second argument")
|
|
}
|
|
if len(a) >= 3 {
|
|
align, ok := a[2].(number)
|
|
if !ok {
|
|
return exception("'widget-make-button' expects a number (-1, 0, or 1) as its optional third argument (alignment), a non-number value was given")
|
|
}
|
|
switch int(align) {
|
|
case -1:
|
|
b.(*widget.Select).Alignment = fyne.TextAlignLeading
|
|
case 0:
|
|
b.(*widget.Select).Alignment = fyne.TextAlignCenter
|
|
case 1:
|
|
b.(*widget.Select).Alignment = fyne.TextAlignTrailing
|
|
default:
|
|
return exception("'widget-make-select' expects a number (-1, 0, or 1) as its optional third argument (alignment), a number outside of the range was given")
|
|
}
|
|
}
|
|
return b
|
|
|
|
},
|
|
"widget-make-markdown": func(a ...expression) expression {
|
|
if len(a) == 0 {
|
|
return exception("'widget-make-markdown' expects a string but was given no arguments")
|
|
}
|
|
var b guiWidget
|
|
b = widget.NewRichTextFromMarkdown(String(a[0], false))
|
|
|
|
if len(a) >= 2 {
|
|
wrap := AnythingToBool(a[1]).(bool)
|
|
if wrap {
|
|
b.(*widget.RichText).Wrapping = fyne.TextWrapWord
|
|
}
|
|
}
|
|
return b
|
|
},
|
|
"widget-make-checkbox": func(a ...expression) expression {
|
|
if len(a) < 2 {
|
|
return exception("'widget-make-checkbox' expects a text string and a callback (lambda), too few arguments were given")
|
|
}
|
|
var b guiWidget
|
|
switch f := a[1].(type) {
|
|
case func(...expression) expression:
|
|
b = widget.NewCheck(String(a[0], false), func(in bool) { f(in) })
|
|
case proc:
|
|
b = widget.NewCheck(String(a[0], false), func(in bool) { apply(f, []expression{in}) })
|
|
default:
|
|
return exception("'widget-make-checkbox' was given a non-procedure value as its second argument")
|
|
}
|
|
return b
|
|
},
|
|
"widget-make-hyperlink": func(a ...expression) expression {
|
|
if len(a) < 2 {
|
|
return exception("'widget-make-hyperlink' expects a text string and a URL string, too few arguments were given")
|
|
}
|
|
u, err := url.Parse(String(a[1], false))
|
|
if err != nil {
|
|
return exception("'widget-make-hyperlink' expects a valid URL as its second argument, could not parse URL from the given string")
|
|
}
|
|
return guiWidget(widget.NewHyperlink(String(a[0], false), u))
|
|
},
|
|
"widget-make-button": func(a ...expression) expression {
|
|
if len(a) < 2 {
|
|
return exception("'widget-make-button' expects at least two argument, a string representing the text of the label and a lambda, too few arguments were given")
|
|
}
|
|
var b guiWidget
|
|
switch f := a[1].(type) {
|
|
case func(...expression) expression:
|
|
b = widget.NewButton(String(a[0], false), func() { f() })
|
|
case proc:
|
|
b = widget.NewButton(String(a[0], false), func() { apply(f, []expression{}) })
|
|
default:
|
|
return exception("'widget-make-button' was given a non-procedure value as its second argument")
|
|
}
|
|
if len(a) >= 3 {
|
|
align, ok := a[2].(number)
|
|
if !ok {
|
|
return exception("'widget-make-button' expects a number (-1, 0, or 1) as its optional third argument (alignment), a non-number value was given")
|
|
}
|
|
switch int(align) {
|
|
case -1:
|
|
b.(*widget.Button).Alignment = widget.ButtonAlignLeading
|
|
case 0:
|
|
b.(*widget.Button).Alignment = widget.ButtonAlignCenter
|
|
case 1:
|
|
b.(*widget.Button).Alignment = widget.ButtonAlignTrailing
|
|
default:
|
|
return exception("'widget-make-button' expects a number (-1, 0, or 1) as its optional third argument (alignment), a number outside of the range was given")
|
|
}
|
|
}
|
|
return b
|
|
},
|
|
"widget-get-text": func(a ...expression) expression {
|
|
if len(a) < 1 {
|
|
return exception("'widget-get-text' expects a widget argument, no arguments were given")
|
|
}
|
|
w, ok := a[0].(guiWidget)
|
|
if !ok {
|
|
return exception("'widget-get-text' expects a widget as its first argument, a non-widget value was given")
|
|
}
|
|
switch wid := w.(fyne.Widget).(type) {
|
|
case *widget.Label:
|
|
return wid.Text
|
|
case *widget.Button:
|
|
return wid.Text
|
|
case *widget.Entry:
|
|
return wid.Text
|
|
case *widget.Hyperlink:
|
|
return wid.Text
|
|
default:
|
|
return exception("'widget-get-text' received a widget that it does not support")
|
|
}
|
|
},
|
|
"widget-set-text": func(a ...expression) expression {
|
|
if len(a) < 2 {
|
|
return exception("'widget-set-text' expects two arguments, a widget and a string representing the text of the label, no arguments were given")
|
|
}
|
|
w, ok := a[0].(guiWidget)
|
|
if !ok {
|
|
return exception("'widget-set-text' expects a widget as its first argument, a non-widget value was given")
|
|
}
|
|
txt := String(a[1], false)
|
|
switch wid := w.(fyne.Widget).(type) {
|
|
case *widget.Label:
|
|
wid.SetText(txt)
|
|
case *widget.Button:
|
|
wid.SetText(txt)
|
|
case *widget.Entry:
|
|
wid.SetText(txt)
|
|
case *widget.Hyperlink:
|
|
wid.SetText(txt)
|
|
default:
|
|
return exception("'widget-set-text' expected a valid widget")
|
|
}
|
|
return txt
|
|
},
|
|
"widget-make-separator": func(a ...expression) expression {
|
|
return guiWidget(widget.NewSeparator())
|
|
},
|
|
"widget-make-spacer": func(a ...expression) expression {
|
|
var spacer guiObject = layout.NewSpacer()
|
|
return spacer
|
|
},
|
|
"container-scroll": func(a ...expression) expression {
|
|
if len(a) < 1 {
|
|
return exception("'container-scroll' expects at least one widget or layout, no values were given")
|
|
}
|
|
list, err := ExpToCanvasObjectList(a)
|
|
if err != nil || len(list) < 1 {
|
|
return exception("'container-scroll' was given a non-widget value")
|
|
}
|
|
return guiContainer(container.NewScroll(list[0]))
|
|
},
|
|
"container": func(a ...expression) expression {
|
|
if len(a) < 2 {
|
|
return exception("'container' expects at least two arguments, a string representing the layout type and at least one widget or layout (or in some layout types an additional value), no values were given")
|
|
}
|
|
containerType := String(a[0], false)
|
|
widgetStart := 1
|
|
cols := 0
|
|
if containerType == "grid" {
|
|
if len(a) < 3 {
|
|
return exception("'container' with layout 'grid' expects at least three arguments, a string representing the layout type, a number representing the column count, and at least one widget or layout, no values were given")
|
|
}
|
|
if count, ok := a[1].(number); ok {
|
|
widgetStart = 2
|
|
cols = int(float64(count))
|
|
}
|
|
}
|
|
list, err := ExpToCanvasObjectList(a[widgetStart:])
|
|
if err != nil {
|
|
return exception("'container' was given a non-widget value")
|
|
}
|
|
switch containerType {
|
|
case "none":
|
|
return guiContainer(container.NewWithoutLayout(list...))
|
|
case "hbox":
|
|
return guiContainer(container.NewHBox(list...))
|
|
case "vbox":
|
|
return guiContainer(container.NewVBox(list...))
|
|
case "form":
|
|
return guiContainer(container.New(layout.NewFormLayout(), list...))
|
|
case "center":
|
|
return guiContainer(container.NewCenter(list...))
|
|
case "max":
|
|
return guiContainer(container.NewMax(list...))
|
|
case "padded":
|
|
return guiContainer(container.NewPadded(list...))
|
|
case "grid":
|
|
return guiContainer(container.NewGridWithColumns(cols, list...))
|
|
default:
|
|
return exception("'container-with-layout' was given an invalid layout type")
|
|
}
|
|
},
|
|
"container-size": func(a ...expression) expression {
|
|
if len(a) < 1 {
|
|
return exception("'container-size' expects a container, no value was given")
|
|
}
|
|
c, ok := a[0].(guiContainer)
|
|
if !ok {
|
|
return exception("'container-size' expects a container, a non-container value was given")
|
|
}
|
|
s := c.Size()
|
|
return []expression{number(s.Width), number(s.Height)}
|
|
},
|
|
"container-add-to": func(a ...expression) expression {
|
|
if len(a) < 2 {
|
|
return exception("'container-add-to' expects a container and either a widget or another container, insufficient values were given")
|
|
}
|
|
c, ok := a[0].(guiContainer)
|
|
if !ok {
|
|
return exception("'container-add-to' expects a container, a non-container value was given")
|
|
}
|
|
switch obj := a[1].(type) {
|
|
case guiContainer:
|
|
c.(*fyne.Container).Add(obj)
|
|
case guiWidget:
|
|
c.(*fyne.Container).Add(obj)
|
|
default:
|
|
return exception("'container-add-to' expects a container or a widget as its second argument, a non-widget/non-container value was given")
|
|
}
|
|
return true
|
|
},
|
|
"dialog-open-file": func(a ...expression) expression {
|
|
if len(a) < 3 {
|
|
return exception("'dialog-open-file' expected three arguments: a GUI, a string representing a gui-window-id and a callback that takes a string as an argument; an insufficient number of arguments was given")
|
|
}
|
|
app, ok := a[0].(*GUI)
|
|
if !ok {
|
|
return exception("'dialog-open-file' expected a GUI as its first argument, a non-GUI value was given")
|
|
}
|
|
window := String(a[1], false)
|
|
if _, ok := app.windows[window]; !ok {
|
|
return exception("'dialog-open-file' was given an invalid window-id: " + window)
|
|
}
|
|
cb, ok := a[2].(proc)
|
|
if !ok {
|
|
return exception("'dialog-open-file' expected a callback as its second argument, a non-callback value was given")
|
|
}
|
|
fd := dialog.NewFileOpen(func(reader fyne.URIReadCloser, err error) {
|
|
if err != nil || reader == nil {
|
|
return
|
|
}
|
|
defer reader.Close()
|
|
u := reader.URI().Path()
|
|
apply(cb, []expression{u})
|
|
return
|
|
}, app.windows[window])
|
|
fd.Show()
|
|
return []expression{}
|
|
},
|
|
"dialog-save-file": func(a ...expression) expression {
|
|
if len(a) < 3 {
|
|
return exception("'dialog-save-file' expected three arguments: a GUI, a string representing a gui-window-id and a callback that takes a string as an argument; an insufficient number of arguments was given")
|
|
}
|
|
app, ok := a[0].(*GUI)
|
|
if !ok {
|
|
return exception("'dialog-save-file' expected a GUI as its first argument, a non-GUI value was given")
|
|
}
|
|
window := String(a[1], false)
|
|
if _, ok := app.windows[window]; !ok {
|
|
return exception("'dialog-save-file' was given an invalid window-id: " + window)
|
|
}
|
|
cb, ok := a[2].(proc)
|
|
if !ok {
|
|
return exception("'dialog-save-file' expected a callback as its second argument, a non-callback value was given")
|
|
}
|
|
fd := dialog.NewFileSave(func(writer fyne.URIWriteCloser, err error) {
|
|
if err != nil || writer == nil {
|
|
return
|
|
}
|
|
defer writer.Close()
|
|
u := writer.URI().Path()
|
|
apply(cb, []expression{u})
|
|
return
|
|
}, app.windows[window])
|
|
fd.Show()
|
|
return []expression{}
|
|
},
|
|
"dialog-info": func(a ...expression) expression {
|
|
if len(a) < 4 {
|
|
return exception("'dialog-info' expected four arguments: a GUI, a string representing a window-name, a title string, and a message string; an insufficient number of arguments was given")
|
|
}
|
|
app, ok := a[0].(*GUI)
|
|
if !ok {
|
|
return exception("'dialog-info' expected a GUI as its first argument, a non-GUI value was given")
|
|
}
|
|
window := String(a[1], false)
|
|
if _, ok := app.windows[window]; !ok {
|
|
return exception("'dialog-info' was given an invalid window-id: " + window)
|
|
}
|
|
dialog.ShowInformation(String(a[2], false), String(a[3], false), app.windows[window])
|
|
return []expression{}
|
|
},
|
|
"dialog-error": func(a ...expression) expression {
|
|
if len(a) < 3 {
|
|
return exception("'dialog-error' expected three arguments: a GUI, a string representing a gui-window-id, an error string; an insufficient number of arguments was given")
|
|
}
|
|
app, ok := a[0].(*GUI)
|
|
if !ok {
|
|
return exception("'dialog-error' expected a GUI as its first argument, a non-GUI value was given")
|
|
}
|
|
window := String(a[1], false)
|
|
if _, ok := app.windows[window]; !ok {
|
|
return exception("'dialog-error' was given an invalid window-id: " + window)
|
|
}
|
|
dialog.ShowError(fmt.Errorf(String(a[2], false)), app.windows[window])
|
|
return []expression{}
|
|
},
|
|
}
|
|
|
|
// - BEGIN helpers
|
|
|
|
func ExpToCanvasObjectList(e []expression) ([]fyne.CanvasObject, error) {
|
|
out := make([]fyne.CanvasObject, len(e))
|
|
for i, v := range e {
|
|
switch obj := v.(type) {
|
|
case guiWidget:
|
|
out[i] = obj.(fyne.Widget).(fyne.CanvasObject)
|
|
case fyne.CanvasObject:
|
|
out[i] = obj
|
|
case *fyne.Container:
|
|
out[i] = obj
|
|
case guiObject:
|
|
out[i] = obj.(fyne.CanvasObject)
|
|
case guiContainer:
|
|
out[i] = obj.(fyne.CanvasObject)
|
|
default:
|
|
return out, fmt.Errorf("Invalid list")
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// String converts any value to a string
|
|
// if rawString is true escapes will be
|
|
// shown visually and quotes will be around
|
|
// a string. rawString has no effect on
|
|
// values that are not already strings
|
|
func String(v expression, rawString bool) string {
|
|
switch v := v.(type) {
|
|
case []expression:
|
|
l := make([]string, len(v))
|
|
for i, x := range v {
|
|
l[i] = String(x, true)
|
|
}
|
|
return "(" + strings.Join(l, " ") + ")"
|
|
case string:
|
|
if rawString {
|
|
return fmt.Sprintf("\"%s\"", escapeString(v))
|
|
}
|
|
return v
|
|
case exception:
|
|
return string(v)
|
|
case bool:
|
|
if v {
|
|
return "#t"
|
|
}
|
|
return "#f"
|
|
case number:
|
|
return strconv.FormatFloat(float64(v), 'f', -1, 64)
|
|
case proc:
|
|
var b strings.Builder
|
|
b.WriteString("(lambda ")
|
|
b.WriteString(String(v.params, true))
|
|
b.WriteRune(' ')
|
|
body := String(v.body, true)
|
|
if strings.HasPrefix(body, "(begin ") {
|
|
body = body[7:]
|
|
}
|
|
b.WriteString(body)
|
|
return b.String()
|
|
case func(...expression) expression:
|
|
return fmt.Sprint("Built-in: ", &v)
|
|
case *IOHandle:
|
|
return fmt.Sprintf("%+v", v)
|
|
case guiWidget:
|
|
return "gui-widget"
|
|
case fyne.Widget:
|
|
return "gui-widget"
|
|
case fyne.CanvasObject:
|
|
return "gui-widget"
|
|
case fyne.Container:
|
|
return "gui-container"
|
|
case guiContainer:
|
|
return "gui-container"
|
|
case GUI:
|
|
return "gui-root"
|
|
case *GUI:
|
|
return "gui-root"
|
|
default:
|
|
return fmt.Sprint(v)
|
|
}
|
|
}
|