From 5274187832a9859c684742265d60c1e204bc68d3 Mon Sep 17 00:00:00 2001 From: Giuliano Bellini s294739 Date: Thu, 21 Dec 2023 17:41:51 +0100 Subject: [PATCH] substituted radio components with buttons and fixed favorite notifications bug --- CHANGELOG.md | 2 + src/chart/types/chart_type.rs | 2 +- src/gui/components/mod.rs | 1 - src/gui/components/radio.rs | 131 ------------------- src/gui/pages/initial_page.rs | 1 + src/gui/pages/overview_page.rs | 36 +++-- src/gui/pages/settings_notifications_page.rs | 91 +++++++++---- src/networking/manage_packets.rs | 2 +- src/notifications/types/sound.rs | 23 +++- 9 files changed, 117 insertions(+), 172 deletions(-) delete mode 100644 src/gui/components/radio.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b1a5e4c..9c1a26db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,8 @@ All Sniffnet releases with the relative changes are documented in this file. - Updated Portuguese translation to v1.2 ([#398](https://github.com/GyulyVGC/sniffnet/pull/398)) - Cleaned code implementing the concept of first class theming ([#339](https://github.com/GyulyVGC/sniffnet/pull/339)) - Added documentation about Sniffnet installation on Nix and Tiny Core Linux (respectively [#394](https://github.com/GyulyVGC/sniffnet/pull/394) and [#341](https://github.com/GyulyVGC/sniffnet/pull/341)) +- General aesthetic improvements +- Fixed bug about not delivered favorite notifications in presence of old outgoing connections - Fixed bug causing the application's icon not to be visible in some Linux environments - Fixed a build failure on `powerpc64` ([#356](https://github.com/GyulyVGC/sniffnet/pull/356) — fixes [#353](https://github.com/GyulyVGC/sniffnet/issues/353)) - Fixed a typo in Russian translation ([#389](https://github.com/GyulyVGC/sniffnet/pull/389)) diff --git a/src/chart/types/chart_type.rs b/src/chart/types/chart_type.rs index adc6b6d7..b17773dd 100644 --- a/src/chart/types/chart_type.rs +++ b/src/chart/types/chart_type.rs @@ -11,7 +11,7 @@ pub enum ChartType { impl ChartType { pub(crate) const ALL: [ChartType; 2] = [ChartType::Bytes, ChartType::Packets]; - pub fn get_radio_label(&self, language: Language) -> &str { + pub fn get_label(&self, language: Language) -> &str { match self { ChartType::Packets => packets_translation(language), ChartType::Bytes => bytes_translation(language), diff --git a/src/gui/components/mod.rs b/src/gui/components/mod.rs index 609708aa..b1cac505 100644 --- a/src/gui/components/mod.rs +++ b/src/gui/components/mod.rs @@ -2,6 +2,5 @@ pub mod button; pub mod footer; pub mod header; pub mod modal; -pub mod radio; pub mod tab; pub mod types; diff --git a/src/gui/components/radio.rs b/src/gui/components/radio.rs deleted file mode 100644 index 2c842166..00000000 --- a/src/gui/components/radio.rs +++ /dev/null @@ -1,131 +0,0 @@ -use iced::widget::{Column, Radio, Row, Text}; -use iced::{Alignment, Font, Renderer}; - -use crate::gui::types::message::Message; -use crate::notifications::types::notifications::{ - BytesNotification, FavoriteNotification, Notification, PacketsNotification, -}; -use crate::notifications::types::sound::Sound; -use crate::translations::translations::sound_translation; -use crate::{ChartType, Language, StyleType}; - -pub fn sound_packets_threshold_radios( - packets_notification: PacketsNotification, - font: Font, - language: Language, -) -> Row<'static, Message, Renderer> { - let mut ret_val = Row::new() - .spacing(20) - .push(Text::new(format!("{}:", sound_translation(language))).font(font)); - for option in Sound::ALL { - ret_val = ret_val.push( - Radio::new( - option.get_radio_label(language), - option, - Some(packets_notification.sound), - |value| { - Message::UpdateNotificationSettings( - Notification::Packets(PacketsNotification { - sound: value, - ..packets_notification - }), - value.ne(&Sound::None), - ) - }, - ) - .spacing(7) - .font(font) - .size(15), - ); - } - ret_val -} - -pub fn sound_bytes_threshold_radios( - bytes_notification: BytesNotification, - font: Font, - language: Language, -) -> Row<'static, Message, Renderer> { - let mut ret_val = Row::new() - .spacing(20) - .push(Text::new(format!("{}:", sound_translation(language))).font(font)); - for option in Sound::ALL { - ret_val = ret_val.push( - Radio::new( - option.get_radio_label(language), - option, - Some(bytes_notification.sound), - |value| { - Message::UpdateNotificationSettings( - Notification::Bytes(BytesNotification { - sound: value, - ..bytes_notification - }), - value.ne(&Sound::None), - ) - }, - ) - .spacing(7) - .font(font) - .size(15), - ); - } - ret_val -} - -pub fn sound_favorite_radios( - favorite_notification: FavoriteNotification, - font: Font, - language: Language, -) -> Row<'static, Message, Renderer> { - let mut ret_val = Row::new() - .spacing(20) - .push(Text::new(format!("{}:", sound_translation(language))).font(font)); - for option in Sound::ALL { - ret_val = ret_val.push( - Radio::new( - option.get_radio_label(language), - option, - Some(favorite_notification.sound), - |value| { - Message::UpdateNotificationSettings( - Notification::Favorite(FavoriteNotification { - sound: value, - ..favorite_notification - }), - value.ne(&Sound::None), - ) - }, - ) - .spacing(7) - .font(font) - .size(15), - ); - } - ret_val -} - -pub fn chart_radios( - active: ChartType, - font: Font, - language: Language, -) -> Column<'static, Message, Renderer> { - let mut ret_val = Column::new() - .padding([0, 0, 0, 25]) - .spacing(5) - .align_items(Alignment::Start); - for option in ChartType::ALL { - ret_val = ret_val.push( - Radio::new( - option.get_radio_label(language), - option, - Some(active), - Message::ChartSelection, - ) - .spacing(7) - .font(font) - .size(15), - ); - } - ret_val -} diff --git a/src/gui/pages/initial_page.rs b/src/gui/pages/initial_page.rs index feae2458..95102efa 100644 --- a/src/gui/pages/initial_page.rs +++ b/src/gui/pages/initial_page.rs @@ -156,6 +156,7 @@ fn col_protocol_buttons( buttons_row = buttons_row.push( Button::new( Text::new(format!("{option} {check_symbol}")) + .width(Length::Fill) .shaping(Shaping::Advanced) .horizontal_alignment(Horizontal::Center) .vertical_alignment(Vertical::Center) diff --git a/src/gui/pages/overview_page.rs b/src/gui/pages/overview_page.rs index 59c6c37b..2fc718af 100644 --- a/src/gui/pages/overview_page.rs +++ b/src/gui/pages/overview_page.rs @@ -15,7 +15,6 @@ use iced::{Alignment, Font, Length, Renderer}; use crate::countries::country_utils::get_flag_tooltip; use crate::countries::flags_pictures::FLAGS_WIDTH_BIG; -use crate::gui::components::radio::chart_radios; use crate::gui::components::tab::get_pages_tabs; use crate::gui::styles::button::ButtonType; use crate::gui::styles::container::ContainerType; @@ -532,14 +531,33 @@ fn col_data_representation( font: Font, chart_type: ChartType, ) -> Column<'static, Message, Renderer> { - Column::new() - .width(Length::FillPortion(1)) - .push( - Text::new(format!("{}:", data_representation_translation(language))) - .style(TextType::Subtitle) - .font(font), - ) - .push(chart_radios(chart_type, font, language)) + let mut ret_val = Column::new().spacing(5).width(Length::FillPortion(1)).push( + Text::new(format!("{}:", data_representation_translation(language))) + .style(TextType::Subtitle) + .font(font), + ); + + for option in ChartType::ALL { + let is_active = chart_type.eq(&option); + ret_val = ret_val.push( + Button::new( + Text::new(option.get_label(language).to_owned()) + .width(Length::Fill) + .horizontal_alignment(Horizontal::Center) + .vertical_alignment(Vertical::Center) + .font(font), + ) + .width(Length::Fill) + .height(Length::Fixed(33.0)) + .style(if is_active { + ButtonType::BorderedRoundSelected + } else { + ButtonType::BorderedRound + }) + .on_press(Message::ChartSelection(option)), + ); + } + ret_val } fn col_bytes_packets( diff --git a/src/gui/pages/settings_notifications_page.rs b/src/gui/pages/settings_notifications_page.rs index cb256657..ede96aef 100644 --- a/src/gui/pages/settings_notifications_page.rs +++ b/src/gui/pages/settings_notifications_page.rs @@ -1,18 +1,16 @@ use iced::alignment::{Horizontal, Vertical}; use iced::widget::scrollable::Direction; -use iced::widget::Slider; use iced::widget::{ horizontal_space, vertical_space, Checkbox, Column, Container, Row, Scrollable, Text, TextInput, }; +use iced::widget::{Button, Slider}; use iced::Length::Fixed; use iced::{Alignment, Font, Length, Renderer}; use crate::gui::components::button::button_hide; -use crate::gui::components::radio::{ - sound_bytes_threshold_radios, sound_favorite_radios, sound_packets_threshold_radios, -}; use crate::gui::components::tab::get_settings_tabs; use crate::gui::pages::types::settings_page::SettingsPage; +use crate::gui::styles::button::ButtonType; use crate::gui::styles::container::ContainerType; use crate::gui::styles::scrollbar::ScrollbarType; use crate::gui::styles::style_constants::{ @@ -24,10 +22,12 @@ use crate::gui::types::message::Message; use crate::notifications::types::notifications::{ BytesNotification, FavoriteNotification, Notification, PacketsNotification, }; +use crate::notifications::types::sound::Sound; use crate::translations::translations::{ bytes_threshold_translation, favorite_notification_translation, notifications_title_translation, packets_threshold_translation, per_second_translation, - settings_translation, specify_multiples_translation, threshold_translation, volume_translation, + settings_translation, sound_translation, specify_multiples_translation, threshold_translation, + volume_translation, }; use crate::utils::types::icon::Icon; use crate::{Language, Sniffer, StyleType}; @@ -144,14 +144,13 @@ fn get_packets_notify( .push(horizontal_space(Fixed(50.0))) .push(Text::new(format!("{}: ", threshold_translation(language))).font(font)) .push(input_group_packets(packets_notification, font, language)); - let sound_row = - Row::new() - .push(horizontal_space(Fixed(50.0))) - .push(sound_packets_threshold_radios( - packets_notification, - font, - language, - )); + let sound_row = Row::new() + .push(horizontal_space(Fixed(50.0))) + .push(sound_buttons( + Notification::Packets(packets_notification), + font, + language, + )); ret_val = ret_val .push(vertical_space(Fixed(5.0))) .push(input_row) @@ -210,14 +209,13 @@ fn get_bytes_notify( .push(horizontal_space(Fixed(50.0))) .push(Text::new(format!("{}: ", threshold_translation(language))).font(font)) .push(input_group_bytes(bytes_notification, font, language)); - let sound_row = - Row::new() - .push(horizontal_space(Fixed(50.0))) - .push(sound_bytes_threshold_radios( - bytes_notification, - font, - language, - )); + let sound_row = Row::new() + .push(horizontal_space(Fixed(50.0))) + .push(sound_buttons( + Notification::Bytes(bytes_notification), + font, + language, + )); ret_val = ret_val .push(vertical_space(Fixed(5.0))) .push(input_row) @@ -258,7 +256,11 @@ fn get_favorite_notify( if favorite_notification.notify_on_favorite { let sound_row = Row::new() .push(horizontal_space(Fixed(50.0))) - .push(sound_favorite_radios(favorite_notification, font, language)); + .push(sound_buttons( + Notification::Favorite(favorite_notification), + font, + language, + )); ret_val = ret_val.push(vertical_space(Fixed(5.0))).push(sound_row); Column::new().padding(5).push( Container::new(ret_val) @@ -397,6 +399,51 @@ fn volume_slider( .align_y(Vertical::Center) } +pub fn sound_buttons( + notification: Notification, + font: Font, + language: Language, +) -> Row<'static, Message, Renderer> { + let current_sound = match notification { + Notification::Packets(n) => n.sound, + Notification::Bytes(n) => n.sound, + Notification::Favorite(n) => n.sound, + }; + + let mut ret_val = Row::new() + .align_items(Alignment::Center) + .spacing(5) + .push(Text::new(format!("{}:", sound_translation(language))).font(font)); + + for option in Sound::ALL { + let is_active = current_sound.eq(&option); + let message_value = match notification { + Notification::Packets(n) => { + Notification::Packets(PacketsNotification { sound: option, ..n }) + } + Notification::Bytes(n) => Notification::Bytes(BytesNotification { sound: option, ..n }), + Notification::Favorite(n) => { + Notification::Favorite(FavoriteNotification { sound: option, ..n }) + } + }; + ret_val = ret_val.push( + Button::new(option.get_text(font)) + .width(Length::Fixed(90.0)) + .height(Length::Fixed(30.0)) + .style(if is_active { + ButtonType::BorderedRoundSelected + } else { + ButtonType::BorderedRound + }) + .on_press(Message::UpdateNotificationSettings( + message_value, + option.ne(&Sound::None), + )), + ); + } + ret_val +} + pub fn settings_header( font: Font, font_headers: Font, diff --git a/src/networking/manage_packets.rs b/src/networking/manage_packets.rs index 6d0c300b..694ae554 100644 --- a/src/networking/manage_packets.rs +++ b/src/networking/manage_packets.rs @@ -233,7 +233,7 @@ pub fn modify_or_insert_in_map( if let Some(host_info) = info_traffic .addresses_resolved - .get(&get_address_to_lookup(key, traffic_direction)) + .get(&get_address_to_lookup(key, new_info.traffic_direction)) .cloned() { if info_traffic.favorite_hosts.contains(&host_info.1) { diff --git a/src/notifications/types/sound.rs b/src/notifications/types/sound.rs index ab42be53..a95e2076 100644 --- a/src/notifications/types/sound.rs +++ b/src/notifications/types/sound.rs @@ -1,3 +1,6 @@ +use iced::alignment::{Horizontal, Vertical}; +use iced::widget::Text; +use iced::{Font, Length, Renderer}; use std::fmt; use std::thread; @@ -5,8 +8,8 @@ use rodio::{Decoder, OutputStream, Sink}; use serde::{Deserialize, Serialize}; use crate::notifications::types::sound::Sound::{Gulp, Pop, Swhoosh}; -use crate::translations::translations::none_translation; -use crate::Language; +use crate::utils::types::icon::Icon; +use crate::StyleType; /// Enum representing the possible notification sounds. #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] @@ -39,13 +42,19 @@ impl Sound { } } - pub fn get_radio_label(self, language: Language) -> String { + pub fn get_text( + self, + font: Font, + ) -> iced::advanced::widget::Text<'static, Renderer> { match self { - Gulp => "Gulp".to_string(), - Pop => "Pop".to_string(), - Swhoosh => "Swhoosh".to_string(), - Sound::None => none_translation(language), + Sound::Gulp => Text::new("Gulp").font(font), + Sound::Pop => Text::new("Pop").font(font), + Sound::Swhoosh => Text::new("Swhoosh").font(font), + Sound::None => Icon::Forbidden.to_text(), } + .width(Length::Fill) + .horizontal_alignment(Horizontal::Center) + .vertical_alignment(Vertical::Center) } }