mirror of
https://github.com/youwen5/iamb.git
synced 2025-06-20 13:49:52 -07:00
Support replying to messages (#3)
This commit is contained in:
parent
54ce042384
commit
d13d4b9f7f
7 changed files with 279 additions and 101 deletions
|
@ -8,14 +8,24 @@ use matrix_sdk::{
|
|||
media::{MediaFormat, MediaRequest},
|
||||
room::Room as MatrixRoom,
|
||||
ruma::{
|
||||
events::room::message::{MessageType, RoomMessageEventContent, TextMessageEventContent},
|
||||
events::room::message::{
|
||||
MessageType,
|
||||
OriginalRoomMessageEvent,
|
||||
RoomMessageEventContent,
|
||||
TextMessageEventContent,
|
||||
},
|
||||
OwnedRoomId,
|
||||
RoomId,
|
||||
},
|
||||
};
|
||||
|
||||
use modalkit::{
|
||||
tui::{buffer::Buffer, layout::Rect, widgets::StatefulWidget},
|
||||
tui::{
|
||||
buffer::Buffer,
|
||||
layout::Rect,
|
||||
text::{Span, Spans},
|
||||
widgets::{Paragraph, StatefulWidget, Widget},
|
||||
},
|
||||
widgets::textbox::{TextBox, TextBoxState},
|
||||
widgets::TerminalCursor,
|
||||
widgets::{PromptActions, WindowOps},
|
||||
|
@ -52,10 +62,11 @@ use crate::base::{
|
|||
ProgramContext,
|
||||
ProgramStore,
|
||||
RoomFocus,
|
||||
RoomInfo,
|
||||
SendAction,
|
||||
};
|
||||
|
||||
use crate::message::{Message, MessageContent, MessageTimeStamp};
|
||||
use crate::message::{Message, MessageEvent, MessageKey, MessageTimeStamp};
|
||||
|
||||
use super::scrollback::{Scrollback, ScrollbackState};
|
||||
|
||||
|
@ -69,6 +80,8 @@ pub struct ChatState {
|
|||
|
||||
scrollback: ScrollbackState,
|
||||
focus: RoomFocus,
|
||||
|
||||
reply_to: Option<MessageKey>,
|
||||
}
|
||||
|
||||
impl ChatState {
|
||||
|
@ -89,9 +102,27 @@ impl ChatState {
|
|||
|
||||
scrollback,
|
||||
focus: RoomFocus::MessageBar,
|
||||
|
||||
reply_to: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn get_reply_to<'a>(&self, info: &'a RoomInfo) -> Option<&'a OriginalRoomMessageEvent> {
|
||||
let key = self.reply_to.as_ref()?;
|
||||
let msg = info.messages.get(key)?;
|
||||
|
||||
if let MessageEvent::Original(ev) = &msg.event {
|
||||
Some(ev)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn reset(&mut self) -> EditRope {
|
||||
self.reply_to = None;
|
||||
self.tbox.reset()
|
||||
}
|
||||
|
||||
pub fn refresh_room(&mut self, store: &mut ProgramStore) {
|
||||
if let Some(room) = store.application.worker.client.get_room(self.id()) {
|
||||
self.room = room;
|
||||
|
@ -112,8 +143,13 @@ impl ChatState {
|
|||
let msg = self.scrollback.get_mut(info).ok_or(IambError::NoSelectedMessage)?;
|
||||
|
||||
match act {
|
||||
MessageAction::Cancel => {
|
||||
self.reply_to = None;
|
||||
|
||||
Ok(None)
|
||||
},
|
||||
MessageAction::Download(filename, force) => {
|
||||
if let MessageContent::Original(ev) = &msg.content {
|
||||
if let MessageEvent::Original(ev) = &msg.event {
|
||||
let media = client.media();
|
||||
|
||||
let mut filename = match filename {
|
||||
|
@ -121,7 +157,7 @@ impl ChatState {
|
|||
None => settings.dirs.downloads.clone(),
|
||||
};
|
||||
|
||||
let source = match &ev.msgtype {
|
||||
let source = match &ev.content.msgtype {
|
||||
MessageType::Audio(c) => {
|
||||
if filename.is_dir() {
|
||||
filename.push(c.body.as_str());
|
||||
|
@ -188,6 +224,12 @@ impl ChatState {
|
|||
|
||||
Err(IambError::NoAttachment.into())
|
||||
},
|
||||
MessageAction::Reply => {
|
||||
self.reply_to = self.scrollback.get_key(info);
|
||||
self.focus = RoomFocus::MessageBar;
|
||||
|
||||
Ok(None)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -203,6 +245,7 @@ impl ChatState {
|
|||
.client
|
||||
.get_joined_room(self.id())
|
||||
.ok_or(IambError::NotJoined)?;
|
||||
let info = store.application.rooms.entry(self.id().to_owned()).or_default();
|
||||
|
||||
let (event_id, msg) = match act {
|
||||
SendAction::Submit => {
|
||||
|
@ -214,15 +257,21 @@ impl ChatState {
|
|||
|
||||
let msg = TextMessageEventContent::plain(msg);
|
||||
let msg = MessageType::Text(msg);
|
||||
let msg = RoomMessageEventContent::new(msg);
|
||||
|
||||
let mut msg = RoomMessageEventContent::new(msg);
|
||||
|
||||
if let Some(m) = self.get_reply_to(info) {
|
||||
// XXX: Switch to RoomMessageEventContent::reply() once it's stable?
|
||||
msg = msg.make_reply_to(m);
|
||||
}
|
||||
|
||||
// XXX: second parameter can be a locally unique transaction id.
|
||||
// Useful for doing retries.
|
||||
let resp = room.send(msg.clone(), None).await.map_err(IambError::from)?;
|
||||
let event_id = resp.event_id;
|
||||
|
||||
// Clear the TextBoxState contents now that the message is sent.
|
||||
self.tbox.reset();
|
||||
// Reset message bar state now that it's been sent.
|
||||
self.reset();
|
||||
|
||||
(event_id, msg)
|
||||
},
|
||||
|
@ -252,12 +301,14 @@ impl ChatState {
|
|||
};
|
||||
|
||||
let user = store.application.settings.profile.user_id.clone();
|
||||
let info = store.application.get_room_info(self.id().to_owned());
|
||||
let key = (MessageTimeStamp::LocalEcho, event_id);
|
||||
let msg = MessageContent::Original(msg.into());
|
||||
let msg = MessageEvent::Local(msg.into());
|
||||
let msg = Message::new(msg, user, MessageTimeStamp::LocalEcho);
|
||||
info.messages.insert(key, msg);
|
||||
|
||||
// Jump to the end of the scrollback to show the message.
|
||||
self.scrollback.goto_latest();
|
||||
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
|
@ -333,6 +384,8 @@ impl WindowOps<IambInfo> for ChatState {
|
|||
|
||||
scrollback: self.scrollback.dup(store),
|
||||
focus: self.focus,
|
||||
|
||||
reply_to: None,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -432,7 +485,7 @@ impl PromptActions<ProgramContext, ProgramStore, IambInfo> for ChatState {
|
|||
return Ok(vec![]);
|
||||
}
|
||||
|
||||
let text = self.tbox.reset().trim();
|
||||
let text = self.reset().trim();
|
||||
|
||||
if text.is_empty() {
|
||||
let _ = self.sent.end();
|
||||
|
@ -506,15 +559,34 @@ impl<'a> StatefulWidget for Chat<'a> {
|
|||
let lines = state.tbox.has_lines(5).max(1) as u16;
|
||||
let drawh = area.height;
|
||||
let texth = lines.min(drawh).clamp(1, 5);
|
||||
let scrollh = drawh.saturating_sub(texth);
|
||||
let desch = if state.reply_to.is_some() {
|
||||
drawh.saturating_sub(texth).min(1)
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let scrollh = drawh.saturating_sub(texth).saturating_sub(desch);
|
||||
|
||||
let scrollarea = Rect::new(area.x, area.y, area.width, scrollh);
|
||||
let textarea = Rect::new(scrollarea.x, scrollarea.y + scrollh, scrollarea.width, texth);
|
||||
let descarea = Rect::new(area.x, scrollarea.y + scrollh, area.width, desch);
|
||||
let textarea = Rect::new(area.x, descarea.y + desch, area.width, texth);
|
||||
|
||||
let scrollback_focused = state.focus.is_scrollback() && self.focused;
|
||||
let scrollback = Scrollback::new(self.store).focus(scrollback_focused);
|
||||
scrollback.render(scrollarea, buf, &mut state.scrollback);
|
||||
|
||||
let desc_spans = state.reply_to.as_ref().and_then(|k| {
|
||||
let room = self.store.application.rooms.get(state.id())?;
|
||||
let msg = room.messages.get(k)?;
|
||||
let user = self.store.application.settings.get_user_span(msg.sender.as_ref());
|
||||
let spans = Spans(vec![Span::from("Replying to "), user]);
|
||||
|
||||
spans.into()
|
||||
});
|
||||
|
||||
if let Some(desc_spans) = desc_spans {
|
||||
Paragraph::new(desc_spans).render(descarea, buf);
|
||||
}
|
||||
|
||||
let prompt = if self.focused { "> " } else { " " };
|
||||
|
||||
let tbox = TextBox::new().prompt(prompt);
|
||||
|
|
|
@ -104,11 +104,26 @@ fn nth_after(pos: MessageKey, n: usize, info: &RoomInfo) -> MessageCursor {
|
|||
}
|
||||
|
||||
pub struct ScrollbackState {
|
||||
/// The room identifier.
|
||||
room_id: OwnedRoomId,
|
||||
|
||||
/// The buffer identifier used for saving marks, etc.
|
||||
id: IambBufferId,
|
||||
|
||||
/// The currently selected message in the scrollback.
|
||||
cursor: MessageCursor,
|
||||
|
||||
/// Contextual info about the viewport used during rendering.
|
||||
viewctx: ViewportContext<MessageCursor>,
|
||||
|
||||
/// The jumplist of visited messages.
|
||||
jumped: HistoryList<MessageCursor>,
|
||||
|
||||
/// Whether the full message should be drawn during the next render() call.
|
||||
///
|
||||
/// This is used to ensure that ^E/^Y work nicely when the cursor is currently
|
||||
/// on a multiline message.
|
||||
show_full_on_redraw: bool,
|
||||
}
|
||||
|
||||
impl ScrollbackState {
|
||||
|
@ -117,8 +132,20 @@ impl ScrollbackState {
|
|||
let cursor = MessageCursor::default();
|
||||
let viewctx = ViewportContext::default();
|
||||
let jumped = HistoryList::default();
|
||||
let show_full_on_redraw = false;
|
||||
|
||||
ScrollbackState { room_id, id, cursor, viewctx, jumped }
|
||||
ScrollbackState {
|
||||
room_id,
|
||||
id,
|
||||
cursor,
|
||||
viewctx,
|
||||
jumped,
|
||||
show_full_on_redraw,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn goto_latest(&mut self) {
|
||||
self.cursor = MessageCursor::latest();
|
||||
}
|
||||
|
||||
/// Set the dimensions and placement within the terminal window for this list.
|
||||
|
@ -126,6 +153,13 @@ impl ScrollbackState {
|
|||
self.viewctx.dimensions = (area.width as usize, area.height as usize);
|
||||
}
|
||||
|
||||
pub fn get_key(&self, info: &mut RoomInfo) -> Option<MessageKey> {
|
||||
self.cursor
|
||||
.timestamp
|
||||
.clone()
|
||||
.or_else(|| info.messages.last_key_value().map(|kv| kv.0.clone()))
|
||||
}
|
||||
|
||||
pub fn get_mut<'a>(&mut self, info: &'a mut RoomInfo) -> Option<&'a mut Message> {
|
||||
if let Some(k) = &self.cursor.timestamp {
|
||||
info.messages.get_mut(k)
|
||||
|
@ -397,7 +431,7 @@ impl ScrollbackState {
|
|||
continue;
|
||||
}
|
||||
|
||||
if needle.is_match(msg.content.show().as_ref()) {
|
||||
if needle.is_match(msg.event.show().as_ref()) {
|
||||
mc = MessageCursor::from(key.clone()).into();
|
||||
count -= 1;
|
||||
}
|
||||
|
@ -421,7 +455,7 @@ impl ScrollbackState {
|
|||
break;
|
||||
}
|
||||
|
||||
if needle.is_match(msg.content.show().as_ref()) {
|
||||
if needle.is_match(msg.event.show().as_ref()) {
|
||||
mc = MessageCursor::from(key.clone()).into();
|
||||
count -= 1;
|
||||
}
|
||||
|
@ -462,6 +496,7 @@ impl WindowOps<IambInfo> for ScrollbackState {
|
|||
cursor: self.cursor.clone(),
|
||||
viewctx: self.viewctx.clone(),
|
||||
jumped: self.jumped.clone(),
|
||||
show_full_on_redraw: false,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -582,6 +617,8 @@ impl EditorActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
|
|||
self.cursor = pos;
|
||||
}
|
||||
|
||||
self.show_full_on_redraw = true;
|
||||
|
||||
return Ok(None);
|
||||
},
|
||||
EditAction::Yank => {
|
||||
|
@ -667,7 +704,7 @@ impl EditorActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
|
|||
let mut yanked = EditRope::from("");
|
||||
|
||||
for (_, msg) in self.messages(range, info) {
|
||||
yanked += EditRope::from(msg.content.show().into_owned());
|
||||
yanked += EditRope::from(msg.event.show().into_owned());
|
||||
yanked += EditRope::from('\n');
|
||||
}
|
||||
|
||||
|
@ -1173,15 +1210,15 @@ impl<'a> StatefulWidget for Scrollback<'a> {
|
|||
nth_key_before(cursor_key.clone(), height, info)
|
||||
};
|
||||
|
||||
let full = cursor.timestamp.is_none();
|
||||
|
||||
let foc = self.focused || cursor.timestamp.is_some();
|
||||
let full = std::mem::take(&mut state.show_full_on_redraw) || cursor.timestamp.is_none();
|
||||
let mut lines = vec![];
|
||||
let mut sawit = false;
|
||||
let mut prev = None;
|
||||
|
||||
for (key, item) in info.messages.range(&corner_key..) {
|
||||
let sel = key == cursor_key;
|
||||
let txt = item.show(prev, self.focused && sel, &state.viewctx, settings);
|
||||
let txt = item.show(prev, foc && sel, &state.viewctx, settings);
|
||||
|
||||
prev = Some(item);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue