Attempt at making this work on windows systems too

master
Wynd 2024-11-18 11:26:06 +02:00
parent 2bc6898b15
commit 52b97bc36d
3 changed files with 115 additions and 19 deletions

79
Cargo.lock generated
View File

@ -985,7 +985,7 @@ dependencies = [
"log", "log",
"presser", "presser",
"thiserror", "thiserror",
"windows", "windows 0.58.0",
] ]
[[package]] [[package]]
@ -2985,8 +2985,8 @@ dependencies = [
"wasm-bindgen", "wasm-bindgen",
"web-sys", "web-sys",
"wgpu-types", "wgpu-types",
"windows", "windows 0.58.0",
"windows-core", "windows-core 0.58.0",
] ]
[[package]] [[package]]
@ -3000,6 +3000,15 @@ dependencies = [
"web-sys", "web-sys",
] ]
[[package]]
name = "win-screenshot"
version = "4.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1724fbfec1c2344f8da8d2da6b0f8371f009df3291399df8c7b575a8abd121b"
dependencies = [
"windows 0.57.0",
]
[[package]] [[package]]
name = "winapi" name = "winapi"
version = "0.3.9" version = "0.3.9"
@ -3031,13 +3040,35 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows"
version = "0.57.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "12342cb4d8e3b046f3d80effd474a7a02447231330ef77d71daa6fbc40681143"
dependencies = [
"windows-core 0.57.0",
"windows-targets 0.52.6",
]
[[package]] [[package]]
name = "windows" name = "windows"
version = "0.58.0" version = "0.58.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd04d41d93c4992d421894c18c8b43496aa748dd4c081bac0dc93eb0489272b6" checksum = "dd04d41d93c4992d421894c18c8b43496aa748dd4c081bac0dc93eb0489272b6"
dependencies = [ dependencies = [
"windows-core", "windows-core 0.58.0",
"windows-targets 0.52.6",
]
[[package]]
name = "windows-core"
version = "0.57.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2ed2439a290666cd67ecce2b0ffaad89c2a56b976b736e6ece670297897832d"
dependencies = [
"windows-implement 0.57.0",
"windows-interface 0.57.0",
"windows-result 0.1.2",
"windows-targets 0.52.6", "windows-targets 0.52.6",
] ]
@ -3047,13 +3078,24 @@ version = "0.58.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ba6d44ec8c2591c134257ce647b7ea6b20335bf6379a27dac5f1641fcf59f99" checksum = "6ba6d44ec8c2591c134257ce647b7ea6b20335bf6379a27dac5f1641fcf59f99"
dependencies = [ dependencies = [
"windows-implement", "windows-implement 0.58.0",
"windows-interface", "windows-interface 0.58.0",
"windows-result", "windows-result 0.2.0",
"windows-strings", "windows-strings",
"windows-targets 0.52.6", "windows-targets 0.52.6",
] ]
[[package]]
name = "windows-implement"
version = "0.57.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9107ddc059d5b6fbfbffdfa7a7fe3e22a226def0b2608f72e9d552763d3e1ad7"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "windows-implement" name = "windows-implement"
version = "0.58.0" version = "0.58.0"
@ -3065,6 +3107,17 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "windows-interface"
version = "0.57.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29bee4b38ea3cde66011baa44dba677c432a78593e202392d1e9070cf2a7fca7"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "windows-interface" name = "windows-interface"
version = "0.58.0" version = "0.58.0"
@ -3076,6 +3129,15 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "windows-result"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e383302e8ec8515204254685643de10811af0ed97ea37210dc26fb0032647f8"
dependencies = [
"windows-targets 0.52.6",
]
[[package]] [[package]]
name = "windows-result" name = "windows-result"
version = "0.2.0" version = "0.2.0"
@ -3091,7 +3153,7 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4cd9b125c486025df0eabcb585e62173c6c9eddcec5d117d3b6e8c30e2ee4d10" checksum = "4cd9b125c486025df0eabcb585e62173c6c9eddcec5d117d3b6e8c30e2ee4d10"
dependencies = [ dependencies = [
"windows-result", "windows-result 0.2.0",
"windows-targets 0.52.6", "windows-targets 0.52.6",
] ]
@ -3608,6 +3670,7 @@ dependencies = [
"image", "image",
"pollster", "pollster",
"wgpu", "wgpu",
"win-screenshot",
"winit", "winit",
] ]

View File

@ -13,7 +13,6 @@ path = "src/lib.rs"
unsafe_code = { level = "forbid" } unsafe_code = { level = "forbid" }
[dependencies] [dependencies]
ashpd = { version = "0.10", default-features = false, features = ["async-std"] }
winit = "0.30" winit = "0.30"
image = { version = "0.25", default-features = false, features = [ image = { version = "0.25", default-features = false, features = [
"png", "png",
@ -25,6 +24,12 @@ anyhow = "1.0"
pollster = "0.4" pollster = "0.4"
cgmath = "0.18" cgmath = "0.18"
[target.'cfg(target_os = "windows")'.dependencies]
win-screenshot = { version = "4.0" }
[target.'cfg(target_os = "linux")'.dependencies]
ashpd = { version = "0.10", default-features = false, features = ["async-std"] }
[profile.dev] [profile.dev]
codegen-backend = "cranelift" codegen-backend = "cranelift"

View File

@ -1,7 +1,15 @@
use std::{fs::File, io::BufReader, sync::Arc}; use std::{
fs::File,
io::BufReader,
path::{Path, PathBuf},
sync::Arc,
};
#[cfg(target_os = "linux")]
use ashpd::desktop::screenshot::Screenshot; use ashpd::desktop::screenshot::Screenshot;
use pollster::FutureExt; use pollster::FutureExt;
#[cfg(target_os = "windows")]
use win_screenshot::prelude::capture_display;
use winit::{ use winit::{
application::ApplicationHandler, application::ApplicationHandler,
event::WindowEvent, event::WindowEvent,
@ -18,16 +26,36 @@ pub struct App<'a> {
impl<'a> ApplicationHandler for App<'a> { impl<'a> ApplicationHandler for App<'a> {
fn resumed(&mut self, event_loop: &winit::event_loop::ActiveEventLoop) { fn resumed(&mut self, event_loop: &winit::event_loop::ActiveEventLoop) {
let response = Screenshot::request() let mut path: Option<PathBuf> = None;
.interactive(false)
.modal(false)
.send()
.block_on()
.expect("failed to send screenshot request")
.response()
.expect("failed to receive screenshot response");
let path = response.uri().to_file_path().unwrap(); #[cfg(target_os = "linux")]
{
let response = Screenshot::request()
.interactive(false)
.modal(false)
.send()
.block_on()
.expect("failed to send screenshot request")
.response()
.expect("failed to receive screenshot response");
path = Some(response.uri().to_file_path().unwrap());
}
#[cfg(target_os = "windows")]
{
let buf = capture_display().expect("failed to capture display");
let img = image::DynamicImage::ImageRgba8(
image::RgbaImage::from_raw(buf.width, buf.height, buf.pixels).unwrap(),
);
let img_path = [".", "screen.png"].iter().collect();
img.to_rgb8().save(&img_path).unwrap();
path = Some(img_path);
}
if path.is_none() {
panic!("Unable to capture screen");
}
let path = path.unwrap();
let file = File::open(&path).unwrap(); let file = File::open(&path).unwrap();
let buffer = BufReader::new(file); let buffer = BufReader::new(file);
let mut image = image::load(buffer, image::ImageFormat::Png).unwrap(); let mut image = image::load(buffer, image::ImageFormat::Png).unwrap();