wip: component refactor

This commit is contained in:
🪞👃🪞 2024-06-21 12:13:11 +03:00
parent 5082bf9fdf
commit c18aa2cbbd
14 changed files with 374 additions and 272 deletions

View file

@ -7,6 +7,7 @@ mod sampler;
mod mixer; mod mixer;
mod looper; mod looper;
mod plugin; mod plugin;
mod launcher;
pub use self::transport::Transport; pub use self::transport::Transport;
pub use self::chain::Chain; pub use self::chain::Chain;
@ -15,6 +16,7 @@ pub use self::sampler::Sampler;
pub use self::mixer::Mixer; pub use self::mixer::Mixer;
pub use self::looper::Looper; pub use self::looper::Looper;
pub use self::plugin::Plugin; pub use self::plugin::Plugin;
pub use self::launcher::Launcher;
use crossterm::event; use crossterm::event;

View file

@ -67,6 +67,9 @@ pub fn render (state: &Chain, buf: &mut Buffer, area: Rect)
} }
impl Focus for Chain { impl Focus for Chain {
fn unfocus (&mut self) {
self.focused = false
}
fn focused (&self) -> Option<&Box<dyn Device>> { fn focused (&self) -> Option<&Box<dyn Device>> {
match self.focused { match self.focused {
true => self.items.get(self.focus), true => self.items.get(self.focus),
@ -80,29 +83,36 @@ impl Focus for Chain {
} }
} }
fn handle_focus (&mut self, event: &FocusEvent) -> Usually<bool> { fn handle_focus (&mut self, event: &FocusEvent) -> Usually<bool> {
match event { Ok(match event {
FocusEvent::Backward => { FocusEvent::Backward => {
if self.focus == 0 { if self.focus == 0 {
self.focus = self.items.len(); self.focus = self.items.len();
} }
self.focus = self.focus - 1; self.focus = self.focus - 1;
true
}, },
FocusEvent::Forward => { FocusEvent::Forward => {
self.focus = self.focus + 1; self.focus = self.focus + 1;
if self.focus >= self.items.len() { if self.focus >= self.items.len() {
self.focus = 0; self.focus = 0;
} }
true
}, },
FocusEvent::Inward => { FocusEvent::Inward => {
self.focused = true; self.focused = true;
self.items[self.focus].handle(&AppEvent::Focus)?; self.items[self.focus].handle(&AppEvent::Focus)?;
true
}, },
FocusEvent::Outward => { FocusEvent::Outward => {
if self.focused {
self.focused = false; self.focused = false;
self.items[self.focus].handle(&AppEvent::Blur)?; self.items[self.focus].handle(&AppEvent::Blur)?;
true
} else {
false
}
}, },
}; })
Ok(true)
} }
} }

View file

@ -6,7 +6,7 @@ pub struct Launcher {
impl Launcher { impl Launcher {
pub fn new (name: &str) -> Result<DynamicDevice<Self>, Box<dyn Error>> { pub fn new (name: &str) -> Result<DynamicDevice<Self>, Box<dyn Error>> {
Ok(DynamicDevice::new(render, handle, Self { Ok(DynamicDevice::new(render, handle, process, Self {
name: name.into(), name: name.into(),
})) }))
} }
@ -16,10 +16,23 @@ pub fn process (_: &mut Launcher, _: &Client, _: &ProcessScope) -> Control {
Control::Continue Control::Continue
} }
pub fn render (_: &Launcher, _: &mut Buffer, _: Rect) -> Usually<Rect> { pub fn render (_: &Launcher, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
Ok(Rect::default()) for i in 1..=8 {
buf.set_string(area.x + 2 + (i-1) * 10, area.y, format!("Track#{i} | "), Style::default())
}
for i in 0..=7 {
for j in 0..=7 {
buf.set_string(area.x + 2 + i * 10, area.y + 2 + j, format!("······· | "), Style::default().dim())
}
}
Ok(draw_box(buf, Rect {
x: area.x,
y: area.y - 1,
width: area.width,
height: 12
}))
} }
pub fn handle (_: &mut Launcher, _: &AppEvent) -> Result<(), Box<dyn Error>> { pub fn handle (_: &mut Launcher, _: &AppEvent) -> Usually<bool> {
Ok(()) Ok(false)
} }

View file

@ -7,7 +7,8 @@ mod vst3;
pub struct Plugin { pub struct Plugin {
name: String, name: String,
path: String, path: String,
plugin: Option<PluginKind> plugin: Option<PluginKind>,
parameter_offset: usize,
} }
enum PluginKind { enum PluginKind {
@ -24,6 +25,7 @@ impl Plugin {
name: name.into(), name: name.into(),
path: HELM.into(), path: HELM.into(),
plugin: None, plugin: None,
parameter_offset: 0,
}); });
device.state.lock().unwrap().plugin = Some(self::lv2::plug_in(HELM)?); device.state.lock().unwrap().plugin = Some(self::lv2::plug_in(HELM)?);
Ok(device) Ok(device)
@ -46,7 +48,7 @@ pub fn render (state: &Plugin, buf: &mut Buffer, Rect { x, y, .. }: Rect)
match &state.plugin { match &state.plugin {
Some(PluginKind::LV2(ports, instance)) => { Some(PluginKind::LV2(ports, instance)) => {
let mut height = 3; let mut height = 3;
for (i, port) in ports.iter().enumerate() { for (i, port) in ports.iter().skip(state.parameter_offset).enumerate() {
if i >= 10 { if i >= 10 {
break break
} }
@ -56,7 +58,7 @@ pub fn render (state: &Plugin, buf: &mut Buffer, Rect { x, y, .. }: Rect)
), Style::default()); ), Style::default());
height = height + 1; height = height + 1;
} }
Ok(draw_box(buf, Rect { x, y, width: 40, height })) Ok(draw_box(buf, Rect { x, y, width: 40, height: height.max(10) }))
}, },
_ => { _ => {
buf.set_string(x + 1, y + 3, &format!(" Parameter 1 0.0"), style); buf.set_string(x + 1, y + 3, &format!(" Parameter 1 0.0"), style);
@ -68,6 +70,17 @@ pub fn render (state: &Plugin, buf: &mut Buffer, Rect { x, y, .. }: Rect)
} }
} }
pub fn handle (_: &mut Plugin, _: &AppEvent) -> Usually<bool> { pub fn handle (s: &mut Plugin, event: &AppEvent) -> Usually<bool> {
Ok(false) handle_keymap(s, event, keymap!(Plugin {
[Up, NONE, "cursor_up", "move cursor up",
|s: &mut Plugin|{
s.parameter_offset = s.parameter_offset.saturating_sub(1);
Ok(true)
}],
[Down, NONE, "cursor_down", "move cursor down",
|s: &mut Plugin|{
s.parameter_offset = s.parameter_offset + 1;
Ok(true)
}]
}))
} }

