diff options
author | 2025-03-20 23:02:13 +0000 | |
---|---|---|
committer | 2025-03-20 23:02:13 +0000 | |
commit | 96938ae1dc648cea788660ed92f1e61d954aeb1b (patch) | |
tree | 2fea3a8fab54b28a45b1ecd59844a216077231e7 | |
parent | 8595b2e23b2f719e83b312a28f9dc72b9b01c1b3 (diff) | |
download | macaw-96938ae1dc648cea788660ed92f1e61d954aeb1b.tar.gz macaw-96938ae1dc648cea788660ed92f1e61d954aeb1b.tar.bz2 macaw-96938ae1dc648cea788660ed92f1e61d954aeb1b.zip |
fix: spacing
-rw-r--r-- | src/main.rs | 35 | ||||
-rw-r--r-- | src/message_view.rs | 81 |
2 files changed, 78 insertions, 38 deletions
diff --git a/src/main.rs b/src/main.rs index bc60f0a..fcd0281 100644 --- a/src/main.rs +++ b/src/main.rs @@ -46,6 +46,7 @@ pub struct Config { auto_connect: bool, storage_dir: Option<String>, dburl: Option<String>, + message_view_config: message_view::Config, } impl Default for Config { @@ -54,6 +55,7 @@ impl Default for Config { auto_connect: true, storage_dir: None, dburl: None, + message_view_config: message_view::Config::default(), } } } @@ -338,7 +340,6 @@ pub enum Message { GotChats(Vec<Chat>), GotMessageHistory(Chat, IndexMap<Uuid, ChatMessage>), ToggleChat(JID), - MessageCompose(String), SendMessage(JID, String), Error(Error), MessageView(message_view::Message), @@ -548,7 +549,7 @@ impl Macaw { } None => {} } - self.open_chat = Some(MessageView::new(jid.clone())); + self.open_chat = Some(MessageView::new(jid.clone(), &self.config)); let jid1 = jid.clone(); match &self.client { Account::LoggedIn(client) => { @@ -691,12 +692,6 @@ impl Macaw { } Task::none() } - Message::MessageCompose(m) => { - if let Some(open_chat) = &mut self.open_chat { - open_chat.new_message = m; - } - Task::none() - } Message::SendMessage(jid, body) => { let client = match &self.client { Account::LoggedIn(client) => client.clone(), @@ -755,18 +750,20 @@ impl Macaw { }; let connected = self.client.is_connected(); - let account_view = row![ + let account_view = container(row![ text(client_jid), - toggler(connected) - .on_toggle(|connect| { - if connect { - Message::Connect - } else { - Message::Disconnect - } - }) - .label(connection_status), - ]; + horizontal_space(), + text(connection_status), + horizontal_space().width(8), + toggler(connected).on_toggle(|connect| { + if connect { + Message::Connect + } else { + Message::Disconnect + } + }) + ]) + .padding(8); // TODO: config width/resizing let sidebar = column![chats_list, account_view].height(Fill).width(300); diff --git a/src/message_view.rs b/src/message_view.rs index 4709dd6..aefa1cb 100644 --- a/src/message_view.rs +++ b/src/message_view.rs @@ -6,7 +6,10 @@ use iced::{ border::Radius, color, theme::Palette, - widget::{button, column, container, row, scrollable, text, text_input, Column}, + widget::{ + button, column, container, horizontal_space, row, scrollable, text, text_editor, + text_editor::Content, text_input, Column, + }, Border, Color, Element, Length::{Fill, Shrink}, Theme, @@ -14,19 +17,34 @@ use iced::{ use indexmap::IndexMap; use jid::JID; use luz::chat::Message as ChatMessage; +use serde::{Deserialize, Serialize}; use uuid::Uuid; pub struct MessageView { + pub config: Config, pub jid: JID, pub message_history: IndexMap<Uuid, ChatMessage>, - pub new_message: String, + pub new_message: Content, +} + +#[derive(Serialize, Deserialize, Clone)] +pub struct Config { + pub send_on_enter: bool, +} + +impl Default for Config { + fn default() -> Self { + Self { + send_on_enter: true, + } + } } #[derive(Debug, Clone)] pub enum Message { MessageHistory(Vec<ChatMessage>), Message(ChatMessage), - MessageCompose(String), + MessageCompose(text_editor::Action), SendMessage(String), } @@ -36,15 +54,17 @@ pub enum Action { } impl MessageView { - pub fn new(jid: JID) -> Self { + pub fn new(jid: JID, config: &super::Config) -> Self { Self { jid, // TODO: save position in message history message_history: IndexMap::new(), // TODO: save draft (as part of chat struct?) - new_message: String::new(), + new_message: Content::new(), + config: config.message_view_config.clone(), } } + pub fn update(&mut self, message: Message) -> Action { match message { Message::MessageHistory(messages) => { @@ -68,18 +88,30 @@ impl MessageView { } Action::None } - Message::MessageCompose(s) => { - self.new_message = s; + Message::MessageCompose(a) => { + 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); + } + _ => {} + }, + _ => {} + } + self.new_message.perform(a); Action::None } Message::SendMessage(m) => { - self.new_message = String::new(); + self.new_message = Content::new(); Action::SendMessage(m) } } } + pub fn view(&self) -> Element<Message> { - let mut messages_view = column![].spacing(8); + let mut messages_view = column![].spacing(8).padding(8); let mut latest_date = NaiveDate::MIN; for (_id, message) in &self.message_history { let message_date = message.timestamp.naive_local().date(); @@ -89,10 +121,15 @@ impl MessageView { } messages_view = messages_view.push(self.message(message)); } + let text_editor = text_editor(&self.new_message) + .placeholder("new message") + .on_action(Message::MessageCompose) + .wrapping(text::Wrapping::WordOrGlyph); let message_send_input = row![ - text_input("new message", &self.new_message).on_input(Message::MessageCompose), - button("send").on_press(Message::SendMessage(self.new_message.clone())) - ]; + text_editor, + button("send").on_press(Message::SendMessage(self.new_message.text())) + ] + .padding(8); column![ scrollable(messages_view) .height(Fill) @@ -112,9 +149,8 @@ impl MessageView { container( container( column![ - text(message.body.body.as_str()), - container(text(timestamp).wrapping(text::Wrapping::None).size(12)) - .align_right(Fill) + text(message.body.body.as_str()).wrapping(text::Wrapping::WordOrGlyph), + container(text(timestamp).wrapping(text::Wrapping::None).size(12)) // .align_right(Fill) ] .width(Shrink) .max_width(500), @@ -126,7 +162,8 @@ impl MessageView { .background(palette.primary.weak.color) .border(Border { color: Color::BLACK, - width: 4., + width: 0., + // width: 4., radius: Radius::new(16), }) }), @@ -137,9 +174,14 @@ impl MessageView { let element: Element<Message> = container( container( column![ - text(message.body.body.as_str()), + text(message.body.body.as_str()).wrapping(text::Wrapping::WordOrGlyph), container(text(timestamp).wrapping(text::Wrapping::None).size(12)) - .align_right(Fill) + .align_right(Fill) // row![ + // // horizontal_space(), + // // horizontal_space(), + // text(timestamp).wrapping(text::Wrapping::None).size(12) + // ] // container(text(timestamp).wrapping(text::Wrapping::None).size(12)) + // .align_right(Fill) ] .width(Shrink) .max_width(500), @@ -151,7 +193,8 @@ impl MessageView { .background(palette.primary.base.color) .border(Border { color: Color::BLACK, - width: 4., + width: 0., + // width: 4., radius: Radius::new(16), }) }), |