mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 20:26:42 +01:00
add focusing to chain; nested focus works for the most part
This commit is contained in:
parent
7b568c55b8
commit
5082bf9fdf
4 changed files with 98 additions and 21 deletions
|
|
@ -2,16 +2,18 @@ use crate::prelude::*;
|
|||
|
||||
pub struct Chain {
|
||||
name: String,
|
||||
devices: Vec<Box<dyn Device>>,
|
||||
focused: usize,
|
||||
focused: bool,
|
||||
focus: usize,
|
||||
items: Vec<Box<dyn Device>>,
|
||||
}
|
||||
|
||||
impl Chain {
|
||||
pub fn new (name: &str, devices: Vec<Box<dyn Device>>) -> Result<DynamicDevice<Self>, Box<dyn Error>> {
|
||||
pub fn new (name: &str, items: Vec<Box<dyn Device>>) -> Result<DynamicDevice<Self>, Box<dyn Error>> {
|
||||
Ok(DynamicDevice::new(render, handle, process, Self {
|
||||
name: name.into(),
|
||||
devices,
|
||||
focused: 0
|
||||
focused: false,
|
||||
focus: 0,
|
||||
items,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
|
@ -30,13 +32,20 @@ pub fn render (state: &Chain, buf: &mut Buffer, area: Rect)
|
|||
buf.set_string(area.x + area.width - 1, y, "│", Style::default().black());
|
||||
buf.set_string(area.x + 2, y, "Input...", Style::default().dim());
|
||||
let mut x = 0u16;
|
||||
for device in state.devices.iter() {
|
||||
for (i, device) in state.items.iter().enumerate() {
|
||||
let result = device.render(buf, Rect {
|
||||
x: area.x,
|
||||
y,
|
||||
width: area.width,
|
||||
height: 21//area.height.saturating_sub(y)
|
||||
})?;
|
||||
if i == state.focus {
|
||||
if state.focused {
|
||||
draw_box_styled(buf, result, Some(Style::default().green().not_dim()))
|
||||
} else {
|
||||
draw_box_styled_dotted(buf, result, Some(Style::default().green().dim()))
|
||||
};
|
||||
};
|
||||
//let result = Rect { x: 0, y: 0, width: result.width, height: 21 };
|
||||
x = x.max(result.width);
|
||||
y = y + result.height;
|
||||
|
|
@ -57,6 +66,55 @@ pub fn render (state: &Chain, buf: &mut Buffer, area: Rect)
|
|||
}))
|
||||
}
|
||||
|
||||
pub fn handle (_: &mut Chain, _: &AppEvent) -> Usually<bool> {
|
||||
Ok(false)
|
||||
impl Focus for Chain {
|
||||
fn focused (&self) -> Option<&Box<dyn Device>> {
|
||||
match self.focused {
|
||||
true => self.items.get(self.focus),
|
||||
false => None
|
||||
}
|
||||
}
|
||||
fn focused_mut (&mut self) -> Option<&mut Box<dyn Device>> {
|
||||
match self.focused {
|
||||
true => self.items.get_mut(self.focus),
|
||||
false => None
|
||||
}
|
||||
}
|
||||
fn handle_focus (&mut self, event: &FocusEvent) -> Usually<bool> {
|
||||
match event {
|
||||
FocusEvent::Backward => {
|
||||
if self.focus == 0 {
|
||||
self.focus = self.items.len();
|
||||
}
|
||||
self.focus = self.focus - 1;
|
||||
},
|
||||
FocusEvent::Forward => {
|
||||
self.focus = self.focus + 1;
|
||||
if self.focus >= self.items.len() {
|
||||
self.focus = 0;
|
||||
}
|
||||
},
|
||||
FocusEvent::Inward => {
|
||||
self.focused = true;
|
||||
self.items[self.focus].handle(&AppEvent::Focus)?;
|
||||
},
|
||||
FocusEvent::Outward => {
|
||||
self.focused = false;
|
||||
self.items[self.focus].handle(&AppEvent::Blur)?;
|
||||
},
|
||||
};
|
||||
Ok(true)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn handle (state: &mut Chain, event: &AppEvent) -> Usually<bool> {
|
||||
handle_focus(state, event, keymap!(Chain {
|
||||
[Up, NONE, "focus_up", "focus row above",
|
||||
|s: &mut Chain|s.handle_focus(&FocusEvent::Backward)],
|
||||
[Down, NONE, "focus_down", "focus row below",
|
||||
|s: &mut Chain|s.handle_focus(&FocusEvent::Forward)],
|
||||
[Enter, NONE, "focus_down", "focus row below",
|
||||
|s: &mut Chain|s.handle_focus(&FocusEvent::Inward)],
|
||||
[Esc, NONE, "focus_down", "focus row below",
|
||||
|s: &mut Chain|s.handle_focus(&FocusEvent::Outward)]
|
||||
}))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue