2018-05-08 07:36:21 +10:00
|
|
|
extern crate image;
|
2019-02-24 12:59:00 +11:00
|
|
|
use std::path::Path;
|
2019-06-22 01:33:15 +10:00
|
|
|
use winit::{
|
|
|
|
event::Event,
|
|
|
|
event_loop::{ControlFlow, EventLoop},
|
|
|
|
window::{Icon, WindowBuilder},
|
|
|
|
};
|
2019-02-24 12:59:00 +11:00
|
|
|
|
2018-05-08 07:36:21 +10:00
|
|
|
fn main() {
|
2020-01-07 07:28:58 +11:00
|
|
|
simple_logger::init().unwrap();
|
|
|
|
|
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
|
|
|
|
2019-02-06 02:30:33 +11: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| {
|
|
|
|
*control_flow = ControlFlow::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 {
|
2019-02-06 02:30:33 +11:00
|
|
|
CloseRequested => *control_flow = ControlFlow::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
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-02-24 12:59:00 +11:00
|
|
|
fn load_icon(path: &Path) -> Icon {
|
|
|
|
let (icon_rgba, icon_width, icon_height) = {
|
|
|
|
let image = image::open(path).expect("Failed to open icon path");
|
|
|
|
use image::{GenericImageView, Pixel};
|
|
|
|
let (width, height) = image.dimensions();
|
|
|
|
let mut rgba = Vec::with_capacity((width * height) as usize * 4);
|
|
|
|
for (_, _, pixel) in image.pixels() {
|
|
|
|
rgba.extend_from_slice(&pixel.to_rgba().data);
|
|
|
|
}
|
|
|
|
(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
|
|
|
}
|