2024-01-03 15:27:32 -07:00
|
|
|
use cosmic::{
|
2024-07-09 11:16:14 -06:00
|
|
|
cosmic_theme, font,
|
2024-01-05 08:55:18 -07:00
|
|
|
iced::{
|
2024-07-09 11:16:14 -06:00
|
|
|
advanced::{
|
|
|
|
|
graphics,
|
|
|
|
|
text::{self, Paragraph},
|
|
|
|
|
},
|
2024-01-05 08:55:18 -07:00
|
|
|
alignment::{Horizontal, Vertical},
|
2024-07-12 19:40:46 +02:00
|
|
|
clipboard::dnd::DndAction,
|
2024-09-14 02:11:06 -04:00
|
|
|
event,
|
2024-10-09 15:41:10 -06:00
|
|
|
futures,
|
2024-02-22 16:17:39 -07:00
|
|
|
futures::SinkExt,
|
2024-10-02 14:53:24 -06:00
|
|
|
keyboard::Modifiers,
|
2024-10-21 13:51:10 -06:00
|
|
|
stream,
|
2024-01-29 11:09:05 -07:00
|
|
|
//TODO: export in cosmic::widget
|
2024-02-29 16:25:54 -07:00
|
|
|
widget::{
|
2024-10-25 13:24:17 +02:00
|
|
|
horizontal_rule, rule,
|
2024-08-20 13:26:10 -06:00
|
|
|
scrollable::{self, AbsoluteOffset, Viewport},
|
2024-02-29 16:25:54 -07:00
|
|
|
},
|
2024-01-29 11:09:05 -07:00
|
|
|
Alignment,
|
2024-07-12 19:40:46 +02:00
|
|
|
Border,
|
2024-02-29 11:25:46 -07:00
|
|
|
Color,
|
2024-02-22 21:34:21 -07:00
|
|
|
ContentFit,
|
2024-01-29 11:09:05 -07:00
|
|
|
Length,
|
|
|
|
|
Point,
|
2024-02-26 12:05:29 -07:00
|
|
|
Rectangle,
|
|
|
|
|
Size,
|
2024-10-21 13:51:10 -06:00
|
|
|
Subscription,
|
2024-01-05 08:55:18 -07:00
|
|
|
},
|
2024-09-14 02:11:06 -04:00
|
|
|
iced_core::{mouse::ScrollDelta, widget::tree},
|
2024-09-11 19:02:47 +02:00
|
|
|
theme,
|
2024-07-12 19:40:46 +02:00
|
|
|
widget::{
|
2024-11-11 23:53:25 +01:00
|
|
|
self,
|
2024-07-12 19:40:46 +02:00
|
|
|
menu::{action::MenuAction, key_bind::KeyBind},
|
2024-10-21 13:51:10 -06:00
|
|
|
DndDestination, DndSource, Id, Space, Widget,
|
2024-07-12 19:40:46 +02:00
|
|
|
},
|
2024-10-21 13:51:10 -06:00
|
|
|
Element,
|
2024-01-03 15:27:32 -07:00
|
|
|
};
|
2024-07-12 19:40:46 +02:00
|
|
|
|
2024-08-27 09:04:52 +02:00
|
|
|
use chrono::{DateTime, Utc};
|
2024-10-29 03:18:05 -04:00
|
|
|
use i18n_embed::LanguageLoader;
|
2024-03-04 11:16:25 -07:00
|
|
|
use mime_guess::{mime, Mime};
|
2024-01-24 09:30:06 -05:00
|
|
|
use once_cell::sync::Lazy;
|
2024-03-13 23:23:00 -04:00
|
|
|
use serde::{Deserialize, Serialize};
|
2024-01-03 15:27:32 -07:00
|
|
|
use std::{
|
2024-11-19 20:17:58 -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-09-15 16:00:20 -06:00
|
|
|
fmt::{self, Display},
|
2024-10-04 11:52:34 +02:00
|
|
|
fs::{self, File, Metadata},
|
|
|
|
|
io::{BufRead, BufReader},
|
2024-08-21 13:52:52 -07:00
|
|
|
os::unix::fs::MetadataExt,
|
2024-09-09 13:27:54 -06:00
|
|
|
path::{Path, PathBuf},
|
2024-10-09 20:18:43 -06:00
|
|
|
sync::{atomic, Arc, Mutex, RwLock},
|
2024-09-20 02:27:23 -04:00
|
|
|
time::{Duration, Instant, SystemTime},
|
2024-01-03 15:27:32 -07:00
|
|
|
};
|
2024-10-09 18:55:09 -06:00
|
|
|
use tokio::sync::mpsc;
|
2024-11-15 23:24:52 +01:00
|
|
|
use walkdir::WalkDir;
|
2024-01-03 15:27:32 -07:00
|
|
|
|
2024-02-18 02:44:54 -05:00
|
|
|
use crate::{
|
2024-09-20 14:13:04 -06:00
|
|
|
app::{self, Action, PreviewItem, PreviewKind},
|
2024-07-12 19:40:46 +02:00
|
|
|
clipboard::{ClipboardCopy, ClipboardKind, ClipboardPaste},
|
2024-10-04 16:28:30 -06:00
|
|
|
config::{DesktopConfig, IconSizes, TabConfig, ICON_SCALE_MAX, ICON_SIZE_GRID},
|
2024-02-18 02:44:54 -05:00
|
|
|
dialog::DialogKind,
|
2024-07-12 19:40:46 +02:00
|
|
|
fl,
|
2024-07-12 19:43:50 +02:00
|
|
|
localize::{LANGUAGE_CHRONO, LANGUAGE_SORTER},
|
2024-07-12 19:40:46 +02:00
|
|
|
menu,
|
2024-03-01 16:10:30 -07:00
|
|
|
mime_app::{mime_apps, MimeApp},
|
2024-03-04 11:16:25 -07:00
|
|
|
mime_icon::{mime_for_path, mime_icon},
|
2024-10-09 15:41:10 -06:00
|
|
|
mounter::MOUNTERS,
|
2024-02-26 12:05:29 -07:00
|
|
|
mouse_area,
|
2024-11-15 17:30:25 -07:00
|
|
|
operation::Controller,
|
2024-09-25 14:18:28 -06:00
|
|
|
thumbnailer::thumbnailer,
|
2024-02-18 02:44:54 -05:00
|
|
|
};
|
2024-08-21 13:52:52 -07:00
|
|
|
use unix_permissions_ext::UNIXPermissionsExt;
|
2024-08-27 07:26:47 -06:00
|
|
|
use uzers::{get_group_by_gid, get_user_by_uid};
|
2024-04-10 11:41:25 -04:00
|
|
|
|
|
|
|
|
pub const DOUBLE_CLICK_DURATION: Duration = Duration::from_millis(500);
|
|
|
|
|
pub const HOVER_DURATION: Duration = Duration::from_millis(1600);
|
2024-10-09 15:41:10 -06:00
|
|
|
//TODO: best limit for search items
|
2024-10-09 19:01:51 -06:00
|
|
|
const MAX_SEARCH_LATENCY: Duration = Duration::from_millis(20);
|
|
|
|
|
const MAX_SEARCH_RESULTS: usize = 200;
|
2024-10-10 10:05:48 -06:00
|
|
|
//TODO: configurable thumbnail size?
|
|
|
|
|
const THUMBNAIL_SIZE: u32 = (ICON_SIZE_GRID as u32) * (ICON_SCALE_MAX as u32);
|
2024-01-03 15:33:28 -07:00
|
|
|
|
2024-02-22 15:04:37 -07:00
|
|
|
//TODO: adjust for locales?
|
2024-09-15 16:00:20 -06:00
|
|
|
const DATE_TIME_FORMAT: &'static str = "%b %-d, %-Y, %-I:%M %p";
|
|
|
|
|
const TIME_FORMAT: &'static str = "%-I:%M %p";
|
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-10-16 10:22:25 +11:00
|
|
|
highlighted: bool,
|
2024-02-29 15:29:10 -07:00
|
|
|
focused: bool,
|
2024-02-29 11:25:46 -07:00
|
|
|
accent: bool,
|
2024-07-11 10:12:50 -06:00
|
|
|
condensed_radius: bool,
|
2024-08-20 13:26:10 -06:00
|
|
|
desktop: bool,
|
2024-10-21 13:51:10 -06:00
|
|
|
) -> widget::button::Style {
|
2024-02-29 11:25:46 -07:00
|
|
|
let cosmic = theme.cosmic();
|
2024-10-21 13:51:10 -06:00
|
|
|
let mut appearance = widget::button::Style::new();
|
2024-02-29 11:25:46 -07:00
|
|
|
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-10-16 10:22:25 +11:00
|
|
|
} else if highlighted {
|
|
|
|
|
if accent {
|
|
|
|
|
appearance.background = Some(Color::from(cosmic.bg_component_color()).into());
|
|
|
|
|
appearance.icon_color = Some(Color::from(cosmic.on_bg_component_color()));
|
|
|
|
|
appearance.text_color = Some(Color::from(cosmic.on_bg_component_color()));
|
|
|
|
|
} else {
|
|
|
|
|
appearance.background = Some(Color::from(cosmic.bg_component_color()).into());
|
|
|
|
|
}
|
2024-08-20 13:26:10 -06:00
|
|
|
} else if desktop {
|
|
|
|
|
appearance.background = Some(Color::from(cosmic.bg_color()).into());
|
|
|
|
|
appearance.icon_color = Some(Color::from(cosmic.on_bg_color()));
|
|
|
|
|
appearance.text_color = Some(Color::from(cosmic.on_bg_color()));
|
2024-02-29 11:25:46 -07:00
|
|
|
}
|
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-07-11 10:12:50 -06:00
|
|
|
if condensed_radius {
|
|
|
|
|
appearance.border_radius = cosmic.radius_xs().into();
|
|
|
|
|
} else {
|
|
|
|
|
appearance.border_radius = cosmic.radius_s().into();
|
|
|
|
|
}
|
2024-02-29 11:25:46 -07:00
|
|
|
appearance
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-20 13:26:10 -06:00
|
|
|
fn button_style(
|
|
|
|
|
selected: bool,
|
2024-10-16 10:22:25 +11:00
|
|
|
highlighted: bool,
|
2024-08-20 13:26:10 -06:00
|
|
|
accent: bool,
|
|
|
|
|
condensed_radius: bool,
|
|
|
|
|
desktop: bool,
|
|
|
|
|
) -> theme::Button {
|
2024-02-29 11:25:46 -07:00
|
|
|
//TODO: move to libcosmic?
|
2024-01-05 09:36:16 -07:00
|
|
|
theme::Button::Custom {
|
2024-07-11 10:12:50 -06:00
|
|
|
active: Box::new(move |focused, theme| {
|
2024-10-16 10:22:25 +11:00
|
|
|
button_appearance(
|
|
|
|
|
theme,
|
|
|
|
|
selected,
|
|
|
|
|
highlighted,
|
|
|
|
|
focused,
|
|
|
|
|
accent,
|
|
|
|
|
condensed_radius,
|
|
|
|
|
desktop,
|
|
|
|
|
)
|
2024-07-11 10:12:50 -06:00
|
|
|
}),
|
|
|
|
|
disabled: Box::new(move |theme| {
|
2024-10-16 10:22:25 +11:00
|
|
|
button_appearance(
|
|
|
|
|
theme,
|
|
|
|
|
selected,
|
|
|
|
|
highlighted,
|
|
|
|
|
false,
|
|
|
|
|
accent,
|
|
|
|
|
condensed_radius,
|
|
|
|
|
desktop,
|
|
|
|
|
)
|
2024-07-11 10:12:50 -06:00
|
|
|
}),
|
2024-01-05 09:36:16 -07:00
|
|
|
hovered: Box::new(move |focused, theme| {
|
2024-10-16 10:22:25 +11:00
|
|
|
button_appearance(
|
|
|
|
|
theme,
|
|
|
|
|
selected,
|
|
|
|
|
highlighted,
|
|
|
|
|
focused,
|
|
|
|
|
accent,
|
|
|
|
|
condensed_radius,
|
|
|
|
|
desktop,
|
|
|
|
|
)
|
2024-01-05 09:36:16 -07:00
|
|
|
}),
|
|
|
|
|
pressed: Box::new(move |focused, theme| {
|
2024-10-16 10:22:25 +11:00
|
|
|
button_appearance(
|
|
|
|
|
theme,
|
|
|
|
|
selected,
|
|
|
|
|
highlighted,
|
|
|
|
|
focused,
|
|
|
|
|
accent,
|
|
|
|
|
condensed_radius,
|
|
|
|
|
desktop,
|
|
|
|
|
)
|
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)
|
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-10-04 16:28:30 -06:00
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
pub fn trash_entries() -> usize {
|
|
|
|
|
0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(not(target_os = "macos"))]
|
|
|
|
|
pub fn trash_entries() -> usize {
|
|
|
|
|
match trash::os_limited::list() {
|
|
|
|
|
Ok(entries) => entries.len(),
|
|
|
|
|
Err(_err) => 0,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn trash_icon(icon_size: u16) -> widget::icon::Handle {
|
|
|
|
|
widget::icon::from_name(if trash_entries() > 0 {
|
|
|
|
|
"user-trash-full"
|
|
|
|
|
} else {
|
|
|
|
|
"user-trash"
|
|
|
|
|
})
|
|
|
|
|
.size(icon_size)
|
|
|
|
|
.handle()
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-31 14:15:21 -07:00
|
|
|
pub fn trash_icon_symbolic(icon_size: u16) -> widget::icon::Handle {
|
2024-10-04 16:28:30 -06:00
|
|
|
widget::icon::from_name(if trash_entries() > 0 {
|
2024-01-10 12:57:30 -07:00
|
|
|
"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-08-21 13:52:52 -07:00
|
|
|
enum PermissionOwner {
|
|
|
|
|
Owner,
|
|
|
|
|
Group,
|
|
|
|
|
Other,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn format_permissions_owner(metadata: &Metadata, owner: PermissionOwner) -> String {
|
|
|
|
|
return match owner {
|
|
|
|
|
PermissionOwner::Owner => get_user_by_uid(metadata.uid())
|
|
|
|
|
.and_then(|user| user.name().to_str().map(ToOwned::to_owned))
|
|
|
|
|
.unwrap_or_default(),
|
|
|
|
|
PermissionOwner::Group => get_group_by_gid(metadata.gid())
|
|
|
|
|
.and_then(|group| group.name().to_str().map(ToOwned::to_owned))
|
|
|
|
|
.unwrap_or_default(),
|
|
|
|
|
PermissionOwner::Other => String::from(""),
|
|
|
|
|
};
|
|
|
|
|
}
|
2024-11-15 23:24:52 +01:00
|
|
|
|
2024-08-21 13:52:52 -07:00
|
|
|
fn format_permissions(metadata: &Metadata, owner: PermissionOwner) -> String {
|
2024-08-26 13:28:44 -07:00
|
|
|
let mut perms: Vec<String> = Vec::new();
|
|
|
|
|
if match owner {
|
2024-08-21 13:52:52 -07:00
|
|
|
PermissionOwner::Owner => metadata.permissions().readable_by_owner(),
|
|
|
|
|
PermissionOwner::Group => metadata.permissions().readable_by_group(),
|
|
|
|
|
PermissionOwner::Other => metadata.permissions().readable_by_other(),
|
2024-08-26 13:28:44 -07:00
|
|
|
} {
|
|
|
|
|
perms.push(fl!("read"));
|
|
|
|
|
}
|
|
|
|
|
if match owner {
|
2024-08-21 13:52:52 -07:00
|
|
|
PermissionOwner::Owner => metadata.permissions().writable_by_owner(),
|
|
|
|
|
PermissionOwner::Group => metadata.permissions().writable_by_group(),
|
|
|
|
|
PermissionOwner::Other => metadata.permissions().writable_by_other(),
|
2024-08-26 13:28:44 -07:00
|
|
|
} {
|
|
|
|
|
perms.push(fl!("write"));
|
|
|
|
|
}
|
|
|
|
|
if match owner {
|
2024-08-21 13:52:52 -07:00
|
|
|
PermissionOwner::Owner => metadata.permissions().executable_by_owner(),
|
|
|
|
|
PermissionOwner::Group => metadata.permissions().executable_by_group(),
|
|
|
|
|
PermissionOwner::Other => metadata.permissions().executable_by_other(),
|
2024-08-26 13:28:44 -07:00
|
|
|
} {
|
|
|
|
|
perms.push(fl!("execute"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
perms.join(" ")
|
2024-08-21 13:52:52 -07:00
|
|
|
}
|
2024-01-05 15:32:42 -07:00
|
|
|
|
2024-10-09 20:18:43 -06:00
|
|
|
struct FormatTime(SystemTime);
|
2024-09-15 16:00:20 -06:00
|
|
|
|
2024-09-20 02:27:23 -04:00
|
|
|
impl FormatTime {
|
|
|
|
|
fn from_secs(secs: i64) -> Option<Self> {
|
|
|
|
|
// This looks convoluted because we need to ensure the units match up
|
|
|
|
|
let secs: u64 = secs.try_into().ok()?;
|
|
|
|
|
let now = SystemTime::now();
|
|
|
|
|
let filetime_diff = now
|
|
|
|
|
.duration_since(SystemTime::UNIX_EPOCH)
|
|
|
|
|
.map(|from_epoch| from_epoch.as_secs())
|
|
|
|
|
.ok()
|
|
|
|
|
.and_then(|now_secs| now_secs.checked_sub(secs))
|
|
|
|
|
.map(Duration::from_secs)?;
|
|
|
|
|
now.checked_add(filetime_diff).map(FormatTime)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:00:20 -06:00
|
|
|
impl Display for FormatTime {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
let date_time = chrono::DateTime::<chrono::Local>::from(self.0);
|
|
|
|
|
let now = chrono::Local::now();
|
2024-10-10 01:56:35 -04:00
|
|
|
if date_time.date_naive() == now.date_naive() {
|
2024-09-15 16:00:20 -06:00
|
|
|
write!(
|
|
|
|
|
f,
|
|
|
|
|
"{}, {}",
|
|
|
|
|
fl!("today"),
|
|
|
|
|
date_time.format_localized(TIME_FORMAT, *LANGUAGE_CHRONO)
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
date_time
|
|
|
|
|
.format_localized(DATE_TIME_FORMAT, *LANGUAGE_CHRONO)
|
|
|
|
|
.fmt(f)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-09 20:18:43 -06:00
|
|
|
fn format_time(time: SystemTime) -> FormatTime {
|
2024-09-15 16:00:20 -06:00
|
|
|
FormatTime(time)
|
|
|
|
|
}
|
|
|
|
|
|
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-08-20 13:26:10 -06:00
|
|
|
pub fn parse_desktop_file(path: &Path) -> (Option<String>, Option<String>) {
|
|
|
|
|
let entry = match freedesktop_entry_parser::parse_entry(path) {
|
|
|
|
|
Ok(ok) => ok,
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::warn!("failed to parse {:?}: {}", path, err);
|
|
|
|
|
return (None, None);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
(
|
|
|
|
|
entry
|
|
|
|
|
.section("Desktop Entry")
|
|
|
|
|
.attr("Name")
|
|
|
|
|
.map(|x| x.to_string()),
|
|
|
|
|
entry
|
|
|
|
|
.section("Desktop Entry")
|
|
|
|
|
.attr("Icon")
|
|
|
|
|
.map(|x| x.to_string()),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-31 14:54:19 -06:00
|
|
|
pub fn item_from_entry(
|
|
|
|
|
path: PathBuf,
|
|
|
|
|
name: String,
|
|
|
|
|
metadata: fs::Metadata,
|
|
|
|
|
sizes: IconSizes,
|
|
|
|
|
) -> Item {
|
2024-08-20 13:26:10 -06:00
|
|
|
let mut display_name = Item::display_name(&name);
|
2024-07-11 10:12:50 -06:00
|
|
|
|
2024-05-31 14:54:19 -06:00
|
|
|
let hidden = name.starts_with(".") || hidden_attribute(&metadata);
|
|
|
|
|
|
|
|
|
|
let (mime, icon_handle_grid, icon_handle_list, icon_handle_list_condensed) =
|
|
|
|
|
if metadata.is_dir() {
|
|
|
|
|
(
|
|
|
|
|
//TODO: make this a static
|
|
|
|
|
"inode/directory".parse().unwrap(),
|
|
|
|
|
folder_icon(&path, sizes.grid()),
|
|
|
|
|
folder_icon(&path, sizes.list()),
|
|
|
|
|
folder_icon(&path, sizes.list_condensed()),
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
let mime = mime_for_path(&path);
|
2024-08-20 13:26:10 -06:00
|
|
|
//TODO: clean this up, implement for trash
|
|
|
|
|
let icon_name_opt = if mime == "application/x-desktop" {
|
|
|
|
|
let (desktop_name_opt, icon_name_opt) = parse_desktop_file(&path);
|
|
|
|
|
if let Some(desktop_name) = desktop_name_opt {
|
|
|
|
|
display_name = Item::display_name(&desktop_name);
|
|
|
|
|
}
|
|
|
|
|
icon_name_opt
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
};
|
|
|
|
|
if let Some(icon_name) = icon_name_opt {
|
|
|
|
|
(
|
|
|
|
|
mime.clone(),
|
|
|
|
|
widget::icon::from_name(&*icon_name)
|
|
|
|
|
.size(sizes.grid())
|
|
|
|
|
.handle(),
|
|
|
|
|
widget::icon::from_name(&*icon_name)
|
|
|
|
|
.size(sizes.list())
|
|
|
|
|
.handle(),
|
|
|
|
|
widget::icon::from_name(&*icon_name)
|
|
|
|
|
.size(sizes.list_condensed())
|
|
|
|
|
.handle(),
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
(
|
|
|
|
|
mime.clone(),
|
|
|
|
|
mime_icon(mime.clone(), sizes.grid()),
|
|
|
|
|
mime_icon(mime.clone(), sizes.list()),
|
|
|
|
|
mime_icon(mime, sizes.list_condensed()),
|
|
|
|
|
)
|
|
|
|
|
}
|
2024-05-31 14:54:19 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let open_with = mime_apps(&mime);
|
|
|
|
|
|
|
|
|
|
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-11-15 17:30:25 -07:00
|
|
|
let dir_size = if metadata.is_dir() {
|
|
|
|
|
DirSize::Calculating(Controller::new())
|
|
|
|
|
} else {
|
|
|
|
|
DirSize::NotDirectory
|
|
|
|
|
};
|
|
|
|
|
|
2024-05-31 14:54:19 -06:00
|
|
|
Item {
|
|
|
|
|
name,
|
2024-08-20 13:26:10 -06:00
|
|
|
display_name,
|
2024-05-31 14:54:19 -06:00
|
|
|
metadata: ItemMetadata::Path { metadata, children },
|
|
|
|
|
hidden,
|
2024-09-13 09:35:37 -06:00
|
|
|
location_opt: Some(Location::Path(path)),
|
2024-05-31 14:54:19 -06:00
|
|
|
mime,
|
|
|
|
|
icon_handle_grid,
|
|
|
|
|
icon_handle_list,
|
|
|
|
|
icon_handle_list_condensed,
|
|
|
|
|
open_with,
|
2024-09-25 13:28:14 -06:00
|
|
|
thumbnail_opt: None,
|
2024-05-31 14:54:19 -06:00
|
|
|
button_id: widget::Id::unique(),
|
|
|
|
|
pos_opt: Cell::new(None),
|
|
|
|
|
rect_opt: Cell::new(None),
|
|
|
|
|
selected: false,
|
2024-10-16 10:22:25 +11:00
|
|
|
highlighted: false,
|
2024-05-31 14:54:19 -06:00
|
|
|
overlaps_drag_rect: false,
|
2024-11-15 17:30:25 -07:00
|
|
|
dir_size,
|
2024-05-31 14:54:19 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-08 11:51:07 -06:00
|
|
|
pub fn item_from_path<P: Into<PathBuf>>(path: P, sizes: IconSizes) -> Result<Item, String> {
|
|
|
|
|
let path = path.into();
|
2024-10-14 10:09:57 -06:00
|
|
|
let name = match path.file_name() {
|
|
|
|
|
Some(name_os) => name_os
|
|
|
|
|
.to_str()
|
|
|
|
|
.ok_or_else(|| {
|
|
|
|
|
format!(
|
|
|
|
|
"failed to parse file name for {:?}: {:?} is not valid UTF-8",
|
|
|
|
|
path, name_os
|
|
|
|
|
)
|
|
|
|
|
})?
|
|
|
|
|
.to_string(),
|
|
|
|
|
None => fl!("filesystem"),
|
|
|
|
|
};
|
2024-07-08 11:51:07 -06:00
|
|
|
let metadata = fs::metadata(&path)
|
|
|
|
|
.map_err(|err| format!("failed to read metadata for {:?}: {}", path, err))?;
|
|
|
|
|
Ok(item_from_entry(path, name, metadata, sizes))
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-18 02:44:54 -05:00
|
|
|
pub fn scan_path(tab_path: &PathBuf, sizes: IconSizes) -> Vec<Item> {
|
2024-01-05 08:55:18 -07:00
|
|
|
let mut items = Vec::new();
|
2024-10-04 19:02:22 +02:00
|
|
|
let mut hidden_files = 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;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-05-31 14:54:19 -06:00
|
|
|
let path = entry.path();
|
|
|
|
|
|
2024-01-05 08:55:18 -07:00
|
|
|
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!(
|
2024-05-31 14:54:19 -06:00
|
|
|
"failed to parse entry at {:?}: {:?} is not valid UTF-8",
|
|
|
|
|
path,
|
2024-01-05 08:55:18 -07:00
|
|
|
name_os,
|
|
|
|
|
);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-10-04 18:35:02 +02:00
|
|
|
if name == ".hidden" {
|
2024-10-04 19:02:22 +02:00
|
|
|
hidden_files = parse_hidden_file(&path);
|
2024-10-04 18:35:02 +02:00
|
|
|
}
|
|
|
|
|
|
2024-08-19 09:51:04 -06:00
|
|
|
let metadata = match fs::metadata(&path) {
|
2024-01-05 14:44:20 -07:00
|
|
|
Ok(ok) => ok,
|
|
|
|
|
Err(err) => {
|
2024-05-31 14:54:19 -06:00
|
|
|
log::warn!("failed to read metadata for entry at {:?}: {}", path, err);
|
2024-01-05 14:44:20 -07:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-05-31 14:54:19 -06:00
|
|
|
items.push(item_from_entry(path, name, metadata, sizes));
|
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,
|
2024-08-20 13:26:10 -06:00
|
|
|
_ => LANGUAGE_SORTER.compare(&a.display_name, &b.display_name),
|
2024-01-06 12:59:36 -07:00
|
|
|
});
|
2024-10-04 11:53:27 -06:00
|
|
|
items.iter_mut().for_each(|item| {
|
2024-10-04 19:02:22 +02:00
|
|
|
if hidden_files
|
|
|
|
|
.iter()
|
|
|
|
|
.find(|hidden| &&item.name == hidden)
|
|
|
|
|
.is_some()
|
|
|
|
|
{
|
|
|
|
|
item.hidden = true;
|
2024-10-04 18:35:02 +02:00
|
|
|
}
|
|
|
|
|
});
|
2024-01-05 16:17:23 -07:00
|
|
|
items
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-09 20:18:43 -06:00
|
|
|
pub fn scan_search<F: Fn(&Path, &str, Metadata) -> bool + Sync>(
|
2024-10-09 15:41:10 -06:00
|
|
|
tab_path: &PathBuf,
|
|
|
|
|
term: &str,
|
2024-10-09 21:02:12 -06:00
|
|
|
show_hidden: bool,
|
2024-10-09 15:41:10 -06:00
|
|
|
callback: F,
|
|
|
|
|
) {
|
|
|
|
|
if term.is_empty() {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-05-31 15:19:47 -06:00
|
|
|
|
|
|
|
|
let pattern = regex::escape(&term);
|
|
|
|
|
let regex = match regex::RegexBuilder::new(&pattern)
|
|
|
|
|
.case_insensitive(true)
|
|
|
|
|
.build()
|
|
|
|
|
{
|
|
|
|
|
Ok(ok) => ok,
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::warn!("failed to parse regex {:?}: {}", pattern, err);
|
2024-10-09 15:41:10 -06:00
|
|
|
return;
|
2024-05-31 15:19:47 -06:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ignore::WalkBuilder::new(tab_path)
|
2024-10-09 21:02:12 -06:00
|
|
|
.standard_filters(false)
|
|
|
|
|
.hidden(!show_hidden)
|
2024-05-31 15:19:47 -06:00
|
|
|
//TODO: only use this on supported targets
|
|
|
|
|
.same_file_system(true)
|
|
|
|
|
.build_parallel()
|
|
|
|
|
.run(|| {
|
|
|
|
|
Box::new(|entry_res| {
|
|
|
|
|
let Ok(entry) = entry_res else {
|
|
|
|
|
// Skip invalid entries
|
|
|
|
|
return ignore::WalkState::Skip;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let Some(file_name) = entry.file_name().to_str() else {
|
|
|
|
|
// Skip anything with an invalid name
|
|
|
|
|
return ignore::WalkState::Skip;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if regex.is_match(file_name) {
|
|
|
|
|
let path = entry.path();
|
|
|
|
|
|
2024-10-09 20:18:43 -06:00
|
|
|
let metadata = match entry.metadata() {
|
2024-05-31 15:19:47 -06:00
|
|
|
Ok(ok) => ok,
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::warn!("failed to read metadata for entry at {:?}: {}", path, err);
|
|
|
|
|
return ignore::WalkState::Continue;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-10-09 20:18:43 -06:00
|
|
|
//TODO: use entry.into_path?
|
|
|
|
|
if !callback(path, file_name, metadata) {
|
2024-10-09 15:41:10 -06:00
|
|
|
return ignore::WalkState::Quit;
|
|
|
|
|
}
|
2024-05-31 15:19:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ignore::WalkState::Continue
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-05 16:17:23 -07:00
|
|
|
// 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")
|
|
|
|
|
)
|
|
|
|
|
)))]
|
2024-03-01 13:20:47 -07:00
|
|
|
pub fn scan_trash(_sizes: IconSizes) -> Vec<Item> {
|
2024-01-05 16:17:23 -07:00
|
|
|
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")
|
|
|
|
|
)
|
|
|
|
|
))]
|
2024-02-18 02:44:54 -05:00
|
|
|
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-03-20 09:56:54 -06:00
|
|
|
let original_path = entry.original_path();
|
2024-06-18 22:41:44 -04:00
|
|
|
let name = entry.name.to_string_lossy().to_string();
|
2024-08-20 13:26:10 -06:00
|
|
|
let display_name = Item::display_name(&name);
|
2024-01-05 16:17:23 -07:00
|
|
|
|
2024-03-01 16:10:30 -07:00
|
|
|
let (mime, icon_handle_grid, icon_handle_list, icon_handle_list_condensed) =
|
2024-02-29 19:47:27 -07:00
|
|
|
match metadata.size {
|
|
|
|
|
trash::TrashItemSize::Entries(_) => (
|
2024-03-01 16:10:30 -07:00
|
|
|
//TODO: make this a static
|
|
|
|
|
"inode/directory".parse().unwrap(),
|
2024-03-20 09:56:54 -06:00
|
|
|
folder_icon(&original_path, sizes.grid()),
|
|
|
|
|
folder_icon(&original_path, sizes.list()),
|
|
|
|
|
folder_icon(&original_path, sizes.list_condensed()),
|
2024-02-29 19:47:27 -07:00
|
|
|
),
|
2024-03-04 11:16:25 -07:00
|
|
|
trash::TrashItemSize::Bytes(_) => {
|
2024-03-20 09:56:54 -06:00
|
|
|
//TODO: do not use original path
|
|
|
|
|
let mime = mime_for_path(&original_path);
|
2024-03-04 11:16:25 -07:00
|
|
|
(
|
|
|
|
|
mime.clone(),
|
|
|
|
|
mime_icon(mime.clone(), sizes.grid()),
|
|
|
|
|
mime_icon(mime.clone(), sizes.list()),
|
|
|
|
|
mime_icon(mime, sizes.list_condensed()),
|
|
|
|
|
)
|
|
|
|
|
}
|
2024-02-29 19:47:27 -07:00
|
|
|
};
|
2024-01-05 16:17:23 -07:00
|
|
|
|
|
|
|
|
items.push(Item {
|
|
|
|
|
name,
|
2024-08-20 13:26:10 -06:00
|
|
|
display_name,
|
2024-01-30 10:47:41 -07:00
|
|
|
metadata: ItemMetadata::Trash { metadata, entry },
|
2024-01-05 16:17:23 -07:00
|
|
|
hidden: false,
|
2024-09-13 09:35:37 -06:00
|
|
|
location_opt: None,
|
2024-03-01 16:10:30 -07:00
|
|
|
mime,
|
2024-01-05 16:17:23 -07:00
|
|
|
icon_handle_grid,
|
|
|
|
|
icon_handle_list,
|
2024-02-29 19:47:27 -07:00
|
|
|
icon_handle_list_condensed,
|
2024-03-01 13:20:47 -07:00
|
|
|
open_with: Vec::new(),
|
2024-03-04 13:41:18 -07:00
|
|
|
thumbnail_opt: Some(ItemThumbnail::NotImage),
|
2024-02-29 15:38:03 -07:00
|
|
|
button_id: widget::Id::unique(),
|
2024-02-29 13:24:57 -07:00
|
|
|
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,
|
2024-10-16 10:22:25 +11:00
|
|
|
highlighted: false,
|
2024-05-26 22:03:43 -06:00
|
|
|
overlaps_drag_rect: false,
|
2024-11-15 17:30:25 -07:00
|
|
|
dir_size: DirSize::NotDirectory,
|
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-08-20 13:26:10 -06:00
|
|
|
_ => LANGUAGE_SORTER.compare(&a.display_name, &b.display_name),
|
2024-01-05 08:55:18 -07:00
|
|
|
});
|
|
|
|
|
items
|
2024-01-03 15:27:32 -07:00
|
|
|
}
|
|
|
|
|
|
2024-08-16 14:42:09 +02:00
|
|
|
fn uri_to_path(uri: String) -> Option<PathBuf> {
|
|
|
|
|
//TODO support for external drive or cloud?
|
|
|
|
|
if uri.starts_with("file://") {
|
|
|
|
|
let path_str = &uri[7..];
|
|
|
|
|
Some(PathBuf::from(path_str))
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn scan_recents(sizes: IconSizes) -> Vec<Item> {
|
|
|
|
|
let mut recents = Vec::new();
|
|
|
|
|
|
2024-09-20 14:13:04 -06:00
|
|
|
match recently_used_xbel::parse_file() {
|
2024-08-16 14:42:09 +02:00
|
|
|
Ok(recent_files) => {
|
|
|
|
|
for bookmark in recent_files.bookmarks {
|
|
|
|
|
let uri = bookmark.href;
|
|
|
|
|
let path = match uri_to_path(uri) {
|
|
|
|
|
None => continue,
|
|
|
|
|
Some(path) => path,
|
|
|
|
|
};
|
|
|
|
|
let last_edit = match bookmark.modified.parse::<DateTime<Utc>>() {
|
|
|
|
|
Ok(last_edit) => last_edit,
|
|
|
|
|
Err(_) => continue,
|
|
|
|
|
};
|
|
|
|
|
let last_visit = match bookmark.visited.parse::<DateTime<Utc>>() {
|
|
|
|
|
Ok(last_visit) => last_visit,
|
|
|
|
|
Err(_) => continue,
|
|
|
|
|
};
|
|
|
|
|
let path_buf = PathBuf::from(path);
|
|
|
|
|
let path_exist = path_buf.exists();
|
|
|
|
|
|
|
|
|
|
if path_exist {
|
|
|
|
|
let file_name = path_buf.file_name();
|
|
|
|
|
|
|
|
|
|
if let Some(name) = file_name {
|
|
|
|
|
let name = name.to_string_lossy().to_string();
|
|
|
|
|
|
|
|
|
|
let metadata = match path_buf.metadata() {
|
|
|
|
|
Ok(ok) => ok,
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::warn!(
|
|
|
|
|
"failed to read metadata for entry at {:?}: {}",
|
|
|
|
|
path_buf.clone(),
|
|
|
|
|
err
|
|
|
|
|
);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
};
|
2024-10-09 18:55:09 -06:00
|
|
|
|
2024-08-16 14:42:09 +02:00
|
|
|
let item = item_from_entry(path_buf, name, metadata, sizes);
|
2024-08-27 09:04:52 +02:00
|
|
|
recents.push((
|
|
|
|
|
item,
|
|
|
|
|
if last_edit.le(&last_visit) {
|
|
|
|
|
last_edit
|
|
|
|
|
} else {
|
|
|
|
|
last_visit
|
|
|
|
|
},
|
|
|
|
|
))
|
2024-08-16 14:42:09 +02:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
log::warn!("recent file path not exist: {:?}", path_buf);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::warn!("Error reading recent files: {:?}", err);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
recents.sort_by(|a, b| b.1.cmp(&a.1));
|
|
|
|
|
|
2024-08-27 19:01:36 +02:00
|
|
|
recents.into_iter().take(50).map(|(item, _)| item).collect()
|
2024-08-16 14:42:09 +02:00
|
|
|
}
|
|
|
|
|
|
2024-10-09 15:41:10 -06:00
|
|
|
pub fn scan_network(uri: &str, sizes: IconSizes) -> Vec<Item> {
|
|
|
|
|
for (_key, mounter) in MOUNTERS.iter() {
|
2024-09-13 15:13:37 -06:00
|
|
|
match mounter.network_scan(uri, sizes) {
|
|
|
|
|
Some(Ok(items)) => return items,
|
|
|
|
|
Some(Err(err)) => {
|
2024-09-16 09:09:21 -06:00
|
|
|
log::warn!("failed to scan {:?}: {}", uri, err);
|
2024-09-13 15:13:37 -06:00
|
|
|
}
|
|
|
|
|
None => {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Vec::new()
|
2024-09-12 15:54:54 -06:00
|
|
|
}
|
|
|
|
|
|
2024-10-04 16:28:30 -06:00
|
|
|
//TODO: organize desktop items based on display
|
|
|
|
|
pub fn scan_desktop(
|
|
|
|
|
tab_path: &PathBuf,
|
2024-10-09 15:41:10 -06:00
|
|
|
_display: &str,
|
2024-10-04 16:28:30 -06:00
|
|
|
desktop_config: DesktopConfig,
|
|
|
|
|
sizes: IconSizes,
|
|
|
|
|
) -> Vec<Item> {
|
|
|
|
|
let mut items = Vec::new();
|
|
|
|
|
|
|
|
|
|
if desktop_config.show_content {
|
|
|
|
|
items.extend(scan_path(tab_path, sizes));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if desktop_config.show_mounted_drives {
|
2024-10-09 15:41:10 -06:00
|
|
|
for (_mounter_key, mounter) in MOUNTERS.iter() {
|
2024-10-04 16:28:30 -06:00
|
|
|
for mounter_item in mounter.items(sizes).unwrap_or_default() {
|
|
|
|
|
let Some(path) = mounter_item.path() else {
|
|
|
|
|
continue;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Get most item data from path
|
|
|
|
|
let mut item = match item_from_path(&path, sizes) {
|
|
|
|
|
Ok(item) => item,
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::warn!("failed to get item from mounter item {:?}: {}", path, err);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//Override some data with mounter information
|
|
|
|
|
item.name = mounter_item.name();
|
|
|
|
|
item.display_name = Item::display_name(&item.name);
|
|
|
|
|
|
|
|
|
|
//TODO: use icon size for mounter item icon
|
|
|
|
|
if let Some(icon) = mounter_item.icon(false) {
|
|
|
|
|
item.icon_handle_grid = icon.clone();
|
|
|
|
|
item.icon_handle_list = icon.clone();
|
|
|
|
|
item.icon_handle_list_condensed = icon;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
items.push(item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if desktop_config.show_trash {
|
|
|
|
|
let name = fl!("trash");
|
|
|
|
|
let display_name = Item::display_name(&name);
|
|
|
|
|
|
|
|
|
|
let metadata = ItemMetadata::SimpleDir {
|
|
|
|
|
entries: trash_entries() as u64,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let (mime, icon_handle_grid, icon_handle_list, icon_handle_list_condensed) = {
|
|
|
|
|
(
|
|
|
|
|
"inode/directory".parse().unwrap(),
|
|
|
|
|
trash_icon(sizes.grid()),
|
|
|
|
|
trash_icon(sizes.list()),
|
|
|
|
|
trash_icon(sizes.list_condensed()),
|
|
|
|
|
)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
items.push(Item {
|
|
|
|
|
name,
|
|
|
|
|
display_name,
|
|
|
|
|
metadata,
|
|
|
|
|
hidden: false,
|
|
|
|
|
location_opt: Some(Location::Trash),
|
|
|
|
|
mime,
|
|
|
|
|
icon_handle_grid,
|
|
|
|
|
icon_handle_list,
|
|
|
|
|
icon_handle_list_condensed,
|
|
|
|
|
open_with: Vec::new(),
|
|
|
|
|
thumbnail_opt: Some(ItemThumbnail::NotImage),
|
|
|
|
|
button_id: widget::Id::unique(),
|
|
|
|
|
pos_opt: Cell::new(None),
|
|
|
|
|
rect_opt: Cell::new(None),
|
|
|
|
|
selected: false,
|
2024-10-16 10:22:25 +11:00
|
|
|
highlighted: false,
|
2024-10-04 16:28:30 -06:00
|
|
|
overlaps_drag_rect: false,
|
2024-11-15 17:30:25 -07:00
|
|
|
dir_size: DirSize::NotDirectory,
|
2024-10-04 16:28:30 -06:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
items
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-09 15:41:10 -06:00
|
|
|
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
|
2024-01-05 16:17:23 -07:00
|
|
|
pub enum Location {
|
2024-10-09 15:41:10 -06:00
|
|
|
Desktop(PathBuf, String, DesktopConfig),
|
2024-10-04 16:28:30 -06:00
|
|
|
Network(String, String),
|
2024-01-05 16:17:23 -07:00
|
|
|
Path(PathBuf),
|
2024-10-04 16:28:30 -06:00
|
|
|
Recents,
|
2024-10-09 21:02:12 -06:00
|
|
|
Search(PathBuf, String, bool, Instant),
|
2024-01-05 16:17:23 -07:00
|
|
|
Trash,
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-10 11:41:25 -04:00
|
|
|
impl std::fmt::Display for Location {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
match self {
|
2024-10-09 15:41:10 -06:00
|
|
|
Self::Desktop(path, display, ..) => {
|
|
|
|
|
write!(f, "{} on display {display}", path.display())
|
|
|
|
|
}
|
|
|
|
|
Self::Network(uri, ..) => write!(f, "{}", uri),
|
2024-04-10 11:41:25 -04:00
|
|
|
Self::Path(path) => write!(f, "{}", path.display()),
|
2024-10-04 16:28:30 -06:00
|
|
|
Self::Recents => write!(f, "recents"),
|
2024-10-09 15:41:10 -06:00
|
|
|
Self::Search(path, term, ..) => write!(f, "search {} for {}", path.display(), term),
|
2024-04-10 11:41:25 -04:00
|
|
|
Self::Trash => write!(f, "trash"),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-05 16:17:23 -07:00
|
|
|
impl Location {
|
2024-09-13 09:35:37 -06:00
|
|
|
pub fn path_opt(&self) -> Option<&PathBuf> {
|
|
|
|
|
match self {
|
2024-10-09 15:41:10 -06:00
|
|
|
Self::Desktop(path, ..) => Some(&path),
|
2024-09-13 09:35:37 -06:00
|
|
|
Self::Path(path) => Some(&path),
|
2024-10-09 15:41:10 -06:00
|
|
|
Self::Search(path, ..) => Some(&path),
|
2024-09-13 09:35:37 -06:00
|
|
|
_ => None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-09 15:41:10 -06:00
|
|
|
pub fn with_path(&self, path: PathBuf) -> Self {
|
|
|
|
|
match self {
|
|
|
|
|
Self::Desktop(_, display, desktop_config) => {
|
|
|
|
|
Self::Desktop(path, display.clone(), *desktop_config)
|
|
|
|
|
}
|
|
|
|
|
Self::Path(..) => Self::Path(path),
|
2024-10-09 21:02:12 -06:00
|
|
|
Self::Search(_, term, show_hidden, _) => {
|
|
|
|
|
Self::Search(path, term.clone(), *show_hidden, Instant::now())
|
|
|
|
|
}
|
2024-10-09 15:41:10 -06:00
|
|
|
other => other.clone(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-10 13:53:01 -06:00
|
|
|
pub fn scan(&self, sizes: IconSizes) -> (Option<Item>, Vec<Item>) {
|
|
|
|
|
let items = match self {
|
2024-10-09 15:41:10 -06:00
|
|
|
Self::Desktop(path, display, desktop_config) => {
|
|
|
|
|
scan_desktop(path, display, *desktop_config, sizes)
|
2024-10-04 16:28:30 -06:00
|
|
|
}
|
2024-02-18 02:44:54 -05:00
|
|
|
Self::Path(path) => scan_path(path, sizes),
|
2024-10-09 15:41:10 -06:00
|
|
|
Self::Search(..) => {
|
|
|
|
|
// Search is done incrementally
|
|
|
|
|
Vec::new()
|
|
|
|
|
}
|
2024-02-18 02:44:54 -05:00
|
|
|
Self::Trash => scan_trash(sizes),
|
2024-08-16 14:42:09 +02:00
|
|
|
Self::Recents => scan_recents(sizes),
|
2024-10-09 15:41:10 -06:00
|
|
|
Self::Network(uri, _) => scan_network(uri, sizes),
|
2024-10-10 13:53:01 -06:00
|
|
|
};
|
|
|
|
|
let parent_item_opt = match self.path_opt() {
|
|
|
|
|
Some(path) => match item_from_path(path, sizes) {
|
|
|
|
|
Ok(item) => Some(item),
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::warn!("failed to get item for {:?}: {}", path, err);
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
//TODO: support other locations?
|
|
|
|
|
None => None,
|
|
|
|
|
};
|
|
|
|
|
(parent_item_opt, items)
|
2024-01-05 16:17:23 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-21 13:51:10 -06:00
|
|
|
pub struct TaskWrapper(pub cosmic::Task<Message>);
|
|
|
|
|
|
|
|
|
|
impl From<cosmic::Task<Message>> for TaskWrapper {
|
|
|
|
|
fn from(task: cosmic::Task<Message>) -> Self {
|
|
|
|
|
Self(task)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl fmt::Debug for TaskWrapper {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
f.debug_struct("TaskWrapper").finish()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-20 13:26:10 -06:00
|
|
|
#[derive(Debug)]
|
2024-02-26 12:51:22 -07:00
|
|
|
pub enum Command {
|
|
|
|
|
Action(Action),
|
2024-09-12 15:54:54 -06:00
|
|
|
AddNetworkDrive,
|
2024-10-14 10:09:57 -06:00
|
|
|
AddToSidebar(PathBuf),
|
2024-11-19 20:17:58 -07:00
|
|
|
ChangeLocation(String, Location, Option<Vec<PathBuf>>),
|
2024-08-20 13:26:10 -06:00
|
|
|
DropFiles(PathBuf, ClipboardPaste),
|
2024-05-09 13:24:06 -06:00
|
|
|
EmptyTrash,
|
2024-11-11 11:38:07 -07:00
|
|
|
#[cfg(feature = "desktop")]
|
2024-10-29 03:18:05 -04:00
|
|
|
ExecEntryAction(cosmic::desktop::DesktopEntryData, usize),
|
2024-10-21 13:51:10 -06:00
|
|
|
Iced(TaskWrapper),
|
2024-08-20 13:26:10 -06:00
|
|
|
MoveToTrash(Vec<PathBuf>),
|
2024-02-26 14:11:45 -07:00
|
|
|
OpenFile(PathBuf),
|
2024-06-29 19:35:27 -06:00
|
|
|
OpenInNewTab(PathBuf),
|
2024-06-30 10:05:58 -06:00
|
|
|
OpenInNewWindow(PathBuf),
|
2024-10-04 16:28:30 -06:00
|
|
|
OpenTrash,
|
2024-10-02 15:26:02 -06:00
|
|
|
Preview(PreviewKind),
|
2024-09-23 15:56:32 -06:00
|
|
|
WindowDrag,
|
2024-09-23 16:05:43 -06:00
|
|
|
WindowToggleMaximize,
|
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 {
|
2024-09-12 15:54:54 -06:00
|
|
|
AddNetworkDrive,
|
2024-01-10 10:26:30 -07:00
|
|
|
Click(Option<usize>),
|
2024-04-10 11:41:25 -04:00
|
|
|
DoubleClick(Option<usize>),
|
|
|
|
|
ClickRelease(Option<usize>),
|
|
|
|
|
DragEnd(Option<usize>),
|
2024-02-11 00:24:35 -05:00
|
|
|
Config(TabConfig),
|
2024-02-26 12:05:29 -07:00
|
|
|
ContextAction(Action),
|
|
|
|
|
ContextMenu(Option<Point>),
|
2024-06-30 15:45:01 -06:00
|
|
|
LocationContextMenuPoint(Option<Point>),
|
|
|
|
|
LocationContextMenuIndex(Option<usize>),
|
2024-06-30 10:05:58 -06:00
|
|
|
LocationMenuAction(LocationMenuAction),
|
2024-02-29 11:25:46 -07:00
|
|
|
Drag(Option<Rectangle>),
|
2024-01-29 11:58:36 -07:00
|
|
|
EditLocation(Option<Location>),
|
2024-10-13 12:01:55 -05:00
|
|
|
EditLocationEnable,
|
2024-06-30 10:05:58 -06:00
|
|
|
OpenInNewTab(PathBuf),
|
2024-05-09 13:24:06 -06:00
|
|
|
EmptyTrash,
|
2024-11-11 11:38:07 -07:00
|
|
|
#[cfg(feature = "desktop")]
|
2024-10-29 03:18:05 -04:00
|
|
|
ExecEntryAction(Option<PathBuf>, usize),
|
2024-09-23 15:56:32 -06:00
|
|
|
Gallery(bool),
|
2024-09-24 08:39:13 -06:00
|
|
|
GalleryPrevious,
|
|
|
|
|
GalleryNext,
|
2024-10-02 13:23:41 -06:00
|
|
|
GalleryToggle,
|
2024-01-30 10:47:41 -07:00
|
|
|
GoNext,
|
|
|
|
|
GoPrevious,
|
2024-02-28 16:16:59 -07:00
|
|
|
ItemDown,
|
|
|
|
|
ItemLeft,
|
|
|
|
|
ItemRight,
|
|
|
|
|
ItemUp,
|
2024-01-09 15:34:48 -07:00
|
|
|
Location(Location),
|
2024-02-06 22:58:29 -05:00
|
|
|
LocationUp,
|
2024-09-23 11:59:05 -06:00
|
|
|
Open(Option<PathBuf>),
|
2024-06-30 14:57:37 -06:00
|
|
|
RightClick(Option<usize>),
|
2024-06-29 19:35:27 -06:00
|
|
|
MiddleClick(usize),
|
2024-02-26 12:05:29 -07:00
|
|
|
Scroll(Viewport),
|
2024-09-20 14:34:44 -06:00
|
|
|
ScrollToFocus,
|
2024-10-09 18:55:09 -06:00
|
|
|
SearchContext(Location, SearchContextWrapper),
|
|
|
|
|
SearchReady(bool),
|
2024-02-29 21:13:03 -07:00
|
|
|
SelectAll,
|
2024-09-11 13:29:00 -06:00
|
|
|
SetSort(HeadingOptions, bool),
|
2024-03-04 13:41:18 -07:00
|
|
|
Thumbnail(PathBuf, ItemThumbnail),
|
2024-02-11 00:24:35 -05:00
|
|
|
ToggleShowHidden,
|
2024-01-05 15:17:38 -07:00
|
|
|
View(View),
|
2024-02-28 05:18:40 +01:00
|
|
|
ToggleSort(HeadingOptions),
|
2024-04-10 11:41:25 -04:00
|
|
|
Drop(Option<(Location, ClipboardPaste)>),
|
|
|
|
|
DndHover(Location),
|
|
|
|
|
DndEnter(Location),
|
|
|
|
|
DndLeave(Location),
|
2024-09-23 15:56:32 -06:00
|
|
|
WindowDrag,
|
2024-09-23 16:05:43 -06:00
|
|
|
WindowToggleMaximize,
|
2024-10-02 15:49:13 -06:00
|
|
|
ZoomIn,
|
|
|
|
|
ZoomOut,
|
2024-10-16 10:22:25 +11:00
|
|
|
HighlightDeactivate(usize),
|
|
|
|
|
HighlightActivate(usize),
|
2024-11-15 17:30:25 -07:00
|
|
|
DirectorySize(PathBuf, DirSize),
|
2024-01-05 09:36:16 -07:00
|
|
|
}
|
|
|
|
|
|
2024-06-30 10:05:58 -06:00
|
|
|
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
|
|
|
|
pub enum LocationMenuAction {
|
|
|
|
|
OpenInNewTab(usize),
|
|
|
|
|
OpenInNewWindow(usize),
|
2024-09-20 14:13:04 -06:00
|
|
|
Preview(usize),
|
2024-10-14 10:09:57 -06:00
|
|
|
AddToSidebar(usize),
|
2024-06-30 10:05:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl MenuAction for LocationMenuAction {
|
|
|
|
|
type Message = Message;
|
|
|
|
|
|
|
|
|
|
fn message(&self) -> Self::Message {
|
|
|
|
|
Message::LocationMenuAction(*self)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-15 17:30:25 -07:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
|
pub enum DirSize {
|
|
|
|
|
Calculating(Controller),
|
|
|
|
|
Directory(u64),
|
|
|
|
|
NotDirectory,
|
|
|
|
|
Error(String),
|
|
|
|
|
}
|
|
|
|
|
|
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-09-13 15:13:37 -06:00
|
|
|
SimpleDir {
|
|
|
|
|
entries: u64,
|
|
|
|
|
},
|
|
|
|
|
SimpleFile {
|
|
|
|
|
size: u64,
|
|
|
|
|
},
|
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-09-13 15:13:37 -06:00
|
|
|
Self::SimpleDir { .. } => true,
|
|
|
|
|
Self::SimpleFile { .. } => false,
|
2024-01-06 12:59:36 -07:00
|
|
|
}
|
|
|
|
|
}
|
2024-10-09 20:18:43 -06:00
|
|
|
|
|
|
|
|
pub fn modified(&self) -> Option<SystemTime> {
|
|
|
|
|
match self {
|
|
|
|
|
Self::Path { metadata, .. } => metadata.modified().ok(),
|
|
|
|
|
_ => None,
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-06 12:59:36 -07:00
|
|
|
}
|
|
|
|
|
|
2024-10-10 10:05:48 -06:00
|
|
|
#[derive(Debug)]
|
2024-03-04 13:41:18 -07:00
|
|
|
pub enum ItemThumbnail {
|
|
|
|
|
NotImage,
|
2024-10-10 09:07:02 -06:00
|
|
|
Image(widget::image::Handle, Option<(u32, u32)>),
|
|
|
|
|
Svg(widget::svg::Handle),
|
2024-10-10 10:05:48 -06:00
|
|
|
Text(widget::text_editor::Content),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Clone for ItemThumbnail {
|
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
|
match self {
|
|
|
|
|
Self::NotImage => Self::NotImage,
|
|
|
|
|
Self::Image(handle, size_opt) => Self::Image(handle.clone(), *size_opt),
|
|
|
|
|
Self::Svg(handle) => Self::Svg(handle.clone()),
|
2024-10-10 11:15:23 -06:00
|
|
|
// Content cannot be cloned simply
|
|
|
|
|
Self::Text(content) => {
|
|
|
|
|
Self::Text(widget::text_editor::Content::with_text(&content.text()))
|
|
|
|
|
}
|
2024-10-10 10:05:48 -06:00
|
|
|
}
|
|
|
|
|
}
|
2024-03-04 13:41:18 -07:00
|
|
|
}
|
|
|
|
|
|
2024-09-25 13:28:14 -06:00
|
|
|
impl ItemThumbnail {
|
2024-10-10 10:05:48 -06:00
|
|
|
pub fn new(path: &Path, metadata: fs::Metadata, mime: mime::Mime, thumbnail_size: u32) -> Self {
|
|
|
|
|
let size = metadata.len();
|
|
|
|
|
let check_size = |thumbnailer: &str, max_size| {
|
|
|
|
|
if size <= max_size {
|
|
|
|
|
true
|
|
|
|
|
} else {
|
|
|
|
|
log::warn!(
|
|
|
|
|
"skipping internal {} thumbnailer for {:?}: file size {} is larger than {}",
|
|
|
|
|
thumbnailer,
|
|
|
|
|
path,
|
|
|
|
|
format_size(size),
|
|
|
|
|
format_size(max_size)
|
|
|
|
|
);
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
//TODO: adjust limits for internal thumbnailers as desired
|
|
|
|
|
if mime.type_() == mime::IMAGE
|
|
|
|
|
&& mime.subtype() == mime::SVG
|
|
|
|
|
&& check_size("svg", 8 * 1000 * 1000)
|
|
|
|
|
{
|
2024-09-25 14:18:28 -06:00
|
|
|
// Try built-in svg thumbnailer
|
2024-09-25 13:28:14 -06:00
|
|
|
match fs::read(&path) {
|
|
|
|
|
Ok(data) => {
|
|
|
|
|
//TODO: validate SVG data
|
2024-10-10 09:07:02 -06:00
|
|
|
return ItemThumbnail::Svg(widget::svg::Handle::from_memory(data));
|
2024-09-25 13:28:14 -06:00
|
|
|
}
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::warn!("failed to read {:?}: {}", path, err);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-10 10:05:48 -06:00
|
|
|
} else if mime.type_() == mime::IMAGE && check_size("image", 64 * 1000 * 1000) {
|
2024-09-25 14:18:28 -06:00
|
|
|
// Try built-in image thumbnailer
|
2024-11-11 09:14:03 -07:00
|
|
|
match image::ImageReader::open(&path).and_then(|img| img.with_guessed_format()) {
|
2024-09-25 13:28:14 -06:00
|
|
|
Ok(reader) => match reader.decode() {
|
|
|
|
|
Ok(image) => {
|
2024-10-10 09:07:02 -06:00
|
|
|
let thumbnail =
|
|
|
|
|
image.thumbnail(thumbnail_size, thumbnail_size).into_rgba8();
|
|
|
|
|
return ItemThumbnail::Image(
|
2024-10-21 13:51:10 -06:00
|
|
|
widget::image::Handle::from_rgba(
|
2024-10-10 09:07:02 -06:00
|
|
|
thumbnail.width(),
|
|
|
|
|
thumbnail.height(),
|
|
|
|
|
thumbnail.into_raw(),
|
|
|
|
|
),
|
2024-09-25 13:28:14 -06:00
|
|
|
Some((image.width(), image.height())),
|
2024-09-25 14:18:28 -06:00
|
|
|
);
|
2024-09-25 13:28:14 -06:00
|
|
|
}
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::warn!("failed to decode {:?}: {}", path, err);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::warn!("failed to read {:?}: {}", path, err);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-10 10:05:48 -06:00
|
|
|
} else if mime.type_() == mime::TEXT && check_size("text", 8 * 1000 * 1000) {
|
2024-11-14 09:18:35 -07:00
|
|
|
/*TODO: fix performance issues, widget::text_editr::Content::with_text forces all text to shape, which blocks rendering
|
2024-10-10 10:05:48 -06:00
|
|
|
match fs::read_to_string(&path) {
|
|
|
|
|
Ok(data) => {
|
|
|
|
|
return ItemThumbnail::Text(widget::text_editor::Content::with_text(&data));
|
|
|
|
|
}
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::warn!("failed to read {:?}: {}", path, err);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-14 09:18:35 -07:00
|
|
|
*/
|
2024-09-25 14:18:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Try external thumbnailers
|
|
|
|
|
for thumbnailer in thumbnailer(&mime) {
|
|
|
|
|
let prefix = if thumbnailer.exec.starts_with("evince-thumbnailer ") {
|
2024-09-25 13:28:14 -06:00
|
|
|
//TODO: apparmor config for evince-thumbnailer does not allow /tmp/cosmic-files*
|
2024-09-25 14:18:28 -06:00
|
|
|
"gnome-desktop-"
|
2024-09-25 13:28:14 -06:00
|
|
|
} else {
|
2024-09-25 14:18:28 -06:00
|
|
|
"cosmic-files-"
|
2024-09-25 13:28:14 -06:00
|
|
|
};
|
2024-09-25 14:18:28 -06:00
|
|
|
let file = match tempfile::NamedTempFile::with_prefix(prefix) {
|
|
|
|
|
Ok(ok) => ok,
|
2024-09-25 13:28:14 -06:00
|
|
|
Err(err) => {
|
|
|
|
|
log::warn!(
|
|
|
|
|
"failed to create temporary file for thumbnail of {:?}: {}",
|
|
|
|
|
path,
|
|
|
|
|
err
|
|
|
|
|
);
|
2024-09-25 14:18:28 -06:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let Some(mut command) = thumbnailer.command(&path, file.path(), thumbnail_size) else {
|
|
|
|
|
continue;
|
|
|
|
|
};
|
|
|
|
|
match command.status() {
|
|
|
|
|
Ok(status) => {
|
|
|
|
|
if status.success() {
|
2024-11-11 09:14:03 -07:00
|
|
|
match image::ImageReader::open(file.path())
|
2024-09-25 14:18:28 -06:00
|
|
|
.and_then(|img| img.with_guessed_format())
|
|
|
|
|
{
|
2024-10-10 09:07:02 -06:00
|
|
|
Ok(reader) => match reader.decode().map(|image| image.into_rgba8()) {
|
2024-09-25 14:18:28 -06:00
|
|
|
Ok(image) => {
|
2024-10-10 09:07:02 -06:00
|
|
|
return ItemThumbnail::Image(
|
2024-10-21 13:51:10 -06:00
|
|
|
widget::image::Handle::from_rgba(
|
2024-10-10 09:07:02 -06:00
|
|
|
image.width(),
|
|
|
|
|
image.height(),
|
|
|
|
|
image.into_raw(),
|
|
|
|
|
),
|
|
|
|
|
None,
|
|
|
|
|
);
|
2024-09-25 14:18:28 -06:00
|
|
|
}
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::warn!("failed to decode {:?}: {}", path, err);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::warn!("failed to read {:?}: {}", path, err);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
log::warn!("failed to run {:?} for {:?}: {}", thumbnailer, path, status);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::warn!("failed to run {:?} for {:?}: {}", thumbnailer, path, err);
|
2024-09-25 13:28:14 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-25 14:18:28 -06:00
|
|
|
|
|
|
|
|
ItemThumbnail::NotImage
|
2024-09-25 13:28:14 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-01 16:10:30 -07:00
|
|
|
#[derive(Clone, Debug)]
|
2024-01-05 09:36:16 -07:00
|
|
|
pub struct Item {
|
|
|
|
|
pub name: String,
|
2024-08-20 13:26:10 -06:00
|
|
|
pub display_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-09-13 09:35:37 -06:00
|
|
|
pub location_opt: Option<Location>,
|
2024-03-01 16:10:30 -07:00
|
|
|
pub mime: Mime,
|
2024-01-05 09:36:16 -07:00
|
|
|
pub icon_handle_grid: widget::icon::Handle,
|
|
|
|
|
pub icon_handle_list: widget::icon::Handle,
|
2024-02-29 19:47:27 -07:00
|
|
|
pub icon_handle_list_condensed: widget::icon::Handle,
|
2024-03-01 16:10:30 -07:00
|
|
|
pub open_with: Vec<MimeApp>,
|
2024-03-04 13:41:18 -07:00
|
|
|
pub thumbnail_opt: Option<ItemThumbnail>,
|
2024-02-29 15:38:03 -07:00
|
|
|
pub button_id: widget::Id,
|
2024-02-29 13:24:57 -07:00
|
|
|
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,
|
2024-10-16 10:22:25 +11:00
|
|
|
pub highlighted: bool,
|
2024-05-26 22:03:43 -06:00
|
|
|
pub overlaps_drag_rect: bool,
|
2024-11-15 17:30:25 -07:00
|
|
|
pub dir_size: DirSize,
|
2024-01-05 09:36:16 -07:00
|
|
|
}
|
|
|
|
|
|
2024-01-05 14:44:20 -07:00
|
|
|
impl Item {
|
2024-08-20 13:26:10 -06:00
|
|
|
fn display_name(name: &str) -> String {
|
2024-07-11 10:12:50 -06:00
|
|
|
// In order to wrap at periods and underscores, add a zero width space after each one
|
|
|
|
|
name.replace(".", ".\u{200B}").replace("_", "_\u{200B}")
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-13 09:35:37 -06:00
|
|
|
pub fn path_opt(&self) -> Option<&PathBuf> {
|
|
|
|
|
self.location_opt.as_ref()?.path_opt()
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-10 10:05:48 -06:00
|
|
|
pub fn can_gallery(&self) -> bool {
|
|
|
|
|
self.mime.type_() == mime::IMAGE || self.mime.type_() == mime::TEXT
|
2024-10-09 18:55:09 -06:00
|
|
|
}
|
|
|
|
|
|
2024-10-10 10:05:48 -06:00
|
|
|
fn preview<'a>(&'a self, sizes: IconSizes) -> Element<'a, app::Message> {
|
2024-11-10 08:04:43 +01:00
|
|
|
let spacing = cosmic::theme::active().cosmic().spacing;
|
2024-03-04 13:41:18 -07:00
|
|
|
// This loads the image only if thumbnailing worked
|
2024-03-20 09:56:54 -06:00
|
|
|
let icon = widget::icon::icon(self.icon_handle_grid.clone())
|
|
|
|
|
.content_fit(ContentFit::Contain)
|
|
|
|
|
.size(sizes.grid())
|
|
|
|
|
.into();
|
2024-03-04 13:41:18 -07:00
|
|
|
match self
|
|
|
|
|
.thumbnail_opt
|
|
|
|
|
.as_ref()
|
|
|
|
|
.unwrap_or(&ItemThumbnail::NotImage)
|
|
|
|
|
{
|
2024-03-20 09:56:54 -06:00
|
|
|
ItemThumbnail::NotImage => icon,
|
2024-10-10 09:07:02 -06:00
|
|
|
ItemThumbnail::Image(handle, _) => {
|
2024-10-04 16:28:30 -06:00
|
|
|
if let Some(path) = self.path_opt() {
|
2024-10-10 10:05:48 -06:00
|
|
|
if self.mime.type_() == mime::IMAGE {
|
2024-09-25 11:30:17 -06:00
|
|
|
return widget::image(widget::image::Handle::from_path(path)).into();
|
|
|
|
|
}
|
2024-03-20 09:56:54 -06:00
|
|
|
}
|
2024-10-10 09:07:02 -06:00
|
|
|
widget::image(handle.clone()).into()
|
2024-03-04 13:41:18 -07:00
|
|
|
}
|
2024-10-10 09:07:02 -06:00
|
|
|
ItemThumbnail::Svg(handle) => widget::svg(handle.clone()).into(),
|
2024-11-11 23:53:25 +01:00
|
|
|
ItemThumbnail::Text(content) => widget::text_editor(content)
|
|
|
|
|
.class(cosmic::theme::iced::TextEditor::Custom(Box::new(
|
|
|
|
|
text_editor_class,
|
|
|
|
|
)))
|
|
|
|
|
.width(THUMBNAIL_SIZE as f32)
|
|
|
|
|
.height(Length::Fixed(THUMBNAIL_SIZE as f32))
|
|
|
|
|
.padding(spacing.space_xxs)
|
|
|
|
|
.into(),
|
2024-03-04 13:41:18 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-22 16:47:16 +01:00
|
|
|
pub fn preview_header(&self) -> Vec<Element<app::Message>> {
|
|
|
|
|
let mut row = Vec::with_capacity(3);
|
|
|
|
|
row.push(
|
|
|
|
|
widget::button::icon(widget::icon::from_name("go-previous-symbolic"))
|
|
|
|
|
.on_press(app::Message::TabMessage(None, Message::ItemLeft))
|
|
|
|
|
.into(),
|
|
|
|
|
);
|
|
|
|
|
row.push(
|
|
|
|
|
widget::button::icon(widget::icon::from_name("go-next-symbolic"))
|
|
|
|
|
.on_press(app::Message::TabMessage(None, Message::ItemRight))
|
|
|
|
|
.into(),
|
|
|
|
|
);
|
|
|
|
|
if self.can_gallery() {
|
|
|
|
|
if let Some(_path) = self.path_opt() {
|
|
|
|
|
row.push(
|
|
|
|
|
widget::button::icon(widget::icon::from_name("view-fullscreen-symbolic"))
|
|
|
|
|
.on_press(app::Message::TabMessage(None, Message::Gallery(true)))
|
|
|
|
|
.into(),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
row
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn preview_view<'a>(&'a self, sizes: IconSizes) -> Element<'a, app::Message> {
|
2024-09-23 11:59:05 -06:00
|
|
|
let cosmic_theme::Spacing {
|
|
|
|
|
space_xxxs,
|
|
|
|
|
space_m,
|
|
|
|
|
..
|
|
|
|
|
} = theme::active().cosmic().spacing;
|
2024-02-22 15:04:37 -07:00
|
|
|
|
2024-09-23 11:59:05 -06:00
|
|
|
let mut column = widget::column().spacing(space_m);
|
|
|
|
|
|
2024-02-22 16:17:39 -07:00
|
|
|
column = column.push(widget::row::with_children(vec![
|
2024-10-21 13:51:10 -06:00
|
|
|
widget::horizontal_space().into(),
|
2024-03-04 13:41:18 -07:00
|
|
|
self.preview(sizes),
|
2024-10-21 13:51:10 -06:00
|
|
|
widget::horizontal_space().into(),
|
2024-01-05 14:58:50 -07:00
|
|
|
]));
|
2024-01-05 14:44:20 -07:00
|
|
|
|
2024-09-23 11:59:05 -06:00
|
|
|
let mut details = widget::column().spacing(space_xxxs);
|
|
|
|
|
details = details.push(widget::text::heading(self.name.clone()));
|
2024-11-25 03:24:24 +01:00
|
|
|
details = details.push(widget::text::body(fl!(
|
|
|
|
|
"type",
|
|
|
|
|
mime = self.mime.to_string()
|
|
|
|
|
)));
|
2024-09-23 12:50:46 -06:00
|
|
|
let mut settings = Vec::new();
|
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() {
|
2024-11-25 03:24:24 +01:00
|
|
|
details = details.push(widget::text::body(fl!("items", items = children)));
|
2024-11-15 17:30:25 -07:00
|
|
|
let size = match &self.dir_size {
|
|
|
|
|
DirSize::Calculating(_) => fl!("calculating"),
|
|
|
|
|
DirSize::Directory(size) => format_size(*size),
|
|
|
|
|
DirSize::NotDirectory => String::new(),
|
|
|
|
|
DirSize::Error(err) => err.clone(),
|
2024-11-15 23:24:52 +01:00
|
|
|
};
|
2024-11-25 03:24:24 +01:00
|
|
|
details = details.push(widget::text::body(fl!("item-size", size = size)));
|
2024-01-06 20:31:00 -07:00
|
|
|
} else {
|
2024-11-25 03:24:24 +01:00
|
|
|
details = details.push(widget::text::body(fl!(
|
2024-11-15 23:24:52 +01:00
|
|
|
"item-size",
|
|
|
|
|
size = format_size(metadata.len())
|
2024-02-22 16:17:39 -07:00
|
|
|
)));
|
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-11-25 03:24:24 +01:00
|
|
|
details = details.push(widget::text::body(fl!(
|
2024-11-15 23:24:52 +01:00
|
|
|
"item-created",
|
|
|
|
|
created = format_time(time).to_string()
|
|
|
|
|
)));
|
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-11-25 03:24:24 +01:00
|
|
|
details = details.push(widget::text::body(fl!(
|
2024-11-15 23:24:52 +01:00
|
|
|
"item-modified",
|
|
|
|
|
modified = format_time(time).to_string()
|
|
|
|
|
)));
|
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-11-25 03:24:24 +01:00
|
|
|
details = details.push(widget::text::body(fl!(
|
2024-11-15 23:24:52 +01:00
|
|
|
"item-accessed",
|
|
|
|
|
accessed = format_time(time).to_string()
|
|
|
|
|
)));
|
2024-01-06 12:59:36 -07:00
|
|
|
}
|
2024-09-23 12:50:46 -06:00
|
|
|
|
2024-08-21 13:52:52 -07:00
|
|
|
#[cfg(not(target_os = "windows"))]
|
|
|
|
|
{
|
2024-09-23 12:50:46 -06:00
|
|
|
settings.push(
|
|
|
|
|
widget::settings::item::builder(format_permissions_owner(
|
|
|
|
|
metadata,
|
|
|
|
|
PermissionOwner::Owner,
|
|
|
|
|
))
|
|
|
|
|
.description(fl!("owner"))
|
2024-11-25 03:24:24 +01:00
|
|
|
.control(widget::text::body(format_permissions(
|
2024-09-23 12:50:46 -06:00
|
|
|
metadata,
|
|
|
|
|
PermissionOwner::Owner,
|
|
|
|
|
))),
|
2024-08-21 13:52:52 -07:00
|
|
|
);
|
|
|
|
|
|
2024-09-23 12:50:46 -06:00
|
|
|
settings.push(
|
|
|
|
|
widget::settings::item::builder(format_permissions_owner(
|
|
|
|
|
metadata,
|
|
|
|
|
PermissionOwner::Group,
|
|
|
|
|
))
|
|
|
|
|
.description(fl!("group"))
|
2024-11-25 03:24:24 +01:00
|
|
|
.control(widget::text::body(format_permissions(
|
2024-09-23 12:50:46 -06:00
|
|
|
metadata,
|
|
|
|
|
PermissionOwner::Group,
|
|
|
|
|
))),
|
2024-08-21 13:52:52 -07:00
|
|
|
);
|
|
|
|
|
|
2024-09-23 12:50:46 -06:00
|
|
|
settings.push(widget::settings::item::builder(fl!("other")).control(
|
2024-11-25 03:24:24 +01:00
|
|
|
widget::text::body(format_permissions(metadata, PermissionOwner::Other)),
|
2024-09-23 12:50:46 -06:00
|
|
|
));
|
2024-08-21 13:52:52 -07:00
|
|
|
}
|
2024-01-06 12:59:36 -07:00
|
|
|
}
|
2024-09-13 15:13:37 -06:00
|
|
|
_ => {
|
|
|
|
|
//TODO: other metadata types
|
2024-01-05 16:17:23 -07:00
|
|
|
}
|
2024-01-05 14:44:20 -07:00
|
|
|
}
|
2024-09-23 12:55:46 -06:00
|
|
|
match self
|
|
|
|
|
.thumbnail_opt
|
|
|
|
|
.as_ref()
|
|
|
|
|
.unwrap_or(&ItemThumbnail::NotImage)
|
|
|
|
|
{
|
2024-10-10 09:07:02 -06:00
|
|
|
ItemThumbnail::Image(_, Some((width, height))) => {
|
2024-11-25 03:24:24 +01:00
|
|
|
details = details.push(widget::text::body(format!("{}x{}", width, height)));
|
2024-09-23 12:55:46 -06:00
|
|
|
}
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
2024-09-23 11:59:05 -06:00
|
|
|
column = column.push(details);
|
|
|
|
|
|
|
|
|
|
if let Some(path) = self.path_opt() {
|
|
|
|
|
column = column.push(widget::button::standard(fl!("open")).on_press(
|
|
|
|
|
app::Message::TabMessage(None, Message::Open(Some(path.to_path_buf()))),
|
|
|
|
|
));
|
|
|
|
|
}
|
2024-01-05 14:44:20 -07:00
|
|
|
|
2024-09-23 12:50:46 -06:00
|
|
|
if !settings.is_empty() {
|
|
|
|
|
let mut section = widget::settings::section();
|
|
|
|
|
for setting in settings {
|
|
|
|
|
section = section.add(setting);
|
|
|
|
|
}
|
|
|
|
|
column = column.push(section);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-22 16:17:39 -07:00
|
|
|
column.into()
|
2024-01-05 14:44:20 -07:00
|
|
|
}
|
2024-07-08 11:51:07 -06:00
|
|
|
|
2024-10-10 10:05:48 -06:00
|
|
|
pub fn replace_view<'a>(
|
|
|
|
|
&'a self,
|
2024-07-08 11:51:07 -06:00
|
|
|
heading: String,
|
|
|
|
|
sizes: IconSizes,
|
2024-10-10 10:05:48 -06:00
|
|
|
) -> Element<'a, app::Message> {
|
2024-07-08 11:51:07 -06:00
|
|
|
let cosmic_theme::Spacing { space_xxxs, .. } = theme::active().cosmic().spacing;
|
|
|
|
|
|
|
|
|
|
let mut row = widget::row().spacing(space_xxxs);
|
|
|
|
|
row = row.push(self.preview(sizes));
|
|
|
|
|
|
|
|
|
|
let mut column = widget::column().spacing(space_xxxs);
|
|
|
|
|
column = column.push(widget::text::heading(heading));
|
|
|
|
|
|
|
|
|
|
//TODO: translate!
|
|
|
|
|
//TODO: correct display of folder size?
|
|
|
|
|
match &self.metadata {
|
|
|
|
|
ItemMetadata::Path { metadata, children } => {
|
|
|
|
|
if metadata.is_dir() {
|
2024-11-25 03:24:24 +01:00
|
|
|
column = column.push(widget::text::body(format!("Items: {}", children)));
|
2024-07-08 11:51:07 -06:00
|
|
|
} else {
|
2024-11-25 03:24:24 +01:00
|
|
|
column = column.push(widget::text::body(format!(
|
2024-07-08 11:51:07 -06:00
|
|
|
"Size: {}",
|
|
|
|
|
format_size(metadata.len())
|
|
|
|
|
)));
|
|
|
|
|
}
|
|
|
|
|
if let Ok(time) = metadata.modified() {
|
2024-11-25 03:24:24 +01:00
|
|
|
column = column.push(widget::text::body(format!(
|
2024-07-08 11:51:07 -06:00
|
|
|
"Last modified: {}",
|
2024-09-15 16:00:20 -06:00
|
|
|
format_time(time)
|
2024-07-08 11:51:07 -06:00
|
|
|
)));
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-13 15:13:37 -06:00
|
|
|
_ => {
|
|
|
|
|
//TODO: other metadata
|
2024-07-08 11:51:07 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
row = row.push(column);
|
|
|
|
|
row.into()
|
|
|
|
|
}
|
2024-01-05 14:44:20 -07:00
|
|
|
}
|
|
|
|
|
|
2024-05-29 23:33:12 +02:00
|
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq, Deserialize, Serialize)]
|
2024-01-05 09:36:16 -07:00
|
|
|
pub enum View {
|
|
|
|
|
Grid,
|
|
|
|
|
List,
|
|
|
|
|
}
|
2024-03-13 23:23:00 -04:00
|
|
|
#[derive(Clone, Copy, Debug, Hash, PartialEq, PartialOrd, Ord, Eq, Deserialize, Serialize)]
|
2024-02-27 22:28:06 -07:00
|
|
|
pub enum HeadingOptions {
|
2024-03-14 02:17:27 -04:00
|
|
|
Name = 0,
|
2024-02-28 05:18:40 +01:00
|
|
|
Modified,
|
|
|
|
|
Size,
|
2024-09-20 02:27:23 -04:00
|
|
|
TrashedOn,
|
2024-02-28 05:18:40 +01:00
|
|
|
}
|
2024-01-05 09:36:16 -07:00
|
|
|
|
2024-03-14 02:17:27 -04:00
|
|
|
impl fmt::Display for HeadingOptions {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
match self {
|
|
|
|
|
HeadingOptions::Name => write!(f, "{}", fl!("name")),
|
|
|
|
|
HeadingOptions::Modified => write!(f, "{}", fl!("modified")),
|
|
|
|
|
HeadingOptions::Size => write!(f, "{}", fl!("size")),
|
2024-09-20 02:27:23 -04:00
|
|
|
HeadingOptions::TrashedOn => write!(f, "{}", fl!("trashed-on")),
|
2024-03-14 02:17:27 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl HeadingOptions {
|
|
|
|
|
pub fn names() -> Vec<String> {
|
|
|
|
|
vec![
|
|
|
|
|
HeadingOptions::Name.to_string(),
|
|
|
|
|
HeadingOptions::Modified.to_string(),
|
|
|
|
|
HeadingOptions::Size.to_string(),
|
2024-09-20 02:27:23 -04:00
|
|
|
HeadingOptions::TrashedOn.to_string(),
|
2024-03-14 02:17:27 -04:00
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-20 13:26:10 -06:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
|
pub enum Mode {
|
|
|
|
|
App,
|
|
|
|
|
Desktop,
|
|
|
|
|
Dialog(DialogKind),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Mode {
|
|
|
|
|
/// Whether multiple files can be selected in this mode
|
|
|
|
|
pub fn multiple(&self) -> bool {
|
|
|
|
|
match self {
|
|
|
|
|
Mode::App | Mode::Desktop => true,
|
|
|
|
|
Mode::Dialog(dialog) => dialog.multiple(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-09 18:55:09 -06:00
|
|
|
struct SearchContext {
|
|
|
|
|
results_rx: mpsc::Receiver<(PathBuf, String, Metadata)>,
|
|
|
|
|
ready: Arc<atomic::AtomicBool>,
|
2024-10-09 20:18:43 -06:00
|
|
|
last_modified_opt: Arc<RwLock<Option<SystemTime>>>,
|
2024-10-09 18:55:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct SearchContextWrapper(Option<SearchContext>);
|
|
|
|
|
|
|
|
|
|
impl Clone for SearchContextWrapper {
|
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
|
Self(None)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl fmt::Debug for SearchContextWrapper {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
f.debug_struct("SearchContextWrapper").finish()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-10 11:41:25 -04:00
|
|
|
// TODO when creating items, pass <Arc<SelectedItems>> to each item
|
|
|
|
|
// as a drag data, so that when dnd is initiated, they are all included
|
2024-01-05 09:36:16 -07:00
|
|
|
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-06-30 15:45:01 -06:00
|
|
|
pub location_context_menu_point: Option<Point>,
|
|
|
|
|
pub location_context_menu_index: Option<usize>,
|
2024-01-05 09:36:16 -07:00
|
|
|
pub context_menu: Option<Point>,
|
2024-08-20 13:26:10 -06:00
|
|
|
pub mode: Mode,
|
2024-02-29 16:25:54 -07:00
|
|
|
pub scroll_opt: Option<AbsoluteOffset>,
|
2024-05-31 09:51:34 -06:00
|
|
|
pub size_opt: Cell<Option<Size>>,
|
2024-08-16 08:13:25 -06:00
|
|
|
pub item_view_size_opt: Cell<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,
|
2024-01-30 10:47:41 -07:00
|
|
|
pub history_i: usize,
|
|
|
|
|
pub history: Vec<Location>,
|
2024-02-10 02:13:13 -05:00
|
|
|
pub config: TabConfig,
|
2024-10-02 14:53:24 -06:00
|
|
|
pub sort_name: HeadingOptions,
|
|
|
|
|
pub sort_direction: bool,
|
2024-09-23 15:56:32 -06:00
|
|
|
pub gallery: bool,
|
2024-10-10 13:53:01 -06:00
|
|
|
pub(crate) parent_item_opt: Option<Item>,
|
2024-03-20 16:27:00 -06:00
|
|
|
pub(crate) items_opt: Option<Vec<Item>>,
|
2024-04-10 11:41:25 -04:00
|
|
|
pub dnd_hovered: Option<(Location, Instant)>,
|
2024-02-29 16:25:54 -07:00
|
|
|
scrollable_id: widget::Id,
|
2024-02-29 15:21:59 -07:00
|
|
|
select_focus: Option<usize>,
|
2024-05-27 10:23:48 -06:00
|
|
|
select_range: Option<(usize, usize)>,
|
2024-04-10 11:41:25 -04:00
|
|
|
clicked: Option<usize>,
|
2024-05-26 22:03:43 -06:00
|
|
|
selected_clicked: bool,
|
2024-09-20 09:09:39 -06:00
|
|
|
last_right_click: Option<usize>,
|
2024-10-09 18:55:09 -06:00
|
|
|
search_context: Option<SearchContext>,
|
2024-01-05 09:36:16 -07:00
|
|
|
}
|
|
|
|
|
|
2024-11-15 17:30:25 -07:00
|
|
|
fn calculate_dir_size(path: &Path, controller: Controller) -> Result<u64, String> {
|
|
|
|
|
let mut total = 0;
|
|
|
|
|
for entry_res in WalkDir::new(path) {
|
|
|
|
|
controller.check()?;
|
|
|
|
|
//TODO: report more errors?
|
|
|
|
|
if let Ok(entry) = entry_res {
|
|
|
|
|
if let Ok(metadata) = entry.metadata() {
|
|
|
|
|
if metadata.is_file() {
|
|
|
|
|
total += metadata.len();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(total)
|
2024-11-15 23:24:52 +01:00
|
|
|
}
|
|
|
|
|
|
2024-09-09 13:27:54 -06:00
|
|
|
fn folder_name<P: AsRef<Path>>(path: P) -> (String, bool) {
|
|
|
|
|
let path = path.as_ref();
|
|
|
|
|
let mut found_home = false;
|
|
|
|
|
let name = match path.file_name() {
|
|
|
|
|
Some(name) => {
|
|
|
|
|
if path == crate::home_dir() {
|
|
|
|
|
found_home = true;
|
|
|
|
|
fl!("home")
|
|
|
|
|
} else {
|
|
|
|
|
name.to_string_lossy().to_string()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
None => {
|
|
|
|
|
fl!("filesystem")
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
(name, found_home)
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-04 11:52:34 +02:00
|
|
|
// parse .hidden file and return files path
|
2024-10-04 19:02:22 +02:00
|
|
|
fn parse_hidden_file(path: &PathBuf) -> Vec<String> {
|
2024-10-04 11:52:34 +02:00
|
|
|
let file = match File::open(path) {
|
|
|
|
|
Ok(f) => f,
|
|
|
|
|
Err(_) => return Vec::new(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let reader = BufReader::new(file);
|
2024-10-04 19:02:22 +02:00
|
|
|
let mut paths: Vec<String> = Vec::new();
|
2024-10-04 11:52:34 +02:00
|
|
|
|
|
|
|
|
for line in reader.lines() {
|
|
|
|
|
if let Ok(line) = line {
|
|
|
|
|
if !line.is_empty() {
|
2024-10-04 19:02:22 +02:00
|
|
|
paths.push(line.trim().to_string());
|
2024-10-04 11:52:34 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
paths
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-03 15:27:32 -07:00
|
|
|
impl Tab {
|
2024-02-10 02:13:13 -05:00
|
|
|
pub fn new(location: Location, config: TabConfig) -> 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-06-30 15:45:01 -06:00
|
|
|
location_context_menu_point: None,
|
|
|
|
|
location_context_menu_index: None,
|
2024-08-20 13:26:10 -06:00
|
|
|
mode: Mode::App,
|
2024-02-26 12:05:29 -07:00
|
|
|
scroll_opt: None,
|
2024-05-31 09:51:34 -06:00
|
|
|
size_opt: Cell::new(None),
|
2024-08-16 08:13:25 -06:00
|
|
|
item_view_size_opt: Cell::new(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(),
|
2024-01-30 10:47:41 -07:00
|
|
|
history_i: 0,
|
|
|
|
|
history,
|
2024-02-10 02:13:13 -05:00
|
|
|
config,
|
2024-10-02 14:53:24 -06:00
|
|
|
sort_name: HeadingOptions::Name,
|
|
|
|
|
sort_direction: true,
|
2024-09-23 15:56:32 -06:00
|
|
|
gallery: false,
|
2024-10-10 13:53:01 -06:00
|
|
|
parent_item_opt: None,
|
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,
|
2024-05-27 10:23:48 -06:00
|
|
|
select_range: None,
|
2024-04-10 11:41:25 -04:00
|
|
|
clicked: None,
|
|
|
|
|
dnd_hovered: None,
|
2024-05-26 22:03:43 -06:00
|
|
|
selected_clicked: false,
|
2024-09-20 09:09:39 -06:00
|
|
|
last_right_click: None,
|
2024-10-09 18:55:09 -06:00
|
|
|
search_context: None,
|
2024-01-03 15:27:32 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn title(&self) -> String {
|
2024-01-05 16:17:23 -07:00
|
|
|
match &self.location {
|
2024-10-09 15:41:10 -06:00
|
|
|
Location::Desktop(path, _, _) => {
|
2024-10-04 16:28:30 -06:00
|
|
|
let (name, _) = folder_name(path);
|
|
|
|
|
name
|
|
|
|
|
}
|
2024-01-05 16:17:23 -07:00
|
|
|
Location::Path(path) => {
|
2024-09-09 13:27:54 -06:00
|
|
|
let (name, _) = folder_name(path);
|
|
|
|
|
name
|
2024-01-05 16:17:23 -07:00
|
|
|
}
|
2024-10-09 15:41:10 -06:00
|
|
|
Location::Search(path, term, ..) => {
|
2024-05-31 15:19:47 -06:00
|
|
|
//TODO: translate
|
2024-09-09 13:27:54 -06:00
|
|
|
let (name, _) = folder_name(path);
|
|
|
|
|
format!("Search \"{}\": {}", term, name)
|
2024-05-31 15:19:47 -06:00
|
|
|
}
|
2024-01-05 16:17:23 -07:00
|
|
|
Location::Trash => {
|
|
|
|
|
fl!("trash")
|
|
|
|
|
}
|
2024-08-16 14:42:09 +02:00
|
|
|
Location::Recents => {
|
|
|
|
|
fl!("recents")
|
|
|
|
|
}
|
2024-09-13 15:13:37 -06:00
|
|
|
Location::Network(_uri, display_name) => display_name.clone(),
|
2024-01-05 16:17:23 -07:00
|
|
|
}
|
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()
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-20 13:26:10 -06:00
|
|
|
pub fn items_opt_mut(&mut self) -> Option<&mut Vec<Item>> {
|
|
|
|
|
self.items_opt.as_mut()
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-25 16:06:11 -06:00
|
|
|
pub fn set_items(&mut self, mut items: Vec<Item>) {
|
|
|
|
|
let selected = self.selected_locations();
|
|
|
|
|
for item in items.iter_mut() {
|
|
|
|
|
item.selected = false;
|
|
|
|
|
if let Some(location) = &item.location_opt {
|
|
|
|
|
if selected.contains(location) {
|
|
|
|
|
item.selected = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-29 13:42:13 -07:00
|
|
|
self.items_opt = Some(items);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-25 16:06:11 -06:00
|
|
|
pub fn selected_locations(&self) -> Vec<Location> {
|
|
|
|
|
let mut locations = Vec::new();
|
|
|
|
|
if let Some(ref items) = self.items_opt {
|
|
|
|
|
for item in items.iter() {
|
|
|
|
|
if item.selected {
|
|
|
|
|
if let Some(location) = &item.location_opt {
|
|
|
|
|
locations.push(location.clone());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
locations
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-29 13:42:13 -07:00
|
|
|
pub fn select_all(&mut self) {
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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) {
|
2024-02-29 14:34:41 -07:00
|
|
|
if let Some(ref mut items) = self.items_opt {
|
2024-02-29 21:13:03 -07:00
|
|
|
for item in items.iter_mut() {
|
2024-08-17 13:16:53 -06:00
|
|
|
item.selected = item.name == name;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-19 20:17:58 -07:00
|
|
|
pub fn select_paths(&mut self, paths: Vec<PathBuf>) {
|
2024-08-17 13:16:53 -06:00
|
|
|
if let Some(ref mut items) = self.items_opt {
|
|
|
|
|
for item in items.iter_mut() {
|
2024-11-19 20:17:58 -07:00
|
|
|
item.selected = false;
|
|
|
|
|
if let Some(path) = item.path_opt() {
|
|
|
|
|
if paths.contains(path) {
|
|
|
|
|
item.selected = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
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 {
|
2024-05-27 10:23:48 -06:00
|
|
|
if self.select_focus.is_none() || self.select_range.is_none() {
|
|
|
|
|
// Set select range to initial state if necessary
|
|
|
|
|
self.select_range = self.select_focus.map(|i| (i, i));
|
2024-02-29 15:21:59 -07:00
|
|
|
}
|
2024-05-27 10:23:48 -06:00
|
|
|
|
|
|
|
|
if let Some(pos) = self.select_range_start_pos_opt() {
|
2024-02-29 15:21:59 -07:00
|
|
|
if pos.0 < row || (pos.0 == row && pos.1 < col) {
|
|
|
|
|
start = pos;
|
|
|
|
|
} else {
|
|
|
|
|
end = pos;
|
2024-02-29 13:24:57 -07:00
|
|
|
}
|
2024-02-29 15:21:59 -07:00
|
|
|
}
|
2024-05-27 10:23:48 -06:00
|
|
|
}
|
2024-02-29 13:24:57 -07:00
|
|
|
|
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-05-27 10:23:48 -06:00
|
|
|
self.select_range = if mod_shift {
|
|
|
|
|
self.select_range.map(|r| (r.0, i))
|
|
|
|
|
} else {
|
|
|
|
|
Some((i, i))
|
|
|
|
|
};
|
2024-05-26 22:03:43 -06:00
|
|
|
found = true;
|
2024-02-29 13:24:57 -07:00
|
|
|
}
|
2024-02-29 15:21:59 -07:00
|
|
|
item.selected = true;
|
|
|
|
|
}
|
2024-02-29 13:24:57 -07:00
|
|
|
}
|
2024-02-29 15:21:59 -07:00
|
|
|
found
|
2024-02-29 13:24:57 -07:00
|
|
|
}
|
|
|
|
|
|
2024-05-26 22:03:43 -06:00
|
|
|
pub fn select_rect(&mut self, rect: Rectangle, mod_ctrl: bool, mod_shift: bool) {
|
2024-02-29 13:24:57 -07:00
|
|
|
if let Some(ref mut items) = self.items_opt {
|
2024-05-26 22:03:43 -06:00
|
|
|
for item in items.iter_mut() {
|
|
|
|
|
let was_overlapped = item.overlaps_drag_rect;
|
|
|
|
|
item.overlaps_drag_rect = item
|
|
|
|
|
.rect_opt
|
|
|
|
|
.get()
|
|
|
|
|
.map(|r| r.intersects(&rect))
|
|
|
|
|
.unwrap_or(false);
|
|
|
|
|
|
|
|
|
|
item.selected = if mod_ctrl || mod_shift {
|
|
|
|
|
if was_overlapped == item.overlaps_drag_rect {
|
|
|
|
|
item.selected
|
|
|
|
|
} else {
|
|
|
|
|
!item.selected
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
item.overlaps_drag_rect
|
2024-02-29 15:21:59 -07:00
|
|
|
};
|
2024-02-29 13:24:57 -07:00
|
|
|
}
|
|
|
|
|
}
|
2024-02-29 15:21:59 -07:00
|
|
|
}
|
|
|
|
|
|
2024-02-29 20:53:15 -07:00
|
|
|
pub fn select_focus_id(&self) -> Option<widget::Id> {
|
2024-02-29 15:38:03 -07:00
|
|
|
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),
|
|
|
|
|
};
|
2024-08-16 08:13:25 -06:00
|
|
|
let size = self
|
|
|
|
|
.item_view_size_opt
|
|
|
|
|
.get()
|
|
|
|
|
.unwrap_or_else(|| Size::new(0.0, 0.0));
|
2024-02-29 16:25:54 -07:00
|
|
|
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-05-27 10:23:48 -06:00
|
|
|
fn select_range_start_pos_opt(&self) -> Option<(usize, usize)> {
|
2024-02-29 15:21:59 -07:00
|
|
|
let items = self.items_opt.as_ref()?;
|
2024-05-27 10:23:48 -06:00
|
|
|
let item = items.get(self.select_range.map(|r| r.0)?)?;
|
2024-02-29 15:21:59 -07:00
|
|
|
item.pos_opt.get()
|
2024-02-29 13:24:57 -07:00
|
|
|
}
|
|
|
|
|
|
2024-02-29 21:13:03 -07:00
|
|
|
fn select_first_pos_opt(&self) -> Option<(usize, usize)> {
|
|
|
|
|
let items = self.items_opt.as_ref()?;
|
|
|
|
|
let mut first = None;
|
|
|
|
|
for item in items.iter() {
|
|
|
|
|
if !item.selected {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let (row, col) = match item.pos_opt.get() {
|
|
|
|
|
Some(some) => some,
|
|
|
|
|
None => continue,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
first = Some(match first {
|
|
|
|
|
Some((first_row, first_col)) => {
|
|
|
|
|
if row < first_row {
|
|
|
|
|
(row, col)
|
|
|
|
|
} else if row == first_row {
|
|
|
|
|
(row, col.min(first_row))
|
|
|
|
|
} else {
|
|
|
|
|
(first_row, first_col)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
None => (row, col),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
first
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn select_last_pos_opt(&self) -> Option<(usize, usize)> {
|
|
|
|
|
let items = self.items_opt.as_ref()?;
|
|
|
|
|
let mut last = None;
|
|
|
|
|
for item in items.iter() {
|
|
|
|
|
if !item.selected {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let (row, col) = match item.pos_opt.get() {
|
|
|
|
|
Some(some) => some,
|
|
|
|
|
None => continue,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
last = Some(match last {
|
|
|
|
|
Some((last_row, last_col)) => {
|
|
|
|
|
if row > last_row {
|
|
|
|
|
(row, col)
|
|
|
|
|
} else if row == last_row {
|
|
|
|
|
(row, col.max(last_row))
|
|
|
|
|
} else {
|
|
|
|
|
(last_row, last_col)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
None => (row, col),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
last
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-28 09:28:46 -06:00
|
|
|
pub fn change_location(&mut self, location: &Location, history_i_opt: Option<usize>) {
|
|
|
|
|
self.location = location.clone();
|
2024-09-17 11:31:15 -06:00
|
|
|
self.context_menu = None;
|
2024-05-28 09:28:46 -06:00
|
|
|
self.edit_location = None;
|
2024-09-25 15:32:54 -06:00
|
|
|
self.items_opt = None;
|
|
|
|
|
//TODO: remember scroll by location?
|
|
|
|
|
self.scroll_opt = None;
|
|
|
|
|
self.select_focus = None;
|
2024-10-09 18:55:09 -06:00
|
|
|
self.search_context = None;
|
2024-05-28 09:28:46 -06: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.clone());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
2024-01-30 10:47:41 -07:00
|
|
|
let mut history_i_opt = None;
|
2024-08-20 13:26:10 -06:00
|
|
|
let mod_ctrl = modifiers.contains(Modifiers::CTRL) && self.mode.multiple();
|
|
|
|
|
let mod_shift = modifiers.contains(Modifiers::SHIFT) && self.mode.multiple();
|
2024-01-03 15:27:32 -07:00
|
|
|
match message {
|
2024-09-12 15:54:54 -06:00
|
|
|
Message::AddNetworkDrive => {
|
|
|
|
|
commands.push(Command::AddNetworkDrive);
|
|
|
|
|
}
|
2024-04-10 11:41:25 -04:00
|
|
|
Message::ClickRelease(click_i_opt) => {
|
2024-05-26 19:50:41 -06:00
|
|
|
if click_i_opt == self.clicked.take() {
|
2024-04-10 11:41:25 -04:00
|
|
|
return commands;
|
|
|
|
|
}
|
|
|
|
|
self.context_menu = None;
|
2024-06-30 15:45:01 -06:00
|
|
|
self.location_context_menu_index = None;
|
2024-05-26 19:50:41 -06:00
|
|
|
if let Some(ref mut items) = self.items_opt {
|
|
|
|
|
for (i, item) in items.iter_mut().enumerate() {
|
|
|
|
|
if mod_ctrl {
|
|
|
|
|
if Some(i) == click_i_opt && item.selected {
|
|
|
|
|
item.selected = false;
|
2024-05-27 10:23:48 -06:00
|
|
|
self.select_range = None;
|
2024-05-26 19:50:41 -06:00
|
|
|
}
|
|
|
|
|
} else if Some(i) != click_i_opt {
|
|
|
|
|
item.selected = false;
|
2024-04-10 11:41:25 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Message::DragEnd(_) => {
|
|
|
|
|
self.clicked = None;
|
2024-05-26 22:03:43 -06:00
|
|
|
if let Some(ref mut items) = self.items_opt {
|
|
|
|
|
for item in items.iter_mut() {
|
|
|
|
|
item.overlaps_drag_rect = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-10 11:41:25 -04:00
|
|
|
}
|
|
|
|
|
Message::DoubleClick(click_i_opt) => {
|
|
|
|
|
if let Some(clicked_item) = self
|
|
|
|
|
.items_opt
|
|
|
|
|
.as_ref()
|
|
|
|
|
.and_then(|items| click_i_opt.and_then(|click_i| items.get(click_i)))
|
|
|
|
|
{
|
2024-09-13 15:13:37 -06:00
|
|
|
if let Some(location) = &clicked_item.location_opt {
|
2024-04-10 11:41:25 -04:00
|
|
|
if clicked_item.metadata.is_dir() {
|
2024-09-13 15:13:37 -06:00
|
|
|
cd = Some(location.clone());
|
2024-04-10 11:41:25 -04:00
|
|
|
} else {
|
2024-10-04 16:28:30 -06:00
|
|
|
if let Some(path) = location.path_opt() {
|
|
|
|
|
commands.push(Command::OpenFile(path.to_path_buf()));
|
2024-09-13 15:13:37 -06:00
|
|
|
} else {
|
|
|
|
|
log::warn!("no path for item {:?}", clicked_item);
|
|
|
|
|
}
|
2024-04-10 11:41:25 -04:00
|
|
|
}
|
|
|
|
|
} else {
|
2024-09-13 15:13:37 -06:00
|
|
|
log::warn!("no location for item {:?}", clicked_item);
|
2024-04-10 11:41:25 -04:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
log::warn!("no item for click index {:?}", click_i_opt);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-10 10:26:30 -07:00
|
|
|
Message::Click(click_i_opt) => {
|
2024-05-26 22:03:43 -06:00
|
|
|
self.selected_clicked = false;
|
2024-04-10 11:41:25 -04:00
|
|
|
self.context_menu = None;
|
2024-10-13 12:01:55 -05:00
|
|
|
self.edit_location = None;
|
2024-06-30 15:45:01 -06:00
|
|
|
self.location_context_menu_index = None;
|
2024-05-26 19:50:41 -06:00
|
|
|
if click_i_opt.is_none() {
|
2024-04-10 11:41:25 -04:00
|
|
|
self.clicked = click_i_opt;
|
|
|
|
|
}
|
2024-05-27 10:23:48 -06:00
|
|
|
|
|
|
|
|
if mod_shift {
|
|
|
|
|
if let Some(click_i) = click_i_opt {
|
|
|
|
|
self.select_range = self
|
|
|
|
|
.select_range
|
|
|
|
|
.map_or(Some((click_i, click_i)), |r| Some((r.0, click_i)));
|
|
|
|
|
if let Some(range) = self.select_range {
|
|
|
|
|
let min = range.0.min(range.1);
|
|
|
|
|
let max = range.0.max(range.1);
|
2024-10-09 18:55:09 -06:00
|
|
|
let (sort_name, sort_direction, _) = self.sort_options();
|
2024-10-09 20:18:43 -06:00
|
|
|
//TODO: this assumes the default sort order!
|
2024-10-09 15:41:10 -06:00
|
|
|
if sort_name == HeadingOptions::Name && sort_direction {
|
2024-07-17 02:29:06 -04:00
|
|
|
// A default/unsorted tab's view is consistent with how the
|
|
|
|
|
// Items are laid out internally (items_opt), so Items can be
|
|
|
|
|
// linearly selected
|
|
|
|
|
if let Some(ref mut items) = self.items_opt {
|
|
|
|
|
for item in items.iter_mut().skip(min).take(max - min + 1) {
|
|
|
|
|
item.selected = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// A sorted tab's items can't be linearly selected
|
|
|
|
|
// Let's say we have:
|
|
|
|
|
// index | file
|
|
|
|
|
// 0 | file0
|
|
|
|
|
// 1 | file1
|
|
|
|
|
// 2 | file2
|
|
|
|
|
// This is both the default sort and internal ordering
|
|
|
|
|
// When sorted it may be displayed as:
|
|
|
|
|
// 1 | file1
|
|
|
|
|
// 0 | file0
|
|
|
|
|
// 2 | file2
|
|
|
|
|
// However, the internal ordering is still the same thus
|
|
|
|
|
// linearly selecting items doesn't work. Shift selecting
|
|
|
|
|
// file0 and file2 would select indices 0 to 2 when it should
|
|
|
|
|
// select indices 0 AND 2 from items_opt
|
|
|
|
|
let indices: Vec<_> = self
|
|
|
|
|
.column_sort()
|
|
|
|
|
.map(|sorted| sorted.into_iter().map(|(i, _)| i).collect())
|
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
|
let len = self
|
|
|
|
|
.items_opt
|
|
|
|
|
.as_deref()
|
|
|
|
|
.map(|items| items.len())
|
|
|
|
|
.unwrap_or_default();
|
|
|
|
|
(0..len).collect()
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Find the true indices for the min and max element w.r.t.
|
|
|
|
|
// a sorted tab.
|
|
|
|
|
let min = indices
|
|
|
|
|
.iter()
|
|
|
|
|
.position(|&offset| offset == min)
|
|
|
|
|
.unwrap_or_default();
|
|
|
|
|
// We can't skip `min_real` elements here because the index of
|
|
|
|
|
// `max` may actually be before `min` in a sorted tab
|
|
|
|
|
let max = indices
|
|
|
|
|
.iter()
|
|
|
|
|
.position(|&offset| offset == max)
|
|
|
|
|
.unwrap_or_else(|| indices.len());
|
|
|
|
|
let min_real = min.min(max);
|
|
|
|
|
let max_real = max.max(min);
|
|
|
|
|
|
|
|
|
|
if let Some(ref mut items) = self.items_opt {
|
|
|
|
|
for index in indices
|
|
|
|
|
.into_iter()
|
|
|
|
|
.skip(min_real)
|
|
|
|
|
.take(max_real - min_real + 1)
|
|
|
|
|
{
|
|
|
|
|
if let Some(item) = items.get_mut(index) {
|
|
|
|
|
item.selected = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-26 14:31:50 -07:00
|
|
|
}
|
|
|
|
|
}
|
2024-05-27 10:23:48 -06:00
|
|
|
}
|
|
|
|
|
self.clicked = click_i_opt;
|
|
|
|
|
self.select_focus = click_i_opt;
|
|
|
|
|
self.selected_clicked = true;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
let dont_unset = mod_ctrl
|
|
|
|
|
|| self.column_sort().is_some_and(|l| {
|
|
|
|
|
l.iter()
|
|
|
|
|
.any(|(e_i, e)| Some(e_i) == click_i_opt.as_ref() && e.selected)
|
|
|
|
|
});
|
|
|
|
|
if let Some(ref mut items) = self.items_opt {
|
|
|
|
|
for (i, item) in items.iter_mut().enumerate() {
|
|
|
|
|
if Some(i) == click_i_opt {
|
|
|
|
|
// Filter out selection if it does not match dialog kind
|
2024-08-20 13:26:10 -06:00
|
|
|
if let Mode::Dialog(dialog) = &self.mode {
|
2024-05-27 10:23:48 -06:00
|
|
|
let item_is_dir = item.metadata.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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !item.selected {
|
|
|
|
|
self.clicked = click_i_opt;
|
|
|
|
|
item.selected = true;
|
|
|
|
|
self.select_range = Some((i, i));
|
|
|
|
|
}
|
2024-09-20 14:13:04 -06:00
|
|
|
self.select_focus = click_i_opt;
|
2024-05-27 10:23:48 -06:00
|
|
|
self.selected_clicked = true;
|
|
|
|
|
} else if !dont_unset && item.selected {
|
2024-05-26 19:50:41 -06:00
|
|
|
self.clicked = click_i_opt;
|
2024-05-27 10:23:48 -06:00
|
|
|
item.selected = false;
|
2024-05-26 19:50:41 -06:00
|
|
|
}
|
2024-01-03 15:27:32 -07:00
|
|
|
}
|
|
|
|
|
}
|
2024-02-29 21:13:03 -07:00
|
|
|
}
|
2024-01-03 15:27:32 -07:00
|
|
|
}
|
2024-02-11 00:24:35 -05:00
|
|
|
Message::Config(config) => {
|
2024-10-02 14:53:24 -06:00
|
|
|
// View is preserved for existing tabs
|
|
|
|
|
let view = self.config.view;
|
2024-12-08 08:57:49 +01:00
|
|
|
let show_hidden = self.config.show_hidden;
|
2024-02-11 00:24:35 -05:00
|
|
|
self.config = config;
|
2024-10-02 14:53:24 -06:00
|
|
|
self.config.view = view;
|
2024-12-08 08:57:49 +01:00
|
|
|
self.config.show_hidden = show_hidden;
|
2024-02-11 00:24:35 -05:00
|
|
|
}
|
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) => {
|
2024-10-13 12:01:55 -05:00
|
|
|
self.edit_location = None;
|
2024-06-30 14:57:37 -06:00
|
|
|
if point_opt.is_none() || !mod_shift {
|
|
|
|
|
self.context_menu = point_opt;
|
2024-09-20 09:09:39 -06:00
|
|
|
//TODO: hack for clearing selecting when right clicking empty space
|
|
|
|
|
if self.context_menu.is_some() && self.last_right_click.take().is_none() {
|
|
|
|
|
if let Some(ref mut items) = self.items_opt {
|
|
|
|
|
for item in items.iter_mut() {
|
|
|
|
|
item.selected = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-30 14:57:37 -06:00
|
|
|
}
|
2024-02-26 12:05:29 -07:00
|
|
|
}
|
2024-06-30 15:45:01 -06:00
|
|
|
Message::LocationContextMenuPoint(point_opt) => {
|
|
|
|
|
self.location_context_menu_point = point_opt;
|
|
|
|
|
}
|
|
|
|
|
Message::LocationContextMenuIndex(index_opt) => {
|
|
|
|
|
self.location_context_menu_index = index_opt;
|
2024-06-30 10:05:58 -06:00
|
|
|
}
|
|
|
|
|
Message::LocationMenuAction(action) => {
|
2024-06-30 15:45:01 -06:00
|
|
|
self.location_context_menu_index = None;
|
2024-06-30 10:05:58 -06:00
|
|
|
let path_for_index = |ancestor_index| {
|
2024-10-09 15:41:10 -06:00
|
|
|
self.location
|
|
|
|
|
.path_opt()
|
|
|
|
|
.and_then(|path| path.ancestors().nth(ancestor_index))
|
|
|
|
|
.map(|path| path.to_path_buf())
|
2024-06-30 10:05:58 -06:00
|
|
|
};
|
|
|
|
|
match action {
|
|
|
|
|
LocationMenuAction::OpenInNewTab(ancestor_index) => {
|
|
|
|
|
if let Some(path) = path_for_index(ancestor_index) {
|
|
|
|
|
commands.push(Command::OpenInNewTab(path));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
LocationMenuAction::OpenInNewWindow(ancestor_index) => {
|
|
|
|
|
if let Some(path) = path_for_index(ancestor_index) {
|
|
|
|
|
commands.push(Command::OpenInNewWindow(path));
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-20 14:13:04 -06:00
|
|
|
LocationMenuAction::Preview(ancestor_index) => {
|
|
|
|
|
if let Some(path) = path_for_index(ancestor_index) {
|
|
|
|
|
//TODO: blocking code, run in command
|
|
|
|
|
match item_from_path(&path, IconSizes::default()) {
|
|
|
|
|
Ok(item) => {
|
2024-10-02 15:26:02 -06:00
|
|
|
commands.push(Command::Preview(PreviewKind::Custom(
|
|
|
|
|
PreviewItem(item),
|
|
|
|
|
)));
|
2024-09-20 14:13:04 -06:00
|
|
|
}
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::warn!("failed to get item from path {:?}: {}", path, err);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-30 13:33:44 -06:00
|
|
|
}
|
2024-10-14 10:09:57 -06:00
|
|
|
LocationMenuAction::AddToSidebar(ancestor_index) => {
|
|
|
|
|
if let Some(path) = path_for_index(ancestor_index) {
|
|
|
|
|
commands.push(Command::AddToSidebar(path));
|
|
|
|
|
} else {
|
|
|
|
|
log::warn!(
|
|
|
|
|
"no ancestor {ancestor_index} for location {:?}",
|
|
|
|
|
self.location
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-30 10:05:58 -06:00
|
|
|
}
|
|
|
|
|
}
|
2024-02-29 11:25:46 -07:00
|
|
|
Message::Drag(rect_opt) => match rect_opt {
|
|
|
|
|
Some(rect) => {
|
2024-04-10 11:41:25 -04:00
|
|
|
self.context_menu = None;
|
2024-06-30 15:45:01 -06:00
|
|
|
self.location_context_menu_index = None;
|
2024-05-26 22:03:43 -06:00
|
|
|
self.select_rect(rect, mod_ctrl, mod_shift);
|
2024-02-29 21:13:03 -07:00
|
|
|
if self.select_focus.take().is_some() {
|
|
|
|
|
// Unfocus currently focused button
|
2024-10-21 13:51:10 -06:00
|
|
|
commands.push(Command::Iced(
|
|
|
|
|
widget::button::focus(widget::Id::unique()).into(),
|
|
|
|
|
));
|
2024-02-29 21:13:03 -07:00
|
|
|
}
|
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() {
|
2024-10-21 13:51:10 -06:00
|
|
|
commands.push(Command::Iced(
|
|
|
|
|
widget::text_input::focus(self.edit_location_id.clone()).into(),
|
|
|
|
|
));
|
2024-02-28 15:19:07 -07:00
|
|
|
}
|
2024-01-29 11:58:36 -07:00
|
|
|
self.edit_location = edit_location;
|
|
|
|
|
}
|
2024-10-13 12:01:55 -05:00
|
|
|
Message::EditLocationEnable => {
|
2024-10-21 13:51:10 -06:00
|
|
|
commands.push(Command::Iced(
|
|
|
|
|
widget::text_input::focus(self.edit_location_id.clone()).into(),
|
|
|
|
|
));
|
2024-10-13 12:01:55 -05:00
|
|
|
self.edit_location = Some(self.location.clone());
|
2024-10-03 13:01:59 -06:00
|
|
|
}
|
2024-06-30 10:05:58 -06:00
|
|
|
Message::OpenInNewTab(path) => {
|
|
|
|
|
commands.push(Command::OpenInNewTab(path));
|
|
|
|
|
}
|
2024-05-09 13:24:06 -06:00
|
|
|
Message::EmptyTrash => {
|
|
|
|
|
commands.push(Command::EmptyTrash);
|
|
|
|
|
}
|
2024-11-11 11:38:07 -07:00
|
|
|
#[cfg(feature = "desktop")]
|
2024-10-29 03:18:05 -04:00
|
|
|
Message::ExecEntryAction(path, action) => {
|
|
|
|
|
let lang_id = crate::localize::LANGUAGE_LOADER.current_language();
|
|
|
|
|
let language = lang_id.language.as_str();
|
|
|
|
|
match path.map_or_else(
|
|
|
|
|
|| {
|
|
|
|
|
let items = self.items_opt.as_deref()?;
|
|
|
|
|
items.iter().find(|item| item.selected).and_then(|item| {
|
|
|
|
|
let location = item.location_opt.as_ref()?;
|
|
|
|
|
let path = location.path_opt()?;
|
|
|
|
|
cosmic::desktop::load_desktop_file(Some(language), path)
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
|path| cosmic::desktop::load_desktop_file(Some(language), path),
|
|
|
|
|
) {
|
|
|
|
|
Some(entry) => commands.push(Command::ExecEntryAction(entry, action)),
|
|
|
|
|
None => log::warn!("Invalid desktop entry path passed to ExecEntryAction"),
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-23 15:56:32 -06:00
|
|
|
Message::Gallery(gallery) => {
|
|
|
|
|
self.gallery = gallery;
|
|
|
|
|
}
|
2024-09-24 08:39:13 -06:00
|
|
|
Message::GalleryPrevious | Message::GalleryNext => {
|
|
|
|
|
let mut pos_opt = None;
|
|
|
|
|
if let Some(mut indices) = self.column_sort() {
|
|
|
|
|
if matches!(message, Message::GalleryPrevious) {
|
|
|
|
|
indices.reverse();
|
|
|
|
|
}
|
|
|
|
|
let mut found = false;
|
|
|
|
|
for (index, item) in indices {
|
|
|
|
|
if self.select_focus == None {
|
|
|
|
|
found = true;
|
|
|
|
|
}
|
|
|
|
|
if self.select_focus == Some(index) {
|
|
|
|
|
found = true;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if found {
|
2024-10-10 10:05:48 -06:00
|
|
|
if item.can_gallery() {
|
2024-09-24 08:39:13 -06:00
|
|
|
pos_opt = item.pos_opt.get();
|
|
|
|
|
if pos_opt.is_some() {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if let Some((row, col)) = pos_opt {
|
|
|
|
|
// Should mod_shift be available?
|
|
|
|
|
self.select_position(row, col, mod_shift);
|
|
|
|
|
}
|
|
|
|
|
if let Some(offset) = self.select_focus_scroll() {
|
2024-10-21 13:51:10 -06:00
|
|
|
commands.push(Command::Iced(
|
|
|
|
|
scrollable::scroll_to(self.scrollable_id.clone(), offset).into(),
|
|
|
|
|
));
|
2024-09-24 08:39:13 -06:00
|
|
|
}
|
|
|
|
|
if let Some(id) = self.select_focus_id() {
|
2024-10-21 13:51:10 -06:00
|
|
|
commands.push(Command::Iced(widget::button::focus(id).into()));
|
2024-09-24 08:39:13 -06:00
|
|
|
}
|
|
|
|
|
}
|
2024-10-02 13:23:41 -06:00
|
|
|
Message::GalleryToggle => {
|
2024-10-04 00:18:27 +05:30
|
|
|
if let Some(indices) = self.column_sort() {
|
|
|
|
|
for (_, item) in indices.iter() {
|
2024-10-10 10:05:48 -06:00
|
|
|
if item.selected && item.can_gallery() {
|
2024-10-04 00:18:27 +05:30
|
|
|
self.gallery = !self.gallery;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-02 13:23:41 -06:00
|
|
|
}
|
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-02-29 13:24:57 -07:00
|
|
|
Message::ItemDown => {
|
2024-10-10 10:11:28 -06:00
|
|
|
if self.gallery {
|
|
|
|
|
for command in self.update(Message::GalleryNext, modifiers) {
|
|
|
|
|
commands.push(command);
|
2024-05-27 14:10:14 -06:00
|
|
|
}
|
2024-10-10 10:11:28 -06:00
|
|
|
} else {
|
|
|
|
|
if let Some((row, col)) =
|
|
|
|
|
self.select_focus_pos_opt().or(self.select_last_pos_opt())
|
|
|
|
|
{
|
|
|
|
|
if self.select_focus.is_none() {
|
|
|
|
|
// Select last item in current selection to focus it.
|
|
|
|
|
self.select_position(row, col, mod_shift);
|
|
|
|
|
}
|
2024-05-27 14:10:14 -06:00
|
|
|
|
2024-10-10 10:11:28 -06:00
|
|
|
//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);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Select first item
|
|
|
|
|
//TODO: select first in scroll
|
|
|
|
|
self.select_position(0, 0, mod_shift);
|
|
|
|
|
}
|
|
|
|
|
if let Some(offset) = self.select_focus_scroll() {
|
2024-10-21 13:51:10 -06:00
|
|
|
commands.push(Command::Iced(
|
|
|
|
|
scrollable::scroll_to(self.scrollable_id.clone(), offset).into(),
|
|
|
|
|
));
|
2024-10-10 10:11:28 -06:00
|
|
|
}
|
|
|
|
|
if let Some(id) = self.select_focus_id() {
|
2024-10-21 13:51:10 -06:00
|
|
|
commands.push(Command::Iced(widget::button::focus(id).into()));
|
2024-02-28 16:16:59 -07:00
|
|
|
}
|
2024-02-29 15:38:03 -07:00
|
|
|
}
|
2024-02-28 16:16:59 -07:00
|
|
|
}
|
2024-02-29 13:24:57 -07:00
|
|
|
Message::ItemLeft => {
|
2024-10-10 10:11:28 -06:00
|
|
|
if self.gallery {
|
|
|
|
|
for command in self.update(Message::GalleryPrevious, modifiers) {
|
|
|
|
|
commands.push(command);
|
2024-05-27 14:10:14 -06:00
|
|
|
}
|
2024-10-10 10:11:28 -06:00
|
|
|
} else {
|
|
|
|
|
if let Some((row, col)) =
|
|
|
|
|
self.select_focus_pos_opt().or(self.select_first_pos_opt())
|
2024-02-29 13:24:57 -07:00
|
|
|
{
|
2024-10-10 10:11:28 -06:00
|
|
|
if self.select_focus.is_none() {
|
|
|
|
|
// Select first item in current selection to focus it.
|
|
|
|
|
self.select_position(row, col, mod_shift);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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-29 13:24:57 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-10 10:11:28 -06: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
|
|
|
}
|
|
|
|
|
}
|
2024-10-10 10:11:28 -06:00
|
|
|
} else {
|
|
|
|
|
// Select first item
|
|
|
|
|
//TODO: select first in scroll
|
|
|
|
|
self.select_position(0, 0, mod_shift);
|
|
|
|
|
}
|
|
|
|
|
if let Some(offset) = self.select_focus_scroll() {
|
2024-10-21 13:51:10 -06:00
|
|
|
commands.push(Command::Iced(
|
|
|
|
|
scrollable::scroll_to(self.scrollable_id.clone(), offset).into(),
|
|
|
|
|
));
|
2024-10-10 10:11:28 -06:00
|
|
|
}
|
|
|
|
|
if let Some(id) = self.select_focus_id() {
|
2024-10-21 13:51:10 -06:00
|
|
|
commands.push(Command::Iced(widget::button::focus(id).into()));
|
2024-02-28 16:16:59 -07:00
|
|
|
}
|
2024-02-29 15:38:03 -07:00
|
|
|
}
|
2024-02-29 13:24:57 -07:00
|
|
|
}
|
|
|
|
|
Message::ItemRight => {
|
2024-10-10 10:11:28 -06:00
|
|
|
if self.gallery {
|
|
|
|
|
for command in self.update(Message::GalleryNext, modifiers) {
|
|
|
|
|
commands.push(command);
|
2024-05-27 14:10:14 -06:00
|
|
|
}
|
2024-10-10 10:11:28 -06:00
|
|
|
} else {
|
|
|
|
|
if let Some((row, col)) =
|
|
|
|
|
self.select_focus_pos_opt().or(self.select_last_pos_opt())
|
|
|
|
|
{
|
|
|
|
|
if self.select_focus.is_none() {
|
|
|
|
|
// Select last item in current selection to focus it.
|
2024-02-29 13:24:57 -07:00
|
|
|
self.select_position(row, col, mod_shift);
|
2024-02-28 16:16:59 -07:00
|
|
|
}
|
2024-10-10 10:11:28 -06:00
|
|
|
// 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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Select first item
|
|
|
|
|
//TODO: select first in scroll
|
|
|
|
|
self.select_position(0, 0, mod_shift);
|
|
|
|
|
}
|
|
|
|
|
if let Some(offset) = self.select_focus_scroll() {
|
2024-10-21 13:51:10 -06:00
|
|
|
commands.push(Command::Iced(
|
|
|
|
|
scrollable::scroll_to(self.scrollable_id.clone(), offset).into(),
|
|
|
|
|
));
|
2024-10-10 10:11:28 -06:00
|
|
|
}
|
|
|
|
|
if let Some(id) = self.select_focus_id() {
|
2024-10-21 13:51:10 -06:00
|
|
|
commands.push(Command::Iced(widget::button::focus(id).into()));
|
2024-02-28 16:16:59 -07:00
|
|
|
}
|
2024-02-29 15:38:03 -07:00
|
|
|
}
|
2024-02-29 13:24:57 -07:00
|
|
|
}
|
|
|
|
|
Message::ItemUp => {
|
2024-10-10 10:11:28 -06:00
|
|
|
if self.gallery {
|
|
|
|
|
for command in self.update(Message::GalleryPrevious, modifiers) {
|
|
|
|
|
commands.push(command);
|
2024-05-27 14:10:14 -06:00
|
|
|
}
|
2024-10-10 10:11:28 -06:00
|
|
|
} else {
|
|
|
|
|
if let Some((row, col)) =
|
|
|
|
|
self.select_focus_pos_opt().or(self.select_first_pos_opt())
|
2024-02-29 13:24:57 -07:00
|
|
|
{
|
2024-10-10 10:11:28 -06:00
|
|
|
if self.select_focus.is_none() {
|
|
|
|
|
// Select first item in current selection to focus it.
|
|
|
|
|
self.select_position(row, col, mod_shift);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//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
|
|
|
|
|
//TODO: select first in scroll
|
|
|
|
|
self.select_position(0, 0, mod_shift);
|
|
|
|
|
}
|
|
|
|
|
if let Some(offset) = self.select_focus_scroll() {
|
2024-10-21 13:51:10 -06:00
|
|
|
commands.push(Command::Iced(
|
|
|
|
|
scrollable::scroll_to(self.scrollable_id.clone(), offset).into(),
|
|
|
|
|
));
|
2024-10-10 10:11:28 -06:00
|
|
|
}
|
|
|
|
|
if let Some(id) = self.select_focus_id() {
|
2024-10-21 13:51:10 -06:00
|
|
|
commands.push(Command::Iced(widget::button::focus(id).into()));
|
2024-02-29 13:24:57 -07:00
|
|
|
}
|
2024-02-29 15:38:03 -07:00
|
|
|
}
|
2024-02-28 16:16:59 -07:00
|
|
|
}
|
2024-01-09 15:34:48 -07:00
|
|
|
Message::Location(location) => {
|
2024-05-09 13:55:57 -06:00
|
|
|
// Workaround to support favorited files
|
|
|
|
|
match &location {
|
|
|
|
|
Location::Path(path) => {
|
|
|
|
|
if path.is_dir() {
|
|
|
|
|
cd = Some(location);
|
|
|
|
|
} else {
|
|
|
|
|
commands.push(Command::OpenFile(path.clone()));
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-12 15:54:54 -06:00
|
|
|
_ => {
|
2024-05-31 15:19:47 -06:00
|
|
|
cd = Some(location);
|
|
|
|
|
}
|
2024-05-09 13:55:57 -06:00
|
|
|
}
|
2024-01-03 17:04:08 -07:00
|
|
|
}
|
2024-02-06 22:58:29 -05: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 {
|
2024-02-07 21:11:57 -05:00
|
|
|
if let Some(parent) = path.parent() {
|
|
|
|
|
cd = Some(Location::Path(parent.to_owned()));
|
2024-02-06 22:58:29 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-23 11:59:05 -06:00
|
|
|
Message::Open(path_opt) => {
|
|
|
|
|
match path_opt {
|
|
|
|
|
Some(path) => {
|
|
|
|
|
if path.is_dir() {
|
|
|
|
|
cd = Some(Location::Path(path));
|
|
|
|
|
} else {
|
|
|
|
|
commands.push(Command::OpenFile(path));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
None => {
|
|
|
|
|
if let Some(ref mut items) = self.items_opt {
|
|
|
|
|
for item in items.iter() {
|
|
|
|
|
if item.selected {
|
|
|
|
|
if let Some(location) = &item.location_opt {
|
|
|
|
|
if item.metadata.is_dir() {
|
|
|
|
|
//TODO: allow opening multiple tabs?
|
|
|
|
|
cd = Some(location.clone());
|
|
|
|
|
} else {
|
2024-10-04 16:28:30 -06:00
|
|
|
if let Some(path) = location.path_opt() {
|
|
|
|
|
commands
|
|
|
|
|
.push(Command::OpenFile(path.to_path_buf()));
|
2024-09-23 11:59:05 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
//TODO: open properties?
|
2024-09-13 15:13:37 -06:00
|
|
|
}
|
2024-02-28 15:46:23 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-30 14:57:37 -06:00
|
|
|
Message::RightClick(click_i_opt) => {
|
|
|
|
|
self.update(Message::Click(click_i_opt), modifiers);
|
2024-01-10 10:26:30 -07:00
|
|
|
if let Some(ref mut items) = self.items_opt {
|
2024-06-30 14:57:37 -06:00
|
|
|
if !click_i_opt.map_or(false, |click_i| {
|
|
|
|
|
items.get(click_i).map_or(false, |x| x.selected)
|
|
|
|
|
}) {
|
2024-01-10 10:26:30 -07:00
|
|
|
// If item not selected, clear selection on other items
|
|
|
|
|
for (i, item) in items.iter_mut().enumerate() {
|
2024-06-30 14:57:37 -06:00
|
|
|
item.selected = Some(i) == click_i_opt;
|
2024-01-10 10:26:30 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-20 09:09:39 -06:00
|
|
|
//TODO: hack for clearing selecting when right clicking empty space
|
|
|
|
|
self.last_right_click = click_i_opt;
|
2024-01-10 10:26:30 -07:00
|
|
|
}
|
2024-06-29 19:35:27 -06:00
|
|
|
Message::MiddleClick(click_i) => {
|
2024-06-30 14:57:37 -06:00
|
|
|
self.update(Message::Click(Some(click_i)), modifiers);
|
|
|
|
|
if !mod_ctrl && !mod_shift {
|
2024-06-29 19:35:27 -06:00
|
|
|
if let Some(ref mut items) = self.items_opt {
|
|
|
|
|
for (i, item) in items.iter_mut().enumerate() {
|
|
|
|
|
item.selected = i == click_i;
|
|
|
|
|
}
|
2024-06-30 14:57:37 -06:00
|
|
|
self.select_range = Some((click_i, click_i));
|
2024-06-29 19:35:27 -06:00
|
|
|
}
|
|
|
|
|
if let Some(clicked_item) =
|
|
|
|
|
self.items_opt.as_ref().and_then(|items| items.get(click_i))
|
|
|
|
|
{
|
2024-10-04 16:28:30 -06:00
|
|
|
if let Some(path) = clicked_item.path_opt() {
|
2024-06-29 19:35:27 -06:00
|
|
|
if clicked_item.metadata.is_dir() {
|
|
|
|
|
//cd = Some(Location::Path(path.clone()));
|
|
|
|
|
commands.push(Command::OpenInNewTab(path.clone()))
|
|
|
|
|
} else {
|
|
|
|
|
commands.push(Command::OpenFile(path.clone()));
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
log::warn!("no path for item {:?}", clicked_item);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
log::warn!("no item for click index {:?}", click_i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-16 10:22:25 +11:00
|
|
|
Message::HighlightDeactivate(i) => {
|
|
|
|
|
if let Some(item) = self.items_opt.as_mut().and_then(|f| f.get_mut(i)) {
|
|
|
|
|
item.highlighted = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Message::HighlightActivate(i) => {
|
|
|
|
|
if let Some(item) = self.items_opt.as_mut().and_then(|f| f.get_mut(i)) {
|
|
|
|
|
item.highlighted = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-09 18:55:09 -06:00
|
|
|
|
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-09-20 14:34:44 -06:00
|
|
|
Message::ScrollToFocus => {
|
|
|
|
|
if let Some(offset) = self.select_focus_scroll() {
|
2024-10-21 13:51:10 -06:00
|
|
|
commands.push(Command::Iced(
|
|
|
|
|
scrollable::scroll_to(self.scrollable_id.clone(), offset).into(),
|
|
|
|
|
));
|
2024-09-20 14:34:44 -06:00
|
|
|
}
|
|
|
|
|
}
|
2024-10-09 18:55:09 -06:00
|
|
|
Message::SearchContext(location, context) => {
|
2024-10-09 15:41:10 -06:00
|
|
|
if location == self.location {
|
2024-10-09 18:55:09 -06:00
|
|
|
self.search_context = context.0;
|
2024-10-09 15:41:10 -06:00
|
|
|
} else {
|
|
|
|
|
log::warn!(
|
2024-10-09 18:55:09 -06:00
|
|
|
"search context provided for {:?} instead of {:?}",
|
2024-10-09 15:41:10 -06:00
|
|
|
location,
|
|
|
|
|
self.location
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-10-09 18:55:09 -06:00
|
|
|
}
|
|
|
|
|
Message::SearchReady(finished) => {
|
|
|
|
|
if let Some(context) = &mut self.search_context {
|
|
|
|
|
if let Some(items) = &mut self.items_opt {
|
|
|
|
|
if finished || context.ready.swap(false, atomic::Ordering::SeqCst) {
|
|
|
|
|
let duration = Instant::now();
|
|
|
|
|
while let Some((path, name, metadata)) =
|
|
|
|
|
context.results_rx.blocking_recv()
|
|
|
|
|
{
|
|
|
|
|
//TODO: combine this with column_sort logic, they must match!
|
|
|
|
|
let item_modified = metadata.modified().ok();
|
|
|
|
|
let index = match items.binary_search_by(|other| {
|
2024-10-09 20:18:43 -06:00
|
|
|
item_modified.cmp(&other.metadata.modified())
|
2024-10-09 18:55:09 -06:00
|
|
|
}) {
|
|
|
|
|
Ok(index) => index,
|
|
|
|
|
Err(index) => index,
|
|
|
|
|
};
|
|
|
|
|
if index < MAX_SEARCH_RESULTS {
|
|
|
|
|
//TODO: use correct IconSizes
|
|
|
|
|
items.insert(
|
|
|
|
|
index,
|
|
|
|
|
item_from_entry(path, name, metadata, IconSizes::default()),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
// Ensure that updates make it to the GUI in a timely manner
|
|
|
|
|
if !finished && duration.elapsed() >= MAX_SEARCH_LATENCY {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if items.len() >= MAX_SEARCH_RESULTS {
|
|
|
|
|
items.truncate(MAX_SEARCH_RESULTS);
|
2024-10-09 20:18:43 -06:00
|
|
|
if let Some(last_modified) =
|
|
|
|
|
items.last().and_then(|item| item.metadata.modified())
|
|
|
|
|
{
|
|
|
|
|
*context.last_modified_opt.write().unwrap() = Some(last_modified);
|
|
|
|
|
}
|
2024-10-09 18:55:09 -06:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
log::warn!("search ready but items array is empty");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if finished {
|
|
|
|
|
self.search_context = None;
|
2024-10-09 15:41:10 -06:00
|
|
|
}
|
|
|
|
|
}
|
2024-02-29 21:13:03 -07:00
|
|
|
Message::SelectAll => {
|
|
|
|
|
self.select_all();
|
|
|
|
|
if self.select_focus.take().is_some() {
|
|
|
|
|
// Unfocus currently focused button
|
2024-10-21 13:51:10 -06:00
|
|
|
commands.push(Command::Iced(
|
|
|
|
|
widget::button::focus(widget::Id::unique()).into(),
|
|
|
|
|
));
|
2024-02-29 21:13:03 -07:00
|
|
|
}
|
|
|
|
|
}
|
2024-09-11 13:29:00 -06:00
|
|
|
Message::SetSort(heading_option, dir) => {
|
2024-10-09 15:41:10 -06:00
|
|
|
if !matches!(self.location, Location::Search(..)) {
|
|
|
|
|
self.sort_name = heading_option;
|
|
|
|
|
self.sort_direction = dir;
|
|
|
|
|
}
|
2024-09-11 13:29:00 -06:00
|
|
|
}
|
2024-03-04 13:41:18 -07:00
|
|
|
Message::Thumbnail(path, thumbnail) => {
|
2024-02-22 16:17:39 -07:00
|
|
|
if let Some(ref mut items) = self.items_opt {
|
2024-09-13 09:35:37 -06:00
|
|
|
let location = Location::Path(path);
|
2024-02-22 16:17:39 -07:00
|
|
|
for item in items.iter_mut() {
|
2024-09-13 09:35:37 -06:00
|
|
|
if item.location_opt.as_ref() == Some(&location) {
|
2024-10-10 09:07:02 -06:00
|
|
|
let handle_opt = match &thumbnail {
|
|
|
|
|
ItemThumbnail::NotImage => None,
|
|
|
|
|
ItemThumbnail::Image(handle, _) => Some(widget::icon::Handle {
|
|
|
|
|
symbolic: false,
|
|
|
|
|
data: widget::icon::Data::Image(handle.clone()),
|
|
|
|
|
}),
|
|
|
|
|
ItemThumbnail::Svg(handle) => Some(widget::icon::Handle {
|
|
|
|
|
symbolic: false,
|
|
|
|
|
data: widget::icon::Data::Svg(handle.clone()),
|
|
|
|
|
}),
|
2024-10-10 10:05:48 -06:00
|
|
|
//TODO: text thumbnails?
|
|
|
|
|
ItemThumbnail::Text(_text) => None,
|
2024-10-10 09:07:02 -06:00
|
|
|
};
|
|
|
|
|
if let Some(handle) = handle_opt {
|
|
|
|
|
item.icon_handle_grid = handle.clone();
|
|
|
|
|
item.icon_handle_list = handle.clone();
|
|
|
|
|
item.icon_handle_list_condensed = handle;
|
2024-02-22 16:17:39 -07:00
|
|
|
}
|
2024-03-04 13:41:18 -07:00
|
|
|
item.thumbnail_opt = Some(thumbnail);
|
2024-02-22 16:17:39 -07:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-09 21:02:12 -06:00
|
|
|
Message::ToggleShowHidden => {
|
|
|
|
|
self.config.show_hidden = !self.config.show_hidden;
|
|
|
|
|
if let Location::Search(path, term, ..) = &self.location {
|
|
|
|
|
cd = Some(Location::Search(
|
|
|
|
|
path.clone(),
|
|
|
|
|
term.clone(),
|
|
|
|
|
self.config.show_hidden,
|
|
|
|
|
Instant::now(),
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-05 15:17:38 -07:00
|
|
|
Message::View(view) => {
|
2024-05-29 23:33:12 +02:00
|
|
|
self.config.view = view;
|
2024-01-05 15:17:38 -07:00
|
|
|
}
|
2024-02-28 05:18:40 +01:00
|
|
|
Message::ToggleSort(heading_option) => {
|
2024-10-09 15:41:10 -06:00
|
|
|
if !matches!(self.location, Location::Search(..)) {
|
|
|
|
|
let heading_sort = if self.sort_name == heading_option {
|
|
|
|
|
!self.sort_direction
|
|
|
|
|
} else {
|
|
|
|
|
// Default modified to descending, and others to ascending.
|
|
|
|
|
heading_option != HeadingOptions::Modified
|
|
|
|
|
};
|
|
|
|
|
self.sort_direction = heading_sort;
|
|
|
|
|
self.sort_name = heading_option;
|
|
|
|
|
}
|
2024-02-28 05:18:40 +01:00
|
|
|
}
|
2024-04-10 11:41:25 -04:00
|
|
|
Message::Drop(Some((to, mut from))) => {
|
|
|
|
|
self.dnd_hovered = None;
|
|
|
|
|
match to {
|
|
|
|
|
Location::Path(to) => {
|
|
|
|
|
if let Ok(entries) = fs::read_dir(&to) {
|
|
|
|
|
for i in entries.into_iter().filter_map(|e| e.ok()) {
|
|
|
|
|
let i = i.path();
|
|
|
|
|
from.paths.retain(|p| &i != p);
|
|
|
|
|
if from.paths.is_empty() {
|
|
|
|
|
log::info!("All dropped files already in target directory.");
|
|
|
|
|
return commands;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
commands.push(Command::DropFiles(to, from))
|
|
|
|
|
}
|
2024-04-10 13:56:43 -04:00
|
|
|
Location::Trash if matches!(from.kind, ClipboardKind::Cut) => {
|
|
|
|
|
commands.push(Command::MoveToTrash(from.paths))
|
|
|
|
|
}
|
2024-09-13 15:13:37 -06:00
|
|
|
_ => {
|
|
|
|
|
log::warn!("{:?} to {:?} is not supported.", from.kind, to);
|
2024-09-12 15:54:54 -06:00
|
|
|
}
|
2024-04-10 11:41:25 -04:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
Message::Drop(None) => {
|
|
|
|
|
self.dnd_hovered = None;
|
|
|
|
|
}
|
|
|
|
|
Message::DndHover(loc) => {
|
2024-09-20 14:13:04 -06:00
|
|
|
if self
|
|
|
|
|
.dnd_hovered
|
|
|
|
|
.as_ref()
|
|
|
|
|
.is_some_and(|(l, i)| *l == loc && i.elapsed() > HOVER_DURATION)
|
|
|
|
|
{
|
2024-04-10 11:41:25 -04:00
|
|
|
cd = Some(loc);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Message::DndEnter(loc) => {
|
|
|
|
|
self.dnd_hovered = Some((loc.clone(), Instant::now()));
|
|
|
|
|
if loc != self.location {
|
2024-10-21 13:51:10 -06:00
|
|
|
commands.push(Command::Iced(
|
|
|
|
|
cosmic::Task::perform(
|
|
|
|
|
async move {
|
|
|
|
|
tokio::time::sleep(HOVER_DURATION).await;
|
|
|
|
|
Message::DndHover(loc)
|
|
|
|
|
},
|
|
|
|
|
|x| x,
|
|
|
|
|
)
|
|
|
|
|
.into(),
|
|
|
|
|
));
|
2024-04-10 11:41:25 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Message::DndLeave(loc) => {
|
|
|
|
|
if Some(&loc) == self.dnd_hovered.as_ref().map(|(l, _)| l) {
|
|
|
|
|
self.dnd_hovered = None;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-23 15:56:32 -06:00
|
|
|
Message::WindowDrag => {
|
|
|
|
|
commands.push(Command::WindowDrag);
|
|
|
|
|
}
|
2024-09-23 16:05:43 -06:00
|
|
|
Message::WindowToggleMaximize => {
|
|
|
|
|
commands.push(Command::WindowToggleMaximize);
|
|
|
|
|
}
|
2024-10-02 15:49:13 -06:00
|
|
|
Message::ZoomIn => {
|
|
|
|
|
commands.push(Command::Action(Action::ZoomIn));
|
|
|
|
|
}
|
|
|
|
|
Message::ZoomOut => {
|
|
|
|
|
commands.push(Command::Action(Action::ZoomOut));
|
|
|
|
|
}
|
2024-11-15 23:24:52 +01:00
|
|
|
Message::DirectorySize(path, dir_size) => {
|
|
|
|
|
let location = Location::Path(path);
|
|
|
|
|
if let Some(ref mut item) = self.parent_item_opt {
|
|
|
|
|
if item.location_opt.as_ref() == Some(&location) {
|
2024-11-15 17:30:25 -07:00
|
|
|
item.dir_size = dir_size.clone();
|
2024-11-15 23:24:52 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if let Some(ref mut items) = self.items_opt {
|
|
|
|
|
for item in items.iter_mut() {
|
|
|
|
|
if item.location_opt.as_ref() == Some(&location) {
|
2024-11-15 17:30:25 -07:00
|
|
|
item.dir_size = dir_size;
|
2024-11-15 23:24:52 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-03 15:27:32 -07:00
|
|
|
}
|
2024-09-20 14:13:04 -06:00
|
|
|
|
2024-09-25 15:32:54 -06:00
|
|
|
// Scroll to top if needed
|
|
|
|
|
if self.scroll_opt.is_none() {
|
|
|
|
|
let offset = AbsoluteOffset { x: 0.0, y: 0.0 };
|
|
|
|
|
self.scroll_opt = Some(offset);
|
2024-10-21 13:51:10 -06:00
|
|
|
commands.push(Command::Iced(
|
|
|
|
|
scrollable::scroll_to(self.scrollable_id.clone(), offset).into(),
|
|
|
|
|
));
|
2024-09-25 15:32:54 -06:00
|
|
|
}
|
|
|
|
|
|
2024-09-20 14:13:04 -06:00
|
|
|
// Change directory if requested
|
2024-02-20 11:58:39 -07:00
|
|
|
if let Some(location) = cd {
|
2024-08-20 13:26:10 -06:00
|
|
|
if matches!(self.mode, Mode::Desktop) {
|
|
|
|
|
match location {
|
|
|
|
|
Location::Path(path) => {
|
|
|
|
|
commands.push(Command::OpenFile(path));
|
|
|
|
|
}
|
2024-10-04 16:28:30 -06:00
|
|
|
Location::Trash => {
|
|
|
|
|
commands.push(Command::OpenTrash);
|
|
|
|
|
}
|
2024-08-20 13:26:10 -06:00
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
} else if location != self.location {
|
2024-10-09 15:41:10 -06:00
|
|
|
if location.path_opt().map_or(true, |path| path.is_dir()) {
|
2024-10-04 16:28:30 -06:00
|
|
|
let prev_path = if let Some(path) = self.location.path_opt() {
|
2024-11-19 20:17:58 -07:00
|
|
|
Some(vec![path.to_path_buf()])
|
2024-08-17 13:16:53 -06:00
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
};
|
2024-05-28 09:28:46 -06:00
|
|
|
self.change_location(&location, history_i_opt);
|
2024-08-17 13:16:53 -06:00
|
|
|
commands.push(Command::ChangeLocation(self.title(), location, prev_path));
|
2024-03-18 14:46:54 -06:00
|
|
|
} else {
|
|
|
|
|
log::warn!("tried to cd to {:?} which is not a directory", location);
|
2024-01-30 10:47:41 -07:00
|
|
|
}
|
2024-01-09 15:42:42 -07:00
|
|
|
}
|
2024-01-03 15:27:32 -07:00
|
|
|
}
|
2024-09-20 14:13:04 -06:00
|
|
|
|
2024-02-26 14:11:45 -07:00
|
|
|
commands
|
2024-01-03 15:27:32 -07:00
|
|
|
}
|
|
|
|
|
|
2024-10-09 18:55:09 -06:00
|
|
|
pub(crate) fn sort_options(&self) -> (HeadingOptions, bool, bool) {
|
2024-10-09 15:41:10 -06:00
|
|
|
match self.location {
|
2024-10-09 18:55:09 -06:00
|
|
|
Location::Search(..) => (HeadingOptions::Modified, false, false),
|
|
|
|
|
_ => (
|
|
|
|
|
self.sort_name,
|
|
|
|
|
self.sort_direction,
|
|
|
|
|
self.config.folders_first,
|
|
|
|
|
),
|
2024-10-09 15:41:10 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-29 18:58:59 -07:00
|
|
|
fn column_sort(&self) -> Option<Vec<(usize, &Item)>> {
|
2024-02-29 03:46:14 +01:00
|
|
|
let check_reverse = |ord: Ordering, sort: bool| {
|
|
|
|
|
if sort {
|
|
|
|
|
ord
|
|
|
|
|
} else {
|
|
|
|
|
ord.reverse()
|
|
|
|
|
}
|
|
|
|
|
};
|
2024-02-29 18:58:59 -07:00
|
|
|
let mut items: Vec<_> = self.items_opt.as_ref()?.iter().enumerate().collect();
|
2024-10-09 18:55:09 -06:00
|
|
|
let (sort_name, sort_direction, folders_first) = self.sort_options();
|
2024-10-09 15:41:10 -06:00
|
|
|
match sort_name {
|
2024-02-29 03:46:14 +01:00
|
|
|
HeadingOptions::Size => {
|
2024-02-29 12:26:45 -07:00
|
|
|
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),
|
|
|
|
|
},
|
2024-09-13 15:13:37 -06:00
|
|
|
ItemMetadata::SimpleDir { entries } => (true, *entries),
|
|
|
|
|
ItemMetadata::SimpleFile { size } => (false, *size),
|
2024-02-29 03:46:14 +01:00
|
|
|
};
|
2024-02-29 18:58:59 -07:00
|
|
|
let (a_is_entry, a_size) = get_size(a.1);
|
|
|
|
|
let (b_is_entry, b_size) = get_size(b.1);
|
2024-02-29 03:46:14 +01:00
|
|
|
|
2024-05-30 13:19:09 -06:00
|
|
|
//TODO: use folders_first?
|
|
|
|
|
match (a_is_entry, b_is_entry) {
|
2024-02-29 03:46:14 +01:00
|
|
|
(true, false) => Ordering::Less,
|
|
|
|
|
(false, true) => Ordering::Greater,
|
2024-10-09 15:41:10 -06:00
|
|
|
_ => check_reverse(a_size.cmp(&b_size), sort_direction),
|
2024-05-30 13:19:09 -06:00
|
|
|
}
|
2024-02-29 03:46:14 +01:00
|
|
|
})
|
|
|
|
|
}
|
2024-02-29 12:26:45 -07:00
|
|
|
HeadingOptions::Name => items.sort_by(|a, b| {
|
2024-10-09 18:55:09 -06:00
|
|
|
if folders_first {
|
2024-05-28 10:40:36 -06:00
|
|
|
match (a.1.metadata.is_dir(), b.1.metadata.is_dir()) {
|
|
|
|
|
(true, false) => Ordering::Less,
|
|
|
|
|
(false, true) => Ordering::Greater,
|
2024-05-30 13:19:09 -06:00
|
|
|
_ => check_reverse(
|
2024-08-20 13:26:10 -06:00
|
|
|
LANGUAGE_SORTER.compare(&a.1.display_name, &b.1.display_name),
|
2024-10-09 15:41:10 -06:00
|
|
|
sort_direction,
|
2024-05-30 13:19:09 -06:00
|
|
|
),
|
2024-05-28 10:40:36 -06:00
|
|
|
}
|
|
|
|
|
} else {
|
2024-08-20 13:26:10 -06:00
|
|
|
check_reverse(
|
|
|
|
|
LANGUAGE_SORTER.compare(&a.1.display_name, &b.1.display_name),
|
2024-10-09 15:41:10 -06:00
|
|
|
sort_direction,
|
2024-08-20 13:26:10 -06:00
|
|
|
)
|
2024-05-30 13:19:09 -06:00
|
|
|
}
|
2024-02-29 03:46:14 +01:00
|
|
|
}),
|
|
|
|
|
HeadingOptions::Modified => {
|
2024-02-29 12:26:45 -07:00
|
|
|
items.sort_by(|a, b| {
|
2024-10-09 20:18:43 -06:00
|
|
|
let a_modified = a.1.metadata.modified();
|
|
|
|
|
let b_modified = b.1.metadata.modified();
|
2024-10-09 18:55:09 -06:00
|
|
|
if folders_first {
|
2024-05-30 13:19:09 -06:00
|
|
|
match (a.1.metadata.is_dir(), b.1.metadata.is_dir()) {
|
|
|
|
|
(true, false) => Ordering::Less,
|
|
|
|
|
(false, true) => Ordering::Greater,
|
2024-10-09 15:41:10 -06:00
|
|
|
_ => check_reverse(a_modified.cmp(&b_modified), sort_direction),
|
2024-05-30 13:19:09 -06:00
|
|
|
}
|
|
|
|
|
} else {
|
2024-10-09 15:41:10 -06:00
|
|
|
check_reverse(a_modified.cmp(&b_modified), sort_direction)
|
2024-05-30 13:19:09 -06:00
|
|
|
}
|
2024-02-29 03:46:14 +01:00
|
|
|
});
|
|
|
|
|
}
|
2024-09-20 02:27:23 -04:00
|
|
|
HeadingOptions::TrashedOn => {
|
|
|
|
|
let time_deleted = |x: &Item| match &x.metadata {
|
|
|
|
|
ItemMetadata::Trash { entry, .. } => Some(entry.time_deleted),
|
|
|
|
|
_ => None,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
items.sort_by(|a, b| {
|
|
|
|
|
let a_time_deleted = time_deleted(a.1);
|
|
|
|
|
let b_time_deleted = time_deleted(b.1);
|
2024-10-09 18:55:09 -06:00
|
|
|
if folders_first {
|
2024-09-20 02:27:23 -04:00
|
|
|
match (a.1.metadata.is_dir(), b.1.metadata.is_dir()) {
|
|
|
|
|
(true, false) => Ordering::Less,
|
|
|
|
|
(false, true) => Ordering::Greater,
|
2024-10-09 15:41:10 -06:00
|
|
|
_ => check_reverse(a_time_deleted.cmp(&b_time_deleted), sort_direction),
|
2024-09-20 02:27:23 -04:00
|
|
|
}
|
|
|
|
|
} else {
|
2024-10-09 15:41:10 -06:00
|
|
|
check_reverse(b_time_deleted.cmp(&a_time_deleted), sort_direction)
|
2024-09-20 02:27:23 -04:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-02-29 03:46:14 +01:00
|
|
|
}
|
2024-02-29 12:26:45 -07:00
|
|
|
Some(items)
|
2024-02-29 03:46:14 +01:00
|
|
|
}
|
|
|
|
|
|
2024-09-20 09:00:59 -06:00
|
|
|
fn dnd_dest<'a>(
|
|
|
|
|
&self,
|
|
|
|
|
location: &Location,
|
|
|
|
|
element: impl Into<Element<'a, Message>>,
|
|
|
|
|
) -> Element<'a, Message> {
|
|
|
|
|
let location1 = location.clone();
|
|
|
|
|
let location2 = location.clone();
|
|
|
|
|
let location3 = location.clone();
|
|
|
|
|
let is_dnd_hovered = self.dnd_hovered.as_ref().map(|(l, _)| l) == Some(&location);
|
2024-10-21 13:51:10 -06:00
|
|
|
let mut container = widget::container(
|
2024-09-20 09:00:59 -06:00
|
|
|
DndDestination::for_data::<ClipboardPaste>(element, move |data, action| {
|
|
|
|
|
if let Some(mut data) = data {
|
|
|
|
|
if action == DndAction::Copy {
|
|
|
|
|
Message::Drop(Some((location1.clone(), data)))
|
|
|
|
|
} else if action == DndAction::Move {
|
|
|
|
|
data.kind = ClipboardKind::Cut;
|
|
|
|
|
Message::Drop(Some((location1.clone(), data)))
|
|
|
|
|
} else {
|
|
|
|
|
log::warn!("unsupported action: {:?}", action);
|
|
|
|
|
Message::Drop(None)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Message::Drop(None)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.on_enter(move |_, _, _| Message::DndEnter(location2.clone()))
|
|
|
|
|
.on_leave(move || Message::DndLeave(location3.clone())),
|
2024-10-21 13:51:10 -06:00
|
|
|
);
|
|
|
|
|
if is_dnd_hovered {
|
|
|
|
|
container = container.style(|t| {
|
|
|
|
|
let mut a = widget::container::Style::default();
|
2024-09-20 09:00:59 -06:00
|
|
|
let t = t.cosmic();
|
|
|
|
|
// todo use theme drop target color
|
|
|
|
|
let mut bg = t.accent_color();
|
|
|
|
|
bg.alpha = 0.2;
|
|
|
|
|
a.background = Some(Color::from(bg).into());
|
|
|
|
|
a.border = Border {
|
|
|
|
|
color: t.accent_color().into(),
|
|
|
|
|
width: 1.0,
|
|
|
|
|
radius: t.radius_s().into(),
|
|
|
|
|
};
|
|
|
|
|
a
|
2024-10-21 13:51:10 -06:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
container.into()
|
2024-09-20 09:00:59 -06:00
|
|
|
}
|
|
|
|
|
|
2024-09-23 15:56:32 -06:00
|
|
|
pub fn gallery_view(&self) -> Element<Message> {
|
|
|
|
|
let cosmic_theme::Spacing {
|
|
|
|
|
space_xxs,
|
2024-09-23 19:30:19 -06:00
|
|
|
space_xs,
|
2024-09-23 15:56:32 -06:00
|
|
|
space_m,
|
|
|
|
|
..
|
|
|
|
|
} = theme::active().cosmic().spacing;
|
|
|
|
|
|
|
|
|
|
//TODO: display error messages when image not found?
|
|
|
|
|
let mut name_opt = None;
|
2024-10-10 10:05:48 -06:00
|
|
|
let mut element_opt: Option<Element<Message>> = None;
|
2024-09-23 15:56:32 -06:00
|
|
|
if let Some(index) = self.select_focus {
|
|
|
|
|
if let Some(items) = &self.items_opt {
|
|
|
|
|
if let Some(item) = items.get(index) {
|
|
|
|
|
name_opt = Some(widget::text::heading(&item.display_name));
|
|
|
|
|
match item
|
|
|
|
|
.thumbnail_opt
|
|
|
|
|
.as_ref()
|
|
|
|
|
.unwrap_or(&ItemThumbnail::NotImage)
|
|
|
|
|
{
|
2024-09-24 08:39:13 -06:00
|
|
|
ItemThumbnail::NotImage => {}
|
2024-10-10 09:07:02 -06:00
|
|
|
ItemThumbnail::Image(handle, _) => {
|
2024-09-23 15:56:32 -06:00
|
|
|
if let Some(path) = item.path_opt() {
|
2024-10-10 10:05:48 -06:00
|
|
|
element_opt = Some(
|
2024-10-21 15:39:57 -06:00
|
|
|
widget::container(
|
|
|
|
|
//TODO: use widget::image::viewer, when its zoom can be reset
|
|
|
|
|
widget::image(widget::image::Handle::from_path(path)),
|
|
|
|
|
)
|
2024-10-23 00:16:30 +02:00
|
|
|
.center(Length::Fill)
|
2024-10-21 15:39:57 -06:00
|
|
|
.into(),
|
2024-09-24 08:39:13 -06:00
|
|
|
);
|
2024-09-25 11:30:17 -06:00
|
|
|
} else {
|
2024-10-10 10:05:48 -06:00
|
|
|
element_opt = Some(
|
2024-10-21 15:39:57 -06:00
|
|
|
widget::container(
|
|
|
|
|
//TODO: use widget::image::viewer, when its zoom can be reset
|
|
|
|
|
widget::image(handle.clone()),
|
|
|
|
|
)
|
2024-10-23 00:16:30 +02:00
|
|
|
.center(Length::Fill)
|
2024-10-21 15:39:57 -06:00
|
|
|
.into(),
|
2024-09-23 15:56:32 -06:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-10 09:07:02 -06:00
|
|
|
ItemThumbnail::Svg(handle) => {
|
2024-10-10 10:05:48 -06:00
|
|
|
element_opt = Some(
|
2024-10-10 09:07:02 -06:00
|
|
|
widget::svg(handle.clone())
|
2024-09-25 11:30:17 -06:00
|
|
|
.width(Length::Fill)
|
|
|
|
|
.height(Length::Fill)
|
|
|
|
|
.into(),
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-10-10 10:05:48 -06:00
|
|
|
ItemThumbnail::Text(text) => {
|
2024-11-10 08:04:43 +01:00
|
|
|
element_opt = Some(
|
2024-11-11 23:53:25 +01:00
|
|
|
widget::container(
|
|
|
|
|
widget::text_editor(text).padding(space_xxs).class(
|
|
|
|
|
cosmic::theme::iced::TextEditor::Custom(Box::new(
|
|
|
|
|
text_editor_class,
|
|
|
|
|
)),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
.center(Length::Fill)
|
|
|
|
|
.into(),
|
2024-11-10 08:04:43 +01:00
|
|
|
)
|
2024-10-10 10:05:48 -06:00
|
|
|
}
|
2024-09-23 15:56:32 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut column = widget::column::with_capacity(2);
|
2024-10-21 13:51:10 -06:00
|
|
|
column = column.push(widget::Space::with_height(Length::Fixed(space_m.into())));
|
2024-09-23 15:56:32 -06:00
|
|
|
{
|
2024-10-21 13:51:10 -06:00
|
|
|
let mut row = widget::row::with_capacity(5).align_y(Alignment::Center);
|
|
|
|
|
row = row.push(widget::horizontal_space());
|
2024-09-23 15:56:32 -06:00
|
|
|
if let Some(name) = name_opt {
|
|
|
|
|
row = row.push(name);
|
|
|
|
|
}
|
2024-10-21 13:51:10 -06:00
|
|
|
row = row.push(widget::horizontal_space());
|
2024-09-23 15:56:32 -06:00
|
|
|
row = row.push(
|
|
|
|
|
widget::button::icon(widget::icon::from_name("window-close-symbolic"))
|
2024-10-21 13:51:10 -06:00
|
|
|
.class(theme::Button::Standard)
|
2024-09-23 15:56:32 -06:00
|
|
|
.on_press(Message::Gallery(false)),
|
|
|
|
|
);
|
2024-10-21 13:51:10 -06:00
|
|
|
row = row.push(widget::Space::with_width(Length::Fixed(space_m.into())));
|
2024-09-23 15:56:32 -06:00
|
|
|
// This mouse area provides window drag while the header bar is hidden
|
2024-09-23 16:05:43 -06:00
|
|
|
let mouse_area = mouse_area::MouseArea::new(row)
|
|
|
|
|
.on_drag(|_| Message::WindowDrag)
|
|
|
|
|
.on_double_click(|_| Message::WindowToggleMaximize);
|
2024-09-23 15:56:32 -06:00
|
|
|
column = column.push(mouse_area);
|
|
|
|
|
}
|
|
|
|
|
{
|
2024-10-21 13:51:10 -06:00
|
|
|
let mut row = widget::row::with_capacity(7).align_y(Alignment::Center);
|
|
|
|
|
row = row.push(widget::Space::with_width(Length::Fixed(space_m.into())));
|
2024-09-23 15:56:32 -06:00
|
|
|
row = row.push(
|
|
|
|
|
widget::button::icon(widget::icon::from_name("go-previous-symbolic"))
|
2024-09-23 19:30:19 -06:00
|
|
|
.padding(space_xs)
|
2024-10-21 13:51:10 -06:00
|
|
|
.class(theme::Button::Standard)
|
2024-09-24 08:39:13 -06:00
|
|
|
.on_press(Message::GalleryPrevious),
|
2024-09-23 15:56:32 -06:00
|
|
|
);
|
2024-10-21 13:51:10 -06:00
|
|
|
row = row.push(widget::Space::with_width(Length::Fixed(space_xxs.into())));
|
2024-10-10 10:05:48 -06:00
|
|
|
if let Some(element) = element_opt {
|
|
|
|
|
row = row.push(element);
|
2024-09-23 15:56:32 -06:00
|
|
|
} else {
|
|
|
|
|
//TODO: what to do when no image?
|
2024-09-23 19:30:19 -06:00
|
|
|
row = row.push(widget::Space::new(Length::Fill, Length::Fill));
|
2024-09-23 15:56:32 -06:00
|
|
|
}
|
2024-10-21 13:51:10 -06:00
|
|
|
row = row.push(widget::Space::with_width(Length::Fixed(space_xxs.into())));
|
2024-09-23 15:56:32 -06:00
|
|
|
row = row.push(
|
|
|
|
|
widget::button::icon(widget::icon::from_name("go-next-symbolic"))
|
2024-09-23 19:30:19 -06:00
|
|
|
.padding(space_xs)
|
2024-10-21 13:51:10 -06:00
|
|
|
.class(theme::Button::Standard)
|
2024-09-24 08:39:13 -06:00
|
|
|
.on_press(Message::GalleryNext),
|
2024-09-23 15:56:32 -06:00
|
|
|
);
|
2024-10-21 13:51:10 -06:00
|
|
|
row = row.push(widget::Space::with_width(Length::Fixed(space_m.into())));
|
2024-09-23 15:56:32 -06:00
|
|
|
column = column.push(row);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
widget::container(column)
|
|
|
|
|
.width(Length::Fill)
|
|
|
|
|
.height(Length::Fill)
|
2024-10-21 13:51:10 -06:00
|
|
|
.style(|theme| {
|
2024-09-23 15:56:32 -06:00
|
|
|
let cosmic = theme.cosmic();
|
|
|
|
|
let mut bg = cosmic.bg_color();
|
|
|
|
|
bg.alpha = 0.75;
|
2024-10-21 13:51:10 -06:00
|
|
|
widget::container::Style {
|
2024-09-23 15:56:32 -06:00
|
|
|
background: Some(Color::from(bg).into()),
|
|
|
|
|
..Default::default()
|
|
|
|
|
}
|
2024-10-21 13:51:10 -06:00
|
|
|
})
|
2024-09-23 15:56:32 -06:00
|
|
|
.into()
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-10 13:02:06 -06:00
|
|
|
pub fn location_view(&self) -> Element<Message> {
|
2024-07-09 11:16:14 -06:00
|
|
|
//TODO: responsiveness is done in a hacky way, potentially move this to a custom widget?
|
|
|
|
|
fn text_width<'a>(
|
|
|
|
|
content: &'a str,
|
|
|
|
|
font: font::Font,
|
|
|
|
|
font_size: f32,
|
|
|
|
|
line_height: f32,
|
|
|
|
|
) -> f32 {
|
2024-10-21 13:51:10 -06:00
|
|
|
let text: text::Text<&'a str, font::Font> = text::Text {
|
2024-07-09 11:16:14 -06:00
|
|
|
content,
|
|
|
|
|
bounds: Size::INFINITY,
|
|
|
|
|
size: font_size.into(),
|
|
|
|
|
line_height: text::LineHeight::Absolute(line_height.into()),
|
|
|
|
|
font,
|
|
|
|
|
horizontal_alignment: Horizontal::Left,
|
|
|
|
|
vertical_alignment: Vertical::Top,
|
|
|
|
|
shaping: text::Shaping::default(),
|
2024-10-21 13:51:10 -06:00
|
|
|
wrapping: text::Wrapping::None,
|
2024-07-09 11:16:14 -06:00
|
|
|
};
|
|
|
|
|
graphics::text::Paragraph::with_text(text)
|
|
|
|
|
.min_bounds()
|
|
|
|
|
.width
|
|
|
|
|
}
|
|
|
|
|
fn text_width_body<'a>(content: &'a str) -> f32 {
|
|
|
|
|
//TODO: should libcosmic set the font when using widget::text::body?
|
2024-10-05 07:39:03 -06:00
|
|
|
text_width(content, font::default(), 14.0, 20.0)
|
2024-07-09 11:16:14 -06:00
|
|
|
}
|
|
|
|
|
fn text_width_heading<'a>(content: &'a str) -> f32 {
|
2024-10-05 07:39:03 -06:00
|
|
|
text_width(content, font::semibold(), 14.0, 20.0)
|
2024-07-09 11:16:14 -06:00
|
|
|
}
|
|
|
|
|
|
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-07-31 14:11:37 +02:00
|
|
|
space_m,
|
2024-01-29 11:58:36 -07:00
|
|
|
..
|
2024-02-29 12:26:45 -07:00
|
|
|
} = theme::active().cosmic().spacing;
|
2024-07-31 14:11:37 +02:00
|
|
|
|
2024-07-09 11:16:14 -06:00
|
|
|
let size = self.size_opt.get().unwrap_or(Size::new(0.0, 0.0));
|
2024-01-29 11:58:36 -07:00
|
|
|
|
2024-07-13 11:34:03 +02:00
|
|
|
let mut row = widget::row::with_capacity(5)
|
2024-10-21 13:51:10 -06:00
|
|
|
.align_y(Alignment::Center)
|
2024-07-13 11:34:03 +02:00
|
|
|
.padding([space_xxxs, 0]);
|
2024-07-09 11:16:14 -06:00
|
|
|
let mut w = 0.0;
|
2024-01-30 10:47:41 -07:00
|
|
|
|
|
|
|
|
let mut prev_button =
|
2024-09-20 09:24:03 -06:00
|
|
|
widget::button::custom(widget::icon::from_name("go-previous-symbolic").size(16))
|
2024-01-30 10:47:41 -07:00
|
|
|
.padding(space_xxs)
|
2024-10-21 13:51:10 -06:00
|
|
|
.class(theme::Button::Icon);
|
2024-01-30 10:47:41 -07:00
|
|
|
if self.history_i > 0 && !self.history.is_empty() {
|
|
|
|
|
prev_button = prev_button.on_press(Message::GoPrevious);
|
|
|
|
|
}
|
|
|
|
|
row = row.push(prev_button);
|
2024-07-09 11:16:14 -06:00
|
|
|
w += 16.0 + 2.0 * space_xxs as f32;
|
2024-01-30 10:47:41 -07:00
|
|
|
|
2024-09-20 09:24:03 -06:00
|
|
|
let mut next_button =
|
|
|
|
|
widget::button::custom(widget::icon::from_name("go-next-symbolic").size(16))
|
|
|
|
|
.padding(space_xxs)
|
2024-10-21 13:51:10 -06:00
|
|
|
.class(theme::Button::Icon);
|
2024-01-30 10:47:41 -07:00
|
|
|
if self.history_i + 1 < self.history.len() {
|
|
|
|
|
next_button = next_button.on_press(Message::GoNext);
|
|
|
|
|
}
|
|
|
|
|
row = row.push(next_button);
|
2024-07-09 11:16:14 -06:00
|
|
|
w += 16.0 + 2.0 * space_xxs as f32;
|
2024-01-30 10:47:41 -07:00
|
|
|
|
2024-10-21 13:51:10 -06:00
|
|
|
row = row.push(widget::Space::with_width(Length::Fixed(space_s.into())));
|
2024-07-09 11:16:14 -06:00
|
|
|
w += space_s as f32;
|
2024-01-30 10:47:41 -07:00
|
|
|
|
2024-07-31 14:11:37 +02:00
|
|
|
//TODO: allow resizing?
|
|
|
|
|
let name_width = 300.0;
|
|
|
|
|
let modified_width = 200.0;
|
|
|
|
|
let size_width = 100.0;
|
|
|
|
|
let condensed = size.width < (name_width + modified_width + size_width);
|
|
|
|
|
|
2024-10-09 18:55:09 -06:00
|
|
|
let (sort_name, sort_direction, _) = self.sort_options();
|
2024-07-31 14:11:37 +02:00
|
|
|
let heading_item = |name, width, msg| {
|
|
|
|
|
let mut row = widget::row::with_capacity(2)
|
2024-10-21 13:51:10 -06:00
|
|
|
.align_y(Alignment::Center)
|
2024-10-25 13:24:17 +02:00
|
|
|
.spacing(space_xxxs)
|
2024-07-31 14:11:37 +02:00
|
|
|
.width(width);
|
|
|
|
|
row = row.push(widget::text::heading(name));
|
2024-10-09 15:41:10 -06:00
|
|
|
match (sort_name == msg, sort_direction) {
|
2024-07-31 14:11:37 +02:00
|
|
|
(true, true) => {
|
|
|
|
|
row = row.push(widget::icon::from_name("pan-down-symbolic").size(16));
|
|
|
|
|
}
|
|
|
|
|
(true, false) => {
|
|
|
|
|
row = row.push(widget::icon::from_name("pan-up-symbolic").size(16));
|
|
|
|
|
}
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
//TODO: make it possible to resize with the mouse
|
2024-09-23 15:56:32 -06:00
|
|
|
return mouse_area::MouseArea::new(row)
|
2024-07-31 14:11:37 +02:00
|
|
|
.on_press(move |_point_opt| Message::ToggleSort(msg))
|
|
|
|
|
.into();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let heading_row = widget::row::with_children(vec![
|
|
|
|
|
heading_item(fl!("name"), Length::Fill, HeadingOptions::Name),
|
2024-09-20 02:27:23 -04:00
|
|
|
if self.location == Location::Trash {
|
|
|
|
|
heading_item(
|
|
|
|
|
fl!("trashed-on"),
|
|
|
|
|
Length::Fixed(modified_width),
|
|
|
|
|
HeadingOptions::TrashedOn,
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
heading_item(
|
|
|
|
|
fl!("modified"),
|
|
|
|
|
Length::Fixed(modified_width),
|
|
|
|
|
HeadingOptions::Modified,
|
|
|
|
|
)
|
|
|
|
|
},
|
2024-07-31 14:11:37 +02:00
|
|
|
heading_item(fl!("size"), Length::Fixed(size_width), HeadingOptions::Size),
|
|
|
|
|
])
|
2024-10-21 13:51:10 -06:00
|
|
|
.align_y(Alignment::Center)
|
2024-07-31 14:11:37 +02:00
|
|
|
.height(Length::Fixed((space_m + 4).into()))
|
2024-10-25 13:24:17 +02:00
|
|
|
.padding([0, space_xxs]);
|
|
|
|
|
|
|
|
|
|
let accent_rule =
|
|
|
|
|
horizontal_rule(1).class(theme::Rule::Custom(Box::new(|theme| rule::Style {
|
|
|
|
|
color: theme.cosmic().accent_color().into(),
|
|
|
|
|
width: 1,
|
|
|
|
|
radius: 0.0.into(),
|
|
|
|
|
fill_mode: rule::FillMode::Full,
|
|
|
|
|
})));
|
2024-11-11 23:53:25 +01:00
|
|
|
let heading_rule = widget::container(horizontal_rule(1))
|
2024-10-25 13:24:17 +02:00
|
|
|
.padding([0, theme::active().cosmic().corner_radii.radius_xs[0] as u16]);
|
2024-07-31 14:11:37 +02:00
|
|
|
|
2024-01-29 11:58:36 -07:00
|
|
|
if let Some(location) = &self.edit_location {
|
2024-10-09 15:41:10 -06:00
|
|
|
//TODO: allow editing other locations
|
|
|
|
|
if let Some(path) = location.path_opt() {
|
|
|
|
|
row = row.push(
|
|
|
|
|
widget::button::custom(
|
|
|
|
|
widget::icon::from_name("window-close-symbolic").size(16),
|
|
|
|
|
)
|
|
|
|
|
.on_press(Message::EditLocation(None))
|
|
|
|
|
.padding(space_xxs)
|
2024-10-21 13:51:10 -06:00
|
|
|
.class(theme::Button::Icon),
|
2024-10-09 15:41:10 -06:00
|
|
|
);
|
|
|
|
|
row = row.push(
|
|
|
|
|
widget::text_input("", path.to_string_lossy())
|
|
|
|
|
.id(self.edit_location_id.clone())
|
|
|
|
|
.on_input(|input| {
|
|
|
|
|
Message::EditLocation(Some(location.with_path(PathBuf::from(input))))
|
|
|
|
|
})
|
|
|
|
|
.on_submit(Message::Location(location.clone()))
|
|
|
|
|
.line_height(1.0),
|
|
|
|
|
);
|
|
|
|
|
let mut column = widget::column::with_capacity(4).padding([0, space_s]);
|
|
|
|
|
column = column.push(row);
|
2024-10-25 13:24:17 +02:00
|
|
|
column = column.push(accent_rule);
|
2024-10-09 15:41:10 -06:00
|
|
|
if self.config.view == View::List && !condensed {
|
|
|
|
|
column = column.push(heading_row);
|
2024-10-25 13:24:17 +02:00
|
|
|
column = column.push(heading_rule);
|
2024-01-29 11:58:36 -07:00
|
|
|
}
|
2024-10-09 15:41:10 -06:00
|
|
|
return column.into();
|
2024-01-29 11:58:36 -07:00
|
|
|
}
|
2024-10-09 15:41:10 -06:00
|
|
|
} else if let Some(path) = self.location.path_opt() {
|
2024-01-30 10:47:41 -07:00
|
|
|
row = row.push(
|
2024-06-30 10:05:58 -06:00
|
|
|
crate::mouse_area::MouseArea::new(
|
2024-09-20 09:24:03 -06:00
|
|
|
widget::button::custom(widget::icon::from_name("edit-symbolic").size(16))
|
2024-06-30 10:05:58 -06:00
|
|
|
.padding(space_xxs)
|
2024-10-21 13:51:10 -06:00
|
|
|
.class(theme::Button::Icon)
|
2024-08-16 10:33:32 -06:00
|
|
|
.on_press(Message::EditLocation(Some(self.location.clone()))),
|
2024-06-30 10:05:58 -06:00
|
|
|
)
|
|
|
|
|
.on_middle_press(move |_| Message::OpenInNewTab(path.clone())),
|
2024-01-30 10:47:41 -07:00
|
|
|
);
|
2024-07-09 11:16:14 -06:00
|
|
|
w += 16.0 + 2.0 * space_xxs as f32;
|
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 {
|
2024-10-09 15:41:10 -06:00
|
|
|
Location::Desktop(path, ..) | Location::Path(path) | Location::Search(path, ..) => {
|
2024-07-09 11:16:14 -06:00
|
|
|
let excess_str = "...";
|
|
|
|
|
let excess_width = text_width_body(excess_str);
|
2024-06-30 10:05:58 -06:00
|
|
|
for (index, ancestor) in path.ancestors().enumerate() {
|
2024-09-09 13:27:54 -06:00
|
|
|
let (name, found_home) = folder_name(&ancestor);
|
2024-07-09 11:16:14 -06:00
|
|
|
let (name_width, name_text) = if children.is_empty() {
|
|
|
|
|
(
|
|
|
|
|
text_width_heading(&name),
|
2024-10-21 13:51:10 -06:00
|
|
|
widget::text::heading(name).wrapping(text::Wrapping::None),
|
2024-07-09 11:16:14 -06:00
|
|
|
)
|
2024-01-29 11:16:19 -07:00
|
|
|
} else {
|
2024-01-29 11:09:05 -07:00
|
|
|
children.push(
|
|
|
|
|
widget::icon::from_name("go-next-symbolic")
|
|
|
|
|
.size(16)
|
|
|
|
|
.icon()
|
|
|
|
|
.into(),
|
|
|
|
|
);
|
2024-07-09 11:16:14 -06:00
|
|
|
w += 16.0;
|
|
|
|
|
(
|
|
|
|
|
text_width_body(&name),
|
2024-10-21 13:51:10 -06:00
|
|
|
widget::text::body(name).wrapping(text::Wrapping::None),
|
2024-07-09 11:16:14 -06:00
|
|
|
)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Add padding for mouse area
|
|
|
|
|
w += 2.0 * space_xxxs as f32;
|
|
|
|
|
|
|
|
|
|
let mut row = widget::row::with_capacity(2)
|
2024-10-21 13:51:10 -06:00
|
|
|
.align_y(Alignment::Center)
|
2024-07-09 11:16:14 -06:00
|
|
|
.spacing(space_xxxs);
|
|
|
|
|
//TODO: figure out why this hardcoded offset is needed after the first item is ellipsed
|
2024-10-11 08:29:22 -06:00
|
|
|
let overflow_offset = 64.0;
|
2024-09-09 13:14:41 -06:00
|
|
|
let overflow = w + name_width + overflow_offset > size.width && index > 0;
|
2024-07-09 11:16:14 -06:00
|
|
|
if overflow {
|
|
|
|
|
row = row.push(widget::text::body(excess_str));
|
|
|
|
|
w += excess_width;
|
|
|
|
|
} else {
|
|
|
|
|
row = row.push(name_text);
|
|
|
|
|
w += name_width;
|
2024-01-10 12:57:30 -07:00
|
|
|
}
|
|
|
|
|
|
2024-10-09 15:41:10 -06:00
|
|
|
let location = self.location.with_path(ancestor.to_path_buf());
|
2024-06-30 10:05:58 -06:00
|
|
|
let mut mouse_area = crate::mouse_area::MouseArea::new(
|
2024-09-20 09:24:03 -06:00
|
|
|
widget::button::custom(row)
|
2024-01-10 12:57:30 -07:00
|
|
|
.padding(space_xxxs)
|
2024-10-21 13:51:10 -06:00
|
|
|
.class(theme::Button::Link)
|
2024-09-20 09:00:59 -06:00
|
|
|
.on_press(Message::Location(location.clone())),
|
2024-08-16 10:33:32 -06:00
|
|
|
);
|
2024-06-30 10:05:58 -06:00
|
|
|
|
2024-06-30 15:45:01 -06:00
|
|
|
if self.location_context_menu_index.is_some() {
|
|
|
|
|
mouse_area = mouse_area.on_right_press(move |_point_opt| {
|
|
|
|
|
Message::LocationContextMenuIndex(None)
|
|
|
|
|
})
|
2024-06-30 10:05:58 -06:00
|
|
|
} else {
|
2024-08-16 10:33:32 -06:00
|
|
|
mouse_area = mouse_area.on_right_press_no_capture(move |_point_opt| {
|
2024-06-30 15:45:01 -06:00
|
|
|
Message::LocationContextMenuIndex(Some(index))
|
2024-06-30 10:05:58 -06:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-30 12:32:51 -06:00
|
|
|
let mouse_area = if let Location::Path(_) = &self.location {
|
|
|
|
|
mouse_area
|
|
|
|
|
.on_middle_press(move |_| Message::OpenInNewTab(ancestor.to_path_buf()))
|
|
|
|
|
} else {
|
|
|
|
|
mouse_area
|
|
|
|
|
};
|
|
|
|
|
|
2024-09-20 09:00:59 -06:00
|
|
|
children.push(self.dnd_dest(&location, mouse_area));
|
2024-01-10 12:57:30 -07:00
|
|
|
|
2024-07-09 11:16:14 -06:00
|
|
|
if found_home || overflow {
|
2024-01-10 12:57:30 -07:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
children.reverse();
|
|
|
|
|
}
|
|
|
|
|
Location::Trash => {
|
|
|
|
|
children.push(
|
2024-09-20 09:24:03 -06:00
|
|
|
widget::button::custom(widget::text::heading(fl!("trash")))
|
2024-01-10 12:57:30 -07:00
|
|
|
.padding(space_xxxs)
|
|
|
|
|
.on_press(Message::Location(Location::Trash))
|
2024-10-21 13:51:10 -06:00
|
|
|
.class(theme::Button::Text)
|
2024-01-10 12:57:30 -07:00
|
|
|
.into(),
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-08-16 14:42:09 +02:00
|
|
|
Location::Recents => {
|
|
|
|
|
children.push(
|
2024-09-20 09:24:03 -06:00
|
|
|
widget::button::custom(widget::text::heading(fl!("recents")))
|
2024-08-16 14:42:09 +02:00
|
|
|
.padding(space_xxxs)
|
|
|
|
|
.on_press(Message::Location(Location::Recents))
|
2024-10-21 13:51:10 -06:00
|
|
|
.class(theme::Button::Text)
|
2024-08-16 14:42:09 +02:00
|
|
|
.into(),
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-09-13 15:13:37 -06:00
|
|
|
Location::Network(uri, display_name) => {
|
2024-09-12 15:54:54 -06:00
|
|
|
children.push(
|
2024-09-20 09:24:03 -06:00
|
|
|
widget::button::custom(widget::text::heading(display_name))
|
2024-09-12 15:54:54 -06:00
|
|
|
.padding(space_xxxs)
|
2024-09-13 15:13:37 -06:00
|
|
|
.on_press(Message::Location(Location::Network(
|
|
|
|
|
uri.clone(),
|
|
|
|
|
display_name.clone(),
|
|
|
|
|
)))
|
2024-10-21 13:51:10 -06:00
|
|
|
.class(theme::Button::Text)
|
2024-09-12 15:54:54 -06:00
|
|
|
.into(),
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-01-10 12:57:30 -07:00
|
|
|
}
|
2024-01-29 11:58:36 -07:00
|
|
|
|
2024-01-30 10:47:41 -07:00
|
|
|
for child in children {
|
|
|
|
|
row = row.push(child);
|
|
|
|
|
}
|
2024-07-31 14:11:37 +02:00
|
|
|
let mut column = widget::column::with_capacity(4).padding([0, space_s]);
|
2024-07-13 01:53:30 +02:00
|
|
|
column = column.push(row);
|
2024-10-25 13:24:17 +02:00
|
|
|
column = column.push(accent_rule);
|
2024-06-30 10:05:58 -06:00
|
|
|
|
2024-07-31 14:11:37 +02:00
|
|
|
if self.config.view == View::List && !condensed {
|
|
|
|
|
column = column.push(heading_row);
|
2024-10-25 13:24:17 +02:00
|
|
|
column = column.push(heading_rule);
|
2024-07-31 14:11:37 +02:00
|
|
|
}
|
|
|
|
|
|
2024-07-13 01:53:30 +02:00
|
|
|
let mouse_area = crate::mouse_area::MouseArea::new(column)
|
2024-06-30 15:45:01 -06:00
|
|
|
.on_right_press(Message::LocationContextMenuPoint);
|
|
|
|
|
|
|
|
|
|
let mut popover = widget::popover(mouse_area);
|
|
|
|
|
if let (Some(point), Some(index)) = (
|
|
|
|
|
self.location_context_menu_point,
|
|
|
|
|
self.location_context_menu_index,
|
|
|
|
|
) {
|
2024-06-30 10:05:58 -06:00
|
|
|
popover = popover
|
2024-06-30 15:45:01 -06:00
|
|
|
.popup(menu::location_context_menu(index))
|
2024-06-30 10:05:58 -06:00
|
|
|
.position(widget::popover::Position::Point(point))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
popover.into()
|
2024-01-10 12:57:30 -07:00
|
|
|
}
|
|
|
|
|
|
2024-02-29 12:26:45 -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
|
|
|
|
2025-01-11 15:00:56 -07:00
|
|
|
mouse_area::MouseArea::new(widget::column::with_children(vec![widget::container(
|
2024-08-20 13:26:10 -06:00
|
|
|
widget::column::with_children(match self.mode {
|
|
|
|
|
Mode::App | Mode::Dialog(_) => vec![
|
|
|
|
|
widget::icon::from_name("folder-symbolic")
|
|
|
|
|
.size(64)
|
|
|
|
|
.icon()
|
|
|
|
|
.into(),
|
2024-11-25 03:24:24 +01:00
|
|
|
widget::text::body(if has_hidden {
|
2024-08-20 13:26:10 -06:00
|
|
|
fl!("empty-folder-hidden")
|
2024-10-09 15:41:10 -06:00
|
|
|
} else if matches!(self.location, Location::Search(..)) {
|
2024-08-20 13:26:10 -06:00
|
|
|
fl!("no-results")
|
|
|
|
|
} else {
|
|
|
|
|
fl!("empty-folder")
|
|
|
|
|
})
|
2024-01-05 09:36:16 -07:00
|
|
|
.into(),
|
2024-08-20 13:26:10 -06:00
|
|
|
],
|
|
|
|
|
Mode::Desktop => Vec::new(),
|
|
|
|
|
})
|
2024-10-21 13:51:10 -06:00
|
|
|
.align_x(Alignment::Center)
|
2024-02-26 12:05:29 -07:00
|
|
|
.spacing(space_xxs),
|
|
|
|
|
)
|
2024-10-25 13:24:17 +02:00
|
|
|
.center(Length::Fill)
|
2025-01-11 15:00:56 -07:00
|
|
|
.into()]))
|
|
|
|
|
.on_press(|_| Message::Click(None))
|
2024-01-05 09:36:16 -07:00
|
|
|
.into()
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-21 13:51:10 -06:00
|
|
|
pub fn grid_view(&self) -> (Option<Element<'static, Message>>, Element<Message>, bool) {
|
2024-02-29 11:25:46 -07:00
|
|
|
let cosmic_theme::Spacing {
|
2024-02-29 15:52:13 -07:00
|
|
|
space_m,
|
2024-02-29 11:25:46 -07:00
|
|
|
space_xxs,
|
|
|
|
|
space_xxxs,
|
|
|
|
|
..
|
2024-02-29 12:26:45 -07:00
|
|
|
} = theme::active().cosmic().spacing;
|
2024-01-05 09:36:16 -07:00
|
|
|
|
2024-02-18 02:44:54 -05:00
|
|
|
let TabConfig {
|
|
|
|
|
show_hidden,
|
|
|
|
|
icon_sizes,
|
2024-03-13 23:23:00 -04:00
|
|
|
..
|
2024-02-18 02:44:54 -05:00
|
|
|
} = self.config;
|
2024-01-29 11:25:43 -07:00
|
|
|
|
2024-07-11 10:12:50 -06:00
|
|
|
let text_height = 3 * 20; // 3 lines of text
|
|
|
|
|
let item_width = (3 * space_xxs + icon_sizes.grid() + 3 * space_xxs) as usize;
|
2024-02-29 20:35:40 -07:00
|
|
|
let item_height =
|
|
|
|
|
(space_xxxs + icon_sizes.grid() + space_xxxs + text_height + space_xxxs) as usize;
|
|
|
|
|
|
2024-05-31 09:51:34 -06:00
|
|
|
let (width, height) = match self.size_opt.get() {
|
2024-02-29 15:52:13 -07:00
|
|
|
Some(size) => (
|
|
|
|
|
(size.width.floor() as usize)
|
|
|
|
|
.checked_sub(2 * (space_m as usize))
|
|
|
|
|
.unwrap_or(0)
|
2024-02-29 20:35:40 -07:00
|
|
|
.max(item_width),
|
|
|
|
|
(size.height.floor() as usize).max(item_height),
|
2024-02-29 15:52:13 -07:00
|
|
|
),
|
2024-02-29 20:35:40 -07:00
|
|
|
None => (item_width, item_height),
|
2024-02-29 11:40:31 -07:00
|
|
|
};
|
2024-02-29 11:25:46 -07:00
|
|
|
|
|
|
|
|
let (cols, column_spacing) = {
|
2024-02-29 20:35:40 -07:00
|
|
|
let width_m1 = width.checked_sub(item_width).unwrap_or(0);
|
|
|
|
|
let cols_m1 = width_m1 / (item_width + space_xxs as usize);
|
2024-02-29 11:25:46 -07:00
|
|
|
let cols = cols_m1 + 1;
|
|
|
|
|
let spacing = width_m1
|
|
|
|
|
.checked_div(cols_m1)
|
|
|
|
|
.unwrap_or(0)
|
2024-02-29 20:35:40 -07:00
|
|
|
.checked_sub(item_width)
|
2024-02-29 11:25:46 -07:00
|
|
|
.unwrap_or(0);
|
|
|
|
|
(cols, spacing as u16)
|
|
|
|
|
};
|
|
|
|
|
|
2024-08-20 13:26:10 -06:00
|
|
|
let rows = {
|
|
|
|
|
let height_m1 = height.checked_sub(item_height).unwrap_or(0);
|
|
|
|
|
let rows_m1 = height_m1 / (item_height + space_xxs as usize);
|
|
|
|
|
rows_m1 + 1
|
|
|
|
|
};
|
|
|
|
|
|
2024-02-29 11:25:46 -07:00
|
|
|
let mut grid = widget::grid()
|
|
|
|
|
.column_spacing(column_spacing)
|
|
|
|
|
.row_spacing(space_xxs)
|
2024-07-15 11:11:52 +02:00
|
|
|
.padding(space_xxs.into());
|
2024-04-10 11:41:25 -04:00
|
|
|
let mut dnd_items: Vec<(usize, (usize, usize), &Item)> = Vec::new();
|
|
|
|
|
let mut drag_w_i = usize::MAX;
|
|
|
|
|
let mut drag_n_i = usize::MAX;
|
|
|
|
|
let mut drag_e_i = 0;
|
|
|
|
|
let mut drag_s_i = 0;
|
|
|
|
|
|
2024-05-30 18:36:57 -06:00
|
|
|
let mut children = Vec::new();
|
|
|
|
|
|
2024-03-15 03:10:02 -04:00
|
|
|
if let Some(items) = self.column_sort() {
|
2024-01-05 08:55:18 -07:00
|
|
|
let mut count = 0;
|
2024-02-29 11:25:46 -07:00
|
|
|
let mut col = 0;
|
|
|
|
|
let mut row = 0;
|
2024-08-20 13:26:10 -06:00
|
|
|
let mut page_row = 0;
|
2024-01-05 08:55:18 -07:00
|
|
|
let mut hidden = 0;
|
2024-08-20 13:26:10 -06:00
|
|
|
let mut grid_elements = Vec::new();
|
2024-03-15 03:10:02 -04:00
|
|
|
for &(i, item) in items.iter() {
|
2024-02-11 00:24:35 -05:00
|
|
|
if !show_hidden && item.hidden {
|
2024-02-29 13:24:57 -07:00
|
|
|
item.pos_opt.set(None);
|
2024-02-29 11:25:46 -07:00
|
|
|
item.rect_opt.set(None);
|
2024-01-05 08:55:18 -07:00
|
|
|
hidden += 1;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2024-02-29 13:24:57 -07:00
|
|
|
item.pos_opt.set(Some((row, col)));
|
2024-02-29 11:25:46 -07:00
|
|
|
item.rect_opt.set(Some(Rectangle::new(
|
|
|
|
|
Point::new(
|
2024-02-29 20:35:40 -07:00
|
|
|
(col * (item_width + column_spacing as usize) + space_m as usize) as f32,
|
|
|
|
|
(row * (item_height + space_xxs as usize)) as f32,
|
2024-02-29 11:25:46 -07:00
|
|
|
),
|
2024-02-29 20:35:40 -07:00
|
|
|
Size::new(item_width as f32, item_height as f32),
|
2024-02-29 11:25:46 -07:00
|
|
|
)));
|
2024-01-05 08:55:18 -07:00
|
|
|
|
2024-02-29 11:25:46 -07:00
|
|
|
//TODO: one focus group per grid item (needs custom widget)
|
2024-10-10 11:59:57 -06:00
|
|
|
let buttons: Vec<Element<Message>> = vec![
|
2024-09-20 09:24:03 -06:00
|
|
|
widget::button::custom(
|
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()),
|
|
|
|
|
)
|
2024-07-11 10:12:50 -06:00
|
|
|
.padding(space_xxxs)
|
2024-10-16 10:22:25 +11:00
|
|
|
.class(button_style(
|
|
|
|
|
item.selected,
|
|
|
|
|
item.highlighted,
|
|
|
|
|
false,
|
|
|
|
|
false,
|
|
|
|
|
false,
|
|
|
|
|
))
|
2024-10-10 11:59:57 -06:00
|
|
|
.into(),
|
|
|
|
|
widget::tooltip(
|
|
|
|
|
widget::button::custom(widget::text::body(&item.display_name))
|
|
|
|
|
.id(item.button_id.clone())
|
|
|
|
|
.padding([0, space_xxxs])
|
2024-10-21 13:51:10 -06:00
|
|
|
.class(button_style(
|
2024-10-10 11:59:57 -06:00
|
|
|
item.selected,
|
2024-10-16 10:22:25 +11:00
|
|
|
item.highlighted,
|
2024-10-10 11:59:57 -06:00
|
|
|
true,
|
|
|
|
|
true,
|
|
|
|
|
matches!(self.mode, Mode::Desktop),
|
|
|
|
|
)),
|
2024-10-21 13:51:10 -06:00
|
|
|
widget::text::body(&item.name),
|
2024-10-10 11:59:57 -06:00
|
|
|
widget::tooltip::Position::Bottom,
|
|
|
|
|
)
|
|
|
|
|
.into(),
|
2024-02-29 11:25:46 -07:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
let mut column = widget::column::with_capacity(buttons.len())
|
2024-10-21 13:51:10 -06:00
|
|
|
.align_x(Alignment::Center)
|
2024-02-29 20:35:40 -07:00
|
|
|
.height(Length::Fixed(item_height as f32))
|
|
|
|
|
.width(Length::Fixed(item_width as f32));
|
2024-02-29 11:25:46 -07:00
|
|
|
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(
|
2024-06-30 14:57:37 -06:00
|
|
|
move |_point_opt| Message::RightClick(Some(i)),
|
2024-02-29 11:25:46 -07:00
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-01-05 15:10:46 -07:00
|
|
|
}
|
2024-02-29 11:25:46 -07:00
|
|
|
|
2024-09-20 09:00:59 -06:00
|
|
|
let column: Element<Message> =
|
|
|
|
|
if item.metadata.is_dir() && item.location_opt.is_some() {
|
|
|
|
|
self.dnd_dest(&item.location_opt.clone().unwrap(), column)
|
2024-04-10 11:41:25 -04:00
|
|
|
} else {
|
2024-09-20 09:00:59 -06:00
|
|
|
column.into()
|
|
|
|
|
};
|
2024-04-10 11:41:25 -04:00
|
|
|
|
|
|
|
|
if item.selected {
|
|
|
|
|
dnd_items.push((i, (row, col), item));
|
|
|
|
|
drag_w_i = drag_w_i.min(col);
|
|
|
|
|
drag_n_i = drag_n_i.min(row);
|
|
|
|
|
drag_e_i = drag_e_i.max(col);
|
|
|
|
|
drag_s_i = drag_s_i.max(row);
|
|
|
|
|
}
|
|
|
|
|
let mouse_area = crate::mouse_area::MouseArea::new(column)
|
|
|
|
|
.on_press(move |_| Message::Click(Some(i)))
|
|
|
|
|
.on_double_click(move |_| Message::DoubleClick(Some(i)))
|
2024-06-29 19:35:27 -06:00
|
|
|
.on_release(move |_| Message::ClickRelease(Some(i)))
|
2024-10-16 10:22:25 +11:00
|
|
|
.on_middle_press(move |_| Message::MiddleClick(i))
|
|
|
|
|
.on_enter(move || Message::HighlightActivate(i))
|
|
|
|
|
.on_exit(move || Message::HighlightDeactivate(i));
|
2024-08-20 13:26:10 -06:00
|
|
|
|
|
|
|
|
//TODO: error if the row or col is already set?
|
|
|
|
|
while grid_elements.len() <= row {
|
|
|
|
|
grid_elements.push(Vec::new());
|
|
|
|
|
}
|
|
|
|
|
grid_elements[row].push(mouse_area);
|
2024-02-29 11:25:46 -07:00
|
|
|
|
2024-01-05 09:36:16 -07:00
|
|
|
count += 1;
|
2024-08-20 13:26:10 -06:00
|
|
|
if matches!(self.mode, Mode::Desktop) {
|
2024-02-29 11:25:46 -07:00
|
|
|
row += 1;
|
2024-08-20 13:26:10 -06:00
|
|
|
if row >= page_row + rows {
|
|
|
|
|
row = 0;
|
|
|
|
|
col += 1;
|
|
|
|
|
}
|
|
|
|
|
if col >= cols {
|
|
|
|
|
col = 0;
|
|
|
|
|
page_row += rows;
|
|
|
|
|
row = page_row;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
col += 1;
|
|
|
|
|
if col >= cols {
|
|
|
|
|
col = 0;
|
|
|
|
|
row += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for row_elements in grid_elements {
|
|
|
|
|
for element in row_elements {
|
|
|
|
|
grid = grid.push(element);
|
2024-02-29 11:25:46 -07:00
|
|
|
}
|
2024-08-20 13:26:10 -06:00
|
|
|
grid = grid.insert_row();
|
2024-01-05 09:36:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if count == 0 {
|
2024-05-09 13:28:44 -06:00
|
|
|
return (None, self.empty_view(hidden > 0), false);
|
2024-01-05 09:36:16 -07:00
|
|
|
}
|
2024-02-29 11:40:31 -07:00
|
|
|
|
2024-05-30 18:36:57 -06:00
|
|
|
children.push(grid.into());
|
|
|
|
|
|
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
|
2024-07-15 11:11:52 +02:00
|
|
|
{
|
2024-02-29 11:40:31 -07:00
|
|
|
let mut max_bottom = 0;
|
2024-03-16 00:52:32 -04:00
|
|
|
for (_, item) in items {
|
|
|
|
|
if let Some(rect) = item.rect_opt.get() {
|
2024-02-29 11:40:31 -07:00
|
|
|
let bottom = (rect.y + rect.height).ceil() as usize;
|
|
|
|
|
if bottom > max_bottom {
|
|
|
|
|
max_bottom = bottom;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-16 08:13:25 -06:00
|
|
|
|
|
|
|
|
let top_deduct = 7 * (space_xxs as usize);
|
|
|
|
|
|
|
|
|
|
self.item_view_size_opt
|
|
|
|
|
.set(self.size_opt.get().map(|s| Size {
|
|
|
|
|
width: s.width,
|
|
|
|
|
height: s.height - top_deduct as f32,
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
let spacer_height = height.checked_sub(max_bottom + top_deduct).unwrap_or(0);
|
2024-02-29 11:40:31 -07:00
|
|
|
if spacer_height > 0 {
|
2024-05-30 18:36:57 -06:00
|
|
|
children.push(
|
2024-10-21 13:51:10 -06:00
|
|
|
widget::container(Space::with_height(Length::Fixed(spacer_height as f32)))
|
2024-05-30 18:36:57 -06:00
|
|
|
.into(),
|
|
|
|
|
)
|
2024-02-29 11:40:31 -07:00
|
|
|
}
|
2024-07-15 11:11:52 +02:00
|
|
|
}
|
2024-01-05 09:36:16 -07:00
|
|
|
}
|
2024-02-29 11:40:31 -07:00
|
|
|
|
2024-04-10 11:41:25 -04:00
|
|
|
(
|
|
|
|
|
(!dnd_items.is_empty()).then(|| {
|
|
|
|
|
let mut dnd_grid = widget::grid()
|
|
|
|
|
.column_spacing(column_spacing)
|
|
|
|
|
.row_spacing(space_xxs)
|
2024-07-15 11:11:52 +02:00
|
|
|
.padding(space_xxs.into());
|
2024-04-10 11:41:25 -04:00
|
|
|
|
|
|
|
|
let mut dnd_item_i = 0;
|
|
|
|
|
for r in drag_n_i..=drag_s_i {
|
|
|
|
|
dnd_grid = dnd_grid.insert_row();
|
|
|
|
|
for c in drag_w_i..=drag_e_i {
|
|
|
|
|
let Some((i, (row, col), item)) = dnd_items.get(dnd_item_i) else {
|
|
|
|
|
break;
|
|
|
|
|
};
|
|
|
|
|
if *row == r && *col == c {
|
|
|
|
|
let buttons = vec![
|
2024-09-20 09:24:03 -06:00
|
|
|
widget::button::custom(
|
2024-04-10 11:41:25 -04:00
|
|
|
widget::icon::icon(item.icon_handle_grid.clone())
|
|
|
|
|
.content_fit(ContentFit::Contain)
|
|
|
|
|
.size(icon_sizes.grid()),
|
|
|
|
|
)
|
|
|
|
|
.on_press(Message::Click(Some(*i)))
|
|
|
|
|
.padding(space_xxxs)
|
2024-10-21 13:51:10 -06:00
|
|
|
.class(button_style(
|
2024-07-11 10:12:50 -06:00
|
|
|
item.selected,
|
2024-10-16 10:22:25 +11:00
|
|
|
item.highlighted,
|
2024-07-11 10:12:50 -06:00
|
|
|
false,
|
|
|
|
|
false,
|
2024-08-20 13:26:10 -06:00
|
|
|
false,
|
2024-07-11 10:12:50 -06:00
|
|
|
)),
|
2024-11-25 03:24:24 +01:00
|
|
|
widget::button::custom(widget::text::body(
|
|
|
|
|
item.display_name.clone(),
|
|
|
|
|
))
|
|
|
|
|
.id(item.button_id.clone())
|
|
|
|
|
.on_press(Message::Click(Some(*i)))
|
|
|
|
|
.padding([0, space_xxxs])
|
|
|
|
|
.class(button_style(
|
|
|
|
|
item.selected,
|
|
|
|
|
item.highlighted,
|
|
|
|
|
true,
|
|
|
|
|
true,
|
|
|
|
|
false,
|
|
|
|
|
)),
|
2024-04-10 11:41:25 -04:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
let mut column = widget::column::with_capacity(buttons.len())
|
2024-10-21 13:51:10 -06:00
|
|
|
.align_x(Alignment::Center)
|
2024-04-10 11:41:25 -04:00
|
|
|
.height(Length::Fixed(item_height as f32))
|
|
|
|
|
.width(Length::Fixed(item_width as f32));
|
|
|
|
|
for button in buttons {
|
|
|
|
|
column = column.push(button)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dnd_grid = dnd_grid.push(column);
|
|
|
|
|
dnd_item_i += 1;
|
|
|
|
|
} else {
|
|
|
|
|
dnd_grid = dnd_grid.push(
|
2024-10-21 13:51:10 -06:00
|
|
|
widget::container(Space::with_height(item_width as f32))
|
2024-04-10 11:41:25 -04:00
|
|
|
.height(Length::Fixed(item_height as f32)),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Element::from(dnd_grid)
|
|
|
|
|
}),
|
2024-07-03 16:31:30 -06:00
|
|
|
mouse_area::MouseArea::new(
|
|
|
|
|
widget::container(widget::column::with_children(children)).width(Length::Fill),
|
|
|
|
|
)
|
|
|
|
|
.on_press(|_| Message::Click(None))
|
|
|
|
|
.on_drag(Message::Drag)
|
|
|
|
|
.on_drag_end(|_| Message::DragEnd(None))
|
|
|
|
|
.show_drag_rect(true)
|
|
|
|
|
.on_release(|_| Message::ClickRelease(None))
|
|
|
|
|
.into(),
|
2024-05-09 13:28:44 -06:00
|
|
|
true,
|
2024-02-29 11:25:46 -07:00
|
|
|
)
|
2024-01-05 09:36:16 -07:00
|
|
|
}
|
|
|
|
|
|
2024-10-21 13:51:10 -06:00
|
|
|
pub fn list_view(&self) -> (Option<Element<'static, Message>>, Element<Message>, bool) {
|
2024-02-29 15:52:13 -07:00
|
|
|
let cosmic_theme::Spacing {
|
2024-07-13 12:10:39 +02:00
|
|
|
space_m,
|
|
|
|
|
space_s,
|
|
|
|
|
space_xxs,
|
|
|
|
|
..
|
2024-02-29 15:52:13 -07:00
|
|
|
} = theme::active().cosmic().spacing;
|
2024-01-05 09:36:16 -07:00
|
|
|
|
2024-02-29 19:47:27 -07:00
|
|
|
let TabConfig {
|
|
|
|
|
show_hidden,
|
|
|
|
|
icon_sizes,
|
2024-05-28 10:40:36 -06:00
|
|
|
..
|
2024-02-29 19:47:27 -07:00
|
|
|
} = self.config;
|
|
|
|
|
|
2024-05-31 09:51:34 -06:00
|
|
|
let size = self.size_opt.get().unwrap_or_else(|| Size::new(0.0, 0.0));
|
2024-02-29 19:47:27 -07:00
|
|
|
//TODO: allow resizing?
|
|
|
|
|
let name_width = 300.0;
|
|
|
|
|
let modified_width = 200.0;
|
|
|
|
|
let size_width = 100.0;
|
|
|
|
|
let condensed = size.width < (name_width + modified_width + size_width);
|
2024-10-09 15:41:10 -06:00
|
|
|
let is_search = matches!(self.location, Location::Search(..));
|
2024-09-17 11:55:54 -06:00
|
|
|
let icon_size = if condensed || is_search {
|
2024-02-29 19:47:27 -07:00
|
|
|
icon_sizes.list_condensed()
|
|
|
|
|
} else {
|
|
|
|
|
icon_sizes.list()
|
|
|
|
|
};
|
|
|
|
|
let row_height = icon_size + 2 * space_xxs;
|
2024-01-29 10:39:12 -07:00
|
|
|
|
2024-02-28 05:18:40 +01:00
|
|
|
let mut children: Vec<Element<_>> = Vec::new();
|
2024-02-29 19:47:27 -07:00
|
|
|
let mut y = 0;
|
2024-01-05 15:32:42 -07:00
|
|
|
|
2024-10-25 13:24:17 +02:00
|
|
|
let rule_padding = theme::active().cosmic().corner_radii.radius_xs[0] as u16;
|
|
|
|
|
|
2024-04-10 11:41:25 -04:00
|
|
|
let items = self.column_sort();
|
|
|
|
|
let mut drag_items = Vec::new();
|
|
|
|
|
if let Some(items) = items {
|
2024-01-05 09:36:16 -07:00
|
|
|
let mut count = 0;
|
|
|
|
|
let mut hidden = 0;
|
2024-02-29 18:58:59 -07:00
|
|
|
for (i, item) in items {
|
2024-10-04 11:52:34 +02:00
|
|
|
if item.hidden && !show_hidden {
|
2024-02-29 13:24:57 -07:00
|
|
|
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;
|
|
|
|
|
}
|
2024-02-29 13:24:57 -07:00
|
|
|
item.pos_opt.set(Some((count, 0)));
|
2024-02-29 12:26:45 -07:00
|
|
|
item.rect_opt.set(Some(Rectangle::new(
|
2024-02-29 15:52:13 -07:00
|
|
|
Point::new(space_m as f32, y as f32),
|
|
|
|
|
Size::new(size.width - (2 * space_m) as f32, row_height as f32),
|
2024-02-29 12:26:45 -07:00
|
|
|
)));
|
2024-01-05 09:36:16 -07:00
|
|
|
|
2024-01-29 11:09:05 -07:00
|
|
|
if count > 0 {
|
2024-08-09 09:59:25 -06:00
|
|
|
children.push(
|
2024-11-11 23:53:25 +01:00
|
|
|
widget::container(horizontal_rule(1))
|
2024-10-25 13:24:17 +02:00
|
|
|
.padding([0, rule_padding])
|
2024-08-09 09:59:25 -06:00
|
|
|
.into(),
|
|
|
|
|
);
|
2024-02-29 12:26:45 -07:00
|
|
|
y += 1;
|
2024-01-29 11:09:05 -07:00
|
|
|
}
|
|
|
|
|
|
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-09-15 16:00:20 -06:00
|
|
|
Ok(time) => format_time(time).to_string(),
|
2024-01-29 10:39:12 -07:00
|
|
|
Err(_) => String::new(),
|
|
|
|
|
},
|
2024-09-20 02:27:23 -04:00
|
|
|
ItemMetadata::Trash { entry, .. } => FormatTime::from_secs(entry.time_deleted)
|
|
|
|
|
.map(|t| t.to_string())
|
|
|
|
|
.unwrap_or_default(),
|
2024-09-13 15:13:37 -06:00
|
|
|
_ => 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() {
|
2024-09-13 15:13:37 -06:00
|
|
|
//TODO: translate
|
|
|
|
|
if *children == 1 {
|
|
|
|
|
format!("{} item", children)
|
|
|
|
|
} else {
|
|
|
|
|
format!("{} items", children)
|
|
|
|
|
}
|
2024-01-29 10:39:12 -07:00
|
|
|
} 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-09-13 15:13:37 -06:00
|
|
|
ItemMetadata::SimpleDir { entries } => {
|
|
|
|
|
//TODO: translate
|
|
|
|
|
if *entries == 1 {
|
|
|
|
|
format!("{} item", entries)
|
|
|
|
|
} else {
|
|
|
|
|
format!("{} items", entries)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ItemMetadata::SimpleFile { size } => format_size(*size),
|
2024-01-29 10:39:12 -07:00
|
|
|
};
|
|
|
|
|
|
2024-02-29 19:47:27 -07:00
|
|
|
let row = if condensed {
|
|
|
|
|
widget::row::with_children(vec![
|
|
|
|
|
widget::icon::icon(item.icon_handle_list_condensed.clone())
|
|
|
|
|
.content_fit(ContentFit::Contain)
|
|
|
|
|
.size(icon_size)
|
|
|
|
|
.into(),
|
|
|
|
|
widget::column::with_children(vec![
|
2024-11-25 03:24:24 +01:00
|
|
|
widget::text::body(item.display_name.clone()).into(),
|
2024-02-29 19:47:27 -07:00
|
|
|
//TODO: translate?
|
2024-07-26 12:29:29 +02:00
|
|
|
widget::text::caption(format!("{} - {}", modified_text, size_text))
|
|
|
|
|
.into(),
|
2024-02-29 19:47:27 -07:00
|
|
|
])
|
|
|
|
|
.into(),
|
|
|
|
|
])
|
2024-09-11 19:02:47 +02:00
|
|
|
.height(Length::Fixed(row_height as f32))
|
2024-10-21 13:51:10 -06:00
|
|
|
.align_y(Alignment::Center)
|
2024-02-29 19:47:27 -07:00
|
|
|
.spacing(space_xxs)
|
2024-09-17 11:55:54 -06:00
|
|
|
} else if is_search {
|
|
|
|
|
widget::row::with_children(vec![
|
|
|
|
|
widget::icon::icon(item.icon_handle_list_condensed.clone())
|
|
|
|
|
.content_fit(ContentFit::Contain)
|
|
|
|
|
.size(icon_size)
|
|
|
|
|
.into(),
|
|
|
|
|
widget::column::with_children(vec![
|
2024-11-25 03:24:24 +01:00
|
|
|
widget::text::body(item.display_name.clone()).into(),
|
2024-09-17 11:55:54 -06:00
|
|
|
widget::text::caption(match item.path_opt() {
|
|
|
|
|
Some(path) => path.display().to_string(),
|
|
|
|
|
None => String::new(),
|
|
|
|
|
})
|
|
|
|
|
.into(),
|
|
|
|
|
])
|
|
|
|
|
.width(Length::Fill)
|
|
|
|
|
.into(),
|
2024-11-25 03:24:24 +01:00
|
|
|
widget::text::body(modified_text.clone())
|
2024-09-17 11:55:54 -06:00
|
|
|
.width(Length::Fixed(modified_width))
|
|
|
|
|
.into(),
|
2024-11-25 03:24:24 +01:00
|
|
|
widget::text::body(size_text.clone())
|
2024-09-17 11:55:54 -06:00
|
|
|
.width(Length::Fixed(size_width))
|
|
|
|
|
.into(),
|
|
|
|
|
])
|
|
|
|
|
.height(Length::Fixed(row_height as f32))
|
2024-10-21 13:51:10 -06:00
|
|
|
.align_y(Alignment::Center)
|
2024-09-17 11:55:54 -06:00
|
|
|
.spacing(space_xxs)
|
2024-02-29 19:47:27 -07:00
|
|
|
} else {
|
2024-01-05 15:10:46 -07:00
|
|
|
widget::row::with_children(vec![
|
2024-02-22 21:46:25 -07:00
|
|
|
widget::icon::icon(item.icon_handle_list.clone())
|
|
|
|
|
.content_fit(ContentFit::Contain)
|
2024-02-29 19:47:27 -07:00
|
|
|
.size(icon_size)
|
2024-02-22 21:46:25 -07:00
|
|
|
.into(),
|
2024-11-25 03:24:24 +01:00
|
|
|
widget::text::body(item.display_name.clone())
|
2024-08-20 13:26:10 -06:00
|
|
|
.width(Length::Fill)
|
|
|
|
|
.into(),
|
2024-11-25 03:24:24 +01:00
|
|
|
widget::text::body(modified_text.clone())
|
2024-02-29 19:47:27 -07:00
|
|
|
.width(Length::Fixed(modified_width))
|
|
|
|
|
.into(),
|
2024-11-25 03:24:24 +01:00
|
|
|
widget::text::body(size_text.clone())
|
2024-02-29 19:47:27 -07:00
|
|
|
.width(Length::Fixed(size_width))
|
|
|
|
|
.into(),
|
2024-01-05 15:10:46 -07:00
|
|
|
])
|
2024-09-11 19:02:47 +02:00
|
|
|
.height(Length::Fixed(row_height as f32))
|
2024-10-21 13:51:10 -06:00
|
|
|
.align_y(Alignment::Center)
|
2024-02-29 19:47:27 -07:00
|
|
|
.spacing(space_xxs)
|
|
|
|
|
};
|
|
|
|
|
|
2024-04-10 11:41:25 -04:00
|
|
|
let button = |row| {
|
2024-06-30 14:57:37 -06:00
|
|
|
let mouse_area = crate::mouse_area::MouseArea::new(
|
2024-09-20 09:24:03 -06:00
|
|
|
widget::button::custom(row)
|
2024-04-10 11:41:25 -04:00
|
|
|
.width(Length::Fill)
|
|
|
|
|
.id(item.button_id.clone())
|
2024-09-11 19:02:47 +02:00
|
|
|
.padding([0, space_xxs])
|
2024-10-16 10:22:25 +11:00
|
|
|
.class(button_style(
|
|
|
|
|
item.selected,
|
|
|
|
|
item.highlighted,
|
|
|
|
|
true,
|
2024-10-25 13:24:17 +02:00
|
|
|
true,
|
2024-10-16 10:22:25 +11:00
|
|
|
false,
|
|
|
|
|
)),
|
2024-04-10 11:41:25 -04:00
|
|
|
)
|
|
|
|
|
.on_press(move |_| Message::Click(Some(i)))
|
|
|
|
|
.on_double_click(move |_| Message::DoubleClick(Some(i)))
|
|
|
|
|
.on_release(move |_| Message::ClickRelease(Some(i)))
|
2024-10-16 10:22:25 +11:00
|
|
|
.on_middle_press(move |_| Message::MiddleClick(i))
|
|
|
|
|
.on_enter(move || Message::HighlightActivate(i))
|
|
|
|
|
.on_exit(move || Message::HighlightDeactivate(i));
|
2024-06-30 14:57:37 -06:00
|
|
|
|
|
|
|
|
if self.context_menu.is_some() {
|
|
|
|
|
mouse_area
|
|
|
|
|
} else {
|
|
|
|
|
mouse_area.on_right_press_no_capture(move |_point_opt| {
|
|
|
|
|
Message::RightClick(Some(i))
|
|
|
|
|
})
|
|
|
|
|
}
|
2024-04-10 11:41:25 -04:00
|
|
|
};
|
|
|
|
|
|
2024-06-30 14:57:37 -06:00
|
|
|
let button_row = button(row.into());
|
2024-09-13 09:35:37 -06:00
|
|
|
let button_row: Element<_> =
|
|
|
|
|
if item.metadata.is_dir() && item.location_opt.is_some() {
|
2024-09-20 14:13:04 -06:00
|
|
|
self.dnd_dest(item.location_opt.as_ref().unwrap(), button_row)
|
2024-04-10 11:41:25 -04:00
|
|
|
} else {
|
2024-09-13 09:35:37 -06:00
|
|
|
button_row.into()
|
|
|
|
|
};
|
2024-04-10 11:41:25 -04:00
|
|
|
|
|
|
|
|
if item.selected || !drag_items.is_empty() {
|
|
|
|
|
let dnd_row = if !item.selected {
|
2024-10-21 13:51:10 -06:00
|
|
|
Element::from(Space::with_height(Length::Fixed(row_height as f32)))
|
2024-04-10 11:41:25 -04:00
|
|
|
} else if condensed {
|
|
|
|
|
widget::row::with_children(vec![
|
|
|
|
|
widget::icon::icon(item.icon_handle_list_condensed.clone())
|
|
|
|
|
.content_fit(ContentFit::Contain)
|
|
|
|
|
.size(icon_size)
|
|
|
|
|
.into(),
|
|
|
|
|
widget::column::with_children(vec![
|
2024-11-25 03:24:24 +01:00
|
|
|
widget::text::body(item.display_name.clone()).into(),
|
2024-04-10 11:41:25 -04:00
|
|
|
//TODO: translate?
|
2024-11-25 03:24:24 +01:00
|
|
|
widget::text::body(format!("{} - {}", modified_text, size_text))
|
|
|
|
|
.into(),
|
2024-04-10 11:41:25 -04:00
|
|
|
])
|
2024-01-05 15:10:46 -07:00
|
|
|
.into(),
|
2024-04-10 11:41:25 -04:00
|
|
|
])
|
2024-10-21 13:51:10 -06:00
|
|
|
.align_y(Alignment::Center)
|
2024-04-10 11:41:25 -04:00
|
|
|
.spacing(space_xxs)
|
|
|
|
|
.into()
|
2024-09-17 11:55:54 -06:00
|
|
|
} else if is_search {
|
|
|
|
|
widget::row::with_children(vec![
|
|
|
|
|
widget::icon::icon(item.icon_handle_list_condensed.clone())
|
|
|
|
|
.content_fit(ContentFit::Contain)
|
|
|
|
|
.size(icon_size)
|
|
|
|
|
.into(),
|
|
|
|
|
widget::column::with_children(vec![
|
2024-11-25 03:24:24 +01:00
|
|
|
widget::text::body(item.display_name.clone()).into(),
|
2024-09-17 11:55:54 -06:00
|
|
|
widget::text::caption(match item.path_opt() {
|
|
|
|
|
Some(path) => path.display().to_string(),
|
|
|
|
|
None => String::new(),
|
|
|
|
|
})
|
|
|
|
|
.into(),
|
|
|
|
|
])
|
|
|
|
|
.width(Length::Fill)
|
|
|
|
|
.into(),
|
2024-11-25 03:24:24 +01:00
|
|
|
widget::text::body(modified_text.clone())
|
2024-09-17 11:55:54 -06:00
|
|
|
.width(Length::Fixed(modified_width))
|
|
|
|
|
.into(),
|
2024-11-25 03:24:24 +01:00
|
|
|
widget::text::body(size_text.clone())
|
2024-09-17 11:55:54 -06:00
|
|
|
.width(Length::Fixed(size_width))
|
|
|
|
|
.into(),
|
|
|
|
|
])
|
2024-10-21 13:51:10 -06:00
|
|
|
.align_y(Alignment::Center)
|
2024-09-17 11:55:54 -06:00
|
|
|
.spacing(space_xxs)
|
|
|
|
|
.into()
|
2024-04-10 11:41:25 -04:00
|
|
|
} else {
|
|
|
|
|
widget::row::with_children(vec![
|
|
|
|
|
widget::icon::icon(item.icon_handle_list.clone())
|
|
|
|
|
.content_fit(ContentFit::Contain)
|
|
|
|
|
.size(icon_size)
|
|
|
|
|
.into(),
|
2024-11-25 03:24:24 +01:00
|
|
|
widget::text::body(item.display_name.clone())
|
2024-08-20 13:26:10 -06:00
|
|
|
.width(Length::Fill)
|
|
|
|
|
.into(),
|
2024-04-10 11:41:25 -04:00
|
|
|
widget::text(modified_text)
|
|
|
|
|
.width(Length::Fixed(modified_width))
|
|
|
|
|
.into(),
|
2024-11-25 03:24:24 +01:00
|
|
|
widget::text::body(size_text)
|
2024-04-10 11:41:25 -04:00
|
|
|
.width(Length::Fixed(size_width))
|
|
|
|
|
.into(),
|
|
|
|
|
])
|
2024-10-21 13:51:10 -06:00
|
|
|
.align_y(Alignment::Center)
|
2024-04-10 11:41:25 -04:00
|
|
|
.spacing(space_xxs)
|
|
|
|
|
.into()
|
|
|
|
|
};
|
|
|
|
|
if item.selected {
|
|
|
|
|
drag_items.push(
|
|
|
|
|
widget::container(button(dnd_row))
|
|
|
|
|
.width(Length::Shrink)
|
|
|
|
|
.into(),
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
drag_items.push(dnd_row);
|
|
|
|
|
}
|
2024-01-05 15:10:46 -07:00
|
|
|
}
|
2024-04-10 11:41:25 -04:00
|
|
|
|
2024-01-05 08:55:18 -07:00
|
|
|
count += 1;
|
2024-02-29 12:26:45 -07:00
|
|
|
y += row_height;
|
2024-04-10 11:41:25 -04:00
|
|
|
children.push(button_row);
|
2024-01-03 15:27:32 -07:00
|
|
|
}
|
|
|
|
|
|
2024-01-05 08:55:18 -07:00
|
|
|
if count == 0 {
|
2024-05-09 13:28:44 -06:00
|
|
|
return (None, self.empty_view(hidden > 0), false);
|
2024-01-05 08:55:18 -07:00
|
|
|
}
|
2024-01-03 15:27:32 -07:00
|
|
|
}
|
2024-04-10 11:41:25 -04:00
|
|
|
//TODO: HACK If we don't reach the bottom of the view, go ahead and add a spacer to do that
|
2024-07-15 11:11:52 +02:00
|
|
|
{
|
2024-09-17 11:55:54 -06:00
|
|
|
let top_deduct = (if condensed || is_search { 6 } else { 9 }) * space_xxs;
|
2024-08-16 08:13:25 -06:00
|
|
|
|
|
|
|
|
self.item_view_size_opt
|
|
|
|
|
.set(self.size_opt.get().map(|s| Size {
|
|
|
|
|
width: s.width,
|
|
|
|
|
height: s.height - top_deduct as f32,
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
let spacer_height = size.height - y as f32 - top_deduct as f32;
|
|
|
|
|
if spacer_height > 0. {
|
2024-10-21 13:51:10 -06:00
|
|
|
children.push(
|
|
|
|
|
widget::container(Space::with_height(Length::Fixed(spacer_height))).into(),
|
|
|
|
|
);
|
2024-04-10 11:41:25 -04:00
|
|
|
}
|
2024-07-15 11:11:52 +02:00
|
|
|
}
|
2024-10-21 13:51:10 -06:00
|
|
|
let drag_col = (!drag_items.is_empty())
|
|
|
|
|
.then(|| Element::from(widget::column::with_children(drag_items)));
|
2024-04-10 11:41:25 -04:00
|
|
|
|
|
|
|
|
(
|
|
|
|
|
drag_col,
|
|
|
|
|
mouse_area::MouseArea::new(
|
2024-07-31 15:58:17 +02:00
|
|
|
widget::column::with_children(children).padding([0, space_s]),
|
2024-04-10 11:41:25 -04:00
|
|
|
)
|
|
|
|
|
.with_id(Id::new("list-view"))
|
2024-05-27 14:55:37 -06:00
|
|
|
.on_press(|_| Message::Click(None))
|
2024-04-10 11:41:25 -04:00
|
|
|
.on_drag(Message::Drag)
|
|
|
|
|
.on_drag_end(|_| Message::DragEnd(None))
|
|
|
|
|
.show_drag_rect(true)
|
|
|
|
|
.on_release(|_| Message::ClickRelease(None))
|
|
|
|
|
.into(),
|
2024-05-09 13:28:44 -06:00
|
|
|
true,
|
2024-04-10 11:41:25 -04:00
|
|
|
)
|
2024-01-05 09:36:16 -07:00
|
|
|
}
|
|
|
|
|
|
2024-05-31 09:51:34 -06:00
|
|
|
pub fn view_responsive(
|
|
|
|
|
&self,
|
|
|
|
|
key_binds: &HashMap<KeyBind, Action>,
|
|
|
|
|
size: Size,
|
|
|
|
|
) -> Element<Message> {
|
|
|
|
|
// Update cached size
|
|
|
|
|
self.size_opt.set(Some(size));
|
|
|
|
|
|
2024-09-12 15:54:54 -06:00
|
|
|
let cosmic_theme::Spacing {
|
|
|
|
|
space_xxs,
|
|
|
|
|
space_xs,
|
|
|
|
|
..
|
|
|
|
|
} = theme::active().cosmic().spacing;
|
|
|
|
|
|
2024-08-20 13:26:10 -06:00
|
|
|
let location_view_opt = if matches!(self.mode, Mode::Desktop) {
|
|
|
|
|
None
|
|
|
|
|
} else {
|
|
|
|
|
Some(self.location_view())
|
|
|
|
|
};
|
2024-05-29 23:33:12 +02:00
|
|
|
let (drag_list, mut item_view, can_scroll) = match self.config.view {
|
2024-02-29 12:26:45 -07:00
|
|
|
View::Grid => self.grid_view(),
|
|
|
|
|
View::List => self.list_view(),
|
2024-02-26 12:05:29 -07:00
|
|
|
};
|
2024-04-10 11:41:25 -04:00
|
|
|
item_view = widget::container(item_view).width(Length::Fill).into();
|
|
|
|
|
let files = self
|
|
|
|
|
.items_opt
|
|
|
|
|
.as_ref()
|
|
|
|
|
.map(|items| {
|
|
|
|
|
items
|
|
|
|
|
.iter()
|
|
|
|
|
.filter(|item| item.selected)
|
2024-09-13 09:35:37 -06:00
|
|
|
.filter_map(|item| item.path_opt().map(|x| x.clone()))
|
|
|
|
|
.collect::<Vec<PathBuf>>()
|
2024-04-10 11:41:25 -04:00
|
|
|
})
|
|
|
|
|
.unwrap_or_default();
|
2024-10-21 13:51:10 -06:00
|
|
|
let item_view =
|
|
|
|
|
DndSource::<Message, ClipboardCopy>::with_id(item_view, Id::new("tab-view"));
|
2024-05-26 22:03:43 -06:00
|
|
|
|
|
|
|
|
let item_view = match drag_list {
|
|
|
|
|
Some(drag_list) if self.selected_clicked => {
|
2024-10-21 13:51:10 -06:00
|
|
|
let drag_list = ArcElementWrapper::<Message>(Arc::new(Mutex::new(drag_list)));
|
2024-05-26 22:03:43 -06:00
|
|
|
item_view
|
|
|
|
|
.drag_content(move || {
|
|
|
|
|
ClipboardCopy::new(crate::clipboard::ClipboardKind::Copy, &files)
|
|
|
|
|
})
|
2025-01-14 08:54:43 -07:00
|
|
|
.drag_icon(move |v| {
|
2024-10-21 13:51:10 -06:00
|
|
|
let state: tree::State = Widget::<Message, _, _>::state(&drag_list);
|
2025-01-14 08:54:43 -07:00
|
|
|
(Element::from(drag_list.clone()).map(|_m| ()), state, v)
|
2024-05-26 22:03:43 -06:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
_ => item_view,
|
2024-04-10 11:41:25 -04:00
|
|
|
};
|
2024-05-26 22:03:43 -06:00
|
|
|
|
2024-04-10 11:41:25 -04:00
|
|
|
let tab_location = self.location.clone();
|
|
|
|
|
let mut mouse_area = mouse_area::MouseArea::new(item_view)
|
|
|
|
|
.on_press(move |_point_opt| Message::Click(None))
|
|
|
|
|
.on_release(|_| Message::ClickRelease(None))
|
2024-09-20 14:34:44 -06:00
|
|
|
//TODO: better way to keep focused item in view
|
|
|
|
|
.on_resize(|_| Message::ScrollToFocus)
|
2024-04-10 11:41:25 -04:00
|
|
|
.on_back_press(move |_point_opt| Message::GoPrevious)
|
2024-09-14 02:11:06 -04:00
|
|
|
.on_forward_press(move |_point_opt| Message::GoNext)
|
|
|
|
|
.on_scroll(respond_to_scroll_direction);
|
2024-04-10 11:41:25 -04:00
|
|
|
|
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 {
|
2024-06-30 14:57:37 -06:00
|
|
|
mouse_area = mouse_area.on_right_press(Message::ContextMenu);
|
2024-02-26 12:05:29 -07:00
|
|
|
}
|
2024-04-10 11:41:25 -04:00
|
|
|
|
2024-02-27 09:52:39 -07:00
|
|
|
let mut popover = widget::popover(mouse_area);
|
2024-04-10 11:41:25 -04:00
|
|
|
|
2024-02-27 09:52:39 -07:00
|
|
|
if let Some(point) = self.context_menu {
|
2024-02-27 10:26:56 -07:00
|
|
|
popover = popover
|
2024-02-28 15:46:23 -07:00
|
|
|
.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
|
|
|
}
|
2024-05-09 13:28:44 -06:00
|
|
|
let mut tab_column = widget::column::with_capacity(3);
|
2024-08-20 13:26:10 -06:00
|
|
|
if let Some(location_view) = location_view_opt {
|
|
|
|
|
tab_column = tab_column.push(location_view);
|
|
|
|
|
}
|
2024-05-09 13:28:44 -06:00
|
|
|
if can_scroll {
|
|
|
|
|
tab_column = tab_column.push(
|
2024-05-31 09:51:34 -06:00
|
|
|
widget::scrollable(popover)
|
|
|
|
|
.id(self.scrollable_id.clone())
|
|
|
|
|
.on_scroll(Message::Scroll)
|
|
|
|
|
.width(Length::Fill)
|
|
|
|
|
.height(Length::Fill),
|
2024-05-09 13:28:44 -06:00
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
tab_column = tab_column.push(popover);
|
|
|
|
|
}
|
2024-09-12 15:54:54 -06:00
|
|
|
match &self.location {
|
|
|
|
|
Location::Trash => {
|
|
|
|
|
if let Some(items) = self.items_opt() {
|
|
|
|
|
if !items.is_empty() {
|
|
|
|
|
tab_column = tab_column.push(
|
|
|
|
|
widget::layer_container(widget::row::with_children(vec![
|
2024-10-21 13:51:10 -06:00
|
|
|
widget::horizontal_space().into(),
|
2024-09-12 15:54:54 -06:00
|
|
|
widget::button::standard(fl!("empty-trash"))
|
|
|
|
|
.on_press(Message::EmptyTrash)
|
|
|
|
|
.into(),
|
|
|
|
|
]))
|
|
|
|
|
.padding([space_xxs, space_xs])
|
|
|
|
|
.layer(cosmic_theme::Layer::Primary),
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-05-09 13:24:06 -06:00
|
|
|
}
|
|
|
|
|
}
|
2024-10-02 14:53:24 -06:00
|
|
|
Location::Network(uri, _display_name) if uri == "network:///" => {
|
2024-09-12 15:54:54 -06:00
|
|
|
tab_column = tab_column.push(
|
|
|
|
|
widget::layer_container(widget::row::with_children(vec![
|
2024-10-21 13:51:10 -06:00
|
|
|
widget::horizontal_space().into(),
|
2024-09-12 15:54:54 -06:00
|
|
|
widget::button::standard(fl!("add-network-drive"))
|
|
|
|
|
.on_press(Message::AddNetworkDrive)
|
|
|
|
|
.into(),
|
|
|
|
|
]))
|
|
|
|
|
.padding([space_xxs, space_xs])
|
|
|
|
|
.layer(cosmic_theme::Layer::Primary),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
_ => {}
|
2024-05-09 13:24:06 -06:00
|
|
|
}
|
|
|
|
|
let mut tab_view = widget::container(tab_column)
|
|
|
|
|
.height(Length::Fill)
|
|
|
|
|
.width(Length::Fill);
|
2024-04-10 11:41:25 -04:00
|
|
|
|
|
|
|
|
if self.dnd_hovered.as_ref().map(|(l, _)| l) == Some(&tab_location) {
|
2024-10-21 13:51:10 -06:00
|
|
|
tab_view = tab_view.style(|t| {
|
|
|
|
|
let mut a = widget::container::Style::default();
|
2024-04-10 11:41:25 -04:00
|
|
|
let c = t.cosmic();
|
|
|
|
|
a.border = cosmic::iced_core::Border {
|
|
|
|
|
color: (c.accent_color()).into(),
|
|
|
|
|
width: 1.,
|
|
|
|
|
radius: c.radius_0().into(),
|
|
|
|
|
};
|
|
|
|
|
a
|
2024-10-21 13:51:10 -06:00
|
|
|
});
|
2024-04-10 11:41:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let tab_location_2 = self.location.clone();
|
|
|
|
|
let tab_location_3 = self.location.clone();
|
|
|
|
|
let dnd_dest = DndDestination::for_data(tab_view, move |data, action| {
|
|
|
|
|
if let Some(mut data) = data {
|
|
|
|
|
if action == DndAction::Copy {
|
|
|
|
|
Message::Drop(Some((tab_location.clone(), data)))
|
|
|
|
|
} else if action == DndAction::Move {
|
|
|
|
|
data.kind = ClipboardKind::Cut;
|
|
|
|
|
Message::Drop(Some((tab_location.clone(), data)))
|
|
|
|
|
} else {
|
|
|
|
|
log::warn!("unsupported action: {:?}", action);
|
|
|
|
|
Message::Drop(None)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Message::Drop(None)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.on_enter(move |_, _, _| Message::DndEnter(tab_location_2.clone()))
|
|
|
|
|
.on_leave(move || Message::DndLeave(tab_location_3.clone()));
|
|
|
|
|
|
|
|
|
|
dnd_dest.into()
|
2024-01-03 15:27:32 -07:00
|
|
|
}
|
2024-02-22 16:17:39 -07:00
|
|
|
|
2024-05-31 09:51:34 -06:00
|
|
|
pub fn view<'a>(&'a self, key_binds: &'a HashMap<KeyBind, Action>) -> Element<Message> {
|
|
|
|
|
widget::responsive(|size| self.view_responsive(key_binds, size)).into()
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-15 15:37:31 -07:00
|
|
|
pub fn subscription(&self, preview: bool) -> Subscription<Message> {
|
2024-10-09 15:41:10 -06:00
|
|
|
let Some(items) = &self.items_opt else {
|
|
|
|
|
return Subscription::none();
|
|
|
|
|
};
|
2024-02-29 12:26:45 -07:00
|
|
|
|
2024-10-09 15:41:10 -06:00
|
|
|
//TODO: how many thumbnail loads should be in flight at once?
|
|
|
|
|
let jobs = 8;
|
2024-10-09 18:55:09 -06:00
|
|
|
let mut subscriptions = Vec::with_capacity(jobs + 1);
|
2024-10-09 15:41:10 -06:00
|
|
|
|
|
|
|
|
//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),
|
2024-02-29 12:26:45 -07:00
|
|
|
};
|
2024-10-09 15:41:10 -06:00
|
|
|
let size = self.size_opt.get().unwrap_or_else(|| Size::new(0.0, 0.0));
|
|
|
|
|
Rectangle::new(point, size)
|
|
|
|
|
};
|
2024-02-29 12:26:45 -07:00
|
|
|
|
2024-10-09 15:41:10 -06:00
|
|
|
//TODO: HACK to ensure positions are up to date since subscription runs before view
|
|
|
|
|
match self.config.view {
|
|
|
|
|
View::Grid => _ = self.grid_view(),
|
|
|
|
|
View::List => _ = self.list_view(),
|
|
|
|
|
};
|
2024-02-29 12:26:45 -07:00
|
|
|
|
2024-10-09 15:41:10 -06:00
|
|
|
for item in items.iter() {
|
|
|
|
|
if item.thumbnail_opt.is_some() {
|
2024-10-09 18:55:09 -06:00
|
|
|
// Skip items that already have a mime type and thumbnail
|
2024-10-09 15:41:10 -06:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match item.rect_opt.get() {
|
|
|
|
|
Some(rect) => {
|
|
|
|
|
if !rect.intersects(&visible_rect) {
|
|
|
|
|
// Skip items that are not visible
|
2024-02-29 12:26:45 -07:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-09 15:41:10 -06:00
|
|
|
None => {
|
|
|
|
|
// Skip items with no determined rect (this should include hidden items)
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-29 12:26:45 -07:00
|
|
|
|
2024-10-09 18:55:09 -06:00
|
|
|
let Some(path) = item.path_opt().map(|path| path.to_path_buf()) else {
|
|
|
|
|
continue;
|
|
|
|
|
};
|
2024-10-10 10:05:48 -06:00
|
|
|
let ItemMetadata::Path { metadata, .. } = item.metadata.clone() else {
|
|
|
|
|
continue;
|
|
|
|
|
};
|
2024-10-09 18:55:09 -06:00
|
|
|
let mime = item.mime.clone();
|
2024-11-15 23:24:52 +01:00
|
|
|
|
2024-10-21 13:51:10 -06:00
|
|
|
subscriptions.push(Subscription::run_with_id(
|
2024-11-15 23:24:52 +01:00
|
|
|
("thumbnail", path.clone()),
|
2024-10-21 13:51:10 -06:00
|
|
|
stream::channel(1, |mut output| async move {
|
2024-10-09 18:55:09 -06:00
|
|
|
let message = {
|
|
|
|
|
let path = path.clone();
|
|
|
|
|
tokio::task::spawn_blocking(move || {
|
2024-10-09 20:18:43 -06:00
|
|
|
let start = Instant::now();
|
2024-10-10 10:05:48 -06:00
|
|
|
let thumbnail =
|
|
|
|
|
ItemThumbnail::new(&path, metadata, mime, THUMBNAIL_SIZE);
|
2024-10-09 15:41:10 -06:00
|
|
|
log::debug!("thumbnailed {:?} in {:?}", path, start.elapsed());
|
2024-10-09 18:55:09 -06:00
|
|
|
Message::Thumbnail(path.clone(), thumbnail)
|
2024-10-09 15:41:10 -06:00
|
|
|
})
|
|
|
|
|
.await
|
2024-10-09 18:55:09 -06:00
|
|
|
.unwrap()
|
|
|
|
|
};
|
2024-03-20 09:56:54 -06:00
|
|
|
|
2024-10-09 18:55:09 -06:00
|
|
|
match output.send(message).await {
|
|
|
|
|
Ok(()) => {}
|
|
|
|
|
Err(err) => {
|
2024-11-15 23:24:52 +01:00
|
|
|
log::warn!("failed to send thumbnail for {:?}: {}", &path, err);
|
2024-10-09 15:41:10 -06:00
|
|
|
}
|
2024-10-09 18:55:09 -06:00
|
|
|
}
|
2024-02-22 16:17:39 -07:00
|
|
|
|
2024-10-09 18:55:09 -06:00
|
|
|
std::future::pending().await
|
2024-10-21 13:51:10 -06:00
|
|
|
}),
|
2024-10-09 18:55:09 -06:00
|
|
|
));
|
2024-02-22 16:17:39 -07:00
|
|
|
|
2024-10-09 15:41:10 -06:00
|
|
|
if subscriptions.len() >= jobs {
|
|
|
|
|
break;
|
2024-02-22 16:17:39 -07:00
|
|
|
}
|
|
|
|
|
}
|
2024-10-09 18:55:09 -06:00
|
|
|
|
2024-11-15 15:37:31 -07:00
|
|
|
if preview {
|
|
|
|
|
// Load directory size for selected items
|
2024-11-15 17:30:25 -07:00
|
|
|
if let Some(item) = items
|
2024-11-15 15:37:31 -07:00
|
|
|
.iter()
|
2024-11-15 17:30:25 -07:00
|
|
|
.filter(|item| item.selected)
|
|
|
|
|
.next()
|
|
|
|
|
.or(self.parent_item_opt.as_ref())
|
2024-11-15 15:37:31 -07:00
|
|
|
{
|
|
|
|
|
// Item must have a path
|
2024-11-15 17:30:25 -07:00
|
|
|
if let Some(path) = item.path_opt().map(|path| path.to_path_buf()) {
|
|
|
|
|
// Item must be calculating directory size
|
|
|
|
|
if let DirSize::Calculating(controller) = &item.dir_size {
|
|
|
|
|
let controller = controller.clone();
|
|
|
|
|
subscriptions.push(Subscription::run_with_id(
|
|
|
|
|
("dir_size", path.clone()),
|
|
|
|
|
stream::channel(1, |mut output| async move {
|
|
|
|
|
let message = {
|
|
|
|
|
let path = path.clone();
|
|
|
|
|
tokio::task::spawn_blocking(move || {
|
|
|
|
|
let start = Instant::now();
|
|
|
|
|
match calculate_dir_size(&path, controller) {
|
|
|
|
|
Ok(size) => {
|
|
|
|
|
log::debug!(
|
|
|
|
|
"calculated directory size of {:?} in {:?}",
|
|
|
|
|
path,
|
|
|
|
|
start.elapsed()
|
|
|
|
|
);
|
|
|
|
|
Message::DirectorySize(
|
|
|
|
|
path.clone(),
|
|
|
|
|
DirSize::Directory(size),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::warn!(
|
|
|
|
|
"failed to calculate directory size of {:?}: {}",
|
|
|
|
|
path,
|
|
|
|
|
err
|
|
|
|
|
);
|
|
|
|
|
Message::DirectorySize(
|
|
|
|
|
path.clone(),
|
|
|
|
|
DirSize::Error(err),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.await
|
|
|
|
|
.unwrap()
|
|
|
|
|
};
|
2024-11-15 23:24:52 +01:00
|
|
|
|
2024-11-15 17:30:25 -07:00
|
|
|
match output.send(message).await {
|
|
|
|
|
Ok(()) => {}
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::warn!(
|
|
|
|
|
"failed to send directory size for {:?}: {}",
|
|
|
|
|
&path,
|
|
|
|
|
err
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-15 23:24:52 +01:00
|
|
|
|
2024-11-15 17:30:25 -07:00
|
|
|
std::future::pending().await
|
|
|
|
|
}),
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-15 15:37:31 -07:00
|
|
|
}
|
2024-11-15 23:24:52 +01:00
|
|
|
}
|
|
|
|
|
|
2024-10-09 18:55:09 -06:00
|
|
|
// Load search items incrementally
|
2024-10-09 21:02:12 -06:00
|
|
|
if let Location::Search(path, term, show_hidden, start) = &self.location {
|
2024-10-09 18:55:09 -06:00
|
|
|
let location = self.location.clone();
|
|
|
|
|
let path = path.clone();
|
|
|
|
|
let term = term.clone();
|
2024-10-09 21:02:12 -06:00
|
|
|
let show_hidden = *show_hidden;
|
2024-10-09 18:55:09 -06:00
|
|
|
let start = start.clone();
|
2024-10-21 13:51:10 -06:00
|
|
|
subscriptions.push(Subscription::run_with_id(
|
2024-10-09 18:55:09 -06:00
|
|
|
location.clone(),
|
2024-10-21 13:51:10 -06:00
|
|
|
stream::channel(2, move |mut output| async move {
|
2024-10-09 18:55:09 -06:00
|
|
|
//TODO: optimal size?
|
|
|
|
|
let (results_tx, results_rx) = mpsc::channel(65536);
|
|
|
|
|
|
|
|
|
|
let ready = Arc::new(atomic::AtomicBool::new(false));
|
2024-10-09 20:18:43 -06:00
|
|
|
let last_modified_opt = Arc::new(RwLock::new(None));
|
2024-10-09 18:55:09 -06:00
|
|
|
output
|
|
|
|
|
.send(Message::SearchContext(
|
|
|
|
|
location.clone(),
|
|
|
|
|
SearchContextWrapper(Some(SearchContext {
|
|
|
|
|
results_rx,
|
|
|
|
|
ready: ready.clone(),
|
2024-10-09 20:18:43 -06:00
|
|
|
last_modified_opt: last_modified_opt.clone(),
|
2024-10-09 18:55:09 -06:00
|
|
|
})),
|
|
|
|
|
))
|
|
|
|
|
.await
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
let output = Arc::new(tokio::sync::Mutex::new(output));
|
|
|
|
|
{
|
|
|
|
|
let output = output.clone();
|
|
|
|
|
tokio::task::spawn_blocking(move || {
|
2024-10-09 21:02:12 -06:00
|
|
|
scan_search(
|
|
|
|
|
&path,
|
|
|
|
|
&term,
|
|
|
|
|
show_hidden,
|
|
|
|
|
move |path, name, metadata| -> bool {
|
|
|
|
|
// Don't send if the result is too old
|
|
|
|
|
if let Some(last_modified) = *last_modified_opt.read().unwrap()
|
|
|
|
|
{
|
|
|
|
|
if let Ok(modified) = metadata.modified() {
|
|
|
|
|
if modified < last_modified {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2024-10-09 20:18:43 -06:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-09 21:02:12 -06:00
|
|
|
match results_tx.blocking_send((
|
|
|
|
|
path.to_path_buf(),
|
|
|
|
|
name.to_string(),
|
|
|
|
|
metadata,
|
|
|
|
|
)) {
|
|
|
|
|
Ok(()) => {
|
|
|
|
|
if !ready.swap(true, atomic::Ordering::SeqCst) {
|
|
|
|
|
// Wake up update method
|
|
|
|
|
futures::executor::block_on(async {
|
|
|
|
|
output
|
|
|
|
|
.lock()
|
|
|
|
|
.await
|
|
|
|
|
.send(Message::SearchReady(false))
|
|
|
|
|
.await
|
|
|
|
|
})
|
|
|
|
|
.is_ok()
|
|
|
|
|
} else {
|
|
|
|
|
true
|
|
|
|
|
}
|
2024-10-09 18:55:09 -06:00
|
|
|
}
|
2024-10-09 21:02:12 -06:00
|
|
|
Err(_) => false,
|
2024-10-09 18:55:09 -06:00
|
|
|
}
|
2024-10-09 21:02:12 -06:00
|
|
|
},
|
|
|
|
|
);
|
2024-10-09 18:55:09 -06:00
|
|
|
log::info!(
|
|
|
|
|
"searched for {:?} in {:?} in {:?}",
|
|
|
|
|
term,
|
|
|
|
|
path,
|
|
|
|
|
start.elapsed(),
|
|
|
|
|
);
|
|
|
|
|
})
|
|
|
|
|
.await
|
|
|
|
|
.unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Send final ready
|
|
|
|
|
let _ = output.lock().await.send(Message::SearchReady(true)).await;
|
|
|
|
|
|
|
|
|
|
std::future::pending().await
|
2024-10-21 13:51:10 -06:00
|
|
|
}),
|
2024-10-09 18:55:09 -06:00
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-09 15:41:10 -06:00
|
|
|
Subscription::batch(subscriptions)
|
2024-02-22 16:17:39 -07:00
|
|
|
}
|
2024-01-03 15:27:32 -07:00
|
|
|
}
|
2024-02-01 16:29:10 +00:00
|
|
|
|
2024-09-14 02:11:06 -04:00
|
|
|
pub fn respond_to_scroll_direction(delta: ScrollDelta, modifiers: Modifiers) -> Option<Message> {
|
|
|
|
|
if !modifiers.control() {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-12 02:00:50 -04:00
|
|
|
let delta_y = match delta {
|
2024-09-14 02:11:06 -04:00
|
|
|
ScrollDelta::Lines { y, .. } => y,
|
|
|
|
|
ScrollDelta::Pixels { y, .. } => y,
|
2024-09-12 02:00:50 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if delta_y > 0.0 {
|
2024-09-14 02:11:06 -04:00
|
|
|
return Some(Message::ZoomIn);
|
2024-09-12 02:00:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if delta_y < 0.0 {
|
2024-09-14 02:11:06 -04:00
|
|
|
return Some(Message::ZoomOut);
|
2024-09-12 02:00:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-01 16:29:10 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
2024-07-08 02:00:52 -04:00
|
|
|
use std::{fs, io, path::PathBuf};
|
2024-02-01 16:29:10 +00:00
|
|
|
|
2024-09-14 02:11:06 -04:00
|
|
|
use cosmic::{iced::mouse::ScrollDelta, iced_runtime::keyboard::Modifiers};
|
2024-02-05 20:58:17 -05:00
|
|
|
use log::{debug, trace};
|
|
|
|
|
use tempfile::TempDir;
|
2024-02-01 16:29:10 +00:00
|
|
|
use test_log::test;
|
|
|
|
|
|
2024-09-17 10:34:08 -06:00
|
|
|
use super::{respond_to_scroll_direction, scan_path, Location, Message, Tab};
|
2024-02-10 02:13:13 -05:00
|
|
|
use crate::{
|
|
|
|
|
app::test_utils::{
|
2024-10-03 13:01:59 -06: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,
|
2024-02-10 02:13:13 -05:00
|
|
|
},
|
2024-02-18 02:44:54 -05:00
|
|
|
config::{IconSizes, TabConfig},
|
2024-02-01 16:29:10 +00:00
|
|
|
};
|
|
|
|
|
|
2024-02-04 07:28:51 -05:00
|
|
|
// 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();
|
2024-02-10 02:13:13 -05:00
|
|
|
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))
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-01 16:29:10 +00:00
|
|
|
#[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)?;
|
2024-02-01 16:29:10 +00:00
|
|
|
let path = fs.path();
|
|
|
|
|
|
2024-02-04 07:28:51 -05:00
|
|
|
// Read directory entries and sort as cosmic-files does
|
|
|
|
|
let entries = read_dir_sorted(path)?;
|
2024-02-01 16:29:10 +00:00
|
|
|
|
|
|
|
|
debug!("Calling scan_path(\"{}\")", path.display());
|
2024-02-18 02:44:54 -05:00
|
|
|
let actual = scan_path(&path.to_owned(), IconSizes::default());
|
2024-02-01 16:29:10 +00:00
|
|
|
|
|
|
|
|
// 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)?;
|
2024-02-01 16:29:10 +00:00
|
|
|
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());
|
2024-02-18 02:44:54 -05:00
|
|
|
let actual = scan_path(&invalid_path, IconSizes::default());
|
2024-02-01 16:29:10 +00:00
|
|
|
|
|
|
|
|
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());
|
2024-02-18 02:44:54 -05:00
|
|
|
let actual = scan_path(&path.to_owned(), IconSizes::default());
|
2024-02-01 16:29:10 +00:00
|
|
|
|
|
|
|
|
assert_eq!(0, path.read_dir()?.count());
|
2024-02-03 02:31:08 -05:00
|
|
|
assert!(actual.is_empty());
|
2024-02-01 16:29:10 +00:00
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2024-02-03 02:31:08 -05:00
|
|
|
|
|
|
|
|
#[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
|
2024-02-04 07:28:51 -05:00
|
|
|
let next_dir = filter_dirs(path)?
|
2024-02-03 02:31:08 -05:00
|
|
|
.next()
|
|
|
|
|
.expect("temp directory should have at least one directory");
|
|
|
|
|
|
2024-02-10 02:13:13 -05:00
|
|
|
let mut tab = Tab::new(Location::Path(path.to_owned()), TabConfig::default());
|
2024-02-03 02:31:08 -05:00
|
|
|
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<()> {
|
2024-02-04 07:28:51 -05:00
|
|
|
// Select the second directory with no keys held down
|
|
|
|
|
tab_selects_item(&[1], Modifiers::empty(), &[false, true])
|
2024-02-03 02:31:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn tab_click_double_opens_folder() -> io::Result<()> {
|
2024-02-04 07:28:51 -05:00
|
|
|
let (fs, mut tab) = tab_click_new(NUM_FILES, NUM_NESTED, NUM_DIRS, NUM_NESTED, NAME_LEN)?;
|
|
|
|
|
let path = fs.path();
|
2024-04-27 08:25:02 -06:00
|
|
|
|
2024-02-04 07:28:51 -05:00
|
|
|
// Simulate double clicking second directory
|
2024-04-24 23:30:31 +02:00
|
|
|
debug!("Emitting double click Message::DoubleClick(Some(1))");
|
|
|
|
|
tab.update(Message::DoubleClick(Some(1)), Modifiers::empty());
|
2024-02-04 07:28:51 -05:00
|
|
|
|
|
|
|
|
// Path to second directory
|
|
|
|
|
let second_dir = read_dir_sorted(path)?
|
2024-02-05 20:58:17 -05:00
|
|
|
.into_iter()
|
2024-02-04 07:28:51 -05:00
|
|
|
.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(())
|
2024-02-03 02:31:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn tab_click_ctrl_selects_multiple() -> io::Result<()> {
|
2024-02-04 07:28:51 -05:00
|
|
|
// Select the first and second directory by holding down ctrl
|
|
|
|
|
tab_selects_item(&[0, 1], Modifiers::CTRL, &[true, true])
|
2024-02-03 02:31:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[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(())
|
2024-02-03 02:31:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[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(())
|
2024-02-03 02:31:08 -05:00
|
|
|
}
|
|
|
|
|
|
2024-09-12 02:00:50 -04:00
|
|
|
#[test]
|
2024-09-14 02:11:06 -04:00
|
|
|
fn tab_scroll_up_with_ctrl_modifier_zooms() -> io::Result<()> {
|
2024-09-17 10:34:08 -06:00
|
|
|
let message_maybe =
|
|
|
|
|
respond_to_scroll_direction(ScrollDelta::Pixels { x: 0.0, y: 1.0 }, Modifiers::CTRL);
|
2024-09-14 02:11:06 -04:00
|
|
|
assert!(!message_maybe.is_none());
|
|
|
|
|
assert!(matches!(message_maybe.unwrap(), Message::ZoomIn));
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2024-09-12 08:49:28 -04:00
|
|
|
|
2024-09-14 02:11:06 -04:00
|
|
|
#[test]
|
|
|
|
|
fn tab_scroll_up_without_ctrl_modifier_does_not_zoom() -> io::Result<()> {
|
2024-09-17 10:34:08 -06:00
|
|
|
let message_maybe =
|
|
|
|
|
respond_to_scroll_direction(ScrollDelta::Pixels { x: 0.0, y: 1.0 }, Modifiers::empty());
|
2024-09-14 02:11:06 -04:00
|
|
|
assert!(message_maybe.is_none());
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2024-09-12 02:00:50 -04:00
|
|
|
|
2024-09-14 02:11:06 -04:00
|
|
|
#[test]
|
|
|
|
|
fn tab_scroll_down_with_ctrl_modifier_zooms() -> io::Result<()> {
|
2024-09-17 10:34:08 -06:00
|
|
|
let message_maybe =
|
|
|
|
|
respond_to_scroll_direction(ScrollDelta::Pixels { x: 0.0, y: -1.0 }, Modifiers::CTRL);
|
2024-09-14 02:11:06 -04:00
|
|
|
assert!(!message_maybe.is_none());
|
|
|
|
|
assert!(matches!(message_maybe.unwrap(), Message::ZoomOut));
|
2024-09-12 02:00:50 -04:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn tab_scroll_down_without_ctrl_modifier_does_not_zoom() -> io::Result<()> {
|
2024-09-17 10:34:08 -06:00
|
|
|
let message_maybe = respond_to_scroll_direction(
|
|
|
|
|
ScrollDelta::Pixels { x: 0.0, y: -1.0 },
|
|
|
|
|
Modifiers::empty(),
|
|
|
|
|
);
|
2024-09-14 02:11:06 -04:00
|
|
|
assert!(message_maybe.is_none());
|
2024-09-12 02:00:50 -04:00
|
|
|
Ok(())
|
|
|
|
|
}
|
2024-02-03 02:31:08 -05:00
|
|
|
#[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();
|
2024-02-10 02:13:13 -05:00
|
|
|
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(())
|
2024-02-03 02:31:08 -05:00
|
|
|
}
|
2024-02-06 22:58:29 -05:00
|
|
|
|
|
|
|
|
#[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");
|
|
|
|
|
|
2024-02-10 02:13:13 -05:00
|
|
|
let mut tab = Tab::new(Location::Path(next_dir.clone()), TabConfig::default());
|
2024-02-06 22:58:29 -05:00
|
|
|
// 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(())
|
|
|
|
|
}
|
2024-07-08 02:00:52 -04:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn sort_long_number_file_names() -> io::Result<()> {
|
|
|
|
|
let fs = empty_fs()?;
|
|
|
|
|
let path = fs.path();
|
|
|
|
|
|
|
|
|
|
// Create files with names 255 characters long that only contain a single number
|
|
|
|
|
// Example: 0000...0 for 255 characters
|
|
|
|
|
// https://en.wikipedia.org/wiki/Filename#Comparison_of_filename_limitations
|
|
|
|
|
let mut base_nums: Vec<_> = ('0'..'9').collect();
|
|
|
|
|
fastrand::shuffle(&mut base_nums);
|
|
|
|
|
debug!("Shuffled numbers for paths: {base_nums:?}");
|
|
|
|
|
let paths: Vec<_> = base_nums
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|&base| path.join(std::iter::repeat(base).take(255).collect::<String>()))
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
for (file, &base) in paths.iter().zip(base_nums.iter()) {
|
|
|
|
|
trace!("Creating long file name for {base}");
|
|
|
|
|
fs::File::create(file)?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
debug!("Creating tab for directory of long file names");
|
|
|
|
|
Tab::new(Location::Path(path.into()), TabConfig::default());
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2024-02-01 16:29:10 +00:00
|
|
|
}
|
2024-04-10 11:41:25 -04:00
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub struct ArcElementWrapper<M>(pub Arc<Mutex<Element<'static, M>>>);
|
|
|
|
|
|
|
|
|
|
impl<M> Widget<M, cosmic::Theme, cosmic::Renderer> for ArcElementWrapper<M> {
|
|
|
|
|
fn size(&self) -> Size<Length> {
|
|
|
|
|
self.0.lock().unwrap().as_widget().size()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn size_hint(&self) -> Size<Length> {
|
|
|
|
|
self.0.lock().unwrap().as_widget().size_hint()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn layout(
|
|
|
|
|
&self,
|
|
|
|
|
tree: &mut tree::Tree,
|
|
|
|
|
renderer: &cosmic::Renderer,
|
|
|
|
|
limits: &cosmic::iced_core::layout::Limits,
|
|
|
|
|
) -> cosmic::iced_core::layout::Node {
|
|
|
|
|
self.0
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.as_widget_mut()
|
|
|
|
|
.layout(tree, renderer, limits)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn draw(
|
|
|
|
|
&self,
|
|
|
|
|
tree: &tree::Tree,
|
|
|
|
|
renderer: &mut cosmic::Renderer,
|
|
|
|
|
theme: &cosmic::Theme,
|
|
|
|
|
style: &cosmic::iced_core::renderer::Style,
|
|
|
|
|
layout: cosmic::iced_core::Layout<'_>,
|
|
|
|
|
cursor: cosmic::iced_core::mouse::Cursor,
|
|
|
|
|
viewport: &Rectangle,
|
|
|
|
|
) {
|
|
|
|
|
self.0
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.as_widget()
|
|
|
|
|
.draw(tree, renderer, theme, style, layout, cursor, viewport)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn tag(&self) -> tree::Tag {
|
|
|
|
|
self.0.lock().unwrap().as_widget().tag()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn state(&self) -> tree::State {
|
|
|
|
|
self.0.lock().unwrap().as_widget().state()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn children(&self) -> Vec<tree::Tree> {
|
|
|
|
|
self.0.lock().unwrap().as_widget().children()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn diff(&mut self, tree: &mut tree::Tree) {
|
|
|
|
|
self.0.lock().unwrap().as_widget_mut().diff(tree)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn operate(
|
|
|
|
|
&self,
|
|
|
|
|
state: &mut tree::Tree,
|
|
|
|
|
layout: cosmic::iced_core::Layout<'_>,
|
|
|
|
|
renderer: &cosmic::Renderer,
|
2024-10-21 13:51:10 -06:00
|
|
|
operation: &mut dyn widget::Operation,
|
2024-04-10 11:41:25 -04:00
|
|
|
) {
|
|
|
|
|
self.0
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.as_widget()
|
|
|
|
|
.operate(state, layout, renderer, operation)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn on_event(
|
|
|
|
|
&mut self,
|
|
|
|
|
_state: &mut tree::Tree,
|
|
|
|
|
_event: cosmic::iced::Event,
|
|
|
|
|
_layout: cosmic::iced_core::Layout<'_>,
|
|
|
|
|
_cursor: cosmic::iced_core::mouse::Cursor,
|
|
|
|
|
_renderer: &cosmic::Renderer,
|
|
|
|
|
_clipboard: &mut dyn cosmic::iced_core::Clipboard,
|
|
|
|
|
_shell: &mut cosmic::iced_core::Shell<'_, M>,
|
|
|
|
|
_viewport: &Rectangle,
|
2024-09-14 02:11:06 -04:00
|
|
|
) -> event::Status {
|
2024-04-10 11:41:25 -04:00
|
|
|
self.0.lock().unwrap().as_widget_mut().on_event(
|
|
|
|
|
_state, _event, _layout, _cursor, _renderer, _clipboard, _shell, _viewport,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn mouse_interaction(
|
|
|
|
|
&self,
|
|
|
|
|
_state: &tree::Tree,
|
|
|
|
|
_layout: cosmic::iced_core::Layout<'_>,
|
|
|
|
|
_cursor: cosmic::iced_core::mouse::Cursor,
|
|
|
|
|
_viewport: &Rectangle,
|
|
|
|
|
_renderer: &cosmic::Renderer,
|
|
|
|
|
) -> cosmic::iced_core::mouse::Interaction {
|
|
|
|
|
self.0
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.as_widget()
|
|
|
|
|
.mouse_interaction(_state, _layout, _cursor, _viewport, _renderer)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn overlay<'a>(
|
|
|
|
|
&'a mut self,
|
|
|
|
|
_state: &'a mut tree::Tree,
|
|
|
|
|
_layout: cosmic::iced_core::Layout<'_>,
|
|
|
|
|
_renderer: &cosmic::Renderer,
|
2024-10-21 13:51:10 -06:00
|
|
|
_translation: cosmic::iced_core::Vector,
|
2024-04-10 11:41:25 -04:00
|
|
|
) -> Option<cosmic::iced_core::overlay::Element<'a, M, cosmic::Theme, cosmic::Renderer>> {
|
|
|
|
|
// TODO
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn id(&self) -> Option<Id> {
|
|
|
|
|
self.0.lock().unwrap().as_widget().id()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn set_id(&mut self, _id: Id) {
|
|
|
|
|
self.0.lock().unwrap().as_widget_mut().set_id(_id)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn drag_destinations(
|
|
|
|
|
&self,
|
|
|
|
|
_state: &tree::Tree,
|
|
|
|
|
_layout: cosmic::iced_core::Layout<'_>,
|
2024-05-31 19:23:13 -04:00
|
|
|
renderer: &cosmic::Renderer,
|
2024-04-10 11:41:25 -04:00
|
|
|
_dnd_rectangles: &mut cosmic::iced_core::clipboard::DndDestinationRectangles,
|
|
|
|
|
) {
|
2024-05-31 19:23:13 -04:00
|
|
|
self.0.lock().unwrap().as_widget().drag_destinations(
|
|
|
|
|
_state,
|
|
|
|
|
_layout,
|
|
|
|
|
renderer,
|
|
|
|
|
_dnd_rectangles,
|
|
|
|
|
)
|
2024-04-10 11:41:25 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<Message: 'static> From<ArcElementWrapper<Message>> for Element<'static, Message> {
|
|
|
|
|
fn from(wrapper: ArcElementWrapper<Message>) -> Self {
|
|
|
|
|
Element::new(wrapper)
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-10 08:04:43 +01:00
|
|
|
|
|
|
|
|
fn text_editor_class(
|
|
|
|
|
theme: &cosmic::Theme,
|
|
|
|
|
status: cosmic::widget::text_editor::Status,
|
|
|
|
|
) -> cosmic::iced_widget::text_editor::Style {
|
|
|
|
|
let cosmic = theme.cosmic();
|
|
|
|
|
let container = theme.current_container();
|
|
|
|
|
|
|
|
|
|
let mut background: cosmic::iced::Color = container.component.base.into();
|
|
|
|
|
background.a = 0.25;
|
|
|
|
|
let selection = cosmic.accent.base.into();
|
|
|
|
|
let value = cosmic.palette.neutral_9.into();
|
|
|
|
|
let mut placeholder = cosmic.palette.neutral_9;
|
|
|
|
|
placeholder.alpha = 0.7;
|
|
|
|
|
let placeholder = placeholder.into();
|
|
|
|
|
let icon = cosmic.background.on.into();
|
|
|
|
|
|
|
|
|
|
match status {
|
|
|
|
|
cosmic::iced_widget::text_editor::Status::Active
|
|
|
|
|
| cosmic::iced_widget::text_editor::Status::Disabled => {
|
|
|
|
|
cosmic::iced_widget::text_editor::Style {
|
|
|
|
|
background: background.into(),
|
|
|
|
|
border: cosmic::iced::Border {
|
|
|
|
|
radius: cosmic.corner_radii.radius_m.into(),
|
|
|
|
|
width: 2.0,
|
|
|
|
|
color: container.component.divider.into(),
|
|
|
|
|
},
|
|
|
|
|
icon,
|
|
|
|
|
placeholder,
|
|
|
|
|
value,
|
|
|
|
|
selection,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
cosmic::iced_widget::text_editor::Status::Hovered
|
|
|
|
|
| cosmic::iced_widget::text_editor::Status::Focused => {
|
|
|
|
|
cosmic::iced_widget::text_editor::Style {
|
|
|
|
|
background: background.into(),
|
|
|
|
|
border: cosmic::iced::Border {
|
|
|
|
|
radius: cosmic.corner_radii.radius_m.into(),
|
|
|
|
|
width: 2.0,
|
|
|
|
|
color: cosmic::iced::Color::from(cosmic.accent.base),
|
|
|
|
|
},
|
|
|
|
|
icon,
|
|
|
|
|
placeholder,
|
|
|
|
|
value,
|
|
|
|
|
selection,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|