Files

115 lines
2.8 KiB
Go
Raw Permalink Normal View History

2026-02-10 11:03:04 -06:00
package main
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
2026-02-10 12:51:58 -06:00
"fyne.io/fyne/v2/theme"
2026-02-10 11:03:04 -06:00
"fyne.io/fyne/v2/widget"
)
type calculation struct {
id int
equation string
diceRolls string
result string
}
type historyItemRenderer struct {
item *historyItem
2026-02-10 12:51:58 -06:00
equationLabel *customLabel
diceRollsLabel *customLabel
resultLabel *customLabel
container fyne.CanvasObject
2026-02-10 11:03:04 -06:00
objects []fyne.CanvasObject
}
func (r *historyItemRenderer) MinSize() fyne.Size {
minSize := r.container.MinSize()
// Ensure a minimum height so items don't overlap
if minSize.Height < 90 {
minSize = fyne.NewSize(minSize.Width, 90)
}
return minSize
2026-02-10 11:03:04 -06:00
}
func (r *historyItemRenderer) Layout(size fyne.Size) {
r.container.Resize(size)
2026-02-10 11:03:04 -06:00
}
func (r *historyItemRenderer) ApplyTheme() {
}
func (r *historyItemRenderer) Refresh() {
2026-02-10 12:51:58 -06:00
r.equationLabel.Text = r.item.calc.equation
r.diceRollsLabel.Text = r.item.calc.diceRolls
r.resultLabel.Text = r.item.calc.result
2026-02-10 11:03:04 -06:00
r.equationLabel.Refresh()
r.diceRollsLabel.Refresh()
r.resultLabel.Refresh()
r.container.Refresh()
2026-02-10 11:03:04 -06:00
}
func (r *historyItemRenderer) Objects() []fyne.CanvasObject {
return r.objects
}
func (r *historyItemRenderer) Destroy() {
}
type historyItem struct {
widget.BaseWidget
calc *calculation
onTapped func(equation string)
}
func (h *historyItem) Tapped(*fyne.PointEvent) {
if h.onTapped != nil && h.calc != nil {
h.onTapped(h.calc.equation)
}
}
func (h *historyItem) TappedSecondary(*fyne.PointEvent) {
2026-02-10 11:03:04 -06:00
}
func (h *historyItem) CreateRenderer() fyne.WidgetRenderer {
2026-02-10 12:51:58 -06:00
equationLabel := newCustomLabel(h.calc.equation, fyne.CurrentApp().Settings().Theme().Size(theme.SizeNameText)*2, fyne.TextStyle{Bold: true}, fyne.TextAlignLeading, theme.ForegroundColor())
diceRollsLabel := newCustomLabel(h.calc.diceRolls, fyne.CurrentApp().Settings().Theme().Size(theme.SizeNameText)*1.5, fyne.TextStyle{Italic: true}, fyne.TextAlignLeading, theme.ForegroundColor())
resultLabel := newCustomLabel(h.calc.result, fyne.CurrentApp().Settings().Theme().Size(theme.SizeNameText)*2, fyne.TextStyle{}, fyne.TextAlignTrailing, theme.ForegroundColor())
2026-02-10 11:03:04 -06:00
layout := container.NewBorder(
nil,
nil,
container.NewVBox(
equationLabel,
diceRollsLabel,
),
resultLabel,
)
return &historyItemRenderer{
item: h,
equationLabel: equationLabel,
diceRollsLabel: diceRollsLabel,
resultLabel: resultLabel,
container: layout,
2026-02-10 11:03:04 -06:00
objects: []fyne.CanvasObject{layout},
}
}
func (h *historyItem) SetCalculation(c *calculation) {
h.calc = c
h.Refresh()
}
func newHistoryItem(c *calculation) *historyItem {
item := &historyItem{calc: c}
item.ExtendBaseWidget(item)
return item
}
func newHistoryItemWithCallback(c *calculation, onTapped func(equation string)) *historyItem {
item := &historyItem{calc: c, onTapped: onTapped}
item.ExtendBaseWidget(item)
return item
}