Skip to content

Commit

Permalink
added SetMode for windows
Browse files Browse the repository at this point in the history
  • Loading branch information
reujab committed Jun 30, 2021
1 parent b16dca6 commit 78f1ee5
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.16

require (
github.com/smartystreets/goconvey v1.6.4 // indirect
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a
gopkg.in/ini.v1 v1.62.0
gopkg.in/yaml.v2 v2.4.0
)
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIK
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
Expand Down
11 changes: 11 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ import (
"path/filepath"
)

type Mode int

const (
Center Mode = iota
Crop
Fit
Span
Stretch
Tile
)

// Desktop contains the current desktop environment on Linux.
// Empty string on all other operating systems.
var Desktop = os.Getenv("XDG_CURRENT_DESKTOP")
Expand Down
49 changes: 49 additions & 0 deletions windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package wallpaper

import (
"golang.org/x/sys/windows/registry"
"os"
"strings"
"syscall"
Expand Down Expand Up @@ -57,6 +58,54 @@ func SetFromFile(filename string) error {
return nil
}

// SetMode sets the wallpaper mode.
func SetMode(mode Mode) error {
key, _, err := registry.CreateKey(registry.CURRENT_USER, "Control Panel\\Desktop", registry.SET_VALUE)
if err != nil {
return err
}
defer key.Close()

var tile string
if mode == Tile {
tile = "1"
} else {
tile = "0"
}
err = key.SetStringValue("TileWallpaper", tile)
if err != nil {
return err
}

var style string
switch mode {
case Center, Tile:
style = "0"
case Fit:
style = "6"
case Span:
style = "22"
case Stretch:
style = "2"
case Crop:
style = "10"
default:
panic("invalid wallpaper mode")
}
err = key.SetStringValue("WallpaperStyle", style)
if err != nil {
return err
}

// updates wallpaper
path, err := Get()
if err != nil {
return err
}

return SetFromFile(path)
}

func getCacheDir() (string, error) {
return os.TempDir(), nil
}

0 comments on commit 78f1ee5

Please sign in to comment.