Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions examples/runtest.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
//! This is NOT a real example. This is a test designed to see if we can actually run the turtle
//! process
// To run, use the command: cargo run --features unstable --example runtest
#[cfg(all(not(feature = "unstable")))]
compile_error!("This example relies on unstable features. Run with `--features unstable`");

use std::process;

use turtle::Turtle;
use turtle::Drawing;

fn main() {
let mut turtle = Turtle::new();
let mut drawing = Drawing::new();
let mut turtle = drawing.add_turtle();

turtle.set_speed(2);
turtle.right(90.0);
turtle.forward(50.0);

//TODO: Exiting the process currently doesn't cause the window to get closed. We should add a
// `close(self)` or `quit(self)` method to `Drawing` that closes the window explicitly.
process::exit(0);
drawing.destroy();
}
4 changes: 4 additions & 0 deletions src/async_drawing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,8 @@ impl AsyncDrawing {
pub async fn debug(&self) -> impl Debug {
self.client.debug_drawing().await
}

pub fn destroy(self) {
self.client.destroy();
}
}
26 changes: 26 additions & 0 deletions src/drawing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,32 @@ impl Drawing {
pub fn save_svg<P: AsRef<Path>>(&self, path: P) -> Result<(), ExportError> {
block_on(self.drawing.save_svg(path))
}

/// Destroys underlying window and drops self.
///
/// Subsequent commands to turtle, created using [`Drawing::add_turtle`], might panic.
///
/// ```rust
/// use turtle::Drawing;
///
/// let mut drawing = Drawing::new();
/// let mut turtle = drawing.add_turtle();
///
/// turtle.set_speed(2);
/// turtle.right(90.0);
/// turtle.forward(50.0);
///
/// // close window
/// drawing.destroy();
///
/// // this will panic!
/// // turtle.forward(100.0)
/// ```
#[cfg(feature = "unstable")]
#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
pub fn destroy(self) {
Comment thread
sunjay marked this conversation as resolved.
self.drawing.destroy();
}
}

#[cfg(test)]
Expand Down
5 changes: 5 additions & 0 deletions src/ipc_protocol/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ pub enum ClientRequest {
///
/// Response: `ServerResponse::DebugDrawing`
DebugDrawing,

/// Destroys drawing window.
///
/// Response: N/A
DestroyDrawing,
}

#[derive(Debug, Serialize, Deserialize)]
Expand Down
4 changes: 4 additions & 0 deletions src/ipc_protocol/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,4 +422,8 @@ impl ProtocolClient {
_ => unreachable!("bug: expected to receive `DebugDrawing` in response to `DebugDrawing` request"),
}
}

pub fn destroy(self) {
self.client.send(ClientRequest::DestroyDrawing);
}
}
4 changes: 4 additions & 0 deletions src/renderer_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ fn dispatch_request(
DebugDrawing => {
handlers::debug_drawing(conn, &app.read())
},

DestroyDrawing => {
handlers::destroy_drawing(event_loop)
},
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/renderer_server/event_loop_notifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ pub enum MainThreadAction {
SetIsMaximized(bool),
/// Change the fullscreen state of the window
SetIsFullscreen(bool),
/// Exit event loop (close window)
Exit,
}

/// Notifies the main loop when actions need to take place
Expand Down Expand Up @@ -60,6 +62,10 @@ impl EventLoopNotifier {
self.send_action(MainThreadAction::SetIsFullscreen(is_fullscreen))
}

pub fn exit(&self) -> Result<(), EventLoopClosed> {
self.send_action(MainThreadAction::Exit)
}

fn send_action(&self, action: MainThreadAction) -> Result<(), EventLoopClosed> {
Ok(self.event_loop.send_event(action)?)
}
Expand Down
2 changes: 2 additions & 0 deletions src/renderer_server/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod animation;
mod fill;
mod clear;
mod debug;
mod destroy_drawing;

pub(crate) use create_turtle::*;
pub(crate) use export_drawings::*;
Expand All @@ -17,6 +18,7 @@ pub(crate) use animation::*;
pub(crate) use fill::*;
pub(crate) use clear::*;
pub(crate) use debug::*;
pub(crate) use destroy_drawing::*;

use thiserror::Error;

Expand Down
7 changes: 7 additions & 0 deletions src/renderer_server/handlers/destroy_drawing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use super::super::event_loop_notifier::EventLoopNotifier;
use super::HandlerError;

pub(crate) fn destroy_drawing(event_loop: &EventLoopNotifier) -> Result<(), HandlerError> {
event_loop.exit()?;
Ok(())
}
4 changes: 4 additions & 0 deletions src/renderer_server/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ pub fn run_main(
} else { None });
},

GlutinEvent::UserEvent(MainThreadAction::Exit) => {
*control_flow = ControlFlow::Exit;
}

GlutinEvent::RedrawRequested(_) => {
// Check if we just rendered
let last_render_delay = last_render.elapsed();
Expand Down
4 changes: 4 additions & 0 deletions src/renderer_server/test_event_loop_notifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ impl EventLoopNotifier {
pub fn set_is_fullscreen(&self, _is_fullscreen: bool) -> Result<(), EventLoopClosed> {
Ok(())
}

pub fn exit(&self) -> Result<(), EventLoopClosed> {
Ok(())
}
}