src/run.rs

119 lines · 4013 bytes

  1use crossterm::event::{self, Event, MouseButton, MouseEventKind};
  2use ratatui::{backend::CrosstermBackend, Terminal};
  3use std::{any::TypeId, error::Error, io::Stdout};
  4use tui_additions::framework::Framework;
  5
  6use crate::{
  7    config::*,
  8    global::{functions::*, structs::*},
  9    items::SearchFilter,
 10};
 11
 12/// the main event loop of the program
 13pub fn run(
 14    terminal: &mut Terminal<CrosstermBackend<Stdout>>,
 15    framework: &mut Framework,
 16) -> Result<(), Box<dyn Error>> {
 17    loop {
 18        // repeat forever until all tasks are ran (and Tasks is cleared)
 19        if let Some(tasks) = framework.data.state.get_mut::<Tasks>().unwrap().pop() {
 20            tasks.run(framework, terminal)?;
 21            continue;
 22        }
 23
 24        framework
 25            .data
 26            .global
 27            .get_mut::<Status>()
 28            .unwrap()
 29            .storage
 30            .clear();
 31
 32        // exits the function is `.exit` is true - a way for items/commands to exit the program
 33        if framework.data.global.get::<Status>().unwrap().exit {
 34            break;
 35        }
 36        *framework.data.global.get_mut::<Message>().unwrap() = Message::None;
 37
 38        match event::read()? {
 39            Event::Mouse(mouse)
 40                if framework
 41                    .data
 42                    .global
 43                    .get::<MainConfig>()
 44                    .unwrap()
 45                    .mouse_support =>
 46            {
 47                if mouse.kind != MouseEventKind::Down(MouseButton::Left) {
 48                    continue;
 49                }
 50
 51                *framework.data.global.get_mut::<Message>().unwrap() = Message::None;
 52
 53                let status = framework.data.global.get_mut::<Status>().unwrap();
 54                status.command_capture = None;
 55
 56                // check if the search filter popup is clicked
 57                let mut searchfilter_clicked = false;
 58                if status.search_filter_opened {
 59                    let (mut frameworkclean, state) = framework.split_clean();
 60                    for row in state.0.iter_mut() {
 61                        for item in row.items.iter_mut() {
 62                            if item.item.type_id() == TypeId::of::<SearchFilter>()
 63                                && item.item.mouse_event(
 64                                    &mut frameworkclean,
 65                                    0,
 66                                    0,
 67                                    mouse.column,
 68                                    mouse.row,
 69                                )
 70                            {
 71                                searchfilter_clicked = true;
 72                                break;
 73                            }
 74                        }
 75
 76                        if searchfilter_clicked {
 77                            break;
 78                        }
 79                    }
 80                }
 81
 82                let updated = if searchfilter_clicked {
 83                    true
 84                } else {
 85                    framework.mouse_event(mouse.column, mouse.row)
 86                };
 87
 88                if updated {
 89                    framework
 90                        .data
 91                        .state
 92                        .get_mut::<Tasks>()
 93                        .unwrap()
 94                        .priority
 95                        .push(Task::RenderAll);
 96                }
 97            }
 98            Event::Key(key) => key_input(key, framework, terminal),
 99            // always render if there is a screen resize event
100            Event::Resize(_, _) => {
101                framework
102                    .data
103                    .state
104                    .get_mut::<Tasks>()
105                    .unwrap()
106                    .priority
107                    .push(Task::RenderAll);
108                framework
109                    .data
110                    .global
111                    .get_mut::<Status>()
112                    .unwrap()
113                    .render_image = true;
114            }
115            _ => {}
116        }
117    }
118    Ok(())
119}