canary-rs/apps/magpie/src/window.rs

63 lines
1.7 KiB
Rust
Raw Normal View History

2022-10-28 04:23:03 +00:00
use std::collections::HashMap;
use glium::backend::glutin::DisplayCreationError;
2022-10-28 02:15:47 +00:00
use glium::glutin;
use glutin::event::{Event, WindowEvent};
2022-10-28 04:23:03 +00:00
use glutin::event_loop::{ControlFlow, EventLoop, EventLoopProxy};
use glutin::window::WindowId;
2022-10-28 02:15:47 +00:00
use crate::gl::Graphics;
2022-10-28 04:52:42 +00:00
use crate::ipc::{IpcMessage, IpcMessageSender};
2022-10-28 04:23:03 +00:00
pub enum WindowMessage {
Quit,
}
pub type WindowMessageSender = EventLoopProxy<WindowMessage>;
2022-10-28 02:15:47 +00:00
pub struct Window {
pub display: glium::Display,
pub graphics: Graphics,
2022-10-28 04:23:03 +00:00
}
impl Window {
pub fn new(event_loop: &EventLoop<()>) -> Result<Self, DisplayCreationError> {
let wb = glutin::window::WindowBuilder::new();
let cb = glutin::ContextBuilder::new();
let display = glium::Display::new(wb, cb, &event_loop)?;
let graphics = Graphics::new(&display);
Ok(Self { display, graphics })
}
}
pub struct WindowStore {
2022-10-28 04:52:42 +00:00
pub ipc_sender: IpcMessageSender,
2022-10-28 04:23:03 +00:00
pub windows: HashMap<WindowId, Window>,
}
impl WindowStore {
2022-10-28 04:52:42 +00:00
pub fn new(ipc_sender: IpcMessageSender) -> Self {
2022-10-28 04:23:03 +00:00
Self {
ipc_sender,
windows: Default::default(),
}
}
pub fn run(self, event_loop: EventLoop<WindowMessage>) -> ! {
event_loop.run(move |event, event_loop, control_flow| {
*control_flow = ControlFlow::Wait;
2022-10-28 04:23:03 +00:00
match event {
Event::WindowEvent { window_id, event } => match event {
_ => {}
},
Event::MainEventsCleared => {}
Event::UserEvent(event) => match event {
WindowMessage::Quit => *control_flow = ControlFlow::Exit,
2022-10-28 04:52:42 +00:00
},
2022-10-28 04:23:03 +00:00
_ => {}
}
});
}
2022-10-28 02:15:47 +00:00
}