fix vertical multi-step focus

This commit is contained in:
🪞👃🪞 2024-11-25 22:20:13 +01:00
parent f5f2a3545f
commit bbf9ec0afd
2 changed files with 59 additions and 19 deletions

View file

@ -106,30 +106,61 @@ pub trait FocusGrid: HasFocus {
self.focus_layout()[y][x]
}
fn focus_update (&mut self) {
self.focus_to(self.focus_current())
self.focus_to(self.focus_current());
self.focus_updated()
}
fn focus_up (&mut self) {
let layout = self.focus_layout();
let (x, y) = self.focus_cursor();
let next_y = if y == 0 { layout.len().saturating_sub(1) } else { y - 1 };
let next_x = if layout[y].len() == layout[next_y].len() { x } else {
((x as f32 / layout[y].len() as f32) * layout[next_y].len() as f32) as usize
};
*self.focus_cursor_mut() = (next_x, next_y);
let original_focused = self.focused();
let (_, original_y) = self.focus_cursor();
loop {
let (x, y) = self.focus_cursor();
let next_y = if y == 0 {
self.focus_layout().len().saturating_sub(1)
} else {
y - 1
};
if next_y == original_y {
break
}
let next_x = if self.focus_layout()[y].len() == self.focus_layout()[next_y].len() {
x
} else {
((x as f32 / self.focus_layout()[original_y].len() as f32) * self.focus_layout()[next_y].len() as f32) as usize
};
*self.focus_cursor_mut() = (next_x, next_y);
if self.focus_current() != original_focused {
break
}
}
self.focus_update();
}
fn focus_down (&mut self) {
let layout = self.focus_layout();
let (x, y) = self.focus_cursor();
let next_y = if y >= layout.len().saturating_sub(1) { 0 } else { y + 1 };
let next_x = if layout[y].len() == layout[next_y].len() { x } else {
((x as f32 / layout[y].len() as f32) * layout[next_y].len() as f32) as usize
};
*self.focus_cursor_mut() = (next_x, next_y);
let original_focused = self.focused();
let (_, original_y) = self.focus_cursor();
loop {
let (x, y) = self.focus_cursor();
let next_y = if y >= self.focus_layout().len().saturating_sub(1) {
0
} else {
y + 1
};
if next_y == original_y {
break
}
let next_x = if self.focus_layout()[y].len() == self.focus_layout()[next_y].len() {
x
} else {
((x as f32 / self.focus_layout()[original_y].len() as f32) * self.focus_layout()[next_y].len() as f32) as usize
};
*self.focus_cursor_mut() = (next_x, next_y);
if self.focus_current() != original_focused {
break
}
}
self.focus_update();
}
fn focus_left (&mut self) {
let focused = self.focused();
let original_focused = self.focused();
let (original_x, y) = self.focus_cursor();
loop {
let x = self.focus_cursor().0;
@ -142,14 +173,14 @@ pub trait FocusGrid: HasFocus {
break
}
*self.focus_cursor_mut() = (next_x, y);
if self.focus_current() != focused {
if self.focus_current() != original_focused {
break
}
}
self.focus_update();
}
fn focus_right (&mut self) {
let focused = self.focused();
let original_focused = self.focused();
let (original_x, y) = self.focus_cursor();
loop {
let x = self.focus_cursor().0;
@ -162,7 +193,7 @@ pub trait FocusGrid: HasFocus {
break
}
self.focus_cursor_mut().0 = next_x;
if self.focus_current() != focused {
if self.focus_current() != original_focused {
break
}
}

View file

@ -854,6 +854,15 @@ where
})
}
}
#[macro_export] macro_rules! col_up {
($($expr:expr),* $(,)?) => { Stack::down(move|add|{ $(add(&$expr)?;)* Ok(()) }) };
($pat:pat in $collection:expr => $item:expr) => {
Stack::up(move |add|{
for $pat in $collection { add(&$item)?; }
Ok(())
})
}
}
#[macro_export] macro_rules! row {
($($expr:expr),* $(,)?) => { Stack::right(move|add|{ $(add(&$expr)?;)* Ok(()) }) };
($pat:pat in $collection:expr => $item:expr) => {