summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2024-02-19 14:41:32 -0600
committerJeff Carr <[email protected]>2024-02-19 14:41:32 -0600
commit10eb3685d71a8b4f74a6b3430ef54d56d9aa625d (patch)
treea76b5908c74863be81d0178ae6efeb26dd90f0f6
parent337a55eaac4ffc516c26286b04b039533adb157d (diff)
try out a "Mirror" concept for widgets
-rw-r--r--label.go67
-rw-r--r--node.go48
-rw-r--r--setText.go8
-rw-r--r--structs.go4
4 files changed, 111 insertions, 16 deletions
diff --git a/label.go b/label.go
index 23e2de4..df33a5a 100644
--- a/label.go
+++ b/label.go
@@ -1,6 +1,7 @@
package gui
import (
+ "go.wit.com/log"
"go.wit.com/widget"
)
@@ -13,3 +14,69 @@ func (parent *Node) NewLabel(text string) *Node {
sendAction(newNode, widget.Add)
return newNode
}
+
+// an experiemental idea
+// basically, this is like a cell in a spreadsheet that is viable
+// in one place but also exists sonewhere else
+func (parent *Node) Mirror(m *Node) *Node {
+ switch m.WidgetType {
+ case widget.Label:
+ text := m.label
+ newNode := parent.newNode(text, widget.Label)
+ newNode.label = text
+ newNode.progname = text
+ newNode.isMirror = m
+ m.hasMirrors = append(m.hasMirrors, newNode)
+
+ // inform the toolkits
+ sendAction(newNode, widget.Add)
+ return newNode
+ default:
+ return nil
+ }
+}
+
+// make a mirror widget without a parent
+func RawMirror(m *Node) *Node {
+ switch m.WidgetType {
+ case widget.Label:
+ newNode := rawNode("MIRROR", widget.Label)
+ newNode.isMirror = m
+ m.hasMirrors = append(m.hasMirrors, newNode)
+ return newNode
+ default:
+ return nil
+ }
+}
+
+func (n *Node) IsMirror() bool {
+ if n.isMirror == nil {
+ return false
+ }
+ return true
+}
+
+func (n *Node) hasMirror() bool {
+ if len(n.hasMirrors) == 0 {
+ return false
+ }
+ return true
+}
+
+func (n *Node) updateMirrors() {
+ if n.IsMirror() {
+ // n is a mirror of something else
+ return
+ }
+ for _, m := range n.hasMirrors {
+ switch m.WidgetType {
+ case widget.Label:
+ m.label = n.label
+ m.changed = true
+ // inform the toolkits
+ sendAction(m, widget.SetText)
+ default:
+ log.Log(WARN, "can not mirror widget type", m.WidgetType)
+ }
+ }
+}
diff --git a/node.go b/node.go
index 156a507..7c314e9 100644
--- a/node.go
+++ b/node.go
@@ -13,12 +13,21 @@ and it initializes basic default values
there isn't much to see here.
*/
-func (n *Node) newNode(s string, t widget.WidgetType) *Node {
- if n == nil {
+func (parent *Node) newNode(s string, t widget.WidgetType) *Node {
+ if parent == nil {
log.Log(WARN, "newNode() ERROR got parent == nil")
// this is an error internal to this gui package
- return n
+ return parent
}
+ newN := rawNode(s, t)
+ parent.Append(newN)
+ return newN
+}
+
+// I've been using 'raw' in the code here and there
+// to indicate a widget that hasn't be placed in the binary tree
+// there is probably a more approriate name
+func rawNode(s string, t widget.WidgetType) *Node {
var newN *Node
newN = addNode()
@@ -37,19 +46,7 @@ func (n *Node) newNode(s string, t widget.WidgetType) *Node {
newN.borderless = false // usually this is just used for Window widgets
newN.enabled = true
newN.changed = true
-
- if n.WidgetType == widget.Grid {
- n.gridIncrement()
- }
- newN.AtW = n.NextW
- newN.AtH = n.NextH
-
- // honor the visable settings of the parent
- newN.visable = n.visable
newN.hidden = false // by default, always draw a widget
-
- n.children = append(n.children, newN)
- newN.parent = n
return newN
}
@@ -65,6 +62,27 @@ func addNode() *Node {
return n
}
+func (parent *Node) Append(n *Node) {
+ if n.parent != nil {
+ log.Log(WARN, "Widget already has a parent already assigned")
+ return
+ }
+ // track the parent and this widget as a child
+ parent.children = append(parent.children, n)
+ n.parent = parent
+
+ // if the parent is a grid, add it to the next cell
+ if parent.WidgetType == widget.Grid {
+ parent.gridIncrement()
+ }
+ n.AtW = parent.NextW
+ n.AtH = parent.NextH
+
+ // honor the visable settings of the parent
+ n.visable = parent.visable
+}
+
+// returns the parent widget
func (n *Node) Parent() *Node {
if !n.Ready() {
return n
diff --git a/setText.go b/setText.go
index a3f7de9..67b7f4c 100644
--- a/setText.go
+++ b/setText.go
@@ -17,7 +17,13 @@ func (n *Node) SetLabel(label string) *Node {
case widget.Button:
n.label = label
case widget.Label:
- n.label = label
+ if n.IsMirror() {
+ // n is a mirror of something else
+ log.Log(WARN, "SetLabel() error this widget is a mirror", n.id)
+ } else {
+ n.label = label
+ n.updateMirrors()
+ }
case widget.Group:
n.label = label
case widget.Window:
diff --git a/structs.go b/structs.go
index d14f08d..8515184 100644
--- a/structs.go
+++ b/structs.go
@@ -130,6 +130,10 @@ type Node struct {
parent *Node
children []*Node
+ // Experiment. This might be a great idea or a terrible idea
+ isMirror *Node // is a mirror of some other widget
+ hasMirrors []*Node // has a mirror
+
// RETHINK EVERYTHING BELOW HERE
pad bool // the toolkit may use this. it's up to the toolkit
margin bool // the toolkit may use this. it's up to the toolkit