big ass refactor (rip client)

This commit is contained in:
🪞👃🪞 2024-07-03 14:51:48 +03:00
parent 94c1f83ef2
commit 8c3cf53c67
56 changed files with 2232 additions and 1891 deletions

View file

@ -1,189 +0,0 @@
use crate::core::*;
use super::*;
pub trait Focus {
fn unfocus (&mut self);
fn focused (&self) -> Option<&Box<dyn Device>>;
fn focused_mut (&mut self) -> Option<&mut Box<dyn Device>>;
fn handle_focus (&mut self, event: &FocusEvent) -> Usually<bool>;
}
pub enum FocusEvent { Forward, Backward, Inward, Outward, }
pub fn handle_focus <T: Focus> (
state: &mut T,
event: &AppEvent,
keymap: &[KeyBinding<T>]
) -> Usually<bool> {
let handled = if let Some(focused) = state.focused_mut() {
focused.handle(event)
} else {
Ok(false)
};
return Ok(handled? || handle_keymap(
state, event, keymap
)?)
}
pub struct FocusColumn(pub Option<usize>, pub Column);
impl Handle for FocusColumn {
fn handle (&mut self, event: &AppEvent) -> Usually<bool> {
handle_focus(self, event, KEYMAP_FOCUS_COLUMN)
}
}
impl Render for FocusColumn {
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
let (rect, _rects) = Column::draw(buf, area, self.1.0.as_ref(), 0)?;
//if i == self.focus {
//if self.focused {
//draw_box_styled(buf, result, Some(Style::default().white().not_dim()))
//} else {
//draw_box_styled_dotted(buf, result, Some(Style::default().white().dim()))
//};
//};
Ok(rect)
}
}
const KEYMAP_FOCUS_COLUMN: &'static [KeyBinding<FocusColumn>] = keymap!(FocusColumn {
[Up, NONE, "focus_up", "focus row above",
|s: &mut FocusColumn|s.handle_focus(&FocusEvent::Backward)],
[Down, NONE, "focus_down", "focus row below",
|s: &mut FocusColumn|s.handle_focus(&FocusEvent::Forward)],
[Enter, NONE, "focus_down", "focus row below",
|s: &mut FocusColumn|s.handle_focus(&FocusEvent::Inward)],
[Esc, NONE, "focus_down", "focus row below",
|s: &mut FocusColumn|s.handle_focus(&FocusEvent::Outward)]
});
impl Focus for FocusColumn {
fn unfocus (&mut self) {
self.0 = None
}
fn focused (&self) -> Option<&Box<dyn Device>> {
self.0.map(|index|self.1.0.get(index))?
}
fn focused_mut (&mut self) -> Option<&mut Box<dyn Device>> {
self.0.map(|index|self.1.0.get_mut(index))?
}
fn handle_focus (&mut self, event: &FocusEvent) -> Usually<bool> {
Ok(match event {
FocusEvent::Backward => match self.0 {
Some(i) => {
self.0 = Some(if i == 0 {
self.1.0.len() - 1
} else {
i - 1
});
true
},
_ => false
},
FocusEvent::Forward => match self.0 {
Some(i) => {
self.0 = Some(if i >= self.1.0.len() {
0
} else {
i + 1
});
true
},
_ => false
},
FocusEvent::Inward => match self.0 {
None => {
self.0 = Some(0);
true
},
_ => false
},
FocusEvent::Outward => match self.0 {
Some(_i) => {
self.0 = None;
true
},
_ => false
},
})
}
}
pub struct FocusRow(pub Option<usize>, pub Row);
impl Handle for FocusRow {
fn handle (&mut self, event: &AppEvent) -> Usually<bool> {
handle_focus(self, event, KEYMAP_FOCUS_ROW)
}
}
impl Render for FocusRow {
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
let (rect, _rects) = Row::draw(buf, area, &self.1.0, 0)?;
Ok(rect)
}
}
const KEYMAP_FOCUS_ROW: &'static [KeyBinding<FocusRow>] = keymap!(FocusRow {
[Left, NONE, "focus_up", "focus row above",
|s: &mut FocusRow|s.handle_focus(&FocusEvent::Backward)],
[Right, NONE, "focus_down", "focus row below",
|s: &mut FocusRow|s.handle_focus(&FocusEvent::Forward)],
[Enter, NONE, "focus_down", "focus row below",
|s: &mut FocusRow|s.handle_focus(&FocusEvent::Inward)],
[Esc, NONE, "focus_down", "focus row below",
|s: &mut FocusRow|s.handle_focus(&FocusEvent::Outward)]
});
impl Focus for FocusRow {
fn unfocus (&mut self) {
self.0 = None
}
fn focused (&self) -> Option<&Box<dyn Device>> {
self.0.map(|index|self.1.0.get(index))?
}
fn focused_mut (&mut self) -> Option<&mut Box<dyn Device>> {
self.0.map(|index|self.1.0.get_mut(index))?
}
fn handle_focus (&mut self, event: &FocusEvent) -> Usually<bool> {
Ok(match event {
FocusEvent::Backward => match self.0 {
Some(i) => {
self.0 = Some(if i == 0 {
self.1.0.len() - 1
} else {
i - 1
});
true
},
_ => false
},
FocusEvent::Forward => match self.0 {
Some(i) => {
self.0 = Some(if i >= self.1.0.len() {
0
} else {
i + 1
});
true
},
_ => false
},
FocusEvent::Inward => match self.0 {
None => {
self.0 = Some(0);
true
},
_ => false
},
FocusEvent::Outward => match self.0 {
Some(_i) => {
self.0 = None;
true
},
_ => false
},
})
}
}