From 7c08fe44282c076e2470fe19a1eacff209cfa4f2 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Thu, 9 Apr 2026 08:38:33 +0300 Subject: Fix shared text cache cleanup --- main.go | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 56 insertions(+), 16 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index d913835..dd7fc9c 100644 --- a/main.go +++ b/main.go @@ -52,6 +52,44 @@ func prepareSharedTextLoad(text string, autoLog bool) (sharedTextLoadMode, strin return sharedTextLoadPrefill, text, true } +func clearSharedTextCache() { + if path := sharedTextCachePath(); path != "" { + _ = os.Remove(path) + } +} + +func handleSharedTextLoad( + text string, + autoLog bool, + dir string, + prefill func(string), + focus func(), + resetInput func(), + clearCache func(), + logFn func(string, string) error, + showInfo func(string, string), + showError func(error), +) { + mode, sharedText, ok := prepareSharedTextLoad(text, autoLog) + if !ok { + clearCache() + return + } + if mode == sharedTextLoadAutoLog { + if err := logFn(dir, sharedText); err != nil { + showError(err) + return + } + showInfo("Logged", "Shared text has been logged.") + resetInput() + clearCache() + return + } + prefill(sharedText) + focus() + clearCache() +} + // newInputWidget creates the multi-line text entry with platform-appropriate // wrapping and row count settings. func newInputWidget() *widget.Entry { @@ -152,27 +190,29 @@ func createMainWindow(a fyne.App) fyne.Window { } return } - mode, sharedText, ok := prepareSharedTextLoad(txt, a.Preferences().BoolWithFallback("AutoLogSharedText", false)) - if !ok { - return - } loadingSharedText = true defer func() { loadingSharedText = false }() - if mode == sharedTextLoadAutoLog { - dir := a.Preferences().StringWithFallback("Directory", defaultDirectory) - if err := logEntry(dir, sharedText); err != nil { + handleSharedTextLoad( + txt, + a.Preferences().BoolWithFallback("AutoLogSharedText", false), + a.Preferences().StringWithFallback("Directory", defaultDirectory), + input.SetText, + func() { + window.Canvas().Focus(input) + }, + resetInput, + clearSharedTextCache, + logEntry, + func(title, message string) { + dialog.ShowInformation(title, message, window) + }, + func(err error) { dialog.ShowError(err, window) - return - } - dialog.ShowInformation("Logged", "Shared text has been logged.", window) - resetInput() - return - } - input.SetText(sharedText) - charCount.SetText(fmt.Sprintf("%d chars", len(sharedText))) - window.Canvas().Focus(input) + }, + ) + charCount.SetText(fmt.Sprintf("%d chars", len(input.Text))) } if fyne.CurrentDevice().IsMobile() { -- cgit v1.2.3