Add support for enabling a monospace editor font via an environment variable or configuration file.

This commit is contained in:
2026-07-05 01:14:48 +08:00
parent 9511143a72
commit 0fbc15ed4f
+30
View File
@@ -9,6 +9,7 @@ import (
"image/color" "image/color"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"fyne.io/fyne/v2" "fyne.io/fyne/v2"
"fyne.io/fyne/v2/app" "fyne.io/fyne/v2/app"
@@ -89,6 +90,31 @@ func isReadonly() bool {
return os.Getenv("READONLY") == "true" return os.Getenv("READONLY") == "true"
} }
// fixedPitchEnabled reports whether the user opted into a monospace
// (programmer) font. The value comes from the TINY_ENCRYPT_ENABLE_FIXED_PITCH
// environment variable; when that is unset it falls back to the file at
// ~/.config/envs/TINY_ENCRYPT_ENABLE_FIXED_PITCH (content trimmed). The values
// true/on/yes/1 are accepted, case-insensitively.
func fixedPitchEnabled() bool {
v := os.Getenv("TINY_ENCRYPT_ENABLE_FIXED_PITCH")
if v == "" {
home, err := os.UserHomeDir()
if err != nil {
return false
}
data, err := os.ReadFile(filepath.Join(home, ".config", "envs", "TINY_ENCRYPT_ENABLE_FIXED_PITCH"))
if err != nil {
return false
}
v = strings.TrimSpace(string(data))
}
switch strings.ToLower(v) {
case "true", "on", "yes", "1":
return true
}
return false
}
// windowConfig is persisted to ~/.cache/secure-editor-go-config.json so the // windowConfig is persisted to ~/.cache/secure-editor-go-config.json so the
// editor remembers its window size across runs. // editor remembers its window size across runs.
type windowConfig struct { type windowConfig struct {
@@ -246,6 +272,10 @@ func main() {
textEntry := widget.NewMultiLineEntry() textEntry := widget.NewMultiLineEntry()
textEntry.Wrapping = fyne.TextWrapWord textEntry.Wrapping = fyne.TextWrapWord
if fixedPitchEnabled() {
// Use Fyne's bundled monospace font for a programmer-style editor.
textEntry.TextStyle = fyne.TextStyle{Monospace: true}
}
initialText := string(plaintext) initialText := string(plaintext)
textEntry.SetText(initialText) textEntry.SetText(initialText)