Skip to content

Commit

Permalink
feat: added config to persist window size and position
Browse files Browse the repository at this point in the history
  • Loading branch information
GyulyVGC committed Aug 25, 2023
1 parent 4f47bcc commit 8e0dea2
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 8 deletions.
27 changes: 27 additions & 0 deletions src/configs/types/config_window.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use iced::window::Position;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Copy, Clone)]
pub struct ConfigWindow {
pub position: (i32, i32),
pub size: (u32, u32),
}

impl Default for ConfigWindow {
fn default() -> Self {
Self {
position: (0, 0),
size: (1190, 670),
}
}
}

pub trait ToPosition {
fn to_position(self) -> Position;
}

impl ToPosition for (i32, i32) {
fn to_position(self) -> Position {
Position::Specific(self.0, self.1)
}
}
1 change: 1 addition & 0 deletions src/configs/types/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod config_advanced_settings;
pub mod config_device;
pub mod config_settings;
pub mod config_window;
16 changes: 13 additions & 3 deletions src/gui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ impl Application for Sniffer {
font::load(SARASA_MONO_BOLD_BYTES).map(Message::FontLoaded),
font::load(SARASA_MONO_BYTES).map(Message::FontLoaded),
font::load(ICONS_BYTES).map(Message::FontLoaded),
iced::window::maximize(true),
]),
)
}
Expand Down Expand Up @@ -146,8 +145,15 @@ impl Application for Sniffer {

fn subscription(&self) -> Subscription<Message> {
const NO_MODIFIER: Modifiers = Modifiers::empty();
let hot_keys_subscription = subscription::events_with(|event, _| match event {
let window_events_subscription = subscription::events_with(|event, _| match event {
Window(window::Event::Focused) => Some(Message::WindowFocused),
Window(window::Event::Moved { x, y }) => Some(Message::WindowMoved(x, y)),
Window(window::Event::Resized { width, height }) => {
Some(Message::WindowResized(width, height))
}
_ => None,
});
let hot_keys_subscription = subscription::events_with(|event, _| match event {
Keyboard(Event::KeyPressed {
key_code,
modifiers,
Expand Down Expand Up @@ -184,7 +190,11 @@ impl Application for Sniffer {
iced::time::every(Duration::from_millis(PERIOD_TICK)).map(|_| Message::TickInit)
}
};
Subscription::batch([hot_keys_subscription, time_subscription])
Subscription::batch([
window_events_subscription,
hot_keys_subscription,
time_subscription,
])
}

fn theme(&self) -> Self::Theme {
Expand Down
3 changes: 1 addition & 2 deletions src/gui/styles/types/palette.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ pub fn get_colors(style: StyleType) -> Palette {
}

pub fn to_rgb_color(color: Color) -> RGBColor {
#[allow(clippy::cast_possible_truncation)]
#[allow(clippy::cast_sign_loss)]
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
if color.r <= 1.0
&& color.r >= 0.0
&& color.g <= 1.0
Expand Down
4 changes: 4 additions & 0 deletions src/gui/types/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,8 @@ pub enum Message {
ChangeScaleFactor(f64),
/// Restore default advanced settings
RestoreDefaults,
/// The app window position has been changed
WindowMoved(i32, i32),
/// The app window size has been changed
WindowResized(u32, u32),
}
30 changes: 30 additions & 0 deletions src/gui/types/sniffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use iced::{window, Command};
use pcap::Device;

use crate::chart::manage_chart_data::update_charts_data;
use crate::configs::types::config_window::ConfigWindow;
use crate::gui::components::types::my_modal::MyModal;
use crate::gui::pages::types::running_page::RunningPage;
use crate::gui::pages::types::settings_page::SettingsPage;
Expand Down Expand Up @@ -91,6 +92,8 @@ pub struct Sniffer {
pub last_focus_time: std::time::Instant,
/// Advanced settings
pub advanced_settings: ConfigAdvancedSettings,
/// Position and size of the app window
pub window: ConfigWindow,
}

impl Sniffer {
Expand All @@ -101,6 +104,7 @@ impl Sniffer {
config_settings: &ConfigSettings,
config_device: &ConfigDevice,
config_advanced_settings: ConfigAdvancedSettings,
config_window: ConfigWindow,
newer_release_available: Arc<Mutex<Result<bool, String>>>,
) -> Self {
Self {
Expand Down Expand Up @@ -130,6 +134,7 @@ impl Sniffer {
selected_connection: 0,
last_focus_time: std::time::Instant::now(),
advanced_settings: config_advanced_settings,
window: config_window,
}
}

Expand Down Expand Up @@ -236,6 +241,18 @@ impl Sniffer {
self.advanced_settings.scale_factor = multiplier;
}
Message::RestoreDefaults => self.advanced_settings = ConfigAdvancedSettings::default(),
Message::WindowMoved(x, y) => {
self.window.position = (x, y);
confy::store("sniffnet", "window", self.window).unwrap_or(());
}
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
Message::WindowResized(width, height) => {
let scaled_width = (f64::from(width) * self.advanced_settings.scale_factor) as u32;
let scaled_height =
(f64::from(height) * self.advanced_settings.scale_factor) as u32;
self.window.size = (scaled_width, scaled_height);
confy::store("sniffnet", "window", self.window).unwrap_or(());
}
_ => {}
}
Command::none()
Expand Down Expand Up @@ -557,6 +574,7 @@ mod tests {
&Default::default(),
&Default::default(),
Default::default(),
Default::default(),
Arc::new(Mutex::new(Err(String::new()))),
);

Expand All @@ -580,6 +598,7 @@ mod tests {
&Default::default(),
&Default::default(),
Default::default(),
Default::default(),
Arc::new(Mutex::new(Err(String::new()))),
);

Expand All @@ -603,6 +622,7 @@ mod tests {
&Default::default(),
&Default::default(),
Default::default(),
Default::default(),
Arc::new(Mutex::new(Err(String::new()))),
);

Expand All @@ -626,6 +646,7 @@ mod tests {
&Default::default(),
&Default::default(),
Default::default(),
Default::default(),
Arc::new(Mutex::new(Err(String::new()))),
);

Expand All @@ -647,6 +668,7 @@ mod tests {
&Default::default(),
&Default::default(),
Default::default(),
Default::default(),
Arc::new(Mutex::new(Err(String::new()))),
);

Expand All @@ -670,6 +692,7 @@ mod tests {
&Default::default(),
&Default::default(),
Default::default(),
Default::default(),
Arc::new(Mutex::new(Err(String::new()))),
);

Expand All @@ -694,6 +717,7 @@ mod tests {
&Default::default(),
&Default::default(),
Default::default(),
Default::default(),
Arc::new(Mutex::new(Err(String::new()))),
);

Expand All @@ -717,6 +741,7 @@ mod tests {
&Default::default(),
&Default::default(),
Default::default(),
Default::default(),
Arc::new(Mutex::new(Err(String::new()))),
);
// remove 1
Expand Down Expand Up @@ -914,6 +939,7 @@ mod tests {
&Default::default(),
&Default::default(),
Default::default(),
Default::default(),
Arc::new(Mutex::new(Err(String::new()))),
);

Expand Down Expand Up @@ -1009,6 +1035,7 @@ mod tests {
&Default::default(),
&Default::default(),
Default::default(),
Default::default(),
Arc::new(Mutex::new(Err(String::new()))),
);

Expand All @@ -1034,6 +1061,7 @@ mod tests {
&Default::default(),
&Default::default(),
Default::default(),
Default::default(),
Arc::new(Mutex::new(Err(String::new()))),
);

Expand Down Expand Up @@ -1203,6 +1231,7 @@ mod tests {
&Default::default(),
&Default::default(),
Default::default(),
Default::default(),
Arc::new(Mutex::new(Err(String::new()))),
);
sniffer.runtime_data.logged_notifications =
Expand Down Expand Up @@ -1233,6 +1262,7 @@ mod tests {
&Default::default(),
&Default::default(),
Default::default(),
Default::default(),
Arc::new(Mutex::new(Err(String::new()))),
);
sniffer.last_focus_time = std::time::Instant::now().sub(Duration::from_millis(400));
Expand Down
14 changes: 11 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use std::{panic, process, thread};

#[cfg(target_os = "linux")]
use iced::window::PlatformSpecific;
use iced::window::Position;
use iced::{window, Application, Font, Settings};

use crate::configs::types::config_advanced_settings::ConfigAdvancedSettings;
use crate::configs::types::config_window::{ConfigWindow, ToPosition};
use chart::types::chart_type::ChartType;
use chart::types::traffic_chart::TrafficChart;
use cli::parse_cli_args;
Expand Down Expand Up @@ -102,6 +102,13 @@ pub fn main() -> iced::Result {
ConfigAdvancedSettings::default()
};

let config_window = if let Ok(window) = confy::load::<ConfigWindow>("sniffnet", "window") {
window
} else {
confy::store("sniffnet", "window", ConfigWindow::default()).unwrap_or(());
ConfigWindow::default()
};

thread::Builder::new()
.name("thread_check_updates".to_string())
.spawn(move || {
Expand All @@ -122,8 +129,8 @@ pub fn main() -> iced::Result {
// id needed for Linux Wayland; should match StartupWMClass in .desktop file; see issue #292
id: Some("sniffnet".to_string()),
window: window::Settings {
size: (1190, 670), // start size
position: Position::Centered,
size: config_window.size, // start size
position: config_window.position.to_position(),
min_size: Some((800, 500)), // min size allowed
max_size: None,
visible: true,
Expand All @@ -144,6 +151,7 @@ pub fn main() -> iced::Result {
&config_settings,
&config_device,
config_advanced_settings,
config_window,
newer_release_available1,
),
default_font: Font::with_name("Sarasa Mono SC"),
Expand Down

0 comments on commit 8e0dea2

Please sign in to comment.