diff options
Diffstat (limited to 'src/message_view.rs')
-rw-r--r-- | src/message_view.rs | 81 |
1 files changed, 62 insertions, 19 deletions
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), }) }), |