logic for sending messages

This commit is contained in:
2025-08-28 11:40:02 +02:00
parent 45651d9a2b
commit cc318dcb09
2 changed files with 30 additions and 46 deletions

View File

@@ -1,5 +1,5 @@
use iced::{ use iced::{
Border, Background, Border, Color,
widget::{Button, container}, widget::{Button, container},
}; };
use iced::{Length, Shadow}; use iced::{Length, Shadow};
@@ -46,29 +46,26 @@ where
.on_press(press_variant(id)) .on_press(press_variant(id))
} }
// Utility to round a button border while keeping other default styling. pub fn round_button<'a, Message>(
// Note: Applying a new .style(...) replaces any previous style closure. button: iced::widget::Button<'a, Message>,
// Use this helper early (or integrate its logic) if you also need custom colors. radius: f32,
pub fn round_button<'a, Message>(button: Button<'a, Message>, radius: f32) -> Button<'a, Message> ) -> iced::widget::Button<'a, Message>
where where
Message: 'a, Message: 'a,
{ {
button.style(move |_theme, status| { button.style(move |_theme, status| {
// Start from default style for this status
let mut style = iced::widget::button::Style::default(); let mut style = iced::widget::button::Style::default();
let green = Color::from_rgb8(0, 255, 128);
// Simple example tweak: lighter shadow when pressed if status == iced::widget::button::Status::Pressed {
match status {
iced::widget::button::Status::Pressed => {
style.shadow.offset = iced::Vector { x: 1.0, y: 1.0 }; style.shadow.offset = iced::Vector { x: 1.0, y: 1.0 };
} }
_ => {}
}
// Override only the radius, keep other border attributes style.background = Some(Background::Color(green));
style.border = Border { style.border = Border {
radius: radius.into(), radius: radius.into(),
..style.border color: green,
width: 4.,
}; };
style style

View File

@@ -1,11 +1,12 @@
use iced::Length::{self, Fill, FillPortion}; use iced::Length::{self, Fill, FillPortion};
use iced::widget::text_editor; use iced::widget::container;
use iced::widget::{Container, container}; use iced::widget::text_editor::{self};
use iced::widget::text_editor::{Action, Content, Edit};
use iced::{ use iced::{
Background, Color, Element, Task, Theme, Background, Color, Element, Task, Theme,
widget::{Image, column, container::Style, row, text}, widget::{Image, column, container::Style, row, text},
}; };
use iced::{Border, Font}; use iced::{Border, Error, Font};
mod buttons; mod buttons;
mod channels; mod channels;
@@ -13,7 +14,9 @@ mod servers;
use servers::{User, get_servers_buttons}; use servers::{User, get_servers_buttons};
use crate::channels::get_channels_buttons; use channels::get_channels_buttons;
use buttons::round_button;
fn main() -> Result<(), iced::Error> { fn main() -> Result<(), iced::Error> {
iced::application(Liscord::title, Liscord::update, Liscord::view) iced::application(Liscord::title, Liscord::update, Liscord::view)
@@ -67,10 +70,14 @@ impl Liscord {
self.selected_channel = Some(channel_id); self.selected_channel = Some(channel_id);
// async tasks // async tasks
} }
Event::MessageInput(Action::Edit(Edit::Enter)) => {
let _ = self.update(Event::SendMessage);
}
Event::MessageInput(action) => { Event::MessageInput(action) => {
// println!("{action:?}");
self.message_editor.perform(action); self.message_editor.perform(action);
} }
Event::SendMessage => println!("Message Sent !"), Event::SendMessage => self.send_message().expect("Message failed"),
} }
Task::none() Task::none()
} }
@@ -182,6 +189,12 @@ impl Liscord {
.center() .center()
.into() .into()
} }
fn send_message(&mut self) -> Result<(), Error> {
println!("Sending message: {}", self.message_editor.text());
self.message_editor = Content::new();
Ok(())
}
} }
fn icon<'a>(codepoint: char) -> Element<'a, Event> { fn icon<'a>(codepoint: char) -> Element<'a, Event> {
@@ -193,29 +206,3 @@ fn icon<'a>(codepoint: char) -> Element<'a, Event> {
fn send_icon<'a>() -> Element<'a, Event> { fn send_icon<'a>() -> Element<'a, Event> {
icon('\u{F1D8}') icon('\u{F1D8}')
} }
pub fn round_button<'a, Message>(
button: iced::widget::Button<'a, Message>,
radius: f32,
) -> iced::widget::Button<'a, Message>
where
Message: 'a,
{
button.style(move |_theme, status| {
let mut style = iced::widget::button::Style::default();
let green = Color::from_rgb8(0, 255, 128);
if status == iced::widget::button::Status::Pressed {
style.shadow.offset = iced::Vector { x: 1.0, y: 1.0 };
}
style.background = Some(Background::Color(green));
style.border = Border {
radius: radius.into(),
color: green,
width: 4.,
};
style
})
}