From c334c9c9f50b44f1e310668a1f25bc5987607c90 Mon Sep 17 00:00:00 2001 From: Giuliano Bellini s294739 Date: Sat, 9 Dec 2023 16:44:22 +0100 Subject: [PATCH] moved custom style path selection to settings style tab --- README.md | 2 +- src/gui/pages/settings_advanced_page.rs | 160 +++++++----------------- src/gui/pages/settings_style_page.rs | 63 +++++++++- src/gui/types/message.rs | 6 +- src/gui/types/sniffer.rs | 7 +- src/translations/translations_3.rs | 22 ++-- src/utils/types/icon.rs | 4 +- 7 files changed, 120 insertions(+), 144 deletions(-) diff --git a/README.md b/README.md index 3cae022a..665410d4 100644 --- a/README.md +++ b/README.md @@ -430,7 +430,7 @@ The currently usable hotkeys are reported in the following. The example theme above uses colors from [Catppuccin Mocha](https://github.com/catppuccin/catppuccin). To use a custom theme for your instance of Sniffnet, specify the path of your TOML file in the application's - advanced settings (accessible from the top right corner of the app). + settings (at the bottom of the style tab). ## Troubleshooting diff --git a/src/gui/pages/settings_advanced_page.rs b/src/gui/pages/settings_advanced_page.rs index 4b77df1b..5ae7043e 100644 --- a/src/gui/pages/settings_advanced_page.rs +++ b/src/gui/pages/settings_advanced_page.rs @@ -3,10 +3,7 @@ use std::sync::Arc; use iced::advanced::widget::Text; use iced::alignment::{Horizontal, Vertical}; use iced::widget::tooltip::Position; -use iced::widget::{ - button, horizontal_space, lazy, vertical_space, Column, Container, Row, Slider, TextInput, - Tooltip, -}; +use iced::widget::{button, vertical_space, Column, Container, Row, Slider, TextInput, Tooltip}; use iced::Length::Fixed; use iced::{Alignment, Font, Length, Renderer}; @@ -17,18 +14,14 @@ use crate::gui::styles::container::ContainerType; use crate::gui::styles::style_constants::{get_font, get_font_headers, FONT_SIZE_SUBTITLE}; use crate::gui::styles::text::TextType; use crate::gui::styles::text_input::TextInputType; -use crate::gui::styles::types::custom_palette::CustomPalette; use crate::gui::types::message::Message; use crate::mmdb::types::mmdb_reader::MmdbReader; use crate::translations::translations_2::country_translation; use crate::translations::translations_3::{ - advanced_settings_translation, custom_style_translation, file_path_translation, - info_mmdb_paths_translation, mmdb_paths_translation, params_not_editable_translation, - restore_defaults_translation, scale_factor_translation, + advanced_settings_translation, info_mmdb_paths_translation, mmdb_paths_translation, + params_not_editable_translation, scale_factor_translation, }; -use crate::utils::formatted_strings::get_default_report_file_path; -use crate::utils::types::icon::Icon; -use crate::{ConfigAdvancedSettings, Language, RunningPage, Sniffer, StyleType}; +use crate::{Language, RunningPage, Sniffer, StyleType}; pub fn settings_advanced_page(sniffer: &Sniffer) -> Container> { let font = get_font(sniffer.style); @@ -51,40 +44,34 @@ pub fn settings_advanced_page(sniffer: &Sniffer) -> Container Container Row<'static, Message, Renderer> { - let mut ret_val = Row::new().spacing(10).align_items(Alignment::Center).push( +fn title_row(language: Language, font: Font) -> Row<'static, Message, Renderer> { + Row::new().spacing(10).align_items(Alignment::Center).push( Text::new(advanced_settings_translation(language)) .style(TextType::Title) .font(font) .size(FONT_SIZE_SUBTITLE), - ); - - if advanced_settings.ne(&ConfigAdvancedSettings::default()) { - ret_val = ret_val.push( - Tooltip::new( - button( - Icon::Restore - .to_text() - .vertical_alignment(Vertical::Center) - .horizontal_alignment(Horizontal::Center) - .size(17), - ) - .padding(2) - .height(Fixed(25.0)) - .width(Fixed(25.0)) - .on_press(Message::RestoreDefaults), - restore_defaults_translation(language), - Position::Right, - ) - .font(font) - .style(ContainerType::Tooltip), - ); - } - - ret_val + ) } fn scale_factor_slider( @@ -160,33 +119,31 @@ fn scale_factor_slider( .width(Fixed(150.0)), ), ) - .padding(5) - .width(Length::FillPortion(1)) .align_x(Horizontal::Center) .align_y(Vertical::Center) } -fn report_path_setting( - is_editable: bool, - language: Language, - font: Font, - custom_path: String, -) -> Row<'static, Message, Renderer> { - let mut input = TextInput::new(&get_default_report_file_path(), &custom_path) - .padding([0, 5]) - .font(font) - .width(Length::Fixed(500.0)) - .style(TextInputType::Standard); - - if is_editable { - input = input.on_input(Message::CustomReport); - } - - Row::new() - .push(Text::new(format!("{}:", file_path_translation(language))).font(font)) - .push(horizontal_space(5)) - .push(input) -} +// fn report_path_setting( +// is_editable: bool, +// language: Language, +// font: Font, +// custom_path: &str, +// ) -> Row<'static, Message, Renderer> { +// let mut input = TextInput::new(&get_default_report_file_path(), custom_path) +// .padding([0, 5]) +// .font(font) +// .width(Length::Fixed(500.0)) +// .style(TextInputType::Standard); +// +// if is_editable { +// input = input.on_input(Message::CustomReport); +// } +// +// Row::new() +// .push(Text::new(format!("{}:", file_path_translation(language))).font(font)) +// .push(horizontal_space(5)) +// .push(input) +// } fn mmdb_settings( is_editable: bool, @@ -285,32 +242,3 @@ fn mmdb_input( .push(Text::new(format!("{caption}:")).font(font)) .push(input) } - -fn lazy_custom_style_settings( - language: Language, - font: Font, - custom_path: &str, -) -> Row<'static, Message, Renderer> { - let is_error = if custom_path.is_empty() { - false - } else { - CustomPalette::from_file(custom_path).is_err() - }; - - let input = TextInput::new("-", custom_path) - .on_input(Message::LoadStyle) - .on_submit(Message::LoadStyle(custom_path.to_string())) - .padding([0, 5]) - .font(font) - .width(Length::Fixed(200.0)) - .style(if is_error { - TextInputType::Error - } else { - TextInputType::Standard - }); - - Row::new() - .spacing(5) - .push(Text::new(format!("{}:", custom_style_translation(language))).font(font)) - .push(input) -} diff --git a/src/gui/pages/settings_style_page.rs b/src/gui/pages/settings_style_page.rs index bc5b933b..453f1ca3 100644 --- a/src/gui/pages/settings_style_page.rs +++ b/src/gui/pages/settings_style_page.rs @@ -1,6 +1,6 @@ use iced::alignment::{Horizontal, Vertical}; use iced::widget::scrollable::Direction; -use iced::widget::{button, horizontal_space, vertical_space, Rule}; +use iced::widget::{button, horizontal_space, lazy, vertical_space, Rule, TextInput}; use iced::widget::{Button, Column, Container, Row, Scrollable, Space, Text}; use iced::Length::Fixed; use iced::{Alignment, Element, Font, Length, Renderer}; @@ -16,7 +16,8 @@ use crate::gui::styles::style_constants::{ get_font, get_font_headers, BORDER_WIDTH, FONT_SIZE_SUBTITLE, }; use crate::gui::styles::text::TextType; -use crate::gui::styles::types::custom_palette::ExtraStyles; +use crate::gui::styles::text_input::TextInputType; +use crate::gui::styles::types::custom_palette::{CustomPalette, ExtraStyles}; use crate::gui::styles::types::gradient_type::GradientType; use crate::gui::types::message::Message; use crate::translations::translations::{ @@ -24,6 +25,7 @@ use crate::translations::translations::{ yeti_day_translation, yeti_night_translation, }; use crate::translations::translations_2::color_gradients_translation; +use crate::translations::translations_3::custom_style_translation; use crate::utils::types::icon::Icon; use crate::StyleType::{Day, DeepSea, MonAmour, Night}; use crate::{Language, Sniffer, StyleType}; @@ -101,6 +103,19 @@ pub fn settings_style_page(sniffer: &Sniffer) -> Container Button<'static, Message, Renderer> { + let is_custom_toml_style_set = matches!(style, StyleType::Custom(ExtraStyles::CustomToml(_))); + + let is_error = if custom_path.is_empty() { + false + } else { + CustomPalette::from_file(custom_path).is_err() + }; + + let input = TextInput::new("-", custom_path) + .on_input(Message::LoadStyle) + .on_submit(Message::LoadStyle(custom_path.to_string())) + .padding([0, 5]) + .font(font) + .width(Length::Fixed(300.0)) + .style(if is_error { + TextInputType::Error + } else { + TextInputType::Standard + }); + + let content = Column::new() + .align_items(Alignment::Center) + .spacing(5) + .push(Text::new(custom_style_translation(language)).font(font)) + .push(input); + + Button::new(content) + .height(Length::Fixed(75.0)) + .width(Length::Fixed(380.0)) + .padding([10, 0, 5, 0]) + .style(if is_custom_toml_style_set { + ButtonType::BorderedRoundSelected + } else { + ButtonType::BorderedRound + }) + .on_press(Message::LoadStyle(custom_path.to_string())) +} diff --git a/src/gui/types/message.rs b/src/gui/types/message.rs index 65a455de..171378f2 100644 --- a/src/gui/types/message.rs +++ b/src/gui/types/message.rs @@ -91,8 +91,6 @@ pub enum Message { GradientsSelection(GradientType), /// Set UI scale factor ChangeScaleFactor(f64), - /// Restore default advanced settings - RestoreDefaults, /// The app window position has been changed WindowMoved(i32, i32), /// The app window size has been changed @@ -101,8 +99,8 @@ pub enum Message { CustomCountryDb(String), /// The ASN MMDB custom path has been updated CustomAsnDb(String), - /// The path for the output report has been updated - CustomReport(String), + // /// The path for the output report has been updated + // CustomReport(String), /// Save the configurations of the app and quit CloseRequested, } diff --git a/src/gui/types/sniffer.rs b/src/gui/types/sniffer.rs index 06b53143..a2938a55 100644 --- a/src/gui/types/sniffer.rs +++ b/src/gui/types/sniffer.rs @@ -264,7 +264,6 @@ impl Sniffer { Message::ChangeScaleFactor(multiplier) => { self.advanced_settings.scale_factor = multiplier; } - Message::RestoreDefaults => self.advanced_settings = ConfigAdvancedSettings::default(), Message::WindowMoved(x, y) => { self.window.position = (x, y); } @@ -283,9 +282,9 @@ impl Sniffer { self.advanced_settings.mmdb_asn = db.clone(); self.asn_mmdb_reader = Arc::new(MmdbReader::from(&db, ASN_MMDB)); } - Message::CustomReport(path) => { - self.advanced_settings.output_path = path; - } + // Message::CustomReport(path) => { + // self.advanced_settings.output_path = path; + // } Message::CloseRequested => { self.get_configs().store(); return iced::window::close(); diff --git a/src/translations/translations_3.rs b/src/translations/translations_3.rs index 99e753fc..4527dd1c 100644 --- a/src/translations/translations_3.rs +++ b/src/translations/translations_3.rs @@ -18,14 +18,6 @@ pub fn scale_factor_translation(language: Language) -> &'static str { } } -pub fn restore_defaults_translation(language: Language) -> &'static str { - match language { - Language::EN => "Restore defaults", - Language::IT => "Ripristina valori predefiniti", - _ => "Restore defaults", - } -} - pub fn mmdb_paths_translation(language: Language) -> &'static str { match language { Language::EN => "Database file paths (MMDB format)", @@ -59,13 +51,13 @@ pub fn params_not_editable_translation(language: Language) -> &'static str { } } -pub fn file_path_translation(language: Language) -> &'static str { - match language { - Language::EN => "File path", - Language::IT => "Percorso del file", - _ => "File path", - } -} +// pub fn file_path_translation(language: Language) -> &'static str { +// match language { +// Language::EN => "File path", +// Language::IT => "Percorso del file", +// _ => "File path", +// } +// } pub fn custom_style_translation(language: Language) -> &'static str { match language { diff --git a/src/utils/types/icon.rs b/src/utils/types/icon.rs index 1d5cbf34..20a20075 100644 --- a/src/utils/types/icon.rs +++ b/src/utils/types/icon.rs @@ -30,7 +30,7 @@ pub enum Icon { Notification, Overview, PacketsThreshold, - Restore, + // Restore, Rocket, Settings, Sniffnet, @@ -67,7 +67,7 @@ impl Icon { Icon::Notification => "7", Icon::Overview => "d", Icon::PacketsThreshold => "e", - Icon::Restore => "k", + // Icon::Restore => "k", Icon::Rocket => "S", Icon::Settings => "a", Icon::Sniffnet => "A",