pub struct DrawHandler { /* private fields */ }Expand description
Manager for drawing operations.
Implementations§
Source§impl DrawHandler
impl DrawHandler
Sourcepub fn new() -> Self
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 }Sourcepub fn new_with_drawing_area(drawing_area: DrawingArea) -> Self
pub fn new_with_drawing_area(drawing_area: DrawingArea) -> Self
Create a new DrawHandler with an existing gtk::DrawingArea.
Sourcepub fn get_context(&mut self) -> DrawContext
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 }Sourcepub fn size(&self) -> (i32, i32)
pub fn size(&self) -> (i32, i32)
Get the width and height of the DrawHandler in pixels.
Sourcepub fn height(&self) -> i32
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 }Sourcepub fn width(&self) -> i32
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 }Sourcepub fn surface_height(&self) -> i32
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.
Sourcepub fn surface_width(&self) -> i32
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.
Sourcepub fn drawing_area(&self) -> &DrawingArea
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
impl Debug for DrawHandler
Auto Trait Implementations§
impl Freeze for DrawHandler
impl !RefUnwindSafe for DrawHandler
impl !Send for DrawHandler
impl !Sync for DrawHandler
impl Unpin for DrawHandler
impl !UnwindSafe for DrawHandler
Blanket Implementations§
Source§impl<C> AsyncPosition<()> for C
impl<C> AsyncPosition<()> for C
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