fix horizontal multi-step focus

This commit is contained in:
🪞👃🪞 2024-11-25 21:46:59 +01:00
parent d54a259ed3
commit f5f2a3545f
3 changed files with 275 additions and 194 deletions

View file

@ -101,10 +101,12 @@ pub trait FocusGrid: HasFocus {
fn focus_layout (&self) -> &[&[Self::Item]]; fn focus_layout (&self) -> &[&[Self::Item]];
fn focus_cursor (&self) -> (usize, usize); fn focus_cursor (&self) -> (usize, usize);
fn focus_cursor_mut (&mut self) -> &mut (usize, usize); fn focus_cursor_mut (&mut self) -> &mut (usize, usize);
fn focus_update (&mut self) { fn focus_current (&self) -> Self::Item {
let (x, y) = self.focus_cursor(); let (x, y) = self.focus_cursor();
let item = self.focus_layout()[y][x]; self.focus_layout()[y][x]
self.focus_to(item) }
fn focus_update (&mut self) {
self.focus_to(self.focus_current())
} }
fn focus_up (&mut self) { fn focus_up (&mut self) {
let layout = self.focus_layout(); let layout = self.focus_layout();
@ -127,17 +129,43 @@ pub trait FocusGrid: HasFocus {
self.focus_update(); self.focus_update();
} }
fn focus_left (&mut self) { fn focus_left (&mut self) {
let layout = self.focus_layout(); let focused = self.focused();
let (x, y) = self.focus_cursor(); let (original_x, y) = self.focus_cursor();
let next_x = if x == 0 { layout[y].len().saturating_sub(1) } else { x - 1 }; loop {
*self.focus_cursor_mut() = (next_x, y); let x = self.focus_cursor().0;
let next_x = if x == 0 {
self.focus_layout()[y].len().saturating_sub(1)
} else {
x - 1
};
if next_x == original_x {
break
}
*self.focus_cursor_mut() = (next_x, y);
if self.focus_current() != focused {
break
}
}
self.focus_update(); self.focus_update();
} }
fn focus_right (&mut self) { fn focus_right (&mut self) {
let layout = self.focus_layout(); let focused = self.focused();
let (x, y) = self.focus_cursor(); let (original_x, y) = self.focus_cursor();
let next_x = if x >= layout[y].len().saturating_sub(1) { 0 } else { x + 1 }; loop {
*self.focus_cursor_mut() = (next_x, y); let x = self.focus_cursor().0;
let next_x = if x >= self.focus_layout()[y].len().saturating_sub(1) {
0
} else {
x + 1
};
if next_x == original_x {
break
}
self.focus_cursor_mut().0 = next_x;
if self.focus_current() != focused {
break
}
}
self.focus_update(); self.focus_update();
} }
} }
@ -187,3 +215,56 @@ impl<T: FocusGrid + HasEnter> FocusOrder for T {
self.focus_update(); self.focus_update();
} }
} }
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_focus () {
struct FocusTest {
focused: char,
cursor: (usize, usize)
}
impl HasFocus for FocusTest {
type Item = char;
fn focused (&self) -> Self::Item {
self.focused
}
fn set_focused (&mut self, to: Self::Item) {
self.focused = to
}
}
impl FocusGrid for FocusTest {
fn focus_cursor (&self) -> (usize, usize) {
self.cursor
}
fn focus_cursor_mut (&mut self) -> &mut (usize, usize) {
&mut self.cursor
}
fn focus_layout (&self) -> &[&[Self::Item]] {
&[
&['a', 'a', 'a', 'b', 'b', 'd'],
&['a', 'a', 'a', 'b', 'b', 'd'],
&['a', 'a', 'a', 'c', 'c', 'd'],
&['a', 'a', 'a', 'c', 'c', 'd'],
&['e', 'e', 'e', 'e', 'e', 'e'],
]
}
}
let mut tester = FocusTest { focused: 'a', cursor: (0, 0) };
tester.focus_right();
assert_eq!(tester.cursor.0, 3);
assert_eq!(tester.focused, 'b');
tester.focus_down();
assert_eq!(tester.cursor.1, 2);
assert_eq!(tester.focused, 'c');
}
}

View file

