2024-01-03 15:27:32 -07:00
|
|
|
use cosmic::{
|
|
|
|
|
app::Core,
|
|
|
|
|
cosmic_theme,
|
2024-01-05 08:55:18 -07:00
|
|
|
iced::{
|
|
|
|
|
alignment::{Horizontal, Vertical},
|
2024-01-10 10:47:12 -07:00
|
|
|
keyboard::Modifiers,
|
2024-01-29 11:09:05 -07:00
|
|
|
//TODO: export in cosmic::widget
|
|
|
|
|
widget::horizontal_rule,
|
|
|
|
|
Alignment,
|
|
|
|
|
Length,
|
|
|
|
|
Point,
|
2024-01-05 08:55:18 -07:00
|
|
|
},
|
2024-01-03 15:27:32 -07:00
|
|
|
theme, widget, Element,
|
|
|
|
|
};
|
2024-01-24 09:30:06 -05:00
|
|
|
use once_cell::sync::Lazy;
|
2024-01-03 15:27:32 -07:00
|
|
|
use std::{
|
|
|
|
|
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,
|
|
|
|
|
process,
|
|
|
|
|
time::{Duration, Instant},
|
|
|
|
|
};
|
|
|
|
|
|
2024-01-05 08:55:18 -07:00
|
|
|
use crate::{fl, mime_icon::mime_icon};
|
2024-01-03 15:33:28 -07:00
|
|
|
|
2024-01-03 15:27:32 -07:00
|
|
|
const DOUBLE_CLICK_DURATION: Duration = Duration::from_millis(500);
|
2024-01-05 09:36:16 -07:00
|
|
|
//TODO: configurable
|
2024-02-01 15:55:52 -07:00
|
|
|
const ICON_SIZE_DIALOG: u16 = 16;
|
2024-01-05 09:36:16 -07:00
|
|
|
const ICON_SIZE_LIST: u16 = 32;
|
|
|
|
|
const ICON_SIZE_GRID: u16 = 64;
|
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-01-05 09:36:16 -07:00
|
|
|
fn button_style(selected: bool) -> theme::Button {
|
|
|
|
|
//TODO: move to libcosmic
|
|
|
|
|
theme::Button::Custom {
|
|
|
|
|
active: Box::new(move |focused, theme| {
|
|
|
|
|
let mut appearance =
|
|
|
|
|
widget::button::StyleSheet::active(theme, focused, &theme::Button::MenuItem);
|
|
|
|
|
if !selected {
|
|
|
|
|
appearance.background = None;
|
|
|
|
|
}
|
|
|
|
|
appearance
|
|
|
|
|
}),
|
|
|
|
|
disabled: Box::new(move |theme| {
|
|
|
|
|
let mut appearance =
|
|
|
|
|
widget::button::StyleSheet::disabled(theme, &theme::Button::MenuItem);
|
|
|
|
|
if !selected {
|
|
|
|
|
appearance.background = None;
|
|
|
|
|
}
|
|
|
|
|
appearance
|
|
|
|
|
}),
|
|
|
|
|
hovered: Box::new(move |focused, theme| {
|
|
|
|
|
widget::button::StyleSheet::hovered(theme, focused, &theme::Button::MenuItem)
|
|
|
|
|
}),
|
|
|
|
|
pressed: Box::new(move |focused, theme| {
|
|
|
|
|
widget::button::StyleSheet::pressed(theme, focused, &theme::Button::MenuItem)
|
|
|
|
|
}),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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)
|
2024-01-05 08:55:18 -07:00
|
|
|
.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()
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-31 14:15:21 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2024-01-03 15:41:01 -07:00
|
|
|
#[cfg(target_os = "linux")]
|
2024-01-04 16:08:22 -07:00
|
|
|
fn open_command(path: &PathBuf) -> process::Command {
|
2024-01-03 15:41:01 -07:00
|
|
|
let mut command = process::Command::new("xdg-open");
|
|
|
|
|
command.arg(path);
|
|
|
|
|
command
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(target_os = "macos")]
|
2024-01-04 16:08:22 -07:00
|
|
|
fn open_command(path: &PathBuf) -> process::Command {
|
2024-01-03 15:41:01 -07:00
|
|
|
let mut command = process::Command::new("open");
|
|
|
|
|
command.arg(path);
|
|
|
|
|
command
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(target_os = "redox")]
|
2024-01-04 16:08:22 -07:00
|
|
|
fn open_command(path: &PathBuf) -> process::Command {
|
2024-01-03 15:41:01 -07:00
|
|
|
let mut command = process::Command::new("launcher");
|
|
|
|
|
command.arg(path);
|
|
|
|
|
command
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(target_os = "windows")]
|
2024-01-04 16:08:22 -07:00
|
|
|
fn open_command(path: &PathBuf) -> process::Command {
|
2024-01-28 06:23:40 -05:00
|
|
|
use std::os::windows::process::CommandExt;
|
|
|
|
|
|
2024-01-03 15:41:01 -07:00
|
|
|
let mut command = process::Command::new("cmd");
|
2024-01-28 06:23:40 -05:00
|
|
|
|
|
|
|
|
command
|
|
|
|
|
.arg("/c")
|
|
|
|
|
.arg("start")
|
|
|
|
|
.raw_arg("\"\"")
|
|
|
|
|
.arg(path)
|
|
|
|
|
.creation_flags(0x08000000);
|
2024-01-03 15:41:01 -07:00
|
|
|
command
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-05 16:17:23 -07:00
|
|
|
pub fn scan_path(tab_path: &PathBuf) -> Vec<Item> {
|
2024-01-05 08:55:18 -07:00
|
|
|
let mut items = Vec::new();
|
2024-01-05 16:17:23 -07:00
|
|
|
match fs::read_dir(tab_path) {
|
2024-01-05 08:55:18 -07:00
|
|
|
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,
|
2024-01-05 08:55:18 -07:00
|
|
|
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);
|
|
|
|
|
|
2024-01-05 08:55:18 -07:00
|
|
|
let path = entry.path();
|
2024-01-05 14:44:20 -07:00
|
|
|
|
2024-01-05 08:55:18 -07:00
|
|
|
//TODO: configurable size
|
2024-02-01 15:55:52 -07:00
|
|
|
let (icon_handle_dialog, icon_handle_grid, icon_handle_list) = if metadata.is_dir()
|
|
|
|
|
{
|
2024-01-05 09:36:16 -07:00
|
|
|
(
|
2024-02-01 15:55:52 -07:00
|
|
|
folder_icon(&path, ICON_SIZE_DIALOG),
|
2024-01-05 09:36:16 -07:00
|
|
|
folder_icon(&path, ICON_SIZE_GRID),
|
|
|
|
|
folder_icon(&path, ICON_SIZE_LIST),
|
|
|
|
|
)
|
2024-01-05 08:55:18 -07:00
|
|
|
} else {
|
2024-01-05 09:36:16 -07:00
|
|
|
(
|
2024-02-01 15:55:52 -07:00
|
|
|
mime_icon(&path, ICON_SIZE_DIALOG),
|
2024-01-05 09:36:16 -07:00
|
|
|
mime_icon(&path, ICON_SIZE_GRID),
|
|
|
|
|
mime_icon(&path, ICON_SIZE_LIST),
|
|
|
|
|
)
|
2024-01-05 08:55:18 -07:00
|
|
|
};
|
|
|
|
|
|
2024-01-06 20:31:00 -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
|
|
|
|
|
};
|
|
|
|
|
|
2024-01-05 08:55:18 -07:00
|
|
|
items.push(Item {
|
|
|
|
|
name,
|
2024-01-30 10:47:41 -07:00
|
|
|
metadata: ItemMetadata::Path { metadata, children },
|
2024-01-05 08:55:18 -07:00
|
|
|
hidden,
|
2024-01-05 14:44:20 -07:00
|
|
|
path,
|
2024-02-01 15:55:52 -07:00
|
|
|
icon_handle_dialog,
|
2024-01-05 09:36:16 -07:00
|
|
|
icon_handle_grid,
|
|
|
|
|
icon_handle_list,
|
2024-01-10 09:47:47 -07:00
|
|
|
selected: false,
|
|
|
|
|
click_time: None,
|
2024-01-05 08:55:18 -07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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() -> Vec<Item> {
|
|
|
|
|
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();
|
2024-01-30 10:47:41 -07:00
|
|
|
let name = entry.name.clone();
|
2024-01-05 16:17:23 -07:00
|
|
|
|
|
|
|
|
//TODO: configurable size
|
2024-02-01 15:55:52 -07:00
|
|
|
let (icon_handle_dialog, icon_handle_grid, icon_handle_list) = match metadata.size {
|
2024-01-10 08:53:22 -07:00
|
|
|
trash::TrashItemSize::Entries(_) => (
|
2024-02-01 15:55:52 -07:00
|
|
|
folder_icon(&path, ICON_SIZE_DIALOG),
|
2024-01-06 12:59:36 -07:00
|
|
|
folder_icon(&path, ICON_SIZE_GRID),
|
|
|
|
|
folder_icon(&path, ICON_SIZE_LIST),
|
2024-01-10 08:53:22 -07:00
|
|
|
),
|
|
|
|
|
trash::TrashItemSize::Bytes(_) => (
|
2024-02-01 15:55:52 -07:00
|
|
|
mime_icon(&path, ICON_SIZE_DIALOG),
|
2024-01-06 12:59:36 -07:00
|
|
|
mime_icon(&path, ICON_SIZE_GRID),
|
|
|
|
|
mime_icon(&path, ICON_SIZE_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,
|
2024-01-30 10:47:41 -07:00
|
|
|
metadata: ItemMetadata::Trash { metadata, entry },
|
2024-01-05 16:17:23 -07:00
|
|
|
hidden: false,
|
|
|
|
|
path,
|
2024-02-01 15:55:52 -07:00
|
|
|
icon_handle_dialog,
|
2024-01-05 16:17:23 -07:00
|
|
|
icon_handle_grid,
|
|
|
|
|
icon_handle_list,
|
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()) {
|
2024-01-05 08:55:18 -07:00
|
|
|
(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),
|
2024-01-05 08:55:18 -07:00
|
|
|
});
|
|
|
|
|
items
|
2024-01-03 15:27:32 -07:00
|
|
|
}
|
|
|
|
|
|
2024-01-09 15:42:42 -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) -> Vec<Item> {
|
|
|
|
|
match self {
|
|
|
|
|
Self::Path(path) => scan_path(path),
|
|
|
|
|
Self::Trash => scan_trash(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-09 15:34:48 -07:00
|
|
|
#[derive(Clone, Debug)]
|
2024-01-05 09:36:16 -07:00
|
|
|
pub enum Message {
|
2024-01-10 10:26:30 -07:00
|
|
|
Click(Option<usize>),
|
2024-01-29 11:58:36 -07:00
|
|
|
EditLocation(Option<Location>),
|
2024-01-30 10:47:41 -07:00
|
|
|
GoNext,
|
|
|
|
|
GoPrevious,
|
2024-01-09 15:34:48 -07:00
|
|
|
Location(Location),
|
2024-01-10 10:26:30 -07:00
|
|
|
RightClick(usize),
|
2024-01-05 15:17:38 -07:00
|
|
|
View(View),
|
2024-01-05 09:36:16 -07:00
|
|
|
}
|
|
|
|
|
|
2024-01-06 12:59:36 -07:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
|
pub enum ItemMetadata {
|
2024-01-30 10:47:41 -07:00
|
|
|
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 {
|
2024-01-30 10:47:41 -07:00
|
|
|
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-01 15:55:52 -07:00
|
|
|
pub icon_handle_dialog: widget::icon::Handle,
|
2024-01-05 09:36:16 -07:00
|
|
|
pub icon_handle_grid: widget::icon::Handle,
|
|
|
|
|
pub icon_handle_list: widget::icon::Handle,
|
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 {
|
2024-02-01 15:14:14 -07:00
|
|
|
pub fn property_view(&self, core: &Core) -> Element<crate::app::Message> {
|
2024-01-05 14:58:50 -07:00
|
|
|
let mut section = widget::settings::view_section("");
|
|
|
|
|
section = section.add(widget::settings::item::item_row(vec![
|
|
|
|
|
widget::icon::icon(self.icon_handle_list.clone())
|
|
|
|
|
.size(ICON_SIZE_LIST)
|
2024-01-05 14:44:20 -07:00
|
|
|
.into(),
|
2024-01-05 14:58:50 -07:00
|
|
|
widget::text(self.name.clone()).into(),
|
|
|
|
|
]));
|
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 {
|
2024-01-30 10:47:41 -07:00
|
|
|
ItemMetadata::Path { metadata, children } => {
|
2024-01-06 20:31:00 -07:00
|
|
|
if metadata.is_dir() {
|
|
|
|
|
section = section.add(widget::settings::item::item(
|
|
|
|
|
"Items",
|
|
|
|
|
widget::text(format!("{}", children)),
|
|
|
|
|
));
|
|
|
|
|
} else {
|
2024-01-06 12:59:36 -07:00
|
|
|
section = section.add(widget::settings::item::item(
|
|
|
|
|
"Size",
|
|
|
|
|
widget::text(format_size(metadata.len())),
|
|
|
|
|
));
|
|
|
|
|
}
|
2024-01-05 14:44:20 -07:00
|
|
|
|
2024-01-06 12:59:36 -07:00
|
|
|
if let Ok(time) = metadata.accessed() {
|
|
|
|
|
section = section.add(widget::settings::item(
|
|
|
|
|
"Accessed",
|
|
|
|
|
widget::text(
|
|
|
|
|
chrono::DateTime::<chrono::Local>::from(time)
|
|
|
|
|
.format("%c")
|
|
|
|
|
.to_string(),
|
|
|
|
|
),
|
|
|
|
|
));
|
|
|
|
|
}
|
2024-01-05 14:44:20 -07:00
|
|
|
|
2024-01-06 12:59:36 -07:00
|
|
|
if let Ok(time) = metadata.modified() {
|
|
|
|
|
section = section.add(widget::settings::item(
|
|
|
|
|
"Modified",
|
|
|
|
|
widget::text(
|
|
|
|
|
chrono::DateTime::<chrono::Local>::from(time)
|
|
|
|
|
.format("%c")
|
|
|
|
|
.to_string(),
|
|
|
|
|
),
|
|
|
|
|
));
|
|
|
|
|
}
|
2024-01-05 14:44:20 -07:00
|
|
|
|
2024-01-06 12:59:36 -07:00
|
|
|
if let Ok(time) = metadata.created() {
|
|
|
|
|
section = section.add(widget::settings::item(
|
|
|
|
|
"Created",
|
|
|
|
|
widget::text(
|
|
|
|
|
chrono::DateTime::<chrono::Local>::from(time)
|
|
|
|
|
.format("%c")
|
|
|
|
|
.to_string(),
|
|
|
|
|
),
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-30 10:47:41 -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-01-05 14:58:50 -07:00
|
|
|
section.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)
|
|
|
|
|
// icon_handles
|
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,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
|
pub struct Tab {
|
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 items_opt: Option<Vec<Item>>,
|
|
|
|
|
pub view: View,
|
2024-02-01 15:55:52 -07:00
|
|
|
pub dialog: bool,
|
2024-01-29 11:58:36 -07:00
|
|
|
pub edit_location: Option<Location>,
|
2024-01-30 10:47:41 -07:00
|
|
|
pub history_i: usize,
|
|
|
|
|
pub history: Vec<Location>,
|
2024-01-05 09:36:16 -07:00
|
|
|
}
|
|
|
|
|
|
2024-01-03 15:27:32 -07:00
|
|
|
impl Tab {
|
2024-01-05 16:17:23 -07:00
|
|
|
pub fn new(location: Location) -> Self {
|
2024-01-30 10:47:41 -07:00
|
|
|
let history = vec![location.clone()];
|
2024-01-05 08:55:18 -07:00
|
|
|
Self {
|
2024-01-05 16:17:23 -07:00
|
|
|
location,
|
2024-01-03 15:27:32 -07:00
|
|
|
context_menu: None,
|
2024-01-05 08:55:18 -07:00
|
|
|
items_opt: None,
|
2024-01-06 20:31:00 -07:00
|
|
|
view: View::List,
|
2024-02-01 15:55:52 -07:00
|
|
|
dialog: false,
|
2024-01-29 11:58:36 -07:00
|
|
|
edit_location: None,
|
2024-01-30 10:47:41 -07:00
|
|
|
history_i: 0,
|
|
|
|
|
history,
|
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-01-10 10:47:12 -07:00
|
|
|
pub fn update(&mut self, message: Message, modifiers: Modifiers) -> bool {
|
2024-01-03 15:27:32 -07:00
|
|
|
let mut cd = None;
|
2024-01-30 10:47:41 -07:00
|
|
|
let mut history_i_opt = None;
|
2024-01-03 15:27:32 -07:00
|
|
|
match message {
|
2024-01-10 10:26:30 -07:00
|
|
|
Message::Click(click_i_opt) => {
|
2024-01-05 08:55:18 -07:00
|
|
|
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-01-10 09:47:47 -07:00
|
|
|
item.selected = true;
|
2024-01-10 10:26:30 -07:00
|
|
|
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() {
|
|
|
|
|
cd = Some(Location::Path(item.path.clone()));
|
2024-02-01 15:57:01 -07:00
|
|
|
} else if !self.dialog {
|
2024-01-10 10:26:30 -07:00
|
|
|
let mut command = open_command(&item.path);
|
|
|
|
|
match command.spawn() {
|
|
|
|
|
Ok(_) => (),
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::warn!(
|
|
|
|
|
"failed to open {:?}: {}",
|
|
|
|
|
item.path,
|
|
|
|
|
err
|
|
|
|
|
);
|
2024-01-10 09:47:47 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-10 10:26:30 -07:00
|
|
|
}
|
|
|
|
|
Location::Trash => {
|
|
|
|
|
//TODO: open properties?
|
2024-01-03 15:41:01 -07:00
|
|
|
}
|
|
|
|
|
}
|
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());
|
2024-01-10 10:47:12 -07:00
|
|
|
} else if modifiers.contains(Modifiers::CTRL) {
|
|
|
|
|
// Holding control allows multiple selection
|
|
|
|
|
item.click_time = None;
|
2024-01-05 08:55:18 -07:00
|
|
|
} 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
|
|
|
}
|
2024-01-29 11:58:36 -07:00
|
|
|
Message::EditLocation(edit_location) => {
|
|
|
|
|
self.edit_location = edit_location;
|
|
|
|
|
}
|
2024-01-30 10:47:41 -07:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-09 15:34:48 -07:00
|
|
|
Message::Location(location) => {
|
|
|
|
|
cd = Some(location);
|
2024-01-03 17:04:08 -07:00
|
|
|
}
|
2024-01-10 10:26:30 -07:00
|
|
|
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() {
|
2024-01-10 10:47:12 -07:00
|
|
|
if i == click_i {
|
|
|
|
|
item.selected = true;
|
|
|
|
|
} else if modifiers.contains(Modifiers::CTRL) {
|
|
|
|
|
// Holding control allows multiple selection
|
|
|
|
|
} else {
|
|
|
|
|
item.selected = false;
|
|
|
|
|
}
|
|
|
|
|
item.click_time = None;
|
2024-01-10 10:26:30 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-05 15:17:38 -07:00
|
|
|
Message::View(view) => {
|
|
|
|
|
self.view = view;
|
|
|
|
|
}
|
2024-01-03 15:27:32 -07:00
|
|
|
}
|
2024-01-30 10:47:41 -07:00
|
|
|
if let Some(mut location) = cd {
|
2024-01-09 15:42:42 -07:00
|
|
|
if location != self.location {
|
2024-01-30 10:47:41 -07:00
|
|
|
self.location = location.clone();
|
2024-01-09 15:42:42 -07:00
|
|
|
self.items_opt = None;
|
2024-01-29 11:58:36 -07:00
|
|
|
self.edit_location = None;
|
2024-01-30 10:47:41 -07:00
|
|
|
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();
|
|
|
|
|
self.history.push(location);
|
|
|
|
|
}
|
2024-01-09 15:42:42 -07:00
|
|
|
true
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
2024-01-03 15:27:32 -07:00
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-30 10:47:41 -07:00
|
|
|
pub fn location_view(&self, core: &Core) -> Element<Message> {
|
2024-01-29 11:58:36 -07:00
|
|
|
let cosmic_theme::Spacing {
|
|
|
|
|
space_xxxs,
|
|
|
|
|
space_xxs,
|
2024-01-30 10:47:41 -07:00
|
|
|
space_s,
|
2024-01-29 11:58:36 -07:00
|
|
|
..
|
|
|
|
|
} = core.system_theme().cosmic().spacing;
|
|
|
|
|
|
2024-01-30 10:47:41 -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) => {
|
2024-01-30 10:47:41 -07:00
|
|
|
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)
|
2024-01-30 10:47:41 -07:00
|
|
|
.style(theme::Button::Icon),
|
|
|
|
|
);
|
|
|
|
|
row = row.push(
|
2024-01-29 11:58:36 -07:00
|
|
|
widget::text_input("", path.to_string_lossy())
|
|
|
|
|
.on_input(|input| {
|
|
|
|
|
Message::EditLocation(Some(Location::Path(PathBuf::from(input))))
|
|
|
|
|
})
|
2024-01-30 10:47:41 -07:00
|
|
|
.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 {
|
2024-01-30 10:47:41 -07:00
|
|
|
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")
|
2024-01-29 11:09:05 -07:00
|
|
|
} 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 {
|
2024-01-29 11:09:05 -07:00
|
|
|
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)))
|
2024-01-29 11:09:05 -07:00
|
|
|
.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
|
|
|
|
2024-01-30 10:47:41 -07:00
|
|
|
for child in children {
|
|
|
|
|
row = row.push(child);
|
|
|
|
|
}
|
|
|
|
|
row.into()
|
2024-01-10 12:57:30 -07:00
|
|
|
}
|
|
|
|
|
|
2024-01-05 09:36:16 -07:00
|
|
|
pub fn empty_view(&self, has_hidden: bool, core: &Core) -> Element<Message> {
|
2024-01-03 15:27:32 -07:00
|
|
|
let cosmic_theme::Spacing { space_xxs, .. } = core.system_theme().cosmic().spacing;
|
|
|
|
|
|
2024-01-30 11:32:47 -07:00
|
|
|
widget::column::with_children(vec![
|
|
|
|
|
self.location_view(core),
|
|
|
|
|
widget::container(
|
|
|
|
|
widget::column::with_children(vec![
|
|
|
|
|
widget::icon::from_name("folder-symbolic")
|
|
|
|
|
.size(64)
|
|
|
|
|
.icon()
|
|
|
|
|
.into(),
|
|
|
|
|
widget::text(if has_hidden {
|
|
|
|
|
fl!("empty-folder-hidden")
|
|
|
|
|
} else {
|
|
|
|
|
fl!("empty-folder")
|
|
|
|
|
})
|
2024-01-05 09:36:16 -07:00
|
|
|
.into(),
|
2024-01-30 11:32:47 -07:00
|
|
|
])
|
|
|
|
|
.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, core: &Core) -> Element<Message> {
|
|
|
|
|
let cosmic_theme::Spacing { space_xxs, .. } = core.system_theme().cosmic().spacing;
|
|
|
|
|
|
2024-01-29 11:25:43 -07:00
|
|
|
//TODO: get from config
|
|
|
|
|
let item_width = Length::Fixed(96.0);
|
|
|
|
|
let item_height = Length::Fixed(116.0);
|
|
|
|
|
|
2024-01-05 09:36:16 -07:00
|
|
|
let mut children: Vec<Element<_>> = Vec::new();
|
2024-01-05 08:55:18 -07:00
|
|
|
if let Some(ref items) = self.items_opt {
|
|
|
|
|
let mut count = 0;
|
|
|
|
|
let mut hidden = 0;
|
|
|
|
|
for (i, item) in items.iter().enumerate() {
|
|
|
|
|
if item.hidden {
|
|
|
|
|
hidden += 1;
|
|
|
|
|
//TODO: SHOW HIDDEN OPTION
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-05 15:10:46 -07:00
|
|
|
let button = widget::button(
|
|
|
|
|
widget::column::with_children(vec![
|
|
|
|
|
widget::icon::icon(item.icon_handle_grid.clone())
|
|
|
|
|
.size(ICON_SIZE_GRID)
|
|
|
|
|
.into(),
|
|
|
|
|
widget::text(item.name.clone()).into(),
|
|
|
|
|
])
|
|
|
|
|
.align_items(Alignment::Center)
|
|
|
|
|
.spacing(space_xxs)
|
2024-01-29 11:25:43 -07:00
|
|
|
.height(item_height)
|
|
|
|
|
.width(item_width),
|
2024-01-05 15:10:46 -07:00
|
|
|
)
|
2024-01-10 09:47:47 -07:00
|
|
|
.style(button_style(item.selected))
|
2024-01-10 10:26:30 -07:00
|
|
|
.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(
|
|
|
|
|
crate::mouse_area::MouseArea::new(button)
|
2024-01-10 10:26:30 -07:00
|
|
|
.on_right_press_no_capture(move |_point_opt| Message::RightClick(i))
|
2024-01-05 15:10:46 -07:00
|
|
|
.into(),
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-01-05 09:36:16 -07:00
|
|
|
count += 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if count == 0 {
|
|
|
|
|
return self.empty_view(hidden > 0, core);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-01 15:55:52 -07:00
|
|
|
widget::column::with_children(vec![
|
2024-01-30 10:47:41 -07:00
|
|
|
self.location_view(core),
|
2024-02-01 15:55:52 -07:00
|
|
|
widget::scrollable(widget::flex_row(children))
|
|
|
|
|
.width(Length::Fill)
|
|
|
|
|
.into(),
|
|
|
|
|
])
|
2024-01-29 11:22:21 -07:00
|
|
|
.into()
|
2024-01-05 09:36:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn list_view(&self, core: &Core) -> Element<Message> {
|
|
|
|
|
let cosmic_theme::Spacing { space_xxs, .. } = core.system_theme().cosmic().spacing;
|
|
|
|
|
|
2024-01-29 10:39:12 -07:00
|
|
|
//TODO: make adaptive?
|
|
|
|
|
let column_width = Length::Fixed(200.0);
|
|
|
|
|
|
2024-01-05 09:36:16 -07:00
|
|
|
let mut children: Vec<Element<_>> = Vec::new();
|
2024-01-05 15:32:42 -07:00
|
|
|
|
|
|
|
|
children.push(
|
|
|
|
|
widget::row::with_children(vec![
|
2024-01-29 10:39:12 -07:00
|
|
|
widget::text::heading(fl!("name"))
|
|
|
|
|
.width(Length::Fill)
|
|
|
|
|
.into(),
|
|
|
|
|
//TODO: do not show modified column when in the trash
|
|
|
|
|
widget::text::heading(fl!("modified"))
|
|
|
|
|
.width(column_width)
|
|
|
|
|
.into(),
|
|
|
|
|
widget::text::heading(fl!("size"))
|
|
|
|
|
.width(column_width)
|
|
|
|
|
.into(),
|
2024-01-05 15:32:42 -07:00
|
|
|
])
|
|
|
|
|
.align_items(Alignment::Center)
|
|
|
|
|
.padding(space_xxs)
|
|
|
|
|
.spacing(space_xxs)
|
|
|
|
|
.into(),
|
|
|
|
|
);
|
|
|
|
|
|
2024-01-29 11:09:05 -07:00
|
|
|
children.push(horizontal_rule(1).into());
|
2024-01-05 15:32:42 -07:00
|
|
|
|
2024-01-05 09:36:16 -07:00
|
|
|
if let Some(ref items) = self.items_opt {
|
|
|
|
|
let mut count = 0;
|
|
|
|
|
let mut hidden = 0;
|
|
|
|
|
for (i, item) in items.iter().enumerate() {
|
|
|
|
|
if item.hidden {
|
|
|
|
|
hidden += 1;
|
|
|
|
|
//TODO: SHOW HIDDEN OPTION
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-29 11:09:05 -07:00
|
|
|
if count > 0 {
|
|
|
|
|
children.push(horizontal_rule(1).into());
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-29 10:39:12 -07:00
|
|
|
let modified_text = match &item.metadata {
|
2024-01-30 10:47:41 -07:00
|
|
|
ItemMetadata::Path { metadata, .. } => match metadata.modified() {
|
2024-01-29 10:39:12 -07:00
|
|
|
Ok(time) => chrono::DateTime::<chrono::Local>::from(time)
|
|
|
|
|
.format("%c")
|
|
|
|
|
.to_string(),
|
|
|
|
|
Err(_) => String::new(),
|
|
|
|
|
},
|
2024-01-30 10:47:41 -07:00
|
|
|
ItemMetadata::Trash { .. } => String::new(),
|
2024-01-29 10:39:12 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let size_text = match &item.metadata {
|
2024-01-30 10:47:41 -07:00
|
|
|
ItemMetadata::Path { metadata, children } => {
|
2024-01-29 10:39:12 -07:00
|
|
|
if metadata.is_dir() {
|
|
|
|
|
format!("{} items", children)
|
|
|
|
|
} else {
|
|
|
|
|
format_size(metadata.len())
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-30 10:47:41 -07:00
|
|
|
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![
|
2024-02-01 15:55:52 -07:00
|
|
|
if self.dialog {
|
|
|
|
|
widget::icon::icon(item.icon_handle_dialog.clone())
|
|
|
|
|
.size(ICON_SIZE_DIALOG)
|
|
|
|
|
.into()
|
|
|
|
|
} else {
|
|
|
|
|
widget::icon::icon(item.icon_handle_list.clone())
|
|
|
|
|
.size(ICON_SIZE_LIST)
|
|
|
|
|
.into()
|
|
|
|
|
},
|
2024-01-29 10:39:12 -07:00
|
|
|
widget::text(item.name.clone()).width(Length::Fill).into(),
|
|
|
|
|
widget::text(modified_text).width(column_width).into(),
|
|
|
|
|
widget::text(size_text).width(column_width).into(),
|
2024-01-05 15:10:46 -07:00
|
|
|
])
|
|
|
|
|
.align_items(Alignment::Center)
|
|
|
|
|
.spacing(space_xxs),
|
|
|
|
|
)
|
2024-01-10 09:47:47 -07:00
|
|
|
.style(button_style(item.selected))
|
2024-01-10 10:26:30 -07:00
|
|
|
.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(
|
|
|
|
|
crate::mouse_area::MouseArea::new(button)
|
2024-01-10 10:26:30 -07:00
|
|
|
.on_right_press_no_capture(move |_point_opt| Message::RightClick(i))
|
2024-01-05 15:10:46 -07:00
|
|
|
.into(),
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-01-05 08:55:18 -07:00
|
|
|
count += 1;
|
2024-01-03 15:27:32 -07:00
|
|
|
}
|
|
|
|
|
|
2024-01-05 08:55:18 -07:00
|
|
|
if count == 0 {
|
2024-01-05 09:36:16 -07:00
|
|
|
return self.empty_view(hidden > 0, core);
|
2024-01-05 08:55:18 -07:00
|
|
|
}
|
2024-01-03 15:27:32 -07:00
|
|
|
}
|
2024-02-01 15:55:52 -07:00
|
|
|
|
|
|
|
|
widget::column::with_children(vec![
|
|
|
|
|
self.location_view(core).into(),
|
|
|
|
|
widget::scrollable(
|
|
|
|
|
widget::column::with_children(children)
|
|
|
|
|
// Hack to make room for scroll bar
|
|
|
|
|
.padding([0, space_xxs, 0, 0]),
|
|
|
|
|
)
|
|
|
|
|
.width(Length::Fill)
|
|
|
|
|
.into(),
|
|
|
|
|
])
|
2024-01-29 10:39:12 -07:00
|
|
|
.into()
|
2024-01-05 09:36:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn view(&self, core: &Core) -> Element<Message> {
|
2024-01-05 16:17:23 -07:00
|
|
|
widget::container(match self.view {
|
|
|
|
|
View::Grid => self.grid_view(core),
|
|
|
|
|
View::List => self.list_view(core),
|
|
|
|
|
})
|
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-01 16:29:10 +00:00
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use std::io;
|
|
|
|
|
|
|
|
|
|
use log::debug;
|
|
|
|
|
use test_log::test;
|
|
|
|
|
|
|
|
|
|
use super::scan_path;
|
2024-02-01 15:14:14 -07:00
|
|
|
use crate::app::test_utils::{
|
2024-02-01 16:29:10 +00:00
|
|
|
empty_fs, eq_path_item, simple_fs, sort_files, NAME_LEN, NUM_DIRS, NUM_FILES, NUM_NESTED,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn scan_path_succeeds_on_valid_path() -> io::Result<()> {
|
|
|
|
|
let fs = simple_fs(NUM_FILES, NUM_DIRS, NUM_NESTED, NAME_LEN)?;
|
|
|
|
|
let path = fs.path();
|
|
|
|
|
|
|
|
|
|
let mut entries: Vec<_> = path
|
|
|
|
|
.read_dir()?
|
|
|
|
|
.map(|maybe_entry| maybe_entry.map(|entry| entry.path()))
|
|
|
|
|
.collect::<io::Result<_>>()?;
|
|
|
|
|
entries.sort_by(|a, b| sort_files(a, b));
|
|
|
|
|
|
|
|
|
|
debug!("Calling scan_path(\"{}\")", path.display());
|
|
|
|
|
let actual = scan_path(&path.to_owned());
|
|
|
|
|
|
|
|
|
|
// 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<()> {
|
|
|
|
|
let fs = simple_fs(NUM_FILES, 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);
|
|
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
|
|
|
|
|
assert_eq!(0, path.read_dir()?.count());
|
|
|
|
|
assert_eq!(0, actual.len());
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|