View file

@ -76,7 +76,7 @@ impl Sequencer {
output_port: client.register_port("out", MidiOut::default())?, output_port: client.register_port("out", MidiOut::default())?,
output_connect: vec![], output_connect: vec![],
mode: SequencerView::Vertical, mode: SequencerView::Horizontal,
note_axis: (36, 68), note_axis: (36, 68),
note_cursor: 0, note_cursor: 0,
time_axis: (0, 64), time_axis: (0, 64),
@ -167,7 +167,7 @@ fn process_out (s: &mut Sequencer, scope: &ProcessScope) {
} }
} }
fn render (s: &Sequencer, buf: &mut Buffer, area: Rect) -> Usually<Rect> { fn render (s: &Sequencer, buf: &mut Buffer, mut area: Rect) -> Usually<Rect> {
let Rect { x, y, width, .. } = area; let Rect { x, y, width, .. } = area;
let (time0, time1) = s.time_axis; let (time0, time1) = s.time_axis;
let (note0, note1) = s.note_axis; let (note0, note1) = s.note_axis;
@ -199,7 +199,7 @@ fn render (s: &Sequencer, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
x, x,
y, y,
width: header.width.max(piano.width), width: header.width.max(piano.width),
height: header.height + piano.height + 3 height: header.height + piano.height
})) }))
} }
@ -210,42 +210,55 @@ fn draw_header (s: &Sequencer, buf: &mut Buffer, area: Rect, beat: usize) -> Usu
let steps = s.steps % s.resolution; let steps = s.steps % s.resolution;
let Rect { x, y, .. } = area; let Rect { x, y, .. } = area;
let style = Style::default().gray(); let style = Style::default().gray();
buf.set_string(x + 1, y + 1,
&format!("{rep}.{step:2} / {reps}.{steps}"),
style);
buf.set_string(x + 2, y + 1, buf.set_string(x + 2, y + 1,
&format!("{}", &s.name), &format!("{rep}.{step:2} / {reps}.{steps}"),
style.white().bold());
buf.set_string(x + 1, y + 2,
&format!(" ▶ PLAY │ ⏹ STOP │ │"),
style); style);
buf.set_string(x + 2, y + 2, buf.set_string(x + 2, y + 2,
&format!("▶ PLAY"), if s.playing { &format!("⏹ STOP"),
Style::default().green() style);
} else { //buf.set_string(x + 2, y + 2,
Style::default().dim() //&format!("▶ PLAY"), if s.playing {
}); //Style::default().green()
buf.set_string(x + 24, y + 2, //} else {
//Style::default().dim()
//});
buf.set_string(x + 10, y + 2,
&format!("⏺ REC"), if s.recording { &format!("⏺ REC"), if s.recording {
Style::default().red() Style::default().red()
} else { } else {
Style::default().dim() Style::default().dim()
}); });
buf.set_string(x + 32, y + 2, buf.set_string(x + 17, y + 2,
&format!("⏺ DUB"), if s.overdub { &format!("⏺ DUB"), if s.overdub {
Style::default().yellow() Style::default().yellow()
} else { } else {
Style::default().dim() Style::default().dim()
}); });
Ok(Rect { x, y, width: 39, height: 4 }) let clips = draw_clips(s, buf, area)?;
Ok(Rect { x, y, width: area.width, height: 4 })
}
fn draw_clips (s: &Sequencer, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
let Rect { x, y, .. } = area;
let style = Style::default().gray();
buf.set_string(x + 2, y + 4, &format!("{}", &s.name), style.white().bold());
buf.set_string(x + 2, y + 6, &format!("{}", &s.name), style.dim());
buf.set_string(x + 2, y + 8, &format!("{}", &s.name), style.dim());
buf.set_string(x + 2, y + 10, &format!("{}", &s.name), style.dim());
buf.set_string(x + 2, y + 12, &format!("{}", &s.name), style.dim());
buf.set_string(x + 2, y + 14, &format!("{}", &s.name), style.dim());
buf.set_string(x + 2, y + 16, &format!("{}", &s.name), style.dim());
buf.set_string(x + 2, y + 18, &format!("{}", &s.name), style.dim());
Ok(Rect { x, y, width: 14, height: 14 })
} }
const KEYS_VERTICAL: [&'static str; 6] = [ const KEYS_VERTICAL: [&'static str; 6] = [
"", "", "", "", "", "", "", "", "", "", "", "",
]; ];
fn draw_vertical (s: &Sequencer, buf: &mut Buffer, area: Rect, beat: usize) -> Usually<Rect> { fn draw_vertical (s: &Sequencer, buf: &mut Buffer, mut area: Rect, beat: usize) -> Usually<Rect> {
let ppq = s.timebase.ppq() as u32; let ppq = s.timebase.ppq() as u32;
area.x = area.x + 13;
let Rect { x, y, .. } = area; let Rect { x, y, .. } = area;
let (time0, time1) = s.time_axis; let (time0, time1) = s.time_axis;
let (note0, note1) = s.note_axis; let (note0, note1) = s.note_axis;
@ -369,7 +382,8 @@ const KEY_HORIZONTAL_STYLE: [Style;12] = [
KEY_WHITE, KEY_BLACK, KEY_WHITE, KEY_BLACK, KEY_WHITE, KEY_BLACK, KEY_WHITE, KEY_WHITE, KEY_BLACK, KEY_WHITE, KEY_BLACK, KEY_WHITE, KEY_BLACK, KEY_WHITE,
]; ];
fn draw_horizontal (s: &Sequencer, buf: &mut Buffer, area: Rect) -> Usually<Rect> { fn draw_horizontal (s: &Sequencer, buf: &mut Buffer, mut area: Rect) -> Usually<Rect> {
area.x = area.x + 13;
let Rect { x, y, .. } = area; let Rect { x, y, .. } = area;
let (time0, time1) = s.time_axis; let (time0, time1) = s.time_axis;
let (note0, note1) = s.note_axis; let (note0, note1) = s.note_axis;
@ -377,9 +391,9 @@ fn draw_horizontal (s: &Sequencer, buf: &mut Buffer, area: Rect) -> Usually<Rect
let bg = Style::default().on_black(); let bg = Style::default().on_black();
for i in 0..32.max(note1-note0)/2 { for i in 0..32.max(note1-note0)/2 {
let y = y + i; let y = y + i;
buf.set_string(x + 1, y, KEYS_VERTICAL[(i % 6) as usize], bw); buf.set_string(x + 2, y, KEYS_VERTICAL[(i % 6) as usize], bw);
buf.set_string(x + 2, y, "", bw); buf.set_string(x + 3, y, "", bw);
buf.set_string(x + 5, y, &" ".repeat((time1 - time0) as usize), bg); buf.set_string(x + 6, y, &"·".repeat((time1 - time0) as usize), bg.dim());
if i % 6 == 0 { if i % 6 == 0 {
let octave = format!("C{}", ((note1 - i) / 6) as i8 - 4); let octave = format!("C{}", ((note1 - i) / 6) as i8 - 4);
buf.set_string(x+5, y, &octave, Style::default()); buf.set_string(x+5, y, &octave, Style::default());
@ -395,7 +409,7 @@ fn draw_horizontal (s: &Sequencer, buf: &mut Buffer, area: Rect) -> Usually<Rect
} }
} }
let height = 32.max(note1 - note0) / 2; let height = 32.max(note1 - note0) / 2;
buf.set_string(x + 2, y + height + 1, format!( buf.set_string(x + 2, y + height, format!(
" Q 1/{} | N {} ({}-{}) | T {} ({}-{})", " Q 1/{} | N {} ({}-{}) | T {} ({}-{})",
4 * s.resolution, 4 * s.resolution,
s.note_axis.0 + s.note_cursor, s.note_axis.0 + s.note_cursor,
@ -411,7 +425,12 @@ fn draw_horizontal (s: &Sequencer, buf: &mut Buffer, area: Rect) -> Usually<Rect
if s.note_cursor % 2 == 0 { "" } else { "" }, if s.note_cursor % 2 == 0 { "" } else { "" },
Style::default() Style::default()
); );
Ok(Rect { x, y, width: time1 - time0 + 6, height}) Ok(Rect {
x: x - 13,
y,
width: time1 - time0 + 19,
height: height + 2
})
} }
pub fn handle (s: &mut Sequencer, event: &AppEvent) -> Usually<bool> { pub fn handle (s: &mut Sequencer, event: &AppEvent) -> Usually<bool> {
@ -495,7 +514,7 @@ fn cursor_up (s: &mut Sequencer) -> Usually<bool> {
match s.mode { match s.mode {
SequencerView::Vertical => time_cursor_dec(s), SequencerView::Vertical => time_cursor_dec(s),
SequencerView::Horizontal => note_cursor_dec(s), SequencerView::Horizontal => note_cursor_dec(s),
_ => unimplemented!() _ => Ok(false)
}; };
Ok(true) Ok(true)
} }
@ -503,7 +522,7 @@ fn cursor_down (s: &mut Sequencer) -> Usually<bool> {
match s.mode { match s.mode {
SequencerView::Vertical => time_cursor_inc(s), SequencerView::Vertical => time_cursor_inc(s),
SequencerView::Horizontal => note_cursor_inc(s), SequencerView::Horizontal => note_cursor_inc(s),
_ => unimplemented!() _ => Ok(false)
}; };
Ok(true) Ok(true)
} }
@ -511,7 +530,7 @@ fn cursor_left (s: &mut Sequencer) -> Usually<bool> {
match s.mode { match s.mode {
SequencerView::Vertical => note_cursor_dec(s), SequencerView::Vertical => note_cursor_dec(s),
SequencerView::Horizontal => time_cursor_dec(s), SequencerView::Horizontal => time_cursor_dec(s),
_ => unimplemented!() _ => Ok(false)
}; };
Ok(true) Ok(true)
} }
@ -519,7 +538,7 @@ fn cursor_right (s: &mut Sequencer) -> Usually<bool> {
match s.mode { match s.mode {
SequencerView::Vertical => note_cursor_inc(s), SequencerView::Vertical => note_cursor_inc(s),
SequencerView::Horizontal => time_cursor_inc(s), SequencerView::Horizontal => time_cursor_inc(s),
_ => unimplemented!() _ => Ok(false)
}; };
Ok(true) Ok(true)
} }
@ -539,7 +558,8 @@ impl SequencerView {
fn next (&self) -> Self { fn next (&self) -> Self {
match self { match self {
Self::Horizontal => Self::Vertical, Self::Horizontal => Self::Vertical,
Self::Vertical => Self::Horizontal, Self::Vertical => Self::Tiny,
Self::Tiny => Self::Horizontal,
_ => self.clone() _ => self.clone()
} }
} }

View file

@ -72,12 +72,12 @@ pub fn render (state: &Transport, buf: &mut Buffer, mut area: Rect)
"STOP", "STOP",
"REC", "REC",
"DUB", "DUB",
"0.0.00",
"0:00.000",
&format!("BPM {:03}.{:03}", &format!("BPM {:03}.{:03}",
state.timebase.tempo() / 1000, state.timebase.tempo() / 1000,
state.timebase.tempo() % 1000, state.timebase.tempo() % 1000,
) ),
"0.0+00",
"0:00.000",
].iter() { ].iter() {
buf.set_string(area.x + x, area.y + 1, button, label); buf.set_string(area.x + x, area.y + 1, button, label);
x = x + button.len() as u16 + 1; x = x + button.len() as u16 + 1;

View file

@ -1,208 +0,0 @@
use crate::prelude::*;
pub trait Focus {
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 Rows {
focused: bool,
focus: usize,
items: Vec<Box<dyn Device>>,
}
pub struct Columns {
focused: bool,
focus: usize,
items: Vec<Box<dyn Device>>,
}
impl Rows {
pub fn new (focused: bool, items: Vec<Box<dyn Device>>) -> Self {
Self { focused, focus: 0, items }
}
}
impl Columns {
pub fn new (focused: bool, items: Vec<Box<dyn Device>>) -> Self {
Self { focused, focus: 0, items }
}
}
impl Focus for Rows {
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)
}
}
impl Focus for Columns {
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)
}
}
impl Device for Rows {
fn handle (&mut self, event: &AppEvent) -> Usually<bool> {
handle_focus(self, event, keymap!(Self {
[Up, NONE, "focus_up", "focus row above",
|s: &mut Self|s.handle_focus(&FocusEvent::Backward)],
[Down, NONE, "focus_down", "focus row below",
|s: &mut Self|s.handle_focus(&FocusEvent::Forward)],
[Enter, NONE, "focus_down", "focus row below",
|s: &mut Self|s.handle_focus(&FocusEvent::Inward)],
[Esc, NONE, "focus_down", "focus row below",
|s: &mut Self|s.handle_focus(&FocusEvent::Outward)]
}))
}
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
let mut w = 0u16;
let mut h = 0u16;
for (i, device) in self.items.iter().enumerate() {
let result = device.render(buf, Rect {
x: area.x,
y: area.y + h,
width: area.width,
height: area.height - h
})?;
if i == self.focus {
if self.focused {
draw_box_styled(buf, result, Some(Style::default().green().not_dim()))
} else {
draw_box_styled_dotted(buf, result, Some(Style::default().green().dim()))
};
};
w = w.max(result.width);
h = h + result.height;
}
Ok(Rect { x: area.x, y: area.y, width: w, height: h })
}
}
impl Device for Columns {
fn handle (&mut self, event: &AppEvent) -> Usually<bool> {
handle_focus(self, event, keymap!(Self {
[Left, NONE, "focus_up", "focus row above",
|s: &mut Self|s.handle_focus(&FocusEvent::Backward)],
[Right, NONE, "focus_down", "focus row below",
|s: &mut Self|s.handle_focus(&FocusEvent::Forward)],
[Enter, NONE, "focus_down", "focus row below",
|s: &mut Self|s.handle_focus(&FocusEvent::Inward)],
[Esc, NONE, "focus_down", "focus row below",
|s: &mut Self|s.handle_focus(&FocusEvent::Outward)]
}))
}
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
let mut w = 0u16;
let mut h = 0u16;
for (i, device) in self.items.iter().enumerate() {
let result = device.render(buf, Rect {
x: area.x + w,
y: area.y,
width: area.width - w,
height: area.height
})?;
if i == self.focus {
if self.focused {
draw_box_styled(buf, result, Some(Style::default().green().not_dim()))
} else {
draw_box_styled_dotted(buf, result, Some(Style::default().green().dim()))
};
};
w = w + result.width;
h = h.max(result.height);
}
Ok(Rect { x: area.x, y: area.y, width: w, height: h })
}
}

