diff --git a/screenshot.png b/screenshot.png index dad30d1..a0ad8ad 100644 Binary files a/screenshot.png and b/screenshot.png differ diff --git a/src/filetree.rs b/src/filetree.rs index ad333b3..fad66ee 100644 --- a/src/filetree.rs +++ b/src/filetree.rs @@ -9,6 +9,7 @@ pub struct Node { pub size: u64, pub name: String, pub depth: u64, + pub is_file: bool, pub parent: Option, pub children: Vec, } @@ -37,13 +38,14 @@ impl Tree { node = p; } } - pub fn add_elem(&mut self, parent: NodeID, name: String, size: u64) { + pub fn add_elem(&mut self, parent: NodeID, name: String, is_file: bool, size: u64) { self.last_id += 1; let node = Node { id: self.last_id, name, size, depth: self.elems[parent].depth + 1, + is_file, parent: Some(parent), children: vec![], }; @@ -84,14 +86,15 @@ pub fn walk_into_tree(tree_mutex: Arc>) -> jwalk::Result<()> { let e = entry?; let file_size = e.metadata()?.len(); let file_name = e.file_name.into_string().unwrap_or_default(); + let is_file = e.file_type.is_file(); { // we lock and unlock this at every item, so the gui thread can grab it easily let mut tree = tree_mutex.lock().unwrap(); if e.depth > last_depth { - tree.add_elem(last_node, file_name, file_size); + tree.add_elem(last_node, file_name, is_file, file_size); } else if e.depth == last_depth { if let Some(parent) = tree.get_elem(last_node).parent { - tree.add_elem(parent, file_name, file_size); + tree.add_elem(parent, file_name, is_file, file_size); } } else { let mut parent = last_node; @@ -101,7 +104,7 @@ pub fn walk_into_tree(tree_mutex: Arc>) -> jwalk::Result<()> { None => parent, // we never get here I guess } } - tree.add_elem(parent, file_name, file_size); + tree.add_elem(parent, file_name, is_file, file_size); } last_depth = e.depth; last_node = tree.last_id; diff --git a/src/gui/treemap_widget/imp.rs b/src/gui/treemap_widget/imp.rs index 28eed22..d71f273 100644 --- a/src/gui/treemap_widget/imp.rs +++ b/src/gui/treemap_widget/imp.rs @@ -10,7 +10,6 @@ use gtk::pango; use gtk::prelude::*; use gtk::subclass::prelude::*; use gtk::Tooltip; -use node_color::NodeColor; use once_cell::sync::Lazy; use std::cell::RefCell; use std::collections::HashMap; @@ -180,8 +179,11 @@ fn update_rects( pango_context: &pango::Context, ) { if let Some(gui_node) = gui_node_map.get(&node.id) { - let color = NodeColor::depth_color(node.depth as usize); - snapshot.append_color(&color.get_rgba(), &gui_node.rect); + let color = match node.is_file { + false => node_color::depth_dir_color(node.depth as usize), + true => node_color::depth_file_color(node.depth as usize), + }; + snapshot.append_color(&color, &gui_node.rect); let layout = pango::Layout::new(pango_context); layout.set_text(&format!( "{} ({})", diff --git a/src/gui/treemap_widget/imp/node_color.rs b/src/gui/treemap_widget/imp/node_color.rs index 8a21464..b633e89 100644 --- a/src/gui/treemap_widget/imp/node_color.rs +++ b/src/gui/treemap_widget/imp/node_color.rs @@ -1,70 +1,53 @@ use gtk::gdk::RGBA; +use once_cell::sync::Lazy; + // https://www.schemecolor.com/light-to-dark-blue.php -#[derive(Copy, Clone, PartialEq)] -pub enum NodeColor { - Blue1, - Blue2, - Blue3, - Blue4, - Blue5, -} +// light blue #bcd2e8 +const DIR_R: f32 = 0xbc as f32 / 256.0; +const DIR_G: f32 = 0xd2 as f32 / 256.0; +const DIR_B: f32 = 0xe8 as f32 / 256.0; +const DIR_A: f32 = 0xff as f32 / 256.0; + +// bisque color #ffc0cb +const FILE_R: f32 = 0xff as f32 / 256.0; +const FILE_G: f32 = 0xc0 as f32 / 256.0; +const FILE_B: f32 = 0xcb as f32 / 256.0; +const FILE_A: f32 = 0xff as f32 / 256.0; -// TODO: get once_cell statics here to generate RGBA only once -impl NodeColor { - pub fn get_rgba(&self) -> RGBA { - match *self { - NodeColor::Blue1 => RGBA::new( - 0xbc as f32 / 256.0, - 0xd2 as f32 / 256.0, - 0xe8 as f32 / 256.0, - 0xff as f32 / 256.0, - ), - NodeColor::Blue2 => RGBA::new( - 0x91 as f32 / 256.0, - 0xba as f32 / 256.0, - 0xd6 as f32 / 256.0, - 0xff as f32 / 256.0, - ), - NodeColor::Blue3 => RGBA::new( - 0x73 as f32 / 256.0, - 0xa5 as f32 / 256.0, - 0xc6 as f32 / 256.0, - 0xff as f32 / 256.0, - ), - NodeColor::Blue4 => RGBA::new( - 0x52 as f32 / 256.0, - 0x8a as f32 / 256.0, - 0xae as f32 / 256.0, - 0xff as f32 / 256.0, - ), - NodeColor::Blue5 => RGBA::new( - 0x2e as f32 / 256.0, - 0x59 as f32 / 256.0, - 0x84 as f32 / 256.0, - 0xff as f32 / 256.0, - ), - } - } +static DIR: Lazy<[RGBA; 5]> = Lazy::new(|| { + (0..5) + .map(|depth| { + RGBA::new( + DIR_R * (1.1 * DIR_R / 1.0).powi(depth as i32), + DIR_G * (1.1 * DIR_G / 1.0).powi(depth as i32), + DIR_B * (1.1 * DIR_B / 1.0).powi(depth as i32), + DIR_A, + ) + }) + .collect::>() + .try_into() + .unwrap() +}); - /*pub fn next_color(&self) -> Self { - match *self { - NodeColor::Blue1 => NodeColor::Blue2, - NodeColor::Blue2 => NodeColor::Blue3, - NodeColor::Blue3 => NodeColor::Blue4, - NodeColor::Blue4 => NodeColor::Blue5, - NodeColor::Blue5 => NodeColor::Blue1, - } - }*/ +static FILE: Lazy<[RGBA; 5]> = Lazy::new(|| { + (0..5) + .map(|depth| { + RGBA::new( + FILE_R * (1.1 * FILE_R / 1.0).powi(depth as i32), + FILE_G * (1.1 * FILE_G / 1.0).powi(depth as i32), + FILE_B * (1.1 * FILE_B / 1.0).powi(depth as i32), + FILE_A, + ) + }) + .collect::>() + .try_into() + .unwrap() +}); + +pub fn depth_dir_color(depth: usize) -> RGBA { + DIR[depth % 5] +} - pub fn depth_color(mut depth: usize) -> Self { - depth %= 5; - match depth { - 0 => NodeColor::Blue1, - 1 => NodeColor::Blue2, - 2 => NodeColor::Blue3, - 3 => NodeColor::Blue4, - 4 => NodeColor::Blue5, - _ => NodeColor::Blue1, - } - } +pub fn depth_file_color(depth: usize) -> RGBA { + FILE[depth % 5] }