Skip to content

Commit

Permalink
migrating to iced 0.13 (position and size management)
Browse files Browse the repository at this point in the history
  • Loading branch information
GyulyVGC committed Sep 20, 2024
1 parent cc12852 commit 2c15942
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
39 changes: 22 additions & 17 deletions src/configs/types/config_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@ use serde::{Deserialize, Serialize};
#[cfg(not(test))]
use crate::SNIFFNET_LOWERCASE;

#[derive(Serialize, Deserialize, Copy, Clone, PartialEq, Debug)]
pub struct PositionTuple(pub f32, pub f32);
#[derive(Serialize, Deserialize, Copy, Clone, PartialEq, Debug)]
pub struct SizeTuple(pub f32, pub f32);

#[derive(Serialize, Deserialize, Copy, Clone, PartialEq, Debug)]
pub struct ConfigWindow {
pub position: (f32, f32),
pub size: (f32, f32),
pub thumbnail_position: (f32, f32),
pub position: PositionTuple,
pub size: SizeTuple,
pub thumbnail_position: PositionTuple,
}

impl ConfigWindow {
pub const DEFAULT_SIZE: (f32, f32) = (1190.0, 670.0);
const THUMBNAIL_SIZE: (f32, f32) = (360.0, 222.0);
pub const DEFAULT_SIZE: SizeTuple = SizeTuple(1190.0, 670.0);
const THUMBNAIL_SIZE: SizeTuple = SizeTuple(360.0, 222.0);

const MIN_POS_X: f32 = -50.0;
const MIN_POS_Y: f32 = -50.0;
Expand All @@ -41,17 +46,17 @@ impl ConfigWindow {
confy::store(SNIFFNET_LOWERCASE, Self::FILE_NAME, self).unwrap_or(());
}

pub fn thumbnail_size(factor: f64) -> (f32, f32) {
pub fn thumbnail_size(factor: f64) -> SizeTuple {
Self::THUMBNAIL_SIZE.scale_and_check(factor)
}
}

impl Default for ConfigWindow {
fn default() -> Self {
Self {
position: (0.0, 0.0),
position: PositionTuple(0.0, 0.0),
size: ConfigWindow::DEFAULT_SIZE,
thumbnail_position: (0.0, 0.0),
thumbnail_position: PositionTuple(0.0, 0.0),
}
}
}
Expand All @@ -60,7 +65,7 @@ pub trait ToPosition {
fn to_position(self) -> Position;
}

impl ToPosition for (f32, f32) {
impl ToPosition for PositionTuple {
fn to_position(self) -> Position {
Position::Specific(Point {
x: self.0,
Expand All @@ -73,7 +78,7 @@ pub trait ToPoint {
fn to_point(self) -> Point;
}

impl ToPoint for (f32, f32) {
impl ToPoint for PositionTuple {
fn to_point(self) -> Point {
Point {
x: self.0,
Expand All @@ -86,7 +91,7 @@ pub trait ToSize {
fn to_size(self) -> Size;
}

impl ToSize for (f32, f32) {
impl ToSize for SizeTuple {
fn to_size(self) -> Size {
Size {
width: self.0,
Expand All @@ -99,8 +104,8 @@ pub trait ScaleAndCheck {
fn scale_and_check(self, factor: f64) -> Self;
}

impl ScaleAndCheck for (f32, f32) {
fn scale_and_check(self, factor: f64) -> (f32, f32) {
impl ScaleAndCheck for SizeTuple {
fn scale_and_check(self, factor: f64) -> SizeTuple {
let factor = factor as f32;
let mut x = self.0 * factor;
let mut y = self.1 * factor;
Expand All @@ -110,12 +115,12 @@ impl ScaleAndCheck for (f32, f32) {
if y < ConfigWindow::MIN_SIZE_Y {
y = ConfigWindow::MIN_SIZE_Y;
}
(x, y)
SizeTuple(x, y)
}
}

impl ScaleAndCheck for (f32, f32) {
fn scale_and_check(self, factor: f64) -> (f32, f32) {
impl ScaleAndCheck for PositionTuple {
fn scale_and_check(self, factor: f64) -> PositionTuple {
let factor = factor as f32;
let mut x = self.0 * factor;
let mut y = self.1 * factor;
Expand All @@ -131,7 +136,7 @@ impl ScaleAndCheck for (f32, f32) {
if y > ConfigWindow::MAX_POS_Y {
y = ConfigWindow::MAX_POS_Y;
}
(x, y)
PositionTuple(x, y)
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/gui/sniffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ use pcap::Device;
use rfd::FileHandle;

use crate::chart::manage_chart_data::update_charts_data;
use crate::configs::types::config_window::{ConfigWindow, ScaleAndCheck, ToPoint, ToSize};
use crate::configs::types::config_window::{
ConfigWindow, PositionTuple, ScaleAndCheck, SizeTuple, ToPoint, ToSize,
};
use crate::gui::components::footer::footer;
use crate::gui::components::header::header;
use crate::gui::components::modal::{get_clear_all_overlay, get_exit_overlay, new_modal};
Expand Down Expand Up @@ -397,7 +399,7 @@ impl Sniffer {
}
Message::WindowMoved(x, y) => {
let scale_factor = self.configs.lock().unwrap().settings.scale_factor;
let scaled = (x, y).scale_and_check(scale_factor);
let scaled = PositionTuple(x, y).scale_and_check(scale_factor);
if self.thumbnail {
self.configs.lock().unwrap().window.thumbnail_position = scaled;
} else {
Expand All @@ -408,7 +410,7 @@ impl Sniffer {
if !self.thumbnail {
let scale_factor = self.configs.lock().unwrap().settings.scale_factor;
self.configs.lock().unwrap().window.size =
(width, height).scale_and_check(scale_factor);
SizeTuple(width, height).scale_and_check(scale_factor);
} else if !self.timing_events.was_just_thumbnail_enter() {
return self.update(Message::ToggleThumbnail(true));
}
Expand Down

0 comments on commit 2c15942

Please sign in to comment.