Skip to content

Commit

Permalink
✨ Adding a way to set wallpaper for all displays
Browse files Browse the repository at this point in the history
  • Loading branch information
igorkulman committed Jan 17, 2021
1 parent 1afd96c commit 57c91d0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
28 changes: 18 additions & 10 deletions Sources/ChangeMenuBarColor/Commands/Abstract/Command.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,33 @@ import SwiftHEXColors

class Command {
func createWallpaper(screen: NSScreen) -> NSImage? {
return nil
fatalError("Override for each type")
}

var useAllDisplays: Bool {
fatalError("Override for each type")
}

func run() {
Log.info("Starting up")
Log.info("Starting up\n")

guard let screen = NSScreen.main else {
Log.error("Could not find the main screen")
return
}
let screens: [NSScreen] = useAllDisplays ? NSScreen.screens : [NSScreen.main].compactMap({ $0 })

guard let adjustedWallpaper = createWallpaper(screen: screen), let data = adjustedWallpaper.jpgData else {
Log.error("Could not generate new wallpaper fr the main screen")
guard !screens.isEmpty else {
Log.error("Could not detect any screens")
return
}

setWallpaper(screen: screen, wallpaper: data)
for (index, screen) in screens.enumerated() {
guard let adjustedWallpaper = createWallpaper(screen: screen), let data = adjustedWallpaper.jpgData else {
Log.error("Could not generate new wallpaper screen \(index)")
continue
}

setWallpaper(screen: screen, wallpaper: data)
}

Log.info("All done!")
Log.info("\nAll done!")
}

func loadWallpaperImage(wallpaper: String?, screen: NSScreen) -> NSImage? {
Expand Down
7 changes: 7 additions & 0 deletions Sources/ChangeMenuBarColor/Commands/Gradient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ final class Gradient: Command, ParsableCommand {
@Argument(help: "Wallpaper to use. If not provided the current macOS wallpaper will be used")
private var wallpaper: String?

@Flag(help: "Flag to set wallpaper for all displays not just the main display")
private var allDisplays: Bool = false

override var useAllDisplays: Bool {
return allDisplays
}

override func createWallpaper(screen: NSScreen) -> NSImage? {
guard let wallpaper = loadWallpaperImage(wallpaper: wallpaper, screen: screen) else {
return nil
Expand Down
7 changes: 7 additions & 0 deletions Sources/ChangeMenuBarColor/Commands/SolidColor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ final class SolidColor: Command, ParsableCommand {
@Argument(help: "Wallpaper to use. If not provided the current macOS wallpaper will be used")
private var wallpaper: String?

@Flag(help: "Flag to set wallpaper for all displays not just the main display")
private var allDisplays: Bool = false

override var useAllDisplays: Bool {
return allDisplays
}

override func createWallpaper(screen: NSScreen) -> NSImage? {
guard let wallpaper = loadWallpaperImage(wallpaper: wallpaper, screen: screen) else {
return nil
Expand Down

0 comments on commit 57c91d0

Please sign in to comment.