Skip to content

Commit

Permalink
Extract user_input_event from tui
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaschan committed Sep 4, 2024
1 parent c4c1bc9 commit 5dd8ba4
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 57 deletions.
5 changes: 5 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
[workspace]
members = ["insanity-core", "insanity-native-tui-app", "insanity-tui-adapter"]
default-members = ["insanity-native-tui-app"]
resolver = "2"
1 change: 1 addition & 0 deletions insanity-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ version = "1.1.9"
edition = "2021"

[dependencies]
bon = "2.1.1"
15 changes: 1 addition & 14 deletions insanity-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1 @@
pub fn add(left: u64, right: u64) -> u64 {
left + right
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}
pub mod user_input_event;
10 changes: 10 additions & 0 deletions insanity-core/src/user_input_event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#[derive(Debug, PartialEq, Eq)]
pub enum UserInputEvent {
DisablePeer(String),
EnablePeer(String),
DisableDenoise(String),
EnableDenoise(String),
SetVolume(String, usize),
SendMessage(String),
SetMuteSelf(bool),
}
2 changes: 2 additions & 0 deletions insanity-native-tui-app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name = "insanity-tui-app"
version = "1.1.9"
authors = ["Nicolas Chan <[email protected]>"]
edition = "2018"
default-run = "insanity"

[[bin]]
name = "insanity"
Expand All @@ -24,6 +25,7 @@ uuid = { version = "1.7", features = ["serde", "v4"] }
dirs = "5.0.1"
serde_json = "1.0.114"
rubato = "0.10"
insanity-core = { path = "../insanity-core" }
insanity-tui-adapter = { path = "../insanity-tui-adapter" }
sha2 = "0.10.8"
whoami = "1.5.1"
Expand Down
27 changes: 14 additions & 13 deletions insanity-native-tui-app/src/connection_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use std::{
},
};

use insanity_tui_adapter::{AppEvent, UserAction};
use insanity_core::user_input_event::UserInputEvent;
use insanity_tui_adapter::AppEvent;

