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, `\`, "/")
|
||||
|
||||
Reference in New Issue
Block a user