Skip to content

Instantly share code, notes, and snippets.

@jongary
Last active April 16, 2023 00:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jongary/8ef8d89fd98824395eeb2d8303941925 to your computer and use it in GitHub Desktop.
Save jongary/8ef8d89fd98824395eeb2d8303941925 to your computer and use it in GitHub Desktop.
What I learned making Backdrop X

What I learned making Backdrop X

Introduction

I'm retired from the software development business, but I do like to tinker with the odd programming project.

Backdrop X (https://apps.apple.com/us/app/backdrop-x/id6446458434?mt=12) is a recreation of my 1990's era shareware application called Backdrop. When I wrote the original Backdrop for Macintosh System 6 with Multifinder, desktops just had a desktop pattern.

Fast forward to 2023, and Apple has added a bit of complexity to what is now called Wallpaper. Wallpapers evolve over the day. They automatically adapt to dark and light user interface appearances. You can shuffle through pictures and solid colors.

Features and scope of Backdrop X

Backdrop X creates a window the size of the screen, and draws the wallpaper in it. That might not seem very useful, but if your desktop is cluttered or reveals any sensitive information, you could use Backdrop X to obscure it in a video conference, preparing technical documents, or just to remove distractions.

If you find the Spaces feature of macOS awkward to use (as I do), Backdrop is a normal application, so you use typical multitasking commands to launch, quit, hide, and show it.

Backdrop X does not imitate the changeable nature of the macOS Wallpaper feature. This is intentional, as the use cases for it suggest a static background is better than an unpredictable one. For example, while creating a software manual, the background shouldn't change with the time of day.

Backdrop does allow you to choose which image within a particular wallpaper file if contains multiple images, and it can optionally chose light and dark images based on UI appearance automatically.

In the case you've selected either of the two Auto-rotate options for colors and Pictures, Backdrop X selects a random image from the respective folder. Make a choice, you monster! 😄

Technical approach

Wallpaper images and metadata

Wallpaper images come in a few flavors.

  • Single image in a file.
  • Two images in a file, one for light and dark appearances.
  • Multiple images in a file, with metadata that allows the system to change the image based on the position of the sun or the time of day.

Wallpapers are stored in HEIC files. HEIC can store multiple images within a single file, which is how Apple supplies so many variations for time, solar azimuth, and UI appearance.

Apple also stores custom metadata in the file to specify the system how to use each image. i have to thank Marcin Czachurski (https://github.com/mczachurski/wallpapper) and the other contributors to the wallpaper repo for the data types stored in the the metadata.

Determining the current wallpaper file.

NSWorkspace.shared.desktopImageURL yields the currently selected file, sort of. NSWorkspace.shared.desktopImageOptions looks like it will specify how to display the image, but it only provides an NSImageScaling mode and an allowsClipping flag, which doesn't cover the range of options in the Wallpaper system settings panel. I had to replicate the fill, fit, stretch, center, and tile options in the app itself.

The Wallpaper system setting panel also offers "Auto-rotate" for both solid colors and photos. When one of these options is used, the NSWorkspace.shared.desktopImageURL doesn't report the current image, but rather the parent directory of the respective collection of files.

Solid colors.

Solid colors are supplied as small image files. Except (and there's always an exception) if you choose a custom color. In that case you can obtain the color via NSWorkspace.shared.desktopImageOptions. OS supplied Solid colors in the Wallpaper panel are just reported as file URL.

Handling errors

While the system supplied images and pictures in the user's Pictures folder are accessible to Backdrop X, there are other image sources supported by the Wallpapers system settings panel. You can also chose damaged or unsupported images in the Wallpapers panel, and the system will fall back to a default image.

I handle the following by falling back to a default image:

  • Files inaccessible to Backdrop X sandbox.
  • Files that cannot be opened by Core Graphics API's due to damage or compatibility.

Drawing

I used a combination of view position and CoreImage operations depending on the display mode selected. In all cases, I use the color reported by NSWorkspace.shared.desktopImageOptions as the background color for the view to match the Wallpaper feature of the system.

  • Tiled mode is handled by setting the background color of the layer to NSColor(patternImage: desktopImage).cgColor and setting the NSImageView.image to nil.
  • Fill mode is handled by creating a scaled and cropped image the size of the screen using CoreImage.
  • Fit, Stretch, and Center can be handled by NSImageScaling modes.

Surprises

  • The auto-rotate option reporting a directory as the desktop image URL.
  • If you ever choose a custom color, it will stick and any selected desktop image with transparency will be drawn over that. I chose to replicate that quirk.
  • There is some secret sauce to positioning tiled images. The pattern does not start at the left edge of the screen. Someday I may try to replicate that.
  • Solid color files.

Security

It took one submission to TestFlight for me to discover that I didn't have access to the the wallpaper files supplied by the OS, downloaded by the user from the Wallpaper setting panel, or the user's Pictures folder. That required some additional entitlements.

Conclusion

It took just few minutes to create an app that splats a simple wallpaper image on the screen. Of course, the project escalated from an afternoon to occasional tinkering over several weeks. Way more than I expected.

That's software for you.

@jongary
Copy link
Author

jongary commented Apr 12, 2023

And... if you care, it's Swift/AppKit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment