1
0
mirror of https://github.com/AvengeMedia/DankMaterialShell.git synced 2026-05-13 07:42:46 -04:00

screenshot: respect XDG_PICTURES_DIR

fixes #2383
This commit is contained in:
bbedward
2026-05-11 10:00:21 -04:00
parent b192b5f779
commit 676219bc09
3 changed files with 38 additions and 39 deletions
+2 -34
View File
@@ -9,10 +9,8 @@ import (
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"time" "time"
"github.com/AvengeMedia/DankMaterialShell/core/internal/log"
"github.com/AvengeMedia/DankMaterialShell/core/internal/utils" "github.com/AvengeMedia/DankMaterialShell/core/internal/utils"
) )
@@ -105,7 +103,7 @@ func GetOutputDir() string {
return dir return dir
} }
if xdgPics := getXDGPicturesDir(); xdgPics != "" { if xdgPics := utils.XDGPicturesDir(); xdgPics != "" {
screenshotDir := filepath.Join(xdgPics, "Screenshots") screenshotDir := filepath.Join(xdgPics, "Screenshots")
if err := os.MkdirAll(screenshotDir, 0o755); err == nil { if err := os.MkdirAll(screenshotDir, 0o755); err == nil {
return screenshotDir return screenshotDir
@@ -113,42 +111,12 @@ func GetOutputDir() string {
return xdgPics return xdgPics
} }
if home := os.Getenv("HOME"); home != "" { if home, err := os.UserHomeDir(); err == nil {
return home return home
} }
return "." return "."
} }
func getXDGPicturesDir() string {
userConfigDir, err := os.UserConfigDir()
if err != nil {
log.Error("failed to get user config dir", "err", err)
return ""
}
userDirsFile := filepath.Join(userConfigDir, "user-dirs.dirs")
data, err := os.ReadFile(userDirsFile)
if err != nil {
return ""
}
for _, line := range strings.Split(string(data), "\n") {
if len(line) == 0 || line[0] == '#' {
continue
}
const prefix = "XDG_PICTURES_DIR="
if !strings.HasPrefix(line, prefix) {
continue
}
path := strings.Trim(line[len(prefix):], "\"")
expanded, err := utils.ExpandPath(path)
if err != nil {
return ""
}
return expanded
}
return ""
}
func WriteToFile(buf *ShmBuffer, path string, format Format, quality int) error { func WriteToFile(buf *ShmBuffer, path string, format Format, quality int) error {
return WriteToFileWithFormat(buf, path, format, quality, uint32(FormatARGB8888)) return WriteToFileWithFormat(buf, path, format, quality, uint32(FormatARGB8888))
} }
+6 -5
View File
@@ -64,12 +64,13 @@ func SendNotification(result NotifyResult) {
summary := "Screenshot captured" summary := "Screenshot captured"
body := "" body := ""
if result.Clipboard && result.FilePath != "" { switch {
body = fmt.Sprintf("Copied to clipboard\n%s", filepath.Base(result.FilePath)) case result.FilePath != "" && result.Clipboard:
} else if result.Clipboard { body = fmt.Sprintf("%s\nCopied to clipboard", filepath.Base(result.FilePath))
body = "Copied to clipboard" case result.FilePath != "":
} else if result.FilePath != "" {
body = filepath.Base(result.FilePath) body = filepath.Base(result.FilePath)
case result.Clipboard:
body = "Copied to clipboard"
} }
obj := conn.Object(notifyDest, notifyPath) obj := conn.Object(notifyDest, notifyPath)
+30
View File
@@ -38,6 +38,36 @@ func XDGConfigHome() string {
return filepath.Join(home, ".config") return filepath.Join(home, ".config")
} }
func XDGPicturesDir() string {
if dir := os.Getenv("XDG_PICTURES_DIR"); dir != "" {
if expanded, err := ExpandPath(dir); err == nil {
return expanded
}
}
data, err := os.ReadFile(filepath.Join(XDGConfigHome(), "user-dirs.dirs"))
if err != nil {
return ""
}
const prefix = "XDG_PICTURES_DIR="
for line := range strings.SplitSeq(string(data), "\n") {
if len(line) == 0 || line[0] == '#' {
continue
}
if !strings.HasPrefix(line, prefix) {
continue
}
path := strings.Trim(line[len(prefix):], "\"")
expanded, err := ExpandPath(path)
if err != nil {
return ""
}
return expanded
}
return ""
}
func EmacsConfigDir() string { func EmacsConfigDir() string {
home, _ := os.UserHomeDir() home, _ := os.UserHomeDir()