diff options
author | 2025-03-21 17:10:57 +0000 | |
---|---|---|
committer | 2025-03-21 17:10:57 +0000 | |
commit | e5d9f9becd3c6a732cf42f51fc48ac01f6650df8 (patch) | |
tree | 177973e05358007bdf34c0b15a919a479974d80c | |
parent | 96938ae1dc648cea788660ed92f1e61d954aeb1b (diff) | |
download | macaw-e5d9f9becd3c6a732cf42f51fc48ac01f6650df8.tar.gz macaw-e5d9f9becd3c6a732cf42f51fc48ac01f6650df8.tar.bz2 macaw-e5d9f9becd3c6a732cf42f51fc48ac01f6650df8.zip |
feat: send message on enter (configurable)
-rw-r--r-- | src/main.rs | 44 | ||||
-rw-r--r-- | src/message_view.rs | 19 |
2 files changed, 60 insertions, 3 deletions
diff --git a/src/main.rs b/src/main.rs index fcd0281..08dd833 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,7 @@ use std::sync::Arc; use chrono::Local; use iced::alignment::Horizontal::Right; use iced::futures::{SinkExt, Stream, StreamExt}; +use iced::keyboard::{on_key_press, on_key_release, Key, Modifiers}; use iced::theme::palette::{ Background, Danger, Extended, Pair, Primary, Secondary, Success, Warning, }; @@ -301,6 +302,7 @@ async fn main() -> iced::Result { } }; iced::application("Macaw", Macaw::update, Macaw::view) + .subscription(subscription) .theme(Macaw::theme) .run_with(|| { ( @@ -329,8 +331,36 @@ async fn main() -> iced::Result { } } +fn subscription(state: &Macaw) -> Subscription<Message> { + Subscription::batch([press_subscription(state), release_subscription(state)]) +} + +fn press_subscription(_state: &Macaw) -> Subscription<Message> { + on_key_press(handle_key_press) +} + +fn handle_key_press(key: Key, r#mod: Modifiers) -> Option<Message> { + match key { + Key::Named(iced::keyboard::key::Named::Shift) => Some(Message::ShiftPressed), + _ => None, + } +} + +fn release_subscription(_state: &Macaw) -> Subscription<Message> { + on_key_release(handle_key_release) +} + +fn handle_key_release(key: Key, r#mod: Modifiers) -> Option<Message> { + match key { + Key::Named(iced::keyboard::key::Named::Shift) => Some(Message::ShiftReleased), + _ => None, + } +} + #[derive(Debug, Clone)] pub enum Message { + ShiftPressed, + ShiftReleased, LoginModal(login_modal::Message), ClientCreated(Client), Luz(UpdateMessage), @@ -722,6 +752,20 @@ impl Macaw { Task::none() } } + Message::ShiftPressed => { + info!("shift pressed"); + if let Some(open_chat) = &mut self.open_chat { + open_chat.shift_pressed = true; + } + Task::none() + } + Message::ShiftReleased => { + info!("shift released"); + if let Some(open_chat) = &mut self.open_chat { + open_chat.shift_pressed = false; + } + Task::none() + } } } diff --git a/src/message_view.rs b/src/message_view.rs index aefa1cb..16e8ac1 100644 --- a/src/message_view.rs +++ b/src/message_view.rs @@ -25,6 +25,7 @@ pub struct MessageView { pub jid: JID, pub message_history: IndexMap<Uuid, ChatMessage>, pub new_message: Content, + pub shift_pressed: bool, } #[derive(Serialize, Deserialize, Clone)] @@ -62,6 +63,8 @@ impl MessageView { // TODO: save draft (as part of chat struct?) new_message: Content::new(), config: config.message_view_config.clone(), + // TODO: have centralised modifier state location? + shift_pressed: false, } } @@ -92,9 +95,19 @@ impl MessageView { match &a { text_editor::Action::Edit(edit) => match edit { text_editor::Edit::Enter => { - let message = self.new_message.text(); - self.new_message = Content::new(); - return Action::SendMessage(message); + if self.config.send_on_enter { + if !self.shift_pressed { + let message = self.new_message.text(); + self.new_message = Content::new(); + return Action::SendMessage(message); + } + } else { + if self.shift_pressed { + let message = self.new_message.text(); + self.new_message = Content::new(); + return Action::SendMessage(message); + } + } } _ => {} }, |