DrawHandler

Struct DrawHandler 

Source
pub struct DrawHandler { /* private fields */ }
Expand description

Manager for drawing operations.

Implementations§

Source§

impl DrawHandler

Source

pub fn new() -> Self

Create a new DrawHandler.

Examples found in repository?
relm4/examples/drawing.rs (line 123)
114    fn init(
115        _: Self::Init,
116        root: Self::Root,
117        sender: ComponentSender<Self>,
118    ) -> ComponentParts<Self> {
119        let model = App {
120            width: 100.0,
121            height: 100.0,
122            points: Vec::new(),
123            handler: DrawHandler::new(),
124        };
125
126        let area = model.handler.drawing_area();
127        let widgets = view_output!();
128
129        sender.command(|out, shutdown| {
130            shutdown
131                .register(async move {
132                    loop {
133                        tokio::time::sleep(Duration::from_millis(20)).await;
134                        out.send(UpdatePointsMsg).unwrap();
135                    }
136                })
137                .drop_on_shutdown()
138        });
139
140        ComponentParts { model, widgets }
141    }
Source

pub fn new_with_drawing_area(drawing_area: DrawingArea) -> Self

Create a new DrawHandler with an existing gtk::DrawingArea.

Source

pub fn get_context(&mut self) -> DrawContext

Get the drawing context to draw on a gtk::DrawingArea. If the size of the gtk::DrawingArea changed, the contents of the surface will be replaced by a new, empty surface.

Examples found in repository?
relm4/examples/drawing.rs (line 70)
69    fn update(&mut self, msg: Msg, _sender: ComponentSender<Self>, _root: &Self::Root) {
70        let cx = self.handler.get_context();
71
72        match msg {
73            Msg::AddPoint((x, y)) => {
74                self.points.push(Point::new(x, y));
75            }
76            Msg::Resize => {
77                self.width = self.handler.width() as f64;
78                self.height = self.handler.height() as f64;
79            }
80            Msg::Reset => {
81                cx.set_operator(Operator::Clear);
82                cx.set_source_rgba(0.0, 0.0, 0.0, 0.0);
83                cx.paint().expect("Couldn't fill context");
84            }
85        }
86
87        draw(&cx, &self.points);
88    }
89
90    fn update_cmd(&mut self, _: UpdatePointsMsg, _: ComponentSender<Self>, _root: &Self::Root) {
91        for point in &mut self.points {
92            let Point { x, y, .. } = point;
93            if *x < 0.0 {
94                point.xs = point.xs.abs();
95            } else if *x > self.width {
96                point.xs = -point.xs.abs();
97            }
98            *x = x.clamp(0.0, self.width);
99            *x += point.xs;
100
101            if *y < 0.0 {
102                point.ys = point.ys.abs();
103            } else if *y > self.height {
104                point.ys = -point.ys.abs();
105            }
106            *y = y.clamp(0.0, self.height);
107            *y += point.ys;
108        }
109
110        let cx = self.handler.get_context();
111        draw(&cx, &self.points);
112    }
Source

pub fn size(&self) -> (i32, i32)

Get the width and height of the DrawHandler in pixels.

Source

pub fn height(&self) -> i32

Get the height of the DrawHandler in pixels.

Examples found in repository?
relm4/examples/drawing.rs (line 78)
69    fn update(&mut self, msg: Msg, _sender: ComponentSender<Self>, _root: &Self::Root) {
70        let cx = self.handler.get_context();
71
72        match msg {
73            Msg::AddPoint((x, y)) => {
74                self.points.push(Point::new(x, y));
75            }
76            Msg::Resize => {
77                self.width = self.handler.width() as f64;
78                self.height = self.handler.height() as f64;
79            }
80            Msg::Reset => {
81                cx.set_operator(Operator::Clear);
82                cx.set_source_rgba(0.0, 0.0, 0.0, 0.0);
83                cx.paint().expect("Couldn't fill context");
84            }
85        }
86
87        draw(&cx, &self.points);
88    }
Source

pub fn width(&self) -> i32

Get the width of the DrawHandler in pixels.

Examples found in repository?
relm4/examples/drawing.rs (line 77)
69    fn update(&mut self, msg: Msg, _sender: ComponentSender<Self>, _root: &Self::Root) {
70        let cx = self.handler.get_context();
71
72        match msg {
73            Msg::AddPoint((x, y)) => {
74                self.points.push(Point::new(x, y));
75            }
76            Msg::Resize => {
77                self.width = self.handler.width() as f64;
78                self.height = self.handler.height() as f64;
79            }
80            Msg::Reset => {
81                cx.set_operator(Operator::Clear);
82                cx.set_source_rgba(0.0, 0.0, 0.0, 0.0);
83                cx.paint().expect("Couldn't fill context");
84            }
85        }
86
87        draw(&cx, &self.points);
88    }
Source

pub fn surface_height(&self) -> i32

Get the height of the inner ImageSurface.

NOTE: Depending on the monitor scaling this is not necessarily the height in pixels.

Source

pub fn surface_width(&self) -> i32

Get the width of the inner ImageSurface.

NOTE: Depending on the monitor scaling this is not necessarily the width in pixels.

Source

pub fn drawing_area(&self) -> &DrawingArea

Get the gtk::DrawingArea of the DrawHandler.

Examples found in repository?
relm4/examples/drawing.rs (line 126)
114    fn init(
115        _: Self::Init,
116        root: Self::Root,
117        sender: ComponentSender<Self>,
118    ) -> ComponentParts<Self> {
119        let model = App {
120            width: 100.0,
121            height: 100.0,
122            points: Vec::new(),
123            handler: DrawHandler::new(),
124        };
125
126        let area = model.handler.drawing_area();
127        let widgets = view_output!();
128
129        sender.command(|out, shutdown| {
130            shutdown
131                .register(async move {
132                    loop {
133                        tokio::time::sleep(Duration::from_millis(20)).await;
134                        out.send(UpdatePointsMsg).unwrap();
135                    }
136                })
137                .drop_on_shutdown()
138        });
139
140        ComponentParts { model, widgets }
141    }

Trait Implementations§

Source§

impl Debug for DrawHandler

Source§

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

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

impl Default for DrawHandler

Source§

fn default() -> Self

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

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<C> AsyncPosition<()> for C

Source§

fn position(_index: usize)

Returns the position. 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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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<C, I> Position<(), I> for C

Source§

fn position(&self, _index: &I)

Returns the position. 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> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more