fix clippy lints

This commit is contained in:
Giuliano Bellini s294739 2023-08-14 15:19:40 +02:00
parent ffd46afc77
commit 3bf154a4de
15 changed files with 155 additions and 132 deletions

View file

@ -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<StyleType>> {
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<usize>,
@ -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<StyleType>> {
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())
}

View file

@ -42,17 +42,6 @@ pub fn inspect_page(sniffer: &Sniffer) -> Container<Message, Renderer<StyleType>
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<StyleType>>
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<StyleType>>
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),
);
}

View file

@ -41,17 +41,6 @@ pub fn notifications_page(sniffer: &Sniffer) -> Container<Message, Renderer<Styl
.height(Length::Fill);
let tabs = get_pages_tabs(
[
RunningPage::Overview,
RunningPage::Inspect,
RunningPage::Notifications,
],
&["d ", "5 ", "7 "],
&[
Message::ChangeRunningPage(RunningPage::Overview),
Message::ChangeRunningPage(RunningPage::Inspect),
Message::TickInit,
],
RunningPage::Notifications,
font,
font_headers,

View file

@ -76,17 +76,6 @@ pub fn overview_page(sniffer: &Sniffer) -> Container<Message, Renderer<StyleType
(_observed, filtered) => {
//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,

View file

@ -40,20 +40,8 @@ pub fn settings_language_page(sniffer: &Sniffer) -> Container<Message, Renderer<
sniffer.language,
))
.push(get_settings_tabs(
[
SettingsPage::Notifications,
SettingsPage::Appearance,
SettingsPage::Language,
],
&["7 ", "K ", "c "],
&[
Message::OpenSettings(SettingsPage::Notifications),
Message::OpenSettings(SettingsPage::Appearance),
Message::TickInit,
],
SettingsPage::Language,
font,
font_headers,
sniffer.language,
))
.push(vertical_space(Fixed(15.0)))

View file

@ -45,20 +45,8 @@ pub fn settings_notifications_page(sniffer: &Sniffer) -> Container<Message, Rend
sniffer.language,
))
.push(get_settings_tabs(
[
SettingsPage::Notifications,
SettingsPage::Appearance,
SettingsPage::Language,
],
&["7 ", "K ", "c "],
&[
Message::TickInit,
Message::OpenSettings(SettingsPage::Appearance),
Message::OpenSettings(SettingsPage::Language),
],
SettingsPage::Notifications,
font,
font_headers,
sniffer.language,
))
.push(vertical_space(Fixed(15.0)))

View file

@ -41,20 +41,8 @@ pub fn settings_style_page(sniffer: &Sniffer) -> Container<Message, Renderer<Sty
sniffer.language,
))
.push(get_settings_tabs(
[
SettingsPage::Notifications,
SettingsPage::Appearance,
SettingsPage::Language,
],
&["7 ", "K ", "c "],
&[
Message::OpenSettings(SettingsPage::Notifications),
Message::TickInit,
Message::OpenSettings(SettingsPage::Language),
],
SettingsPage::Appearance,
font,
font_headers,
sniffer.language,
))
.push(vertical_space(Length::Fixed(15.0)))

View file

@ -1,6 +1,11 @@
use iced::widget::Text;
use iced::Renderer;
use crate::gui::styles::style_constants::ICONS;
use crate::gui::types::message::Message;
use crate::translations::translations::{notifications_translation, overview_translation};
use crate::translations::translations_2::inspect_translation;
use crate::Language;
use crate::{Language, StyleType};
/// This enum defines the current running page.
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
@ -14,6 +19,12 @@ pub enum RunningPage {
}
impl RunningPage {
pub const ALL: [RunningPage; 3] = [
RunningPage::Overview,
RunningPage::Inspect,
RunningPage::Notifications,
];
pub fn get_tab_label(&self, language: Language) -> &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<StyleType>> {
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)]

View file

@ -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<StyleType>> {
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)]

View file

@ -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,

View file

@ -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(),

View file

@ -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,
}
}

View file

@ -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<StyleType>>,
)>,
usize,
) {
pub fn get_searched_entries(sniffer: &Sniffer) -> (Vec<ReportEntry>, 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(),

View file

@ -1 +1,2 @@
pub mod report_entry;
pub mod report_sort_type;

View file

@ -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<StyleType>>,
}