diff options
| author | Jeff Carr <[email protected]> | 2024-02-19 14:41:32 -0600 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2024-02-19 14:41:32 -0600 |
| commit | 10eb3685d71a8b4f74a6b3430ef54d56d9aa625d (patch) | |
| tree | a76b5908c74863be81d0178ae6efeb26dd90f0f6 | |
| parent | 337a55eaac4ffc516c26286b04b039533adb157d (diff) | |
try out a "Mirror" concept for widgets
| -rw-r--r-- | label.go | 67 | ||||
| -rw-r--r-- | node.go | 48 | ||||
| -rw-r--r-- | setText.go | 8 | ||||
| -rw-r--r-- | structs.go | 4 |
4 files changed, 111 insertions, 16 deletions
@@ -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) + } + } +} @@ -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 @@ -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: @@ -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 |