@ -1,190 +1,190 @@
use crate::*; use crate::*;
struct TestEngine([u16;4], Vec<Vec<char>>); //struct TestEngine([u16;4], Vec<Vec<char>>);
impl Engine for TestEngine { //impl Engine for TestEngine {
type Unit = u16; //type Unit = u16;
type Size = [Self::Unit;2]; //type Size = [Self::Unit;2];
type Area = [Self::Unit;4]; //type Area = [Self::Unit;4];
type Input = Self; //type Input = Self;
type Handled = bool; //type Handled = bool;
fn exited (&self) -> bool { //fn exited (&self) -> bool {
true //true
} //}
fn area (&self) -> Self::Area { //fn area (&self) -> Self::Area {
self.0 //self.0
} //}
fn area_mut (&mut self) -> &mut Self::Area { //fn area_mut (&mut self) -> &mut Self::Area {
&mut self.0 //&mut self.0
} //}
} //}
#[derive(Copy, Clone)] //#[derive(Copy, Clone)]
struct TestArea(u16, u16); //struct TestArea(u16, u16);
impl Widget for TestArea { //impl Widget for TestArea {
type Engine = TestEngine; //type Engine = TestEngine;
fn layout (&self, to: [u16;2]) -> Perhaps<[u16;2]> { //fn layout (&self, to: [u16;2]) -> Perhaps<[u16;2]> {
Ok(Some([to[0], to[1], self.0, self.1])) //Ok(Some([to[0], to[1], self.0, self.1]))
} //}
fn render (&self, to: &mut Self::Engine) -> Perhaps<[u16;4]> { //fn render (&self, to: &mut Self::Engine) -> Perhaps<[u16;4]> {
if let Some(layout) = self.layout(to.area())? { //if let Some(layout) = self.layout(to.area())? {
for y in layout.y()..layout.y()+layout.h()-1 { //for y in layout.y()..layout.y()+layout.h()-1 {
for x in layout.x()..layout.x()+layout.w()-1 { //for x in layout.x()..layout.x()+layout.w()-1 {
to.1[y as usize][x as usize] = '*'; //to.1[y as usize][x as usize] = '*';
} //}
} //}
Ok(Some(layout)) //Ok(Some(layout))
} else { //} else {
Ok(None) //Ok(None)
} //}
} //}
} //}
#[test]
fn test_plus_minus () -> Usually<()> {
let area = [0, 0, 10, 10];
let engine = TestEngine(area, vec![vec![' ';10];10]);
let test = TestArea(4, 4);
assert_eq!(test.layout(area)?, Some([0, 0, 4, 4]));
assert_eq!(Push::X(1, test).layout(area)?, Some([1, 0, 4, 4]));
Ok(())
}
#[test]
fn test_outset_align () -> Usually<()> {
let area = [0, 0, 10, 10];
let engine = TestEngine(area, vec![vec![' ';10];10]);
let test = TestArea(4, 4);
assert_eq!(test.layout(area)?, Some([0, 0, 4, 4]));
assert_eq!(Outset::X(1, test).layout(area)?, Some([0, 0, 6, 4]));
assert_eq!(Align::X(test).layout(area)?, Some([3, 0, 4, 4]));
assert_eq!(Align::X(Outset::X(1, test)).layout(area)?, Some([2, 0, 6, 4]));
assert_eq!(Outset::X(1, Align::X(test)).layout(area)?, Some([2, 0, 6, 4]));
Ok(())
}
//#[test] //#[test]
//fn test_misc () -> Usually<()> { //fn test_plus_minus () -> Usually<()> {
//let area: [u16;4] = [0, 0, 10, 10]; //let area = [0, 0, 10, 10];
//let test = TestArea(4, 4); //let engine = TestEngine(area, vec![vec![' ';10];10]);
//assert_eq!(test.layout(area)?, //let test = TestArea(4, 4);
//Some([0, 0, 4, 4])); //assert_eq!(test.layout(area)?, Some([0, 0, 4, 4]));
//assert_eq!(Align::Center(test).layout(area)?, //assert_eq!(Push::X(1, test).layout(area)?, Some([1, 0, 4, 4]));
//Some([3, 3, 4, 4]));
//assert_eq!(Align::Center(Stack::down(|add|{
//add(&test)?;
//add(&test)
//})).layout(area)?,
//Some([3, 1, 4, 8]));
//assert_eq!(Align::Center(Stack::down(|add|{
//add(&Outset::XY(2, 2, test))?;
//add(&test)
//})).layout(area)?,
//Some([2, 0, 6, 10]));
//assert_eq!(Align::Center(Stack::down(|add|{
//add(&Outset::XY(2, 2, test))?;
//add(&Inset::XY(2, 2, test))
//})).layout(area)?,
//Some([2, 1, 6, 8]));
//assert_eq!(Stack::down(|add|{
//add(&Outset::XY(2, 2, test))?;
//add(&Inset::XY(2, 2, test))
//}).layout(area)?,
//Some([0, 0, 6, 8]));
//assert_eq!(Stack::right(|add|{
//add(&Stack::down(|add|{
//add(&Outset::XY(2, 2, test))?;
//add(&Inset::XY(2, 2, test))
//}))?;
//add(&Align::Center(TestArea(2 ,2)))
//}).layout(area)?,
//Some([0, 0, 8, 8]));
//Ok(()) //Ok(())
//} //}
//#[test] //#[test]
//fn test_offset () -> Usually<()> { //fn test_outset_align () -> Usually<()> {
//let area: [u16;4] = [50, 50, 100, 100]; //let area = [0, 0, 10, 10];
//let test = TestArea(3, 3); //let engine = TestEngine(area, vec![vec![' ';10];10]);
//assert_eq!(Push::X(1, test).layout(area)?, Some([51, 50, 3, 3])); //let test = TestArea(4, 4);
//assert_eq!(Push::Y(1, test).layout(area)?, Some([50, 51, 3, 3])); //assert_eq!(test.layout(area)?, Some([0, 0, 4, 4]));
//assert_eq!(Push::XY(1, 1, test).layout(area)?, Some([51, 51, 3, 3])); //assert_eq!(Outset::X(1, test).layout(area)?, Some([0, 0, 6, 4]));
//assert_eq!(Align::X(test).layout(area)?, Some([3, 0, 4, 4]));
//assert_eq!(Align::X(Outset::X(1, test)).layout(area)?, Some([2, 0, 6, 4]));
//assert_eq!(Outset::X(1, Align::X(test)).layout(area)?, Some([2, 0, 6, 4]));
//Ok(()) //Ok(())
//} //}
//#[test] ////#[test]
//fn test_outset () -> Usually<()> { ////fn test_misc () -> Usually<()> {
//let area: [u16;4] = [50, 50, 100, 100]; ////let area: [u16;4] = [0, 0, 10, 10];
//let test = TestArea(3, 3); ////let test = TestArea(4, 4);
//assert_eq!(Outset::X(1, test).layout(area)?, Some([49, 50, 5, 3])); ////assert_eq!(test.layout(area)?,
//assert_eq!(Outset::Y(1, test).layout(area)?, Some([50, 49, 3, 5])); ////Some([0, 0, 4, 4]));
//assert_eq!(Outset::XY(1, 1, test).layout(area)?, Some([49, 49, 5, 5])); ////assert_eq!(Align::Center(test).layout(area)?,
//Ok(()) ////Some([3, 3, 4, 4]));
//} ////assert_eq!(Align::Center(Stack::down(|add|{
////add(&test)?;
////add(&test)
////})).layout(area)?,
////Some([3, 1, 4, 8]));
////assert_eq!(Align::Center(Stack::down(|add|{
////add(&Outset::XY(2, 2, test))?;
////add(&test)
////})).layout(area)?,
////Some([2, 0, 6, 10]));
////assert_eq!(Align::Center(Stack::down(|add|{
////add(&Outset::XY(2, 2, test))?;
////add(&Inset::XY(2, 2, test))
////})).layout(area)?,
////Some([2, 1, 6, 8]));
////assert_eq!(Stack::down(|add|{
////add(&Outset::XY(2, 2, test))?;
////add(&Inset::XY(2, 2, test))
////}).layout(area)?,
////Some([0, 0, 6, 8]));
////assert_eq!(Stack::right(|add|{
////add(&Stack::down(|add|{
////add(&Outset::XY(2, 2, test))?;
////add(&Inset::XY(2, 2, test))
////}))?;
////add(&Align::Center(TestArea(2 ,2)))
////}).layout(area)?,
////Some([0, 0, 8, 8]));
////Ok(())
////}
//#[test] ////#[test]
//fn test_inset () -> Usually<()> { ////fn test_offset () -> Usually<()> {
//let area: [u16;4] = [50, 50, 100, 100]; ////let area: [u16;4] = [50, 50, 100, 100];
//let test = TestArea(3, 3); ////let test = TestArea(3, 3);
//assert_eq!(Inset::X(1, test).layout(area)?, Some([51, 50, 1, 3])); ////assert_eq!(Push::X(1, test).layout(area)?, Some([51, 50, 3, 3]));
//assert_eq!(Inset::Y(1, test).layout(area)?, Some([50, 51, 3, 1])); ////assert_eq!(Push::Y(1, test).layout(area)?, Some([50, 51, 3, 3]));
//assert_eq!(Inset::XY(1, 1, test).layout(area)?, Some([51, 51, 1, 1])); ////assert_eq!(Push::XY(1, 1, test).layout(area)?, Some([51, 51, 3, 3]));
//Ok(()) ////Ok(())
//} ////}
//#[test] ////#[test]
//fn test_stuff () -> Usually<()> { ////fn test_outset () -> Usually<()> {
//let area: [u16;4] = [0, 0, 100, 100]; ////let area: [u16;4] = [50, 50, 100, 100];
//assert_eq!("1".layout(area)?, ////let test = TestArea(3, 3);
//Some([0, 0, 1, 1])); ////assert_eq!(Outset::X(1, test).layout(area)?, Some([49, 50, 5, 3]));
//assert_eq!("333".layout(area)?, ////assert_eq!(Outset::Y(1, test).layout(area)?, Some([50, 49, 3, 5]));
//Some([0, 0, 3, 1])); ////assert_eq!(Outset::XY(1, 1, test).layout(area)?, Some([49, 49, 5, 5]));
//assert_eq!(Layers::new(|add|{add(&"1")?;add(&"333")}).layout(area)?, ////Ok(())
//Some([0, 0, 3, 1])); ////}
//assert_eq!(Stack::down(|add|{add(&"1")?;add(&"333")}).layout(area)?,
//Some([0, 0, 3, 2])); ////#[test]
//assert_eq!(Stack::right(|add|{add(&"1")?;add(&"333")}).layout(area)?, ////fn test_inset () -> Usually<()> {
//Some([0, 0, 4, 1])); ////let area: [u16;4] = [50, 50, 100, 100];
//assert_eq!(Stack::down(|add|{ ////let test = TestArea(3, 3);
//add(&Stack::right(|add|{add(&"1")?;add(&"333")}))?; ////assert_eq!(Inset::X(1, test).layout(area)?, Some([51, 50, 1, 3]));
//add(&"55555") ////assert_eq!(Inset::Y(1, test).layout(area)?, Some([50, 51, 3, 1]));
//}).layout(area)?, ////assert_eq!(Inset::XY(1, 1, test).layout(area)?, Some([51, 51, 1, 1]));
//Some([0, 0, 5, 2])); ////Ok(())
//let area: [u16;4] = [1, 1, 100, 100]; ////}
//assert_eq!(Outset::X(1, Stack::right(|add|{add(&"1")?;add(&"333")})).layout(area)?,
//Some([0, 1, 6, 1])); ////#[test]
//assert_eq!(Outset::Y(1, Stack::right(|add|{add(&"1")?;add(&"333")})).layout(area)?, ////fn test_stuff () -> Usually<()> {
//Some([1, 0, 4, 3])); ////let area: [u16;4] = [0, 0, 100, 100];
//assert_eq!(Outset::XY(1, 1, Stack::right(|add|{add(&"1")?;add(&"333")})).layout(area)?, ////assert_eq!("1".layout(area)?,
//Some([0, 0, 6, 3])); ////Some([0, 0, 1, 1]));
//assert_eq!(Stack::down(|add|{ ////assert_eq!("333".layout(area)?,
//add(&Outset::XY(1, 1, "1"))?; ////Some([0, 0, 3, 1]));
//add(&Outset::XY(1, 1, "333")) ////assert_eq!(Layers::new(|add|{add(&"1")?;add(&"333")}).layout(area)?,
//}).layout(area)?, ////Some([0, 0, 3, 1]));
//Some([1, 1, 5, 6])); ////assert_eq!(Stack::down(|add|{add(&"1")?;add(&"333")}).layout(area)?,
//let area: [u16;4] = [1, 1, 95, 100]; ////Some([0, 0, 3, 2]));
//assert_eq!(Align::Center(Stack::down(|add|{ ////assert_eq!(Stack::right(|add|{add(&"1")?;add(&"333")}).layout(area)?,
//add(&Outset::XY(1, 1, "1"))?; ////Some([0, 0, 4, 1]));
//add(&Outset::XY(1, 1, "333")) ////assert_eq!(Stack::down(|add|{
//})).layout(area)?, ////add(&Stack::right(|add|{add(&"1")?;add(&"333")}))?;
//Some([46, 48, 5, 6])); ////add(&"55555")
//assert_eq!(Align::Center(Stack::down(|add|{ ////}).layout(area)?,
//add(&Layers::new(|add|{ ////Some([0, 0, 5, 2]));
////add(&Outset::XY(1, 1, Background(Color::Rgb(0,128,0))))?; ////let area: [u16;4] = [1, 1, 100, 100];
//add(&Outset::XY(1, 1, "1"))?; ////assert_eq!(Outset::X(1, Stack::right(|add|{add(&"1")?;add(&"333")})).layout(area)?,
//add(&Outset::XY(1, 1, "333"))?; ////Some([0, 1, 6, 1]));
////add(&Background(Color::Rgb(0,128,0)))?; ////assert_eq!(Outset::Y(1, Stack::right(|add|{add(&"1")?;add(&"333")})).layout(area)?,
//Ok(()) ////Some([1, 0, 4, 3]));
//}))?; ////assert_eq!(Outset::XY(1, 1, Stack::right(|add|{add(&"1")?;add(&"333")})).layout(area)?,
//add(&Layers::new(|add|{ ////Some([0, 0, 6, 3]));
////add(&Outset::XY(1, 1, Background(Color::Rgb(0,0,128))))?; ////assert_eq!(Stack::down(|add|{
//add(&Outset::XY(1, 1, "555"))?; ////add(&Outset::XY(1, 1, "1"))?;
//add(&Outset::XY(1, 1, "777777"))?; ////add(&Outset::XY(1, 1, "333"))
////add(&Background(Color::Rgb(0,0,128)))?; ////}).layout(area)?,
//Ok(()) ////Some([1, 1, 5, 6]));
//})) ////let area: [u16;4] = [1, 1, 95, 100];
//})).layout(area)?, ////assert_eq!(Align::Center(Stack::down(|add|{
//Some([46, 48, 5, 6])); ////add(&Outset::XY(1, 1, "1"))?;
//Ok(()) ////add(&Outset::XY(1, 1, "333"))
//} ////})).layout(area)?,
////Some([46, 48, 5, 6]));
////assert_eq!(Align::Center(Stack::down(|add|{
////add(&Layers::new(|add|{
//////add(&Outset::XY(1, 1, Background(Color::Rgb(0,128,0))))?;
////add(&Outset::XY(1, 1, "1"))?;
////add(&Outset::XY(1, 1, "333"))?;
//////add(&Background(Color::Rgb(0,128,0)))?;
////Ok(())
////}))?;
////add(&Layers::new(|add|{
//////add(&Outset::XY(1, 1, Background(Color::Rgb(0,0,128))))?;
////add(&Outset::XY(1, 1, "555"))?;
////add(&Outset::XY(1, 1, "777777"))?;
//////add(&Background(Color::Rgb(0,0,128)))?;
////Ok(())
////}))
////})).layout(area)?,
////Some([46, 48, 5, 6]));
////Ok(())
////}

View file

@ -428,12 +428,12 @@ impl PerfModel {
} }
} }
#[cfg(test)] //#[cfg(test)]
mod test { //mod test {
use super::*; //use super::*;
#[test] //#[test]
fn test_samples_to_ticks () { //fn test_samples_to_ticks () {
let ticks = Ticks(12.3).between_samples(0, 100).collect::<Vec<_>>(); //let ticks = Ticks(12.3).between_samples(0, 100).collect::<Vec<_>>();
println!("{ticks:?}"); //println!("{ticks:?}");
} //}
} //}