| @@ -9,8 +9,23 @@ use ratatui::{ |
| 9 | 9 | use tui_additions::framework::*; |
| 10 | 10 | |
| 11 | 11 | /// a message bar item, contains no fields because the message is taken from `data.global.Message` |
| 12 | | #[derive(Clone, Copy, Default)] |
| 13 | | pub struct MessageBar; |
| 12 | #[derive(Clone, Copy)] |
| 13 | pub enum MessageBar { |
| 14 | Stateless, |
| 15 | /// start and end position of the seeker for mouse scrubbing |
| 16 | Music { |
| 17 | seeker_front: u16, |
| 18 | seeker_rear: u16, |
| 19 | }, |
| 20 | /// bar too short for seeker to display |
| 21 | MusicSeekerLess, |
| 22 | } |
| 23 | |
| 24 | impl Default for MessageBar { |
| 25 | fn default() -> Self { |
| 26 | Self::Stateless |
| 27 | } |
| 28 | } |
| 14 | 29 | |
| 15 | 30 | impl FrameworkItem for MessageBar { |
| 16 | 31 | fn render( |
| @@ -30,6 +45,7 @@ impl FrameworkItem for MessageBar { |
| 30 | 45 | let mpv = framework.data.global.get::<MpvWrapper>().unwrap(); |
| 31 | 46 | if Self::is_mpv_render(framework) { |
| 32 | 47 | let mut label = mpv.property("media-title".to_string()).unwrap(); |
| 48 | |
| 33 | 49 | if let Some((name, ext)) = label.rsplit_once('.') { |
| 34 | 50 | if ext.len() < 5 |
| 35 | 51 | && !name.len() > 13 |
| @@ -42,6 +58,7 @@ impl FrameworkItem for MessageBar { |
| 42 | 58 | label = name[0..name.len() - 13].to_string(); |
| 43 | 59 | } |
| 44 | 60 | } |
| 61 | |
| 45 | 62 | let duration = mpv |
| 46 | 63 | .property("duration".to_string()) |
| 47 | 64 | .unwrap_or_default() |
| @@ -76,6 +93,15 @@ impl FrameworkItem for MessageBar { |
| 76 | 93 | let seeker_pad = if seeker_len > 10 { seeker_len / 10 } else { 0 }; |
| 77 | 94 | seeker_len -= seeker_pad * 2; |
| 78 | 95 | let seeker_pos = (seeker_len - 1) * playerhead as usize / duration as usize; |
| 96 | |
| 97 | let seeker_front = left_chunk.len() + 1 + seeker_pad; // index of first |
| 98 | // character in seeker |
| 99 | let seeker_rear = seeker_front + seeker_len - 1; // index of last character |
| 100 | // in seeker |
| 101 | *self = MessageBar::Music { |
| 102 | seeker_front: seeker_front as u16, |
| 103 | seeker_rear: seeker_rear as u16, |
| 104 | }; |
| 79 | 105 | format!( |
| 80 | 106 | "{left_chunk} {}├{}-{}┤{} {right_chunk}", |
| 81 | 107 | " ".repeat(seeker_pad), |
| @@ -84,13 +110,16 @@ impl FrameworkItem for MessageBar { |
| 84 | 110 | " ".repeat(seeker_pad) |
| 85 | 111 | ) |
| 86 | 112 | } else if length > total_len + 3 { |
| 113 | *self = MessageBar::MusicSeekerLess; |
| 87 | 114 | format!( |
| 88 | 115 | "{left_chunk}{}{right_chunk}", |
| 89 | 116 | " ".repeat(length - total_len) |
| 90 | 117 | ) |
| 91 | 118 | } else if length > 19 { |
| 119 | *self = MessageBar::MusicSeekerLess; |
| 92 | 120 | left_chunk |
| 93 | 121 | } else { |
| 122 | *self = MessageBar::MusicSeekerLess; |
| 94 | 123 | String::from("Not enough width") |
| 95 | 124 | }); |
| 96 | 125 | } |
| @@ -156,6 +185,46 @@ impl FrameworkItem for MessageBar { |
| 156 | 185 | fn selectable(&self) -> bool { |
| 157 | 186 | false |
| 158 | 187 | } |
| 188 | |
| 189 | #[cfg(feature = "mpv")] |
| 190 | fn mouse_passthrough( |
| 191 | &mut self, |
| 192 | framework: &mut FrameworkClean, |
| 193 | _selected: bool, |
| 194 | x: u16, |
| 195 | _y: u16, |
| 196 | _absolute_x: u16, |
| 197 | _absolute_y: u16, |
| 198 | ) -> bool { |
| 199 | match self { |
| 200 | Self::Music { |
| 201 | seeker_front, |
| 202 | seeker_rear, |
| 203 | } => { |
| 204 | use std::sync::mpsc; |
| 205 | |
| 206 | let seek_percentage = |
| 207 | (x as i32 - *seeker_front as i32) * 100 / (*seeker_rear - *seeker_front) as i32; |
| 208 | let mpv = framework.data.global.get::<MpvWrapper>().unwrap(); |
| 209 | let (tx, rx) = mpsc::channel(); |
| 210 | mpv.sender |
| 211 | .send(MpvAction::Command { |
| 212 | name: "seek".to_string(), |
| 213 | args: vec![ |
| 214 | seek_percentage.clamp(0, 100).to_string(), |
| 215 | "absolute-percent".to_string(), |
| 216 | ], |
| 217 | responder: tx, |
| 218 | }) |
| 219 | .unwrap(); |
| 220 | if let MpvResponse::Error(e) = rx.recv().unwrap() { |
| 221 | *framework.data.global.get_mut::<Message>().unwrap() = Message::Error(e); |
| 222 | } |
| 223 | true |
| 224 | } |
| 225 | _ => false, |
| 226 | } |
| 227 | } |
| 159 | 228 | } |
| 160 | 229 | |
| 161 | 230 | #[cfg(feature = "mpv")] |