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

65 lines
1.7 KiB
Rust

use super::*;
use std::collections::HashMap;
use glium::backend::glutin::DisplayCreationError;
use glium::glutin;
use glutin::event::{Event, WindowEvent};
use glutin::event_loop::{ControlFlow, EventLoop, EventLoopProxy};
use glutin::window::WindowId;
use crate::gl::Graphics;
use crate::ipc::IpcMessage;
pub enum WindowMessage {
Quit,
}
pub type WindowMessageSender = EventLoopProxy<WindowMessage>;
pub struct Window {
pub display: glium::Display,
pub graphics: Graphics,
}
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 {
pub ipc_sender: MioSender<IpcMessage>,
pub windows: HashMap<WindowId, Window>,
}
impl WindowStore {
pub fn new(ipc_sender: MioSender<IpcMessage>) -> Self {
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::Poll;
match event {
Event::WindowEvent { window_id, event } => match event {
_ => {}
},
Event::MainEventsCleared => {}
Event::UserEvent(event) => match event {
WindowMessage::Quit => *control_flow = ControlFlow::Exit,
}
_ => {}
}
});
}
}