func createIconButton(driver gxui.Driver, theme gxui.Theme, iconPath string) gxui.Button { button := theme.CreateButton() button.SetType(gxui.PushButton) fileBytes, err := assets.Asset(iconPath) if err != nil { log.Printf("Error: Failed to read asset %s: %s", iconPath, err) return button } f := bytes.NewBuffer(fileBytes) src, _, err := image.Decode(f) if err != nil { log.Printf("Error: Failed to decode image %s: %s", iconPath, err) return button } src = resize.Resize(24, 24, src, resize.Bilinear) rgba := image.NewRGBA(src.Bounds()) draw.Draw(rgba, src.Bounds(), src, image.ZP, draw.Src) texture := driver.CreateTexture(rgba, 1) icon := theme.CreateImage() icon.SetTexture(texture) button.AddChild(icon) return button }
func (defaultTreeControlCreator) Create(theme gxui.Theme, control gxui.Control, node *TreeToListNode) gxui.Control { ll := theme.CreateLinearLayout() ll.SetDirection(gxui.LeftToRight) btn := theme.CreateButton() btn.SetBackgroundBrush(gxui.TransparentBrush) btn.SetBorderPen(gxui.CreatePen(1, gxui.Gray30)) btn.SetMargin(math.Spacing{L: 2, R: 2, T: 1, B: 1}) btn.OnClick(func(ev gxui.MouseEvent) { if ev.Button == gxui.MouseButtonLeft { node.ToggleExpanded() } }) update := func() { btn.SetVisible(!node.IsLeaf()) if node.IsExpanded() { btn.SetText("-") } else { btn.SetText("+") } } update() gxui.WhileAttached(btn, node.OnChange, update) ll.AddChild(btn) ll.AddChild(control) ll.SetPadding(math.Spacing{L: 16 * node.Depth()}) return ll }
func (treeControlCreator) Create(theme gxui.Theme, control gxui.Control, node *mixins.TreeToListNode) gxui.Control { img := theme.CreateImage() imgSize := math.Size{W: 10, H: 10} ll := theme.CreateLinearLayout() ll.SetDirection(gxui.LeftToRight) btn := theme.CreateButton() btn.SetBackgroundBrush(gxui.TransparentBrush) btn.SetBorderPen(gxui.CreatePen(1, gxui.Gray30)) btn.SetMargin(math.Spacing{L: 1, R: 1, T: 1, B: 1}) btn.OnClick(func(ev gxui.MouseEvent) { if ev.Button == gxui.MouseButtonLeft { node.ToggleExpanded() } }) btn.AddChild(img) update := func() { expanded := node.IsExpanded() canvas := theme.Driver().CreateCanvas(imgSize) btn.SetVisible(!node.IsLeaf()) switch { case !btn.IsMouseDown(gxui.MouseButtonLeft) && expanded: canvas.DrawPolygon(expandedPoly, gxui.TransparentPen, gxui.CreateBrush(gxui.Gray70)) case !btn.IsMouseDown(gxui.MouseButtonLeft) && !expanded: canvas.DrawPolygon(collapsedPoly, gxui.TransparentPen, gxui.CreateBrush(gxui.Gray70)) case expanded: canvas.DrawPolygon(expandedPoly, gxui.TransparentPen, gxui.CreateBrush(gxui.Gray30)) case !expanded: canvas.DrawPolygon(collapsedPoly, gxui.TransparentPen, gxui.CreateBrush(gxui.Gray30)) } canvas.Complete() img.SetCanvas(canvas) } btn.OnMouseDown(func(gxui.MouseEvent) { update() }) btn.OnMouseUp(func(gxui.MouseEvent) { update() }) update() gxui.WhileAttached(btn, node.OnChange, update) ll.AddChild(btn) ll.AddChild(control) ll.SetPadding(math.Spacing{L: 16 * node.Depth()}) return ll }
// Create implements gxui.TreeNode. func (n *node) Create(theme gxui.Theme) gxui.Control { layout := theme.CreateLinearLayout() layout.SetDirection(gxui.LeftToRight) label := theme.CreateLabel() label.SetText(n.name) textbox := theme.CreateTextBox() textbox.SetText(n.name) textbox.SetPadding(math.ZeroSpacing) textbox.SetMargin(math.ZeroSpacing) addButton := theme.CreateButton() addButton.SetText("+") addButton.OnClick(func(gxui.MouseEvent) { n.add("<new>") }) edit := func() { layout.RemoveAll() layout.AddChild(textbox) layout.AddChild(addButton) gxui.SetFocus(textbox) } commit := func() { n.name = textbox.Text() label.SetText(n.name) layout.RemoveAll() layout.AddChild(label) layout.AddChild(addButton) } // When the user clicks the label, replace it with an editable text-box label.OnClick(func(gxui.MouseEvent) { edit() }) // When the text-box loses focus, replace it with a label again. textbox.OnLostFocus(commit) layout.AddChild(label) layout.AddChild(addButton) return layout }