From 8f8af54929cdd975b5c4548322a48b22c7d47c8b Mon Sep 17 00:00:00 2001 From: Giuliano Bellini s294739 Date: Fri, 15 Dec 2023 17:47:32 +0100 Subject: [PATCH] start the analysis only if all filters are valid --- src/gui/pages/initial_page.rs | 41 +++++++++++++++++++-------- src/gui/styles/button.rs | 26 ++++++++++++++++- src/gui/styles/types/gradient_type.rs | 20 +++++++++---- src/gui/types/sniffer.rs | 4 ++- src/networking/types/filters.rs | 7 +++++ 5 files changed, 79 insertions(+), 19 deletions(-) diff --git a/src/gui/pages/initial_page.rs b/src/gui/pages/initial_page.rs index cedd1ca1..418065ed 100644 --- a/src/gui/pages/initial_page.rs +++ b/src/gui/pages/initial_page.rs @@ -7,8 +7,8 @@ use iced::widget::scrollable::Direction; use iced::widget::text::Shaping; use iced::widget::tooltip::Position; use iced::widget::{ - button, horizontal_space, vertical_space, Button, Column, Container, Row, Scrollable, Text, - TextInput, Tooltip, + button, horizontal_space, vertical_space, Button, Column, Container, Row, Rule, Scrollable, + Text, TextInput, Tooltip, }; use iced::Length::FillPortion; use iced::{alignment, Font, Length, Renderer}; @@ -24,6 +24,7 @@ use crate::gui::styles::text_input::TextInputType; use crate::gui::styles::types::gradient_type::GradientType; use crate::gui::types::message::Message; use crate::gui::types::sniffer::Sniffer; +use crate::networking::types::filters::Filters; use crate::networking::types::ip_collection::IpCollection; use crate::networking::types::port_collection::PortCollection; use crate::translations::translations::{ @@ -78,7 +79,19 @@ pub fn initial_page(sniffer: &Sniffer) -> Container .push(col_address_filter) .push(col_port_filter), ) - .push(button_start(font, language, color_gradient)); + .push(Rule::horizontal(40)) + .push( + Container::new(button_start( + font, + language, + color_gradient, + &sniffer.filters, + )) + .width(Length::Fill) + .height(Length::Fill) + .align_y(Vertical::Center) + .align_x(Horizontal::Center), + ); let body = Column::new().push(vertical_space(Length::Fixed(5.0))).push( Row::new() @@ -98,10 +111,10 @@ fn col_ip_buttons( let mut buttons_row = Row::new().spacing(5).padding([0, 0, 0, 5]); for option in IpVersion::ALL { let is_active = active_ip_filters.contains(&option); - let check_simbol = if is_active { "✔" } else { "✘" }; + let check_symbol = if is_active { "✔" } else { "✘" }; buttons_row = buttons_row.push( Button::new( - Text::new(format!("{check_simbol} {option}")) + Text::new(format!("{option} {check_symbol}")) .shaping(Shaping::Advanced) .horizontal_alignment(Horizontal::Center) .vertical_alignment(Vertical::Center) @@ -138,10 +151,10 @@ fn col_transport_buttons( let mut buttons_row = Row::new().spacing(5).padding([0, 0, 0, 5]); for option in TransProtocol::ALL { let is_active = active_transport_filters.contains(&option); - let check_simbol = if is_active { "✔" } else { "✘" }; + let check_symbol = if is_active { "✔" } else { "✘" }; buttons_row = buttons_row.push( Button::new( - Text::new(format!("{check_simbol} {option}")) + Text::new(format!("{option} {check_symbol}")) .shaping(Shaping::Advanced) .horizontal_alignment(Horizontal::Center) .vertical_alignment(Vertical::Center) @@ -182,7 +195,7 @@ fn col_address_input( }; let input_row = Row::new().padding([0, 0, 0, 5]).push( TextInput::new(IpCollection::PLACEHOLDER_STR, value) - .padding([0, 5]) + .padding([2, 5]) .on_input(Message::AddressFilter) .font(font) .width(Length::Fixed(310.0)) @@ -217,7 +230,7 @@ fn col_port_input( }; let input_row = Row::new().padding([0, 0, 0, 5]).push( TextInput::new(PortCollection::PLACEHOLDER_STR, value) - .padding([0, 5]) + .padding([2, 5]) .on_input(Message::PortFilter) .font(font) .width(Length::Fixed(180.0)) @@ -244,8 +257,9 @@ fn button_start( font: Font, language: Language, color_gradient: GradientType, + filters: &Filters, ) -> Tooltip<'static, Message, Renderer> { - let content = button( + let mut content = button( Icon::Rocket .to_text() .size(25) @@ -255,8 +269,11 @@ fn button_start( .padding(10) .height(Length::Fixed(80.0)) .width(Length::Fixed(160.0)) - .style(ButtonType::Gradient(color_gradient)) - .on_press(Message::Start); + .style(ButtonType::Gradient(color_gradient)); + + if filters.are_valid() { + content = content.on_press(Message::Start); + } let tooltip = start_translation(language).to_string(); //tooltip.push_str(" [⏎]"); diff --git a/src/gui/styles/button.rs b/src/gui/styles/button.rs index 330ca778..db500c81 100644 --- a/src/gui/styles/button.rs +++ b/src/gui/styles/button.rs @@ -57,6 +57,7 @@ impl button::StyleSheet for StyleType { &colors, *gradient_type, self.is_nightly(), + 1.0, )), _ => Background::Color(colors.buttons), }), @@ -146,6 +147,29 @@ impl button::StyleSheet for StyleType { } fn disabled(&self, style: &Self::Style) -> Appearance { - button::StyleSheet::active(self, style) + let colors = get_colors(*self); + button::Appearance { + background: Some(match style { + ButtonType::Gradient(GradientType::None) => Background::Color(Color { + a: get_alpha_round_containers(*self), + ..colors.secondary + }), + ButtonType::Gradient(gradient_type) => Background::Gradient(get_gradient_buttons( + &colors, + *gradient_type, + self.is_nightly(), + get_alpha_round_containers(*self), + )), + _ => Background::Color(colors.buttons), + }), + border_radius: BORDER_BUTTON_RADIUS.into(), + border_width: BORDER_WIDTH, + shadow_offset: Vector::new(0.0, 0.0), + text_color: colors.text_headers, + border_color: Color { + a: get_alpha_round_containers(*self), + ..colors.secondary + }, + } } } diff --git a/src/gui/styles/types/gradient_type.rs b/src/gui/styles/types/gradient_type.rs index a536f67d..56cb32fa 100644 --- a/src/gui/styles/types/gradient_type.rs +++ b/src/gui/styles/types/gradient_type.rs @@ -51,6 +51,7 @@ pub fn get_gradient_buttons( colors: &Palette, gradient_type: GradientType, is_nightly: bool, + alpha: f32, ) -> Gradient { let mix = if is_nightly { Color::BLACK @@ -61,13 +62,22 @@ pub fn get_gradient_buttons( iced::gradient::Linear::new(Degrees(225.0)) .add_stop( 0.0, - match gradient_type { - GradientType::Mild => mix_colors(mix, colors.secondary), - GradientType::Wild => colors.outgoing, - GradientType::None => colors.secondary, + Color { + a: alpha, + ..match gradient_type { + GradientType::Mild => mix_colors(mix, colors.secondary), + GradientType::Wild => colors.outgoing, + GradientType::None => colors.secondary, + } }, ) - .add_stop(1.0, colors.secondary), + .add_stop( + 1.0, + Color { + a: alpha, + ..colors.secondary + }, + ), ) } diff --git a/src/gui/types/sniffer.rs b/src/gui/types/sniffer.rs index 7a888590..0ecb74cc 100644 --- a/src/gui/types/sniffer.rs +++ b/src/gui/types/sniffer.rs @@ -499,7 +499,9 @@ impl Sniffer { && self.settings_page.is_none() && self.modal.is_none() { - return self.update(Message::Start); + if self.filters.are_valid() { + return self.update(Message::Start); + } } else if self.modal.eq(&Some(MyModal::Quit)) { return self.update(Message::Reset); } else if self.modal.eq(&Some(MyModal::ClearAll)) { diff --git a/src/networking/types/filters.rs b/src/networking/types/filters.rs index 21d6782d..e650b879 100644 --- a/src/networking/types/filters.rs +++ b/src/networking/types/filters.rs @@ -50,4 +50,11 @@ impl Filters { && (self.port_collection.contains(packet_filters_fields.sport) || self.port_collection.contains(packet_filters_fields.dport)) } + + pub fn are_valid(&self) -> bool { + !self.ip.is_empty() + && !self.transport.is_empty() + && IpCollection::new(&self.address_str).is_some() + && PortCollection::new(&self.port_str).is_some() + } }