feat: add tray layout controls
This commit is contained in:
@@ -200,6 +200,63 @@ func (c Config) LayoutFor(device string) (Layout, bool) {
|
||||
return Layout{}, false
|
||||
}
|
||||
|
||||
func (c Config) LayoutNames() []string {
|
||||
seen := make(map[string]bool)
|
||||
var names []string
|
||||
for _, l := range c.Layouts {
|
||||
name := strings.TrimSpace(l.Name)
|
||||
key := strings.ToLower(name)
|
||||
if name != "" && !seen[key] {
|
||||
seen[key] = true
|
||||
names = append(names, name)
|
||||
}
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
// SetActiveLayout updates only the top-level active_layout line, preserving the
|
||||
// user's comments, layout formatting, and ordering.
|
||||
func SetActiveLayout(path, name string) error {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
encoded, err := yaml.Marshal(strings.TrimSpace(name))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
value := strings.TrimSpace(string(encoded))
|
||||
newline := "\n"
|
||||
if strings.Contains(string(data), "\r\n") {
|
||||
newline = "\r\n"
|
||||
}
|
||||
lines := strings.Split(strings.ReplaceAll(string(data), "\r\n", "\n"), "\n")
|
||||
replaced := false
|
||||
for i, line := range lines {
|
||||
if strings.HasPrefix(line, "active_layout:") {
|
||||
lines[i] = "active_layout: " + value
|
||||
replaced = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !replaced {
|
||||
insertAt := 0
|
||||
for i, line := range lines {
|
||||
if strings.HasPrefix(line, "version:") || strings.HasPrefix(line, "gap:") || strings.HasPrefix(line, "shift_drag:") {
|
||||
insertAt = i + 1
|
||||
}
|
||||
}
|
||||
lines = append(lines, "")
|
||||
copy(lines[insertAt+1:], lines[insertAt:])
|
||||
lines[insertAt] = "active_layout: " + value
|
||||
}
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(path, []byte(strings.Join(lines, newline)), info.Mode().Perm())
|
||||
}
|
||||
|
||||
func (c Config) IsExcluded(exe string) bool {
|
||||
// Windows paths may be validated on another OS during cross-compilation.
|
||||
normalized := strings.ReplaceAll(exe, `\`, "/")
|
||||
|
||||
@@ -3,6 +3,7 @@ package config
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -64,6 +65,37 @@ func TestMissingActiveLayoutFails(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestLayoutNamesAreUniqueAndOrdered(t *testing.T) {
|
||||
c := Default()
|
||||
c.Layouts = append(c.Layouts,
|
||||
Layout{Name: "Columns", Monitor: `\\.\DISPLAY2`},
|
||||
Layout{Name: "Rows", Monitor: "*"},
|
||||
)
|
||||
got := c.LayoutNames()
|
||||
if len(got) != 2 || got[0] != "columns" || got[1] != "Rows" {
|
||||
t.Fatalf("unexpected names: %#v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetActiveLayoutPreservesConfiguration(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "fancywin.yaml")
|
||||
original := "# keep this comment\r\nversion: 1\r\nactive_layout: old # old value\r\nlayouts: []\r\n"
|
||||
if err := os.WriteFile(path, []byte(original), 0o640); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := SetActiveLayout(path, "new layout"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
text := string(data)
|
||||
if !strings.Contains(text, "# keep this comment\r\n") || !strings.Contains(text, "active_layout: new layout\r\n") || !strings.Contains(text, "layouts: []\r\n") {
|
||||
t.Fatalf("unexpected rewritten file: %q", text)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExcluded(t *testing.T) {
|
||||
c := Default()
|
||||
c.Excluded = []string{"notepad", "exact.exe"}
|
||||
|
||||
Reference in New Issue
Block a user