VideoTools/internal/ui/mainmenu.go
Stu Leak 5b544b8484 Add history entry delete button and fix Convert module crash
Features:
- Add "×" delete button to each history entry in sidebar
- Click to remove individual entries from history
- Automatically saves and refreshes sidebar after deletion

Bug Fixes:
- Fix nil pointer crash when opening Convert module
- Fixed widget initialization order: bitrateContainer now created
  AFTER bitratePresetSelect is initialized
- Prevented "invalid memory address" panic in tabs layout

Technical Details:
- Added deleteHistoryEntry() method to remove entries by ID
- Updated BuildHistorySidebar signature to accept onEntryDelete callback
- Moved bitrateContainer creation from line 5742 to 5794
- All Select widgets now properly initialized before container creation

The crash was caused by bitrateContainer containing a nil
bitratePresetSelect widget, which crashed when Fyne's layout system
called .Visible() during tab initialization.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-18 11:51:26 -05:00

262 lines
7.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package ui
import (
"fmt"
"image/color"
"sort"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/widget"
"git.leaktechnologies.dev/stu/VideoTools/internal/logging"
"git.leaktechnologies.dev/stu/VideoTools/internal/queue"
"git.leaktechnologies.dev/stu/VideoTools/internal/utils"
)
// ModuleInfo contains information about a module for display
type ModuleInfo struct {
ID string
Label string
Color color.Color
Enabled bool
Category string
}
// HistoryEntry represents a completed job in the history
type HistoryEntry struct {
ID string
Type queue.JobType
Status queue.JobStatus
Title string
InputFile string
OutputFile string
LogPath string
Config map[string]interface{}
CreatedAt time.Time
StartedAt *time.Time
CompletedAt *time.Time
Error string
FFmpegCmd string
}
// BuildMainMenu creates the main menu view with module tiles grouped by category
func BuildMainMenu(modules []ModuleInfo, onModuleClick func(string), onModuleDrop func(string, []fyne.URI), onQueueClick func(), onLogsClick func(), onBenchmarkClick func(), onBenchmarkHistoryClick func(), onToggleSidebar func(), sidebarVisible bool, sidebar fyne.CanvasObject, titleColor, queueColor, textColor color.Color, queueCompleted, queueTotal int) fyne.CanvasObject {
title := canvas.NewText("VIDEOTOOLS", titleColor)
title.TextStyle = fyne.TextStyle{Monospace: true, Bold: true}
title.TextSize = 28
queueTile := buildQueueTile(queueCompleted, queueTotal, queueColor, textColor, onQueueClick)
sidebarToggleBtn := widget.NewButton("☰ History", onToggleSidebar)
sidebarToggleBtn.Importance = widget.LowImportance
benchmarkBtn := widget.NewButton("Run Benchmark", onBenchmarkClick)
benchmarkBtn.Importance = widget.LowImportance
viewResultsBtn := widget.NewButton("View Results", onBenchmarkHistoryClick)
viewResultsBtn.Importance = widget.LowImportance
logsBtn := widget.NewButton("Logs", onLogsClick)
logsBtn.Importance = widget.LowImportance
header := container.New(layout.NewHBoxLayout(), title, layout.NewSpacer(), sidebarToggleBtn, benchmarkBtn, viewResultsBtn, logsBtn, queueTile)
categorized := map[string][]fyne.CanvasObject{}
for i := range modules {
mod := modules[i] // Create new variable for this iteration
modID := mod.ID // Capture for closure
cat := mod.Category
if cat == "" {
cat = "General"
}
var tapFunc func()
var dropFunc func([]fyne.URI)
if mod.Enabled {
// Create new closure with properly captured modID
id := modID // Explicit capture
tapFunc = func() {
onModuleClick(id)
}
dropFunc = func(items []fyne.URI) {
logging.Debug(logging.CatUI, "MainMenu dropFunc called for module=%s itemCount=%d", id, len(items))
onModuleDrop(id, items)
}
}
logging.Debug(logging.CatUI, "Creating tile for module=%s enabled=%v hasDropFunc=%v", modID, mod.Enabled, dropFunc != nil)
categorized[cat] = append(categorized[cat], buildModuleTile(mod, tapFunc, dropFunc))
}
var sections []fyne.CanvasObject
for _, cat := range sortedKeys(categorized) {
sections = append(sections,
canvas.NewText(cat, textColor),
container.NewGridWithColumns(3, categorized[cat]...),
)
}
padding := canvas.NewRectangle(color.Transparent)
padding.SetMinSize(fyne.NewSize(0, 14))
body := container.New(layout.NewVBoxLayout(),
header,
padding,
container.NewVBox(sections...),
)
// Wrap with HSplit if sidebar is visible
if sidebarVisible && sidebar != nil {
split := container.NewHSplit(sidebar, body)
split.Offset = 0.2
return split
}
return body
}
// buildModuleTile creates a single module tile
func buildModuleTile(mod ModuleInfo, tapped func(), dropped func([]fyne.URI)) fyne.CanvasObject {
logging.Debug(logging.CatUI, "building tile %s color=%v enabled=%v", mod.ID, mod.Color, mod.Enabled)
return container.NewPadded(NewModuleTile(mod.Label, mod.Color, mod.Enabled, tapped, dropped))
}
// buildQueueTile creates the queue status tile
func buildQueueTile(completed, total int, queueColor, textColor color.Color, onClick func()) fyne.CanvasObject {
rect := canvas.NewRectangle(queueColor)
rect.CornerRadius = 8
rect.SetMinSize(fyne.NewSize(160, 60))
text := canvas.NewText(fmt.Sprintf("QUEUE: %d/%d", completed, total), textColor)
text.Alignment = fyne.TextAlignCenter
text.TextStyle = fyne.TextStyle{Monospace: true, Bold: true}
text.TextSize = 18
tile := container.NewMax(rect, container.NewCenter(text))
// Make it tappable
tappable := NewTappable(tile, onClick)
return tappable
}
// sortedKeys returns sorted keys for stable category ordering
func sortedKeys(m map[string][]fyne.CanvasObject) []string {
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}
// BuildHistorySidebar creates the history sidebar with tabs
func BuildHistorySidebar(
entries []HistoryEntry,
onEntryClick func(HistoryEntry),
onEntryDelete func(HistoryEntry),
titleColor, bgColor, textColor color.Color,
) fyne.CanvasObject {
// Filter by status
var completedEntries, failedEntries []HistoryEntry
for _, entry := range entries {
if entry.Status == queue.JobStatusCompleted {
completedEntries = append(completedEntries, entry)
} else {
failedEntries = append(failedEntries, entry)
}
}
// Build lists
completedList := buildHistoryList(completedEntries, onEntryClick, onEntryDelete, bgColor, textColor)
failedList := buildHistoryList(failedEntries, onEntryClick, onEntryDelete, bgColor, textColor)
// Tabs
tabs := container.NewAppTabs(
container.NewTabItem("Completed", container.NewVScroll(completedList)),
container.NewTabItem("Failed", container.NewVScroll(failedList)),
)
tabs.SetTabLocation(container.TabLocationTop)
// Header
title := canvas.NewText("HISTORY", titleColor)
title.TextStyle = fyne.TextStyle{Monospace: true, Bold: true}
title.TextSize = 18
header := container.NewVBox(
container.NewCenter(title),
widget.NewSeparator(),
)
return container.NewBorder(header, nil, nil, nil, tabs)
}
func buildHistoryList(
entries []HistoryEntry,
onEntryClick func(HistoryEntry),
onEntryDelete func(HistoryEntry),
bgColor, textColor color.Color,
) *fyne.Container {
if len(entries) == 0 {
return container.NewCenter(widget.NewLabel("No entries"))
}
var items []fyne.CanvasObject
for _, entry := range entries {
items = append(items, buildHistoryItem(entry, onEntryClick, onEntryDelete, bgColor, textColor))
}
return container.NewVBox(items...)
}
func buildHistoryItem(
entry HistoryEntry,
onEntryClick func(HistoryEntry),
onEntryDelete func(HistoryEntry),
bgColor, textColor color.Color,
) fyne.CanvasObject {
// Badge
badge := BuildModuleBadge(entry.Type)
// Capture entry for closures
capturedEntry := entry
// Delete button - small "×" button
deleteBtn := widget.NewButton("×", func() {
onEntryDelete(capturedEntry)
})
deleteBtn.Importance = widget.LowImportance
// Title
titleLabel := widget.NewLabel(utils.ShortenMiddle(entry.Title, 25))
titleLabel.TextStyle = fyne.TextStyle{Bold: true}
// Timestamp
timeStr := "Unknown"
if entry.CompletedAt != nil {
timeStr = entry.CompletedAt.Format("Jan 2, 15:04")
}
timeLabel := widget.NewLabel(timeStr)
timeLabel.TextStyle = fyne.TextStyle{Monospace: true}
// Status color bar
statusColor := GetStatusColor(entry.Status)
statusRect := canvas.NewRectangle(statusColor)
statusRect.SetMinSize(fyne.NewSize(4, 0))
content := container.NewBorder(
nil, nil, statusRect, nil,
container.NewVBox(
container.NewHBox(badge, layout.NewSpacer(), deleteBtn),
titleLabel,
timeLabel,
),
)
card := canvas.NewRectangle(bgColor)
card.CornerRadius = 4
item := container.NewPadded(container.NewMax(card, content))
return NewTappable(item, func() { onEntryClick(capturedEntry) })
}