cosmic-files/src/tab.rs

2050 lines
74 KiB
Rust
Raw Normal View History

2024-01-03 15:27:32 -07:00
use cosmic::{
cosmic_theme,
iced::{
alignment::{Horizontal, Vertical},
2024-02-22 16:17:39 -07:00
futures::SinkExt,
keyboard::Modifiers,
2024-02-22 16:17:39 -07:00
subscription::{self, Subscription},
//TODO: export in cosmic::widget
2024-02-29 16:25:54 -07:00
widget::{
horizontal_rule,
scrollable::{AbsoluteOffset, Viewport},
},
Alignment,
2024-02-29 11:25:46 -07:00
Color,
2024-02-22 21:34:21 -07:00
ContentFit,
Length,
Point,
2024-02-26 12:05:29 -07:00
Rectangle,
Size,
},
2024-01-03 15:27:32 -07:00
theme, widget, Element,
};
2024-02-22 15:04:37 -07:00
use mime_guess::MimeGuess;
2024-01-24 09:30:06 -05:00
use once_cell::sync::Lazy;
2024-01-03 15:27:32 -07:00
use std::{
2024-02-29 11:25:46 -07:00
cell::Cell,
2024-01-03 15:27:32 -07:00
cmp::Ordering,
2024-01-04 15:56:01 -07:00
collections::HashMap,
2024-01-05 14:44:20 -07:00
fmt,
fs::{self, Metadata},
2024-01-03 15:27:32 -07:00
path::PathBuf,
time::{Duration, Instant},
};
use crate::{
2024-02-26 12:05:29 -07:00
app::Action,
config::{IconSizes, TabConfig},
dialog::DialogKind,
fl,
key_bind::KeyBind,
menu,
mime_icon::mime_icon,
2024-02-26 12:05:29 -07:00
mouse_area,
};
2024-01-03 15:27:32 -07:00
const DOUBLE_CLICK_DURATION: Duration = Duration::from_millis(500);
2024-02-29 11:25:46 -07:00
const GRID_ITEM_HEIGHT: usize = 116;
const GRID_ITEM_WIDTH: usize = 96;
2024-02-22 15:04:37 -07:00
//TODO: adjust for locales?
const TIME_FORMAT: &'static str = "%a %-d %b %-Y %r";
2024-01-24 09:30:06 -05:00
static SPECIAL_DIRS: Lazy<HashMap<PathBuf, &'static str>> = Lazy::new(|| {
let mut special_dirs = HashMap::new();
if let Some(dir) = dirs::document_dir() {
special_dirs.insert(dir, "folder-documents");
}
if let Some(dir) = dirs::download_dir() {
special_dirs.insert(dir, "folder-download");
}
if let Some(dir) = dirs::audio_dir() {
special_dirs.insert(dir, "folder-music");
}
if let Some(dir) = dirs::picture_dir() {
special_dirs.insert(dir, "folder-pictures");
}
if let Some(dir) = dirs::public_dir() {
special_dirs.insert(dir, "folder-publicshare");
}
if let Some(dir) = dirs::template_dir() {
special_dirs.insert(dir, "folder-templates");
}
if let Some(dir) = dirs::video_dir() {
special_dirs.insert(dir, "folder-videos");
}
if let Some(dir) = dirs::desktop_dir() {
special_dirs.insert(dir, "user-desktop");
}
if let Some(dir) = dirs::home_dir() {
special_dirs.insert(dir, "user-home");
}
special_dirs
});
2024-02-29 11:25:46 -07:00
fn button_appearance(
theme: &theme::Theme,
selected: bool,
2024-02-29 15:29:10 -07:00
focused: bool,
2024-02-29 11:25:46 -07:00
accent: bool,
) -> widget::button::Appearance {
let cosmic = theme.cosmic();
let mut appearance = widget::button::Appearance::new();
if selected {
if accent {
appearance.background = Some(Color::from(cosmic.accent_color()).into());
appearance.icon_color = Some(Color::from(cosmic.on_accent_color()));
appearance.text_color = Some(Color::from(cosmic.on_accent_color()));
} else {
appearance.background = Some(Color::from(cosmic.bg_component_color()).into());
}
}
2024-02-29 15:29:10 -07:00
if focused && accent {
appearance.outline_width = 1.0;
appearance.outline_color = Color::from(cosmic.accent_color());
appearance.border_width = 2.0;
appearance.border_color = Color::TRANSPARENT;
}
2024-02-29 11:25:46 -07:00
appearance.border_radius = cosmic.radius_s().into();
appearance
}
fn button_style(selected: bool, accent: bool) -> theme::Button {
//TODO: move to libcosmic?
2024-01-05 09:36:16 -07:00
theme::Button::Custom {
2024-02-29 15:29:10 -07:00
active: Box::new(move |focused, theme| button_appearance(theme, selected, focused, accent)),
disabled: Box::new(move |theme| button_appearance(theme, selected, false, accent)),
2024-01-05 09:36:16 -07:00
hovered: Box::new(move |focused, theme| {
2024-02-29 15:29:10 -07:00
button_appearance(theme, selected, focused, accent)
2024-01-05 09:36:16 -07:00
}),
pressed: Box::new(move |focused, theme| {
2024-02-29 15:29:10 -07:00
button_appearance(theme, selected, focused, accent)
2024-01-05 09:36:16 -07:00
}),
}
}
2024-01-09 15:34:48 -07:00
pub fn folder_icon(path: &PathBuf, icon_size: u16) -> widget::icon::Handle {
2024-01-04 15:56:01 -07:00
widget::icon::from_name(SPECIAL_DIRS.get(path).map_or("folder", |x| *x))
.size(icon_size)
.handle()
2024-01-04 15:56:01 -07:00
}
2024-01-09 15:34:48 -07:00
pub fn folder_icon_symbolic(path: &PathBuf, icon_size: u16) -> widget::icon::Handle {
widget::icon::from_name(format!(
"{}-symbolic",
SPECIAL_DIRS.get(path).map_or("folder", |x| *x)
))
.size(icon_size)
.handle()
}
pub fn trash_icon_symbolic(icon_size: u16) -> widget::icon::Handle {
2024-01-10 12:57:30 -07:00
let full = match trash::os_limited::list() {
Ok(entries) => !entries.is_empty(),
Err(_err) => false,
};
widget::icon::from_name(if full {
"user-trash-full-symbolic"
} else {
"user-trash-symbolic"
})
.size(icon_size)
.handle()
}
2024-01-05 15:32:42 -07:00
//TODO: translate, add more levels?
fn format_size(size: u64) -> String {
2024-01-29 10:34:09 -07:00
const KB: u64 = 1000;
const MB: u64 = 1000 * KB;
const GB: u64 = 1000 * MB;
const TB: u64 = 1000 * GB;
2024-01-05 15:32:42 -07:00
2024-01-29 10:34:09 -07:00
if size >= TB {
format!("{:.1} TB", size as f64 / TB as f64)
} else if size >= GB {
format!("{:.1} GB", size as f64 / GB as f64)
} else if size >= MB {
format!("{:.1} MB", size as f64 / MB as f64)
} else if size >= KB {
format!("{:.1} KB", size as f64 / KB as f64)
2024-01-05 15:32:42 -07:00
} else {
format!("{} B", size)
}
}
2024-01-04 16:08:22 -07:00
#[cfg(not(target_os = "windows"))]
2024-01-05 14:44:20 -07:00
fn hidden_attribute(_metadata: &Metadata) -> bool {
2024-01-04 16:08:22 -07:00
false
}
#[cfg(target_os = "windows")]
2024-01-05 14:44:20 -07:00
fn hidden_attribute(metadata: &Metadata) -> bool {
2024-01-04 16:08:22 -07:00
use std::os::windows::fs::MetadataExt;
2024-01-05 14:44:20 -07:00
// https://learn.microsoft.com/en-us/windows/win32/fileio/file-attribute-constants
const FILE_ATTRIBUTE_HIDDEN: u32 = 2;
metadata.file_attributes() & FILE_ATTRIBUTE_HIDDEN == FILE_ATTRIBUTE_HIDDEN
2024-01-04 16:08:22 -07:00
}
pub fn scan_path(tab_path: &PathBuf, sizes: IconSizes) -> Vec<Item> {
let mut items = Vec::new();
2024-01-05 16:17:23 -07:00
match fs::read_dir(tab_path) {
Ok(entries) => {
for entry_res in entries {
let entry = match entry_res {
Ok(ok) => ok,
Err(err) => {
log::warn!("failed to read entry in {:?}: {}", tab_path, err);
continue;
}
};
let name = match entry.file_name().into_string() {
2024-01-05 14:44:20 -07:00
Ok(ok) => ok,
Err(name_os) => {
log::warn!(
"failed to parse entry in {:?}: {:?} is not valid UTF-8",
tab_path,
name_os,
);
continue;
}
};
2024-01-05 14:44:20 -07:00
let metadata = match entry.metadata() {
Ok(ok) => ok,
Err(err) => {
log::warn!(
"failed to read metadata for entry in {:?}: {}",
tab_path,
err
);
continue;
}
};
let hidden = name.starts_with(".") || hidden_attribute(&metadata);
let path = entry.path();
2024-01-05 14:44:20 -07:00
2024-02-22 15:04:37 -07:00
let mime_guess = MimeGuess::from_path(&path);
let (icon_handle_grid, icon_handle_list) = if metadata.is_dir() {
2024-01-05 09:36:16 -07:00
(
folder_icon(&path, sizes.grid()),
folder_icon(&path, sizes.list()),
2024-01-05 09:36:16 -07:00
)
} else {
2024-01-05 09:36:16 -07:00
(
mime_icon(&path, sizes.grid()),
mime_icon(&path, sizes.list()),
2024-01-05 09:36:16 -07:00
)
};
let children = if metadata.is_dir() {
//TODO: calculate children in the background (and make it cancellable?)
match fs::read_dir(&path) {
Ok(entries) => entries.count(),
Err(err) => {
log::warn!("failed to read directory {:?}: {}", path, err);
0
}
}
} else {
0
};
items.push(Item {
name,
metadata: ItemMetadata::Path { metadata, children },
hidden,
2024-01-05 14:44:20 -07:00
path,
2024-02-22 15:04:37 -07:00
mime_guess,
2024-01-05 09:36:16 -07:00
icon_handle_grid,
icon_handle_list,
2024-02-22 16:17:39 -07:00
thumbnail_res_opt: match mime_guess.first() {
Some(mime) if mime.type_() == "image" => None,
_ => Some(Err(())),
},
2024-02-29 15:38:03 -07:00
button_id: widget::Id::unique(),
pos_opt: Cell::new(None),
2024-02-29 11:25:46 -07:00
rect_opt: Cell::new(None),
2024-01-10 09:47:47 -07:00
selected: false,
click_time: None,
});
}
}
Err(err) => {
log::warn!("failed to read directory {:?}: {}", tab_path, err);
}
}
2024-01-06 12:59:36 -07:00
items.sort_by(|a, b| match (a.metadata.is_dir(), b.metadata.is_dir()) {
(true, false) => Ordering::Less,
(false, true) => Ordering::Greater,
_ => lexical_sort::natural_lexical_cmp(&a.name, &b.name),
});
2024-01-05 16:17:23 -07:00
items
}
// This config statement is from trash::os_limited, inverted
#[cfg(not(any(
target_os = "windows",
all(
unix,
not(target_os = "macos"),
not(target_os = "ios"),
not(target_os = "android")
)
)))]
pub fn scan_trash() -> Vec<Item> {
log::warn!("viewing trash not supported on this platform");
Vec::new()
}
// This config statement is from trash::os_limited
#[cfg(any(
target_os = "windows",
all(
unix,
not(target_os = "macos"),
not(target_os = "ios"),
not(target_os = "android")
)
))]
pub fn scan_trash(sizes: IconSizes) -> Vec<Item> {
2024-01-05 16:17:23 -07:00
let mut items: Vec<Item> = Vec::new();
match trash::os_limited::list() {
Ok(entries) => {
for entry in entries {
2024-01-06 12:59:36 -07:00
let metadata = match trash::os_limited::metadata(&entry) {
Ok(ok) => ok,
Err(err) => {
log::warn!("failed to get metadata for trash item {:?}: {}", entry, err);
continue;
}
};
2024-01-05 16:17:23 -07:00
let path = entry.original_path();
let name = entry.name.clone();
2024-01-05 16:17:23 -07:00
2024-02-22 15:04:37 -07:00
let mime_guess = MimeGuess::from_path(&path);
let (icon_handle_grid, icon_handle_list) = match metadata.size {
2024-01-10 08:53:22 -07:00
trash::TrashItemSize::Entries(_) => (
folder_icon(&path, sizes.grid()),
folder_icon(&path, sizes.list()),
2024-01-10 08:53:22 -07:00
),
trash::TrashItemSize::Bytes(_) => (
mime_icon(&path, sizes.grid()),
mime_icon(&path, sizes.list()),
2024-01-10 08:53:22 -07:00
),
2024-01-06 12:59:36 -07:00
};
2024-01-05 16:17:23 -07:00
items.push(Item {
name,
metadata: ItemMetadata::Trash { metadata, entry },
2024-01-05 16:17:23 -07:00
hidden: false,
path,
2024-02-22 15:04:37 -07:00
mime_guess,
2024-01-05 16:17:23 -07:00
icon_handle_grid,
icon_handle_list,
2024-02-22 16:17:39 -07:00
thumbnail_res_opt: Some(Err(())),
2024-02-29 15:38:03 -07:00
button_id: widget::Id::unique(),
pos_opt: Cell::new(None),
2024-02-29 11:25:46 -07:00
rect_opt: Cell::new(None),
2024-01-10 09:47:47 -07:00
selected: false,
click_time: None,
2024-01-05 16:17:23 -07:00
});
}
}
Err(err) => {
log::warn!("failed to read trash items: {}", err);
}
}
2024-01-06 12:59:36 -07:00
items.sort_by(|a, b| match (a.metadata.is_dir(), b.metadata.is_dir()) {
(true, false) => Ordering::Less,
(false, true) => Ordering::Greater,
2024-01-05 09:44:47 -07:00
_ => lexical_sort::natural_lexical_cmp(&a.name, &b.name),
});
items
2024-01-03 15:27:32 -07:00
}
#[derive(Clone, Debug, Eq, PartialEq)]
2024-01-05 16:17:23 -07:00
pub enum Location {
Path(PathBuf),
Trash,
}
impl Location {
pub fn scan(&self, sizes: IconSizes) -> Vec<Item> {
2024-01-05 16:17:23 -07:00
match self {
Self::Path(path) => scan_path(path, sizes),
Self::Trash => scan_trash(sizes),
2024-01-05 16:17:23 -07:00
}
}
}
2024-02-26 12:51:22 -07:00
#[derive(Clone, Debug)]
pub enum Command {
Action(Action),
ChangeLocation(String, Location),
2024-02-29 15:38:03 -07:00
FocusButton(widget::Id),
2024-02-28 15:19:07 -07:00
FocusTextInput(widget::Id),
2024-02-26 14:11:45 -07:00
OpenFile(PathBuf),
2024-02-29 16:25:54 -07:00
Scroll(widget::Id, AbsoluteOffset),
2024-02-26 12:51:22 -07:00
}
2024-01-09 15:34:48 -07:00
#[derive(Clone, Debug)]
2024-01-05 09:36:16 -07:00
pub enum Message {
Click(Option<usize>),
Config(TabConfig),
2024-02-26 12:05:29 -07:00
ContextAction(Action),
ContextMenu(Option<Point>),
2024-02-29 11:25:46 -07:00
Drag(Option<Rectangle>),
2024-01-29 11:58:36 -07:00
EditLocation(Option<Location>),
GoNext,
GoPrevious,
2024-02-28 16:16:59 -07:00
ItemDown,
ItemLeft,
ItemRight,
ItemUp,
2024-01-09 15:34:48 -07:00
Location(Location),
LocationUp,
Open,
2024-02-29 11:25:46 -07:00
Resize(Size),
RightClick(usize),
2024-02-26 12:05:29 -07:00
Scroll(Viewport),
2024-02-22 16:17:39 -07:00
Thumbnail(PathBuf, Result<image::RgbaImage, ()>),
ToggleShowHidden,
2024-01-05 15:17:38 -07:00
View(View),
2024-02-28 05:18:40 +01:00
ToggleSort(HeadingOptions),
2024-01-05 09:36:16 -07:00
}
2024-01-06 12:59:36 -07:00
#[derive(Clone, Debug)]
pub enum ItemMetadata {
Path {
metadata: Metadata,
children: usize,
},
Trash {
metadata: trash::TrashItemMetadata,
entry: trash::TrashItem,
},
2024-01-06 12:59:36 -07:00
}
impl ItemMetadata {
pub fn is_dir(&self) -> bool {
match self {
Self::Path { metadata, .. } => metadata.is_dir(),
Self::Trash { metadata, .. } => match metadata.size {
2024-01-10 08:53:22 -07:00
trash::TrashItemSize::Entries(_) => true,
trash::TrashItemSize::Bytes(_) => false,
},
2024-01-06 12:59:36 -07:00
}
}
}
2024-01-05 09:36:16 -07:00
#[derive(Clone)]
pub struct Item {
pub name: String,
2024-01-06 12:59:36 -07:00
pub metadata: ItemMetadata,
2024-01-05 09:36:16 -07:00
pub hidden: bool,
2024-01-05 14:44:20 -07:00
pub path: PathBuf,
2024-02-22 15:04:37 -07:00
pub mime_guess: MimeGuess,
2024-01-05 09:36:16 -07:00
pub icon_handle_grid: widget::icon::Handle,
pub icon_handle_list: widget::icon::Handle,
2024-02-22 16:17:39 -07:00
pub thumbnail_res_opt: Option<Result<image::RgbaImage, ()>>,
2024-02-29 15:38:03 -07:00
pub button_id: widget::Id,
pub pos_opt: Cell<Option<(usize, usize)>>,
2024-02-29 11:25:46 -07:00
pub rect_opt: Cell<Option<Rectangle>>,
2024-01-10 09:47:47 -07:00
pub selected: bool,
pub click_time: Option<Instant>,
2024-01-05 09:36:16 -07:00
}
2024-01-05 14:44:20 -07:00
impl Item {
pub fn property_view(&self, sizes: IconSizes) -> Element<crate::app::Message> {
let cosmic_theme::Spacing { space_xxxs, .. } = theme::active().cosmic().spacing;
2024-02-22 15:04:37 -07:00
2024-02-22 16:17:39 -07:00
let mut column = widget::column().spacing(space_xxxs);
let is_image = if let Some(mime) = self.mime_guess.first() {
mime.type_() == "image" && self.path.is_file()
} else {
false
};
2024-02-22 15:04:37 -07:00
2024-02-22 16:17:39 -07:00
column = column.push(widget::row::with_children(vec![
widget::horizontal_space(Length::Fill).into(),
if is_image {
widget::image::viewer(widget::image::Handle::from_path(&self.path))
.min_scale(1.0)
.into()
2024-02-22 16:17:39 -07:00
} else {
widget::icon::icon(self.icon_handle_grid.clone())
2024-02-22 21:34:21 -07:00
.content_fit(ContentFit::Contain)
2024-02-22 16:17:39 -07:00
.size(sizes.grid())
.into()
},
widget::horizontal_space(Length::Fill).into(),
2024-01-05 14:58:50 -07:00
]));
2024-01-05 14:44:20 -07:00
2024-02-22 16:17:39 -07:00
column = column.push(widget::text::heading(self.name.clone()));
2024-02-22 15:04:37 -07:00
if let Some(mime) = self.mime_guess.first() {
2024-02-22 16:17:39 -07:00
column = column.push(widget::text(format!("Type: {}", mime)));
2024-02-22 15:04:37 -07:00
}
2024-01-05 14:44:20 -07:00
//TODO: translate!
2024-01-05 14:45:45 -07:00
//TODO: correct display of folder size?
2024-01-06 12:59:36 -07:00
match &self.metadata {
ItemMetadata::Path { metadata, children } => {
if metadata.is_dir() {
2024-02-22 16:17:39 -07:00
column = column.push(widget::text(format!("Items: {}", children)));
} else {
2024-02-22 16:17:39 -07:00
column = column.push(widget::text(format!(
"Size: {}",
format_size(metadata.len())
)));
2024-01-06 12:59:36 -07:00
}
2024-01-05 14:44:20 -07:00
2024-02-22 15:04:37 -07:00
if let Ok(time) = metadata.created() {
2024-02-22 16:17:39 -07:00
column = column.push(widget::text(format!(
"Created: {}",
chrono::DateTime::<chrono::Local>::from(time).format(TIME_FORMAT)
)));
2024-01-06 12:59:36 -07:00
}
2024-01-05 14:44:20 -07:00
2024-01-06 12:59:36 -07:00
if let Ok(time) = metadata.modified() {
2024-02-22 16:17:39 -07:00
column = column.push(widget::text(format!(
"Modified: {}",
chrono::DateTime::<chrono::Local>::from(time).format(TIME_FORMAT)
)));
2024-01-06 12:59:36 -07:00
}
2024-01-05 14:44:20 -07:00
2024-02-22 15:04:37 -07:00
if let Ok(time) = metadata.accessed() {
2024-02-22 16:17:39 -07:00
column = column.push(widget::text(format!(
"Accessed: {}",
chrono::DateTime::<chrono::Local>::from(time).format(TIME_FORMAT)
)));
2024-01-06 12:59:36 -07:00
}
}
ItemMetadata::Trash { .. } => {
2024-01-06 12:59:36 -07:00
//TODO: trash metadata
2024-01-05 16:17:23 -07:00
}
2024-01-05 14:44:20 -07:00
}
2024-02-22 16:17:39 -07:00
column.into()
2024-01-05 14:44:20 -07:00
}
}
2024-01-05 09:36:16 -07:00
impl fmt::Debug for Item {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Item")
.field("name", &self.name)
2024-01-06 12:59:36 -07:00
.field("metadata", &self.metadata)
2024-01-05 09:36:16 -07:00
.field("hidden", &self.hidden)
2024-01-05 14:44:20 -07:00
.field("path", &self.path)
2024-02-29 15:38:03 -07:00
.field("mime_guess", &self.mime_guess)
2024-01-05 14:44:20 -07:00
// icon_handles
2024-02-29 15:38:03 -07:00
// thumbnail_res_opt
.field("button_id", &self.button_id)
.field("pos_opt", &self.pos_opt)
2024-02-29 11:25:46 -07:00
.field("rect_opt", &self.rect_opt)
2024-01-10 09:47:47 -07:00
.field("selected", &self.selected)
.field("click_time", &self.click_time)
2024-01-05 09:36:16 -07:00
.finish()
}
}
#[derive(Clone, Copy, Debug)]
pub enum View {
Grid,
List,
}
2024-02-28 05:18:40 +01:00
#[derive(Clone, Copy, Debug, Hash, PartialEq, PartialOrd, Ord, Eq)]
2024-02-27 22:28:06 -07:00
pub enum HeadingOptions {
2024-02-28 05:18:40 +01:00
Name,
Modified,
Size,
}
2024-01-05 09:36:16 -07:00
#[derive(Clone, Debug)]
pub struct Tab {
2024-02-29 15:38:03 -07:00
//TODO: make more items private
2024-01-05 16:17:23 -07:00
pub location: Location,
2024-01-05 09:36:16 -07:00
pub context_menu: Option<Point>,
pub view: View,
2024-02-15 15:03:01 -07:00
pub dialog: Option<DialogKind>,
2024-02-29 16:25:54 -07:00
pub scroll_opt: Option<AbsoluteOffset>,
2024-02-29 11:25:46 -07:00
pub size_opt: Option<Size>,
2024-01-29 11:58:36 -07:00
pub edit_location: Option<Location>,
2024-02-28 15:19:07 -07:00
pub edit_location_id: widget::Id,
pub history_i: usize,
pub history: Vec<Location>,
pub config: TabConfig,
2024-02-29 13:42:13 -07:00
items_opt: Option<Vec<Item>>,
2024-02-29 16:25:54 -07:00
scrollable_id: widget::Id,
2024-02-29 15:21:59 -07:00
select_focus: Option<usize>,
select_shift: Option<usize>,
2024-02-29 03:46:14 +01:00
sort_name: HeadingOptions,
sort_direction: bool,
2024-01-05 09:36:16 -07:00
}
2024-01-03 15:27:32 -07:00
impl Tab {
pub fn new(location: Location, config: TabConfig) -> Self {
let history = vec![location.clone()];
2024-02-29 03:46:14 +01:00
let sort_name = HeadingOptions::Name;
let sort_direction = true;
Self {
2024-01-05 16:17:23 -07:00
location,
2024-01-03 15:27:32 -07:00
context_menu: None,
2024-02-29 11:25:46 -07:00
view: View::Grid,
2024-02-15 15:03:01 -07:00
dialog: None,
2024-02-26 12:05:29 -07:00
scroll_opt: None,
2024-02-29 11:25:46 -07:00
size_opt: None,
2024-01-29 11:58:36 -07:00
edit_location: None,
2024-02-28 15:19:07 -07:00
edit_location_id: widget::Id::unique(),
history_i: 0,
history,
config,
2024-02-29 13:42:13 -07:00
items_opt: None,
2024-02-29 16:25:54 -07:00
scrollable_id: widget::Id::unique(),
2024-02-29 15:21:59 -07:00
select_focus: None,
select_shift: None,
2024-02-29 03:46:14 +01:00
sort_name,
sort_direction,
2024-01-03 15:27:32 -07:00
}
}
pub fn title(&self) -> String {
//TODO: better title
2024-01-05 16:17:23 -07:00
match &self.location {
Location::Path(path) => {
format!("{}", path.display())
}
Location::Trash => {
fl!("trash")
}
}
2024-01-03 15:27:32 -07:00
}
2024-02-29 13:42:13 -07:00
pub fn items_opt(&self) -> Option<&Vec<Item>> {
self.items_opt.as_ref()
}
pub fn set_items(&mut self, items: Vec<Item>) {
self.items_opt = Some(items);
2024-02-29 15:21:59 -07:00
self.select_focus = None;
2024-02-29 13:42:13 -07:00
}
pub fn select_all(&mut self) {
2024-02-29 15:21:59 -07:00
self.select_focus = None;
2024-02-29 13:42:13 -07:00
if let Some(ref mut items) = self.items_opt {
for item in items.iter_mut() {
if !self.config.show_hidden && item.hidden {
item.selected = false;
continue;
}
item.selected = true;
item.click_time = None;
}
}
}
pub fn select_none(&mut self) -> bool {
2024-02-29 15:21:59 -07:00
self.select_focus = None;
2024-02-29 13:42:13 -07:00
let mut had_selection = false;
if let Some(ref mut items) = self.items_opt {
for item in items.iter_mut() {
if item.selected {
item.selected = false;
had_selection = true;
}
}
}
had_selection
}
2024-02-29 15:21:59 -07:00
pub fn select_name(&mut self, name: &str) {
self.select_focus = None;
2024-02-29 14:34:41 -07:00
if let Some(ref mut items) = self.items_opt {
2024-02-29 15:21:59 -07:00
for (i, item) in items.iter_mut().enumerate() {
if item.name == name {
self.select_focus = Some(i);
item.selected = true;
} else {
item.selected = false;
}
2024-02-29 14:34:41 -07:00
}
}
}
2024-02-29 15:21:59 -07:00
fn select_position(&mut self, row: usize, col: usize, mod_shift: bool) -> bool {
let mut start = (row, col);
let mut end = (row, col);
if mod_shift {
if self.select_focus.is_none() || self.select_shift.is_none() {
// Set select shift to initial state if necessary
self.select_shift = self.select_focus;
}
if let Some(pos) = self.select_shift_pos_opt() {
if pos.0 < row || (pos.0 == row && pos.1 < col) {
start = pos;
} else {
end = pos;
}
2024-02-29 15:21:59 -07:00
}
} else {
// Clear select shift if the modifier is not set
self.select_shift = None;
};
2024-02-29 15:21:59 -07:00
let mut found = false;
if let Some(ref mut items) = self.items_opt {
for (i, item) in items.iter_mut().enumerate() {
item.selected = false;
let pos = match item.pos_opt.get() {
Some(some) => some,
None => continue,
};
if pos.0 < start.0 || (pos.0 == start.0 && pos.1 < start.1) {
// Before start
continue;
}
if pos.0 > end.0 || (pos.0 == end.0 && pos.1 > end.1) {
// After end
continue;
}
if pos == (row, col) {
// Update focus if this is what we wanted to select
self.select_focus = Some(i);
}
2024-02-29 15:21:59 -07:00
item.selected = true;
found = true;
}
}
2024-02-29 15:21:59 -07:00
found
}
2024-02-29 15:21:59 -07:00
pub fn select_rect(&mut self, rect: Rectangle) {
self.select_focus = None;
if let Some(ref mut items) = self.items_opt {
2024-02-29 15:21:59 -07:00
for (_i, item) in items.iter_mut().enumerate() {
//TODO: modifiers
item.selected = match item.rect_opt.get() {
Some(item_rect) => item_rect.intersects(&rect),
None => false,
};
}
}
2024-02-29 15:21:59 -07:00
}
2024-02-29 15:38:03 -07:00
fn select_focus_id(&self) -> Option<widget::Id> {
let items = self.items_opt.as_ref()?;
let item = items.get(self.select_focus?)?;
Some(item.button_id.clone())
}
2024-02-29 15:21:59 -07:00
fn select_focus_pos_opt(&self) -> Option<(usize, usize)> {
let items = self.items_opt.as_ref()?;
let item = items.get(self.select_focus?)?;
item.pos_opt.get()
}
2024-02-29 16:25:54 -07:00
fn select_focus_scroll(&mut self) -> Option<AbsoluteOffset> {
let items = self.items_opt.as_ref()?;
let item = items.get(self.select_focus?)?;
let rect = item.rect_opt.get()?;
//TODO: move to function
let visible_rect = {
let point = match self.scroll_opt {
Some(offset) => Point::new(0.0, offset.y),
None => Point::new(0.0, 0.0),
};
let size = self.size_opt.unwrap_or_else(|| Size::new(0.0, 0.0));
Rectangle::new(point, size)
};
if rect.y < visible_rect.y {
// Scroll up to rect
self.scroll_opt = Some(AbsoluteOffset { x: 0.0, y: rect.y });
self.scroll_opt
} else if (rect.y + rect.height) > (visible_rect.y + visible_rect.height) {
// Scroll down to rect
self.scroll_opt = Some(AbsoluteOffset {
x: 0.0,
y: rect.y + rect.height - visible_rect.height,
});
self.scroll_opt
} else {
// Do not scroll
None
}
}
2024-02-29 15:21:59 -07:00
fn select_shift_pos_opt(&self) -> Option<(usize, usize)> {
let items = self.items_opt.as_ref()?;
let item = items.get(self.select_shift?)?;
item.pos_opt.get()
}
2024-02-26 14:11:45 -07:00
pub fn update(&mut self, message: Message, modifiers: Modifiers) -> Vec<Command> {
let mut commands = Vec::new();
2024-01-03 15:27:32 -07:00
let mut cd = None;
let mut history_i_opt = None;
let mod_ctrl = modifiers.contains(Modifiers::CTRL)
&& self.dialog.as_ref().map_or(true, |x| x.multiple());
let mod_shift = modifiers.contains(Modifiers::SHIFT)
&& self.dialog.as_ref().map_or(true, |x| x.multiple());
2024-01-03 15:27:32 -07:00
match message {
Message::Click(click_i_opt) => {
2024-02-29 15:21:59 -07:00
self.select_focus = None;
if let Some(ref mut items) = self.items_opt {
for (i, item) in items.iter_mut().enumerate() {
2024-01-05 11:18:38 -07:00
if Some(i) == click_i_opt {
2024-02-26 14:31:50 -07:00
// Filter out selection if it does not match dialog kind
if let Some(dialog) = &self.dialog {
let item_is_dir = item.path.is_dir();
if item_is_dir != dialog.is_dir() {
// Allow selecting folder if dialog is for files to make it
// possible to double click
//TODO: clear any other selection when selecting a folder
if !item_is_dir {
continue;
}
}
}
2024-02-29 15:21:59 -07:00
self.select_focus = Some(i);
2024-01-10 09:47:47 -07:00
item.selected = true;
if let Some(click_time) = item.click_time {
if click_time.elapsed() < DOUBLE_CLICK_DURATION {
match self.location {
Location::Path(_) => {
if item.path.is_dir() {
2024-02-26 14:11:45 -07:00
//TODO: allow opening multiple tabs?
cd = Some(Location::Path(item.path.clone()));
2024-02-26 14:11:45 -07:00
} else {
commands.push(Command::OpenFile(item.path.clone()));
2024-01-10 09:47:47 -07:00
}
}
Location::Trash => {
//TODO: open properties?
}
}
2024-01-03 15:27:32 -07:00
}
}
2024-01-10 10:29:49 -07:00
//TODO: prevent triple-click and beyond from opening file?
item.click_time = Some(Instant::now());
} else if mod_ctrl {
// Holding control allows multiple selection
item.click_time = None;
} else {
2024-01-10 09:47:47 -07:00
item.selected = false;
item.click_time = None;
2024-01-03 15:27:32 -07:00
}
}
}
2024-01-05 11:18:38 -07:00
self.context_menu = None;
2024-01-03 15:27:32 -07:00
}
Message::Config(config) => {
self.config = config;
}
2024-02-26 12:05:29 -07:00
Message::ContextAction(action) => {
// Close context menu
self.context_menu = None;
2024-02-26 14:11:45 -07:00
commands.push(Command::Action(action));
2024-02-26 12:05:29 -07:00
}
Message::ContextMenu(point_opt) => {
self.context_menu = point_opt;
}
2024-02-29 11:25:46 -07:00
Message::Drag(rect_opt) => match rect_opt {
Some(rect) => {
2024-02-29 15:21:59 -07:00
self.select_rect(rect);
2024-02-26 12:05:29 -07:00
}
2024-02-29 11:25:46 -07:00
None => {}
2024-02-26 12:05:29 -07:00
},
2024-01-29 11:58:36 -07:00
Message::EditLocation(edit_location) => {
2024-02-28 15:19:07 -07:00
if self.edit_location.is_none() && edit_location.is_some() {
commands.push(Command::FocusTextInput(self.edit_location_id.clone()));
}
2024-01-29 11:58:36 -07:00
self.edit_location = edit_location;
}
Message::GoNext => {
if let Some(history_i) = self.history_i.checked_add(1) {
if let Some(location) = self.history.get(history_i) {
cd = Some(location.clone());
history_i_opt = Some(history_i);
}
}
}
Message::GoPrevious => {
if let Some(history_i) = self.history_i.checked_sub(1) {
if let Some(location) = self.history.get(history_i) {
cd = Some(location.clone());
history_i_opt = Some(history_i);
}
}
}
Message::ItemDown => {
2024-02-29 15:21:59 -07:00
if let Some((row, col)) = self.select_focus_pos_opt() {
//TODO: Shift modifier should select items in between
// Try to select item in next row
if !self.select_position(row + 1, col, mod_shift) {
// Ensure current item is still selected if there are no other items
self.select_position(row, col, mod_shift);
2024-02-28 16:16:59 -07:00
}
} else {
// Select first item
2024-02-29 16:25:54 -07:00
//TODO: select first in scroll
self.select_position(0, 0, mod_shift);
2024-02-28 16:16:59 -07:00
}
2024-02-29 16:25:54 -07:00
if let Some(offset) = self.select_focus_scroll() {
commands.push(Command::Scroll(self.scrollable_id.clone(), offset));
}
2024-02-29 15:38:03 -07:00
if let Some(id) = self.select_focus_id() {
commands.push(Command::FocusButton(id));
}
2024-02-28 16:16:59 -07:00
}
Message::ItemLeft => {
2024-02-29 15:21:59 -07:00
if let Some((row, col)) = self.select_focus_pos_opt() {
// Try to select previous item in current row
if !col
.checked_sub(1)
.map_or(false, |col| self.select_position(row, col, mod_shift))
{
// Try to select last item in previous row
if !row.checked_sub(1).map_or(false, |row| {
let mut col = 0;
if let Some(ref items) = self.items_opt {
for item in items.iter() {
match item.pos_opt.get() {
Some((item_row, item_col)) if item_row == row => {
col = col.max(item_col);
}
_ => continue,
}
}
2024-02-28 16:16:59 -07:00
}
self.select_position(row, col, mod_shift)
}) {
// Ensure current item is still selected if there are no other items
self.select_position(row, col, mod_shift);
2024-02-28 16:16:59 -07:00
}
}
} else {
// Select first item
2024-02-29 16:25:54 -07:00
//TODO: select first in scroll
self.select_position(0, 0, mod_shift);
}
2024-02-29 16:25:54 -07:00
if let Some(offset) = self.select_focus_scroll() {
commands.push(Command::Scroll(self.scrollable_id.clone(), offset));
}
2024-02-29 15:38:03 -07:00
if let Some(id) = self.select_focus_id() {
commands.push(Command::FocusButton(id));
}
}
Message::ItemRight => {
2024-02-29 15:21:59 -07:00
if let Some((row, col)) = self.select_focus_pos_opt() {
// Try to select next item in current row
if !self.select_position(row, col + 1, mod_shift) {
// Try to select first item in next row
if !self.select_position(row + 1, 0, mod_shift) {
// Ensure current item is still selected if there are no other items
self.select_position(row, col, mod_shift);
2024-02-28 16:16:59 -07:00
}
}
} else {
// Select first item
2024-02-29 16:25:54 -07:00
//TODO: select first in scroll
self.select_position(0, 0, mod_shift);
}
2024-02-29 16:25:54 -07:00
if let Some(offset) = self.select_focus_scroll() {
commands.push(Command::Scroll(self.scrollable_id.clone(), offset));
}
2024-02-29 15:38:03 -07:00
if let Some(id) = self.select_focus_id() {
commands.push(Command::FocusButton(id));
}
}
Message::ItemUp => {
2024-02-29 15:21:59 -07:00
if let Some((row, col)) = self.select_focus_pos_opt() {
//TODO: Shift modifier should select items in between
// Try to select item in last row
if !row
.checked_sub(1)
.map_or(false, |row| self.select_position(row, col, mod_shift))
{
// Ensure current item is still selected if there are no other items
self.select_position(row, col, mod_shift);
}
} else {
// Select first item
2024-02-29 16:25:54 -07:00
//TODO: select first in scroll
self.select_position(0, 0, mod_shift);
2024-02-28 16:16:59 -07:00
}
2024-02-29 16:25:54 -07:00
if let Some(offset) = self.select_focus_scroll() {
commands.push(Command::Scroll(self.scrollable_id.clone(), offset));
}
2024-02-29 15:38:03 -07:00
if let Some(id) = self.select_focus_id() {
commands.push(Command::FocusButton(id));
}
2024-02-28 16:16:59 -07:00
}
2024-01-09 15:34:48 -07:00
Message::Location(location) => {
cd = Some(location);
2024-01-03 17:04:08 -07:00
}
Message::LocationUp => {
// Sets location to the path's parent
// Does nothing if path is root or location is Trash
if let Location::Path(ref path) = self.location {
if let Some(parent) = path.parent() {
cd = Some(Location::Path(parent.to_owned()));
}
}
}
Message::Open => {
if let Some(ref mut items) = self.items_opt {
for item in items.iter() {
if item.selected {
match self.location {
Location::Path(_) => {
if item.path.is_dir() {
//TODO: allow opening multiple tabs?
cd = Some(Location::Path(item.path.clone()));
} else {
commands.push(Command::OpenFile(item.path.clone()));
}
}
Location::Trash => {
//TODO: open properties?
}
}
}
}
}
}
2024-02-29 11:25:46 -07:00
Message::Resize(size) => {
self.size_opt = Some(size);
}
Message::RightClick(click_i) => {
if let Some(ref mut items) = self.items_opt {
if !items.get(click_i).map_or(false, |x| x.selected) {
// If item not selected, clear selection on other items
for (i, item) in items.iter_mut().enumerate() {
if i == click_i {
item.selected = true;
} else if mod_ctrl {
// Holding control allows multiple selection
} else {
item.selected = false;
}
item.click_time = None;
}
}
}
}
2024-02-26 12:05:29 -07:00
Message::Scroll(viewport) => {
2024-02-29 16:25:54 -07:00
self.scroll_opt = Some(viewport.absolute_offset());
2024-02-26 12:05:29 -07:00
}
2024-02-22 16:17:39 -07:00
Message::Thumbnail(path, thumbnail_res) => {
if let Some(ref mut items) = self.items_opt {
for item in items.iter_mut() {
if item.path == path {
if let Ok(thumbnail) = &thumbnail_res {
//TODO: pass handles already generated to avoid blocking main thread
let handle = widget::icon::from_raster_pixels(
thumbnail.width(),
thumbnail.height(),
thumbnail.as_raw().clone(),
);
item.icon_handle_grid = handle.clone();
item.icon_handle_list = handle;
}
item.thumbnail_res_opt = Some(thumbnail_res);
break;
}
}
}
}
Message::ToggleShowHidden => self.config.show_hidden = !self.config.show_hidden,
2024-02-28 05:18:40 +01:00
2024-01-05 15:17:38 -07:00
Message::View(view) => {
self.view = view;
}
2024-02-28 05:18:40 +01:00
Message::ToggleSort(heading_option) => {
2024-02-29 03:46:14 +01:00
let heading_sort = if self.sort_name == heading_option {
!self.sort_direction
} else {
true
2024-02-28 05:18:40 +01:00
};
2024-02-29 03:46:14 +01:00
self.sort_direction = heading_sort;
self.sort_name = heading_option;
2024-02-28 05:18:40 +01:00
}
2024-01-03 15:27:32 -07:00
}
if let Some(location) = cd {
if location != self.location {
self.location = location.clone();
self.items_opt = None;
2024-02-29 15:21:59 -07:00
self.select_focus = None;
2024-01-29 11:58:36 -07:00
self.edit_location = None;
if let Some(history_i) = history_i_opt {
// Navigating in history
self.history_i = history_i;
} else {
// Truncate history to remove next entries
self.history.truncate(self.history_i + 1);
// Push to the front of history
self.history_i = self.history.len();
2024-02-26 12:51:22 -07:00
self.history.push(location.clone());
}
2024-02-26 14:11:45 -07:00
commands.push(Command::ChangeLocation(self.title(), location));
}
2024-01-03 15:27:32 -07:00
}
2024-02-26 14:11:45 -07:00
commands
2024-01-03 15:27:32 -07:00
}
fn column_sort(&self) -> Option<Vec<&Item>> {
2024-02-29 03:46:14 +01:00
let check_reverse = |ord: Ordering, sort: bool| {
if sort {
ord
} else {
ord.reverse()
}
};
let mut items: Vec<&Item> = self.items_opt.as_ref()?.iter().collect();
2024-02-29 03:46:14 +01:00
let heading_sort = self.sort_direction;
match self.sort_name {
HeadingOptions::Size => {
items.sort_by(|a, b| {
2024-02-29 03:46:14 +01:00
// entries take precedence over size
let get_size = |x: &Item| match &x.metadata {
ItemMetadata::Path { metadata, children } => {
if metadata.is_dir() {
(true, *children as u64)
} else {
(false, metadata.len())
}
}
ItemMetadata::Trash { metadata, .. } => match metadata.size {
trash::TrashItemSize::Entries(entries) => (true, entries as u64),
trash::TrashItemSize::Bytes(bytes) => (false, bytes),
},
};
let (a_is_entry, a_size) = get_size(a);
let (b_is_entry, b_size) = get_size(b);
let ord = match (a_is_entry, b_is_entry) {
(true, false) => Ordering::Less,
(false, true) => Ordering::Greater,
_ => a_size.cmp(&b_size),
};
check_reverse(ord, heading_sort)
})
}
HeadingOptions::Name => items.sort_by(|a, b| {
2024-02-29 03:46:14 +01:00
let ord = match (a.path.is_dir(), b.path.is_dir()) {
(true, false) => Ordering::Less,
(false, true) => Ordering::Greater,
_ => lexical_sort::natural_lexical_cmp(&a.name, &b.name),
};
check_reverse(ord, heading_sort)
}),
HeadingOptions::Modified => {
items.sort_by(|a, b| {
2024-02-29 03:46:14 +01:00
let get_modified = |x: &Item| match &x.metadata {
ItemMetadata::Path { metadata, .. } => metadata.modified().ok(),
ItemMetadata::Trash { .. } => None,
};
let a = get_modified(a);
let b = get_modified(b);
check_reverse(a.cmp(&b), heading_sort)
});
}
}
Some(items)
2024-02-29 03:46:14 +01:00
}
pub fn location_view(&self) -> Element<Message> {
2024-01-29 11:58:36 -07:00
let cosmic_theme::Spacing {
space_xxxs,
space_xxs,
space_s,
2024-01-29 11:58:36 -07:00
..
} = theme::active().cosmic().spacing;
2024-01-29 11:58:36 -07:00
let mut row = widget::row::with_capacity(5).align_items(Alignment::Center);
let mut prev_button =
widget::button(widget::icon::from_name("go-previous-symbolic").size(16))
.padding(space_xxs)
.style(theme::Button::Icon);
if self.history_i > 0 && !self.history.is_empty() {
prev_button = prev_button.on_press(Message::GoPrevious);
}
row = row.push(prev_button);
let mut next_button = widget::button(widget::icon::from_name("go-next-symbolic").size(16))
.padding(space_xxs)
.style(theme::Button::Icon);
if self.history_i + 1 < self.history.len() {
next_button = next_button.on_press(Message::GoNext);
}
row = row.push(next_button);
row = row.push(widget::horizontal_space(Length::Fixed(space_s.into())));
2024-01-29 11:58:36 -07:00
if let Some(location) = &self.edit_location {
match location {
Location::Path(path) => {
row = row.push(
2024-01-29 11:58:36 -07:00
widget::button(widget::icon::from_name("window-close-symbolic").size(16))
.on_press(Message::EditLocation(None))
.padding(space_xxs)
.style(theme::Button::Icon),
);
row = row.push(
2024-01-29 11:58:36 -07:00
widget::text_input("", path.to_string_lossy())
2024-02-28 15:19:07 -07:00
.id(self.edit_location_id.clone())
2024-01-29 11:58:36 -07:00
.on_input(|input| {
Message::EditLocation(Some(Location::Path(PathBuf::from(input))))
})
.on_submit(Message::Location(location.clone())),
);
return row.into();
2024-01-29 11:58:36 -07:00
}
_ => {
//TODO: allow editing other locations
}
}
2024-01-30 11:26:23 -07:00
} else if let Location::Path(_) = &self.location {
row = row.push(
widget::button(widget::icon::from_name("edit-symbolic").size(16))
.on_press(Message::EditLocation(Some(self.location.clone())))
.padding(space_xxs)
.style(theme::Button::Icon),
);
2024-01-29 11:58:36 -07:00
}
2024-01-10 12:57:30 -07:00
let mut children: Vec<Element<_>> = Vec::new();
match &self.location {
Location::Path(path) => {
let home_dir = crate::home_dir();
for ancestor in path.ancestors() {
let ancestor = ancestor.to_path_buf();
let mut found_home = false;
let mut row = widget::row::with_capacity(2)
.align_items(Alignment::Center)
.spacing(space_xxxs);
2024-01-29 11:16:19 -07:00
let name = match ancestor.file_name() {
2024-01-10 12:57:30 -07:00
Some(name) => {
if ancestor == home_dir {
row = row.push(
widget::icon::icon(folder_icon_symbolic(&ancestor, 16))
.size(16),
);
found_home = true;
2024-01-29 11:16:19 -07:00
fl!("home")
} else {
2024-01-29 11:16:19 -07:00
name.to_string_lossy().to_string()
2024-01-10 12:57:30 -07:00
}
}
None => {
row = row.push(
widget::icon::from_name("drive-harddisk-system-symbolic")
.size(16)
.icon(),
);
2024-01-29 11:16:19 -07:00
fl!("filesystem")
2024-01-10 12:57:30 -07:00
}
2024-01-29 11:16:19 -07:00
};
2024-01-10 12:57:30 -07:00
2024-01-29 11:16:19 -07:00
if children.is_empty() {
row = row.push(widget::text::heading(name));
} else {
children.push(
widget::icon::from_name("go-next-symbolic")
.size(16)
.icon()
.into(),
);
2024-01-29 11:16:19 -07:00
row = row.push(widget::text(name));
2024-01-10 12:57:30 -07:00
}
children.push(
widget::button(row)
.padding(space_xxxs)
.on_press(Message::Location(Location::Path(ancestor)))
.style(theme::Button::Link)
2024-01-10 12:57:30 -07:00
.into(),
);
if found_home {
break;
}
}
children.reverse();
}
Location::Trash => {
let mut row = widget::row::with_capacity(2)
.align_items(Alignment::Center)
.spacing(space_xxxs);
row = row.push(widget::icon::icon(trash_icon_symbolic(16)).size(16));
2024-01-29 11:16:19 -07:00
row = row.push(widget::text::heading(fl!("trash")));
2024-01-10 12:57:30 -07:00
children.push(
widget::button(row)
.padding(space_xxxs)
.on_press(Message::Location(Location::Trash))
.style(theme::Button::Text)
.into(),
);
}
}
2024-01-29 11:58:36 -07:00
for child in children {
row = row.push(child);
}
row.into()
2024-01-10 12:57:30 -07:00
}
pub fn empty_view(&self, has_hidden: bool) -> Element<Message> {
let cosmic_theme::Spacing { space_xxs, .. } = theme::active().cosmic().spacing;
2024-01-03 15:27:32 -07:00
2024-02-26 12:05:29 -07:00
widget::column::with_children(vec![widget::container(
widget::column::with_children(vec![
widget::icon::from_name("folder-symbolic")
.size(64)
.icon()
2024-01-05 09:36:16 -07:00
.into(),
2024-02-26 12:05:29 -07:00
widget::text(if has_hidden {
fl!("empty-folder-hidden")
} else {
fl!("empty-folder")
})
.into(),
])
.align_items(Alignment::Center)
.spacing(space_xxs),
)
.align_x(Horizontal::Center)
.align_y(Vertical::Center)
.width(Length::Fill)
.height(Length::Fill)
.into()])
2024-01-05 09:36:16 -07:00
.into()
}
pub fn grid_view(&self) -> Element<Message> {
2024-02-29 11:25:46 -07:00
let cosmic_theme::Spacing {
space_m,
2024-02-29 11:25:46 -07:00
space_xxs,
space_xxxs,
..
} = theme::active().cosmic().spacing;
2024-01-05 09:36:16 -07:00
let TabConfig {
show_hidden,
icon_sizes,
} = self.config;
2024-01-29 11:25:43 -07:00
2024-02-29 11:40:31 -07:00
let (width, height) = match self.size_opt {
Some(size) => (
(size.width.floor() as usize)
.checked_sub(2 * (space_m as usize))
.unwrap_or(0)
.max(GRID_ITEM_WIDTH),
(size.height.floor() as usize).max(GRID_ITEM_HEIGHT),
),
2024-02-29 11:40:31 -07:00
None => (GRID_ITEM_WIDTH, GRID_ITEM_HEIGHT),
};
2024-02-29 11:25:46 -07:00
let (cols, column_spacing) = {
2024-02-29 11:40:31 -07:00
let width_m1 = width.checked_sub(GRID_ITEM_WIDTH).unwrap_or(0);
2024-02-29 11:25:46 -07:00
let cols_m1 = width_m1 / (GRID_ITEM_WIDTH + space_xxs as usize);
let cols = cols_m1 + 1;
let spacing = width_m1
.checked_div(cols_m1)
.unwrap_or(0)
.checked_sub(GRID_ITEM_WIDTH)
.unwrap_or(0);
(cols, spacing as u16)
};
let mut grid = widget::grid()
.column_spacing(column_spacing)
.row_spacing(space_xxs)
.padding([0, space_m].into());
if let Some(ref items) = self.items_opt {
let mut count = 0;
2024-02-29 11:25:46 -07:00
let mut col = 0;
let mut row = 0;
let mut hidden = 0;
for (i, item) in items.iter().enumerate() {
if !show_hidden && item.hidden {
item.pos_opt.set(None);
2024-02-29 11:25:46 -07:00
item.rect_opt.set(None);
hidden += 1;
continue;
}
item.pos_opt.set(Some((row, col)));
2024-02-29 11:25:46 -07:00
item.rect_opt.set(Some(Rectangle::new(
Point::new(
(col * (GRID_ITEM_WIDTH + column_spacing as usize) + space_m as usize)
as f32,
2024-02-29 11:25:46 -07:00
(row * (GRID_ITEM_HEIGHT + space_xxs as usize)) as f32,
),
Size::new(GRID_ITEM_WIDTH as f32, GRID_ITEM_HEIGHT as f32),
)));
2024-02-29 11:25:46 -07:00
//TODO: one focus group per grid item (needs custom widget)
let buttons = vec![
widget::button(
2024-01-05 15:10:46 -07:00
widget::icon::icon(item.icon_handle_grid.clone())
2024-02-22 21:34:21 -07:00
.content_fit(ContentFit::Contain)
2024-02-29 11:25:46 -07:00
.size(icon_sizes.grid()),
)
.on_press(Message::Click(Some(i)))
.padding(space_xxxs)
.style(button_style(item.selected, false)),
widget::button(widget::text(item.name.clone()))
2024-02-29 15:38:03 -07:00
.id(item.button_id.clone())
2024-02-29 11:25:46 -07:00
.on_press(Message::Click(Some(i)))
.padding([0, space_xxs])
.style(button_style(item.selected, true)),
];
let mut column = widget::column::with_capacity(buttons.len())
2024-01-05 15:10:46 -07:00
.align_items(Alignment::Center)
2024-02-29 11:25:46 -07:00
.height(Length::Fixed(GRID_ITEM_HEIGHT as f32))
.width(Length::Fixed(GRID_ITEM_WIDTH as f32));
for button in buttons {
if self.context_menu.is_some() {
column = column.push(button)
} else {
column = column.push(
mouse_area::MouseArea::new(button).on_right_press_no_capture(
move |_point_opt| Message::RightClick(i),
),
);
}
2024-01-05 15:10:46 -07:00
}
2024-02-29 11:25:46 -07:00
grid = grid.push(column);
2024-01-05 09:36:16 -07:00
count += 1;
2024-02-29 11:25:46 -07:00
col += 1;
if col >= cols {
col = 0;
row += 1;
grid = grid.insert_row();
}
2024-01-05 09:36:16 -07:00
}
if count == 0 {
return self.empty_view(hidden > 0);
2024-01-05 09:36:16 -07:00
}
2024-02-29 11:40:31 -07:00
//TODO: HACK If we don't reach the bottom of the view, go ahead and add a spacer to do that
{
let mut max_bottom = 0;
for item in items.iter() {
if let Some(rect) = item.rect_opt.get() {
let bottom = (rect.y + rect.height).ceil() as usize;
if bottom > max_bottom {
max_bottom = bottom;
}
}
}
let spacer_height = height
.checked_sub(max_bottom + 2 * (space_xxs as usize))
.unwrap_or(0);
if spacer_height > 0 {
if col != 0 {
grid = grid.insert_row();
}
grid = grid.push(widget::vertical_space(Length::Fixed(spacer_height as f32)));
}
}
2024-01-05 09:36:16 -07:00
}
2024-02-29 11:40:31 -07:00
2024-02-29 11:25:46 -07:00
widget::scrollable(
mouse_area::MouseArea::new(grid)
.on_drag(Message::Drag)
.show_drag_rect(true),
)
2024-02-29 16:25:54 -07:00
.id(self.scrollable_id.clone())
2024-02-29 11:25:46 -07:00
.on_scroll(Message::Scroll)
.width(Length::Fill)
.into()
2024-01-05 09:36:16 -07:00
}
pub fn list_view(&self) -> Element<Message> {
let cosmic_theme::Spacing {
space_m, space_xxs, ..
} = theme::active().cosmic().spacing;
2024-01-05 09:36:16 -07:00
2024-01-29 10:39:12 -07:00
//TODO: make adaptive?
let size = self.size_opt.unwrap_or_else(|| Size::new(0.0, 0.0));
let row_height = 40;
2024-02-13 12:49:37 -07:00
let modified_width = Length::Fixed(200.0);
let size_width = Length::Fixed(100.0);
2024-01-29 10:39:12 -07:00
2024-02-28 05:18:40 +01:00
macro_rules! heading_item {
($name: literal, $width: expr, $msg: expr) => {
widget::row::with_children(vec![
widget::text::heading(fl!($name)).into(),
2024-02-29 03:46:14 +01:00
widget::button(widget::icon::from_name(
match (self.sort_name == $msg, self.sort_direction) {
(true, true) => "go-down-symbolic",
(true, false) => "go-up-symbolic",
_ => "list-remove-symbolic",
},
))
2024-02-28 05:18:40 +01:00
.on_press(Message::ToggleSort($msg))
.style(cosmic::style::Button::Icon)
.into(),
])
2024-02-27 22:28:06 -07:00
.spacing(space_xxs)
2024-02-28 05:18:40 +01:00
.width($width)
.align_items(Alignment::Center)
.into()
};
}
2024-01-05 15:32:42 -07:00
2024-02-28 05:18:40 +01:00
let mut children: Vec<Element<_>> = Vec::new();
2024-01-05 15:32:42 -07:00
children.push(
widget::row::with_children(vec![
2024-02-28 05:18:40 +01:00
heading_item!("name", Length::Fill, HeadingOptions::Name),
2024-01-29 10:39:12 -07:00
//TODO: do not show modified column when in the trash
2024-02-28 05:18:40 +01:00
heading_item!("modified", modified_width, HeadingOptions::Modified),
heading_item!("size", size_width, HeadingOptions::Size),
2024-01-05 15:32:42 -07:00
])
.align_items(Alignment::Center)
.height(Length::Fixed(row_height as f32))
2024-01-05 15:32:42 -07:00
.padding(space_xxs)
.spacing(space_xxs)
.into(),
);
let mut y = row_height;
2024-01-05 15:32:42 -07:00
children.push(horizontal_rule(1).into());
y += 1;
2024-01-05 15:32:42 -07:00
2024-02-29 03:46:14 +01:00
if let Some(ref items) = self.column_sort() {
2024-01-05 09:36:16 -07:00
let mut count = 0;
let mut hidden = 0;
let TabConfig {
show_hidden,
icon_sizes,
} = self.config;
2024-01-05 09:36:16 -07:00
for (i, item) in items.iter().enumerate() {
if !show_hidden && item.hidden {
item.pos_opt.set(None);
2024-02-29 11:25:46 -07:00
item.rect_opt.set(None);
2024-01-05 09:36:16 -07:00
hidden += 1;
continue;
}
item.pos_opt.set(Some((count, 0)));
item.rect_opt.set(Some(Rectangle::new(
Point::new(space_m as f32, y as f32),
Size::new(size.width - (2 * space_m) as f32, row_height as f32),
)));
2024-01-05 09:36:16 -07:00
if count > 0 {
children.push(horizontal_rule(1).into());
y += 1;
}
2024-01-29 10:39:12 -07:00
let modified_text = match &item.metadata {
ItemMetadata::Path { metadata, .. } => match metadata.modified() {
2024-01-29 10:39:12 -07:00
Ok(time) => chrono::DateTime::<chrono::Local>::from(time)
2024-02-22 15:04:37 -07:00
.format(TIME_FORMAT)
2024-01-29 10:39:12 -07:00
.to_string(),
Err(_) => String::new(),
},
ItemMetadata::Trash { .. } => String::new(),
2024-01-29 10:39:12 -07:00
};
let size_text = match &item.metadata {
ItemMetadata::Path { metadata, children } => {
2024-01-29 10:39:12 -07:00
if metadata.is_dir() {
format!("{} items", children)
} else {
format_size(metadata.len())
}
}
ItemMetadata::Trash { metadata, .. } => match metadata.size {
2024-01-29 10:39:12 -07:00
trash::TrashItemSize::Entries(entries) => {
//TODO: translate
if entries == 1 {
format!("{} item", entries)
} else {
format!("{} items", entries)
}
}
trash::TrashItemSize::Bytes(bytes) => format_size(bytes),
},
};
2024-01-05 15:32:42 -07:00
//TODO: align columns
2024-01-05 15:10:46 -07:00
let button = widget::button(
widget::row::with_children(vec![
widget::icon::icon(item.icon_handle_list.clone())
.content_fit(ContentFit::Contain)
.size(icon_sizes.list())
.into(),
2024-01-29 10:39:12 -07:00
widget::text(item.name.clone()).width(Length::Fill).into(),
2024-02-13 12:49:37 -07:00
widget::text(modified_text).width(modified_width).into(),
widget::text(size_text).width(size_width).into(),
2024-01-05 15:10:46 -07:00
])
.align_items(Alignment::Center)
.spacing(space_xxs),
)
.height(Length::Fixed(row_height as f32))
2024-02-29 15:38:03 -07:00
.id(item.button_id.clone())
.padding(space_xxs)
.style(button_style(item.selected, true))
.on_press(Message::Click(Some(i)));
2024-01-05 15:10:46 -07:00
if self.context_menu.is_some() {
children.push(button.into());
} else {
children.push(
2024-02-26 12:05:29 -07:00
mouse_area::MouseArea::new(button)
.on_right_press_no_capture(move |_point_opt| Message::RightClick(i))
2024-01-05 15:10:46 -07:00
.into(),
);
}
count += 1;
y += row_height;
2024-01-03 15:27:32 -07:00
}
if count == 0 {
return self.empty_view(hidden > 0);
}
2024-01-03 15:27:32 -07:00
}
2024-02-01 15:55:52 -07:00
widget::scrollable(widget::column::with_children(children).padding([0, space_m]))
2024-02-29 16:25:54 -07:00
.id(self.scrollable_id.clone())
.on_scroll(Message::Scroll)
.width(Length::Fill)
.into()
2024-01-05 09:36:16 -07:00
}
pub fn view(&self, key_binds: &HashMap<KeyBind, Action>) -> Element<Message> {
let location_view = self.location_view();
2024-02-26 12:05:29 -07:00
let item_view = match self.view {
View::Grid => self.grid_view(),
View::List => self.list_view(),
2024-02-26 12:05:29 -07:00
};
let mut mouse_area =
mouse_area::MouseArea::new(widget::container(item_view).height(Length::Fill))
.on_press(move |_point_opt| Message::Click(None))
.on_back_press(move |_point_opt| Message::GoPrevious)
.on_forward_press(move |_point_opt| Message::GoNext)
2024-02-29 11:25:46 -07:00
.on_resize(Message::Resize);
2024-02-26 12:05:29 -07:00
if self.context_menu.is_some() {
mouse_area = mouse_area.on_right_press(move |_point_opt| Message::ContextMenu(None));
} else {
mouse_area =
mouse_area.on_right_press(move |point_opt| Message::ContextMenu(point_opt));
}
2024-02-27 09:52:39 -07:00
let mut popover = widget::popover(mouse_area);
if let Some(point) = self.context_menu {
2024-02-27 10:26:56 -07:00
popover = popover
.popup(menu::context_menu(&self, &key_binds))
2024-02-27 10:26:56 -07:00
.position(widget::popover::Position::Point(point));
2024-02-26 12:05:29 -07:00
}
widget::container(widget::column::with_children(vec![
location_view,
popover.into(),
]))
2024-01-05 11:18:38 -07:00
.height(Length::Fill)
2024-01-05 09:36:16 -07:00
.width(Length::Fill)
.into()
2024-01-03 15:27:32 -07:00
}
2024-02-22 16:17:39 -07:00
pub fn subscription(&self) -> Subscription<Message> {
if let Some(items) = &self.items_opt {
//TODO: how many thumbnail loads should be in flight at once?
let jobs = 8;
let mut subscriptions = Vec::with_capacity(jobs);
2024-02-29 16:25:54 -07:00
//TODO: move to function
let visible_rect = {
let point = match self.scroll_opt {
2024-02-29 16:25:54 -07:00
Some(offset) => Point::new(0.0, offset.y),
None => Point::new(0.0, 0.0),
};
let size = self.size_opt.unwrap_or_else(|| Size::new(0.0, 0.0));
Rectangle::new(point, size)
};
//TODO: HACK to ensure positions are up to date since subscription runs before view
let _ = match self.view {
View::Grid => self.grid_view(),
View::List => self.list_view(),
};
2024-02-22 16:17:39 -07:00
for item in items.iter() {
if item.thumbnail_res_opt.is_some() {
// Skip items that already have a thumbnail
continue;
}
match item.rect_opt.get() {
Some(rect) => {
if !rect.intersects(&visible_rect) {
// Skip items that are not visible
continue;
}
}
2024-02-22 16:17:39 -07:00
None => {
// Skip items with no determined rect (this should include hidden items)
continue;
}
}
let path = item.path.clone();
subscriptions.push(subscription::channel(
path.clone(),
1,
|mut output| async move {
let (path, thumbnail_res) = tokio::task::spawn_blocking(move || {
let start = std::time::Instant::now();
let thumbnail_res = match image::io::Reader::open(&path) {
Ok(reader) => match reader.decode() {
Ok(image) => {
//TODO: configurable thumbnail size
let thumbnail = image.thumbnail(64, 64);
Ok(thumbnail.to_rgba8())
}
2024-02-22 16:17:39 -07:00
Err(err) => {
log::warn!("failed to decode {:?}: {}", path, err);
Err(())
2024-02-22 16:17:39 -07:00
}
},
Err(err) => {
log::warn!("failed to read {:?}: {}", path, err);
Err(())
2024-02-22 16:17:39 -07:00
}
};
log::info!("thumbnailed {:?} in {:?}", path, start.elapsed());
(path, thumbnail_res)
})
.await
.unwrap();
match output
.send(Message::Thumbnail(path.clone(), thumbnail_res))
.await
{
Ok(()) => {}
Err(err) => {
log::warn!("failed to send thumbnail for {:?}: {}", path, err);
}
}
2024-02-22 16:17:39 -07:00
//TODO: how to properly kill this task?
loop {
tokio::time::sleep(std::time::Duration::new(1, 0)).await;
}
},
));
2024-02-22 16:17:39 -07:00
if subscriptions.len() >= jobs {
break;
}
}
Subscription::batch(subscriptions)
} else {
Subscription::none()
}
}
2024-01-03 15:27:32 -07:00
}
#[cfg(test)]
mod tests {
2024-02-05 20:58:17 -05:00
use std::{io, path::PathBuf};
use cosmic::iced_runtime::keyboard::Modifiers;
2024-02-05 20:58:17 -05:00
use log::{debug, trace};
use tempfile::TempDir;
use test_log::test;
2024-02-27 09:58:22 -07:00
use super::{scan_path, Location, Message, Tab};
use crate::{
app::test_utils::{
2024-02-27 09:58:22 -07:00
assert_eq_tab_path, empty_fs, eq_path_item, filter_dirs, read_dir_sorted, simple_fs,
tab_click_new, NAME_LEN, NUM_DIRS, NUM_FILES, NUM_HIDDEN, NUM_NESTED,
},
config::{IconSizes, TabConfig},
};
// Boilerplate for tab tests. Checks if simulated clicks selected items.
fn tab_selects_item(
clicks: &[usize],
modifiers: Modifiers,
expected_selected: &[bool],
) -> io::Result<()> {
let (_fs, mut tab) = tab_click_new(NUM_FILES, NUM_NESTED, NUM_DIRS, NUM_NESTED, NAME_LEN)?;
// Simulate clicks by triggering Message::Click
for &click in clicks {
debug!("Emitting Message::Click(Some({click})) with modifiers: {modifiers:?}");
tab.update(Message::Click(Some(click)), modifiers);
}
let items = tab
.items_opt
.as_deref()
.expect("tab should be populated with items");
for (i, (&expected, actual)) in expected_selected.into_iter().zip(items).enumerate() {
assert_eq!(
expected,
actual.selected,
"expected index {i} to be {}",
if expected {
"selected but it was deselected"
} else {
"deselected but it was selected"
}
);
}
Ok(())
}
2024-02-05 20:58:17 -05:00
fn tab_history() -> io::Result<(TempDir, Tab, Vec<PathBuf>)> {
let fs = simple_fs(NUM_FILES, NUM_NESTED, NUM_DIRS, NUM_NESTED, NAME_LEN)?;
let path = fs.path();
let mut tab = Tab::new(Location::Path(path.into()), TabConfig::default());
2024-02-05 20:58:17 -05:00
// All directories (simple_fs only produces one nested layer)
let dirs: Vec<PathBuf> = filter_dirs(path)?
.flat_map(|dir| {
filter_dirs(&dir).map(|nested_dirs| std::iter::once(dir).chain(nested_dirs))
})
.flatten()
.collect();
assert!(
dirs.len() == NUM_DIRS + NUM_DIRS * NUM_NESTED,
"Sanity check: Have {} dirs instead of {}",
dirs.len(),
NUM_DIRS + NUM_DIRS * NUM_NESTED
);
debug!("Building history by emitting Message::Location");
for dir in &dirs {
debug!(
"Emitting Message::Location(Location::Path(\"{}\"))",
dir.display()
);
tab.update(
Message::Location(Location::Path(dir.clone())),
Modifiers::empty(),
);
}
trace!("Tab history: {:?}", tab.history);
Ok((fs, tab, dirs))
}
#[test]
fn scan_path_succeeds_on_valid_path() -> io::Result<()> {
2024-02-01 23:31:50 -05:00
let fs = simple_fs(NUM_FILES, NUM_HIDDEN, NUM_DIRS, NUM_NESTED, NAME_LEN)?;
let path = fs.path();
// Read directory entries and sort as cosmic-files does
let entries = read_dir_sorted(path)?;
debug!("Calling scan_path(\"{}\")", path.display());
let actual = scan_path(&path.to_owned(), IconSizes::default());
// scan_path shouldn't skip any entries
assert_eq!(entries.len(), actual.len());
// Correct files should be scanned
assert!(entries
.into_iter()
.zip(actual.into_iter())
.all(|(path, item)| eq_path_item(&path, &item)));
Ok(())
}
#[test]
fn scan_path_returns_empty_vec_for_invalid_path() -> io::Result<()> {
2024-02-01 23:31:50 -05:00
let fs = simple_fs(NUM_FILES, NUM_NESTED, NUM_DIRS, NUM_NESTED, NAME_LEN)?;
let path = fs.path();
// A nonexisting path within the temp dir
let invalid_path = path.join("ferris");
assert!(!invalid_path.exists());
debug!("Calling scan_path(\"{}\")", invalid_path.display());
let actual = scan_path(&invalid_path, IconSizes::default());
assert!(actual.is_empty());
Ok(())
}
#[test]
fn scan_path_empty_dir_returns_empty_vec() -> io::Result<()> {
let fs = empty_fs()?;
let path = fs.path();
debug!("Calling scan_path(\"{}\")", path.display());
let actual = scan_path(&path.to_owned(), IconSizes::default());
assert_eq!(0, path.read_dir()?.count());
assert!(actual.is_empty());
Ok(())
}
#[test]
fn tab_location_changes_location() -> io::Result<()> {
let fs = simple_fs(NUM_FILES, NUM_NESTED, NUM_DIRS, NUM_NESTED, NAME_LEN)?;
let path = fs.path();
// Next directory in temp directory
2024-02-05 20:58:17 -05:00
// This does not have to be sorted
let next_dir = filter_dirs(path)?
.next()
.expect("temp directory should have at least one directory");
let mut tab = Tab::new(Location::Path(path.to_owned()), TabConfig::default());
debug!(
"Emitting Message::Location(Location::Path(\"{}\"))",
next_dir.display()
);
tab.update(
Message::Location(Location::Path(next_dir.clone())),
Modifiers::empty(),
);
// Validate that the tab's path updated
// NOTE: `items_opt` is set to None with Message::Location so this ONLY checks for equal paths
// If item contents are NOT None then this needs to be reevaluated for correctness
assert_eq_tab_path(&tab, &next_dir);
assert!(
tab.items_opt.is_none(),
"Tab's `items` is not None which means this test needs to be updated"
);
Ok(())
}
#[test]
fn tab_click_single_selects_item() -> io::Result<()> {
// Select the second directory with no keys held down
tab_selects_item(&[1], Modifiers::empty(), &[false, true])
}
#[test]
fn tab_click_double_opens_folder() -> io::Result<()> {
let (fs, mut tab) = tab_click_new(NUM_FILES, NUM_NESTED, NUM_DIRS, NUM_NESTED, NAME_LEN)?;
let path = fs.path();
// Simulate double clicking second directory
debug!("Emitting first Message::Click(Some(1))");
tab.update(Message::Click(Some(1)), Modifiers::empty());
debug!("Emitting second Message::Click(Some(1))");
tab.update(Message::Click(Some(1)), Modifiers::empty());
// Path to second directory
let second_dir = read_dir_sorted(path)?
2024-02-05 20:58:17 -05:00
.into_iter()
.filter(|p| p.is_dir())
.nth(1)
.expect("should be at least two directories");
// Location should have changed to second_dir
assert_eq_tab_path(&tab, &second_dir);
Ok(())
}
#[test]
fn tab_click_ctrl_selects_multiple() -> io::Result<()> {
// Select the first and second directory by holding down ctrl
tab_selects_item(&[0, 1], Modifiers::CTRL, &[true, true])
}
#[test]
fn tab_gonext_moves_forward_in_history() -> io::Result<()> {
2024-02-05 20:58:17 -05:00
let (fs, mut tab, dirs) = tab_history()?;
let path = fs.path();
// Rewind to the start
for _ in 0..dirs.len() {
debug!("Emitting Message::GoPrevious to rewind to the start",);
tab.update(Message::GoPrevious, Modifiers::empty());
}
assert_eq_tab_path(&tab, path);
// Back to the future. Directories should be in the order they were opened.
for dir in dirs {
debug!("Emitting Message::GoNext",);
tab.update(Message::GoNext, Modifiers::empty());
assert_eq_tab_path(&tab, &dir);
}
Ok(())
}
#[test]
fn tab_goprev_moves_backward_in_history() -> io::Result<()> {
2024-02-05 20:58:17 -05:00
let (fs, mut tab, dirs) = tab_history()?;
let path = fs.path();
for dir in dirs.into_iter().rev() {
assert_eq_tab_path(&tab, &dir);
debug!("Emitting Message::GoPrevious",);
tab.update(Message::GoPrevious, Modifiers::empty());
}
assert_eq_tab_path(&tab, path);
Ok(())
}
#[test]
fn tab_empty_history_does_nothing_on_prev_next() -> io::Result<()> {
2024-02-05 20:58:17 -05:00
let fs = simple_fs(0, NUM_NESTED, NUM_DIRS, 0, NAME_LEN)?;
let path = fs.path();
let mut tab = Tab::new(Location::Path(path.into()), TabConfig::default());
2024-02-05 20:58:17 -05:00
// Tab's location shouldn't change if GoPrev or GoNext is triggered
debug!("Emitting Message::GoPrevious",);
tab.update(Message::GoPrevious, Modifiers::empty());
assert_eq_tab_path(&tab, path);
debug!("Emitting Message::GoNext",);
tab.update(Message::GoNext, Modifiers::empty());
assert_eq_tab_path(&tab, path);
Ok(())
}
#[test]
fn tab_locationup_moves_up_hierarchy() -> io::Result<()> {
let fs = simple_fs(0, NUM_NESTED, NUM_DIRS, 0, NAME_LEN)?;
let path = fs.path();
let mut next_dir = filter_dirs(path)?
.next()
.expect("should be at least one directory");
let mut tab = Tab::new(Location::Path(next_dir.clone()), TabConfig::default());
// This will eventually yield false once root is hit
while next_dir.pop() {
debug!("Emitting Message::LocationUp",);
tab.update(Message::LocationUp, Modifiers::empty());
assert_eq_tab_path(&tab, &next_dir);
}
Ok(())
}
}