Skip to content

Commit 2a1baae

Browse files
Merge pull request #2786 from dovidmoishe/fix/windows-desktop-background-import
fix(desktop-gpui): import desktop wallpaper on windows
2 parents 715e7cc + 290084e commit 2a1baae

2 files changed

Lines changed: 53 additions & 22 deletions

File tree

‎apps/desktop-gpui/src/editor_sidebar.rs‎

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ pub const BACKGROUND_IMAGE_EXTENSIONS: [&str; 6] = ["jpg", "jpeg", "png", "gif",
196196
pub const CURRENT_DESKTOP_BACKGROUND_BASENAME: &str = "current-desktop-background";
197197
/// `recording.rs:92-93`.
198198
const DESKTOP_BACKGROUND_MAX_DIMENSION: u32 = 2560;
199+
#[cfg(target_os = "macos")]
199200
const DESKTOP_BACKGROUND_JPEG_QUALITY: u8 = 82;
200201

201202
/// The sidebar's fallback border/shadow objects. The UI's numbers, **not**
@@ -1953,30 +1954,33 @@ pub(crate) fn decode_wallpaper_thumbnail(id: &str) -> Option<Arc<RenderImage>> {
19531954
/// re-encoding to JPEG at quality 82. The `sips` path matters -- macOS ships
19541955
/// HEIC wallpapers the `image` crate cannot decode.
19551956
fn write_desktop_background_snapshot(source: &Path, output: &Path) -> Result<(), String> {
1956-
let needs_downscale = image::image_dimensions(source).is_ok_and(|(width, height)| {
1957-
width > DESKTOP_BACKGROUND_MAX_DIMENSION || height > DESKTOP_BACKGROUND_MAX_DIMENSION
1958-
});
1959-
1960-
let mut command = std::process::Command::new("sips");
1961-
command
1962-
.arg("-s")
1963-
.arg("format")
1964-
.arg("jpeg")
1965-
.arg("-s")
1966-
.arg("formatOptions")
1967-
.arg(DESKTOP_BACKGROUND_JPEG_QUALITY.to_string());
1968-
if needs_downscale {
1969-
command
1970-
.arg("-Z")
1971-
.arg(DESKTOP_BACKGROUND_MAX_DIMENSION.to_string());
1972-
}
1973-
if let Ok(output_status) = command.arg(source).arg("--out").arg(output).output()
1974-
&& output_status.status.success()
1957+
#[cfg(target_os = "macos")]
19751958
{
1976-
return Ok(());
1959+
let needs_downscale = image::image_dimensions(source).is_ok_and(|(width, height)| {
1960+
width > DESKTOP_BACKGROUND_MAX_DIMENSION || height > DESKTOP_BACKGROUND_MAX_DIMENSION
1961+
});
1962+
1963+
let mut command = std::process::Command::new("sips");
1964+
command
1965+
.arg("-s")
1966+
.arg("format")
1967+
.arg("jpeg")
1968+
.arg("-s")
1969+
.arg("formatOptions")
1970+
.arg(DESKTOP_BACKGROUND_JPEG_QUALITY.to_string());
1971+
if needs_downscale {
1972+
command
1973+
.arg("-Z")
1974+
.arg(DESKTOP_BACKGROUND_MAX_DIMENSION.to_string());
1975+
}
1976+
if let Ok(output_status) = command.arg(source).arg("--out").arg(output).output()
1977+
&& output_status.status.success()
1978+
{
1979+
return Ok(());
1980+
}
19771981
}
19781982

1979-
let decoded = cap_gpui_kernels::codec::open_image(source)
1983+
let decoded = cap_gpui_kernels::codec::decode_image_file(source)
19801984
.map_err(|err| format!("failed to decode desktop background: {err}"))?;
19811985
let decoded = if decoded.width() > DESKTOP_BACKGROUND_MAX_DIMENSION
19821986
|| decoded.height() > DESKTOP_BACKGROUND_MAX_DIMENSION
@@ -2426,6 +2430,13 @@ impl EditorWindow {
24262430
}
24272431
Err(error) => {
24282432
tracing::error!("couldn't import your desktop wallpaper: {error}");
2433+
cx.spawn(async move |_, _| {
2434+
crate::platform::alert_dialog(
2435+
"Couldn't import your desktop wallpaper",
2436+
&error,
2437+
);
2438+
})
2439+
.detach();
24292440
}
24302441
}
24312442
cx.notify();

‎apps/desktop-gpui/src/platform/windows.rs‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,27 @@ pub fn copy_image_bytes_to_clipboard(bytes: &[u8], _cx: &gpui::App) -> Result<()
660660
}
661661

662662
pub fn desktop_picture_path() -> Option<PathBuf> {
663-
None
663+
use std::ffi::OsString;
664+
use std::os::windows::ffi::OsStringExt;
665+
use windows_sys::Win32::UI::WindowsAndMessaging::{
666+
SPI_GETDESKWALLPAPER, SystemParametersInfoW,
667+
};
668+
669+
let mut buffer = vec![0u16; 32_768];
670+
let success = unsafe {
671+
SystemParametersInfoW(
672+
SPI_GETDESKWALLPAPER,
673+
buffer.len() as u32,
674+
buffer.as_mut_ptr().cast(),
675+
0,
676+
)
677+
};
678+
if success == 0 {
679+
tracing::warn!(error = %std::io::Error::last_os_error(), "failed to read desktop wallpaper path");
680+
return None;
681+
}
682+
let length = buffer.iter().position(|character| *character == 0)?;
683+
(length > 0).then(|| PathBuf::from(OsString::from_wide(&buffer[..length])))
664684
}
665685

666686
pub fn set_activation_policy(_regular: bool) -> bool {

0 commit comments

Comments
 (0)