Skip to content

Commit

Permalink
Revamp of node colors, now the files and folders are colored differently
Browse files Browse the repository at this point in the history
  • Loading branch information
salihgerdan committed Mar 5, 2023
1 parent 295a59e commit bd501cf
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 71 deletions.
Binary file modified screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 7 additions & 4 deletions src/filetree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub struct Node {
pub size: u64,
pub name: String,
pub depth: u64,
pub is_file: bool,
pub parent: Option<NodeID>,
pub children: Vec<NodeID>,
}
Expand Down Expand Up @@ -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![],
};
Expand Down Expand Up @@ -84,14 +86,15 @@ pub fn walk_into_tree(tree_mutex: Arc<Mutex<Tree>>) -> 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;
Expand All @@ -101,7 +104,7 @@ pub fn walk_into_tree(tree_mutex: Arc<Mutex<Tree>>) -> 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;
Expand Down
8 changes: 5 additions & 3 deletions src/gui/treemap_widget/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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!(
"{} ({})",
Expand Down
111 changes: 47 additions & 64 deletions src/gui/treemap_widget/imp/node_color.rs
Original file line number Diff line number Diff line change
@@ -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::<Vec<RGBA>>()
.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::<Vec<RGBA>>()
.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]
}

0 comments on commit bd501cf

Please sign in to comment.