116 lines
2.7 KiB
Rust
116 lines
2.7 KiB
Rust
|
use std::{
|
||
|
error::Error,
|
||
|
fs::File,
|
||
|
io::{BufReader, Read},
|
||
|
num::NonZeroU32,
|
||
|
rc::Rc,
|
||
|
};
|
||
|
|
||
|
use ashpd::desktop::screenshot::Screenshot;
|
||
|
use image::{DynamicImage, GenericImageView, ImageReader};
|
||
|
use softbuffer::Surface;
|
||
|
use winit::{
|
||
|
application::ApplicationHandler,
|
||
|
dpi::Size,
|
||
|
event::WindowEvent,
|
||
|
event_loop::{self, ActiveEventLoop, ControlFlow, EventLoop},
|
||
|
monitor::{VideoMode, VideoModeHandle},
|
||
|
raw_window_handle::{DisplayHandle, HasDisplayHandle},
|
||
|
window::{Fullscreen, Window, WindowAttributes},
|
||
|
};
|
||
|
|
||
|
struct App {
|
||
|
window: Rc<Window>,
|
||
|
surface: Surface<Rc<Window>, Rc<Window>>,
|
||
|
image: DynamicImage,
|
||
|
}
|
||
|
|
||
|
impl App {
|
||
|
pub fn new(event_loop: &EventLoop<()>, image: DynamicImage) -> Self {
|
||
|
let window = Rc::new(
|
||
|
event_loop
|
||
|
.create_window(
|
||
|
Window::default_attributes()
|
||
|
.with_fullscreen(Some(Fullscreen::Borderless(None))),
|
||
|
)
|
||
|
.unwrap(),
|
||
|
);
|
||
|
|
||
|
let context = softbuffer::Context::new(window.clone()).unwrap();
|
||
|
let mut surface = softbuffer::Surface::new(&context, window.clone()).unwrap();
|
||
|
|
||
|
surface
|
||
|
.resize(
|
||
|
NonZeroU32::new(image.width()).unwrap(),
|
||
|
NonZeroU32::new(image.height()).unwrap(),
|
||
|
)
|
||
|
.unwrap();
|
||
|
|
||
|
Self {
|
||
|
window,
|
||
|
surface,
|
||
|
image,
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl ApplicationHandler for App {
|
||
|
fn resumed(&mut self, event_loop: &event_loop::ActiveEventLoop) {}
|
||
|
|
||
|
fn window_event(
|
||
|
&mut self,
|
||
|
event_loop: &event_loop::ActiveEventLoop,
|
||
|
window_id: winit::window::WindowId,
|
||
|
event: winit::event::WindowEvent,
|
||
|
) {
|
||
|
match event {
|
||
|
WindowEvent::CloseRequested => {
|
||
|
event_loop.exit();
|
||
|
}
|
||
|
WindowEvent::RedrawRequested => {
|
||
|
let mut buffer = self.surface.buffer_mut().unwrap();
|
||
|
let width = self.image.width() as usize;
|
||
|
for (x, y, pixel) in self.image.pixels() {
|
||
|
let red = pixel.0[0] as u32;
|
||
|
let green = pixel.0[1] as u32;
|
||
|
let blue = pixel.0[2] as u32;
|
||
|
|
||
|
let color = blue | (green << 8) | (red << 16);
|
||
|
buffer[y as usize * width + x as usize] = color;
|
||
|
}
|
||
|
|
||
|
buffer.present().unwrap();
|
||
|
|
||
|
// self.window.as_ref().request_redraw();
|
||
|
}
|
||
|
_ => (),
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#[tokio::main]
|
||
|
async fn main() -> Result<(), Box<dyn Error>> {
|
||
|
let response = Screenshot::request()
|
||
|
.interactive(false)
|
||
|
.modal(false)
|
||
|
.send()
|
||
|
.await
|
||
|
.expect("failed to send screenshot request")
|
||
|
.response()
|
||
|
.expect("failed to receive screenshot response");
|
||
|
|
||
|
// let path = response.uri().path();
|
||
|
let path = response.uri().to_file_path().unwrap();
|
||
|
let file = File::open(path).unwrap();
|
||
|
let buffer = BufReader::new(file);
|
||
|
// let image_reader = ImageReader::new(file);
|
||
|
let image = image::load(buffer, image::ImageFormat::Png).unwrap();
|
||
|
|
||
|
let event_loop = EventLoop::new()?;
|
||
|
event_loop.set_control_flow(ControlFlow::Poll);
|
||
|
|
||
|
let mut app = App::new(&event_loop, image);
|
||
|
|
||
|
Ok(event_loop.run_app(&mut app)?)
|
||
|
}
|