0
src/layout/collect.rs Normal file
View file

61
src/layout/container.rs Normal file
View file

@ -0,0 +1,61 @@
use crate::prelude::*;
pub struct Column(pub Vec<Box<dyn Device>>);
impl Column {
pub fn new (items: Vec<Box<dyn Device>>) -> Self {
Self(items)
}
}
pub struct Row(pub Vec<Box<dyn Device>>);
impl Row {
pub fn new (items: Vec<Box<dyn Device>>) -> Self {
Self(items)
}
}
impl Device for Column {
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
Ok(render_column(&self.0, buf, area)?.0)
}
}
impl Device for Row {
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
Ok(render_row(&self.0, buf, area)?.0)
}
}
pub fn render_column (items: &[Box<dyn Device>], buf: &mut Buffer, area: Rect)
-> Usually<(Rect, Vec<Rect>)>
{
let mut w = 0u16;
let mut h = 0u16;
let mut rects = vec![];
for (i, device) in items.iter().enumerate() {
let rect = Rect { x: area.x, y: area.y + h, width: area.width, height: area.height - h };
let result = device.render(buf, rect)?;
rects.push(result);
w = w.max(result.width);
h = h + result.height;
}
Ok((Rect { x: area.x, y: area.y, width: w, height: h }, rects))
}
pub fn render_row (items: &[Box<dyn Device>], buf: &mut Buffer, area: Rect)
-> Usually<(Rect, Vec<Rect>)>
{
let mut w = 0u16;
let mut h = 0u16;
let mut rects = vec![];
for (i, device) in items.iter().enumerate() {
let rect = Rect { x: area.x + w, y: area.y, width: area.width - w, height: area.height };
let result = device.render(buf, rect)?;
rects.push(result);
w = w + result.width;
h = h.max(result.height);
}
Ok((Rect { x: area.x, y: area.y, width: w, height: h }, rects))
}

