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 {
103103#[derive(Clone, Debug, Eq, PartialEq)]
104104pub enum MessageAction {
105105 /// 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),
107109
108110 /// Download an attachment to the given path.
109111 ///
@@ -446,8 +448,12 @@ impl RoomInfo {
446448 }
447449 }
448450
451 pub fn get_message_key(&self, event_id: &EventId) -> Option<&MessageKey> {
452 self.keys.get(event_id)?.to_message_key()
453 }
454
449455 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)?)
451457 }
452458
453459 pub fn insert_reaction(&mut self, react: ReactionEvent) {
@@ -489,10 +495,10 @@ impl RoomInfo {
489495
490496 match &mut msg.event {
491497 MessageEvent::Original(orig) => {
492 orig.content = *new_content;
498 orig.content.msgtype = new_content.msgtype;
493499 },
494500 MessageEvent::Local(_, content) => {
495 *content = new_content;
501 content.msgtype = new_content.msgtype;
496502 },
497503 MessageEvent::Redacted(_) |
498504 MessageEvent::EncryptedOriginal(_) |
src/commands.rs +1 −1
@@ -177,7 +177,7 @@ fn iamb_cancel(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
177177 return Result::Err(CommandError::InvalidArgument);
178178 }
179179
180 let mact = IambAction::from(MessageAction::Cancel);
180 let mact = IambAction::from(MessageAction::Cancel(desc.bang));
181181 let step = CommandStep::Continue(mact.into(), ctx.context.take());
182182
183183 return Ok(step);
src/windows/room/chat.rs +41 −22
@@ -165,11 +165,20 @@ impl ChatState {
165165 .ok_or(IambError::NoSelectedMessage)?;
166166
167167 match act {
168 MessageAction::Cancel => {
168 MessageAction::Cancel(skip_confirm) => {
169169 self.reply_to = None;
170170 self.editing = None;
171171
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))
173182 },
174183 MessageAction::Download(filename, flags) => {
175184 if let MessageEvent::Original(ev) = &msg.event {
@@ -286,6 +295,7 @@ impl ChatState {
286295 };
287296
288297 self.tbox.set_text(text);
298 self.reply_to = msg.reply_to().and_then(|id| info.get_message_key(&id)).cloned();
289299 self.editing = self.scrollback.get_key(info);
290300 self.focus = RoomFocus::MessageBar;
291301
@@ -742,10 +752,32 @@ impl<'a> StatefulWidget for Chat<'a> {
742752 type State = ChatState;
743753
744754 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.
745777 let lines = state.tbox.has_lines(5).max(1) as u16;
746778 let drawh = area.height;
747779 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() {
749781 drawh.saturating_sub(texth).min(1)
750782 } else {
751783 0
@@ -756,25 +788,7 @@ impl<'a> StatefulWidget for Chat<'a> {
756788 let descarea = Rect::new(area.x, scrollarea.y + scrollh, area.width, desch);
757789 let textarea = Rect::new(area.x, descarea.y + desch, area.width, texth);
758790
759 let scrollback_focused = state.focus.is_scrollback() && self.focused;
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
791 // Render the message bar and any description for it.
778792 if let Some(desc_spans) = desc_spans {
779793 Paragraph::new(desc_spans).render(descarea, buf);
780794 }
@@ -783,5 +797,10 @@ impl<'a> StatefulWidget for Chat<'a> {
783797
784798 let tbox = TextBox::new().prompt(prompt);
785799 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);
786805 }
787806}