2022-10-28 04:23:03 +00:00
|
|
|
use std::collections::HashMap;
|
2022-10-30 00:00:46 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::time::Instant;
|
2022-10-28 04:23:03 +00:00
|
|
|
|
2022-11-02 23:42:01 +00:00
|
|
|
use canary::{Panel, Runtime};
|
2022-10-28 04:23:03 +00:00
|
|
|
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
|
|
|
|
2022-11-01 07:41:13 +00:00
|
|
|
use crate::service::gl::Graphics;
|
|
|
|
use crate::service::ipc::{IpcMessage, IpcMessageSender};
|
2022-10-28 04:23:03 +00:00
|
|
|
|
|
|
|
pub enum WindowMessage {
|
2022-10-30 00:00:46 +00:00
|
|
|
OpenWindow { id: usize, script: PathBuf },
|
2022-10-29 22:54:51 +00:00
|
|
|
CloseWindow { id: usize },
|
2022-10-28 04:23:03 +00:00
|
|
|
Quit,
|
2022-10-31 01:11:24 +00:00
|
|
|
SendMessage { id: usize, msg: Vec<u8> },
|
2022-10-28 04:23:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub type WindowMessageSender = EventLoopProxy<WindowMessage>;
|
2022-10-28 02:15:47 +00:00
|
|
|
|
|
|
|
pub struct Window {
|
|
|
|
pub graphics: Graphics,
|
2022-11-02 23:42:01 +00:00
|
|
|
pub panel: Panel,
|
2022-10-30 00:00:46 +00:00
|
|
|
pub last_update: Instant,
|
2022-10-28 04:23:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Window {
|
2022-10-29 22:54:51 +00:00
|
|
|
pub fn new(
|
2022-11-02 23:42:01 +00:00
|
|
|
panel: Panel,
|
2022-10-29 22:54:51 +00:00
|
|
|
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-30 00:00:46 +00:00
|
|
|
let last_update = Instant::now();
|
|
|
|
Ok(Self {
|
|
|
|
graphics,
|
|
|
|
panel,
|
|
|
|
last_update,
|
|
|
|
})
|
2022-10-29 23:23:56 +00:00
|
|
|
}
|
2022-10-30 00:00:46 +00:00
|
|
|
|
2022-10-29 23:23:56 +00:00
|
|
|
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-31 01:11:24 +00:00
|
|
|
|
2022-10-30 00:00:46 +00:00
|
|
|
pub fn update(&mut self) {
|
|
|
|
let now = Instant::now();
|
|
|
|
let dt = now.duration_since(self.last_update).as_secs_f32();
|
2022-11-02 23:42:01 +00:00
|
|
|
self.panel.update(dt);
|
2022-10-30 00:00:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn draw(&mut self) {
|
2022-11-02 23:42:01 +00:00
|
|
|
let commands = self.panel.draw();
|
|
|
|
self.graphics.draw(&commands);
|
2022-10-30 00:00:46 +00:00
|
|
|
}
|
2022-10-31 01:11:24 +00:00
|
|
|
|
|
|
|
pub fn send_message(&mut self, msg: Vec<u8>) {
|
2022-11-02 23:42:01 +00:00
|
|
|
self.panel.on_message(msg);
|
2022-10-31 01:11:24 +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>,
|
2022-11-02 23:42:01 +00:00
|
|
|
pub runtime: Runtime,
|
2022-10-28 04:23:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl WindowStore {
|
2022-10-28 04:52:42 +00:00
|
|
|
pub fn new(ipc_sender: IpcMessageSender) -> Self {
|
2022-11-02 23:42:01 +00:00
|
|
|
let backend = canary::backend::make_default_backend().unwrap();
|
|
|
|
let runtime = Runtime::new(backend).unwrap();
|
|
|
|
|
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-11-02 23:42:01 +00:00
|
|
|
runtime,
|
2022-10-28 04:23:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-31 01:11:24 +00:00
|
|
|
pub fn get_ipc_window(&mut self, id: usize) -> Option<&mut Window> {
|
|
|
|
self.ipc_to_window
|
|
|
|
.get(&id)
|
|
|
|
.map(|id| self.windows.get_mut(id))
|
|
|
|
.flatten()
|
|
|
|
}
|
|
|
|
|
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) {
|
2022-10-30 00:00:46 +00:00
|
|
|
window.draw();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Event::MainEventsCleared => {
|
|
|
|
for (_id, window) in self.windows.iter_mut() {
|
|
|
|
window.update();
|
|
|
|
window.request_redraw();
|
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 {
|
2022-10-30 00:00:46 +00:00
|
|
|
WindowMessage::OpenWindow { id, script } => {
|
|
|
|
println!("Opening window {} with script {:?}", id, script);
|
|
|
|
let module = std::fs::read(script).unwrap();
|
2022-11-02 23:42:01 +00:00
|
|
|
let mut script = self.runtime.load_module(&module).unwrap();
|
|
|
|
let panel = script.create_panel(vec![]).unwrap();
|
|
|
|
let window = Window::new(panel, &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-31 01:11:24 +00:00
|
|
|
WindowMessage::SendMessage { id, msg } => {
|
|
|
|
if let Some(window) = self.get_ipc_window(id) {
|
|
|
|
window.send_message(msg);
|
|
|
|
}
|
|
|
|
}
|
2022-10-29 22:54:51 +00:00
|
|
|
},
|
|
|
|
_ => {}
|
2022-10-28 04:23:03 +00:00
|
|
|
});
|
|
|
|
}
|
2022-10-28 02:15:47 +00:00
|
|
|
}
|