Fix threading errors with proper initialization guard

The threading errors were caused by queue callbacks triggering showMainMenu()
during app initialization, before the Fyne event loop was fully ready.

Changes:
1. Added initComplete flag to appState struct
2. Queue callback returns early if !initComplete, preventing UI updates
   during initialization
3. Set initComplete=true AFTER ShowAndRun() would handle the event loop
4. Removed nested DoFromGoroutine() which was causing double-wrapping
5. Simplified setContent() to direct calls (no thread wrapping)
6. Callback properly marshals UI updates via DoFromGoroutine() after init

This ensures the queue callback only affects UI after the app is fully
initialized and the Fyne event loop is running.
This commit is contained in:
Stu Leak 2025-11-27 00:23:03 -05:00
parent b16b3439fb
commit 3ffb7c8a97

38
main.go
View File

@ -153,6 +153,7 @@ func (c convertConfig) CoverLabel() string {
type appState struct { type appState struct {
window fyne.Window window fyne.Window
active string active string
initComplete bool // True after initial UI setup completes
source *videoSource source *videoSource
loadedVideos []*videoSource // Multiple loaded videos for navigation loadedVideos []*videoSource // Multiple loaded videos for navigation
currentIndex int // Current video index in loadedVideos currentIndex int // Current video index in loadedVideos
@ -308,27 +309,12 @@ func (s *appState) applyInverseDefaults(src *videoSource) {
func (s *appState) setContent(body fyne.CanvasObject) { func (s *appState) setContent(body fyne.CanvasObject) {
bg := canvas.NewRectangle(backgroundColor) bg := canvas.NewRectangle(backgroundColor)
// Don't set a minimum size - let content determine layout naturally // Don't set a minimum size - let content determine layout naturally
// Always use DoFromGoroutine to ensure we're on the main thread
// This is safe even when already on the main thread
app := fyne.CurrentApp()
if app != nil && app.Driver() != nil {
app.Driver().DoFromGoroutine(func() {
if body == nil { if body == nil {
s.window.SetContent(bg) s.window.SetContent(bg)
} else { return
}
s.window.SetContent(container.NewMax(bg, body)) s.window.SetContent(container.NewMax(bg, body))
} }
}, false)
} else {
// Fallback if app not ready yet (during initialization)
if body == nil {
s.window.SetContent(bg)
} else {
s.window.SetContent(container.NewMax(bg, body))
}
}
}
// showErrorWithCopy displays an error dialog with a "Copy Error" button // showErrorWithCopy displays an error dialog with a "Copy Error" button
func (s *appState) showErrorWithCopy(title string, err error) { func (s *appState) showErrorWithCopy(title string, err error) {
@ -1096,12 +1082,14 @@ func runGUI() {
// Start queue processing (but paused by default) // Start queue processing (but paused by default)
state.jobQueue.Start() state.jobQueue.Start()
// Set callback AFTER showing the window to avoid threading issues during startup // Set callback - queue changes are triggered by job processor goroutine
// Use a goroutine with delay to ensure UI is fully initialized
go func() {
time.Sleep(100 * time.Millisecond)
state.jobQueue.SetChangeCallback(func() { state.jobQueue.SetChangeCallback(func() {
// Queue callbacks come from goroutines, so wrap UI calls // Skip updates during initialization
if !state.initComplete {
return
}
// Marshal UI updates to main thread
app := fyne.CurrentApp() app := fyne.CurrentApp()
if app == nil || app.Driver() == nil { if app == nil || app.Driver() == nil {
return return
@ -1111,13 +1099,15 @@ func runGUI() {
// Update stats bar // Update stats bar
state.updateStatsBar() state.updateStatsBar()
// Refresh UI when queue changes // Refresh UI when queue changes and we're on main menu
if state.active == "" { if state.active == "" {
state.showMainMenu() state.showMainMenu()
} }
}, false) }, false)
}) })
}()
// Mark initialization as complete
state.initComplete = true
defer state.shutdown() defer state.shutdown()
w.SetOnDropped(func(pos fyne.Position, items []fyne.URI) { w.SetOnDropped(func(pos fyne.Position, items []fyne.URI) {