Overhaul UI event handling

This commit is contained in:
Joscha 2022-08-20 18:36:20 +02:00
parent ade06efa01
commit ded927b9f0
9 changed files with 177 additions and 103 deletions

View file

@ -90,7 +90,7 @@ impl<M: Msg, S: MsgStore<M>> ChatState<M, S> {
}
}
pub async fn handle_event(
pub async fn handle_input_event(
&mut self,
terminal: &mut Terminal,
crossterm_lock: &Arc<FairMutex<()>>,
@ -100,7 +100,7 @@ impl<M: Msg, S: MsgStore<M>> ChatState<M, S> {
match self.mode {
Mode::Tree => {
self.tree
.handle_event(terminal, crossterm_lock, event, can_compose)
.handle_input_event(terminal, crossterm_lock, event, can_compose)
.await
}
}

View file

@ -79,7 +79,7 @@ impl<M: Msg, S: MsgStore<M>> InnerTreeViewState<M, S> {
bindings.binding("z", "center cursor on screen");
}
async fn handle_movement_event(&mut self, frame: &mut Frame, event: &InputEvent) -> bool {
async fn handle_movement_input_event(&mut self, frame: &mut Frame, event: &InputEvent) -> bool {
let chat_height = frame.size().height - 3;
match event {
@ -115,7 +115,7 @@ impl<M: Msg, S: MsgStore<M>> InnerTreeViewState<M, S> {
bindings.binding("ctrl+s", "mark all older messages as seen");
}
async fn handle_action_event(&mut self, event: &InputEvent, id: Option<&M::Id>) -> bool {
async fn handle_action_input_event(&mut self, event: &InputEvent, id: Option<&M::Id>) -> bool {
match event {
key!(' ') => {
if let Some(id) = id {
@ -162,7 +162,7 @@ impl<M: Msg, S: MsgStore<M>> InnerTreeViewState<M, S> {
bindings.binding("t", "start a new thread");
}
async fn handle_edit_initiating_event(
async fn handle_edit_initiating_input_event(
&mut self,
event: &InputEvent,
id: Option<M::Id>,
@ -198,7 +198,7 @@ impl<M: Msg, S: MsgStore<M>> InnerTreeViewState<M, S> {
}
}
async fn handle_normal_event(
async fn handle_normal_input_event(
&mut self,
frame: &mut Frame,
event: &InputEvent,
@ -206,12 +206,12 @@ impl<M: Msg, S: MsgStore<M>> InnerTreeViewState<M, S> {
id: Option<M::Id>,
) -> bool {
#[allow(clippy::if_same_then_else)]
if self.handle_movement_event(frame, event).await {
if self.handle_movement_input_event(frame, event).await {
true
} else if self.handle_action_event(event, id.as_ref()).await {
} else if self.handle_action_input_event(event, id.as_ref()).await {
true
} else if can_compose {
self.handle_edit_initiating_event(event, id).await
self.handle_edit_initiating_input_event(event, id).await
} else {
false
}
@ -223,7 +223,7 @@ impl<M: Msg, S: MsgStore<M>> InnerTreeViewState<M, S> {
util::list_editor_key_bindings(bindings, |_| true, true);
}
fn handle_editor_event(
fn handle_editor_input_event(
&mut self,
terminal: &mut Terminal,
crossterm_lock: &Arc<FairMutex<()>>,
@ -251,7 +251,7 @@ impl<M: Msg, S: MsgStore<M>> InnerTreeViewState<M, S> {
}
_ => {
let handled = util::handle_editor_event(
let handled = util::handle_editor_input_event(
&self.editor,
terminal,
crossterm_lock,
@ -282,7 +282,7 @@ impl<M: Msg, S: MsgStore<M>> InnerTreeViewState<M, S> {
}
}
async fn handle_event(
async fn handle_input_event(
&mut self,
terminal: &mut Terminal,
crossterm_lock: &Arc<FairMutex<()>>,
@ -292,7 +292,7 @@ impl<M: Msg, S: MsgStore<M>> InnerTreeViewState<M, S> {
match &self.cursor {
Cursor::Bottom => {
if self
.handle_normal_event(terminal.frame(), event, can_compose, None)
.handle_normal_input_event(terminal.frame(), event, can_compose, None)
.await
{
Reaction::Handled
@ -303,7 +303,7 @@ impl<M: Msg, S: MsgStore<M>> InnerTreeViewState<M, S> {
Cursor::Msg(id) => {
let id = id.clone();
if self
.handle_normal_event(terminal.frame(), event, can_compose, Some(id))
.handle_normal_input_event(terminal.frame(), event, can_compose, Some(id))
.await
{
Reaction::Handled
@ -314,7 +314,7 @@ impl<M: Msg, S: MsgStore<M>> InnerTreeViewState<M, S> {
Cursor::Editor {
coming_from,
parent,
} => self.handle_editor_event(
} => self.handle_editor_input_event(
terminal,
crossterm_lock,
event,
@ -322,7 +322,10 @@ impl<M: Msg, S: MsgStore<M>> InnerTreeViewState<M, S> {
parent.clone(),
),
Cursor::Pseudo { .. } => {
if self.handle_movement_event(terminal.frame(), event).await {
if self
.handle_movement_input_event(terminal.frame(), event)
.await
{
Reaction::Handled
} else {
Reaction::NotHandled
@ -365,7 +368,7 @@ impl<M: Msg, S: MsgStore<M>> TreeViewState<M, S> {
self.0.lock().await.list_key_bindings(bindings, can_compose);
}
pub async fn handle_event(
pub async fn handle_input_event(
&mut self,
terminal: &mut Terminal,
crossterm_lock: &Arc<FairMutex<()>>,
@ -375,7 +378,7 @@ impl<M: Msg, S: MsgStore<M>> TreeViewState<M, S> {
self.0
.lock()
.await
.handle_event(terminal, crossterm_lock, event, can_compose)
.handle_input_event(terminal, crossterm_lock, event, can_compose)
.await
}

View file

@ -11,7 +11,8 @@ use tokio::sync::{mpsc, oneshot};
use toss::styled::Styled;
use toss::terminal::Terminal;
use crate::euph;
use crate::euph::{self, EuphRoomEvent};
use crate::macros::{ok_or_return, some_or_return};
use crate::store::MsgStore;
use crate::ui::chat::{ChatState, Reaction};
use crate::ui::input::{key, InputEvent, KeyBindingsList, KeyEvent};
@ -61,10 +62,32 @@ impl EuphRoom {
}
}
async fn shovel_room_events(
name: String,
mut euph_room_event_rx: mpsc::UnboundedReceiver<EuphRoomEvent>,
ui_event_tx: mpsc::UnboundedSender<UiEvent>,
) {
loop {
let event = some_or_return!(euph_room_event_rx.recv().await);
let event = UiEvent::EuphRoom {
name: name.clone(),
event,
};
ok_or_return!(ui_event_tx.send(event));
}
}
pub fn connect(&mut self) {
if self.room.is_none() {
self.room = Some(euph::Room::new(
self.chat.store().clone(),
let store = self.chat.store().clone();
let name = store.room().to_string();
let (room, euph_room_event_rx) = euph::Room::new(store);
self.room = Some(room);
tokio::task::spawn(Self::shovel_room_events(
name,
euph_room_event_rx,
self.ui_event_tx.clone(),
));
}
@ -353,7 +376,7 @@ impl EuphRoom {
}
}
pub async fn handle_event(
pub async fn handle_input_event(
&mut self,
terminal: &mut Terminal,
crossterm_lock: &Arc<FairMutex<()>>,
@ -366,7 +389,7 @@ impl EuphRoom {
if let Ok(Some(Status::Joined(joined))) = room.status().await {
match self
.chat
.handle_event(terminal, crossterm_lock, event, true)
.handle_input_event(terminal, crossterm_lock, event, true)
.await
{
Reaction::NotHandled => {}
@ -392,7 +415,7 @@ impl EuphRoom {
}
self.chat
.handle_event(terminal, crossterm_lock, event, false)
.handle_input_event(terminal, crossterm_lock, event, false)
.await
.handled()
}
@ -408,7 +431,7 @@ impl EuphRoom {
self.state = State::Normal;
true
}
_ => util::handle_editor_event(
_ => util::handle_editor_input_event(
ed,
terminal,
crossterm_lock,

View file

@ -278,7 +278,7 @@ impl Rooms {
}
}
pub async fn handle_event(
pub async fn handle_input_event(
&mut self,
terminal: &mut Terminal,
crossterm_lock: &Arc<FairMutex<()>>,
@ -326,7 +326,10 @@ impl Rooms {
},
State::ShowRoom(name) => {
if let Some(room) = self.euph_rooms.get_mut(name) {
if room.handle_event(terminal, crossterm_lock, event).await {
if room
.handle_input_event(terminal, crossterm_lock, event)
.await
{
return true;
}
@ -348,7 +351,7 @@ impl Rooms {
}
}
_ => {
return util::handle_editor_event(
return util::handle_editor_input_event(
ed,
terminal,
crossterm_lock,

View file

@ -59,7 +59,7 @@ pub fn list_editor_key_bindings(
bindings.binding("↑/↓", "move cursor up/down");
}
pub fn handle_editor_event(
pub fn handle_editor_input_event(
editor: &EditorState,
terminal: &mut Terminal,
crossterm_lock: &Arc<FairMutex<()>>,