refactor(tracker/gioui): move element etc. functions away from style

Now the element / fg / bg functions are passed to the actual Layout
function, not first put to the style. This avoids moving of the
element function to heap.
This commit is contained in:
5684185+vsariola@users.noreply.github.com
2025-05-26 22:31:01 +03:00
parent de2e64533d
commit d20a23d57b
6 changed files with 35 additions and 39 deletions

View File

@ -32,23 +32,20 @@ type DragList struct {
}
type FilledDragListStyle struct {
dragList *DragList
HoverColor color.NRGBA
Cursor CursorStyle
Selection CursorStyle
ScrollBar ScrollBarStyle
element, bg func(gtx C, i int) D
dragList *DragList
HoverColor color.NRGBA
Cursor CursorStyle
Selection CursorStyle
ScrollBar ScrollBarStyle
}
func NewDragList(model tracker.List, axis layout.Axis) *DragList {
return &DragList{TrackerList: model, List: &layout.List{Axis: axis}, HoverItem: -1, ScrollBar: &ScrollBar{Axis: axis}}
}
func FilledDragList(th *Theme, dragList *DragList, element, bg func(gtx C, i int) D) FilledDragListStyle {
func FilledDragList(th *Theme, dragList *DragList) FilledDragListStyle {
return FilledDragListStyle{
dragList: dragList,
element: element,
bg: bg,
HoverColor: hoveredColor(th.Selection.Active),
Cursor: th.Cursor,
Selection: th.Selection,
@ -68,7 +65,7 @@ func (s FilledDragListStyle) LayoutScrollBar(gtx C) D {
return s.dragList.ScrollBar.Layout(gtx, &s.ScrollBar, s.dragList.TrackerList.Count(), &s.dragList.List.Position)
}
func (s FilledDragListStyle) Layout(gtx C) D {
func (s FilledDragListStyle) Layout(gtx C, element, bg func(gtx C, i int) D) D {
swap := 0
defer op.Offset(image.Point{}).Push(gtx.Ops).Pop()
@ -243,11 +240,11 @@ func (s FilledDragListStyle) Layout(gtx C) D {
return layout.Dimensions{Size: gtx.Constraints.Min}
}
macro := op.Record(gtx.Ops)
dims := s.element(gtx, index)
dims := element(gtx, index)
call := macro.Stop()
gtx.Constraints.Min = dims.Size
if s.bg != nil {
s.bg(gtx, index)
if bg != nil {
bg(gtx, index)
}
cursorBg(gtx)
call.Add(gtx.Ops)

View File

@ -330,7 +330,7 @@ func (ie *InstrumentEditor) layoutInstrumentList(gtx C, t *Tracker) D {
})
}
instrumentList := FilledDragList(t.Theme, ie.instrumentDragList, element, nil)
instrumentList := FilledDragList(t.Theme, ie.instrumentDragList)
instrumentList.ScrollBar = t.Theme.InstrumentEditor.InstrumentList.ScrollBar
defer op.Offset(image.Point{}).Push(gtx.Ops).Pop()
@ -360,7 +360,7 @@ func (ie *InstrumentEditor) layoutInstrumentList(gtx C, t *Tracker) D {
}
}
dims := instrumentList.Layout(gtx)
dims := instrumentList.Layout(gtx, element, nil)
gtx.Constraints = layout.Exact(dims.Size)
instrumentList.LayoutScrollBar(gtx)
return dims
@ -462,7 +462,7 @@ func (ie *InstrumentEditor) layoutUnitList(gtx C, t *Tracker) D {
}
defer op.Offset(image.Point{}).Push(gtx.Ops).Pop()
unitList := FilledDragList(t.Theme, ie.unitDragList, element, nil)
unitList := FilledDragList(t.Theme, ie.unitDragList)
for {
event, ok := gtx.Event(
key.Filter{Focus: ie.unitDragList, Name: key.NameRightArrow},
@ -500,7 +500,7 @@ func (ie *InstrumentEditor) layoutUnitList(gtx C, t *Tracker) D {
layout.Expanded(func(gtx C) D {
defer clip.Rect(image.Rect(0, 0, gtx.Constraints.Max.X, gtx.Constraints.Max.Y)).Push(gtx.Ops).Pop()
gtx.Constraints = layout.Exact(image.Pt(gtx.Dp(140), gtx.Constraints.Max.Y))
dims := unitList.Layout(gtx)
dims := unitList.Layout(gtx, element, nil)
unitList.LayoutScrollBar(gtx)
return dims
}),

View File

@ -321,12 +321,12 @@ func (te *NoteEditor) layoutTracks(gtx C, t *Tracker) D {
widget.Label{Alignment: text.Middle}.Layout(gtx, t.Theme.Material.Shaper, t.Theme.NoteEditor.Note.Font, t.Theme.NoteEditor.Note.TextSize, val, op)
return D{Size: image.Pt(pxWidth, pxHeight)}
}
table := FilledScrollTable(t.Theme, te.scrollTable, cell, colTitle, rowTitle, nil, rowTitleBg)
table := FilledScrollTable(t.Theme, te.scrollTable)
table.RowTitleWidth = trackPatMarkWidth + trackRowMarkWidth
table.ColumnTitleHeight = trackColTitleHeight
table.CellWidth = trackColWidth
table.CellHeight = trackRowHeight
return table.Layout(gtx)
return table.Layout(gtx, cell, colTitle, rowTitle, nil, rowTitleBg)
}
func colorOp(gtx C, c color.NRGBA) op.CallOp {

View File

@ -118,10 +118,10 @@ func (oe *OrderEditor) Layout(gtx C, t *Tracker) D {
return D{Size: image.Pt(gtx.Dp(patternCellWidth), gtx.Dp(patternCellHeight))}
}
table := FilledScrollTable(t.Theme, oe.scrollTable, cell, colTitle, rowTitle, nil, rowTitleBg)
table := FilledScrollTable(t.Theme, oe.scrollTable)
table.ColumnTitleHeight = orderTitleHeight
return table.Layout(gtx)
return table.Layout(gtx, cell, colTitle, rowTitle, nil, rowTitleBg)
}
func (oe *OrderEditor) handleEvents(gtx C, t *Tracker) {

View File

@ -71,12 +71,11 @@ func NewScrollTable(table tracker.Table, vertList, horizList tracker.List) *Scro
return ret
}
func FilledScrollTable(th *Theme, scrollTable *ScrollTable, element func(gtx C, x, y int) D, colTitle, rowTitle, colTitleBg, rowTitleBg func(gtx C, i int) D) ScrollTableStyle {
func FilledScrollTable(th *Theme, scrollTable *ScrollTable) ScrollTableStyle {
return ScrollTableStyle{
RowTitleStyle: FilledDragList(th, scrollTable.RowTitleList, rowTitle, rowTitleBg),
ColTitleStyle: FilledDragList(th, scrollTable.ColTitleList, colTitle, colTitleBg),
RowTitleStyle: FilledDragList(th, scrollTable.RowTitleList),
ColTitleStyle: FilledDragList(th, scrollTable.ColTitleList),
ScrollTable: scrollTable,
element: element,
ScrollBarWidth: unit.Dp(14),
RowTitleWidth: unit.Dp(30),
ColumnTitleHeight: unit.Dp(16),
@ -108,7 +107,7 @@ func (st *ScrollTable) ChildFocused() bool {
return st.ColTitleList.Focused() || st.RowTitleList.Focused()
}
func (s ScrollTableStyle) Layout(gtx C) D {
func (s ScrollTableStyle) Layout(gtx C, element func(gtx C, x, y int) D, colTitle, rowTitle, colTitleBg, rowTitleBg func(gtx C, i int) D) D {
defer clip.Rect(image.Rectangle{Max: gtx.Constraints.Max}).Push(gtx.Ops).Pop()
event.Op(gtx.Ops, s.ScrollTable)
@ -118,11 +117,11 @@ func (s ScrollTableStyle) Layout(gtx C) D {
return Surface{Gray: 24, Focus: s.ScrollTable.Focused() || s.ScrollTable.ChildFocused()}.Layout(gtx, func(gtx C) D {
defer clip.Rect(image.Rect(0, 0, gtx.Constraints.Max.X, gtx.Constraints.Max.Y)).Push(gtx.Ops).Pop()
dims := gtx.Constraints.Max
s.layoutColTitles(gtx, p)
s.layoutRowTitles(gtx, p)
s.layoutColTitles(gtx, p, colTitle, colTitleBg)
s.layoutRowTitles(gtx, p, rowTitle, rowTitleBg)
defer op.Offset(p).Push(gtx.Ops).Pop()
gtx.Constraints = layout.Exact(image.Pt(gtx.Constraints.Max.X-p.X, gtx.Constraints.Max.Y-p.Y))
s.layoutTable(gtx)
s.layoutTable(gtx, element)
s.RowTitleStyle.LayoutScrollBar(gtx)
s.ColTitleStyle.LayoutScrollBar(gtx)
return D{Size: dims}
@ -209,7 +208,7 @@ func (s *ScrollTableStyle) handleEvents(gtx layout.Context, p image.Point) {
}
}
func (s ScrollTableStyle) layoutTable(gtx C) {
func (s ScrollTableStyle) layoutTable(gtx C, element func(gtx C, x, y int) D) {
defer clip.Rect(image.Rectangle{Max: gtx.Constraints.Min}).Push(gtx.Ops).Pop()
if s.ScrollTable.requestFocus {
@ -227,26 +226,26 @@ func (s ScrollTableStyle) layoutTable(gtx C) {
for x := 0; x < colP.Count; x++ {
for y := 0; y < rowP.Count; y++ {
o := op.Offset(image.Pt(cellWidth*x, cellHeight*y)).Push(gtx.Ops)
s.element(gtx, x+colP.First, y+rowP.First)
element(gtx, x+colP.First, y+rowP.First)
o.Pop()
}
}
}
func (s *ScrollTableStyle) layoutRowTitles(gtx C, p image.Point) {
func (s *ScrollTableStyle) layoutRowTitles(gtx C, p image.Point, fg, bg func(gtx C, i int) D) {
defer op.Offset(image.Pt(0, p.Y)).Push(gtx.Ops).Pop()
gtx.Constraints.Min.X = p.X
gtx.Constraints.Max.Y -= p.Y
gtx.Constraints.Min.Y = gtx.Constraints.Max.Y
s.RowTitleStyle.Layout(gtx)
s.RowTitleStyle.Layout(gtx, fg, bg)
}
func (s *ScrollTableStyle) layoutColTitles(gtx C, p image.Point) {
func (s *ScrollTableStyle) layoutColTitles(gtx C, p image.Point, fg, bg func(gtx C, i int) D) {
defer op.Offset(image.Pt(p.X, 0)).Push(gtx.Ops).Pop()
gtx.Constraints.Min.Y = p.Y
gtx.Constraints.Max.X -= p.X
gtx.Constraints.Min.X = gtx.Constraints.Max.X
s.ColTitleStyle.Layout(gtx)
s.ColTitleStyle.Layout(gtx, fg, bg)
}
func (s *ScrollTable) command(gtx C, e key.Event) {

View File

@ -118,8 +118,8 @@ func (pe *UnitEditor) layoutSliders(gtx C, t *Tracker) D {
return D{Size: image.Pt(gtx.Constraints.Max.X, dims.Size.Y)}
}
fdl := FilledDragList(t.Theme, pe.sliderList, element, nil)
dims := fdl.Layout(gtx)
fdl := FilledDragList(t.Theme, pe.sliderList)
dims := fdl.Layout(gtx, element, nil)
gtx.Constraints = layout.Exact(dims.Size)
fdl.LayoutScrollBar(gtx)
return dims
@ -191,8 +191,8 @@ func (pe *UnitEditor) layoutUnitTypeChooser(gtx C, t *Tracker) D {
}
return w.Layout(gtx)
}
fdl := FilledDragList(t.Theme, pe.searchList, element, nil)
dims := fdl.Layout(gtx)
fdl := FilledDragList(t.Theme, pe.searchList)
dims := fdl.Layout(gtx, element, nil)
gtx.Constraints = layout.Exact(dims.Size)
fdl.LayoutScrollBar(gtx)
return dims