Skip to content

Commit

Permalink
Replace StyleType with Arc<StyleType>.
Browse files Browse the repository at this point in the history
`StyleType` is no longer `Copy` because the enum holds `CustomStyle`.
  • Loading branch information
joshuamegnauth54 committed Jun 22, 2023
1 parent 64efb61 commit 9540971
Show file tree
Hide file tree
Showing 37 changed files with 381 additions and 322 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ iced_lazy = "0.6.1"
plotters-iced = "0.8.0"
maxminddb = "0.23.0"
confy = "0.5.1"
serde = { version = "1.0.163", default_features = false, features = ["derive"] }
serde = { version = "1.0.163", default_features = false, features = ["derive", "rc"] }
rodio = { version = "0.17.1", default_features = false, features = ["mp3"] }
reqwest = { version = "0.11.18", default-features = false, features = ["json", "blocking", "rustls-tls"] }
dns-lookup = "2.0.2"
Expand Down
1 change: 1 addition & 0 deletions resources/themes/catppuccin_mocha.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ round_borders = "#74c7ec" # Sapphire
round_containers = "#585b70" # Surface 2
starred = "#f9e2af" # Yellow
badge_alpha = 0.75
color_mix_chart = 0.3
4 changes: 2 additions & 2 deletions src/chart/types/traffic_chart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub struct TrafficChart {
}