180
src/layout/focus.rs Normal file
View file

@ -0,0 +1,180 @@
use crate::prelude::*;
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 Device for FocusColumn {
fn handle (&mut self, event: &AppEvent) -> Usually<bool> {
handle_focus(self, event, KEYMAP_FOCUS_COLUMN)
}
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
let (rect, rects) = super::render_column(self.1.0.as_ref(), buf, area, )?;
//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 Device for FocusRow {
fn handle (&mut self, event: &AppEvent) -> Usually<bool> {
handle_focus(self, event, keymap!(Self {
[Left, NONE, "focus_up", "focus row above",
|s: &mut Self|s.handle_focus(&FocusEvent::Backward)],
[Right, NONE, "focus_down", "focus row below",
|s: &mut Self|s.handle_focus(&FocusEvent::Forward)],
[Enter, NONE, "focus_down", "focus row below",
|s: &mut Self|s.handle_focus(&FocusEvent::Inward)],
[Esc, NONE, "focus_down", "focus row below",
|s: &mut Self|s.handle_focus(&FocusEvent::Outward)]
}))
}
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
let (rect, rects) = super::render_row(&self.1.0, buf, area)?;
Ok(rect)
}
}
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
},
})
}
}

6
src/layout/mod.rs Normal file
View file

@ -0,0 +1,6 @@
mod focus;
pub use focus::*;
mod container;
pub use container::*;
mod scroll;
pub use scroll::*;

