cleanup: sub 4k again

This commit is contained in:
🪞👃🪞 2024-07-09 12:47:53 +03:00
parent 214061a419
commit d8c9abf744
13 changed files with 101 additions and 505 deletions

View file

@ -1,10 +1,7 @@
pub mod focus;
pub mod mixer;
pub mod plugin;
pub mod sampler;
pub use self::focus::*;
use crate::{core::*, handle, App, AppSection};
handle!(App |self, e| {

View file

@ -1,189 +0,0 @@
use crate::{core::*, view::*};
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
},
})
}
}

View file

@ -19,14 +19,15 @@ pub use self::sequencer::SequencerView;
use crate::{render, App, AppSection, core::*};
render!(App |self, buf, area| {
let track = self.track_cursor;
Split::down([
&TransportView::new(self),
&Split::down([
&ArrangerView::new(&self, !self.arranger_mode),
&Split::down([
&If(track > 0, &Split::right([
&ChainView::vertical(&self),
&SequencerView::new(&self),
&ChainView::new(&self, false)
])
]))
])
]).render(buf, area)?;
if let Some(ref modal) = self.modal {

View file

@ -128,3 +128,36 @@ pub fn lozenge_right (buf: &mut Buffer, x: u16, y1: u16, h: u16, style: Option<S
}
}
pub fn draw_box (buffer: &mut Buffer, area: Rect) -> Rect {
draw_box_styled(buffer, area, Some(Style::default().gray().dim()))
}
pub fn draw_box_styled (buffer: &mut Buffer, area: Rect, style: Option<Style>) -> Rect {
if area.width < 1 || area.height < 1 {
return area
}
format!("{}", "".repeat((area.width - 2).into()))
.blit(buffer, area.x, area.y, style);
for y in (area.y + 1)..(area.y + area.height - 1) {
"".blit(buffer, area.x, y, style);
"".blit(buffer, area.x + area.width - 1, y, style);
}
format!("{}", "".repeat((area.width - 2).into()))
.blit(buffer, area.x, area.y + area.height - 1, style);
area
}
pub fn draw_box_styled_dotted (buffer: &mut Buffer, area: Rect, style: Option<Style>) -> Rect {
if area.width < 1 || area.height < 1 {
return area
}
format!("{}", "".repeat((area.width - 2).into()))
.blit(buffer, area.x, area.y, style);
for y in (area.y + 1)..(area.y + area.height - 1) {
"".blit(buffer, area.x, y, style);
"".blit(buffer, area.x + area.width - 1, y, style);
}
format!("{}", "".repeat((area.width - 2).into()))
.blit(buffer, area.x, area.y + area.height - 1, style);
area
}

View file

@ -9,11 +9,20 @@ pub struct ChainView<'a> {
}
impl<'a> ChainView<'a> {
pub fn horizontal (app: &'a App) -> Self {
Self::new(app, false)
}
pub fn vertical (app: &'a App) -> Self {
Self::new(app, true)
}
pub fn new (app: &'a App, vertical: bool) -> Self {
Self {
vertical,
focused: app.section == AppSection::Chain,
track: app.tracks.get(app.track_cursor - 1),
vertical
track: match app.track_cursor {
0 => None,
_ => app.tracks.get(app.track_cursor - 1)
},
}
}
}

View file

@ -1,8 +1,4 @@
use crate::core::*;
mod container; pub use self::container::*;
mod scroll; pub use self::scroll::*;
pub mod table;
pub trait Modal<T>: Device {
fn handle_with_state (&self, state: &mut T, event: &AppEvent) -> Usually<bool>;
@ -16,40 +12,48 @@ pub trait MaxHeight: Device {
impl<T: Device> MaxHeight for T {}
pub fn draw_box (buffer: &mut Buffer, area: Rect) -> Rect {
draw_box_styled(buffer, area, Some(Style::default().gray().dim()))
pub enum Collected<'a> {
Box(Box<dyn Render + Sync + 'a>),
Ref(&'a (dyn Render + Sync + 'a)),
None
}
pub fn draw_box_styled (buffer: &mut Buffer, area: Rect, style: Option<Style>) -> Rect {
if area.width < 1 || area.height < 1 {
return area
impl<'a> Render for Collected<'a> {
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
match self {
Self::Box(item) => (*item).render(buf, area),
Self::Ref(item) => (*item).render(buf, area),
Self::None => Ok(area),
}
}
format!("{}", "".repeat((area.width - 2).into()))
.blit(buffer, area.x, area.y, style);
for y in (area.y + 1)..(area.y + area.height - 1) {
"".blit(buffer, area.x, y, style);
"".blit(buffer, area.x + area.width - 1, y, style);
}
format!("{}", "".repeat((area.width - 2).into()))
.blit(buffer, area.x, area.y + area.height - 1, style);
area
}
pub fn draw_box_styled_dotted (buffer: &mut Buffer, area: Rect, style: Option<Style>) -> Rect {
if area.width < 1 || area.height < 1 {
return area
pub struct Collector<'a>(pub Vec<Collected<'a>>);
//impl<'a, R: Render + 'a> FnOnce<(R,)> for Collector<'a> {
//type Output = ();
//extern "rust-call" fn call_once (self, (device, ): (R,)) -> Self::Output {
//self.add(widget.into_collected());
//}
//}
impl<'a> Collector<'a> {
pub fn collect (collect: impl Fn(&mut Collector<'a>)) -> Self {
let mut items = Self(vec![]);
collect(&mut items);
items
}
format!("{}", "".repeat((area.width - 2).into()))
.blit(buffer, area.x, area.y, style);
for y in (area.y + 1)..(area.y + area.height - 1) {
"".blit(buffer, area.x, y, style);
"".blit(buffer, area.x + area.width - 1, y, style);
fn add (mut self, widget: Collected<'a>) -> Self {
self.0.push(widget);
self
}
format!("{}", "".repeat((area.width - 2).into()))
.blit(buffer, area.x, area.y + area.height - 1, style);
area
}
pub trait Collection<'a, T, U> {
fn add (self, widget: impl Render + 'a) -> Self;
}
//struct AppLayout {
//focus: usize,
//track_focus: usize,

View file

@ -1,42 +0,0 @@
pub enum Collected<'a> {
Box(Box<dyn Render + 'a>),
Ref(&'a (dyn Render + 'a)),
None
}
impl<'a> Render for Collected<'a> {
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
match self {
Self::Box(item) => (*item).render(buf, area),
Self::Ref(item) => (*item).render(buf, area),
Self::None => Ok(area),
}
}
}
pub struct Collector<'a>(pub Vec<Collected<'a>>);
impl<'a, R: Render + 'a> FnOnce<(R)> for Collector<'a> {
type Output = ();
extern "rust-call" fn call_once (self, (device, ): (R,)) -> Self::Output {
self.add(widget.into_collected());
}
}
impl<'a> Collector<'a> {
pub fn collect (collect: impl Fn(&mut Collector<'a>)) -> Self {
let mut items = Self(vec![]);
collect(&mut items);
items
}
fn add (mut self, widget: Collected<'a>) -> Self {
self.0.push(widget);
self
}
}
pub trait Collection<'a, T, U> {
fn add (self, widget: impl Render + 'a) -> Self;
}

View file

@ -1,73 +0,0 @@
use crate::core::*;
pub struct Stack<'a>(pub &'a[Box<dyn Render + Sync>]);
impl<'a> Render for Stack<'a> {
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
let mut area2 = area.clone();
for layer in self.0.iter() {
area2 = layer.render(buf, area2)?;
}
Ok(area)
}
}
pub struct Column(pub Vec<Box<dyn Device>>);
pub struct Row(pub Vec<Box<dyn Device>>);
impl Column {
pub fn new (items: Vec<Box<dyn Device>>) -> Self {
Self(items)
}
pub fn draw (buf: &mut Buffer, area: Rect, items: &[impl Render], gap: i16)
-> Usually<(Rect, Vec<Rect>)>
{
let mut w = 0u16;
let mut h = 0u16;
let mut rects = vec![];
for (_i, device) in items.iter().enumerate() {
let y = area.y + h;
let rect = Rect { x: area.x, y, 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) as i16 + gap).max(0) as u16;
}
Ok((Rect { x: area.x, y: area.y, width: w, height: h }, rects))
}
}
impl Render for Column {
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
Ok(Column::draw(buf, area, &self.0, 0)?.0)
}
}
impl Row {
pub fn new (items: Vec<Box<dyn Device>>) -> Self {
Self(items)
}
pub fn draw (buf: &mut Buffer, area: Rect, items: &[impl Render], gap: i16)
-> Usually<(Rect, Vec<Rect>)>
{
let mut w = 0u16;
let mut h = 0u16;
let mut rects = vec![];
for (_i, device) in items.iter().enumerate() {
let x = area.x + w;
let rect = Rect { x, y: area.y, width: area.width - w, height: area.height };
let result = device.render(buf, rect)?;
rects.push(result);
w = ((w + result.width) as i16 + gap).max(0) as u16;
h = h.max(result.height);
}
Ok((Rect { x: area.x, y: area.y, width: w, height: h }, rects))
}
}
impl Render for Row {
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
Ok(Row::draw(buf, area, &self.0, 0)?.0)
}
}

