ICDB/src/internal/ui/sidebar.go
zervo df8f6716fa Minor UI tweaks
Just for fun!
2025-01-31 23:34:00 +01:00

85 lines
2.5 KiB
Go

package ui
import (
t_ui "icdb/pkg/types/ui"
"image"
"image/color"
"gio.tools/icons"
"gioui.org/layout"
"gioui.org/op/clip"
"gioui.org/op/paint"
"gioui.org/unit"
"gioui.org/widget"
"gioui.org/widget/material"
)
var (
mainButton widget.Clickable
partsButton widget.Clickable
settingsButton widget.Clickable
)
var sidebarWidth = unit.Dp(200)
var sidebarBackground = color.NRGBA{R: 225, G: 225, B: 225, A: 255}
// renderSidebar creates the sidebar UI.
// It renders a colored background and then adds the navigation buttons.
func renderSidebar(th *material.Theme) layout.Widget {
return func(gtx layout.Context) layout.Dimensions {
return layout.Stack{Alignment: layout.Center}.Layout(gtx,
// Expanded: Draw the background to fill the entire left pane.
layout.Expanded(func(gtx layout.Context) layout.Dimensions {
// Use a rectangle that covers the full widget area.
rect := clip.Rect{
Min: image.Pt(0, 0),
Max: gtx.Constraints.Max,
}
paint.FillShape(gtx.Ops, sidebarBackground, rect.Op())
return layout.Dimensions{Size: gtx.Constraints.Max}
}),
// Stacked: Lay out the navigation buttons.
layout.Stacked(func(gtx layout.Context) layout.Dimensions {
// Internal padding for the sidebar.
in := layout.Inset{
Top: unit.Dp(5),
Left: unit.Dp(5),
Right: unit.Dp(5),
Bottom: unit.Dp(5),
}
return in.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
return layout.Flex{
Axis: layout.Vertical,
Alignment: layout.Start,
}.Layout(gtx,
layout.Rigid(renderNavButton(th, icons.ActionHome, "Main", &mainButton, t_ui.UIViewMain)),
layout.Rigid(renderNavButton(th, icons.ActionBuild, "Parts", &partsButton, t_ui.UIViewParts)),
layout.Rigid(renderNavButton(th, icons.ActionSettings, "Settings", &settingsButton, t_ui.UIViewSettings)),
)
})
}),
)
}
}
// renderNavButton creates a navigation icon button.
func renderNavButton(th *material.Theme, icon *widget.Icon, label string, btn *widget.Clickable, target t_ui.UIView) layout.Widget {
return func(gtx layout.Context) layout.Dimensions {
if btn.Clicked(gtx) {
currentView = target
}
// Padding for each button, to add spacing between them.
in := layout.Inset{
Bottom: unit.Dp(8),
Left: unit.Dp(4),
Right: unit.Dp(4),
}
return in.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
btn := material.IconButton(th, btn, icon, label)
btn.Size = unit.Dp(30)
return btn.Layout(gtx)
})
}
}