7
src/layout/scroll.rs Normal file
View file

@ -0,0 +1,7 @@
use crate::prelude::*;
pub struct ScrollY;
pub struct ScrollX;
pub struct ScrollXY;

View file

@ -23,16 +23,12 @@ fn main () -> Result<(), Box<dyn Error>> {
//crate::device::run(Sequencer::new("Rhythm#000")?) //crate::device::run(Sequencer::new("Rhythm#000")?)
let transport = Transport::new("Transport")?; let transport = Transport::new("Transport")?;
let timebase = transport.state.lock().unwrap().timebase(); let timebase = transport.state.lock().unwrap().timebase();
crate::device::run(Rows::new(true, vec![ crate::device::run(FocusColumn(Some(0), Column::new(vec![
Box::new(transport), Box::new(transport) as Box<dyn Device>,
Box::new(Columns::new(false, vec![
Box::new(Chain::new("Chain#0000", vec![ Box::new(Chain::new("Chain#0000", vec![
Box::new(Sequencer::new("Melody#000", &timebase)?), Box::new(Sequencer::new("Melody#000", &timebase)?),
Box::new(Plugin::new("Plugin#000")?), Box::new(Plugin::new("Plugin#000")?),
])?), ])?),
Box::new(Sequencer::new("Melody#001", &timebase)?),
Box::new(Sequencer::new("Rhythm#000", &timebase)?),
])),
//Box::new(Columns::new(false, vec![ //Box::new(Columns::new(false, vec![
//Box::new(Chain::new("Chain#00", vec![ //Box::new(Chain::new("Chain#00", vec![
//Box::new(Sequencer::new("Rhythm#000")?), //Box::new(Sequencer::new("Rhythm#000")?),
@ -45,5 +41,5 @@ fn main () -> Result<(), Box<dyn Error>> {
//])), //])),
//Box::new(Mixer::new("Mixer#000")?), //Box::new(Mixer::new("Mixer#000")?),
//Box::new(Sequencer::new("Rhythm#000")?), //Box::new(Sequencer::new("Rhythm#000")?),
])) ])))
} }

View file

@ -11,8 +11,10 @@ pub use crate::device::{
pub use crate::time::*; pub use crate::time::*;
pub use crate::layout::{ pub use crate::layout::{
Rows, Row,
Columns, Column,
FocusRow,
FocusColumn,
Focus, Focus,
FocusEvent, FocusEvent,
handle_focus handle_focus
@ -106,7 +108,7 @@ pub type KeyBinding<T> = (
} }
#[macro_export] macro_rules! keymap { #[macro_export] macro_rules! keymap {
($T:ty { $([$k:ident $(($char:literal))?, $m:ident, $n: literal, $d: literal, $f: expr]),* }) => { ($T:ty { $([$k:ident $(($char:literal))?, $m:ident, $n: literal, $d: literal, $f: expr]),* $(,)? }) => {
&[ &[
$((KeyCode::$k $(($char))?, KeyModifiers::$m, $n, $d, &$f as KeyHandler<$T>)),* $((KeyCode::$k $(($char))?, KeyModifiers::$m, $n, $d, &$f as KeyHandler<$T>)),*
] as &'static [KeyBinding<$T>] ] as &'static [KeyBinding<$T>]