pub struct CanvasState { /* private fields */ }Expand description
State for a Canvas component.
Contains the shapes to draw, coordinate bounds, and display options.
§Example
use envision::component::{CanvasState, CanvasShape, CanvasMarker};
use ratatui::style::Color;
let state = CanvasState::new()
.with_bounds(0.0, 200.0, 0.0, 100.0)
.with_title("Drawing")
.with_marker(CanvasMarker::HalfBlock)
.with_shapes(vec![
CanvasShape::Circle { x: 100.0, y: 50.0, radius: 25.0, color: Color::Cyan },
]);
assert_eq!(state.shapes().len(), 1);
assert_eq!(state.x_bounds(), [0.0, 200.0]);
assert_eq!(state.y_bounds(), [0.0, 100.0]);Implementations§
Source§impl CanvasState
impl CanvasState
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new empty canvas with default bounds [0, 100] on both axes.
§Example
use envision::component::CanvasState;
let state = CanvasState::new();
assert!(state.shapes().is_empty());
assert_eq!(state.x_bounds(), [0.0, 100.0]);
assert_eq!(state.y_bounds(), [0.0, 100.0]);Sourcepub fn with_shapes(self, shapes: Vec<CanvasShape>) -> Self
pub fn with_shapes(self, shapes: Vec<CanvasShape>) -> Self
Sets the initial shapes (builder pattern).
§Example
use envision::component::{CanvasState, CanvasShape};
use ratatui::style::Color;
let state = CanvasState::new().with_shapes(vec![
CanvasShape::Line { x1: 0.0, y1: 0.0, x2: 100.0, y2: 100.0, color: Color::Red },
]);
assert_eq!(state.shapes().len(), 1);Sourcepub fn with_x_bounds(self, min: f64, max: f64) -> Self
pub fn with_x_bounds(self, min: f64, max: f64) -> Self
Sets the x-axis range (builder pattern).
§Example
use envision::component::CanvasState;
let state = CanvasState::new().with_x_bounds(-50.0, 50.0);
assert_eq!(state.x_bounds(), [-50.0, 50.0]);Sourcepub fn with_y_bounds(self, min: f64, max: f64) -> Self
pub fn with_y_bounds(self, min: f64, max: f64) -> Self
Sets the y-axis range (builder pattern).
§Example
use envision::component::CanvasState;
let state = CanvasState::new().with_y_bounds(0.0, 200.0);
assert_eq!(state.y_bounds(), [0.0, 200.0]);Sourcepub fn with_bounds(self, x_min: f64, x_max: f64, y_min: f64, y_max: f64) -> Self
pub fn with_bounds(self, x_min: f64, x_max: f64, y_min: f64, y_max: f64) -> Self
Sets both x and y axis ranges (builder pattern).
§Example
use envision::component::CanvasState;
let state = CanvasState::new().with_bounds(0.0, 200.0, 0.0, 100.0);
assert_eq!(state.x_bounds(), [0.0, 200.0]);
assert_eq!(state.y_bounds(), [0.0, 100.0]);Sourcepub fn with_title(self, title: impl Into<String>) -> Self
pub fn with_title(self, title: impl Into<String>) -> Self
Sets the border title (builder pattern).
§Example
use envision::component::CanvasState;
let state = CanvasState::new().with_title("Drawing Surface");
assert_eq!(state.title(), Some("Drawing Surface"));Sourcepub fn with_marker(self, marker: CanvasMarker) -> Self
pub fn with_marker(self, marker: CanvasMarker) -> Self
Sets the marker type (builder pattern).
§Example
use envision::component::{CanvasState, CanvasMarker};
let state = CanvasState::new().with_marker(CanvasMarker::Block);
assert_eq!(state.marker(), &CanvasMarker::Block);Sourcepub fn shapes(&self) -> &[CanvasShape]
pub fn shapes(&self) -> &[CanvasShape]
Returns the shapes on the canvas.
§Example
use envision::component::{CanvasState, CanvasShape};
use ratatui::style::Color;
let state = CanvasState::new().with_shapes(vec![
CanvasShape::Circle { x: 50.0, y: 50.0, radius: 10.0, color: Color::Red },
]);
assert_eq!(state.shapes().len(), 1);Sourcepub fn add_shape(&mut self, shape: CanvasShape)
pub fn add_shape(&mut self, shape: CanvasShape)
Adds a shape to the canvas.
§Example
use envision::component::{CanvasState, CanvasShape};
use ratatui::style::Color;
let mut state = CanvasState::new();
state.add_shape(CanvasShape::Line {
x1: 0.0, y1: 0.0, x2: 100.0, y2: 100.0, color: Color::White,
});
assert_eq!(state.shapes().len(), 1);Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Removes all shapes from the canvas.
§Example
use envision::component::{CanvasState, CanvasShape};
use ratatui::style::Color;
let mut state = CanvasState::new().with_shapes(vec![
CanvasShape::Circle { x: 50.0, y: 50.0, radius: 10.0, color: Color::Red },
]);
state.clear();
assert!(state.shapes().is_empty());Sourcepub fn set_x_bounds(&mut self, min: f64, max: f64)
pub fn set_x_bounds(&mut self, min: f64, max: f64)
Sets the x-axis bounds.
§Example
use envision::component::CanvasState;
let mut state = CanvasState::new();
state.set_x_bounds(-100.0, 100.0);
assert_eq!(state.x_bounds(), [-100.0, 100.0]);Sourcepub fn set_y_bounds(&mut self, min: f64, max: f64)
pub fn set_y_bounds(&mut self, min: f64, max: f64)
Sets the y-axis bounds.
§Example
use envision::component::CanvasState;
let mut state = CanvasState::new();
state.set_y_bounds(-50.0, 50.0);
assert_eq!(state.y_bounds(), [-50.0, 50.0]);Sourcepub fn marker(&self) -> &CanvasMarker
pub fn marker(&self) -> &CanvasMarker
Returns the marker type.
Sourcepub fn set_marker(&mut self, marker: CanvasMarker)
pub fn set_marker(&mut self, marker: CanvasMarker)
Sets the marker type.
Sourcepub fn update(&mut self, msg: CanvasMessage) -> Option<()>
pub fn update(&mut self, msg: CanvasMessage) -> Option<()>
Updates the state with a message, returning any output.
Trait Implementations§
Source§impl Clone for CanvasState
impl Clone for CanvasState
Source§fn clone(&self) -> CanvasState
fn clone(&self) -> CanvasState
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for CanvasState
impl Debug for CanvasState
Source§impl Default for CanvasState
impl Default for CanvasState
Source§impl<'de> Deserialize<'de> for CanvasState
impl<'de> Deserialize<'de> for CanvasState
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Source§impl PartialEq for CanvasState
impl PartialEq for CanvasState
Source§impl Serialize for CanvasState
impl Serialize for CanvasState
impl StructuralPartialEq for CanvasState
Auto Trait Implementations§
impl Freeze for CanvasState
impl RefUnwindSafe for CanvasState
impl Send for CanvasState
impl Sync for CanvasState
impl Unpin for CanvasState
impl UnsafeUnpin for CanvasState
impl UnwindSafe for CanvasState
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more