initial commit

This commit is contained in:
Jeff Carr 2024-01-01 16:19:40 -06:00
commit 7f6059a8e0
8 changed files with 496 additions and 0 deletions

15
README.md Normal file
View File

@ -0,0 +1,15 @@
# gadgets
Package gadgets are special collections of widgets
Things to avoid or keep in mind: Also Features these might enable:
```go
* These should never bypass the binary tree
* Hide complexity internally here
* These might require corresponding toolkit implementations?
* These might allow unique/good corresponding toolkit implementations (date/time selection?)
## Warning
Doing this / adding these might be a really bad idea that I will regret later

79
basicCombobox.go Normal file
View File

@ -0,0 +1,79 @@
/*
A Labeled Combobox widget:
-----------------------------
| | |
| Food: | <dropdown> |
| | |
-----------------------------
The user can then edit the dropdown field and type anything into it
*/
package gadgets
import (
"go.wit.com/log"
"go.wit.com/gui/gui"
)
type BasicCombobox struct {
ready bool
name string
parent *gui.Node // parent widget
l *gui.Node // label widget
d *gui.Node // dropdown widget
value string
label string
Custom func()
}
func (d *BasicCombobox) Get() string {
if ! d.Ready() {return ""}
return d.value
}
// Returns true if the status is valid
func (d *BasicCombobox) Ready() bool {
if d == nil {return false}
return d.ready
}
func (d *BasicCombobox) Add(value string) {
if ! d.Ready() {return}
log.Println("BasicCombobox.Add() =", value)
d.d.AddDropdownName(value)
return
}
func (d *BasicCombobox) Set(value string) bool {
if ! d.Ready() {return false}
log.Println("BasicCombobox.Set() =", value)
d.d.SetText(value)
d.value = value
return true
}
func NewBasicCombobox(p *gui.Node, name string) *BasicCombobox {
d := BasicCombobox {
parent: p,
name: name,
ready: false,
}
// various timeout settings
d.l = p.NewLabel(name)
d.d = p.NewCombobox("")
d.d.Custom = func() {
d.value = d.Get()
log.Println("BasicCombobox.Custom() user changed value to =", d.value)
if d.Custom != nil {
d.Custom()
}
}
d.ready = true
return &d
}

79
basicDropdown.go Normal file
View File

@ -0,0 +1,79 @@
/*
A Labeled Dropdown widget:
-----------------------------
| | |
| Food: | <dropdown> |
| | |
-----------------------------
This being a 'Basic Dropdown', the dropdown names must be unique
*/
package gadgets
import (
"go.wit.com/log"
"go.wit.com/gui/gui"
)
type BasicDropdown struct {
ready bool
name string
parent *gui.Node // parent widget
l *gui.Node // label widget
d *gui.Node // dropdown widget
value string
label string
Custom func()
}
func (d *BasicDropdown) Get() string {
if ! d.Ready() {return ""}
return d.d.GetText()
}
// Returns true if the status is valid
func (d *BasicDropdown) Ready() bool {
if d == nil {return false}
return d.ready
}
func (d *BasicDropdown) Add(value string) {
if ! d.Ready() {return}
log.Println("BasicDropdown.Set() =", value)
d.d.AddDropdownName(value)
return
}
func (d *BasicDropdown) Set(value string) bool {
if ! d.Ready() {return false}
log.Println("BasicDropdown.Set() =", value)
d.l.SetText(value)
d.value = value
return true
}
func NewBasicDropdown(p *gui.Node, name string) *BasicDropdown {
d := BasicDropdown {
parent: p,
name: name,
ready: false,
}
// various timeout settings
d.l = p.NewLabel(name)
d.d = p.NewDropdown("")
d.d.Custom = func() {
d.value = d.Get()
log.Println("BasicDropdown.Custom() user changed value to =", d.value)
if d.Custom != nil {
d.Custom()
}
}
d.ready = true
return &d
}

56
basicEntry.go Normal file
View File

@ -0,0 +1,56 @@
/*
A Labeled Single Line Entry widget:
-----------------------------
| | |
| Food: | <type here> |
| | |
-----------------------------
*/
package gadgets
import (
"go.wit.com/log"
"go.wit.com/gui/gui"
)
type BasicEntry struct {
parent *gui.Node // parent widget
l *gui.Node // label widget
v *gui.Node // value widget
value string
label string
Custom func()
}
func (n *BasicEntry) Get() string {
return n.value
}
func (n *BasicEntry) Set(value string) *BasicEntry {
log.Println("BasicEntry.Set() =", value)
if (n.v != nil) {
n.v.Set(value)
}
n.value = value
return n
}
func NewBasicEntry(p *gui.Node, name string) *BasicEntry {
d := BasicEntry {
parent: p,
value: "",
}
// various timeout settings
d.l = p.NewLabel(name)
d.v = p.NewEntryLine("")
d.v.Custom = func() {
d.value = d.v.S
log.Println("BasicEntry.Custom() user changed value to =", d.value)
}
return &d
}

60
basicLabel.go Normal file
View File

@ -0,0 +1,60 @@
/*
A Labeled label:
-----------------------------
| | |
| Food: | Apple |
| | |
-----------------------------
*/
package gadgets
import (
"go.wit.com/log"
"go.wit.com/gui/gui"
)
type Node gui.Node
type BasicLabel struct {
p *gui.Node // parent widget
l *gui.Node // label widget
v *gui.Node // value widget
value string
label string
Custom func()
}
func (n *BasicLabel) Get() string {
return n.value
}
func (n *BasicLabel) Set(value string) *BasicLabel {
log.Println("BasicLabel.Set() =", value)
if (n.v != nil) {
n.v.Set(value)
}
n.value = value
return n
}
func (ngui *Node) NewBasicLabel(name string) *BasicLabel {
var n *gui.Node
n = (*gui.Node)(ngui)
d := BasicLabel {
p: n,
value: "",
}
// various timeout settings
d.l = n.NewLabel(name)
d.v = n.NewLabel("")
d.v.Custom = func() {
d.value = d.v.S
log.Println("BasicLabel.Custom() user changed value to =", d.value)
}
return &d
}

79
durationSlider.go Normal file
View File

@ -0,0 +1,79 @@
/*
A slider that goes between a High and Low time
*/
package gadgets
import (
"log"
"fmt"
"time"
"go.wit.com/gui/gui"
)
type Duration struct {
p *gui.Node // parent widget
l *gui.Node // label widget
s *gui.Node // slider widget
Label string
Low time.Duration
High time.Duration
Duration time.Duration
Custom func()
}
func (n *Duration) Set(d time.Duration) {
var timeRange, step, offset time.Duration
if (d > n.High) {
d = n.High
}
if (d < n.Low) {
d = n.Low
}
// set the duration
n.Duration = d
// figure out the integer offset for the Slider GUI Widget
timeRange = n.High - n.Low
step = timeRange / 1000
if (step == 0) {
log.Println("duration.Set() division by step == 0", n.Low, n.High, timeRange, step)
n.s.Set(0)
return
}
offset = d - n.Low
i := int(offset / step)
log.Println("duration.Set() =", n.Low, n.High, d, "i =", i)
n.s.I = i
n.s.Set(i)
n.s.Custom()
}
func NewDurationSlider(n *gui.Node, label string, low time.Duration, high time.Duration) *Duration {
d := Duration {
p: n,
Label: label,
High: high,
Low: low,
}
// various timeout settings
d.l = n.NewLabel(label)
d.s = n.NewSlider(label, 0, 1000)
d.s.Custom = func () {
d.Duration = low + (high - low) * time.Duration(d.s.I) / 1000
log.Println("d.Duration =", d.Duration)
s := fmt.Sprintf("%s (%v)", d.Label, d.Duration)
d.l.SetText(s)
if (d.Custom != nil) {
d.Custom()
}
}
return &d
}

72
logSettings.go Normal file
View File

@ -0,0 +1,72 @@
package gadgets
import (
"go.wit.com/log"
"go.wit.com/gui/gui"
)
var myLogGui *LogSettings
type LogSettings struct {
ready bool
hidden bool
err error
parent *gui.Node // should be the root of the 'gui' package binary tree
window *gui.Node // our window for displaying the log package settings
group *gui.Node //
grid *gui.Node //
// Primary Directives
status *OneLiner
summary *OneLiner
}
// This is initializes the main DO object
// You can only have one of these
func NewLogSettings(p *gui.Node) *LogSettings {
if myLogGui != nil {return myLogGui}
myLogGui = new(LogSettings)
myLogGui.parent = p
myLogGui.ready = false
myLogGui.window = p.NewWindow("Log Settings")
// make a group label and a grid
myLogGui.group = myLogGui.window.NewGroup("droplets:").Pad()
myLogGui.grid = myLogGui.group.NewGrid("grid", 2, 1).Pad()
myLogGui.ready = true
myLogGui.Hide()
return myLogGui
}
// Returns true if the status is valid
func (d *LogSettings) Ready() bool {
if d == nil {return false}
return d.ready
}
func (d *LogSettings) Show() {
if ! d.Ready() {return}
log.Info("LogSettings.Show() window")
if d.hidden {
d.window.Show()
}
d.hidden = false
}
func (d *LogSettings) Hide() {
if ! d.Ready() {return}
log.Info("LogSettings.Hide() window")
if ! d.hidden {
d.window.Hide()
}
d.hidden = true
}
func (d *LogSettings) Update() bool {
if ! d.Ready() {return false}
return true
}

56
oneLiner.go Normal file
View File

@ -0,0 +1,56 @@
/*
A Labeled label:
-----------------------------
| | |
| Food: | Apple |
| | |
-----------------------------
*/
package gadgets
import (
"go.wit.com/log"
"go.wit.com/gui/gui"
)
type OneLiner struct {
p *gui.Node // parent widget
l *gui.Node // label widget
v *gui.Node // value widget
value string
label string
Custom func()
}
func (n *OneLiner) Get() string {
return n.value
}
func (n *OneLiner) Set(value string) *OneLiner {
log.Println("OneLiner.Set() =", value)
if (n.v != nil) {
n.v.Set(value)
}
n.value = value
return n
}
func NewOneLiner(n *gui.Node, name string) *OneLiner {
d := OneLiner {
p: n,
value: "",
}
// various timeout settings
d.l = n.NewLabel(name)
d.v = n.NewLabel("")
d.v.Custom = func() {
d.value = d.v.S
log.Println("OneLiner.Custom() user changed value to =", d.value)
}
return &d
}