mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 12:16:42 +01:00
wip: p.42, e=22, more intermediary trait layers
This commit is contained in:
parent
638298ad32
commit
dbf6e353b7
15 changed files with 937 additions and 688 deletions
|
|
@ -12,7 +12,7 @@ pub enum FocusCommand {
|
|||
Exit
|
||||
}
|
||||
|
||||
impl<F: FocusGrid> Command<F> for FocusCommand {
|
||||
impl<F: FocusOrder + FocusGrid + FocusEnter> Command<F> for FocusCommand {
|
||||
fn execute (self, state: &mut F) -> Perhaps<FocusCommand> {
|
||||
use FocusCommand::*;
|
||||
match self {
|
||||
|
|
@ -24,93 +24,124 @@ impl<F: FocusGrid> Command<F> for FocusCommand {
|
|||
Right => { state.focus_right(); },
|
||||
Enter => { state.focus_enter(); },
|
||||
Exit => { state.focus_exit(); },
|
||||
_ => {}
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait FocusGrid {
|
||||
pub trait HasFocus {
|
||||
type Item: Copy + PartialEq + Debug;
|
||||
fn focused (&self) -> Self::Item;
|
||||
fn focus (&mut self, target: Self::Item);
|
||||
}
|
||||
|
||||
pub trait FocusOrder {
|
||||
fn focus_next (&mut self);
|
||||
fn focus_prev (&mut self);
|
||||
}
|
||||
|
||||
pub trait FocusEnter {
|
||||
type Item: Copy + PartialEq + Debug;
|
||||
fn layout (&self) -> &[&[Self::Item]];
|
||||
fn cursor (&self) -> (usize, usize);
|
||||
fn cursor_mut (&mut self) -> &mut (usize, usize);
|
||||
fn update_focus (&mut self) {}
|
||||
fn focus_enter (&mut self) {}
|
||||
fn focus_exit (&mut self) {}
|
||||
fn entered (&self) -> Option<Self::Item>;
|
||||
fn focus_entered (&self) -> Option<Self::Item>;
|
||||
}
|
||||
|
||||
pub trait FocusGrid {
|
||||
type Item: Copy + PartialEq + Debug;
|
||||
fn focus_layout (&self) -> &[&[Self::Item]];
|
||||
fn focus_cursor (&self) -> (usize, usize);
|
||||
fn focus_cursor_mut (&mut self) -> &mut (usize, usize);
|
||||
fn focus_update (&mut self) {}
|
||||
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);
|
||||
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);
|
||||
self.focus_update();
|
||||
}
|
||||
fn focus_left (&mut self) {
|
||||
let layout = self.focus_layout();
|
||||
let (x, y) = self.focus_cursor();
|
||||
let next_x = if x == 0 { layout[y].len().saturating_sub(1) } else { x - 1 };
|
||||
*self.focus_cursor_mut() = (next_x, y);
|
||||
self.focus_update();
|
||||
}
|
||||
fn focus_right (&mut self) {
|
||||
let layout = self.focus_layout();
|
||||
let (x, y) = self.focus_cursor();
|
||||
let next_x = if x >= layout[y].len().saturating_sub(1) { 0 } else { x + 1 };
|
||||
*self.focus_cursor_mut() = (next_x, y);
|
||||
self.focus_update();
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, U> HasFocus for U
|
||||
where
|
||||
T: Copy + PartialEq + Debug,
|
||||
U: FocusGrid<Item = T> + FocusOrder,
|
||||
{
|
||||
type Item = T;
|
||||
fn focused (&self) -> Self::Item {
|
||||
let (x, y) = self.cursor();
|
||||
self.layout()[y][x]
|
||||
let (x, y) = self.focus_cursor();
|
||||
self.focus_layout()[y][x]
|
||||
}
|
||||
fn focus (&mut self, target: Self::Item) {
|
||||
while self.focused() != target {
|
||||
self.focus_next()
|
||||
}
|
||||
}
|
||||
fn focus_up (&mut self) {
|
||||
let layout = self.layout();
|
||||
let (x, y) = self.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.cursor_mut() = (next_x, next_y);
|
||||
self.update_focus();
|
||||
}
|
||||
fn focus_down (&mut self) {
|
||||
let layout = self.layout();
|
||||
let (x, y) = self.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.cursor_mut() = (next_x, next_y);
|
||||
self.update_focus();
|
||||
}
|
||||
fn focus_left (&mut self) {
|
||||
let layout = self.layout();
|
||||
let (x, y) = self.cursor();
|
||||
let next_x = if x == 0 { layout[y].len().saturating_sub(1) } else { x - 1 };
|
||||
*self.cursor_mut() = (next_x, y);
|
||||
self.update_focus();
|
||||
}
|
||||
fn focus_right (&mut self) {
|
||||
let layout = self.layout();
|
||||
let (x, y) = self.cursor();
|
||||
let next_x = if x >= layout[y].len().saturating_sub(1) { 0 } else { x + 1 };
|
||||
*self.cursor_mut() = (next_x, y);
|
||||
self.update_focus();
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, U> FocusOrder for U
|
||||
where
|
||||
T: Copy + PartialEq + Debug,
|
||||
U: HasFocus<Item = T> + FocusGrid<Item = T> + FocusEnter
|
||||
{
|
||||
fn focus_next (&mut self) {
|
||||
let current = self.focused();
|
||||
let (x, y) = self.cursor();
|
||||
if x < self.layout()[y].len().saturating_sub(1) {
|
||||
let (x, y) = self.focus_cursor();
|
||||
if x < self.focus_layout()[y].len().saturating_sub(1) {
|
||||
self.focus_right();
|
||||
} else {
|
||||
self.focus_down();
|
||||
self.cursor_mut().0 = 0;
|
||||
self.focus_cursor_mut().0 = 0;
|
||||
}
|
||||
if self.focused() == current { // FIXME: prevent infinite loop
|
||||
self.focus_next()
|
||||
}
|
||||
self.focus_exit();
|
||||
self.update_focus();
|
||||
self.focus_update();
|
||||
}
|
||||
fn focus_prev (&mut self) {
|
||||
let current = self.focused();
|
||||
let (x, _) = self.cursor();
|
||||
let (x, _) = self.focus_cursor();
|
||||
if x > 0 {
|
||||
self.focus_left();
|
||||
} else {
|
||||
self.focus_up();
|
||||
let (_, y) = self.cursor();
|
||||
let next_x = self.layout()[y].len().saturating_sub(1);
|
||||
self.cursor_mut().0 = next_x;
|
||||
let (_, y) = self.focus_cursor();
|
||||
let next_x = self.focus_layout()[y].len().saturating_sub(1);
|
||||
self.focus_cursor_mut().0 = next_x;
|
||||
}
|
||||
if self.focused() == current { // FIXME: prevent infinite loop
|
||||
self.focus_prev()
|
||||
}
|
||||
self.focus_exit();
|
||||
self.update_focus();
|
||||
self.focus_update();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -883,6 +883,7 @@ impl<E: Engine> Measure<E> {
|
|||
pub fn set_h (&self, h: impl Into<usize>) { self.2.store(h.into(), Ordering::Relaxed) }
|
||||
pub fn set_wh (&self, w: impl Into<usize>, h: impl Into<usize>) { self.set_w(w); self.set_h(h); }
|
||||
pub fn new () -> Self { Self(PhantomData::default(), 0.into(), 0.into()) }
|
||||
pub fn format (&self) -> String { format!("{}x{}", self.w(), self.h()) }
|
||||
}
|
||||
|
||||
impl<E: Engine> Widget for Measure<E> {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue