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

94 lines
2.9 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-29 22:54:51 +00:00
use glium::{glutin, Surface};
2022-10-28 02:15:47 +00:00
use glutin::event::{Event, WindowEvent};
2022-10-29 22:54:51 +00:00
use glutin::event_loop::{ControlFlow, EventLoop, EventLoopProxy, EventLoopWindowTarget};
2022-10-28 04:23:03 +00:00
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 {
2022-10-29 22:54:51 +00:00
OpenWindow { id: usize },
CloseWindow { id: usize },
2022-10-28 04:23:03 +00:00
Quit,
}
pub type WindowMessageSender = EventLoopProxy<WindowMessage>;
2022-10-28 02:15:47 +00:00
pub struct Window {
pub graphics: Graphics,
2022-10-28 04:23:03 +00:00
}
impl Window {
2022-10-29 22:54:51 +00:00
pub fn new(
event_loop: &EventLoopWindowTarget<WindowMessage>,
) -> Result<Self, DisplayCreationError> {
2022-10-28 04:23:03 +00:00
let wb = glutin::window::WindowBuilder::new();
let cb = glutin::ContextBuilder::new();
let display = glium::Display::new(wb, cb, &event_loop)?;
2022-10-29 23:23:56 +00:00
let graphics = Graphics::new(display);
2022-10-28 04:23:03 +00:00
2022-10-29 23:23:56 +00:00
Ok(Self { graphics })
}
pub fn get_id(&self) -> WindowId {
self.graphics.display.gl_window().window().id()
2022-10-28 04:23:03 +00:00
}
2022-10-29 22:54:51 +00:00
pub fn request_redraw(&mut self) {
2022-10-29 23:23:56 +00:00
self.graphics.display.gl_window().window().request_redraw();
2022-10-29 22:54:51 +00:00
}
2022-10-28 04:23:03 +00:00
}
pub struct WindowStore {
2022-10-28 04:52:42 +00:00
pub ipc_sender: IpcMessageSender,
2022-10-29 22:54:51 +00:00
pub ipc_to_window: HashMap<usize, WindowId>,
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,
2022-10-29 22:54:51 +00:00
ipc_to_window: Default::default(),
2022-10-28 04:23:03 +00:00
windows: Default::default(),
}
}
2022-10-29 22:54:51 +00:00
pub fn run(mut self, event_loop: EventLoop<WindowMessage>) -> ! {
event_loop.run(move |event, event_loop, control_flow| match event {
Event::WindowEvent { window_id, event } => {
if let Some(window) = self.windows.get_mut(&window_id) {
match event {
WindowEvent::Resized(_) => window.request_redraw(),
_ => {}
}
}
}
Event::RedrawRequested(id) => {
2022-10-29 23:23:56 +00:00
if let Some(window) = self.windows.get_mut(&id) {
window.graphics.draw(&[]);
2022-10-29 22:54:51 +00:00
}
2022-10-28 04:23:03 +00:00
}
2022-10-29 22:54:51 +00:00
Event::UserEvent(event) => match event {
WindowMessage::OpenWindow { id } => {
println!("Opening window {}", id);
let window = Window::new(&event_loop).unwrap();
2022-10-29 23:23:56 +00:00
let window_id = window.get_id();
2022-10-29 22:54:51 +00:00
self.windows.insert(window_id, window);
self.ipc_to_window.insert(id, window_id);
}
WindowMessage::CloseWindow { id } => {
if let Some(window_id) = self.ipc_to_window.remove(&id) {
self.windows.remove(&window_id);
}
}
WindowMessage::Quit => *control_flow = ControlFlow::Exit,
},
_ => {}
2022-10-28 04:23:03 +00:00
});
}
2022-10-28 02:15:47 +00:00
}