Skip to main content

ribir_widgets/
path.rs

1use ribir_core::prelude::*;
2
3/// Widget just use as a paint kit for a path and not care about its size. Use
4/// `[PathWidget]!` instead of.
5#[derive(Declare, Query, Clone)]
6pub struct PathPaintKit {
7  pub path: Path,
8  pub brush: Brush,
9  #[declare(default)]
10  pub style: PathStyle,
11}
12
13macro_rules! paint_method {
14  () => {
15    fn paint(&self, ctx: &mut PaintingCtx) {
16      let painter = ctx.painter();
17      painter.set_brush(self.brush.clone());
18      match &self.style {
19        PathStyle::Fill => painter.fill_path(self.path.clone()),
20        PathStyle::Stroke(strokes) => painter
21          .set_strokes(strokes.clone())
22          .stroke_path(self.path.clone()),
23      };
24    }
25  };
26}
27
28impl Render for PathPaintKit {
29  #[inline]
30  fn perform_layout(&self, clamp: BoxClamp, _: &mut LayoutCtx) -> Size { clamp.max }
31
32  #[inline]
33  fn only_sized_by_parent(&self) -> bool { true }
34
35  paint_method!();
36
37  fn hit_test(&self, _ctx: &HitTestCtx, _: Point) -> HitTest {
38    HitTest { hit: false, can_hit_child: false }
39  }
40}
41
42#[derive(Declare, Query)]
43/// A path widget which size careful and can process events only if user hit at
44/// the path self, not its size cover area.
45pub struct PathWidget {
46  pub path: Path,
47  pub brush: Brush,
48  #[declare(default)]
49  pub style: PathStyle,
50}
51
52/// Path widget just use as a paint kit for a path and not care about its size.
53/// Use `[HitTesPath]!` instead of.
54impl Render for PathWidget {
55  #[inline]
56  fn perform_layout(&self, _: BoxClamp, _: &mut LayoutCtx) -> Size {
57    self.path.bounds().max().to_vector().to_size()
58  }
59
60  paint_method!();
61}