1
0
mirror of https://github.com/AvengeMedia/DankMaterialShell.git synced 2026-05-11 14:59:38 -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

View File

@@ -9,10 +9,8 @@ import (
"io"
"os"
"path/filepath"
"strings"
"time"
"github.com/AvengeMedia/DankMaterialShell/core/internal/log"
"github.com/AvengeMedia/DankMaterialShell/core/internal/utils"
)
@@ -105,7 +103,7 @@ func GetOutputDir() string {
return dir
}
if xdgPics := getXDGPicturesDir(); xdgPics != "" {
if xdgPics := utils.XDGPicturesDir(); xdgPics != "" {
screenshotDir := filepath.Join(xdgPics, "Screenshots")
if err := os.MkdirAll(screenshotDir, 0o755); err == nil {
return screenshotDir
@@ -113,42 +111,12 @@ func GetOutputDir() string {
return xdgPics
}
if home := os.Getenv("HOME"); home != "" {
if home, err := os.UserHomeDir(); err == nil {
return home
}
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 {
return WriteToFileWithFormat(buf, path, format, quality, uint32(FormatARGB8888))
}

View File

@@ -64,12 +64,13 @@ func SendNotification(result NotifyResult) {
summary := "Screenshot captured"
body := ""
if result.Clipboard && result.FilePath != "" {
body = fmt.Sprintf("Copied to clipboard\n%s", filepath.Base(result.FilePath))
} else if result.Clipboard {
body = "Copied to clipboard"
} else if result.FilePath != "" {
switch {
case result.FilePath != "" && result.Clipboard:
body = fmt.Sprintf("%s\nCopied to clipboard", filepath.Base(result.FilePath))
case result.FilePath != "":
body = filepath.Base(result.FilePath)
case result.Clipboard:
body = "Copied to clipboard"
}
obj := conn.Object(notifyDest, notifyPath)

View File

@@ -38,6 +38,36 @@ func XDGConfigHome() string {
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 {
home, _ := os.UserHomeDir()