Skip to main content

gm_lib/tui/
events.rs

1use std::time::Duration;
2
3use alloy::{
4    primitives::Address,
5    signers::{k256::ecdsa::SigningKey, Signature},
6};
7use walletconnect_sdk::wc_message::WcMessage;
8
9use crate::{
10    tui::app::widgets::{
11        invite_popup::{InviteCodeClaimStatus, InviteCodeValidity},
12        tx_popup::TxStatus,
13    },
14    utils::assets::{Asset, LightClientVerification, TokenAddress},
15};
16
17use reqwest::Error as ReqwestError;
18
19use super::app::{
20    pages::walletconnect::WalletConnectStatus,
21    widgets::candle_chart::{Candle, Interval},
22};
23
24pub mod assets;
25pub mod eth_price;
26pub mod helios;
27pub mod input;
28pub mod recent_addresses;
29
30#[derive(Debug)]
31pub enum Event {
32    Input(crossterm::event::KeyEvent),
33
34    AccountChange(Address),
35    ConfigUpdate,
36
37    EthPriceUpdate(String),
38    EthPriceError(ReqwestError),
39
40    HashRateResult(f64),
41    HashRateError(String),
42    VanityResult(SigningKey, usize, Duration),
43
44    AssetsUpdate(Address, Vec<Asset>),
45    AssetsUpdateError(String, bool), // bool - whether to silence the error
46
47    RecentAddressesUpdate(Vec<Address>),
48    RecentAddressesUpdateError(String),
49
50    CandlesUpdate(Vec<Candle>, Interval),
51    CandlesUpdateError(ReqwestError),
52
53    TxUpdate(TxStatus),
54    TxError(String),
55
56    SignResult(Signature),
57    SignError(String),
58
59    WalletConnectStatus(WalletConnectStatus),
60    WalletConnectMessage(Address, Box<WcMessage>),
61    WalletConnectError(Address, String),
62
63    HeliosUpdate {
64        account: Address,
65        network: String,
66        token_address: TokenAddress,
67        status: LightClientVerification,
68    },
69    HeliosError(String),
70
71    InviteCodeValidity(InviteCodeValidity),
72    InviteCodeClaimStatus(InviteCodeClaimStatus),
73}
74
75impl Event {
76    pub fn fmt(&self) -> String {
77        format!("{self:?}")
78    }
79
80    pub fn is_input(&self) -> bool {
81        matches!(self, Event::Input(_))
82    }
83
84    pub fn is_space_or_enter_pressed(&self) -> bool {
85        self.is_char_pressed(Some(' ')) || self.is_key_pressed(crossterm::event::KeyCode::Enter)
86    }
87
88    pub fn is_char_pressed(&self, char: Option<char>) -> bool {
89        if let Some(ch) = char {
90            matches!(
91                self,
92                Event::Input(crossterm::event::KeyEvent {
93                    kind: crossterm::event::KeyEventKind::Press,
94                    code: crossterm::event::KeyCode::Char(c),
95                    ..
96                }) if *c == ch
97            )
98        } else {
99            matches!(
100                self,
101                Event::Input(crossterm::event::KeyEvent {
102                    kind: crossterm::event::KeyEventKind::Press,
103                    ..
104                })
105            )
106        }
107    }
108
109    pub fn is_key_pressed(&self, key: crossterm::event::KeyCode) -> bool {
110        matches!(
111            self,
112            Event::Input(crossterm::event::KeyEvent {
113                kind: crossterm::event::KeyEventKind::Press,
114                code,
115                modifiers: crossterm::event::KeyModifiers::NONE,
116                ..
117            }) if *code == key
118        )
119    }
120}