pub struct DrawingContext<'a>(/* private fields */);Available on crate feature
winio only.Expand description
Canvas drawing context.
Implementations§
Source§impl<'a> DrawingContext<'a>
impl<'a> DrawingContext<'a>
Sourcepub fn set_transform(
&mut self,
transform: Transform2D<f64, LogicalSpace, LogicalSpace>,
) -> Result<(), Error>
pub fn set_transform( &mut self, transform: Transform2D<f64, LogicalSpace, LogicalSpace>, ) -> Result<(), Error>
Set the transform matrix.
Sourcepub fn transform(
&self,
) -> Result<Transform2D<f64, LogicalSpace, LogicalSpace>, Error>
pub fn transform( &self, ) -> Result<Transform2D<f64, LogicalSpace, LogicalSpace>, Error>
Get the transform matrix.
Sourcepub fn draw_path(
&mut self,
pen: impl Pen,
path: &DrawingPath,
) -> Result<(), Error>
pub fn draw_path( &mut self, pen: impl Pen, path: &DrawingPath, ) -> Result<(), Error>
Draw a path.
Examples found in repository?
examples/test/widgets.rs (line 328)
243 fn render(&mut self, _sender: &ComponentSender<Self>) -> Result<(), Self::Error> {
244 let csize = self.window.size()?;
245 {
246 let mut cred_panel = layout! {
247 Grid::from_str("auto,1*,auto", "1*,auto,auto,1*").unwrap(),
248 self.ulabel => { column: 0, row: 1, valign: VAlign::Center },
249 self.uentry => { column: 1, row: 1, margin: Margin::new_all_same(4.0) },
250 self.plabel => { column: 0, row: 2, valign: VAlign::Center },
251 self.pentry => { column: 1, row: 2, margin: Margin::new_all_same(4.0) },
252 self.pcheck => { column: 2, row: 2 },
253 };
254
255 let mut rgroup_panel = Grid::from_str("auto", "1*,auto,auto,auto,1*").unwrap();
256 for (i, rb) in self.radio_group.iter_mut().enumerate() {
257 rgroup_panel.push(rb).row(i + 1).finish();
258 }
259
260 let mut buttons_panel = layout! {
261 StackPanel::new(Orient::Vertical),
262 self.push_button => { margin: Margin::new_all_same(4.0) },
263 self.pop_button => { margin: Margin::new_all_same(4.0) },
264 self.show_button => { margin: Margin::new_all_same(4.0) },
265 };
266
267 let mut root_panel = layout! {
268 Grid::from_str("1*,1*,1*", "1*,auto,1*").unwrap(),
269 cred_panel => { column: 1, row: 0 },
270 rgroup_panel => { column: 2, row: 0, halign: HAlign::Center },
271 self.canvas => { column: 0, row: 1, row_span: 2 },
272 self.combo => { column: 1, row: 1, halign: HAlign::Center },
273 self.progress => { column: 2, row: 1 },
274 self.mltext => { column: 1, row: 2, margin: Margin::new_all_same(8.0) },
275 buttons_panel => { column: 2, row: 2 },
276 };
277
278 root_panel.set_size(csize);
279 }
280
281 let size = self.canvas.size()?;
282 let is_dark = ColorTheme::current()? == ColorTheme::Dark;
283 let back_color = if is_dark {
284 Color::new(255, 255, 255, 255)
285 } else {
286 Color::new(0, 0, 0, 255)
287 };
288 let brush = SolidColorBrush::new(back_color);
289 let pen = BrushPen::new(&brush, 1.0);
290 let mut ctx = self.canvas.context()?;
291 let cx = size.width / 2.0;
292 let cy = size.height / 2.0;
293 let r = cx.min(cy) - 2.0;
294 ctx.draw_pie(
295 &pen,
296 Rect::new(Point::new(cx - r, cy - r), Size::new(r * 2.0, r * 2.0)),
297 std::f64::consts::PI,
298 std::f64::consts::PI * 2.0,
299 );
300
301 let brush2 = LinearGradientBrush::new(
302 [
303 GradientStop::new(Color::new(0x87, 0xCE, 0xEB, 0xFF), 0.0),
304 GradientStop::new(back_color, 1.0),
305 ],
306 RelativePoint::zero(),
307 RelativePoint::new(0.0, 1.0),
308 );
309 let pen2 = BrushPen::new(&brush2, 1.0);
310 ctx.draw_round_rect(
311 &pen2,
312 Rect::new(
313 Point::new(cx - r - 1.0, cy - r - 1.0),
314 Size::new(r * 2.0 + 2.0, r * 1.618 + 2.0),
315 ),
316 Size::new(r / 10.0, r / 10.0),
317 );
318 let mut path = ctx.create_path_builder(Point::new(cx + r + 1.0 - r / 10.0, cy))?;
319 path.add_arc(
320 Point::new(cx, cy + r * 0.618 + 1.0),
321 Size::new(r + 1.0 - r / 10.0, r * 0.382 / 2.0),
322 0.0,
323 std::f64::consts::PI,
324 true,
325 );
326 path.add_line(Point::new(cx - r - 1.0 + r / 10.0, cy));
327 let path = path.build(false)?;
328 ctx.draw_path(&pen, &path);
329 let brush3 = RadialGradientBrush::new(
330 [
331 GradientStop::new(Color::new(0xF5, 0xF5, 0xF5, 0xFF), 0.0),
332 GradientStop::new(
333 Color::accent().unwrap_or(Color::new(0xFF, 0xC0, 0xCB, 0xFF)),
334 1.0,
335 ),
336 ],
337 RelativePoint::new(0.5, 0.5),
338 RelativePoint::new(0.2, 0.5),
339 RelativeSize::new(0.5, 0.5),
340 );
341 let font = DrawingFontBuilder::new()
342 .family("Arial")
343 .size(r / 5.0)
344 .halign(HAlign::Center)
345 .valign(VAlign::Bottom)
346 .build();
347 ctx.draw_str(&brush3, font, Point::new(cx, cy), "Hello world!");
348 Ok(())
349 }Sourcepub fn fill_path(
&mut self,
brush: impl Brush,
path: &DrawingPath,
) -> Result<(), Error>
pub fn fill_path( &mut self, brush: impl Brush, path: &DrawingPath, ) -> Result<(), Error>
Fill a path.
Sourcepub fn draw_arc(
&mut self,
pen: impl Pen,
rect: Rect<f64, LogicalSpace>,
start: f64,
end: f64,
) -> Result<(), Error>
pub fn draw_arc( &mut self, pen: impl Pen, rect: Rect<f64, LogicalSpace>, start: f64, end: f64, ) -> Result<(), Error>
Draw an arc.
Sourcepub fn draw_pie(
&mut self,
pen: impl Pen,
rect: Rect<f64, LogicalSpace>,
start: f64,
end: f64,
) -> Result<(), Error>
pub fn draw_pie( &mut self, pen: impl Pen, rect: Rect<f64, LogicalSpace>, start: f64, end: f64, ) -> Result<(), Error>
Draw an arc.
Examples found in repository?
examples/test/widgets.rs (lines 294-299)
243 fn render(&mut self, _sender: &ComponentSender<Self>) -> Result<(), Self::Error> {
244 let csize = self.window.size()?;
245 {
246 let mut cred_panel = layout! {
247 Grid::from_str("auto,1*,auto", "1*,auto,auto,1*").unwrap(),
248 self.ulabel => { column: 0, row: 1, valign: VAlign::Center },
249 self.uentry => { column: 1, row: 1, margin: Margin::new_all_same(4.0) },
250 self.plabel => { column: 0, row: 2, valign: VAlign::Center },
251 self.pentry => { column: 1, row: 2, margin: Margin::new_all_same(4.0) },
252 self.pcheck => { column: 2, row: 2 },
253 };
254
255 let mut rgroup_panel = Grid::from_str("auto", "1*,auto,auto,auto,1*").unwrap();
256 for (i, rb) in self.radio_group.iter_mut().enumerate() {
257 rgroup_panel.push(rb).row(i + 1).finish();
258 }
259
260 let mut buttons_panel = layout! {
261 StackPanel::new(Orient::Vertical),
262 self.push_button => { margin: Margin::new_all_same(4.0) },
263 self.pop_button => { margin: Margin::new_all_same(4.0) },
264 self.show_button => { margin: Margin::new_all_same(4.0) },
265 };
266
267 let mut root_panel = layout! {
268 Grid::from_str("1*,1*,1*", "1*,auto,1*").unwrap(),
269 cred_panel => { column: 1, row: 0 },
270 rgroup_panel => { column: 2, row: 0, halign: HAlign::Center },
271 self.canvas => { column: 0, row: 1, row_span: 2 },
272 self.combo => { column: 1, row: 1, halign: HAlign::Center },
273 self.progress => { column: 2, row: 1 },
274 self.mltext => { column: 1, row: 2, margin: Margin::new_all_same(8.0) },
275 buttons_panel => { column: 2, row: 2 },
276 };
277
278 root_panel.set_size(csize);
279 }
280
281 let size = self.canvas.size()?;
282 let is_dark = ColorTheme::current()? == ColorTheme::Dark;
283 let back_color = if is_dark {
284 Color::new(255, 255, 255, 255)
285 } else {
286 Color::new(0, 0, 0, 255)
287 };
288 let brush = SolidColorBrush::new(back_color);
289 let pen = BrushPen::new(&brush, 1.0);
290 let mut ctx = self.canvas.context()?;
291 let cx = size.width / 2.0;
292 let cy = size.height / 2.0;
293 let r = cx.min(cy) - 2.0;
294 ctx.draw_pie(
295 &pen,
296 Rect::new(Point::new(cx - r, cy - r), Size::new(r * 2.0, r * 2.0)),
297 std::f64::consts::PI,
298 std::f64::consts::PI * 2.0,
299 );
300
301 let brush2 = LinearGradientBrush::new(
302 [
303 GradientStop::new(Color::new(0x87, 0xCE, 0xEB, 0xFF), 0.0),
304 GradientStop::new(back_color, 1.0),
305 ],
306 RelativePoint::zero(),
307 RelativePoint::new(0.0, 1.0),
308 );
309 let pen2 = BrushPen::new(&brush2, 1.0);
310 ctx.draw_round_rect(
311 &pen2,
312 Rect::new(
313 Point::new(cx - r - 1.0, cy - r - 1.0),
314 Size::new(r * 2.0 + 2.0, r * 1.618 + 2.0),
315 ),
316 Size::new(r / 10.0, r / 10.0),
317 );
318 let mut path = ctx.create_path_builder(Point::new(cx + r + 1.0 - r / 10.0, cy))?;
319 path.add_arc(
320 Point::new(cx, cy + r * 0.618 + 1.0),
321 Size::new(r + 1.0 - r / 10.0, r * 0.382 / 2.0),
322 0.0,
323 std::f64::consts::PI,
324 true,
325 );
326 path.add_line(Point::new(cx - r - 1.0 + r / 10.0, cy));
327 let path = path.build(false)?;
328 ctx.draw_path(&pen, &path);
329 let brush3 = RadialGradientBrush::new(
330 [
331 GradientStop::new(Color::new(0xF5, 0xF5, 0xF5, 0xFF), 0.0),
332 GradientStop::new(
333 Color::accent().unwrap_or(Color::new(0xFF, 0xC0, 0xCB, 0xFF)),
334 1.0,
335 ),
336 ],
337 RelativePoint::new(0.5, 0.5),
338 RelativePoint::new(0.2, 0.5),
339 RelativeSize::new(0.5, 0.5),
340 );
341 let font = DrawingFontBuilder::new()
342 .family("Arial")
343 .size(r / 5.0)
344 .halign(HAlign::Center)
345 .valign(VAlign::Bottom)
346 .build();
347 ctx.draw_str(&brush3, font, Point::new(cx, cy), "Hello world!");
348 Ok(())
349 }Sourcepub fn fill_pie(
&mut self,
brush: impl Brush,
rect: Rect<f64, LogicalSpace>,
start: f64,
end: f64,
) -> Result<(), Error>
pub fn fill_pie( &mut self, brush: impl Brush, rect: Rect<f64, LogicalSpace>, start: f64, end: f64, ) -> Result<(), Error>
Fill a pie.
Sourcepub fn draw_ellipse(
&mut self,
pen: impl Pen,
rect: Rect<f64, LogicalSpace>,
) -> Result<(), Error>
pub fn draw_ellipse( &mut self, pen: impl Pen, rect: Rect<f64, LogicalSpace>, ) -> Result<(), Error>
Draw an ellipse.
Sourcepub fn fill_ellipse(
&mut self,
brush: impl Brush,
rect: Rect<f64, LogicalSpace>,
) -> Result<(), Error>
pub fn fill_ellipse( &mut self, brush: impl Brush, rect: Rect<f64, LogicalSpace>, ) -> Result<(), Error>
Fill an ellipse.
Sourcepub fn draw_line(
&mut self,
pen: impl Pen,
start: Point2D<f64, LogicalSpace>,
end: Point2D<f64, LogicalSpace>,
) -> Result<(), Error>
pub fn draw_line( &mut self, pen: impl Pen, start: Point2D<f64, LogicalSpace>, end: Point2D<f64, LogicalSpace>, ) -> Result<(), Error>
Draw a line.
Sourcepub fn draw_rect(
&mut self,
pen: impl Pen,
rect: Rect<f64, LogicalSpace>,
) -> Result<(), Error>
pub fn draw_rect( &mut self, pen: impl Pen, rect: Rect<f64, LogicalSpace>, ) -> Result<(), Error>
Draw a rectangle.
Sourcepub fn fill_rect(
&mut self,
brush: impl Brush,
rect: Rect<f64, LogicalSpace>,
) -> Result<(), Error>
pub fn fill_rect( &mut self, brush: impl Brush, rect: Rect<f64, LogicalSpace>, ) -> Result<(), Error>
Fill a rectangle.
Sourcepub fn draw_round_rect(
&mut self,
pen: impl Pen,
rect: Rect<f64, LogicalSpace>,
round: Size2D<f64, LogicalSpace>,
) -> Result<(), Error>
pub fn draw_round_rect( &mut self, pen: impl Pen, rect: Rect<f64, LogicalSpace>, round: Size2D<f64, LogicalSpace>, ) -> Result<(), Error>
Draw a rounded rectangle.
Examples found in repository?
examples/test/widgets.rs (lines 310-317)
243 fn render(&mut self, _sender: &ComponentSender<Self>) -> Result<(), Self::Error> {
244 let csize = self.window.size()?;
245 {
246 let mut cred_panel = layout! {
247 Grid::from_str("auto,1*,auto", "1*,auto,auto,1*").unwrap(),
248 self.ulabel => { column: 0, row: 1, valign: VAlign::Center },
249 self.uentry => { column: 1, row: 1, margin: Margin::new_all_same(4.0) },
250 self.plabel => { column: 0, row: 2, valign: VAlign::Center },
251 self.pentry => { column: 1, row: 2, margin: Margin::new_all_same(4.0) },
252 self.pcheck => { column: 2, row: 2 },
253 };
254
255 let mut rgroup_panel = Grid::from_str("auto", "1*,auto,auto,auto,1*").unwrap();
256 for (i, rb) in self.radio_group.iter_mut().enumerate() {
257 rgroup_panel.push(rb).row(i + 1).finish();
258 }
259
260 let mut buttons_panel = layout! {
261 StackPanel::new(Orient::Vertical),
262 self.push_button => { margin: Margin::new_all_same(4.0) },
263 self.pop_button => { margin: Margin::new_all_same(4.0) },
264 self.show_button => { margin: Margin::new_all_same(4.0) },
265 };
266
267 let mut root_panel = layout! {
268 Grid::from_str("1*,1*,1*", "1*,auto,1*").unwrap(),
269 cred_panel => { column: 1, row: 0 },
270 rgroup_panel => { column: 2, row: 0, halign: HAlign::Center },
271 self.canvas => { column: 0, row: 1, row_span: 2 },
272 self.combo => { column: 1, row: 1, halign: HAlign::Center },
273 self.progress => { column: 2, row: 1 },
274 self.mltext => { column: 1, row: 2, margin: Margin::new_all_same(8.0) },
275 buttons_panel => { column: 2, row: 2 },
276 };
277
278 root_panel.set_size(csize);
279 }
280
281 let size = self.canvas.size()?;
282 let is_dark = ColorTheme::current()? == ColorTheme::Dark;
283 let back_color = if is_dark {
284 Color::new(255, 255, 255, 255)
285 } else {
286 Color::new(0, 0, 0, 255)
287 };
288 let brush = SolidColorBrush::new(back_color);
289 let pen = BrushPen::new(&brush, 1.0);
290 let mut ctx = self.canvas.context()?;
291 let cx = size.width / 2.0;
292 let cy = size.height / 2.0;
293 let r = cx.min(cy) - 2.0;
294 ctx.draw_pie(
295 &pen,
296 Rect::new(Point::new(cx - r, cy - r), Size::new(r * 2.0, r * 2.0)),
297 std::f64::consts::PI,
298 std::f64::consts::PI * 2.0,
299 );
300
301 let brush2 = LinearGradientBrush::new(
302 [
303 GradientStop::new(Color::new(0x87, 0xCE, 0xEB, 0xFF), 0.0),
304 GradientStop::new(back_color, 1.0),
305 ],
306 RelativePoint::zero(),
307 RelativePoint::new(0.0, 1.0),
308 );
309 let pen2 = BrushPen::new(&brush2, 1.0);
310 ctx.draw_round_rect(
311 &pen2,
312 Rect::new(
313 Point::new(cx - r - 1.0, cy - r - 1.0),
314 Size::new(r * 2.0 + 2.0, r * 1.618 + 2.0),
315 ),
316 Size::new(r / 10.0, r / 10.0),
317 );
318 let mut path = ctx.create_path_builder(Point::new(cx + r + 1.0 - r / 10.0, cy))?;
319 path.add_arc(
320 Point::new(cx, cy + r * 0.618 + 1.0),
321 Size::new(r + 1.0 - r / 10.0, r * 0.382 / 2.0),
322 0.0,
323 std::f64::consts::PI,
324 true,
325 );
326 path.add_line(Point::new(cx - r - 1.0 + r / 10.0, cy));
327 let path = path.build(false)?;
328 ctx.draw_path(&pen, &path);
329 let brush3 = RadialGradientBrush::new(
330 [
331 GradientStop::new(Color::new(0xF5, 0xF5, 0xF5, 0xFF), 0.0),
332 GradientStop::new(
333 Color::accent().unwrap_or(Color::new(0xFF, 0xC0, 0xCB, 0xFF)),
334 1.0,
335 ),
336 ],
337 RelativePoint::new(0.5, 0.5),
338 RelativePoint::new(0.2, 0.5),
339 RelativeSize::new(0.5, 0.5),
340 );
341 let font = DrawingFontBuilder::new()
342 .family("Arial")
343 .size(r / 5.0)
344 .halign(HAlign::Center)
345 .valign(VAlign::Bottom)
346 .build();
347 ctx.draw_str(&brush3, font, Point::new(cx, cy), "Hello world!");
348 Ok(())
349 }Sourcepub fn fill_round_rect(
&mut self,
brush: impl Brush,
rect: Rect<f64, LogicalSpace>,
round: Size2D<f64, LogicalSpace>,
) -> Result<(), Error>
pub fn fill_round_rect( &mut self, brush: impl Brush, rect: Rect<f64, LogicalSpace>, round: Size2D<f64, LogicalSpace>, ) -> Result<(), Error>
Fill a rounded rectangle.
Sourcepub fn draw_str(
&mut self,
brush: impl Brush,
font: DrawingFont,
pos: Point2D<f64, LogicalSpace>,
text: impl AsRef<str>,
) -> Result<(), Error>
pub fn draw_str( &mut self, brush: impl Brush, font: DrawingFont, pos: Point2D<f64, LogicalSpace>, text: impl AsRef<str>, ) -> Result<(), Error>
Draw a string.
Examples found in repository?
examples/test/widgets.rs (line 347)
243 fn render(&mut self, _sender: &ComponentSender<Self>) -> Result<(), Self::Error> {
244 let csize = self.window.size()?;
245 {
246 let mut cred_panel = layout! {
247 Grid::from_str("auto,1*,auto", "1*,auto,auto,1*").unwrap(),
248 self.ulabel => { column: 0, row: 1, valign: VAlign::Center },
249 self.uentry => { column: 1, row: 1, margin: Margin::new_all_same(4.0) },
250 self.plabel => { column: 0, row: 2, valign: VAlign::Center },
251 self.pentry => { column: 1, row: 2, margin: Margin::new_all_same(4.0) },
252 self.pcheck => { column: 2, row: 2 },
253 };
254
255 let mut rgroup_panel = Grid::from_str("auto", "1*,auto,auto,auto,1*").unwrap();
256 for (i, rb) in self.radio_group.iter_mut().enumerate() {
257 rgroup_panel.push(rb).row(i + 1).finish();
258 }
259
260 let mut buttons_panel = layout! {
261 StackPanel::new(Orient::Vertical),
262 self.push_button => { margin: Margin::new_all_same(4.0) },
263 self.pop_button => { margin: Margin::new_all_same(4.0) },
264 self.show_button => { margin: Margin::new_all_same(4.0) },
265 };
266
267 let mut root_panel = layout! {
268 Grid::from_str("1*,1*,1*", "1*,auto,1*").unwrap(),
269 cred_panel => { column: 1, row: 0 },
270 rgroup_panel => { column: 2, row: 0, halign: HAlign::Center },
271 self.canvas => { column: 0, row: 1, row_span: 2 },
272 self.combo => { column: 1, row: 1, halign: HAlign::Center },
273 self.progress => { column: 2, row: 1 },
274 self.mltext => { column: 1, row: 2, margin: Margin::new_all_same(8.0) },
275 buttons_panel => { column: 2, row: 2 },
276 };
277
278 root_panel.set_size(csize);
279 }
280
281 let size = self.canvas.size()?;
282 let is_dark = ColorTheme::current()? == ColorTheme::Dark;
283 let back_color = if is_dark {
284 Color::new(255, 255, 255, 255)
285 } else {
286 Color::new(0, 0, 0, 255)
287 };
288 let brush = SolidColorBrush::new(back_color);
289 let pen = BrushPen::new(&brush, 1.0);
290 let mut ctx = self.canvas.context()?;
291 let cx = size.width / 2.0;
292 let cy = size.height / 2.0;
293 let r = cx.min(cy) - 2.0;
294 ctx.draw_pie(
295 &pen,
296 Rect::new(Point::new(cx - r, cy - r), Size::new(r * 2.0, r * 2.0)),
297 std::f64::consts::PI,
298 std::f64::consts::PI * 2.0,
299 );
300
301 let brush2 = LinearGradientBrush::new(
302 [
303 GradientStop::new(Color::new(0x87, 0xCE, 0xEB, 0xFF), 0.0),
304 GradientStop::new(back_color, 1.0),
305 ],
306 RelativePoint::zero(),
307 RelativePoint::new(0.0, 1.0),
308 );
309 let pen2 = BrushPen::new(&brush2, 1.0);
310 ctx.draw_round_rect(
311 &pen2,
312 Rect::new(
313 Point::new(cx - r - 1.0, cy - r - 1.0),
314 Size::new(r * 2.0 + 2.0, r * 1.618 + 2.0),
315 ),
316 Size::new(r / 10.0, r / 10.0),
317 );
318 let mut path = ctx.create_path_builder(Point::new(cx + r + 1.0 - r / 10.0, cy))?;
319 path.add_arc(
320 Point::new(cx, cy + r * 0.618 + 1.0),
321 Size::new(r + 1.0 - r / 10.0, r * 0.382 / 2.0),
322 0.0,
323 std::f64::consts::PI,
324 true,
325 );
326 path.add_line(Point::new(cx - r - 1.0 + r / 10.0, cy));
327 let path = path.build(false)?;
328 ctx.draw_path(&pen, &path);
329 let brush3 = RadialGradientBrush::new(
330 [
331 GradientStop::new(Color::new(0xF5, 0xF5, 0xF5, 0xFF), 0.0),
332 GradientStop::new(
333 Color::accent().unwrap_or(Color::new(0xFF, 0xC0, 0xCB, 0xFF)),
334 1.0,
335 ),
336 ],
337 RelativePoint::new(0.5, 0.5),
338 RelativePoint::new(0.2, 0.5),
339 RelativeSize::new(0.5, 0.5),
340 );
341 let font = DrawingFontBuilder::new()
342 .family("Arial")
343 .size(r / 5.0)
344 .halign(HAlign::Center)
345 .valign(VAlign::Bottom)
346 .build();
347 ctx.draw_str(&brush3, font, Point::new(cx, cy), "Hello world!");
348 Ok(())
349 }Sourcepub fn measure_str(
&self,
font: DrawingFont,
text: &str,
) -> Result<Size2D<f64, LogicalSpace>, Error>
pub fn measure_str( &self, font: DrawingFont, text: &str, ) -> Result<Size2D<f64, LogicalSpace>, Error>
Measure string size.
Sourcepub fn create_image(&self, image: DynamicImage) -> Result<DrawingImage, Error>
pub fn create_image(&self, image: DynamicImage) -> Result<DrawingImage, Error>
Create a DrawingContext-compatible image from DynamicImage.
Sourcepub fn draw_image(
&mut self,
image: &DrawingImage,
rect: Rect<f64, LogicalSpace>,
clip: Option<Rect<f64, LogicalSpace>>,
) -> Result<(), Error>
pub fn draw_image( &mut self, image: &DrawingImage, rect: Rect<f64, LogicalSpace>, clip: Option<Rect<f64, LogicalSpace>>, ) -> Result<(), Error>
Draw a image with RGBA format.
Sourcepub fn create_path_builder(
&self,
start: Point2D<f64, LogicalSpace>,
) -> Result<DrawingPathBuilder, Error>
pub fn create_path_builder( &self, start: Point2D<f64, LogicalSpace>, ) -> Result<DrawingPathBuilder, Error>
Create DrawingPathBuilder.
Examples found in repository?
examples/test/widgets.rs (line 318)
243 fn render(&mut self, _sender: &ComponentSender<Self>) -> Result<(), Self::Error> {
244 let csize = self.window.size()?;
245 {
246 let mut cred_panel = layout! {
247 Grid::from_str("auto,1*,auto", "1*,auto,auto,1*").unwrap(),
248 self.ulabel => { column: 0, row: 1, valign: VAlign::Center },
249 self.uentry => { column: 1, row: 1, margin: Margin::new_all_same(4.0) },
250 self.plabel => { column: 0, row: 2, valign: VAlign::Center },
251 self.pentry => { column: 1, row: 2, margin: Margin::new_all_same(4.0) },
252 self.pcheck => { column: 2, row: 2 },
253 };
254
255 let mut rgroup_panel = Grid::from_str("auto", "1*,auto,auto,auto,1*").unwrap();
256 for (i, rb) in self.radio_group.iter_mut().enumerate() {
257 rgroup_panel.push(rb).row(i + 1).finish();
258 }
259
260 let mut buttons_panel = layout! {
261 StackPanel::new(Orient::Vertical),
262 self.push_button => { margin: Margin::new_all_same(4.0) },
263 self.pop_button => { margin: Margin::new_all_same(4.0) },
264 self.show_button => { margin: Margin::new_all_same(4.0) },
265 };
266
267 let mut root_panel = layout! {
268 Grid::from_str("1*,1*,1*", "1*,auto,1*").unwrap(),
269 cred_panel => { column: 1, row: 0 },
270 rgroup_panel => { column: 2, row: 0, halign: HAlign::Center },
271 self.canvas => { column: 0, row: 1, row_span: 2 },
272 self.combo => { column: 1, row: 1, halign: HAlign::Center },
273 self.progress => { column: 2, row: 1 },
274 self.mltext => { column: 1, row: 2, margin: Margin::new_all_same(8.0) },
275 buttons_panel => { column: 2, row: 2 },
276 };
277
278 root_panel.set_size(csize);
279 }
280
281 let size = self.canvas.size()?;
282 let is_dark = ColorTheme::current()? == ColorTheme::Dark;
283 let back_color = if is_dark {
284 Color::new(255, 255, 255, 255)
285 } else {
286 Color::new(0, 0, 0, 255)
287 };
288 let brush = SolidColorBrush::new(back_color);
289 let pen = BrushPen::new(&brush, 1.0);
290 let mut ctx = self.canvas.context()?;
291 let cx = size.width / 2.0;
292 let cy = size.height / 2.0;
293 let r = cx.min(cy) - 2.0;
294 ctx.draw_pie(
295 &pen,
296 Rect::new(Point::new(cx - r, cy - r), Size::new(r * 2.0, r * 2.0)),
297 std::f64::consts::PI,
298 std::f64::consts::PI * 2.0,
299 );
300
301 let brush2 = LinearGradientBrush::new(
302 [
303 GradientStop::new(Color::new(0x87, 0xCE, 0xEB, 0xFF), 0.0),
304 GradientStop::new(back_color, 1.0),
305 ],
306 RelativePoint::zero(),
307 RelativePoint::new(0.0, 1.0),
308 );
309 let pen2 = BrushPen::new(&brush2, 1.0);
310 ctx.draw_round_rect(
311 &pen2,
312 Rect::new(
313 Point::new(cx - r - 1.0, cy - r - 1.0),
314 Size::new(r * 2.0 + 2.0, r * 1.618 + 2.0),
315 ),
316 Size::new(r / 10.0, r / 10.0),
317 );
318 let mut path = ctx.create_path_builder(Point::new(cx + r + 1.0 - r / 10.0, cy))?;
319 path.add_arc(
320 Point::new(cx, cy + r * 0.618 + 1.0),
321 Size::new(r + 1.0 - r / 10.0, r * 0.382 / 2.0),
322 0.0,
323 std::f64::consts::PI,
324 true,
325 );
326 path.add_line(Point::new(cx - r - 1.0 + r / 10.0, cy));
327 let path = path.build(false)?;
328 ctx.draw_path(&pen, &path);
329 let brush3 = RadialGradientBrush::new(
330 [
331 GradientStop::new(Color::new(0xF5, 0xF5, 0xF5, 0xFF), 0.0),
332 GradientStop::new(
333 Color::accent().unwrap_or(Color::new(0xFF, 0xC0, 0xCB, 0xFF)),
334 1.0,
335 ),
336 ],
337 RelativePoint::new(0.5, 0.5),
338 RelativePoint::new(0.2, 0.5),
339 RelativeSize::new(0.5, 0.5),
340 );
341 let font = DrawingFontBuilder::new()
342 .family("Arial")
343 .size(r / 5.0)
344 .halign(HAlign::Center)
345 .valign(VAlign::Bottom)
346 .build();
347 ctx.draw_str(&brush3, font, Point::new(cx, cy), "Hello world!");
348 Ok(())
349 }Auto Trait Implementations§
impl<'a> Freeze for DrawingContext<'a>
impl<'a> RefUnwindSafe for DrawingContext<'a>
impl<'a> !Send for DrawingContext<'a>
impl<'a> !Sync for DrawingContext<'a>
impl<'a> Unpin for DrawingContext<'a>
impl<'a> UnsafeUnpin for DrawingContext<'a>
impl<'a> !UnwindSafe for DrawingContext<'a>
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> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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