Skip to content

Commit 5df5d67

Browse files
committed
feat: make timer text auto-resize with window
Replace fixed font size preference with dynamic auto-sizing: - Remove FontSize preference from settings - Create custom responsive renderer for TappableText - Text size automatically adjusts to fill available window space - Window is now resizable (removed SetFixedSize) - Binary search algorithm finds optimal font size to maximize text while fitting within bounds The timer text will now scale proportionally when the window is resized, always filling the available space with maximum readability.
1 parent 4c0949f commit 5df5d67

4 files changed

Lines changed: 65 additions & 15 deletions

File tree

pref/pref.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ type Pref struct {
1111
WorkRounds int
1212
StartMinimized bool
1313
ShowButtons bool
14-
FontSize int
1514
FontFamily string
1615
}
1716

@@ -24,7 +23,6 @@ func Load() Pref {
2423
WorkRounds: app.Preferences().IntWithFallback("workRounds", 4),
2524
StartMinimized: app.Preferences().BoolWithFallback("startMinimized", false),
2625
ShowButtons: app.Preferences().BoolWithFallback("showButtons", true),
27-
FontSize: app.Preferences().IntWithFallback("fontSize", 60),
2826
FontFamily: app.Preferences().StringWithFallback("fontFamily", ""),
2927
}
3028
}
@@ -37,6 +35,5 @@ func Save(pref Pref) {
3735
app.Preferences().SetInt("workRounds", pref.WorkRounds)
3836
app.Preferences().SetBool("startMinimized", pref.StartMinimized)
3937
app.Preferences().SetBool("showButtons", pref.ShowButtons)
40-
app.Preferences().SetInt("fontSize", pref.FontSize)
4138
app.Preferences().SetString("fontFamily", pref.FontFamily)
4239
}

ui/settings.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,6 @@ func makeForm() *widget.Form {
7979
startMinimizedCheck := widget.NewCheckWithData("Start minimized to tray", startMinimizedBinding)
8080
form.AppendItem(widget.NewFormItem("Startup", startMinimizedCheck))
8181

82-
fontSizeBinding := binding.NewInt()
83-
_ = fontSizeBinding.Set(myPref.FontSize)
84-
form.AppendItem(newIntegerFormItem(fontSizeBinding, "Font size", "Set the font size for the timer display. Default is: %d pixels.", NewRangeValidator(10, 200)))
85-
8682
showButtonsBinding := binding.NewBool()
8783
_ = showButtonsBinding.Set(myPref.ShowButtons)
8884
showButtonsCheck := widget.NewCheckWithData("Show control buttons", showButtonsBinding)
@@ -103,7 +99,6 @@ func makeForm() *widget.Form {
10399
longBreakDuration, _ := longBreakDurationBinding.Get()
104100
workRounds, _ := workRoundsBinding.Get()
105101
startMinimized, _ := startMinimizedBinding.Get()
106-
fontSize, _ := fontSizeBinding.Get()
107102
showButtons, _ := showButtonsBinding.Get()
108103
fontFamily, _ := fontFamilyBinding.Get()
109104

@@ -113,7 +108,6 @@ func makeForm() *widget.Form {
113108
LongBreakDuration: longBreakDuration,
114109
WorkRounds: workRounds,
115110
StartMinimized: startMinimized,
116-
FontSize: fontSize,
117111
ShowButtons: showButtons,
118112
FontFamily: fontFamily,
119113
}

ui/tappable_text.go

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,70 @@ func NewTappableText(title string, color color.Color, tapped func()) *TappableTe
2424
}
2525

2626
func (t *TappableText) CreateRenderer() fyne.WidgetRenderer {
27-
return widget.NewSimpleRenderer(t.Label)
27+
return &responsiveTextRenderer{
28+
text: t,
29+
}
30+
}
31+
32+
// responsiveTextRenderer adjusts text size to fill available space
33+
type responsiveTextRenderer struct {
34+
text *TappableText
35+
}
36+
37+
func (r *responsiveTextRenderer) Layout(size fyne.Size) {
38+
// Calculate optimal text size to fill the available space
39+
padding := float32(20.0)
40+
availableWidth := size.Width - padding
41+
availableHeight := size.Height - padding
42+
43+
if availableWidth <= 0 || availableHeight <= 0 {
44+
return
45+
}
46+
47+
// Binary search for optimal font size
48+
minSize := float32(10.0)
49+
maxSize := availableHeight
50+
bestSize := minSize
51+
52+
for i := 0; i < 15; i++ { // 15 iterations for good precision
53+
testSize := (minSize + maxSize) / 2
54+
r.text.Label.TextSize = testSize
55+
56+
textMinSize := r.text.Label.MinSize()
57+
58+
// Check if text fits within bounds
59+
if textMinSize.Width <= availableWidth && textMinSize.Height <= availableHeight {
60+
bestSize = testSize
61+
minSize = testSize // Try larger
62+
} else {
63+
maxSize = testSize // Too big, try smaller
64+
}
65+
}
66+
67+
r.text.Label.TextSize = bestSize
68+
69+
// Center the text in the available space
70+
textSize := r.text.Label.MinSize()
71+
x := (size.Width - textSize.Width) / 2
72+
y := (size.Height - textSize.Height) / 2
73+
r.text.Label.Move(fyne.NewPos(x, y))
74+
r.text.Label.Resize(textSize)
75+
}
76+
77+
func (r *responsiveTextRenderer) MinSize() fyne.Size {
78+
// Return a reasonable minimum size
79+
return fyne.NewSize(100, 50)
80+
}
81+
82+
func (r *responsiveTextRenderer) Refresh() {
83+
r.text.Label.Refresh()
84+
}
85+
86+
func (r *responsiveTextRenderer) Objects() []fyne.CanvasObject {
87+
return []fyne.CanvasObject{r.text.Label}
88+
}
89+
90+
func (r *responsiveTextRenderer) Destroy() {
2891
}
2992

3093
func (t *TappableText) Tapped(*fyne.PointEvent) {

ui/ui.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func Display(app fyne.App, buildInfo BuildInfo, cliStartMinimized bool) {
3232
mainWindow.SetMaster()
3333
mainWindow.SetContent(MakeClassicLayout(thePomodoro))
3434
mainWindow.SetCloseIntercept(mainWindow.Hide)
35-
mainWindow.SetFixedSize(true)
35+
mainWindow.Resize(fyne.NewSize(300, 200))
3636

3737
if desk, ok := app.(desktop.App); ok {
3838
aboutWindow := makeAboutWindow(app, buildInfo)
@@ -82,7 +82,6 @@ func MakeClassicLayout(thePomodoro *pomodoro.Pomodoro) fyne.CanvasObject {
8282
myPref := pref.Load()
8383

8484
timer := NewTappableText(formatDuration(thePomodoro.RemainingTime), nil, nil)
85-
timer.Label.TextSize = float32(myPref.FontSize)
8685
timer.Label.TextStyle.Bold = true
8786
timer.Label.Alignment = fyne.TextAlignCenter
8887
applyFontFamily(timer, myPref.FontFamily)
@@ -117,9 +116,6 @@ func MakeClassicLayout(thePomodoro *pomodoro.Pomodoro) fyne.CanvasObject {
117116
thePomodoro.SetWorkRounds(newPref.WorkRounds)
118117
thePomodoro.SetRemainingTime()
119118

120-
// Apply font size preference
121-
timer.Label.TextSize = float32(newPref.FontSize)
122-
123119
// Apply font family preference
124120
applyFontFamily(timer, newPref.FontFamily)
125121

0 commit comments

Comments
 (0)