2022-06-10 20:43:33 +10:00
|
|
|
#![allow(clippy::single_match)]
|
|
|
|
|
2019-02-24 12:59:00 +11:00
|
|
|
use std::path::Path;
|
2020-09-10 11:58:30 +10:00
|
|
|
|
|
|
|
use simple_logger::SimpleLogger;
|
2019-06-22 01:33:15 +10:00
|
|
|
use winit::{
|
|
|
|
event::Event,
|
2022-04-10 11:32:02 +10:00
|
|
|
event_loop::EventLoop,
|
2019-06-22 01:33:15 +10:00
|
|
|
window::{Icon, WindowBuilder},
|
|
|
|
};
|
2019-02-24 12:59:00 +11:00
|
|
|
|
2023-06-20 04:46:38 +10:00
|
|
|
#[path = "util/fill.rs"]
|
|
|
|
mod fill;
|
|
|
|
|
2023-04-11 21:50:52 +10:00
|
|
|
fn main() -> Result<(), impl std::error::Error> {
|
2020-09-10 11:58:30 +10:00
|
|
|
SimpleLogger::new().init().unwrap();
|
2020-01-07 07:28:58 +11:00
|
|
|
|
2018-05-08 07:36:21 +10:00
|
|
|
// You'll have to choose an icon size at your own discretion. On X11, the desired size varies
|
|
|
|
// by WM, and on Windows, you still have to account for screen scaling. Here we use 32px,
|
|
|
|
// since it seems to work well enough in most cases. Be careful about going too high, or
|
|
|
|
// you'll be bitten by the low-quality downscaling built into the WM.
|
|
|
|
let path = concat!(env!("CARGO_MANIFEST_DIR"), "/examples/icon.png");
|
2019-02-24 12:59:00 +11:00
|
|
|
|
2019-06-28 01:59:13 +10:00
|
|
|
let icon = load_icon(Path::new(path));
|
2018-05-08 07:36:21 +10:00
|
|
|
|
2023-08-06 12:07:01 +10:00
|
|
|
let event_loop = EventLoop::new();
|
2018-05-08 07:36:21 +10:00
|
|
|
|
2019-02-06 02:30:33 +11:00
|
|
|
let window = WindowBuilder::new()
|
2018-05-08 07:36:21 +10:00
|
|
|
.with_title("An iconic window!")
|
|
|
|
// At present, this only does anything on Windows and X11, so if you want to save load
|
|
|
|
// time, you can put icon loading behind a function that returns `None` on other platforms.
|
|
|
|
.with_window_icon(Some(icon))
|
2019-02-06 02:30:33 +11:00
|
|
|
.build(&event_loop)
|
2018-05-08 07:36:21 +10:00
|
|
|
.unwrap();
|
|
|
|
|
2019-02-06 02:30:33 +11:00
|
|
|
event_loop.run(move |event, _, control_flow| {
|
2022-04-10 11:32:02 +10:00
|
|
|
control_flow.set_wait();
|
2020-01-05 18:12:03 +11:00
|
|
|
|
2019-02-06 02:30:33 +11:00
|
|
|
if let Event::WindowEvent { event, .. } = event {
|
|
|
|
use winit::event::WindowEvent::*;
|
2018-05-08 07:36:21 +10:00
|
|
|
match event {
|
2022-04-10 11:32:02 +10:00
|
|
|
CloseRequested => control_flow.set_exit(),
|
2018-05-08 07:36:21 +10:00
|
|
|
DroppedFile(path) => {
|
2019-02-24 12:59:00 +11:00
|
|
|
window.set_window_icon(Some(load_icon(&path)));
|
2019-06-25 02:14:55 +10:00
|
|
|
}
|
2018-05-08 07:36:21 +10:00
|
|
|
_ => (),
|
|
|
|
}
|
2023-06-20 04:46:38 +10:00
|
|
|
} else if let Event::RedrawRequested(_) = event {
|
|
|
|
fill::fill_window(&window);
|
2018-05-08 07:36:21 +10:00
|
|
|
}
|
2023-04-11 21:50:52 +10:00
|
|
|
})
|
2018-05-08 07:36:21 +10:00
|
|
|
}
|
|
|
|
|
2019-02-24 12:59:00 +11:00
|
|
|
fn load_icon(path: &Path) -> Icon {
|
|
|
|
let (icon_rgba, icon_width, icon_height) = {
|
2020-03-04 10:13:53 +11:00
|
|
|
let image = image::open(path)
|
|
|
|
.expect("Failed to open icon path")
|
2020-11-26 11:20:35 +11:00
|
|
|
.into_rgba8();
|
2019-02-24 12:59:00 +11:00
|
|
|
let (width, height) = image.dimensions();
|
2020-03-04 10:13:53 +11:00
|
|
|
let rgba = image.into_raw();
|
2019-02-24 12:59:00 +11:00
|
|
|
(rgba, width, height)
|
|
|
|
};
|
|
|
|
Icon::from_rgba(icon_rgba, icon_width, icon_height).expect("Failed to open icon")
|
2018-05-08 07:36:21 +10:00
|
|
|
}
|