refactor(tiles): move selected out of tile
This commit is contained in:
@@ -31,7 +31,6 @@ pub struct Tile {
|
||||
pub descendants: Option<u64>,
|
||||
pub percentage: f64,
|
||||
pub file_type: FileType,
|
||||
pub selected: bool
|
||||
}
|
||||
|
||||
impl Tile {
|
||||
@@ -47,7 +46,6 @@ impl Tile {
|
||||
descendants: file_metadata.descendants,
|
||||
percentage: file_metadata.percentage,
|
||||
file_type: file_metadata.file_type.clone(),
|
||||
selected: false,
|
||||
}
|
||||
}
|
||||
pub fn is_right_of(&self, other: &Tile) -> bool {
|
||||
@@ -134,7 +132,7 @@ impl Tile {
|
||||
|
||||
pub struct Board {
|
||||
pub tiles: Vec<Tile>,
|
||||
selected_index: Option<usize>, // None means nothing is selected
|
||||
pub selected_index: Option<usize>, // None means nothing is selected
|
||||
area: Option<Rect>,
|
||||
files: Vec<FileMetadata>,
|
||||
}
|
||||
@@ -211,21 +209,10 @@ impl Board {
|
||||
let mut tree_map = TreeMap::new(empty_space);
|
||||
|
||||
tree_map.squarify(self.files.iter().collect(), vec![]); // TODO: do not clone
|
||||
let mut tiles = tree_map.tiles;
|
||||
if let Some(selected_index) = self.selected_index {
|
||||
let mut selected_tile = tiles.get_mut(selected_index).expect(&format!("could not find selected rect at index {}", selected_index));
|
||||
selected_tile.selected = true;
|
||||
}
|
||||
self.tiles = tiles;
|
||||
self.tiles = tree_map.tiles;
|
||||
}
|
||||
}
|
||||
pub fn set_selected_index (&mut self, next_index: &usize) {
|
||||
if let Some(selected_index) = self.selected_index {
|
||||
let mut existing_selected = self.tiles.get_mut(selected_index).expect(&format!("could not find selected rect at index {}", selected_index));
|
||||
existing_selected.selected = false;
|
||||
}
|
||||
let mut next_selected = self.tiles.get_mut(*next_index).expect(&format!("could not find selected rect at index {}", next_index));
|
||||
next_selected.selected = true;
|
||||
self.selected_index = Some(*next_index);
|
||||
}
|
||||
pub fn has_selected_index (&self) -> bool {
|
||||
@@ -235,10 +222,6 @@ impl Board {
|
||||
}
|
||||
}
|
||||
pub fn reset_selected_index (&mut self) {
|
||||
if let Some(selected_index) = self.selected_index {
|
||||
let mut existing_selected = self.tiles.get_mut(selected_index).expect(&format!("could not find selected rect at index {}", selected_index));
|
||||
existing_selected.selected = false;
|
||||
}
|
||||
self.selected_index = None;
|
||||
}
|
||||
pub fn currently_selected (&self) -> Option<&Tile> {
|
||||
|
||||
@@ -68,7 +68,7 @@ where B: Backend
|
||||
.path_error(ui_effects.current_path_is_red)
|
||||
.is_loading()
|
||||
.render(&mut f, chunks[0]);
|
||||
RectangleGrid::new(&board.tiles).render(&mut f, chunks[1]);
|
||||
RectangleGrid::new(&board.tiles, board.selected_index).render(&mut f, chunks[1]);
|
||||
BottomLine::new(file_tree.failed_to_read)
|
||||
.currently_selected(board.currently_selected())
|
||||
.last_read_path(ui_effects.last_read_path.as_ref())
|
||||
@@ -80,7 +80,7 @@ where B: Backend
|
||||
.path_error(ui_effects.current_path_is_red)
|
||||
.flash_space(ui_effects.frame_around_space_freed)
|
||||
.render(&mut f, chunks[0]);
|
||||
RectangleGrid::new(&board.tiles).render(&mut f, chunks[1]);
|
||||
RectangleGrid::new(&board.tiles, board.selected_index).render(&mut f, chunks[1]);
|
||||
BottomLine::new(file_tree.failed_to_read).currently_selected(board.currently_selected()).render(&mut f, chunks[2]);
|
||||
},
|
||||
UiMode::ScreenTooSmall => {
|
||||
@@ -90,7 +90,7 @@ where B: Backend
|
||||
TitleLine::new(base_path_info, current_path_info, file_tree.space_freed)
|
||||
.path_error(ui_effects.current_path_is_red)
|
||||
.render(&mut f, chunks[0]);
|
||||
RectangleGrid::new(&board.tiles).render(&mut f, chunks[1]);
|
||||
RectangleGrid::new(&board.tiles, board.selected_index).render(&mut f, chunks[1]);
|
||||
BottomLine::new(file_tree.failed_to_read).currently_selected(board.currently_selected()).render(&mut f, chunks[2]);
|
||||
MessageBox::new(file_to_delete, ui_effects.deletion_in_progress).render(&mut f, full_screen);
|
||||
},
|
||||
@@ -99,7 +99,7 @@ where B: Backend
|
||||
.path_error(ui_effects.current_path_is_red)
|
||||
.flash_space(ui_effects.frame_around_space_freed)
|
||||
.render(&mut f, chunks[0]);
|
||||
RectangleGrid::new(&board.tiles).render(&mut f, chunks[1]);
|
||||
RectangleGrid::new(&board.tiles, board.selected_index).render(&mut f, chunks[1]);
|
||||
BottomLine::new(file_tree.failed_to_read).currently_selected(board.currently_selected()).render(&mut f, chunks[2]);
|
||||
ErrorBox::new(message).render(&mut f, full_screen);
|
||||
}
|
||||
|
||||
@@ -15,12 +15,13 @@ pub const MINIMUM_WIDTH: u16 = 8;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct RectangleGrid<'a> {
|
||||
rectangles: &'a [Tile]
|
||||
rectangles: &'a [Tile],
|
||||
selected_rect_index: Option<usize>,
|
||||
}
|
||||
|
||||
impl<'a> RectangleGrid<'a> {
|
||||
pub fn new (rectangles: &'a [Tile]) -> Self {
|
||||
RectangleGrid { rectangles }
|
||||
pub fn new (rectangles: &'a [Tile], selected_rect_index: Option<usize>) -> Self {
|
||||
RectangleGrid { rectangles, selected_rect_index }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,13 +76,13 @@ fn draw_small_files_rect_on_grid(buf: &mut Buffer, rect: Rect) {
|
||||
}
|
||||
}
|
||||
|
||||
fn draw_rect_text_on_grid(buf: &mut Buffer, tile: &Tile) { // TODO: better, combine args
|
||||
fn draw_rect_text_on_grid(buf: &mut Buffer, tile: &Tile, selected: bool) { // TODO: better, combine args
|
||||
let max_text_length = if tile.width > 2 { tile.width - 2 } else { 0 };
|
||||
let name = &tile.name.to_string_lossy();
|
||||
let descendant_count = &tile.descendants;
|
||||
let percentage = &tile.percentage;
|
||||
|
||||
let filename_text = if tile.selected {
|
||||
let filename_text = if selected {
|
||||
match tile.file_type {
|
||||
FileType::File => format!("{}", name),
|
||||
FileType::Folder => format!("{}/", name),
|
||||
@@ -117,7 +118,7 @@ fn draw_rect_text_on_grid(buf: &mut Buffer, tile: &Tile) { // TODO: better, comb
|
||||
let second_line_length = second_line.len(); // TODO: better
|
||||
let second_line_start_position = ((tile.width - second_line_length as u16) as f64 / 2.0).ceil() as u16 + tile.x;
|
||||
|
||||
let ( background_style, first_line_style, second_line_style ) = match ( tile.selected, &tile.file_type ) {
|
||||
let ( background_style, first_line_style, second_line_style ) = match ( selected, &tile.file_type ) {
|
||||
( true, FileType::File ) => {
|
||||
(
|
||||
Some(Style::default().fg(Color::DarkGray).bg(Color::DarkGray)),
|
||||
@@ -271,8 +272,13 @@ impl<'a> Widget for RectangleGrid<'a> {
|
||||
let text_start_position = ((area.width - text_length as u16) as f64 / 2.0).ceil() as u16 + area.x;
|
||||
buf.set_string(text_start_position, (area.height / 2) + area.y - 1, empty_folder_line, text_style);
|
||||
} else {
|
||||
for tile in self.rectangles {
|
||||
for (index, tile) in self.rectangles.into_iter().enumerate() {
|
||||
|
||||
let selected = if let Some(selected_rect_index) = self.selected_rect_index {
|
||||
index == selected_rect_index
|
||||
} else {
|
||||
false
|
||||
};
|
||||
if tile.height < MINIMUM_HEIGHT || tile.width < MINIMUM_WIDTH {
|
||||
small_files.add_rect(&tile);
|
||||
} else if tile.height < MINIMUM_HEIGHT || tile.width < MINIMUM_WIDTH {
|
||||
@@ -281,7 +287,7 @@ impl<'a> Widget for RectangleGrid<'a> {
|
||||
// TODO: fix this properly, probably by refactoring Board to do the rounding
|
||||
// itself
|
||||
} else {
|
||||
draw_rect_text_on_grid(buf, &tile);
|
||||
draw_rect_text_on_grid(buf, &tile, selected);
|
||||
draw_rect_on_grid(buf, &tile);
|
||||
}
|
||||
}
|
||||
@@ -297,7 +303,6 @@ impl<'a> Widget for RectangleGrid<'a> {
|
||||
descendants: None,
|
||||
percentage: 0.0,
|
||||
file_type: FileType::Folder,
|
||||
selected: false
|
||||
}); // draw a frame around the whole area (to properly support the small files and empty folder cases)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user