1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
use crossterm::{
    event::{
        DisableMouseCapture, EnableMouseCapture, Event as CrosstermEvent, KeyEvent, KeyEventKind,
        KeyModifiers,
    },
    execute,
    terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use libmpv::Mpv;
use std::{borrow::Cow, cell::RefCell, rc::Rc, sync::mpsc};
use std::{
    io,
    time::{self, Duration},
};
use tui::{backend::CrosstermBackend, layout::Rect, style::Color, Terminal};

use crate::{
    app::component::Mode,
    command,
    config::Config,
    error::Result,
    events::{self, Channel},
    visualizer::{self, Visualizer},
    widgets::notification::Notification,
};

pub mod app_screen;
pub mod browse_screen;
pub mod component;
pub mod filtered_list;
pub mod modal;
pub mod playlist_screen;

use crate::events::Event;

use self::{
    app_screen::AppScreen,
    component::{Component, MouseHandler, MyBackend},
};

const FRAME_DELAY_MS: u16 = 16;
const HIGH_EVENT_TIMEOUT: u16 = 1000;
const LOW_EVENT_TIMEOUT: u16 = 17;

pub struct App<'a> {
    pub channel: Channel,
    terminal: Terminal<MyBackend>,
    mpv: Mpv,
    next_render: time::Instant,
    next_poll_timeout: u16,
    notification: Notification<'a>,
    visualizer: Option<Visualizer>,
    screen: Rc<RefCell<AppScreen<'a>>>,
    quit: bool,
}

impl<'a> App<'a> {
    pub fn new() -> Result<Self> {
        let stdout = io::stdout();
        let backend = CrosstermBackend::new(stdout);
        let terminal = Terminal::new(backend)?;

        let mpv = Mpv::with_initializer(|mpv| {
            mpv.set_property("video", false)?;
            mpv.set_property("volume", 100)?;
            if let Some(ao) = &Config::global().mpv_ao {
                mpv.set_property("ao", ao.as_str())?;
            }
            Ok(())
        })?;

        let screen = Rc::new(RefCell::new(AppScreen::new()?));

        let channel = Channel::default();

        let next_render = time::Instant::now();
        let next_poll_timeout = LOW_EVENT_TIMEOUT;

        let notification = Notification::default();

        Ok(App {
            channel,
            terminal,
            mpv,
            next_render,
            next_poll_timeout,
            notification,
            visualizer: None,
            screen,
            quit: false,
        })
    }

    pub fn run(&mut self) -> Result<()> {
        self.chain_hook();
        setup_terminal()?;

        self.channel.spawn_terminal_event_getter();
        self.channel.spawn_ticks();

        while !self.quit {
            self.render()
                .map_err(|e| self.notify_err(e.to_string()))
                .ok();
            self.recv_event()
                .map_err(|e| self.notify_err(e.to_string()))
                .ok();
        }

        reset_terminal()?;
        Ok(())
    }

    #[inline]
    fn render(&mut self) -> Result<()> {
        if time::Instant::now() >= self.next_render {
            self.terminal.draw(|frame| {
                let chunk = frame.size();
                self.screen.borrow_mut().render(frame, chunk, ());
                self.notification.render(frame, frame.size(), ());
            })?;

            let mut err = None; // kind of ugly, but simplifies &mut self borrows
            if let Some(ref mut visualizer) = self.visualizer {
                match visualizer.thread_handle() {
                    visualizer::ThreadHandle::Stopped(Ok(())) => {
                        self.visualizer = None;
                    }
                    visualizer::ThreadHandle::Stopped(Err(e)) => {
                        err = Some(format!("The visualizer process exited with error: {}", e));
                        self.visualizer = None;
                    }
                    _ => visualizer.render(self.terminal.current_buffer_mut()),
                }
            }

            if let Some(err) = err {
                self.notify_err(err);
            }

            self.next_render = time::Instant::now()
                .checked_add(Duration::from_millis(FRAME_DELAY_MS as u64))
                .unwrap();
        }
        Ok(())
    }

