Skip to content

Commit

Permalink
moved custom style path selection to settings style tab
Browse files Browse the repository at this point in the history
  • Loading branch information
GyulyVGC committed Dec 9, 2023
1 parent ab79201 commit c334c9c
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 144 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
</details>

## Troubleshooting
Expand Down
160 changes: 44 additions & 116 deletions src/gui/pages/settings_advanced_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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<Message, Renderer<StyleType>> {
let font = get_font(sniffer.style);
Expand All @@ -51,40 +44,34 @@ pub fn settings_advanced_page(sniffer: &Sniffer) -> Container<Message, Renderer<
sniffer.language,
))
.push(vertical_space(Fixed(15.0)))
.push(title_row(
sniffer.language,
font,
&sniffer.advanced_settings,
))
.push(vertical_space(Fixed(5.0)))
.push(title_row(sniffer.language, font))
.push(vertical_space(Fixed(10.0)))
.push(scale_factor_slider(
sniffer.language,
font,
sniffer.advanced_settings.scale_factor,
))
.push(lazy(&sniffer.advanced_settings.style_path, move |_| {
lazy_custom_style_settings(
sniffer.language,
font,
&sniffer.advanced_settings.style_path,
)
}));
.push(vertical_space(Fixed(10.0)));

if !is_editable {
content = content.push(
Container::new(Text::new(params_not_editable_translation(sniffer.language)).font(font))
content = content
.push(
Container::new(
Text::new(params_not_editable_translation(sniffer.language)).font(font),
)
.padding(10.0)
.style(ContainerType::Badge),
);
)
.push(vertical_space(Fixed(10.0)));
}

content = content
.push(report_path_setting(
is_editable,
sniffer.language,
font,
sniffer.advanced_settings.output_path.clone(),
))
// .push(report_path_setting(
// is_editable,
// sniffer.language,
// font,
// &sniffer.advanced_settings.output_path,
// ))
.push(mmdb_settings(
is_editable,
sniffer.language,
Expand All @@ -101,41 +88,13 @@ pub fn settings_advanced_page(sniffer: &Sniffer) -> Container<Message, Renderer<
.style(ContainerType::Modal)
}

fn title_row(
language: Language,
font: Font,
advanced_settings: &ConfigAdvancedSettings,
) -> Row<'static, Message, Renderer<StyleType>> {
let mut ret_val = Row::new().spacing(10).align_items(Alignment::Center).push(
fn title_row(language: Language, font: Font) -> Row<'static, Message, Renderer<StyleType>> {
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(
Expand All @@ -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<StyleType>> {
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<StyleType>> {
// 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,
Expand Down Expand Up @@ -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<StyleType>> {
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)
}
63 changes: 61 additions & 2 deletions src/gui/pages/settings_style_page.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -16,14 +16,16 @@ 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::{
appearance_title_translation, deep_sea_translation, mon_amour_translation,
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};
Expand Down Expand Up @@ -101,6 +103,19 @@ pub fn settings_style_page(sniffer: &Sniffer) -> Container<Message, Renderer<Sty
for children in get_extra_palettes(ExtraStyles::all_styles(), sniffer.style) {
styles_col = styles_col.push(children);
}
styles_col = styles_col
.push(lazy(
(&sniffer.advanced_settings.style_path, sniffer.style),
move |_| {
lazy_custom_style_input(
sniffer.language,
font,
&sniffer.advanced_settings.style_path,
sniffer.style,
)
},
))
.push(vertical_space(10));

let styles_scroll =
Scrollable::new(styles_col).direction(Direction::Vertical(ScrollbarType::properties()));
Expand Down Expand Up @@ -291,3 +306,47 @@ fn get_extra_palettes(

children
}

fn lazy_custom_style_input(
language: Language,
font: Font,
custom_path: &str,
style: StyleType,
) -> Button<'static, Message, Renderer<StyleType>> {
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()))
}
6 changes: 2 additions & 4 deletions src/gui/types/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
}
7 changes: 3 additions & 4 deletions src/gui/types/sniffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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();
Expand Down
Loading

0 comments on commit c334c9c

Please sign in to comment.