generate runtime waybar config and styles

parent 384be8f5
......@@ -9,6 +9,8 @@ user-facing configuration UI is expected to live in `ximperconf shell panel`.
- User config: `~/.config/ximper-shell/panel/config.json`
- User modules: `~/.config/ximper-shell/panel/modules.json`
- User style: `~/.config/ximper-shell/panel/style.css`
- User dark style: `~/.config/ximper-shell/panel/style-dark.css`
- System modules: `/usr/share/ximperdistro/wm/base/waybar/modules.json`
## Commands
......
......@@ -13,6 +13,8 @@ const (
SystemConfigHome = "/usr/share/ximperdistro/wm/base"
SystemWaybarDir = SystemConfigHome + "/waybar"
ModulesFile = SystemWaybarDir + "/modules.json"
StyleFile = SystemWaybarDir + "/style.css"
DarkStyleFile = SystemWaybarDir + "/style-dark.css"
)
type Config struct {
......@@ -81,16 +83,32 @@ func UserModulesPath() string {
return filepath.Join(os.Getenv("HOME"), ".config", "ximper-shell", "panel", "modules.json")
}
func UserStylePath() string {
return filepath.Join(os.Getenv("HOME"), ".config", "ximper-shell", "panel", "style.css")
}
func UserDarkStylePath() string {
return filepath.Join(os.Getenv("HOME"), ".config", "ximper-shell", "panel", "style-dark.css")
}
func RuntimeDir() string {
return filepath.Join(os.TempDir(), "ximper-shell", "panel")
}
func RuntimeWaybarDir() string {
return filepath.Join(RuntimeDir(), "waybar")
}
func RuntimeConfigPath() string {
return filepath.Join(RuntimeDir(), "config.jsonc")
return filepath.Join(RuntimeWaybarDir(), "config")
}
func RuntimeModulesPath() string {
return filepath.Join(RuntimeDir(), "modules.jsonc")
return filepath.Join(RuntimeWaybarDir(), "modules.jsonc")
}
func RuntimeStylePath(name string) string {
return filepath.Join(RuntimeWaybarDir(), name)
}
func RuntimePIDPath() string {
......
......@@ -69,6 +69,9 @@ func GenerateRuntimeConfig() ([]byte, error) {
if err := WriteRuntimeModules(registry); err != nil {
return nil, err
}
if err := WriteRuntimeStyles(); err != nil {
return nil, err
}
return GenerateConfig(cfg, registry)
}
......@@ -102,18 +105,45 @@ func WriteRuntimeModules(registry Registry) error {
return os.WriteFile(RuntimeModulesPath(), data, 0644)
}
func WriteRuntimeStyles() error {
if err := os.MkdirAll(RuntimeWaybarDir(), 0755); err != nil {
return err
}
if err := writeRuntimeStyle(RuntimeStylePath("style.css"), StyleFile, UserStylePath()); err != nil {
return err
}
userDarkStyle := UserStylePath()
if fileExists(UserDarkStylePath()) {
userDarkStyle = UserDarkStylePath()
}
return writeRuntimeStyle(RuntimeStylePath("style-dark.css"), DarkStyleFile, userDarkStyle)
}
func writeRuntimeStyle(runtimePath, systemPath, userPath string) error {
data := "@import url(\"" + systemPath + "\");\n"
if fileExists(userPath) {
data += "@import url(\"" + userPath + "\");\n"
}
return os.WriteFile(runtimePath, []byte(data), 0644)
}
func loadInputs() (Config, Registry, error) {
cfg, err := LoadConfig(UserConfigPath())
if err != nil {
return Config{}, Registry{}, err
}
registry, err := LoadMergedRegistry(ModulesFile, UserModulesPath())
registry, err := loadRegistry()
if err != nil {
return Config{}, Registry{}, err
}
return cfg, registry, nil
}
func loadRegistry() (Registry, error) {
return LoadMergedRegistry(ModulesFile, UserModulesPath())
}
func resolveModuleList(modules []string, position string, registry Registry) ([]string, error) {
resolved := make([]string, 0, len(modules))
for _, module := range modules {
......@@ -125,3 +155,8 @@ func resolveModuleList(modules []string, position string, registry Registry) ([]
}
return resolved, nil
}
func fileExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
......@@ -82,7 +82,7 @@ func applyCommandSetting(cliCommand *cli.Command) {
}
func listModulesCommand(ctx context.Context, cmd *cli.Command) error {
registry, err := LoadMergedRegistry(ModulesFile, UserModulesPath())
registry, err := loadRegistry()
if err != nil {
return err
}
......
......@@ -17,8 +17,7 @@ func StartWaybar() error {
return nil
}
configPath, err := GenerateAndWriteRuntimeConfig()
if err != nil {
if _, err := GenerateAndWriteRuntimeConfig(); err != nil {
return err
}
......@@ -28,8 +27,8 @@ func StartWaybar() error {
}
defer logFile.Close()
cmd := exec.Command("waybar", "-c", configPath)
cmd.Env = append(os.Environ(), "XDG_CONFIG_HOME="+SystemConfigHome)
cmd := exec.Command("waybar")
cmd.Env = append(os.Environ(), "XDG_CONFIG_HOME="+RuntimeDir())
cmd.Stdout = logFile
cmd.Stderr = logFile
cmd.Stdin = nil
......@@ -143,12 +142,18 @@ func isPanelProcess(pid int) bool {
}
hasWaybar := bytes.Contains([]byte(filepath.Base(string(parts[0]))), []byte("waybar"))
hasConfig := false
for _, part := range parts[1:] {
if string(part) == RuntimeConfigPath() {
hasConfig = true
break
return hasWaybar && hasPanelConfigHome(pid)
}
func hasPanelConfigHome(pid int) bool {
data, err := os.ReadFile(filepath.Join("/proc", strconv.Itoa(pid), "environ"))
if err != nil {
return false
}
for _, part := range bytes.Split(bytes.TrimRight(data, "\x00"), []byte{0}) {
if string(part) == "XDG_CONFIG_HOME="+RuntimeDir() {
return true
}
}
return hasWaybar && hasConfig
return false
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment