From 3bf154a4debf6b3257622e90d93cf463772fcd6a Mon Sep 17 00:00:00 2001 From: Giuliano Bellini s294739 Date: Mon, 14 Aug 2023 15:19:40 +0200 Subject: [PATCH] fix clippy lints --- src/gui/components/tab.rs | 98 +++++++++++++------- src/gui/pages/inspect_page.rs | 31 +++---- src/gui/pages/notifications_page.rs | 11 --- src/gui/pages/overview_page.rs | 11 --- src/gui/pages/settings_language_page.rs | 12 --- src/gui/pages/settings_notifications_page.rs | 12 --- src/gui/pages/settings_style_page.rs | 12 --- src/gui/pages/types/running_page.rs | 26 +++++- src/gui/pages/types/settings_page.rs | 26 +++++- src/gui/styles/button.rs | 5 +- src/gui/styles/container.rs | 2 +- src/gui/styles/text.rs | 2 +- src/report/get_report_entries.rs | 24 ++--- src/report/types/mod.rs | 1 + src/report/types/report_entry.rs | 14 +++ 15 files changed, 155 insertions(+), 132 deletions(-) create mode 100644 src/report/types/report_entry.rs diff --git a/src/gui/components/tab.rs b/src/gui/components/tab.rs index 12b63f4a..adc77c72 100644 --- a/src/gui/components/tab.rs +++ b/src/gui/components/tab.rs @@ -5,18 +5,14 @@ use iced::{alignment, Alignment, Font, Length, Renderer}; use crate::gui::pages::types::settings_page::SettingsPage; use crate::gui::styles::button::ButtonType; -use crate::gui::styles::style_constants::{FONT_SIZE_SUBTITLE, ICONS}; +use crate::gui::styles::style_constants::FONT_SIZE_SUBTITLE; use crate::gui::styles::text::TextType; use crate::gui::types::message::Message; use crate::{Language, RunningPage, StyleType}; pub fn get_settings_tabs( - labels: [SettingsPage; 3], - icons: &[&str], - actions: &[Message], active: SettingsPage, font: Font, - font_headers: Font, language: Language, ) -> Row<'static, Message, Renderer> { let mut tabs = Row::new() @@ -25,25 +21,14 @@ pub fn get_settings_tabs( .spacing(2) .padding([0, 3]); - for (i, label) in labels.iter().enumerate() { - let active = label.eq(&active); - tabs = tabs.push(new_tab( - (*label).get_tab_label(language).to_string(), - (*icons.get(i).unwrap()).to_string(), - actions.get(i).unwrap().clone(), - active, - font, - font_headers, - None, - )); + for page in &SettingsPage::ALL { + let active = page.eq(&active); + tabs = tabs.push(new_settings_tab(*page, active, language, font)); } tabs } pub fn get_pages_tabs( - labels: [RunningPage; 3], - icons: &[&str], - actions: &[Message], active: RunningPage, font: Font, font_headers: Font, @@ -56,18 +41,17 @@ pub fn get_pages_tabs( .spacing(2) .padding([0, 3]); - for (i, label) in labels.iter().enumerate() { - let active = label.eq(&active); - let unread = if label.eq(&RunningPage::Notifications) { + for page in &RunningPage::ALL { + let active = page.eq(&active); + let unread = if page.eq(&RunningPage::Notifications) { Some(unread_notifications) } else { None }; - tabs = tabs.push(new_tab( - (*label).get_tab_label(language).to_string(), - (*icons.get(i).unwrap()).to_string(), - actions.get(i).unwrap().clone(), + tabs = tabs.push(new_page_tab( + *page, active, + language, font, font_headers, unread, @@ -76,11 +60,10 @@ pub fn get_pages_tabs( tabs } -fn new_tab( - label: String, - icon: String, - action: Message, +fn new_page_tab( + page: RunningPage, active: bool, + language: Language, font: Font, font_headers: Font, unread: Option, @@ -89,8 +72,7 @@ fn new_tab( .align_items(Alignment::Center) .push(horizontal_space(Length::FillPortion(1))) .push( - Text::new(icon) - .font(ICONS) + page.icon() .size(15) .style(if active { TextType::Title @@ -100,8 +82,9 @@ fn new_tab( .horizontal_alignment(alignment::Horizontal::Center) .vertical_alignment(alignment::Vertical::Center), ) + .push(horizontal_space(10)) .push( - Text::new(label) + Text::new(page.get_tab_label(language).to_string()) .font(font) .size(FONT_SIZE_SUBTITLE) .style(if active { @@ -142,5 +125,52 @@ fn new_tab( } else { ButtonType::TabInactive }) - .on_press(action) + .on_press(page.action()) +} + +fn new_settings_tab( + page: SettingsPage, + active: bool, + language: Language, + font: Font, +) -> Button<'static, Message, Renderer> { + let content = Row::new() + .align_items(Alignment::Center) + .push(horizontal_space(Length::FillPortion(1))) + .push( + page.icon() + .size(15) + .style(if active { + TextType::Title + } else { + TextType::Standard + }) + .horizontal_alignment(alignment::Horizontal::Center) + .vertical_alignment(alignment::Vertical::Center), + ) + .push(horizontal_space(10)) + .push( + Text::new(page.get_tab_label(language).to_string()) + .font(font) + .size(FONT_SIZE_SUBTITLE) + .style(if active { + TextType::Title + } else { + TextType::Standard + }) + .horizontal_alignment(alignment::Horizontal::Center) + .vertical_alignment(alignment::Vertical::Center), + ) + .push(horizontal_space(Length::FillPortion(1))); + + button(content) + .height(Length::Fixed(if active { 35.0 } else { 30.0 })) + .padding(0) + .width(Length::FillPortion(1)) + .style(if active { + ButtonType::TabActive + } else { + ButtonType::TabInactive + }) + .on_press(page.action()) } diff --git a/src/gui/pages/inspect_page.rs b/src/gui/pages/inspect_page.rs index c1120303..625420e9 100644 --- a/src/gui/pages/inspect_page.rs +++ b/src/gui/pages/inspect_page.rs @@ -42,17 +42,6 @@ pub fn inspect_page(sniffer: &Sniffer) -> Container let mut tab_and_body = Column::new().height(Length::Fill); let tabs = get_pages_tabs( - [ - RunningPage::Overview, - RunningPage::Inspect, - RunningPage::Notifications, - ], - &["d ", "5 ", "7 "], - &[ - Message::ChangeRunningPage(RunningPage::Overview), - Message::TickInit, - Message::ChangeRunningPage(RunningPage::Notifications), - ], RunningPage::Inspect, font, font_headers, @@ -134,8 +123,8 @@ fn lazy_report(sniffer: &Sniffer) -> Row<'static, Message, Renderer> let mut scroll_report = Column::new(); let start_entry_num = (sniffer.page_number - 1) * 20 + 1; let end_entry_num = start_entry_num + search_results.len() - 1; - for (key, val, flag) in search_results { - let entry_text_type = if val.traffic_direction == TrafficDirection::Outgoing { + for report_entry in search_results { + let entry_text_type = if report_entry.val.traffic_direction == TrafficDirection::Outgoing { TextType::Outgoing } else { TextType::Incoming @@ -143,17 +132,23 @@ fn lazy_report(sniffer: &Sniffer) -> Row<'static, Message, Renderer> let entry_row = Row::new() .align_items(Alignment::Center) .push( - Text::new(format!(" {}{} ", key.print_gui(), val.print_gui())) - .style(entry_text_type) - .font(font), + Text::new(format!( + " {}{} ", + report_entry.key.print_gui(), + report_entry.val.print_gui() + )) + .style(entry_text_type) + .font(font), ) - .push(flag) + .push(report_entry.tooltip) .push(Text::new(" ")); scroll_report = scroll_report.push( button(entry_row) .padding(2) - .on_press(Message::ShowModal(MyModal::ConnectionDetails(val.index))) + .on_press(Message::ShowModal(MyModal::ConnectionDetails( + report_entry.val.index, + ))) .style(ButtonType::Neutral), ); } diff --git a/src/gui/pages/notifications_page.rs b/src/gui/pages/notifications_page.rs index f5e7caa6..ccf72400 100644 --- a/src/gui/pages/notifications_page.rs +++ b/src/gui/pages/notifications_page.rs @@ -41,17 +41,6 @@ pub fn notifications_page(sniffer: &Sniffer) -> Container Container { //observed > filtered > 0 || observed = filtered > 0 let tabs = get_pages_tabs( - [ - RunningPage::Overview, - RunningPage::Inspect, - RunningPage::Notifications, - ], - &["d ", "5 ", "7 "], - &[ - Message::TickInit, - Message::ChangeRunningPage(RunningPage::Inspect), - Message::ChangeRunningPage(RunningPage::Notifications), - ], RunningPage::Overview, font, font_headers, diff --git a/src/gui/pages/settings_language_page.rs b/src/gui/pages/settings_language_page.rs index 4efe4b8c..6626807d 100644 --- a/src/gui/pages/settings_language_page.rs +++ b/src/gui/pages/settings_language_page.rs @@ -40,20 +40,8 @@ pub fn settings_language_page(sniffer: &Sniffer) -> Container Container Container &str { match self { RunningPage::Overview => overview_translation(language), @@ -37,6 +48,19 @@ impl RunningPage { RunningPage::Notifications => RunningPage::Inspect, } } + + pub fn icon(self) -> iced::advanced::widget::Text<'static, Renderer> { + let char = match self { + RunningPage::Overview => "d", + RunningPage::Inspect => "5", + RunningPage::Notifications => "7", + }; + Text::new(char).font(ICONS) + } + + pub fn action(self) -> Message { + Message::ChangeRunningPage(self) + } } #[cfg(test)] diff --git a/src/gui/pages/types/settings_page.rs b/src/gui/pages/types/settings_page.rs index d199d840..6da42eb5 100644 --- a/src/gui/pages/types/settings_page.rs +++ b/src/gui/pages/types/settings_page.rs @@ -1,7 +1,12 @@ +use iced::widget::Text; +use iced::Renderer; + +use crate::gui::styles::style_constants::ICONS; +use crate::gui::types::message::Message; use crate::translations::translations::{ language_translation, notifications_translation, style_translation, }; -use crate::Language; +use crate::{Language, StyleType}; /// This enum defines the current running page. #[derive(PartialEq, Eq, Clone, Copy, Debug)] @@ -15,6 +20,12 @@ pub enum SettingsPage { } impl SettingsPage { + pub const ALL: [SettingsPage; 3] = [ + SettingsPage::Notifications, + SettingsPage::Appearance, + SettingsPage::Language, + ]; + pub fn get_tab_label(&self, language: Language) -> &str { match self { SettingsPage::Notifications => notifications_translation(language), @@ -38,6 +49,19 @@ impl SettingsPage { SettingsPage::Language => SettingsPage::Appearance, } } + + pub fn icon(self) -> iced::advanced::widget::Text<'static, Renderer> { + let char = match self { + SettingsPage::Notifications => "7", + SettingsPage::Appearance => "K", + SettingsPage::Language => "c", + }; + Text::new(char).font(ICONS) + } + + pub fn action(self) -> Message { + Message::OpenSettings(self) + } } #[cfg(test)] diff --git a/src/gui/styles/button.rs b/src/gui/styles/button.rs index f80bf7cb..96239a29 100644 --- a/src/gui/styles/button.rs +++ b/src/gui/styles/button.rs @@ -44,7 +44,6 @@ impl button::StyleSheet for StyleType { Background::Color(mix_colors(colors.primary, colors.buttons)) } ButtonType::Starred => Background::Color(get_starred_color(*self)), - ButtonType::Badge => Background::Color(colors.secondary), ButtonType::BorderedRound => Background::Color(Color { a: get_alpha_round_containers(*self), ..colors.buttons @@ -52,7 +51,9 @@ impl button::StyleSheet for StyleType { ButtonType::Neutral | ButtonType::NotStarred => { Background::Color(Color::TRANSPARENT) } - ButtonType::Gradient(GradientType::None) => Background::Color(colors.secondary), + ButtonType::Gradient(GradientType::None) | ButtonType::Badge => { + Background::Color(colors.secondary) + } ButtonType::Gradient(gradient_type) => Background::Gradient(get_gradient_buttons( &colors, *gradient_type, diff --git a/src/gui/styles/container.rs b/src/gui/styles/container.rs index f41f1807..87304ef6 100644 --- a/src/gui/styles/container.rs +++ b/src/gui/styles/container.rs @@ -54,7 +54,7 @@ impl iced::widget::container::StyleSheet for StyleType { get_gradient_headers(&colors, *gradient_type, self.is_nightly()), ), ContainerType::Modal => Background::Color(colors.primary), - _ => Background::Color(Color::TRANSPARENT), + ContainerType::Standard => Background::Color(Color::TRANSPARENT), }), border_radius: match style { ContainerType::BorderedRound => BORDER_ROUNDED_RADIUS.into(), diff --git a/src/gui/styles/text.rs b/src/gui/styles/text.rs index b094c95c..f040248e 100644 --- a/src/gui/styles/text.rs +++ b/src/gui/styles/text.rs @@ -81,6 +81,6 @@ pub fn highlight(style: StyleType, element: TextType) -> Color { TextType::Outgoing => colors.outgoing, TextType::Danger => Color::from_rgb(0.8, 0.15, 0.15), TextType::Sponsor => Color::from_rgb(1.0, 0.3, 0.5), - _ => colors.text_body, + TextType::Standard => colors.text_body, } } diff --git a/src/report/get_report_entries.rs b/src/report/get_report_entries.rs index 16fb48d4..3b2d416f 100644 --- a/src/report/get_report_entries.rs +++ b/src/report/get_report_entries.rs @@ -1,33 +1,21 @@ use std::cmp::{min, Ordering}; use std::sync::{Arc, Mutex}; -use iced::widget::Tooltip; -use iced::Renderer; - use crate::countries::country_utils::get_flag_tooltip; use crate::countries::flags_pictures::FLAGS_WIDTH_SMALL; use crate::gui::styles::style_constants::get_font; -use crate::gui::types::message::Message; use crate::networking::manage_packets::get_address_to_lookup; use crate::networking::types::address_port_pair::AddressPortPair; use crate::networking::types::data_info::DataInfo; use crate::networking::types::data_info_host::DataInfoHost; use crate::networking::types::host::Host; use crate::networking::types::info_address_port_pair::InfoAddressPortPair; -use crate::{AppProtocol, ChartType, InfoTraffic, ReportSortType, Sniffer, StyleType}; +use crate::report::types::report_entry::ReportEntry; +use crate::{AppProtocol, ChartType, InfoTraffic, ReportSortType, Sniffer}; /// Returns the elements which satisfy the search constraints and belong to the given page, /// and the total number of elements which satisfy the search constraints -pub fn get_searched_entries( - sniffer: &Sniffer, -) -> ( - Vec<( - AddressPortPair, - InfoAddressPortPair, - Tooltip<'static, Message, Renderer>, - )>, - usize, -) { +pub fn get_searched_entries(sniffer: &Sniffer) -> (Vec, usize) { let info_traffic_lock = sniffer.info_traffic.lock().unwrap(); let mut all_results: Vec<(&AddressPortPair, &InfoAddressPortPair)> = info_traffic_lock .map @@ -125,7 +113,11 @@ pub fn get_searched_entries( sniffer.language, get_font(sniffer.style), ); - (key_val.0.clone(), key_val.1.clone(), flag) + ReportEntry { + key: key_val.0.clone(), + val: key_val.1.clone(), + tooltip: flag, + } }) .collect(), all_results.len(), diff --git a/src/report/types/mod.rs b/src/report/types/mod.rs index cdf5293e..969bc7a7 100644 --- a/src/report/types/mod.rs +++ b/src/report/types/mod.rs @@ -1 +1,2 @@ +pub mod report_entry; pub mod report_sort_type; diff --git a/src/report/types/report_entry.rs b/src/report/types/report_entry.rs new file mode 100644 index 00000000..362aefe0 --- /dev/null +++ b/src/report/types/report_entry.rs @@ -0,0 +1,14 @@ +use iced::widget::Tooltip; +use iced::Renderer; + +use crate::gui::types::message::Message; +use crate::networking::types::address_port_pair::AddressPortPair; +use crate::networking::types::info_address_port_pair::InfoAddressPortPair; +use crate::StyleType; + +/// Struct embedding all the info needed to build a row of the connections report +pub struct ReportEntry { + pub key: AddressPortPair, + pub val: InfoAddressPortPair, + pub tooltip: Tooltip<'static, Message, Renderer>, +}