View file

@ -1,98 +0,0 @@
use crate::core::*;
pub trait BorderStyle {
const NW: &'static str = "";
const N: &'static str = "";
const NE: &'static str = "";
const E: &'static str = "";
const SE: &'static str = "";
const S: &'static str = "";
const SW: &'static str = "";
const W: &'static str = "";
fn draw (&self, buf: &mut Buffer, area: Rect) {
let style = self.style();
for x in area.x+1..(area.x+area.width).saturating_sub(2) {
Self::N.blit(buf, x, area.y, style);
Self::S.blit(buf, x, area.y + area.height - 1, style);
}
for y in area.y+1..(area.y+area.height).saturating_sub(2) {
Self::W.blit(buf, area.x, y, style);
Self::E.blit(buf, area.x + area.width - 1, y, style);
}
Self::NW.blit(buf, area.x, area.y, style);
Self::NE.blit(buf, area.x + area.width - 1, area.y, style);
Self::SW.blit(buf, area.x, area.y + area.height - 1, style);
Self::SE.blit(buf, area.x + area.width - 1, area.y + area.height - 1, style);
}
fn style (&self) -> Option<Style> {
None
}
}
pub struct Lozenge(pub Style);
impl BorderStyle for Lozenge {
const N: &'static str = "";
const S: &'static str = "";
const NW: &'static str = "";
const W: &'static str = "";
const SW: &'static str = "";
const NE: &'static str = "";
const E: &'static str = "";
const SE: &'static str = "";
fn style (&self) -> Option<Style> {
Some(self.0)
}
}
pub struct RailV(pub Style);
impl BorderStyle for RailV {
const NW: &'static str = "";
const W: &'static str = "";
const SW: &'static str = "";
const NE: &'static str = "";
const E: &'static str = "";
const SE: &'static str = "";
fn style (&self) -> Option<Style> {
Some(self.0)
}
}
const LOZENGE: [[&'static str;3];3] = [
["", "", ""],
["", " ", ""],
["", "", ""],
];
pub fn lozenge_left (buf: &mut Buffer, x: u16, y1: u16, h: u16, style: Option<Style>) {
let y2 = y1 + h;
let y3 = y2.saturating_sub(1);
for y in y1..y2 {
if y == y1 {
LOZENGE[0][0]
} else if y == y3 {
LOZENGE[2][0]
} else {
LOZENGE[1][0]
}.blit(buf, x, y, style)
}
}
pub fn lozenge_right (buf: &mut Buffer, x: u16, y1: u16, h: u16, style: Option<Style>) {
let y2 = y1 + h;
let y3 = y2.saturating_sub(1);
for y in y1..y2 {
if y == y1 {
LOZENGE[0][2]
} else if y == y3 {
LOZENGE[2][2]
} else {
LOZENGE[1][2]
}.blit(buf, x, y, style)
}
}

View file

@ -1,5 +0,0 @@
pub struct ScrollY;
pub struct ScrollX;
pub struct ScrollXY;

View file

@ -30,7 +30,10 @@ pub struct SequencerView<'a> {
impl<'a> SequencerView<'a> {
pub fn new (app: &'a App) -> Self {
let track = app.tracks.get(app.track_cursor - 1);
let track = match app.track_cursor {
0 => None,
_ => app.tracks.get(app.track_cursor - 1)
};
let phrase = app.phrase();
Self {
phrase,

View file

@ -1,5 +1,17 @@
use crate::core::*;
pub struct If<'a>(pub bool, pub &'a (dyn Render + Sync));
impl<'a> Render for If<'a> {
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
if self.0 {
self.1.render(buf, area)
} else {
().render(buf, area)
}
}
}
pub enum Direction {
Down,
Right,
@ -11,6 +23,9 @@ impl<'a, const N: usize> Split<'a, N> {
pub fn down (items: [&'a (dyn Render + Sync);N]) -> Self {
Self(Direction::Down, items)
}
pub fn right (items: [&'a (dyn Render + Sync);N]) -> Self {
Self(Direction::Right, items)
}
}
impl<'a, const N: usize> Render for Split<'a, N> {
@ -35,63 +50,3 @@ impl<'a, const N: usize> Render for Split<'a, N> {
Ok(area)
}
}
//pub struct Split<'a> {
//a: &'a (dyn Render + Sync),
//b: &'a (dyn Render + Sync),
//direction: Direction,
//reverse: bool
//}
//impl<'a> Split<'a> {
//pub fn lr (a: &'a (dyn Render + Sync), b: &'a (dyn Render + Sync)) -> Self {
//Self { a, b, direction: Direction::X, reverse: false }
//}
//pub fn rl (a: &'a (dyn Render + Sync), b: &'a (dyn Render + Sync)) -> Self {
//Self { a, b, direction: Direction::X, reverse: true }
//}
//pub fn tb (a: &'a (dyn Render + Sync), b: &'a (dyn Render + Sync)) -> Self {
//Self { a, b, direction: Direction::Y, reverse: false }
//}
//pub fn bt (a: &'a (dyn Render + Sync), b: &'a (dyn Render + Sync)) -> Self {
//Self { a, b, direction: Direction::Y, reverse: true }
//}
//fn split (&self, length: u16) -> (u16, u16) {
//let l1 = (length as f64 * self.proportion).round() as u16;
//let l2 = length.saturating_sub(l1);
//(l1, l2)
//}
//}
//impl<'a> Render for Split<'a> {
//fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
//let Rect { x, y, width, height } = area;
//let area1 = if self.reverse { self.b } else { self.a }
//.render(buf, area)?;
//let area2 = if self.reverse { self.a } else { self.b }
//.render(buf, match (self.direction, self.reverse)Rect {
//})?;
//let (area1, area2) = match self.direction {
//Direction::X => {
//let (w1, w2) = self.split(width);
//(Rect { x, y, width: w1, height }, Rect { x: x + w1, y, width: w2, height })
//},
//Direction::Y => {
//let (h1, h2) = self.split(height);
//(Rect { x, y, width, height: h1 }, Rect { x, y: y + h1, width, height: h2 })
//},
//};
//match self.reverse {
//true => {
//self.b.render(buf, area1)?;
//self.a.render(buf, area2)?;
//},
//false => {
//self.a.render(buf, area1)?;
//self.b.render(buf, area2)?;
//},
//};
//Ok(area)
//}
//}

View file

@ -31,3 +31,4 @@ impl<T> Table<T> {
self.columns[col][row] = cell;
}
}