2018-05-07 17:36:21 -04:00
|
|
|
extern crate image;
|
2019-02-23 20:59:00 -05:00
|
|
|
use std::path::Path;
|
2020-09-09 17:58:30 -08:00
|
|
|
|
|
|
|
use simple_logger::SimpleLogger;
|
2019-06-21 11:33:15 -04:00
|
|
|
use winit::{
|
|
|
|
event::Event,
|
|
|
|
event_loop::{ControlFlow, EventLoop},
|
|
|
|
window::{Icon, WindowBuilder},
|
|
|
|
};
|
2019-02-23 20:59:00 -05:00
|
|
|
|
2018-05-07 17:36:21 -04:00
|
|
|
fn main() {
|
2020-09-09 17:58:30 -08:00
|
|
|
SimpleLogger::new().init().unwrap();
|
2020-01-06 15:28:58 -05:00
|
|
|
|
2018-05-07 17:36:21 -04: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-23 20:59:00 -05:00
|
|
|
|
2019-06-27 17:59:13 +02:00
|
|
|
let icon = load_icon(Path::new(path));
|
2018-05-07 17:36:21 -04:00
|
|
|
|
2019-02-05 10:30:33 -05:00
|
|
|
let event_loop = EventLoop::new();
|
2018-05-07 17:36:21 -04:00
|
|
|
|
2019-02-05 10:30:33 -05:00
|
|
|
let window = WindowBuilder::new()
|
2018-05-07 17:36:21 -04: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-05 10:30:33 -05:00
|
|
|
.build(&event_loop)
|
2018-05-07 17:36:21 -04:00
|
|
|
.unwrap();
|
|
|
|
|
2019-02-05 10:30:33 -05:00
|
|
|
event_loop.run(move |event, _, control_flow| {
|
|
|
|
*control_flow = ControlFlow::Wait;
|
2020-01-05 02:12:03 -05:00
|
|
|
|
2019-02-05 10:30:33 -05:00
|
|
|
if let Event::WindowEvent { event, .. } = event {
|
|
|
|
use winit::event::WindowEvent::*;
|
2018-05-07 17:36:21 -04:00
|
|
|
match event {
|
2019-02-05 10:30:33 -05:00
|
|
|
CloseRequested => *control_flow = ControlFlow::Exit,
|
2018-05-07 17:36:21 -04:00
|
|
|
DroppedFile(path) => {
|
2019-02-23 20:59:00 -05:00
|
|
|
window.set_window_icon(Some(load_icon(&path)));
|
2019-06-24 12:14:55 -04:00
|
|
|
}
|
2018-05-07 17:36:21 -04:00
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-02-23 20:59:00 -05:00
|
|
|
fn load_icon(path: &Path) -> Icon {
|
|
|
|
let (icon_rgba, icon_width, icon_height) = {
|
2020-03-04 00:13:53 +01:00
|
|
|
let image = image::open(path)
|
|
|
|
.expect("Failed to open icon path")
|
|
|
|
.into_rgba();
|
2019-02-23 20:59:00 -05:00
|
|
|
let (width, height) = image.dimensions();
|
2020-03-04 00:13:53 +01:00
|
|
|
let rgba = image.into_raw();
|
2019-02-23 20:59:00 -05:00
|
|
|
(rgba, width, height)
|
|
|
|
};
|
|
|
|
Icon::from_rgba(icon_rgba, icon_width, icon_height).expect("Failed to open icon")
|
2018-05-07 17:36:21 -04:00
|
|
|
}
|