Skip to main content

CanvasState

Struct CanvasState 

Source
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

Source

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]);
Source

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);
Source

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]);
Source

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]);
Source

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]);
Source

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"));
Source

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);
Source

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);
Source

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);
Source

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());
Source

pub fn x_bounds(&self) -> [f64; 2]

Returns the x-axis bounds.

Source

pub fn y_bounds(&self) -> [f64; 2]

Returns the y-axis bounds.

Source

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]);
Source

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]);
Source

pub fn title(&self) -> Option<&str>

Returns the title.

Source

pub fn set_title(&mut self, title: Option<String>)

Sets the title.

Source

pub fn marker(&self) -> &CanvasMarker

Returns the marker type.

Source

pub fn set_marker(&mut self, marker: CanvasMarker)

Sets the marker type.

Source

pub fn update(&mut self, msg: CanvasMessage) -> Option<()>

Updates the state with a message, returning any output.

Trait Implementations§

Source§

impl Clone for CanvasState

Source§

fn clone(&self) -> CanvasState

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CanvasState

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for CanvasState

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for CanvasState

Source§

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

Source§

fn eq(&self, other: &CanvasState) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for CanvasState

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for CanvasState

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> StateExt for T

Source§

fn updated(self, cmd: Command<impl Clone>) -> UpdateResult<Self, impl Clone>

Updates self and returns a command.
Source§

fn unchanged(self) -> UpdateResult<Self, ()>

Returns self with no command.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,