1#![allow(clippy::type_complexity)]
2
3mod device;
4mod messages;
5mod net;
6mod state;
7mod views;
8
9use bevy::{
10 app::{AppExit, Plugin, PreUpdate},
11 ecs::{
12 message::{MessageReader, MessageWriter},
13 system::Res,
14 },
15 state::{app::AppExtStates, state::State},
16};
17use bevy_ratatui::event::KeyMessage;
18
19use crate::{
20 device::DevicePlugin,
21 messages::StrikeMessage,
22 net::NetPlugin,
23 state::AppState,
24 views::{HomeViewPlugin, MonitoringViewPlugin},
25};
26
27#[derive(Debug)]
28pub struct StrikerPlugin;
29
30impl Plugin for StrikerPlugin {
31 fn build(&self, app: &mut bevy::app::App) {
32 app.init_state::<AppState>()
33 .add_message::<StrikeMessage>()
34 .add_plugins((
35 NetPlugin,
36 DevicePlugin,
37 HomeViewPlugin,
38 MonitoringViewPlugin,
39 ))
40 .add_systems(PreUpdate, keybinds);
41 }
42}
43
44fn keybinds(
45 state: Res<State<AppState>>,
46 mut key_reader: MessageReader<KeyMessage>,
47 mut strike_writer: MessageWriter<StrikeMessage>,
48 mut app_exit: MessageWriter<AppExit>,
49) {
50 use ratatui::crossterm::event::KeyCode;
51 for message in key_reader.read() {
52 match message.code {
53 KeyCode::Char('s') if state.get() == &AppState::Home => {
54 strike_writer.write(StrikeMessage::ToggleSearch);
55 }
56 KeyCode::Up if state.get() == &AppState::Home => {
57 strike_writer.write(StrikeMessage::PrevDevice);
58 }
59 KeyCode::Down if state.get() == &AppState::Home => {
60 strike_writer.write(StrikeMessage::NextDevice);
61 }
62 KeyCode::Enter | KeyCode::Char(' ') if state.get() == &AppState::Home => {
63 strike_writer.write(StrikeMessage::MonitorDevice);
64 }
65 KeyCode::Backspace if state.get() == &AppState::Monitoring => {
66 strike_writer.write(StrikeMessage::StopMonitoring);
67 }
68 KeyCode::Char('q') | KeyCode::Esc => {
69 app_exit.write(AppExit::Success);
70 }
71 _ => {}
72 }
73 }
74}