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

252 lines
7.7 KiB
Rust
Raw Normal View History

// Copyright (c) 2022 Marceline Cramer
// SPDX-License-Identifier: AGPL-3.0-or-later
2022-10-28 04:23:03 +00:00
use std::collections::HashMap;
use std::path::PathBuf;
use std::time::Instant;
2022-10-28 04:23:03 +00:00
2022-11-04 01:07:40 +00:00
use canary::{CursorEventKind, Panel, Runtime, Vec2, PX_PER_MM};
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-11-04 01:07:40 +00:00
use glutin::event::{ElementState, Event, MouseButton, 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::service::gl::Graphics;
use crate::service::ipc::{IpcMessage, IpcMessageSender};
2022-10-28 04:23:03 +00:00
#[derive(Clone, Debug)]
2022-10-28 04:23:03 +00:00
pub enum WindowMessage {
2022-11-16 06:51:52 +00:00
OpenWindow {
id: usize,
protocol: String,
script: PathBuf,
},
CloseWindow {
id: usize,
},
2022-10-28 04:23:03 +00:00
Quit,
2022-11-16 06:51:52 +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 ipc_sender: IpcMessageSender,
pub ipc_id: usize,
2022-10-28 02:15:47 +00:00
pub graphics: Graphics,
2022-11-02 23:42:01 +00:00
pub panel: Panel,
pub last_update: Instant,
2022-11-04 01:07:40 +00:00
pub cursor_pos: Vec2,
pub cursor_down: bool,
2022-10-28 04:23:03 +00:00
}
impl Window {
2022-10-29 22:54:51 +00:00
pub fn new(
ipc_sender: IpcMessageSender,
ipc_id: usize,
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> {
let wb = glutin::window::WindowBuilder::new()
.with_transparent(true)
.with_decorations(false);
let cb = glutin::ContextBuilder::new()
.with_vsync(true)
.with_multisampling(4);
2022-10-28 04:23:03 +00:00
let display = glium::Display::new(wb, cb, &event_loop)?;
2022-10-29 23:23:56 +00:00
let graphics = Graphics::new(display);
let last_update = Instant::now();
Ok(Self {
ipc_sender,
ipc_id,
graphics,
panel,
last_update,
2022-11-04 01:07:40 +00:00
cursor_pos: Vec2::ZERO,
cursor_down: false,
})
2022-10-29 23:23:56 +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
/// Receives all messages from the script and forwards them to IPC.
pub fn recv_messages(&mut self) {
for message in self.panel.recv_messages() {
self.ipc_sender.send(IpcMessage::PanelMessage {
window: self.ipc_id,
message,
});
}
}
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-11-05 03:56:41 +00:00
self.last_update = now;
self.recv_messages();
}
pub fn draw(&mut self) {
2022-11-02 23:42:01 +00:00
let commands = self.panel.draw();
self.graphics.draw(&commands);
self.recv_messages();
}
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);
self.recv_messages();
2022-10-31 01:11:24 +00:00
}
2022-11-03 04:37:18 +00:00
pub fn resize(&mut self, new_size: Vec2) {
self.panel.on_resize(new_size);
self.recv_messages();
2022-11-03 04:37:18 +00:00
}
pub fn on_event(&mut self, event: WindowEvent) {
match event {
WindowEvent::Resized(size) => {
2022-11-04 01:07:40 +00:00
self.resize(Vec2::new(size.width as f32, size.height as f32) * PX_PER_MM);
self.request_redraw()
}
2022-11-04 01:07:40 +00:00
WindowEvent::CursorMoved { position, .. } => {
let x = position.x as f32 * PX_PER_MM;
let y = position.y as f32 * PX_PER_MM;
self.cursor_pos = Vec2::new(x, y);
let event = if self.cursor_down {
CursorEventKind::Drag
} else {
CursorEventKind::Hover
};
self.panel.on_cursor_event(event, self.cursor_pos);
self.recv_messages();
2022-11-04 01:07:40 +00:00
}
WindowEvent::MouseInput {
state,
button: MouseButton::Left,
..
} => {
let event = match state {
ElementState::Pressed => {
self.cursor_down = true;
CursorEventKind::Select
}
ElementState::Released => {
self.cursor_down = false;
CursorEventKind::Deselect
}
2022-11-04 01:07:40 +00:00
};
self.panel.on_cursor_event(event, self.cursor_pos);
self.recv_messages();
2022-11-04 01:07:40 +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()
}
pub fn on_message(
&mut self,
event_loop: &EventLoopWindowTarget<WindowMessage>,
message: WindowMessage,
) -> anyhow::Result<bool> {
match message {
WindowMessage::OpenWindow {
id,
protocol,
script,
} => {
println!("Opening window {} with script {:?}", id, script);
let module = std::fs::read(script)?;
let mut script = self.runtime.load_module(&module)?;
2022-11-16 06:51:52 +00:00
let panel = script.create_panel(&protocol, vec![])?;
let window = Window::new(self.ipc_sender.to_owned(), id, panel, &event_loop)?;
let window_id = window.get_id();
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 => return Ok(true),
WindowMessage::SendMessage { id, msg } => {
if let Some(window) = self.get_ipc_window(id) {
window.send_message(msg);
}
}
};
Ok(false)
}
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) {
window.on_event(event);
2022-10-29 22:54:51 +00:00
}
}
Event::RedrawRequested(id) => {
2022-10-29 23:23:56 +00:00
if let Some(window) = self.windows.get_mut(&id) {
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
}
Event::UserEvent(event) => match self.on_message(event_loop, event.clone()) {
Ok(false) => {}
Ok(true) => *control_flow = ControlFlow::Exit,
Err(err) => {
eprintln!("Error while handling message {:?}:\n{}", event, err);
2022-10-31 01:11:24 +00:00
}
2022-10-29 22:54:51 +00:00
},
_ => {}
2022-10-28 04:23:03 +00:00
});
}
2022-10-28 02:15:47 +00:00
}