implement has_size

This commit is contained in:
🪞👃🪞 2024-12-14 22:58:03 +01:00
parent aa8a1a3bd9
commit c27a4a5232
3 changed files with 17 additions and 21 deletions

View file

@ -4,20 +4,14 @@ pub trait HasSize<E: Engine> {
fn size (&self) -> &Measure<E>; fn size (&self) -> &Measure<E>;
} }
#[macro_export] macro_rules! implementor { #[macro_export] macro_rules! has_size {
($name:ident => $Trait:ident) => { (<$E:ty>|$self:ident:$Struct:ident$(<$($L:lifetime),*$($T:ident$(:$U:path)?),*>)?|$cb:expr) => {
#[macro_export] macro_rules! $name { impl $(<$($L),*$($T $(: $U)?),*>)? HasSize<$E> for $Struct $(<$($L),*$($T),*>)? {
(|$self:ident:$Struct:ident$(<$($L:lifetime),*$($T:ident$(:$U:path)?),*>)?|$cb:expr) => { fn size (&$self) -> &Measure<$E> { $cb } // TODO provide trait body
impl $(<$($L),*$($T $(: $U)?),*>)? $Trait for $Struct $(<$($L),*$($T),*>)? {
fn clock (&$self) -> &ClockModel { $cb }
}
}
} }
} }
} }
implementor!(has_size => HasSize);
impl<E: Engine> LayoutDebug<E> for E {} impl<E: Engine> LayoutDebug<E> for E {}
pub trait LayoutDebug<E: Engine> { pub trait LayoutDebug<E: Engine> {

View file

@ -100,7 +100,7 @@ impl Command<PhraseEditorModel> for PhraseCommand {
/// Contains state for viewing and editing a phrase /// Contains state for viewing and editing a phrase
pub struct PhraseEditorModel { pub struct PhraseEditorModel {
/// Renders the phrase /// Renders the phrase
pub mode: Box<dyn PhraseViewMode>, pub mode: Box<dyn PhraseViewMode>,
} }
impl Default for PhraseEditorModel { impl Default for PhraseEditorModel {
@ -111,12 +111,12 @@ impl Default for PhraseEditorModel {
render!(|self: PhraseEditorModel|self.mode); render!(|self: PhraseEditorModel|self.mode);
pub trait PhraseViewMode: Render<Tui> + MIDIRange + MIDIPoint + Debug + Send + Sync { pub trait PhraseViewMode: Render<Tui> + HasSize<Tui> + MIDIRange + MIDIPoint + Debug + Send + Sync {
fn buffer_size (&self, phrase: &Phrase) -> (usize, usize); fn buffer_size (&self, phrase: &Phrase) -> (usize, usize);
fn redraw (&mut self); fn redraw (&mut self);
fn phrase (&self) -> &Option<Arc<RwLock<Phrase>>>; fn phrase (&self) -> &Option<Arc<RwLock<Phrase>>>;
fn phrase_mut (&mut self) -> &mut Option<Arc<RwLock<Phrase>>>; fn phrase_mut (&mut self) -> &mut Option<Arc<RwLock<Phrase>>>;
fn set_phrase (&mut self, phrase: Option<&Arc<RwLock<Phrase>>>) { fn set_phrase (&mut self, phrase: Option<&Arc<RwLock<Phrase>>>) {
*self.phrase_mut() = phrase.map(|p|p.clone()); *self.phrase_mut() = phrase.map(|p|p.clone());
self.redraw(); self.redraw();
} }
@ -124,7 +124,7 @@ pub trait PhraseViewMode: Render<Tui> + MIDIRange + MIDIPoint + Debug + Send + S
impl MIDIViewport<Tui> for PhraseEditorModel {} impl MIDIViewport<Tui> for PhraseEditorModel {}
has_size!(|self:PhraseEditorModel|&self.size); has_size!(<Tui>|self:PhraseEditorModel|self.mode.size());
impl MIDIRange for PhraseEditorModel { impl MIDIRange for PhraseEditorModel {
fn time_zoom (&self) -> usize { self.mode.time_zoom() } fn time_zoom (&self) -> usize { self.mode.time_zoom() }
@ -150,16 +150,16 @@ impl PhraseViewMode for PhraseEditorModel {
fn buffer_size (&self, phrase: &Phrase) -> (usize, usize) { fn buffer_size (&self, phrase: &Phrase) -> (usize, usize) {
self.mode.buffer_size(phrase) self.mode.buffer_size(phrase)
} }
fn redraw (&mut self) { fn redraw (&mut self) {
self.mode.redraw() self.mode.redraw()
} }
fn phrase (&self) -> &Option<Arc<RwLock<Phrase>>> { fn phrase (&self) -> &Option<Arc<RwLock<Phrase>>> {
self.mode.phrase() self.mode.phrase()
} }
fn phrase_mut (&mut self) -> &mut Option<Arc<RwLock<Phrase>>> { fn phrase_mut (&mut self) -> &mut Option<Arc<RwLock<Phrase>>> {
self.mode.phrase_mut() self.mode.phrase_mut()
} }
fn set_phrase (&mut self, phrase: Option<&Arc<RwLock<Phrase>>>) { fn set_phrase (&mut self, phrase: Option<&Arc<RwLock<Phrase>>>) {
self.mode.set_phrase(phrase) self.mode.set_phrase(phrase)
} }
} }

View file

@ -241,6 +241,8 @@ impl PianoHorizontal {
} }
} }
has_size!(<Tui>|self:PianoHorizontal|&self.size);
impl MIDIRange for PianoHorizontal { impl MIDIRange for PianoHorizontal {
fn time_zoom (&self) -> usize { self.range.time_zoom() } fn time_zoom (&self) -> usize { self.range.time_zoom() }
fn set_time_zoom (&self, x: usize) { self.range.set_time_zoom(x); } fn set_time_zoom (&self, x: usize) { self.range.set_time_zoom(x); }