use sha2::{Digest, Sha256};
use veq::{snow_types::SnowKeypair, veq::VeqSocket};
Expand Down Expand Up @@ -38,7 +39,7 @@ pub struct AugmentedInfo {
pub struct ConnectionManager {
socket: VeqSocket,
cancellation_token: CancellationToken,
user_action_tx: mpsc::UnboundedSender<UserAction>,
user_action_tx: mpsc::UnboundedSender<UserInputEvent>,
}

impl ConnectionManager {
Expand All @@ -54,7 +55,7 @@ impl ConnectionManager {
self.cancellation_token.cancel();
}

pub fn send_user_action(&self, action: UserAction) -> anyhow::Result<()> {
pub fn send_user_action(&self, action: UserInputEvent) -> anyhow::Result<()> {
self.user_action_tx.send(action)?;
Ok(())
}
Expand All @@ -66,7 +67,7 @@ impl ConnectionManager {
base_dir: PathBuf,
display_name: Option<String>,
app_event_tx: Option<mpsc::UnboundedSender<AppEvent>>,
user_action_rx: mpsc::UnboundedReceiver<UserAction>,
user_action_rx: mpsc::UnboundedReceiver<UserInputEvent>,
) -> anyhow::Result<()> {
let connection_info = self.socket.connection_info();
log::debug!("Connection info: {:?}", connection_info);
Expand Down Expand Up @@ -216,7 +217,7 @@ impl ConnectionManagerBuilder {
fn manage_peers(
socket: veq::veq::VeqSocket,
app_event_tx: Option<mpsc::UnboundedSender<AppEvent>>,
mut user_action_rx: mpsc::UnboundedReceiver<UserAction>,
mut user_action_rx: mpsc::UnboundedReceiver<UserInputEvent>,
cancellation_token: CancellationToken,
) -> mpsc::UnboundedSender<AugmentedInfo> {
// Channel for the manage_peers task to receive updated peers info.
Expand Down Expand Up @@ -310,50 +311,50 @@ fn update_peer_info(
}

fn handle_user_action(
user_action: UserAction,
user_action: UserInputEvent,
sender_is_muted: Arc<AtomicBool>,
app_event_tx: Option<mpsc::UnboundedSender<AppEvent>>,
managed_peers: &mut HashMap<uuid::Uuid, ManagedPeer>,
) -> anyhow::Result<()> {
match user_action {
UserAction::DisableDenoise(id) => {
UserInputEvent::DisableDenoise(id) => {
let id = uuid::Uuid::from_str(&id)?;
if let Some(peer) = managed_peers.get(&id) {
peer.set_denoise(false)?;
}
}
UserAction::EnableDenoise(id) => {
UserInputEvent::EnableDenoise(id) => {
let id = uuid::Uuid::from_str(&id)?;
if let Some(peer) = managed_peers.get(&id) {
peer.set_denoise(true)?;
}
}
UserAction::DisablePeer(id) => {
UserInputEvent::DisablePeer(id) => {
let id = uuid::Uuid::from_str(&id)?;
if let Some(peer) = managed_peers.get(&id) {
peer.disable()?;
}
}
UserAction::EnablePeer(id) => {
UserInputEvent::EnablePeer(id) => {
let id = uuid::Uuid::from_str(&id)?;
if let Some(peer) = managed_peers.get(&id) {
peer.enable();
}
}
UserAction::SetVolume(id, volume) => {
UserInputEvent::SetVolume(id, volume) => {
let id = uuid::Uuid::from_str(&id)?;
if let Some(peer) = managed_peers.get(&id) {
peer.set_volume(volume)?;
}
}
UserAction::SendMessage(message) => {
UserInputEvent::SendMessage(message) => {
for (_, peer) in managed_peers.iter() {
if let Err(e) = peer.send_message(message.clone()) {
log::debug!("Failed to send message to a peer: {:?}", e);
}
}
}
UserAction::SetMuteSelf(is_muted) => {
UserInputEvent::SetMuteSelf(is_muted) => {
sender_is_muted.store(is_muted, Ordering::Relaxed);
if let Some(app_event_tx) = app_event_tx {
if let Err(e) = app_event_tx.send(AppEvent::MuteSelf(is_muted)) {
Expand Down
1 change: 1 addition & 0 deletions insanity-tui-adapter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ path = "src/main.rs"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
insanity-core = { path = "../insanity-core" }
crossterm = "0.23"
tokio = { version = "1.16.1", features = [
"sync",
Expand Down
32 changes: 11 additions & 21 deletions insanity-tui-adapter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crossterm::{
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use insanity_core::user_input_event::UserInputEvent;
use std::collections::BTreeMap;
use std::{error::Error, io, io::Stdout};
use tokio::{
Expand Down Expand Up @@ -117,19 +118,8 @@ pub enum AppEvent {
MuteSelf(bool),
}

#[derive(Debug, PartialEq, Eq)]
pub enum UserAction {
DisablePeer(String),
EnablePeer(String),
DisableDenoise(String),
EnableDenoise(String),
SetVolume(String, usize),
SendMessage(String),
SetMuteSelf(bool),
}

pub struct App {
pub user_action_sender: UnboundedSender<UserAction>,
pub user_action_sender: UnboundedSender<UserInputEvent>,
pub tabs: [&'static str; NUM_TABS],
pub tab_index: usize,
pub killed: bool,
Expand All @@ -148,7 +138,7 @@ pub struct App {
}

impl App {
pub fn new(sender: UnboundedSender<UserAction>) -> App {
pub fn new(sender: UnboundedSender<UserInputEvent>) -> App {
App {
user_action_sender: sender,
tabs: TAB_NAMES,
Expand Down Expand Up @@ -331,11 +321,11 @@ impl App {
if let Some(peer) = self.selected_peer() {
if peer.state == PeerState::Disabled {
self.user_action_sender
.send(UserAction::EnablePeer(peer.id.clone()))
.send(UserInputEvent::EnablePeer(peer.id.clone()))
.unwrap();
} else {
self.user_action_sender
.send(UserAction::DisablePeer(peer.id.clone()))
.send(UserInputEvent::DisablePeer(peer.id.clone()))
.unwrap();
}
}
Expand All @@ -357,7 +347,7 @@ impl App {
let own_address = self.own_public_key.clone().unwrap_or(default);
self.add_message((own_address, message.clone()));
self.user_action_sender
.send(UserAction::SendMessage(message))
.send(UserInputEvent::SendMessage(message))
.unwrap();
}
}
Expand All @@ -366,11 +356,11 @@ impl App {
if let Some(peer) = self.selected_peer() {
if peer.denoised {
self.user_action_sender
.send(UserAction::DisableDenoise(peer.id.clone()))
.send(UserInputEvent::DisableDenoise(peer.id.clone()))
.unwrap();
} else {
self.user_action_sender
.send(UserAction::EnableDenoise(peer.id.clone()))
.send(UserInputEvent::EnableDenoise(peer.id.clone()))
.unwrap();
}
}
Expand All @@ -379,7 +369,7 @@ impl App {
fn adjust_volume(&mut self, delta: isize) {
if let Some(peer) = self.selected_peer() {
self.user_action_sender
.send(UserAction::SetVolume(
.send(UserInputEvent::SetVolume(
peer.id.clone(),
add_in_bounds(peer.volume, 0, 999, delta),
))
Expand All @@ -399,7 +389,7 @@ impl App {
fn toggle_mute_self(&mut self) {
self.mute_self = !self.mute_self;
self.user_action_sender
.send(UserAction::SetMuteSelf(self.mute_self))
.send(UserInputEvent::SetMuteSelf(self.mute_self))
.unwrap();
}

Expand Down Expand Up @@ -506,7 +496,7 @@ pub async fn handle_input(sender: UnboundedSender<AppEvent>) -> JoinHandle<()> {
pub async fn start_tui() -> Result<
(
UnboundedSender<AppEvent>,
UnboundedReceiver<UserAction>,
UnboundedReceiver<UserInputEvent>,
JoinHandle<Terminal<CrosstermBackend<Stdout>>>,
),
Box<dyn Error>,
Expand Down
17 changes: 9 additions & 8 deletions insanity-tui-adapter/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use insanity_tui_adapter::{start_tui, stop_tui, AppEvent, Peer, PeerState, UserAction};
use insanity_core::user_input_event::UserInputEvent;
use insanity_tui_adapter::{start_tui, stop_tui, AppEvent, Peer, PeerState};
use std::{collections::BTreeMap, error::Error};

#[tokio::main]
Expand Down Expand Up @@ -47,17 +48,17 @@ async fn main() -> Result<(), Box<dyn Error>> {
tokio::spawn(async move {
while let Some(event) = user_action_receiver.recv().await {
match event {
UserAction::EnableDenoise(peer_id) => {
UserInputEvent::EnableDenoise(peer_id) => {
sender
.send(AppEvent::SetPeerDenoise(peer_id, true))
.unwrap();
}
UserAction::DisableDenoise(peer_id) => {
UserInputEvent::DisableDenoise(peer_id) => {
sender
.send(AppEvent::SetPeerDenoise(peer_id, false))
.unwrap();
}
UserAction::DisablePeer(peer_id) => {
UserInputEvent::DisablePeer(peer_id) => {
sender
.send(AppEvent::AddPeer(
peers
Expand All @@ -68,7 +69,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
))
.unwrap();
}
UserAction::EnablePeer(peer_id) => {
UserInputEvent::EnablePeer(peer_id) => {
sender
.send(AppEvent::AddPeer(
peers
Expand All @@ -79,13 +80,13 @@ async fn main() -> Result<(), Box<dyn Error>> {
))
.unwrap();
}
UserAction::SetVolume(peer_id, volume) => {
UserInputEvent::SetVolume(peer_id, volume) => {
sender
.send(AppEvent::SetPeerVolume(peer_id, volume))
.unwrap();
}
UserAction::SendMessage(_message) => {}
UserAction::SetMuteSelf(_) => todo!(),
UserInputEvent::SendMessage(_message) => {}
UserInputEvent::SetMuteSelf(_) => todo!(),
}
}
});
Expand Down

0 comments on commit 5dd8ba4

Please sign in to comment.