sorta working simplified focus!

This commit is contained in:
🪞👃🪞 2024-08-31 21:50:13 +03:00
parent 777904cb35
commit 9f358f8a21
5 changed files with 47 additions and 121 deletions

View file

@ -111,18 +111,6 @@ impl<'a> Render for Box<dyn Render + 'a> {
}
}
//impl<'a, T: Fn(&mut Buffer, Rect) -> Usually<Rect> + Send + Sync + 'a> Render for T {
//fn render (&self, b: &mut Buffer, a: Rect) -> Usually<Rect> {
//(*self)(b, a)
//}
//}
impl<'a> Render for Box<dyn Fn(&mut Buffer, Rect) -> Usually<Rect> + Send + Sync + 'a> {
fn render (&self, b: &mut Buffer, a: Rect) -> Usually<Rect> {
(*self)(b, a)
}
}
impl<T: Render> Render for Arc<T> {
fn render (&self, b: &mut Buffer, a: Rect) -> Usually<Rect> {
self.as_ref().render(b, a)
@ -141,6 +129,18 @@ impl<T: Render + Sync> Render for RwLock<T> {
}
}
//impl<'a, T: Fn(&mut Buffer, Rect) -> Usually<Rect> + Send + Sync + 'a> Render for T {
//fn render (&self, b: &mut Buffer, a: Rect) -> Usually<Rect> {
//(*self)(b, a)
//}
//}
impl<'a> Render for Box<dyn Fn(&mut Buffer, Rect) -> Usually<Rect> + Send + Sync + 'a> {
fn render (&self, b: &mut Buffer, a: Rect) -> Usually<Rect> {
(*self)(b, a)
}
}
pub fn center_box (area: Rect, w: u16, h: u16) -> Rect {
let width = w.min(area.width * 3 / 5);
let height = h.min(area.width * 3 / 5);

View file

@ -27,7 +27,9 @@ impl<'a> Collection<'a> {
pub trait Collect<'a> {
fn add_box (self, item: Box<dyn Render + 'a>) -> Self;
fn add_ref (self, item: &'a dyn Render) -> Self;
fn add <T: Render + Sized + 'a> (self, item: T) -> Self where Self: Sized {
fn add <T: Render + Sized + 'a> (self, item: T) -> Self
where Self: Sized
{
self.add_box(Box::new(item))
}
}

View file

@ -9,12 +9,6 @@ pub enum Direction {
}
impl Direction {
//pub fn split <'a, const N: usize> (&self, items: [&'a (dyn Render + Sync);N]) -> Split<'a, N> {
//Split(*self, items)
//}
pub fn split_focus <'a> (&self, index: usize, items: Renderables<'a>, style: Style) -> SplitFocus<'a> {
SplitFocus(*self, index, items, style)
}
pub fn is_down (&self) -> bool {
match self { Self::Down => true, _ => false }
}
@ -23,27 +17,39 @@ impl Direction {
}
}
pub struct Split<'a>(Collection<'a>, Direction);
pub struct Split<'a> {
items: Collection<'a>,
direction: Direction,
focus: Option<usize>
}
impl<'a> Split<'a> {
pub fn new (direction: Direction) -> Self {
Self(Collection::new(), direction)
Self {
items: Collection::new(),
direction,
focus: None
}
}
pub fn down () -> Self {
Self(Collection::new(), Direction::Down)
Self::new(Direction::Down)
}
pub fn right () -> Self {
Self(Collection::new(), Direction::Right)
Self::new(Direction::Right)
}
pub fn focus (mut self, focus: Option<usize>) -> Self {
self.focus = focus;
self
}
pub fn render_areas (&self, buf: &mut Buffer, area: Rect) -> Usually<(Rect, Vec<Rect>)> {
let Rect { mut x, mut y, mut width, mut height } = area;
let mut areas = vec![];
for item in self.0.0.iter() {
for (index, item) in self.items.0.iter().enumerate() {
if width == 0 || height == 0 {
break
}
let result = item.render(buf, Rect { x, y, width, height })?;
match self.1 {
match self.direction {
Direction::Down => {
y += result.height;
height = height.saturating_sub(result.height);
@ -54,7 +60,10 @@ impl<'a> Split<'a> {
},
_ => unimplemented!()
};
areas.push(area);
areas.push(result);
if self.focus == Some(index) {
Corners(Style::default().green().not_dim()).draw(buf, result)?;
}
}
Ok((area, areas))
}
@ -68,91 +77,11 @@ impl<'a> Render for Split<'a> {
impl<'a> Collect<'a> for Split<'a> {
fn add_box (mut self, item: Box<dyn Render + 'a>) -> Self {
self.0 = self.0.add_box(item);
self.items = self.items.add_box(item);
self
}
fn add_ref (mut self, item: &'a dyn Render) -> Self {
self.0 = self.0.add_ref(item);
self.items = self.items.add_ref(item);
self
}
}
//pub struct Split<'a, const N: usize>(
//pub Direction, pub [&'a (dyn Render + Sync);N]
//);
//impl<'a, const N: usize> Split<'a, N> {
//pub fn down (items: [&'a (dyn Render + Sync);N]) -> Self {
//Self(Direction::Down, items)
//}
//pub fn right (items: [&'a (dyn Render + Sync);N]) -> Self {
//Self(Direction::Right, items)
//}
//pub fn render_areas (&self, buf: &mut Buffer, area: Rect) -> Usually<(Rect, Vec<Rect>)> {
//let Rect { mut x, mut y, mut width, mut height } = area;
//let mut areas = vec![];
//for item in self.1 {
//if width == 0 || height == 0 {
//break
//}
//let result = item.render(buf, Rect { x, y, width, height })?;
//match self.0 {
//Direction::Down => {
//y = y + result.height;
//height = height.saturating_sub(result.height);
//},
//Direction::Right => {
//x = x + result.width;
//width = width.saturating_sub(result.width);
//},
//_ => unimplemented!()
//};
//areas.push(area);
//}
//Ok((area, areas))
//}
//}
//impl<'a, const N: usize> Render for Split<'a, N> {
//fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
//Ok(self.render_areas(buf, area)?.0)
//}
//}
type Renderables<'a> = &'a [&'a (dyn Render + Send + Sync)];
pub struct SplitFocus<'a>(pub Direction, pub usize, pub Renderables<'a>, pub Style);
impl<'a> SplitFocus<'a> {
pub fn render_areas (&self, buf: &mut Buffer, area: Rect) -> Usually<(Rect, Vec<Rect>)> {
let Rect { mut x, mut y, mut width, mut height } = area;
let mut areas = vec![];
for item in self.2.iter() {
if width == 0 || height == 0 {
break
}
let result = item.render(buf, Rect { x, y, width, height })?;
areas.push(result);
match self.0 {
Direction::Down => {
y += result.height;
height = height.saturating_sub(result.height);
},
Direction::Right => {
x += result.width;
width = width.saturating_sub(result.width);
},
_ => unimplemented!()
}
Lozenge(self.3).draw(buf, result)?;
}
Ok((area, areas))
}
}
impl<'a> Render for SplitFocus<'a> {
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
Ok(self.render_areas(buf, area)?.0)
}
}

View file

@ -37,17 +37,11 @@ impl<'a> Render for TrackView<'a> {
_ => { unimplemented!() },
}
fill_bg(buf, area, Nord::bg_lo(self.focused, self.entered));
let devices: Vec<&(dyn Render + Send + Sync)> = chain.devices.as_slice()
.iter()
.map(|d|d as &(dyn Render + Send + Sync))
.collect();
let (area, areas) = self.direction
.split_focus(0, devices.as_slice(), if self.focused {
Style::default().green().dim()
} else {
Style::default().dim()
})
.render_areas(buf, area)?;
let mut split = Split::new(self.direction);
for device in chain.devices.as_slice().iter() {
split = split.add_ref(device);
}
let (area, areas) = split.render_areas(buf, area)?;
if self.focused && self.entered && areas.len() > 0 {
Corners(Style::default().green().not_dim()).draw(buf, areas[0])?;
}

View file

@ -23,7 +23,7 @@ impl ArrangerStandalone {
self.focus = if self.focus > 0 { 1 } else { self.focus - 1 };
}
fn focus_next (&mut self) {
self.focus = if self.focus < 2 { self.focus + 1 } else { 0 };
self.focus = if self.focus < 1 { self.focus + 1 } else { 0 };
}
fn focused (&self) -> &dyn Render {
self.focusable()[self.focus]
@ -107,6 +107,7 @@ impl Render for ArrangerStandalone {
.add_ref(&self.transport)
.add_ref(&self.arranger)
.add_ref(&sequencer)
.focus(Some(self.focus))
.render(buf, area)?;
if let Some(ref modal) = self.arranger.modal {
fill_bg(buf, area, Nord::bg_lo(false, false));