Forward script messages from window to IPC
This commit is contained in:
parent
808d62421c
commit
efae627263
|
@ -35,6 +35,8 @@ pub enum WindowMessage {
|
||||||
pub type WindowMessageSender = EventLoopProxy<WindowMessage>;
|
pub type WindowMessageSender = EventLoopProxy<WindowMessage>;
|
||||||
|
|
||||||
pub struct Window {
|
pub struct Window {
|
||||||
|
pub ipc_sender: IpcMessageSender,
|
||||||
|
pub ipc_id: usize,
|
||||||
pub graphics: Graphics,
|
pub graphics: Graphics,
|
||||||
pub panel: Panel,
|
pub panel: Panel,
|
||||||
pub last_update: Instant,
|
pub last_update: Instant,
|
||||||
|
@ -44,6 +46,8 @@ pub struct Window {
|
||||||
|
|
||||||
impl Window {
|
impl Window {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
|
ipc_sender: IpcMessageSender,
|
||||||
|
ipc_id: usize,
|
||||||
panel: Panel,
|
panel: Panel,
|
||||||
event_loop: &EventLoopWindowTarget<WindowMessage>,
|
event_loop: &EventLoopWindowTarget<WindowMessage>,
|
||||||
) -> Result<Self, DisplayCreationError> {
|
) -> Result<Self, DisplayCreationError> {
|
||||||
|
@ -53,6 +57,8 @@ impl Window {
|
||||||
let graphics = Graphics::new(display);
|
let graphics = Graphics::new(display);
|
||||||
let last_update = Instant::now();
|
let last_update = Instant::now();
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
|
ipc_sender,
|
||||||
|
ipc_id,
|
||||||
graphics,
|
graphics,
|
||||||
panel,
|
panel,
|
||||||
last_update,
|
last_update,
|
||||||
|
@ -69,24 +75,38 @@ impl Window {
|
||||||
self.graphics.display.gl_window().window().request_redraw();
|
self.graphics.display.gl_window().window().request_redraw();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 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) {
|
pub fn update(&mut self) {
|
||||||
let now = Instant::now();
|
let now = Instant::now();
|
||||||
let dt = now.duration_since(self.last_update).as_secs_f32();
|
let dt = now.duration_since(self.last_update).as_secs_f32();
|
||||||
self.panel.update(dt);
|
self.panel.update(dt);
|
||||||
self.last_update = now;
|
self.last_update = now;
|
||||||
|
self.recv_messages();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw(&mut self) {
|
pub fn draw(&mut self) {
|
||||||
let commands = self.panel.draw();
|
let commands = self.panel.draw();
|
||||||
self.graphics.draw(&commands);
|
self.graphics.draw(&commands);
|
||||||
|
self.recv_messages();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn send_message(&mut self, msg: Vec<u8>) {
|
pub fn send_message(&mut self, msg: Vec<u8>) {
|
||||||
self.panel.on_message(msg);
|
self.panel.on_message(msg);
|
||||||
|
self.recv_messages();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn resize(&mut self, new_size: Vec2) {
|
pub fn resize(&mut self, new_size: Vec2) {
|
||||||
self.panel.on_resize(new_size);
|
self.panel.on_resize(new_size);
|
||||||
|
self.recv_messages();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn on_event(&mut self, event: WindowEvent) {
|
pub fn on_event(&mut self, event: WindowEvent) {
|
||||||
|
@ -107,6 +127,7 @@ impl Window {
|
||||||
};
|
};
|
||||||
|
|
||||||
self.panel.on_cursor_event(event, self.cursor_pos);
|
self.panel.on_cursor_event(event, self.cursor_pos);
|
||||||
|
self.recv_messages();
|
||||||
}
|
}
|
||||||
WindowEvent::MouseInput {
|
WindowEvent::MouseInput {
|
||||||
state,
|
state,
|
||||||
|
@ -125,6 +146,7 @@ impl Window {
|
||||||
};
|
};
|
||||||
|
|
||||||
self.panel.on_cursor_event(event, self.cursor_pos);
|
self.panel.on_cursor_event(event, self.cursor_pos);
|
||||||
|
self.recv_messages();
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
@ -164,12 +186,16 @@ impl WindowStore {
|
||||||
message: WindowMessage,
|
message: WindowMessage,
|
||||||
) -> anyhow::Result<bool> {
|
) -> anyhow::Result<bool> {
|
||||||
match message {
|
match message {
|
||||||
WindowMessage::OpenWindow { id, protocol, script } => {
|
WindowMessage::OpenWindow {
|
||||||
|
id,
|
||||||
|
protocol,
|
||||||
|
script,
|
||||||
|
} => {
|
||||||
println!("Opening window {} with script {:?}", id, script);
|
println!("Opening window {} with script {:?}", id, script);
|
||||||
let module = std::fs::read(script)?;
|
let module = std::fs::read(script)?;
|
||||||
let mut script = self.runtime.load_module(&module)?;
|
let mut script = self.runtime.load_module(&module)?;
|
||||||
let panel = script.create_panel(&protocol, vec![])?;
|
let panel = script.create_panel(&protocol, vec![])?;
|
||||||
let window = Window::new(panel, &event_loop)?;
|
let window = Window::new(self.ipc_sender.to_owned(), id, panel, &event_loop)?;
|
||||||
let window_id = window.get_id();
|
let window_id = window.get_id();
|
||||||
self.windows.insert(window_id, window);
|
self.windows.insert(window_id, window);
|
||||||
self.ipc_to_window.insert(id, window_id);
|
self.ipc_to_window.insert(id, window_id);
|
||||||
|
|
Loading…
Reference in New Issue