Commit 4935899aed

4935899aed8cbb8e19358c199d5ea9f549f83649

parent: cc1d2f3bf8

Unregistered key

Ulyssa <git@ulyssa.dev> · 2023-05-02 05:14 UTC

Indicate when you're editing a message (#75)

Layout: unified · split

src/base.rs +10 −4
@@ -103,7 +103,9 @@ pub enum VerifyAction {
103#[derive(Clone, Debug, Eq, PartialEq)] 103#[derive(Clone, Debug, Eq, PartialEq)]
104pub enum MessageAction { 104pub enum MessageAction {
105 /// Cance the current reply or edit. 105 /// Cance the current reply or edit.
106 Cancel, 106 ///
107 /// The [bool] argument indicates whether to skip confirmation for clearing the message bar.
108 Cancel(bool),
107 109
108 /// Download an attachment to the given path. 110 /// Download an attachment to the given path.
109 /// 111 ///
@@ -446,8 +448,12 @@ impl RoomInfo {
446 } 448 }
447 } 449 }
448 450
451 pub fn get_message_key(&self, event_id: &EventId) -> Option<&MessageKey> {
452 self.keys.get(event_id)?.to_message_key()
453 }
454
449 pub fn get_event(&self, event_id: &EventId) -> Option<&Message> { 455 pub fn get_event(&self, event_id: &EventId) -> Option<&Message> {
450 self.messages.get(self.keys.get(event_id)?.to_message_key()?) 456 self.messages.get(self.get_message_key(event_id)?)
451 } 457 }
452 458
453 pub fn insert_reaction(&mut self, react: ReactionEvent) { 459 pub fn insert_reaction(&mut self, react: ReactionEvent) {
@@ -489,10 +495,10 @@ impl RoomInfo {
489 495
490 match &mut msg.event { 496 match &mut msg.event {
491 MessageEvent::Original(orig) => { 497 MessageEvent::Original(orig) => {
492 orig.content = *new_content; 498 orig.content.msgtype = new_content.msgtype;
493 }, 499 },
494 MessageEvent::Local(_, content) => { 500 MessageEvent::Local(_, content) => {
495 *content = new_content; 501 content.msgtype = new_content.msgtype;
496 }, 502 },
497 MessageEvent::Redacted(_) | 503 MessageEvent::Redacted(_) |
498 MessageEvent::EncryptedOriginal(_) | 504 MessageEvent::EncryptedOriginal(_) |
src/commands.rs +1 −1
@@ -177,7 +177,7 @@ fn iamb_cancel(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
177 return Result::Err(CommandError::InvalidArgument); 177 return Result::Err(CommandError::InvalidArgument);
178 } 178 }
179 179
180 let mact = IambAction::from(MessageAction::Cancel); 180 let mact = IambAction::from(MessageAction::Cancel(desc.bang));
181 let step = CommandStep::Continue(mact.into(), ctx.context.take()); 181 let step = CommandStep::Continue(mact.into(), ctx.context.take());
182 182
183 return Ok(step); 183 return Ok(step);
src/windows/room/chat.rs +41 −22
@@ -165,11 +165,20 @@ impl ChatState {
165 .ok_or(IambError::NoSelectedMessage)?; 165 .ok_or(IambError::NoSelectedMessage)?;
166 166
167 match act { 167 match act {
168 MessageAction::Cancel => { 168 MessageAction::Cancel(skip_confirm) => {
169 self.reply_to = None; 169 self.reply_to = None;
170 self.editing = None; 170 self.editing = None;
171 171
172 Ok(None) 172 if skip_confirm {
173 return Ok(None);
174 }
175
176 let msg = "Would you like to clear the message bar?";
177 let act = PromptAction::Abort(false);
178 let prompt = PromptYesNo::new(msg, vec![Action::from(act)]);
179 let prompt = Box::new(prompt);
180
181 Err(UIError::NeedConfirm(prompt))
173 }, 182 },
174 MessageAction::Download(filename, flags) => { 183 MessageAction::Download(filename, flags) => {
175 if let MessageEvent::Original(ev) = &msg.event { 184 if let MessageEvent::Original(ev) = &msg.event {
@@ -286,6 +295,7 @@ impl ChatState {
286 }; 295 };
287 296
288 self.tbox.set_text(text); 297 self.tbox.set_text(text);
298 self.reply_to = msg.reply_to().and_then(|id| info.get_message_key(&id)).cloned();
289 self.editing = self.scrollback.get_key(info); 299 self.editing = self.scrollback.get_key(info);
290 self.focus = RoomFocus::MessageBar; 300 self.focus = RoomFocus::MessageBar;
291 301
@@ -742,10 +752,32 @@ impl<'a> StatefulWidget for Chat<'a> {
742 type State = ChatState; 752 type State = ChatState;
743 753
744 fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) { 754 fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
755 // Determine whether we have a description to show for the message bar.
756 let desc_spans = match (&state.editing, &state.reply_to) {
757 (None, None) => None,
758 (Some(_), None) => Some(Spans::from("Editing message")),
759 (editing, Some(_)) => {
760 state.reply_to.as_ref().and_then(|k| {
761 let room = self.store.application.rooms.get(state.id())?;
762 let msg = room.messages.get(k)?;
763 let user = self.store.application.settings.get_user_span(msg.sender.as_ref());
764 let prefix = if editing.is_some() {
765 Span::from("Editing reply to ")
766 } else {
767 Span::from("Replying to ")
768 };
769 let spans = Spans(vec![prefix, user]);
770
771 spans.into()
772 })
773 },
774 };
775
776 // Determine the region to show each UI element.
745 let lines = state.tbox.has_lines(5).max(1) as u16; 777 let lines = state.tbox.has_lines(5).max(1) as u16;
746 let drawh = area.height; 778 let drawh = area.height;
747 let texth = lines.min(drawh).clamp(1, 5); 779 let texth = lines.min(drawh).clamp(1, 5);
748 let desch = if state.reply_to.is_some() { 780 let desch = if desc_spans.is_some() {
749 drawh.saturating_sub(texth).min(1) 781 drawh.saturating_sub(texth).min(1)
750 } else { 782 } else {
751 0 783 0
@@ -756,25 +788,7 @@ impl<'a> StatefulWidget for Chat<'a> {
756 let descarea = Rect::new(area.x, scrollarea.y + scrollh, area.width, desch); 788 let descarea = Rect::new(area.x, scrollarea.y + scrollh, area.width, desch);
757 let textarea = Rect::new(area.x, descarea.y + desch, area.width, texth); 789 let textarea = Rect::new(area.x, descarea.y + desch, area.width, texth);
758 790
759 let scrollback_focused = state.focus.is_scrollback() && self.focused; 791 // Render the message bar and any description for it.
760 let scrollback = Scrollback::new(self.store).focus(scrollback_focused);
761 scrollback.render(scrollarea, buf, &mut state.scrollback);
762
763 let desc_spans = match (&state.editing, &state.reply_to) {
764 (None, None) => None,
765 (Some(_), _) => Some(Spans::from("Editing message")),
766 (_, Some(_)) => {
767 state.reply_to.as_ref().and_then(|k| {
768 let room = self.store.application.rooms.get(state.id())?;
769 let msg = room.messages.get(k)?;
770 let user = self.store.application.settings.get_user_span(msg.sender.as_ref());
771 let spans = Spans(vec![Span::from("Replying to "), user]);
772
773 spans.into()
774 })
775 },
776 };
777
778 if let Some(desc_spans) = desc_spans { 792 if let Some(desc_spans) = desc_spans {
779 Paragraph::new(desc_spans).render(descarea, buf); 793 Paragraph::new(desc_spans).render(descarea, buf);
780 } 794 }
@@ -783,5 +797,10 @@ impl<'a> StatefulWidget for Chat<'a> {
783 797
784 let tbox = TextBox::new().prompt(prompt); 798 let tbox = TextBox::new().prompt(prompt);
785 tbox.render(textarea, buf, &mut state.tbox); 799 tbox.render(textarea, buf, &mut state.tbox);
800
801 // Render the message scrollback.
802 let scrollback_focused = state.focus.is_scrollback() && self.focused;
803 let scrollback = Scrollback::new(self.store).focus(scrollback_focused);
804 scrollback.render(scrollarea, buf, &mut state.scrollback);
786 } 805 }
787} 806}