1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
package main
import (
"fmt"
"os"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/widget"
)
const (
appId = "org.buetow.quicklogger"
placeholderText = "Enter text here..."
maxTextLength = 5000 // Limit text length to prevent performance issues
)
var (
defaultDirectory = "."
)
var windowSize = fyne.NewSize(400, 100)
func createPreferenceWindow(a fyne.App) fyne.Window {
window := a.NewWindow("Preferences")
directoryPreference := widget.NewEntry()
directoryPreference.SetText(a.Preferences().StringWithFallback("Directory", defaultDirectory))
window.SetContent(container.NewVBox(
container.NewVBox(
widget.NewLabel("Directory:"),
directoryPreference,
),
container.NewHBox(
widget.NewButton("Save", func() {
a.Preferences().SetString("Directory", directoryPreference.Text)
window.Hide()
}),
)))
window.Resize(windowSize)
return window
}
func createMainWindow(a fyne.App) fyne.Window {
// Create main window
window := a.NewWindow("Quick logger")
input := widget.NewMultiLineEntry()
// Optimization 1: Disable word wrapping on Android to improve performance
// Word wrapping causes expensive recalculations on every text change
if fyne.CurrentDevice().IsMobile() {
input.Wrapping = fyne.TextWrapOff
} else {
input.Wrapping = fyne.TextWrapWord
}
input.SetPlaceHolder(placeholderText)
// Optimization 2: Reduce visible rows on mobile to limit rendering area
if fyne.CurrentDevice().IsMobile() {
input.SetMinRowsVisible(10)
} else {
input.SetMinRowsVisible(30)
}
// Optimization 3: Add text length indicator
charCount := widget.NewLabel("0 chars")
// Optimization 4: Throttle text changes with validation
input.OnChanged = func(text string) {
// Update character count
charCount.SetText(fmt.Sprintf("%d chars", len(text)))
// Warn if text is getting too long
if len(text) > maxTextLength {
dialog.ShowInformation("Text Limit",
fmt.Sprintf("Text is getting long (%d chars). Consider logging to avoid performance issues.", len(text)),
window)
}
}
logTextButton := widget.NewButton("Log text", func() {
filename := fmt.Sprintf("%s/ql-%s.md",
a.Preferences().StringWithFallback("Directory", defaultDirectory),
time.Now().Format("060102-150405"),
)
if err := os.WriteFile(filename, []byte(input.Text), 0644); err != nil {
dialog.ShowError(err, window)
return
}
input.SetText("")
input.SetPlaceHolder(placeholderText)
// Reset character count
charCount.SetText("0 chars")
})
// Optimization 5: Add clear button for quick text clearing
clearButton := widget.NewButton("Clear", func() {
input.SetText("")
charCount.SetText("0 chars")
window.Canvas().Focus(input)
})
// If running on Android and shared text file exists, load it into the input
if fyne.CurrentDevice().IsMobile() {
if txt, err := readSharedFromCache(); err == nil && txt != "" {
input.SetText(txt)
charCount.SetText(fmt.Sprintf("%d chars", len(txt)))
}
}
window.SetContent(container.NewVBox(
input,
container.NewHBox(
logTextButton,
clearButton,
widget.NewButton("Preferences", func() {
createPreferenceWindow(a).Show()
}),
charCount, // Show character count
),
))
window.Resize(windowSize)
window.Canvas().Focus(input)
// On Android, also check for new shared text whenever app returns to foreground.
if lc := a.Lifecycle(); lc != nil {
lc.SetOnEnteredForeground(func() {
if txt, err := readSharedFromCache(); err == nil && txt != "" {
input.SetText(txt)
charCount.SetText(fmt.Sprintf("%d chars", len(txt)))
window.Canvas().Focus(input)
}
})
}
return window
}
func main() {
createMainWindow(app.NewWithID(appId)).ShowAndRun()
}
|