From f018988567b02df88357fa85ddf0e3f68b7648c2 Mon Sep 17 00:00:00 2001 From: unspeaker Date: Wed, 4 Dec 2024 21:21:18 +0100 Subject: [PATCH] rename Widget::layout to Widget::min_size --- crates/tek_api/src/lib.rs | 2 +- crates/tek_core/src/engine.rs | 42 +++++------ crates/tek_core/src/space.rs | 74 ++++++++++---------- crates/tek_core/src/test.rs | 2 +- crates/tek_core/src/tui.rs | 26 +++---- crates/tek_tui/src/todo_tui_mixer.rs | 2 +- crates/tek_tui/src/todo_tui_plugin.rs | 2 +- crates/tek_tui/src/todo_tui_sampler.rs | 4 +- crates/tek_tui/src/tui_view_phrase_editor.rs | 40 ++++++----- crates/tek_tui/src/tui_view_transport.rs | 4 +- 10 files changed, 100 insertions(+), 98 deletions(-) diff --git a/crates/tek_api/src/lib.rs b/crates/tek_api/src/lib.rs index a4e9e8b7..9f39f83e 100644 --- a/crates/tek_api/src/lib.rs +++ b/crates/tek_api/src/lib.rs @@ -152,7 +152,7 @@ fn query_ports(client: &Client, names: Vec) -> BTreeMap Widget for JackDevice { //type Engine = E; - //fn layout(&self, to: E::Size) -> Perhaps { + //fn min_size(&self, to: E::Size) -> Perhaps { //self.state.read().unwrap().layout(to) //} //fn render(&self, to: &mut E::Output) -> Usually<()> { diff --git a/crates/tek_core/src/engine.rs b/crates/tek_core/src/engine.rs index aa0c5bee..d762128b 100644 --- a/crates/tek_core/src/engine.rs +++ b/crates/tek_core/src/engine.rs @@ -51,7 +51,7 @@ pub trait Widget: Send + Sync { /// Engine for which this component is implemented type Engine: Engine; /// Minimum size to use - fn layout (&self, to: ::Size) + fn min_size (&self, to: ::Size) -> Perhaps<::Size> { Ok(Some(to)) @@ -61,8 +61,8 @@ pub trait Widget: Send + Sync { } impl Widget for &dyn Widget { type Engine = E; - fn layout (&self, to: E::Size) -> Perhaps { - (*self).layout(to) + fn min_size (&self, to: E::Size) -> Perhaps { + (*self).min_size(to) } fn render (&self, to: &mut E::Output) -> Usually<()> { (*self).render(to) @@ -70,8 +70,8 @@ impl Widget for &dyn Widget { } impl Widget for &mut dyn Widget { type Engine = E; - fn layout (&self, to: E::Size) -> Perhaps { - (**self).layout(to) + fn min_size (&self, to: E::Size) -> Perhaps { + (**self).min_size(to) } fn render (&self, to: &mut E::Output) -> Usually<()> { (**self).render(to) @@ -79,8 +79,8 @@ impl Widget for &mut dyn Widget { } impl<'a, E: Engine> Widget for Box + 'a> { type Engine = E; - fn layout (&self, to: E::Size) -> Perhaps { - (**self).layout(to) + fn min_size (&self, to: E::Size) -> Perhaps { + (**self).min_size(to) } fn render (&self, to: &mut E::Output) -> Usually<()> { (**self).render(to) @@ -88,8 +88,8 @@ impl<'a, E: Engine> Widget for Box + 'a> { } impl> Widget for Arc { type Engine = E; - fn layout (&self, to: E::Size) -> Perhaps { - self.as_ref().layout(to) + fn min_size (&self, to: E::Size) -> Perhaps { + self.as_ref().min_size(to) } fn render (&self, to: &mut E::Output) -> Usually<()> { self.as_ref().render(to) @@ -97,8 +97,8 @@ impl> Widget for Arc { } impl> Widget for Mutex { type Engine = E; - fn layout (&self, to: E::Size) -> Perhaps { - self.lock().unwrap().layout(to) + fn min_size (&self, to: E::Size) -> Perhaps { + self.lock().unwrap().min_size(to) } fn render (&self, to: &mut E::Output) -> Usually<()> { self.lock().unwrap().render(to) @@ -106,8 +106,8 @@ impl> Widget for Mutex { } impl> Widget for RwLock { type Engine = E; - fn layout (&self, to: E::Size) -> Perhaps { - self.read().unwrap().layout(to) + fn min_size (&self, to: E::Size) -> Perhaps { + self.read().unwrap().min_size(to) } fn render (&self, to: &mut E::Output) -> Usually<()> { self.read().unwrap().render(to) @@ -115,8 +115,8 @@ impl> Widget for RwLock { } impl> Widget for Option { type Engine = E; - fn layout (&self, to: E::Size) -> Perhaps { - Ok(self.as_ref().map(|widget|widget.layout(to)).transpose()?.flatten()) + fn min_size (&self, to: E::Size) -> Perhaps { + Ok(self.as_ref().map(|widget|widget.min_size(to)).transpose()?.flatten()) } fn render (&self, to: &mut E::Output) -> Usually<()> { self.as_ref().map(|widget|widget.render(to)).unwrap_or(Ok(())) @@ -130,8 +130,8 @@ pub struct Either, B: Widget>( ); impl, B: Widget> Widget for Either { type Engine = E; - fn layout (&self, to: E::Size) -> Perhaps { - if self.0 { self.1.layout(to) } else { self.2.layout(to) } + fn min_size (&self, to: E::Size) -> Perhaps { + if self.0 { self.1.min_size(to) } else { self.2.min_size(to) } } fn render (&self, to: &mut E::Output) -> Usually<()> { if self.0 { self.1.render(to) } else { self.2.render(to) } @@ -158,7 +158,7 @@ impl< R: Send + Sync + Fn(&mut E::Output)->Usually<()> > Widget for CustomWidget { type Engine = E; - fn layout (&self, to: E::Size) -> Perhaps { + fn min_size (&self, to: E::Size) -> Perhaps { self.0(to) } fn render (&self, to: &mut E::Output) -> Usually<()> { @@ -173,11 +173,11 @@ pub trait Content: Send + Sync { /// Every struct that has [Content] is a renderable [Widget]. impl> Widget for W { type Engine = E; - fn layout (&self, to: E::Size) -> Perhaps { - self.content().layout(to) + fn min_size (&self, to: E::Size) -> Perhaps { + self.content().min_size(to) } fn render (&self, to: &mut E::Output) -> Usually<()> { - match self.layout(to.area().wh().into())? { + match self.min_size(to.area().wh().into())? { Some(wh) => to.render_in(to.area().clip(wh).into(), &self.content()), None => Ok(()) } diff --git a/crates/tek_core/src/space.rs b/crates/tek_core/src/space.rs index a3a684a7..07e76ea1 100644 --- a/crates/tek_core/src/space.rs +++ b/crates/tek_core/src/space.rs @@ -195,8 +195,8 @@ impl> Fill { impl> Widget for Fill { type Engine = E; - fn layout (&self, to: E::Size) -> Perhaps { - let area = self.inner().layout(to.into())?; + fn min_size (&self, to: E::Size) -> Perhaps { + let area = self.inner().min_size(to.into())?; if let Some(area) = area { Ok(Some(match self { Self::X(_) => [to.w().into(), area.h()], @@ -232,11 +232,11 @@ where F: Send + Sync + Fn(&mut dyn FnMut(&dyn Widget)->Usually<()>)->Usually<()> { type Engine = E; - fn layout (&self, area: E::Size) -> Perhaps { + fn min_size (&self, area: E::Size) -> Perhaps { let mut w: E::Unit = 0.into(); let mut h: E::Unit = 0.into(); (self.0)(&mut |layer| { - if let Some(layer_area) = layer.layout(area)? { + if let Some(layer_area) = layer.min_size(area)? { w = w.max(layer_area.w()); h = h.max(layer_area.h()); } @@ -245,7 +245,7 @@ where Ok(Some([w, h].into())) } fn render (&self, to: &mut E::Output) -> Usually<()> { - if let Some(size) = self.layout(to.area().wh().into())? { + if let Some(size) = self.min_size(to.area().wh().into())? { (self.0)(&mut |layer|to.render_in(to.area().clip(size).into(), &layer)) } else { Ok(()) @@ -348,12 +348,12 @@ fn align + From<[N;4]>> (align: &Align, outer: R impl> Widget for Align { type Engine = E; - fn layout (&self, outer_area: E::Size) -> Perhaps { - self.inner().layout(outer_area) + fn min_size (&self, outer_area: E::Size) -> Perhaps { + self.inner().min_size(outer_area) } fn render (&self, to: &mut E::Output) -> Usually<()> { let outer_area = to.area(); - Ok(if let Some(inner_size) = self.layout(outer_area.wh().into())? { + Ok(if let Some(inner_size) = self.min_size(outer_area.wh().into())? { let inner_area = outer_area.clip(inner_size); if let Some(aligned) = align(&self, outer_area.into(), inner_area.into()) { to.render_in(aligned, self.inner())? @@ -382,7 +382,7 @@ impl Fixed { } impl> Widget for Fixed { type Engine = E; - fn layout (&self, to: E::Size) -> Perhaps { + fn min_size (&self, to: E::Size) -> Perhaps { Ok(match self { Self::X(w, _) => if to.w() >= *w { Some([*w, to.h()].into()) } else { None }, @@ -394,7 +394,7 @@ impl> Widget for Fixed { } fn render (&self, to: &mut E::Output) -> Usually<()> { // 👘 πŸ‘™ β†πŸ‘™β†’ - if let Some(size) = self.layout(to.area().wh().into())? { + if let Some(size) = self.min_size(to.area().wh().into())? { to.render_in(to.area().clip(size).into(), self.inner()) } else { Ok(()) @@ -418,8 +418,8 @@ impl Min { } impl> Widget for Min { type Engine = E; - fn layout (&self, to: E::Size) -> Perhaps { - Ok(self.inner().layout(to)?.map(|to|match *self { + fn min_size (&self, to: E::Size) -> Perhaps { + Ok(self.inner().min_size(to)?.map(|to|match *self { Self::X(w, _) => [to.w().max(w), to.h()], Self::Y(h, _) => [to.w(), to.h().max(h)], Self::XY(w, h, _) => [to.w().max(w), to.h().max(h)], @@ -427,7 +427,7 @@ impl> Widget for Min { } // TODO: 👘 πŸ‘™ β†πŸ‘™β†’ fn render (&self, to: &mut E::Output) -> Usually<()> { - Ok(self.layout(to.area().wh().into())? + Ok(self.min_size(to.area().wh().into())? .map(|size|to.render_in(to.area().clip(size).into(), self.inner())) .transpose()?.unwrap_or(())) } @@ -451,15 +451,15 @@ impl Max { impl> Widget for Max { type Engine = E; - fn layout (&self, to: E::Size) -> Perhaps { - Ok(self.inner().layout(to)?.map(|to|match *self { + fn min_size (&self, to: E::Size) -> Perhaps { + Ok(self.inner().min_size(to)?.map(|to|match *self { Self::X(w, _) => [to.w().min(w), to.h()], Self::Y(h, _) => [to.w(), to.h().min(h)], Self::XY(w, h, _) => [to.w().min(w), to.h().min(h)], }.into())) } fn render (&self, to: &mut E::Output) -> Usually<()> { - Ok(self.layout(to.area().wh().into())? + Ok(self.min_size(to.area().wh().into())? .map(|size|to.render_in(to.area().clip(size).into(), self.inner())) .transpose()?.unwrap_or(())) } @@ -483,15 +483,15 @@ impl Grow { impl> Widget for Grow { type Engine = E; - fn layout (&self, to: E::Size) -> Perhaps { - Ok(self.inner().layout(to)?.map(|to|match *self { + fn min_size (&self, to: E::Size) -> Perhaps { + Ok(self.inner().min_size(to)?.map(|to|match *self { Self::X(w, _) => [to.w() + w, to.h()], Self::Y(h, _) => [to.w(), to.h() + h], Self::XY(w, h, _) => [to.w() + w, to.h() + h], }.into())) } fn render (&self, to: &mut E::Output) -> Usually<()> { - Ok(self.layout(to.area().wh().into())? + Ok(self.min_size(to.area().wh().into())? .map(|size|to.render_in(to.area().clip(size).into(), self.inner())) .transpose()?.unwrap_or(())) } @@ -515,8 +515,8 @@ impl Shrink { impl> Widget for Shrink { type Engine = E; - fn layout (&self, to: E::Size) -> Perhaps { - Ok(self.inner().layout(to)?.map(|to|match *self { + fn min_size (&self, to: E::Size) -> Perhaps { + Ok(self.inner().min_size(to)?.map(|to|match *self { Self::X(w, _) => [ if to.w() > w { to.w() - w } else { 0.into() }, to.h() @@ -532,7 +532,7 @@ impl> Widget for Shrink { }.into())) } fn render (&self, to: &mut E::Output) -> Usually<()> { - Ok(self.layout(to.area().wh().into())? + Ok(self.min_size(to.area().wh().into())? .map(|size|to.render_in(to.area().clip(size).into(), self.inner())) .transpose()?.unwrap_or(())) } @@ -586,7 +586,7 @@ impl> Widget for Inset { impl> Widget for Outset { type Engine = E; - fn layout (&self, to: E::Size) -> Perhaps { + fn min_size (&self, to: E::Size) -> Perhaps { match *self { Self::X(x, ref inner) => (inner as &dyn Widget).grow_x(x + x), @@ -594,7 +594,7 @@ impl> Widget for Outset { (inner as &dyn Widget).grow_y(y + y), Self::XY(x, y, ref inner) => (inner as &dyn Widget).grow_xy(x + x, y + y), - }.layout(to) + }.min_size(to) } fn render (&self, to: &mut E::Output) -> Usually<()> { match *self { @@ -632,12 +632,12 @@ impl Push { impl> Widget for Push { type Engine = E; - fn layout (&self, to: E::Size) -> Perhaps { - self.inner().layout(to) + fn min_size (&self, to: E::Size) -> Perhaps { + self.inner().min_size(to) } fn render (&self, to: &mut E::Output) -> Usually<()> { let area = to.area(); - Ok(self.layout(area.wh().into())? + Ok(self.min_size(area.wh().into())? .map(|size|to.render_in(match *self { Self::X(x, _) => [area.x() + x, area.y(), size.w(), size.h()], Self::Y(y, _) => [area.x(), area.y() + y, size.w(), size.h()], @@ -670,12 +670,12 @@ impl Pull { impl> Widget for Pull { type Engine = E; - fn layout (&self, to: E::Size) -> Perhaps { - self.inner().layout(to) + fn min_size (&self, to: E::Size) -> Perhaps { + self.inner().min_size(to) } fn render (&self, to: &mut E::Output) -> Usually<()> { let area = to.area(); - Ok(self.layout(area.wh().into())? + Ok(self.min_size(area.wh().into())? .map(|size|to.render_in(match *self { Self::X(x, _) => [area.x().minus(x), area.y(), size.w(), size.h()], Self::Y(y, _) => [area.x(), area.y().minus(y), size.w(), size.h()], @@ -709,14 +709,14 @@ where F: Send + Sync + Fn(&mut dyn FnMut(&dyn Widget)->Usually<()>)->Usually<()> { type Engine = E; - fn layout (&self, to: E::Size) -> Perhaps { + fn min_size (&self, to: E::Size) -> Perhaps { let mut w = 0.into(); let mut h = 0.into(); match self.1 { Direction::Down => { (self.0)(&mut |component| { if h >= to.h() { return Ok(()) } - let size = component.push_y(h).max_y(to.h() - h).layout(to)?; + let size = component.push_y(h).max_y(to.h() - h).min_size(to)?; if let Some([width, height]) = size.map(|size|size.wh()) { h = h + height.into(); if width > w { w = width; } @@ -727,7 +727,7 @@ where Direction::Right => { (self.0)(&mut |component| { if w >= to.w() { return Ok(()) } - let size = component.push_x(w).max_x(to.w() - w).layout(to)?; + let size = component.push_x(w).max_x(to.w() - w).min_size(to)?; if let Some([width, height]) = size.map(|size|size.wh()) { w = w + width.into(); if height > h { h = height } @@ -748,7 +748,7 @@ where (self.0)(&mut |component| { if h >= area.h() { return Ok(()) } let item = component.push_y(h).max_y(area.h() - h); - let size = item.layout(area.wh().into())?; + let size = item.min_size(area.wh().into())?; if let Some([width, height]) = size.map(|size|size.wh()) { item.render(to)?; h = h + height; @@ -761,7 +761,7 @@ where (self.0)(&mut |component| { if w >= area.w() { return Ok(()) } let item = component.push_x(w).max_x(area.w() - w); - let size = item.layout(area.wh().into())?; + let size = item.min_size(area.wh().into())?; if let Some([width, height]) = size.map(|size|size.wh()) { item.render(to)?; w = width + w; @@ -832,7 +832,7 @@ impl, B: Widget> Split { impl, B: Widget> Widget for Split { type Engine = E; - fn layout (&self, to: E::Size) -> Perhaps { + fn min_size (&self, to: E::Size) -> Perhaps { Ok(Some(to)) } fn render (&self, to: &mut E::Output) -> Usually<()> { @@ -878,7 +878,7 @@ impl Measure { impl Widget for Measure { type Engine = E; - fn layout (&self, _: E::Size) -> Perhaps { + fn min_size (&self, _: E::Size) -> Perhaps { Ok(Some([0u16.into(), 0u16.into()].into())) } fn render (&self, to: &mut E::Output) -> Usually<()> { diff --git a/crates/tek_core/src/test.rs b/crates/tek_core/src/test.rs index 211233b2..4b8a0244 100644 --- a/crates/tek_core/src/test.rs +++ b/crates/tek_core/src/test.rs @@ -24,7 +24,7 @@ struct TestArea(u16, u16); impl Widget for TestArea { type Engine = TestEngine; - fn layout (&self, to: [u16;2]) -> Perhaps<[u16;2]> { + fn min_size (&self, to: [u16;2]) -> Perhaps<[u16;2]> { Ok(Some([to[0], to[1], self.0, self.1])) } fn render (&self, to: &mut Self::Engine) -> Perhaps<[u16;4]> { diff --git a/crates/tek_core/src/tui.rs b/crates/tek_core/src/tui.rs index ae05b31e..d92fab08 100644 --- a/crates/tek_core/src/tui.rs +++ b/crates/tek_core/src/tui.rs @@ -289,32 +289,32 @@ pub fn buffer_update (buf: &mut Buffer, area: [u16;4], callback: &impl Fn(&mut C } impl Widget for &str { type Engine = Tui; - fn layout (&self, _: [u16;2]) -> Perhaps<[u16;2]> { + fn min_size (&self, _: [u16;2]) -> Perhaps<[u16;2]> { // TODO: line breaks Ok(Some([self.chars().count() as u16, 1])) } fn render (&self, to: &mut TuiOutput) -> Usually<()> { let [x, y, ..] = to.area(); - //let [w, h] = self.layout(to.area().wh())?.unwrap(); + //let [w, h] = self.min_size(to.area().wh())?.unwrap(); Ok(to.blit(&self, x, y, None)) } } impl Widget for String { type Engine = Tui; - fn layout (&self, _: [u16;2]) -> Perhaps<[u16;2]> { + fn min_size (&self, _: [u16;2]) -> Perhaps<[u16;2]> { // TODO: line breaks Ok(Some([self.chars().count() as u16, 1])) } fn render (&self, to: &mut TuiOutput) -> Usually<()> { let [x, y, ..] = to.area(); - //let [w, h] = self.layout(to.area().wh())?.unwrap(); + //let [w, h] = self.min_size(to.area().wh())?.unwrap(); Ok(to.blit(&self, x, y, None)) } } impl> Widget for DebugOverlay { type Engine = Tui; - fn layout (&self, to: [u16;2]) -> Perhaps<[u16;2]> { - self.0.layout(to) + fn min_size (&self, to: [u16;2]) -> Perhaps<[u16;2]> { + self.0.min_size(to) } fn render (&self, to: &mut TuiOutput) -> Usually<()> { let [x, y, w, h] = to.area(); @@ -325,13 +325,13 @@ impl> Widget for DebugOverlay { pub struct Styled>(pub Option