mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 19:56:42 +01:00
167 lines
5.8 KiB
Rust
167 lines
5.8 KiB
Rust
use crate::prelude::*;
|
||
use super::*;
|
||
pub struct LauncherGridView<'a> {
|
||
state: &'a Launcher,
|
||
buf: &'a mut Buffer,
|
||
area: Rect,
|
||
focused: bool,
|
||
separator: String
|
||
}
|
||
impl<'a> LauncherGridView<'a> {
|
||
pub fn new (state: &'a Launcher, buf: &'a mut Buffer, area: Rect, focused: bool) -> Self {
|
||
let separator = format!("├{}┤", "-".repeat((area.width - 2).into()));
|
||
Self { state, buf, area, separator, focused }
|
||
}
|
||
pub fn draw (&mut self) -> Usually<Rect> {
|
||
//self.separator_h(0, false);
|
||
//self.separator_h(2, false);
|
||
//self.separator_h((self.state.cursor.1 * 2) as u16, true);
|
||
//self.separator_h(((self.state.cursor.1 + 1) * 2) as u16, true);
|
||
self.area.height = self.state.scenes.len() as u16 + 2;
|
||
let style = Some(Style::default().green().dim());
|
||
if self.focused {
|
||
let Rect { x, y, width, height } = self.area;
|
||
lozenge_left(self.buf, x, y, height, style);
|
||
lozenge_right(self.buf, x + width - 1, y, height, style);
|
||
}
|
||
|
||
let columns = self.column_names();
|
||
|
||
let mut x = self.area.x;
|
||
for (i, w) in self.column_widths().iter().enumerate() {
|
||
if x >= self.area.x + self.area.width {
|
||
break
|
||
}
|
||
self.separator_v(x, i == self.state.cursor.0);
|
||
x = x + w;
|
||
self.separator_v(x, i == self.state.cursor.0);
|
||
}
|
||
|
||
let (mut x, y) = (self.area.x, self.area.y);
|
||
for (i, title) in columns.iter().enumerate() {
|
||
if x >= self.area.x + self.area.width {
|
||
break
|
||
}
|
||
title.blit(
|
||
self.buf, x+1, y, Some(self.highlight(i == self.state.cursor.0).bold())
|
||
);
|
||
if i == 0 {
|
||
self.scenes(x+1, y + 1);
|
||
} else if i < columns.len() {
|
||
self.clips(x+1, y + 1, i - 1);
|
||
}
|
||
let w = (title.len() as u16).max(10) + 3;
|
||
x = x + w;
|
||
}
|
||
|
||
"+Add track…".blit(self.buf, x + 2, y, Some(Style::default().dim()));
|
||
Ok(self.area)
|
||
}
|
||
|
||
fn column_names (&self) -> Vec<&'a str> {
|
||
let mut columns = vec![self.state.name.as_str()];
|
||
for track in self.state.tracks.iter() {
|
||
columns.push(track.name.as_str());
|
||
}
|
||
columns
|
||
}
|
||
|
||
fn column_widths (&self) -> Vec<u16> {
|
||
self.column_names().iter().map(|name|(name.len() as u16).max(10) + 3).collect()
|
||
}
|
||
|
||
fn scenes (&mut self, x: u16, y: u16) -> u16 {
|
||
let mut index = 0usize;
|
||
loop {
|
||
if index >= self.state.scenes.len() {
|
||
break
|
||
}
|
||
if y + index as u16 >= self.area.height {
|
||
break
|
||
}
|
||
if let Some(scene) = self.state.scenes.get(index) {
|
||
let style = Some(self.highlight(
|
||
(0 == self.state.cursor.0) && (index + 1 == self.state.cursor.1)
|
||
).bold());
|
||
"⯈".blit(self.buf, x, y + index as u16, style);
|
||
scene.name.blit(self.buf, x+1, y + index as u16, style);
|
||
}
|
||
index = index + 1;
|
||
}
|
||
let hi = (0 == self.state.cursor.0) &&
|
||
(self.state.scenes.len() + 1 == self.state.cursor.1);
|
||
"+Add scene…".blit(self.buf, x, y + index as u16, Some(if hi {
|
||
self.highlight(true)
|
||
} else {
|
||
Style::default().dim()
|
||
}));
|
||
index as u16
|
||
}
|
||
|
||
fn clips (&mut self, x: u16, y: u16, track: usize) -> u16 {
|
||
let mut index = 0;
|
||
loop {
|
||
if index >= self.state.scenes.len() {
|
||
break
|
||
}
|
||
if y + index as u16 >= self.area.height {
|
||
break
|
||
}
|
||
if let Some(scene) = self.state.scenes.get(index) {
|
||
let hi = (track + 1 == self.state.cursor.0) &&
|
||
(index + 1 == self.state.cursor.1);
|
||
let style = Some(self.highlight(hi));
|
||
let clip = scene.clips.get(track);
|
||
let index = index as u16;
|
||
let label = if let Some(Some(clip)) = clip {
|
||
let track = self.state.tracks[track].sequencer.state();
|
||
let phrase = track.phrases.get(*clip);
|
||
if let Some(phrase) = phrase {
|
||
format!("⯈{}", phrase.name)
|
||
} else {
|
||
format!("????")
|
||
}
|
||
} else {
|
||
format!(" ·········")
|
||
};
|
||
label.blit(self.buf, x, y + index, style);
|
||
}
|
||
index = index + 1;
|
||
}
|
||
let hi = (track + 1 == self.state.cursor.0) &&
|
||
(self.state.scenes.len() + 1 == self.state.cursor.1);
|
||
" + Add clip".blit(self.buf, x, y + index as u16, Some(if hi {
|
||
self.highlight(true)
|
||
} else {
|
||
Style::default().dim()
|
||
}));
|
||
index as u16
|
||
}
|
||
|
||
fn highlight (&self, highlight: bool) -> Style {
|
||
if highlight {
|
||
if self.focused {
|
||
Style::default().green().not_dim()
|
||
} else {
|
||
Style::default().green().dim()
|
||
}
|
||
} else {
|
||
Style::default()
|
||
}
|
||
}
|
||
|
||
fn separator_h (&mut self, y: u16, highlight: bool) {
|
||
let style = Some(self.highlight(highlight));
|
||
self.separator.blit(self.buf, self.area.x, self.area.y + y, style);
|
||
}
|
||
|
||
fn separator_v (&mut self, x: u16, highlight: bool) {
|
||
let style = Some(self.highlight(highlight));
|
||
//"┬".blit(self.buf, x, self.area.y + 0, style);
|
||
for y in self.area.y+1..self.area.y+self.area.height-1 {
|
||
"┊".blit(self.buf, x, y, style);
|
||
}
|
||
//"┴".blit(self.buf, x, self.area.y+self.area.height-1, style);
|
||
}
|
||
}
|
||
|