    #[inline]
    fn recv_event(&mut self) -> Result<()> {
        // NOTE: Big timeout if the last event was long ago, small timeout otherwise.
        // This makes it so after a burst of events, like a Ctrl+V, we get a small timeout
        // immediately after the last event, which triggers a fast render.
        let timeout = Duration::from_millis(self.next_poll_timeout as u64);
        match self.channel.receiver.recv_timeout(timeout) {
            Ok(Event::Terminal(CrosstermEvent::Key(key))) if key.kind == KeyEventKind::Release => {
                // WARN: we ignore every key release event for now because of a crossterm 0.26
                // quirk: https://github.com/crossterm-rs/crossterm/pull/745
                self.next_poll_timeout = FRAME_DELAY_MS;
            }
            Ok(event) => {
                let event = self.transform_event(event);
                self.handle_event(event)?;
                self.next_poll_timeout = FRAME_DELAY_MS;
            }
            Err(mpsc::RecvTimeoutError::Timeout) => {
                self.next_poll_timeout = self.suitable_event_timeout();
            }
            Err(e) => return Err(e.into()),
        }

        Ok(())
    }

    #[inline]
    fn suitable_event_timeout(&self) -> u16 {
        match self.visualizer {
            Some(_) => LOW_EVENT_TIMEOUT,
            None => HIGH_EVENT_TIMEOUT,
        }
    }

    /// Transforms an event, according to the current app state.
    fn transform_event(&self, event: Event) -> Event {
        use Event::*;
        match event {
            Terminal(CrosstermEvent::Key(key_event)) => {
                let has_mods = key_event.modifiers & (KeyModifiers::CONTROL | KeyModifiers::ALT)
                    != KeyModifiers::NONE;
                match self.screen.borrow().mode() {
                    // In insert mode, key events pass through untransformed, unless there's a
                    // control or alt modifier
                    Mode::Insert if !has_mods => event,

                    // Otherwise, events may be transformed into commands
                    _ => self.transform_normal_mode_key(key_event),
                }
            }
            _ => event,
        }
    }

    fn handle_event(&mut self, event: events::Event) -> Result<()> {
        match &event {
            Event::Command(command::Command::ToggleVisualizer) => {
                self.toggle_visualizer()?;
            }
            Event::Terminal(crossterm::event::Event::Mouse(mouse_event)) => {
                let screen = self.screen.clone();
                let chunk = self.frame_size();
                screen
                    .borrow_mut()
                    .handle_mouse(self, chunk, *mouse_event)?;
            }
            _otherwise => {
                let screen = self.screen.clone();
                screen.borrow_mut().handle_event(self, event)?;
            }
        }
        Ok(())
    }

    /// Transforms a key event into the corresponding command, if there is one.
    /// Assumes state is in normal mode
    fn transform_normal_mode_key(&self, key_event: KeyEvent) -> Event {
        use crate::command::Command::Nop;
        use crossterm::event::Event::Key;
        use Event::*;
        match Config::global().keybindings.get_from_event(key_event) {
            Some(cmd) if cmd != Nop => Command(cmd),
            _ => Terminal(Key(key_event)),
        }
    }

    fn toggle_visualizer(&mut self) -> Result<()> {
        if self.visualizer.take().is_none() {
            let opts = crate::visualizer::CavaOptions {
                bars: self.terminal.get_frame().size().width as usize / 2,
            };
            self.visualizer = Some(Visualizer::new(opts)?);
        }
        Ok(())
    }

    fn chain_hook(&mut self) {
        let original_hook = std::panic::take_hook();

        std::panic::set_hook(Box::new(move |panic| {
            reset_terminal().unwrap();
            original_hook(panic);
            std::process::exit(1);
        }));
    }

    pub fn select_screen(&mut self, screen: app_screen::Selected) {
        self.screen.borrow_mut().select(screen);
    }

    pub fn quit(&mut self) {
        self.quit = true;
    }

    ////////////////////////////////
    //        Notification        //
    ////////////////////////////////
    pub fn notify_err(&mut self, err: impl Into<Cow<'a, str>>) {
        self.notification = Notification::new(err, Duration::from_secs(5)).colored(Color::LightRed);
    }

    pub fn notify_info(&mut self, info: impl Into<Cow<'a, str>>) {
        self.notification =
            Notification::new(info, Duration::from_secs(4)).colored(Color::LightCyan);
    }

    pub fn notify_ok(&mut self, text: impl Into<Cow<'a, str>>) {
        self.notification =
            Notification::new(text, Duration::from_secs(4)).colored(Color::LightGreen);
    }

    /////////////////////////
    //        Frame        //
    /////////////////////////
    pub fn frame_size(&mut self) -> Rect {
        self.terminal.get_frame().size()
    }
}

pub fn setup_terminal() -> Result<()> {
    execute!(io::stdout(), EnterAlternateScreen, EnableMouseCapture)?;
    enable_raw_mode()?;
    Ok(())
}

pub fn reset_terminal() -> Result<()> {
    disable_raw_mode()?;
    execute!(io::stdout(), LeaveAlternateScreen, DisableMouseCapture)?;
    Ok(())
}