Skip to content

Commit

Permalink
add option to save window position and restore it from config file
Browse files Browse the repository at this point in the history
  • Loading branch information
BKSalman committed Aug 20, 2024
1 parent b71f973 commit 2a698f1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
23 changes: 22 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use iced::{
widget::{button, checkbox, column, progress_bar, row, text, text_input},
Application, Length, Subscription,
};
use iced::{window, Event};
use iced::{window, Event, Point};
use iced_aw::Card;

use rfd::AsyncFileDialog;
Expand Down Expand Up @@ -67,10 +67,19 @@ pub enum Message {
FontLoaded(Result<(), iced::font::Error>),
}

#[derive(Debug, Deserialize, Serialize)]
pub struct WindowPosition {
pub x: f32,
pub y: f32,
}

#[derive(Debug, Default, Deserialize, Serialize)]
pub struct Config {
bin_dir: Option<PathBuf>,
download_folder: Option<PathBuf>,
#[serde(default)]
pub save_window_position: bool,
pub window_position: Option<WindowPosition>,
options: Options,
}

Expand Down Expand Up @@ -108,6 +117,7 @@ pub struct YtGUI {
progress: f32,
window_height: f32,
window_width: f32,
window_pos: Point,
}

impl YtGUI {
Expand Down Expand Up @@ -317,6 +327,7 @@ impl Application for YtGUI {
window_height: 0.,
window_width: 0.,
is_choosing_folder: false,
window_pos: Point::default(),
},
iced::font::load(iced_aw::BOOTSTRAP_FONT_BYTES).map(Message::FontLoaded),
)
Expand Down Expand Up @@ -478,6 +489,13 @@ impl Application for YtGUI {
if self.command.kill().is_ok() {
tracing::debug!("killed child process");
}
self.config.window_position = Some(WindowPosition {
x: self.window_pos.x,
y: self.window_pos.y,
});
if let Err(e) = self.config.update_config_file() {
tracing::error!("Failed to update config file: {e}");
}
return iced::Command::single(iced_runtime::command::Action::Window(
Action::Close(id),
));
Expand All @@ -486,6 +504,9 @@ impl Application for YtGUI {
self.window_width = width as f32;
self.window_height = height as f32;
}
window::Event::Moved { x, y } if self.config.save_window_position => {
self.window_pos = Point::new(x as f32, y as f32);
}
_ => {}
}
}
Expand Down
16 changes: 15 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

use iced::{window, Application, Settings};
use iced::{
window::{self, Position},
Application, Point, Settings,
};
use ytdlp_gui::{git_hash, logging, Config, YtGUI};

fn main() -> iced::Result {
Expand Down Expand Up @@ -55,12 +58,23 @@ fn main() -> iced::Result {
config
});

let position = if config.save_window_position {
if let Some(window_pos) = &config.window_position {
Position::Specific(Point::new(window_pos.x, window_pos.y))
} else {
Position::default()
}
} else {
Position::default()
};

let settings = Settings {
id: Some(String::from("ytdlp-gui")),
window: window::Settings {
size: iced::Size::new(600., 360.),
resizable: true,
exit_on_close_request: false,
position,
..Default::default()
},
flags: config,
Expand Down

0 comments on commit 2a698f1

Please sign in to comment.