Skip to content

Commit

Permalink
Merge pull request #12 from gg0074x/create_dir_all
Browse files Browse the repository at this point in the history
Fixed saving error
  • Loading branch information
SergioRibera authored Jun 2, 2024
2 parents 64a8e42 + 5553821 commit 54ffed1
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 27 deletions.
29 changes: 10 additions & 19 deletions src/data.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fs::File;
use std::fs::{create_dir_all, File};
use std::io::Read;

use abomonation::{decode, encode};
use abomonation::{decode, encode, Abomonation};
use app_dirs2::{data_root, AppDataType};

use crate::settings::{AppSettings, PinnedClipboard};
Expand All @@ -28,21 +28,6 @@ pub fn load_settings() -> AppSettings {
AppSettings::default()
}

pub fn save_settings(value: &AppSettings) {
if let Ok(mut path) = data_root(AppDataType::UserConfig) {
path.push("super_clipboard");
path.push("settings");
path.set_extension("data");

log::info!("Settings saved into \"{path:?}\"");
if let Ok(mut file) = File::create(path) {
unsafe {
encode(value, &mut file).unwrap();
}
}
}
}

#[must_use]
pub fn load_pined() -> PinnedClipboard {
let Ok(mut path) = data_root(AppDataType::UserConfig) else {
Expand All @@ -65,11 +50,17 @@ pub fn load_pined() -> PinnedClipboard {
PinnedClipboard::default()
}

pub fn save_pined(value: &PinnedClipboard) {
pub fn save<T: Abomonation>(value: &T, ext: &str) {
if let Ok(mut path) = data_root(AppDataType::UserConfig) {
path.push("super_clipboard");

if let Err(e) = create_dir_all(&path) {
log::error!("An error ocurred when creating the config directory: {e}");
return;
}

path.push("settings");
path.set_extension("pined");
path.set_extension(ext);

log::info!("Settings saved into \"{path:?}\"");
if let Ok(mut file) = File::create(path) {
Expand Down
2 changes: 0 additions & 2 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ use arboard::ImageData;
use chrono::prelude::*;
use clap::ValueEnum;

use crate::data::save_pined;

#[derive(Abomonation, Clone, Eq, PartialEq)]
pub struct AppSettings {
max_capacity: u64,
Expand Down
2 changes: 1 addition & 1 deletion src/ui/styles/container_style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl container::StyleSheet for ItemContainer {
r: bg.r,
g: bg.g,
b: bg.b,
a: bg.a + 0.025,
a: bg.a,
})),
border_radius: 0.0.into(),
border_width: 0.0,
Expand Down
8 changes: 4 additions & 4 deletions src/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use iced::widget::scrollable;
use iced::{window, Command};
use log::trace;

use crate::data::{save_pined, save_settings};
use crate::data::save;
use crate::ui::RouterView;
use crate::{
settings::ClipboardItem,
Expand Down Expand Up @@ -123,16 +123,16 @@ pub fn handle_update(app: &mut MainApp, message: MainMessage) -> Command<MainMes
MainMessage::CheckSettings(_) => {
if app.pinned.is_changed {
app.pinned.is_changed = false;
save_pined(&app.pinned);
save(&app.pinned, "pined");
}
if app.settings.is_changed {
app.settings.is_changed = false;
if !app.settings.store() {
let mut set = app.settings.clone();
set.set_clipboard(Vec::new());
save_settings(&set);
save(&set, "data");
} else {
save_settings(&app.settings);
save(&app.settings, "data");
}
}
Command::none()
Expand Down
11 changes: 10 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,16 @@ pub enum Message {
pub fn track_mouse(old: (i32, i32), coords: (i32, i32)) -> (i32, i32) {
let (old_x, old_y) = old;
let (x, y) = coords;
let Ok(DisplayInfo { width, height, x: display_x, y: display_y, .. }) = DisplayInfo::from_point(x, y) else { return coords; };
let Ok(DisplayInfo {
width,
height,
x: display_x,
y: display_y,
..
}) = DisplayInfo::from_point(x, y)
else {
return coords;
};

if old_x == x || old_y == y {
return coords;
Expand Down

0 comments on commit 54ffed1

Please sign in to comment.