Skip to content

Commit

Permalink
Add FocusLost and FocusGained event/action, they require mouse
Browse files Browse the repository at this point in the history
…support

Signed-off-by: FedericoBruzzone <[email protected]>
  • Loading branch information
FedericoBruzzone committed Sep 3, 2024
1 parent 560debe commit e8c84a0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ pub enum Action {
Resize(u16, u16),
/// Paste action with a `String`.
Paste(String),
/// Focus Lost action.
FocusLost,
/// Focus Gained action.
FocusGained,

/// GetMe action.
GetMe,
Expand Down
7 changes: 7 additions & 0 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ pub enum Event {
Init,
/// Render event.
Render,
/// Focus Lost event.
FocusLost,
/// Focus Gained event.
FocusGained,

/// Update area event with a `Rect` struct.
UpdateArea(Rect),
/// EditMessage event with a `String`.
Expand Down Expand Up @@ -170,6 +175,8 @@ impl Display for Event {
Event::Mouse(mouse) => write!(f, "Mouse({:?})", mouse),
Event::UpdateArea(area) => write!(f, "UpdateArea({:?})", area),
Event::Paste(s) => write!(f, "Paste({})", s),
Event::FocusLost => write!(f, "FocusLost"),
Event::FocusGained => write!(f, "FocusGained"),
Event::GetMe => write!(f, "GetMe"),
Event::LoadChats(chat_list, limit) => {
write!(f, "LoadChats({:?}, {})", chat_list, limit)
Expand Down
4 changes: 4 additions & 0 deletions src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ async fn handle_tui_backend_events(
}
}
}
Event::FocusLost => app_context.action_tx().send(Action::FocusLost)?,
Event::FocusGained => app_context.action_tx().send(Action::FocusGained)?,
Event::Paste(ref text) => app_context.action_tx().send(Action::Paste(text.clone()))?,
_ => {}
}
Expand Down Expand Up @@ -264,6 +266,8 @@ pub async fn handle_app_actions(
tui.draw(f, f.area()).unwrap();
})?;
}
Action::FocusLost => tui_backend.suspend()?,
Action::FocusGained => tui_backend.resume()?,
Action::Quit => {
app_context.quit_store(true);
}
Expand Down
10 changes: 8 additions & 2 deletions src/tui_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ impl TuiBackend {
/// # Returns
/// * `Result<(), io::Error>` - An Ok result or an error.
pub fn suspend(&mut self) -> Result<(), std::io::Error> {
tracing::info!("Suspending TuiBackend");
self.exit();
#[cfg(not(windows))]
signal_hook::low_level::raise(signal_hook::consts::signal::SIGTSTP)?;
Expand All @@ -156,6 +157,7 @@ impl TuiBackend {
/// # Returns
/// * `Result<(), io::Error>` - An Ok result or an error.
pub fn resume(&mut self) -> Result<(), std::io::Error> {
tracing::info!("Resuming TuiBackend");
self.enter()?;
Ok(())
}
Expand Down Expand Up @@ -243,8 +245,12 @@ impl TuiBackend {
CrosstermEvent::Resize(width, height) => {
event_tx.send(Event::Resize(width, height))?;
},
CrosstermEvent::FocusLost => {} // [TODO] handle focus lost
CrosstermEvent::FocusGained => {} // [TODO] handle focus gained
CrosstermEvent::FocusLost => {
event_tx.send(Event::FocusLost)?;
}
CrosstermEvent::FocusGained => {
event_tx.send(Event::FocusGained)?;
}
CrosstermEvent::Paste(text) => {
event_tx.send(Event::Paste(text))?;
},
Expand Down

0 comments on commit e8c84a0

Please sign in to comment.