impl TrafficChart {
pub fn new(style: StyleType, language: Language) -> Self {
pub fn new(style: &StyleType, language: Language) -> Self {
TrafficChart {
ticks: 0,
sent_bytes: VecDeque::default(),
Expand Down Expand Up @@ -89,7 +89,7 @@ impl TrafficChart {
self.language = language;
}

pub fn change_colors(&mut self, style: StyleType) {
pub fn change_colors(&mut self, style: &StyleType) {
self.color_font = to_rgb_color(get_colors(style).text_body);
self.color_incoming = to_rgb_color(get_colors(style).incoming);
self.color_outgoing = to_rgb_color(get_colors(style).outgoing);
Expand Down
4 changes: 3 additions & 1 deletion src/configs/types/config_settings.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
//! Module defining the `ConfigSettings` struct, which allows to save and reload
//! the application default configuration.
use std::sync::Arc;

use serde::{Deserialize, Serialize};

use crate::notifications::types::notifications::Notifications;
use crate::{Language, StyleType};

#[derive(Serialize, Deserialize, Default)]
pub struct ConfigSettings {
pub style: StyleType,
pub style: Arc<StyleType>,
pub language: Language,
pub notifications: Notifications,
}
14 changes: 8 additions & 6 deletions src/countries/country_utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::Arc;

use iced::widget::Tooltip;
use iced::{Length, Renderer};
use iced_native::svg::Handle;
Expand Down Expand Up @@ -332,16 +334,16 @@ pub fn get_flag_tooltip(
is_local: bool,
traffic_type: TrafficType,
language: Language,
style: StyleType,
style: &Arc<StyleType>,
) -> Tooltip<'static, Message> {
let (content, tooltip) =
get_flag_from_country(country, width, is_local, traffic_type, language);

let mut tooltip = Tooltip::new(content, tooltip, Position::FollowCursor)
.font(get_font(style))
.font(get_font(&style))
.snap_within_viewport(true)
.style(<StyleTuple as Into<iced::theme::Container>>::into(
StyleTuple(style, ElementType::Tooltip),
StyleTuple(Arc::clone(style), ElementType::Tooltip),
));

if width == FLAGS_WIDTH_SMALL {
Expand All @@ -355,7 +357,7 @@ pub fn get_computer_tooltip(
is_my_address: bool,
traffic_type: TrafficType,
language: Language,
style: StyleType,
style: &Arc<StyleType>,
) -> Tooltip<'static, Message> {
let content = Svg::new(Handle::from_memory(Vec::from(
match (is_my_address, traffic_type) {
Expand All @@ -376,9 +378,9 @@ pub fn get_computer_tooltip(
};

Tooltip::new(content, tooltip, Position::FollowCursor)
.font(get_font(style))
.font(get_font(&style))
.snap_within_viewport(true)
.style(<StyleTuple as Into<iced::theme::Container>>::into(
StyleTuple(style, ElementType::Tooltip),
StyleTuple(Arc::clone(style), ElementType::Tooltip),
))
}
14 changes: 7 additions & 7 deletions src/gui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ impl Application for Sniffer {

fn view(&self) -> Element<Message> {
let status = *self.status_pair.0.lock().unwrap();
let style = self.style;
let font = get_font(style);
let style = &self.style;
let font = get_font(&style);

let header = match status {
Status::Init => header(style, false, self.language, self.last_opened_setting),
Status::Running => header(style, true, self.language, self.last_opened_setting),
Status::Init => header(&style, false, self.language, self.last_opened_setting),
Status::Running => header(&style, true, self.language, self.last_opened_setting),
};

let body = match status {
Expand All @@ -66,7 +66,7 @@ impl Application for Sniffer {
},
};

let footer = footer(self.language, style, &self.newer_release_available.clone());
let footer = footer(self.language, &style, &self.newer_release_available.clone());

let content = Column::new().push(header).push(body).push(footer);

Expand All @@ -88,8 +88,8 @@ impl Application for Sniffer {
}
Some(modal) => {
let overlay = match modal {
MyModal::Quit => get_exit_overlay(style, font, self.language),
MyModal::ClearAll => get_clear_all_overlay(style, font, self.language),
MyModal::Quit => get_exit_overlay(&style, font, self.language),
MyModal::ClearAll => get_clear_all_overlay(&style, font, self.language),
MyModal::ConnectionDetails(connection_index) => {
connection_details_page(self, connection_index)
}
Expand Down
30 changes: 15 additions & 15 deletions src/gui/components/footer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::Language;

pub fn footer(
language: Language,
style: StyleType,
style: &Arc<StyleType>,
newer_release_available: &Arc<Mutex<Result<bool, String>>>,
) -> Container<'static, Message> {
let font_footer = get_font_headers(style);
Expand Down Expand Up @@ -53,11 +53,11 @@ pub fn footer(
.align_y(Vertical::Center)
.align_x(Horizontal::Center)
.style(<StyleTuple as Into<iced::theme::Container>>::into(
StyleTuple(style, ElementType::Headers),
StyleTuple(Arc::clone(style), ElementType::Headers),
))
}

fn get_button_website(style: StyleType) -> Tooltip<'static, Message> {
fn get_button_website(style: &Arc<StyleType>) -> Tooltip<'static, Message> {
let content = button(
Text::new('c'.to_string())
.font(ICONS)
Expand All @@ -67,17 +67,17 @@ fn get_button_website(style: StyleType) -> Tooltip<'static, Message> {
)
.height(Length::Fixed(30.0))
.width(Length::Fixed(30.0))
.style(StyleTuple(style, ElementType::Standard).into())
.style(StyleTuple(Arc::clone(style), ElementType::Standard).into())
.on_press(Message::OpenWebPage(WebPage::Website));

Tooltip::new(content, "Website", Position::Top)
.font(get_font(style))
.font(get_font(&style))
.style(<StyleTuple as Into<iced::theme::Container>>::into(
StyleTuple(style, ElementType::Tooltip),
StyleTuple(Arc::clone(style), ElementType::Tooltip),
))
}

fn get_button_github(style: StyleType) -> Tooltip<'static, Message> {
fn get_button_github(style: &Arc<StyleType>) -> Tooltip<'static, Message> {
let content = button(
Text::new('H'.to_string())
.font(ICONS)
Expand All @@ -87,17 +87,17 @@ fn get_button_github(style: StyleType) -> Tooltip<'static, Message> {
)
.height(Length::Fixed(40.0))
.width(Length::Fixed(40.0))
.style(StyleTuple(style, ElementType::Standard).into())
.style(StyleTuple(Arc::clone(style), ElementType::Standard).into())
.on_press(Message::OpenWebPage(WebPage::Repo));

Tooltip::new(content, "GitHub", Position::Top)
.font(get_font(style))
.style(<StyleTuple as Into<iced::theme::Container>>::into(
StyleTuple(style, ElementType::Tooltip),
StyleTuple(Arc::clone(style), ElementType::Tooltip),
))
}

fn get_button_sponsor(style: StyleType) -> Tooltip<'static, Message> {
fn get_button_sponsor(style: &Arc<StyleType>) -> Tooltip<'static, Message> {
let content = button(
Text::new('❤'.to_string())
.size(28)
Expand All @@ -107,19 +107,19 @@ fn get_button_sponsor(style: StyleType) -> Tooltip<'static, Message> {
)
.height(Length::Fixed(30.0))
.width(Length::Fixed(30.0))
.style(StyleTuple(style, ElementType::Standard).into())
.style(StyleTuple(Arc::clone(style), ElementType::Standard).into())
.on_press(Message::OpenWebPage(WebPage::Sponsor));

Tooltip::new(content, "Sponsor", Position::Top)
.font(get_font(style))
.style(<StyleTuple as Into<iced::theme::Container>>::into(
StyleTuple(style, ElementType::Tooltip),
StyleTuple(Arc::clone(style), ElementType::Tooltip),
))
}

fn get_release_details(
language: Language,
style: StyleType,
style: &Arc<StyleType>,
font_footer: Font,
newer_release_available: &Arc<Mutex<Result<bool, String>>>,
) -> Row<'static, Message> {
Expand All @@ -146,7 +146,7 @@ fn get_release_details(
.padding(5)
.height(Length::Fixed(35.0))
.width(Length::Fixed(35.0))
.style(StyleTuple(style, ElementType::Alert).into())
.style(StyleTuple(Arc::clone(style), ElementType::Alert).into())
.on_press(Message::OpenWebPage(WebPage::WebsiteDownload));
let tooltip = Tooltip::new(
button,
Expand All @@ -155,7 +155,7 @@ fn get_release_details(
)
.font(get_font(style))
.style(<StyleTuple as Into<iced::theme::Container>>::into(
StyleTuple(style, ElementType::Tooltip),
StyleTuple(Arc::clone(style), ElementType::Tooltip),
));
ret_val = ret_val
.push(horizontal_space(Length::Fixed(10.0)))
Expand Down
18 changes: 10 additions & 8 deletions src/gui/components/header.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! GUI upper header
use std::sync::Arc;

use iced::alignment::{Horizontal, Vertical};
use iced::widget::{button, Container, Row, Text, Tooltip};
use iced::Length::FillPortion;
Expand All @@ -15,7 +17,7 @@ use crate::translations::translations::{quit_analysis_translation, settings_tran
use crate::{Language, StyleType};

pub fn header(
style: StyleType,
style: &Arc<StyleType>,
back_button: bool,
language: Language,
last_opened_setting: SettingsPage,
Expand Down Expand Up @@ -61,11 +63,11 @@ pub fn header(
.align_y(Vertical::Center)
.width(Length::Fill)
.style(<StyleTuple as Into<iced::theme::Container>>::into(
StyleTuple(style, ElementType::Headers),
StyleTuple(Arc::clone(style), ElementType::Headers),
))
}

fn get_button_reset(style: StyleType, language: Language) -> Tooltip<'static, Message> {
fn get_button_reset(style: &Arc<StyleType>, language: Language) -> Tooltip<'static, Message> {
let content = button(
Text::new('C'.to_string())
.font(ICONS)
Expand All @@ -76,7 +78,7 @@ fn get_button_reset(style: StyleType, language: Language) -> Tooltip<'static, Me
.padding(10)
.height(Length::Fixed(40.0))
.width(Length::Fixed(60.0))
.style(StyleTuple(style, ElementType::Standard).into())
.style(StyleTuple(Arc::clone(style), ElementType::Standard).into())
.on_press(Message::ResetButtonPressed);

Tooltip::new(
Expand All @@ -86,12 +88,12 @@ fn get_button_reset(style: StyleType, language: Language) -> Tooltip<'static, Me
)
.font(get_font(style))
.style(<StyleTuple as Into<iced::theme::Container>>::into(
StyleTuple(style, ElementType::Tooltip),
StyleTuple(Arc::clone(style), ElementType::Tooltip),
))
}

pub fn get_button_settings(
style: StyleType,
style: &Arc<StyleType>,
language: Language,
open_overlay: SettingsPage,
) -> Tooltip<'static, Message> {
Expand All @@ -104,12 +106,12 @@ pub fn get_button_settings(
.padding(10)
.height(Length::Fixed(40.0))
.width(Length::Fixed(60.0))
.style(StyleTuple(style, ElementType::Standard).into())
.style(StyleTuple(Arc::clone(style), ElementType::Standard).into())
.on_press(Message::OpenSettings(open_overlay));

Tooltip::new(content, settings_translation(language), Position::Left)
.font(get_font(style))
.style(<StyleTuple as Into<iced::theme::Container>>::into(
StyleTuple(style, ElementType::Tooltip),
StyleTuple(Arc::clone(style), ElementType::Tooltip),
))
}
22 changes: 12 additions & 10 deletions src/gui/components/modal.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::Arc;

use iced::alignment::{Alignment, Horizontal, Vertical};
use iced::widget::{
button, horizontal_space, vertical_space, Column, Container, Row, Text, Tooltip,
Expand All @@ -18,7 +20,7 @@ use crate::translations::translations::{
use crate::{Language, StyleType};

pub fn get_exit_overlay(
style: StyleType,
style: &Arc<StyleType>,
font: Font,
language: Language,
) -> Container<'static, Message> {
Expand All @@ -45,12 +47,12 @@ pub fn get_exit_overlay(
.height(Length::Fixed(160.0))
.width(Length::Fixed(450.0))
.style(<StyleTuple as Into<iced::theme::Container>>::into(
StyleTuple(style, ElementType::Standard),
StyleTuple(Arc::clone(style), ElementType::Standard),
))
}

pub fn get_clear_all_overlay(
style: StyleType,
style: &Arc<StyleType>,
font: Font,
language: Language,
) -> Container<'static, Message> {
Expand All @@ -77,12 +79,12 @@ pub fn get_clear_all_overlay(
.height(Length::Fixed(160.0))
.width(Length::Fixed(450.0))
.style(<StyleTuple as Into<iced::theme::Container>>::into(
StyleTuple(style, ElementType::Standard),
StyleTuple(Arc::clone(style), ElementType::Standard),
))
}

fn get_modal_header(
style: StyleType,
style: &Arc<StyleType>,
language: Language,
title: String,
) -> Container<'static, Message> {
Expand Down Expand Up @@ -111,14 +113,14 @@ fn get_modal_header(
.padding(2)
.height(Length::Fixed(20.0))
.width(Length::Fixed(20.0))
.style(StyleTuple(style, ElementType::Standard).into())
.style(StyleTuple(Arc::clone(style), ElementType::Standard).into())
.on_press(Message::HideModal),
tooltip,
Position::Right,
)
.font(font)
.style(<StyleTuple as Into<iced::theme::Container>>::into(
StyleTuple(style, ElementType::Tooltip),
StyleTuple(Arc::clone(style), ElementType::Tooltip),
)),
)
.width(Length::FillPortion(1))
Expand All @@ -130,14 +132,14 @@ fn get_modal_header(
.height(Length::Fixed(40.0))
.width(Length::Fill)
.style(<StyleTuple as Into<iced::theme::Container>>::into(
StyleTuple(style, ElementType::Headers),
StyleTuple(Arc::clone(style), ElementType::Headers),
))
}

fn confirm_button_row(
language: Language,
font: Font,
style: StyleType,
style: &Arc<StyleType>,
message: Message,
) -> Row<'static, Message> {
Row::new()
Expand All @@ -153,7 +155,7 @@ fn confirm_button_row(
.padding(5)
.height(Length::Fixed(40.0))
.width(Length::Fixed(80.0))
.style(StyleTuple(style, ElementType::Alert).into())
.style(StyleTuple(Arc::clone(style), ElementType::Alert).into())
.on_press(message),
)
}
Expand Down
Loading

0 comments on commit 9540971

Please sign in to comment.