diff --git a/src/layout/align.rs b/src/layout/align.rs index 1e1c961..b94788e 100644 --- a/src/layout/align.rs +++ b/src/layout/align.rs @@ -11,10 +11,11 @@ impl> Draw for Align { Ok(align::(area, self.1.layout(area)?, self.0)) } fn draw (self, to: &mut S) -> Perhaps> { - Ok(self.layout(to.area())? - .map(|area|to.clip(area, |to|self.1.draw(to))) - .transpose()? - .flatten()) + Ok(if let Some(area) = self.layout(to.area())? { + to.clip(area, self.1)? + } else { + None + }) } } diff --git a/src/layout/area.rs b/src/layout/area.rs index b6d68da..6533d35 100644 --- a/src/layout/area.rs +++ b/src/layout/area.rs @@ -16,6 +16,7 @@ pub struct Area>( pub Option>, pub T ); + impl_draw!(,>|self: Area, to: S|{ - to.clip(self.0, |to|self.1.draw(to)) + to.clip(self.0, self.1) }); diff --git a/src/layout/exact.rs b/src/layout/exact.rs index e94f51d..ef51dc2 100644 --- a/src/layout/exact.rs +++ b/src/layout/exact.rs @@ -22,7 +22,7 @@ impl , X: Into> + Copy> Draw for Exact< fn draw (self, to: &mut S) -> Perhaps> { let area = layout_exact(&self, to.area()); let item = match self { Self::W(i, ..) => i, Self::H(i, ..) => i, Self::WH(i, ..) => i, _ => unreachable!() }; - to.clip(area, |to|item.draw(to)) + to.clip(area, item) } } diff --git a/src/layout/full.rs b/src/layout/full.rs index 5dbd40f..7da3c3f 100644 --- a/src/layout/full.rs +++ b/src/layout/full.rs @@ -22,17 +22,17 @@ impl_draw!(,>|self: Full, to: T|{ let XYWH(x0, y0, w0, h0) = to.area(); match self { Self::W(item) => if let Some(XYWH(_, y, _, h)) = item.layout(to.area())? { - to.clip(XYWH(x0, y, w0, h), |to|item.draw(to)) + to.clip(XYWH(x0, y, w0, h), item) } else { Ok(None) }, Self::H(item) => if let Some(XYWH(x, _, w, _)) = item.layout(to.area())? { - to.clip(XYWH(x, y0, w, h0), |to|item.draw(to)) + to.clip(XYWH(x, y0, w, h0), item) } else { Ok(None) }, Self::WH(item) => if let Some(XYWH(..)) = item.layout(to.area())? { - to.clip(XYWH(x0, y0, w0, h0), |to|item.draw(to)) + to.clip(XYWH(x0, y0, w0, h0), item) } else { Ok(None) }, diff --git a/src/layout/max.rs b/src/layout/max.rs index 9fc1c40..9b3f40a 100644 --- a/src/layout/max.rs +++ b/src/layout/max.rs @@ -50,6 +50,6 @@ impl, X: Into> + Copy> Draw for Max return Ok(None) }; - to.clip(area, |to|item.draw(to)) + to.clip(area, item) } } diff --git a/src/layout/min.rs b/src/layout/min.rs index d747253..70c6549 100644 --- a/src/layout/min.rs +++ b/src/layout/min.rs @@ -24,16 +24,16 @@ impl_draw!(, X: Into>,>|self: Min Self::__(_) => unreachable!(), Self::W(item, w1) if let Some(XYWH(x, y, w, h)) = item.layout(to.area())? => { let w = w1.into().map(|w1|w.max(w1)).unwrap_or(w); - to.clip(XYWH(x, y, w, h), |to|item.draw(to)) + to.clip(XYWH(x, y, w, h), item) }, Self::H(item, h1) if let Some(XYWH(x, y, w, h)) = item.layout(to.area())? => { let h = h1.into().map(|h1|h.max(h1)).unwrap_or(h); - to.clip(XYWH(x, y, w, h), |to|item.draw(to)) + to.clip(XYWH(x, y, w, h), item) }, Self::WH(item, w1, h1) if let Some(XYWH(x, y, w, h)) = item.layout(to.area())? => { let w = w1.into().map(|w1|w.max(w1)).unwrap_or(w); let h = h1.into().map(|h1|h.max(h1)).unwrap_or(h); - to.clip(XYWH(x, y, w, h), |to|item.draw(to)) + to.clip(XYWH(x, y, w, h), item) }, _ => Ok(None) } diff --git a/src/layout/push.rs b/src/layout/push.rs index 7d1114d..0907ad1 100644 --- a/src/layout/push.rs +++ b/src/layout/push.rs @@ -25,17 +25,17 @@ impl_draw!(, X: Into>,>|self: Push { to.clip(XYWH( x + x1.into().unwrap_or_default(), y, w, h - ), |to|item.draw(to)) + ), item) }, Self::Y(item, y1) if let Some(XYWH(x, y, w, h)) = item.layout(to.area())? => { to.clip(XYWH( x, y + y1.into().unwrap_or_default(), w, h - ), |to|item.draw(to)) + ), item) }, Self::XY(item, x1, y1) if let Some(XYWH(x, y, w, h)) = item.layout(to.area())? => { to.clip(XYWH( x + x1.into().unwrap_or_default(), y + y1.into().unwrap_or_default(), w, h - ), |to|item.draw(to)) + ), item) }, _ => Ok(None) } diff --git a/src/layout/split.rs b/src/layout/split.rs index 555c75a..77120c3 100644 --- a/src/layout/split.rs +++ b/src/layout/split.rs @@ -122,14 +122,14 @@ fn draw_stacks ( origin_b: impl Into>, ) -> Usually<(Option>, Option>)> { let draw_a = |to: &mut S|Ok::<_, Box>(if let Some(origin_a) = origin_a.into() { - to.clip(area_a.into(), |to|a.align(origin_a).draw(to))? + to.clip(area_a.into(), a.align(origin_a))? } else { - to.clip(area_a.into(), |to|a.draw(to))? + to.clip(area_a.into(), a)? }); let draw_b = |to: &mut S|Ok::<_, Box>(if let Some(origin_b) = origin_b.into() { - to.clip(area_b.into(), |to|b.align(origin_b).draw(to))? + to.clip(area_b.into(), b.align(origin_b))? } else { - to.clip(area_b.into(), |to|b.draw(to))? + to.clip(area_b.into(), b)? }); Ok(if matches!(split, Split::Below) { let drawn_b = draw_b(to)?; diff --git a/src/lib.rs b/src/lib.rs index a2371a8..6d92fe2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1366,12 +1366,12 @@ macro_rules! eval_xy ( /// fn area (&self) -> XYWH { /// Default::default() /// } - /// fn clip ( + /// fn clip ( /// &mut self, /// area: impl Into>>, - /// draw: impl FnOnce(&mut Self)->T - /// ) -> T { - /// draw(self) + /// draw: impl Draw + /// ) -> Perhaps> { + /// draw.to(self) /// } /// } /// @@ -1387,11 +1387,11 @@ macro_rules! eval_xy ( /// Get current clipping area fn area (&self) -> XYWH; /// Set clipping area - fn clip ( + fn clip ( &mut self, area: impl Into>>, - draw: impl FnOnce(&mut Self)->T - ) -> T; + draw: impl Draw + ) -> Perhaps>; } /// Implement the [Draw] trait for a particular drawable and [Screen]. @@ -2645,16 +2645,16 @@ macro_rules! eval_xy ( } } - fn clip ( + fn clip ( &mut self, area: impl Into>>, - draw: impl FnOnce(&mut Self)->T - ) -> T { + draw: impl Draw + ) -> Perhaps> { let prev = self.area(); if let Some(area) = area.into() { *self.area_mut() = area.into(); } - let result = draw(self); + let result = draw.draw(self); *self.area_mut() = prev; result } @@ -2815,10 +2815,10 @@ macro_rules! eval_xy ( self.1.layout(area) } fn draw (self, to: &mut Tui) -> Drawn { - self.layout(to.area()).map(|area|to.clip(area, |to|{ + self.layout(to.area()).map(|area|to.clip(area, draw(|to: &mut Tui|{ to.update(&|cell,_,_|{ cell.set_bg(self.0); }); self.1.draw(to) - }))? + })))? } }