summaryrefslogtreecommitdiff
path: root/main.go
blob: 5a7d35a24756b36da959fb9adf08d9b8638c4969 (plain)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package main

import (
	"fmt"
	"os"
	"strings"
	"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 = "."
	defaultTagItems  = []string{
		"infra",
		"log",
		"share",
		"share:li",
		"share:ma",
		"share:no",
		"track",
		"track 10",
		"track 15",
		"track 20",
		"track 25",
		"track 30",
		"track 5",
		"work",
	}
	defaultWhatItems = []string{
		"Breathing",
		"Bulgarian",
		"Ema",
		"Exercise",
		"Meditation",
		"Music",
		"Reading Articles",
		"Reading Books",
		"Stretching",
		"Tech",
	}
)

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))

	tagDropdownPreference := widget.NewEntry()
	tagDropdownPreference.SetText(a.Preferences().StringWithFallback("Tags", strings.Join(defaultTagItems, ",")))

	whatDropdownPreference := widget.NewEntry()
	whatDropdownPreference.SetText(a.Preferences().StringWithFallback("Whats", strings.Join(defaultWhatItems, ",")))

	window.SetContent(container.NewVBox(
		container.NewVBox(
			widget.NewLabel("Directory:"),
			directoryPreference,
			widget.NewLabel("Tags:"),
			tagDropdownPreference,
			widget.NewLabel("Whats:"),
			whatDropdownPreference,
		),
		container.NewHBox(
			widget.NewButton("Save", func() {
				a.Preferences().SetString("Directory", directoryPreference.Text)
				a.Preferences().SetString("Tags", tagDropdownPreference.Text)
				a.Preferences().SetString("Whats", whatDropdownPreference.Text)
				window.Hide()
			}),
			widget.NewButton("Reset dropdowns", func() {
				// directoryPreference.SetText(defaultDirectory)
				tagDropdownPreference.SetText(strings.Join(defaultTagItems, ","))
				whatDropdownPreference.SetText(strings.Join(defaultWhatItems, ","))
			},
			),
		)))
	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)
		}
	}

	// Dropdown with pre-selectable items
	daysDropdown := widget.NewSelect([]string{"0", "1", "3", "7", "14", "30", "60", "99"}, func(selected string) {
		input.SetText(selected + " ")
		window.Canvas().Focus(input)
	})
	daysDropdown.PlaceHolder = "Days"

	tagDropdownItems := strings.Split(a.Preferences().StringWithFallback("Tags", strings.Join(defaultTagItems, ",")), ",")
	tagDropdown := widget.NewSelect(tagDropdownItems, func(selected string) {
		input.Append(selected + " ")
		window.Canvas().Focus(input)
	})
	tagDropdown.PlaceHolder = "Tag"

	whatDropdownItems := strings.Split(a.Preferences().StringWithFallback("Whats", strings.Join(defaultWhatItems, ",")), ",")
	whatDropdown := widget.NewSelect(whatDropdownItems, func(selected string) {
		input.Append(selected + " ")
		window.Canvas().Focus(input)
		input.Cursor()
	})
	whatDropdown.PlaceHolder = "What"

	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)
	})

	window.SetContent(container.NewVBox(
		container.NewHBox(daysDropdown, tagDropdown, whatDropdown, logTextButton),
		input,
		container.NewHBox(
			widget.NewButton("Preferences", func() {
				createPreferenceWindow(a).Show()
			}),
			clearButton,
			charCount, // Show character count
		),
	))
	window.Resize(windowSize)
	window.Canvas().Focus(input)

	return window
}

func main() {
	createMainWindow(app.NewWithID(appId)).ShowAndRun()
}