Chop up a couple functions into smaller functions
This commit is contained in:
parent
1eb8b971f5
commit
3c05b23b90
1 changed files with 146 additions and 151 deletions
|
@ -7,7 +7,7 @@ use chrono::{DateTime, Local};
|
|||
use iced::{
|
||||
alignment::Horizontal,
|
||||
widget::{column, container, scrollable, text, Container},
|
||||
Background, Color, Length,
|
||||
Background, Color, Element, Length,
|
||||
};
|
||||
use irc::proto::BatchSubCommand;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
@ -297,6 +297,23 @@ impl<'a> MessageLog {
|
|||
let channel = self.get_mut(Some(channel_name));
|
||||
|
||||
if channel.is_multiline {
|
||||
Self::handle_multiline_message(channel, nickname, message, message_id);
|
||||
return;
|
||||
}
|
||||
|
||||
Self::handle_singleline_message(channel, nickname, message, message_id, timestamp);
|
||||
|
||||
if !is_active {
|
||||
Self::update_unread_counts(current_nickname, channel, message);
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_multiline_message(
|
||||
channel: &mut Channel,
|
||||
nickname: &str,
|
||||
message: &str,
|
||||
message_id: Option<&str>,
|
||||
) {
|
||||
channel.multiline_nickname = Some(nickname.into());
|
||||
channel
|
||||
.multiline_privmsgs
|
||||
|
@ -307,10 +324,15 @@ impl<'a> MessageLog {
|
|||
if let Some(message_id) = message_id {
|
||||
channel.multiline_message_id = Some(message_id.into());
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
fn handle_singleline_message(
|
||||
channel: &mut Channel,
|
||||
nickname: &str,
|
||||
message: &str,
|
||||
message_id: Option<&str>,
|
||||
timestamp: &DateTime<Local>,
|
||||
) {
|
||||
if message_id.is_none() || channel.message_ids.insert(message_id.unwrap().into()) {
|
||||
channel.messages.push(IrcMessage {
|
||||
detail: MessageDetail::Privmsg {
|
||||
|
@ -321,15 +343,15 @@ impl<'a> MessageLog {
|
|||
timestamp: *timestamp,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if !is_active {
|
||||
fn update_unread_counts(current_nickname: &str, channel: &mut Channel, message: &str) {
|
||||
if util::contains_word(current_nickname, message) {
|
||||
channel.unread_highlights += 1;
|
||||
} else {
|
||||
channel.unread_messages += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn on_other(&mut self, message: &str) {
|
||||
self.get_mut(None).messages.push(IrcMessage {
|
||||
|
@ -351,28 +373,6 @@ impl<'a> MessageLog {
|
|||
|
||||
pub fn view(&self, current_nickname: &str) -> Container<'_, crate::ui_message::UiMessage> {
|
||||
let lighter_grey = Color::new(0.93, 0.94, 0.95, 1.0);
|
||||
let dark_grey = Color::new(0.58, 0.65, 0.65, 1.0);
|
||||
let lighter_green = Color::new(0.94, 0.99, 0.87, 1.0);
|
||||
let dark_red = Color::new(0.75, 0.22, 0.17, 1.0);
|
||||
|
||||
let event_appearance = container::Appearance {
|
||||
background: Some(Background::Color(dark_grey)),
|
||||
border_radius: 8.0.into(),
|
||||
text_color: Some(Color::WHITE),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let message_appearance = container::Appearance {
|
||||
background: Some(Background::Color(Color::WHITE)),
|
||||
border_radius: 8.0.into(),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let own_message_appearance = container::Appearance {
|
||||
background: Some(Background::Color(lighter_green)),
|
||||
border_radius: 8.0.into(),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let channel = self.get(&self.active_channel).unwrap();
|
||||
|
||||
|
@ -386,84 +386,85 @@ impl<'a> MessageLog {
|
|||
"{} members - {}",
|
||||
channel.names.len(),
|
||||
channel.topic.as_deref().unwrap_or_default()
|
||||
))
|
||||
)),
|
||||
]);
|
||||
|
||||
let messages = channel
|
||||
.messages
|
||||
.iter()
|
||||
.flat_map(|irc_message| -> Option<iced::Element<_>> {
|
||||
.flat_map(|irc_message| Self::map_irc_message(current_nickname, irc_message))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
container(column![
|
||||
header,
|
||||
container(
|
||||
scrollable(column(messages))
|
||||
.height(Length::Fill)
|
||||
.width(Length::Fill),
|
||||
)
|
||||
.height(Length::Fill)
|
||||
.width(Length::Fill)
|
||||
.style(move |_: &_| Self::container_appearance(lighter_grey, 0.0)),
|
||||
])
|
||||
.height(Length::Fill)
|
||||
.width(Length::Fill)
|
||||
}
|
||||
|
||||
fn container_appearance(background_color: Color, border_radius: f32) -> container::Appearance {
|
||||
container::Appearance {
|
||||
background: Some(Background::Color(background_color)),
|
||||
border_radius: border_radius.into(),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
fn map_irc_message(
|
||||
current_nickname: &str,
|
||||
irc_message: &IrcMessage,
|
||||
) -> Option<iced::Element<'a, crate::ui_message::UiMessage>> {
|
||||
let dark_grey = Color::new(0.58, 0.65, 0.65, 1.0);
|
||||
let dark_red = Color::new(0.75, 0.22, 0.17, 1.0);
|
||||
let lighter_green = Color::new(0.94, 0.99, 0.87, 1.0);
|
||||
|
||||
let timestamp = irc_message.timestamp.format("%H:%M:%S");
|
||||
|
||||
match &irc_message.detail {
|
||||
MessageDetail::Join { nickname } => Some(
|
||||
container(
|
||||
container(
|
||||
text(format!("{nickname} joined the channel"))
|
||||
.horizontal_alignment(Horizontal::Center),
|
||||
)
|
||||
.style(move |_: &_| event_appearance)
|
||||
.padding([3, 10]),
|
||||
)
|
||||
.width(Length::Fill)
|
||||
.center_x()
|
||||
.padding([3, 0])
|
||||
.into(),
|
||||
),
|
||||
MessageDetail::Join { nickname } => Some(Self::event_container(format!(
|
||||
"{nickname} joined the channel"
|
||||
))),
|
||||
MessageDetail::Part { nickname, reason } => {
|
||||
let reason = match reason {
|
||||
Some(reason) => format!(" ({reason})"),
|
||||
None => String::new(),
|
||||
};
|
||||
Some(
|
||||
container(
|
||||
container(text(format!("{nickname} left the channel{reason}")))
|
||||
.style(move |_: &_| event_appearance)
|
||||
.padding([3, 10]),
|
||||
)
|
||||
.width(Length::Fill)
|
||||
.center_x()
|
||||
.padding([3, 0])
|
||||
.into(),
|
||||
)
|
||||
let reason_text = reason
|
||||
.as_ref()
|
||||
.map(|r| format!(" ({})", r))
|
||||
.unwrap_or_default();
|
||||
Some(Self::event_container(format!(
|
||||
"{nickname} left the channel{reason_text}"
|
||||
)))
|
||||
}
|
||||
MessageDetail::Nick { old, new } => Some(
|
||||
container(
|
||||
container(text(format!("{old} changed their nickname to {new}")))
|
||||
.style(move |_: &_| event_appearance)
|
||||
.padding([3, 10]),
|
||||
)
|
||||
.width(Length::Fill)
|
||||
.center_x()
|
||||
.padding([3, 0])
|
||||
.into(),
|
||||
),
|
||||
MessageDetail::Nick { old, new } => Some(Self::event_container(format!(
|
||||
"{old} changed their nickname to {new}"
|
||||
))),
|
||||
MessageDetail::Quit { nickname, reason } => {
|
||||
let reason = match reason {
|
||||
Some(reason) => format!(" ({reason})"),
|
||||
None => String::new(),
|
||||
};
|
||||
|
||||
Some(
|
||||
container(
|
||||
container(text(format!("{nickname} quit the server{reason}")))
|
||||
.style(move |_: &_| event_appearance)
|
||||
.padding([3, 10]),
|
||||
)
|
||||
.width(Length::Fill)
|
||||
.center_x()
|
||||
.padding([3, 0])
|
||||
.into(),
|
||||
)
|
||||
let reason_text = reason
|
||||
.as_ref()
|
||||
.map(|r| format!(" ({})", r))
|
||||
.unwrap_or_default();
|
||||
Some(Self::event_container(format!(
|
||||
"{nickname} quit the server{reason_text}"
|
||||
)))
|
||||
}
|
||||
MessageDetail::Privmsg { nickname, message } => {
|
||||
let is_self = nickname == current_nickname;
|
||||
let text_color = if is_self { dark_red } else { Color::BLACK };
|
||||
let message_appearance = Self::container_appearance(Color::WHITE, 8.0);
|
||||
let own_message_appearance = Self::container_appearance(lighter_green, 8.0);
|
||||
let mut elements: Vec<Element<'_, _, _>> =
|
||||
vec![text(message).style(text_color).into()];
|
||||
|
||||
let mut elements = Vec::new();
|
||||
if !is_self {
|
||||
elements.push(text(nickname).style(dark_red).into())
|
||||
elements.insert(0, text(nickname).style(dark_red).into());
|
||||
}
|
||||
elements.push(text(message).into());
|
||||
|
||||
elements.push(
|
||||
text(timestamp)
|
||||
.style(dark_grey)
|
||||
|
@ -497,25 +498,19 @@ impl<'a> MessageLog {
|
|||
}
|
||||
MessageDetail::Other { message } => Some(text(message).into()),
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
}
|
||||
|
||||
container(column![
|
||||
header,
|
||||
fn event_container(content: String) -> iced::Element<'a, crate::ui_message::UiMessage> {
|
||||
let dark_grey = Color::new(0.58, 0.65, 0.65, 1.0);
|
||||
container(
|
||||
scrollable(column(messages))
|
||||
.height(Length::Fill)
|
||||
.width(Length::Fill),
|
||||
container(text(content).style(Color::WHITE).horizontal_alignment(Horizontal::Center))
|
||||
.style(move |_: &_| Self::container_appearance(dark_grey, 8.0))
|
||||
.padding([3, 10]),
|
||||
)
|
||||
.height(Length::Fill)
|
||||
.width(Length::Fill)
|
||||
.style(move |_: &_| container::Appearance {
|
||||
background: Some(Background::Color(lighter_grey)),
|
||||
..Default::default()
|
||||
})
|
||||
])
|
||||
.height(Length::Fill)
|
||||
.width(Length::Fill)
|
||||
.center_x()
|
||||
.padding([3, 0])
|
||||
.into()
|
||||
}
|
||||
|
||||
pub fn on_topic(&mut self, channel: &str, topic: &str) {
|
||||
|
|
Loading…
Reference in a new issue