Skip to content

Commit

Permalink
add grid persistence (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
tarkah committed May 8, 2020
1 parent ab951d0 commit b89758a
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 5 deletions.
27 changes: 23 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ config = { version = "0.10", default-features=false, features = ['yaml'] }
dirs = "2.0.2"
lazy_static = "1.4"
serde = { version = "1.0", features = ['derive'] }
serde_yaml = "0.8"

[dependencies.winapi]
version = "0.3"
Expand Down
94 changes: 93 additions & 1 deletion src/grid.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use std::collections::HashMap;
use std::fs;
use std::mem;

use serde::{Deserialize, Serialize};

use winapi::shared::windef::{HBRUSH, HDC};
use winapi::um::wingdi::{CreateSolidBrush, DeleteObject, RGB};
use winapi::um::winuser::{BeginPaint, EndPaint, FillRect, FrameRect, PAINTSTRUCT};
Expand All @@ -23,6 +27,68 @@ pub struct Grid {
zone_margins: u8,
border_margins: u8,
tiles: Vec<Vec<Tile>>, // tiles[row][column]
active_config: String,
configs: GridConfigs,
}

#[derive(Serialize, Deserialize, Clone, Copy)]
pub struct GridConfig {
rows: usize,
columns: usize,
}

impl Default for GridConfig {
fn default() -> Self {
GridConfig {
rows: 2,
columns: 2,
}
}
}

pub type GridConfigs = HashMap<String, GridConfig>;
pub trait GridCache {
fn load() -> GridConfigs;
fn save(&self);
}

impl GridCache for GridConfigs {
fn load() -> GridConfigs {
if let Some(mut config_path) = dirs::config_dir() {
config_path.push("grout");
config_path.push("cache");

if !config_path.exists() {
let _ = fs::create_dir_all(&config_path);
}

config_path.push("grid.yml");

let mut config = config::Config::default();

let file_config = config::File::from(config_path).format(config::FileFormat::Yaml);

if let Ok(config) = config.merge(file_config) {
return config.clone().try_into().unwrap_or_default();
}
}

let mut config = HashMap::new();
config.insert("Default".to_owned(), GridConfig::default());
config
}

fn save(&self) {
if let Some(mut config_path) = dirs::config_dir() {
config_path.push("grout");
config_path.push("cache");
config_path.push("grid.yml");

if let Ok(serialized) = serde_yaml::to_string(&self) {
let _ = fs::write(config_path, serialized);
}
}
}
}

impl From<Config> for Grid {
Expand All @@ -37,6 +103,14 @@ impl From<Config> for Grid {

impl Default for Grid {
fn default() -> Self {
let configs = GridConfigs::load();
let active_config = "Default".to_owned();

let default_config = configs.get(&active_config).cloned().unwrap_or_default();

let rows = default_config.rows;
let columns = default_config.columns;

Grid {
shift_down: false,
control_down: false,
Expand All @@ -48,7 +122,9 @@ impl Default for Grid {
grid_margins: 3,
zone_margins: 10,
border_margins: 10,
tiles: vec![vec![Tile::default(); 2]; 2],
tiles: vec![vec![Tile::default(); columns]; rows],
active_config,
configs,
}
}
}
Expand All @@ -68,6 +144,18 @@ impl Grid {
});
}

fn save_config(&mut self) {
let rows = self.rows();
let columns = self.columns();

if let Some(grid_config) = self.configs.get_mut(&self.active_config) {
grid_config.rows = rows;
grid_config.columns = columns;
}

self.configs.save();
}

pub fn dimensions(&self) -> (u32, u32) {
let width = self.columns() as u32 * TILE_WIDTH
+ (self.columns() as u32 + 1) * self.grid_margins as u32;
Expand Down Expand Up @@ -115,18 +203,21 @@ impl Grid {

pub fn add_row(&mut self) {
self.tiles.push(vec![Tile::default(); self.columns()]);
self.save_config();
}

pub fn add_column(&mut self) {
for row in self.tiles.iter_mut() {
row.push(Tile::default());
}
self.save_config();
}

pub fn remove_row(&mut self) {
if self.rows() > 1 {
self.tiles.pop();
}
self.save_config();
}

pub fn remove_column(&mut self) {
Expand All @@ -135,6 +226,7 @@ impl Grid {
row.pop();
}
}
self.save_config();
}

fn tile_area(&self, row: usize, column: usize) -> Rect {
Expand Down

0 comments on commit b89758a

Please sign in to comment.