2024-02-01 15:14:14 -07:00
|
|
|
// Copyright 2023 System76 <info@system76.com>
|
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
|
|
2024-04-10 11:41:25 -04:00
|
|
|
use cosmic::iced::clipboard::dnd::DndAction;
|
|
|
|
|
use cosmic::widget::dnd_destination::DragId;
|
2024-03-17 14:49:24 -07:00
|
|
|
use cosmic::widget::menu::action::MenuAction;
|
|
|
|
|
use cosmic::widget::menu::key_bind::KeyBind;
|
2024-02-01 15:14:14 -07:00
|
|
|
use cosmic::{
|
|
|
|
|
app::{message, Command, Core},
|
2024-02-27 09:58:22 -07:00
|
|
|
cosmic_config, cosmic_theme, executor,
|
2024-02-01 15:14:14 -07:00
|
|
|
iced::{
|
|
|
|
|
event,
|
|
|
|
|
futures::{self, SinkExt},
|
2024-02-09 07:09:51 -07:00
|
|
|
keyboard::{Event as KeyEvent, Key, Modifiers},
|
2024-02-01 15:14:14 -07:00
|
|
|
subscription::{self, Subscription},
|
2024-02-29 16:25:54 -07:00
|
|
|
widget::scrollable,
|
2024-02-28 09:29:05 -07:00
|
|
|
window, Alignment, Event, Length,
|
2024-02-01 15:14:14 -07:00
|
|
|
},
|
2024-03-20 08:42:28 -06:00
|
|
|
iced_runtime::clipboard,
|
2024-02-29 12:26:45 -07:00
|
|
|
style, theme,
|
2024-03-04 10:28:16 -07:00
|
|
|
widget::{
|
|
|
|
|
self,
|
|
|
|
|
segmented_button::{self, Entity},
|
|
|
|
|
},
|
2024-02-01 15:14:14 -07:00
|
|
|
Application, ApplicationExt, Element,
|
|
|
|
|
};
|
2024-03-20 11:54:37 -06:00
|
|
|
use notify_debouncer_full::{
|
|
|
|
|
new_debouncer,
|
|
|
|
|
notify::{self, RecommendedWatcher, Watcher},
|
|
|
|
|
DebouncedEvent, Debouncer, FileIdMap,
|
|
|
|
|
};
|
2024-04-22 23:14:44 +02:00
|
|
|
use slotmap::Key as SlotMapKey;
|
2024-02-01 15:14:14 -07:00
|
|
|
use std::{
|
|
|
|
|
any::TypeId,
|
2024-02-27 13:48:12 -07:00
|
|
|
collections::{BTreeMap, HashMap, HashSet, VecDeque},
|
2024-03-20 11:54:37 -06:00
|
|
|
env, fmt, fs,
|
2024-02-29 20:18:34 -07:00
|
|
|
num::NonZeroU16,
|
2024-02-01 15:14:14 -07:00
|
|
|
path::PathBuf,
|
2024-03-20 11:54:37 -06:00
|
|
|
process,
|
|
|
|
|
sync::Arc,
|
2024-04-22 13:14:25 -06:00
|
|
|
time::{self, Instant},
|
2024-02-01 15:14:14 -07:00
|
|
|
};
|
|
|
|
|
|
2024-04-10 11:41:25 -04:00
|
|
|
use crate::tab::HOVER_DURATION;
|
2024-02-01 15:14:14 -07:00
|
|
|
use crate::{
|
2024-03-20 10:27:35 -06:00
|
|
|
clipboard::{ClipboardCopy, ClipboardKind, ClipboardPaste},
|
2024-04-30 13:29:58 -06:00
|
|
|
config::{AppTheme, Config, Favorite, IconSizes, TabConfig, CONFIG_VERSION},
|
2024-02-01 15:14:14 -07:00
|
|
|
fl, home_dir,
|
2024-03-17 14:49:24 -07:00
|
|
|
key_bind::key_binds,
|
2024-03-04 10:28:16 -07:00
|
|
|
menu, mime_app,
|
2024-04-22 13:14:25 -06:00
|
|
|
mounter::{mounters, MounterItem, MounterItems, MounterKey, Mounters},
|
2024-02-01 15:14:14 -07:00
|
|
|
operation::Operation,
|
2024-03-04 10:28:16 -07:00
|
|
|
spawn_detached::spawn_detached,
|
2024-03-14 02:17:27 -04:00
|
|
|
tab::{self, HeadingOptions, ItemMetadata, Location, Tab},
|
2024-02-01 15:14:14 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
|
pub struct Flags {
|
|
|
|
|
pub config_handler: Option<cosmic_config::Config>,
|
|
|
|
|
pub config: Config,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
|
|
|
pub enum Action {
|
2024-02-28 09:29:05 -07:00
|
|
|
About,
|
2024-05-09 12:48:33 -06:00
|
|
|
AddToSidebar,
|
2024-02-01 15:14:14 -07:00
|
|
|
Copy,
|
|
|
|
|
Cut,
|
2024-02-28 15:19:07 -07:00
|
|
|
EditLocation,
|
2024-02-06 03:14:12 -05:00
|
|
|
HistoryNext,
|
|
|
|
|
HistoryPrevious,
|
2024-02-28 16:16:59 -07:00
|
|
|
ItemDown,
|
|
|
|
|
ItemLeft,
|
|
|
|
|
ItemRight,
|
|
|
|
|
ItemUp,
|
2024-02-06 22:58:29 -05:00
|
|
|
LocationUp,
|
2024-02-01 15:14:14 -07:00
|
|
|
MoveToTrash,
|
|
|
|
|
NewFile,
|
|
|
|
|
NewFolder,
|
2024-02-28 15:46:23 -07:00
|
|
|
Open,
|
2024-03-04 10:28:16 -07:00
|
|
|
OpenTerminal,
|
2024-03-01 16:10:30 -07:00
|
|
|
OpenWith,
|
2024-02-27 13:08:31 -07:00
|
|
|
Operations,
|
2024-02-01 15:14:14 -07:00
|
|
|
Paste,
|
|
|
|
|
Properties,
|
2024-02-28 15:07:50 -07:00
|
|
|
Rename,
|
2024-02-01 15:14:14 -07:00
|
|
|
RestoreFromTrash,
|
|
|
|
|
SelectAll,
|
|
|
|
|
Settings,
|
|
|
|
|
TabClose,
|
|
|
|
|
TabNew,
|
|
|
|
|
TabNext,
|
|
|
|
|
TabPrev,
|
|
|
|
|
TabViewGrid,
|
|
|
|
|
TabViewList,
|
2024-02-11 00:24:35 -05:00
|
|
|
ToggleShowHidden,
|
2024-03-15 00:34:16 -04:00
|
|
|
ToggleSort(HeadingOptions),
|
2024-02-01 15:14:14 -07:00
|
|
|
WindowClose,
|
|
|
|
|
WindowNew,
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 14:49:24 -07:00
|
|
|
impl MenuAction for Action {
|
|
|
|
|
type Message = Message;
|
|
|
|
|
|
|
|
|
|
fn message(&self, entity_opt: Option<Entity>) -> Message {
|
2024-02-01 15:14:14 -07:00
|
|
|
match self {
|
2024-02-28 09:29:05 -07:00
|
|
|
Action::About => Message::ToggleContextPage(ContextPage::About),
|
2024-05-09 12:48:33 -06:00
|
|
|
Action::AddToSidebar => Message::AddToSidebar(entity_opt),
|
2024-02-01 15:14:14 -07:00
|
|
|
Action::Copy => Message::Copy(entity_opt),
|
|
|
|
|
Action::Cut => Message::Cut(entity_opt),
|
2024-02-28 15:19:07 -07:00
|
|
|
Action::EditLocation => Message::EditLocation(entity_opt),
|
2024-02-28 15:46:23 -07:00
|
|
|
Action::HistoryNext => Message::TabMessage(entity_opt, tab::Message::GoNext),
|
|
|
|
|
Action::HistoryPrevious => Message::TabMessage(entity_opt, tab::Message::GoPrevious),
|
2024-02-28 16:16:59 -07:00
|
|
|
Action::ItemDown => Message::TabMessage(entity_opt, tab::Message::ItemDown),
|
|
|
|
|
Action::ItemLeft => Message::TabMessage(entity_opt, tab::Message::ItemLeft),
|
|
|
|
|
Action::ItemRight => Message::TabMessage(entity_opt, tab::Message::ItemRight),
|
|
|
|
|
Action::ItemUp => Message::TabMessage(entity_opt, tab::Message::ItemUp),
|
2024-02-28 15:46:23 -07:00
|
|
|
Action::LocationUp => Message::TabMessage(entity_opt, tab::Message::LocationUp),
|
2024-02-01 15:14:14 -07:00
|
|
|
Action::MoveToTrash => Message::MoveToTrash(entity_opt),
|
2024-02-27 13:03:39 -07:00
|
|
|
Action::NewFile => Message::NewItem(entity_opt, false),
|
|
|
|
|
Action::NewFolder => Message::NewItem(entity_opt, true),
|
2024-02-28 15:46:23 -07:00
|
|
|
Action::Open => Message::TabMessage(entity_opt, tab::Message::Open),
|
2024-03-04 10:28:16 -07:00
|
|
|
Action::OpenTerminal => Message::OpenTerminal(entity_opt),
|
2024-03-01 16:10:30 -07:00
|
|
|
Action::OpenWith => Message::ToggleContextPage(ContextPage::OpenWith),
|
2024-02-27 13:08:31 -07:00
|
|
|
Action::Operations => Message::ToggleContextPage(ContextPage::Operations),
|
2024-02-01 15:14:14 -07:00
|
|
|
Action::Paste => Message::Paste(entity_opt),
|
2024-04-22 23:14:44 +02:00
|
|
|
Action::Properties => Message::ToggleContextPage(ContextPage::Properties(None)),
|
2024-02-28 15:07:50 -07:00
|
|
|
Action::Rename => Message::Rename(entity_opt),
|
2024-02-01 15:14:14 -07:00
|
|
|
Action::RestoreFromTrash => Message::RestoreFromTrash(entity_opt),
|
2024-02-29 21:13:03 -07:00
|
|
|
Action::SelectAll => Message::TabMessage(entity_opt, tab::Message::SelectAll),
|
2024-02-01 15:14:14 -07:00
|
|
|
Action::Settings => Message::ToggleContextPage(ContextPage::Settings),
|
|
|
|
|
Action::TabClose => Message::TabClose(entity_opt),
|
|
|
|
|
Action::TabNew => Message::TabNew,
|
|
|
|
|
Action::TabNext => Message::TabNext,
|
|
|
|
|
Action::TabPrev => Message::TabPrev,
|
|
|
|
|
Action::TabViewGrid => {
|
|
|
|
|
Message::TabMessage(entity_opt, tab::Message::View(tab::View::Grid))
|
|
|
|
|
}
|
|
|
|
|
Action::TabViewList => {
|
|
|
|
|
Message::TabMessage(entity_opt, tab::Message::View(tab::View::List))
|
|
|
|
|
}
|
2024-02-11 00:24:35 -05:00
|
|
|
Action::ToggleShowHidden => Message::TabMessage(None, tab::Message::ToggleShowHidden),
|
2024-03-17 14:49:24 -07:00
|
|
|
Action::ToggleSort(sort) => Message::TabMessage(None, tab::Message::ToggleSort(*sort)),
|
2024-02-01 15:14:14 -07:00
|
|
|
Action::WindowClose => Message::WindowClose,
|
|
|
|
|
Action::WindowNew => Message::WindowNew,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-22 23:14:44 +02:00
|
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
|
|
|
enum ContextItem {
|
|
|
|
|
NavBar(segmented_button::Entity),
|
|
|
|
|
TabBar(segmented_button::Entity),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
|
|
|
|
pub enum NavMenuAction {
|
|
|
|
|
OpenInNewTab(segmented_button::Entity),
|
|
|
|
|
OpenInNewWindow(segmented_button::Entity),
|
|
|
|
|
Properties(segmented_button::Entity),
|
2024-05-09 12:26:55 -06:00
|
|
|
RemoveFromSidebar(segmented_button::Entity),
|
2024-04-22 23:14:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl MenuAction for NavMenuAction {
|
|
|
|
|
type Message = cosmic::app::Message<Message>;
|
|
|
|
|
|
|
|
|
|
fn message(&self, _entity: Option<Entity>) -> Self::Message {
|
|
|
|
|
cosmic::app::Message::App(Message::NavMenuAction(*self))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-01 15:14:14 -07:00
|
|
|
/// Messages that are used specifically by our [`App`].
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
|
pub enum Message {
|
2024-05-09 12:48:33 -06:00
|
|
|
AddToSidebar(Option<Entity>),
|
2024-02-01 15:14:14 -07:00
|
|
|
AppTheme(AppTheme),
|
|
|
|
|
Config(Config),
|
2024-03-04 10:28:16 -07:00
|
|
|
Copy(Option<Entity>),
|
|
|
|
|
Cut(Option<Entity>),
|
2024-02-27 13:03:39 -07:00
|
|
|
DialogCancel,
|
|
|
|
|
DialogComplete,
|
2024-02-27 13:48:12 -07:00
|
|
|
DialogUpdate(DialogPage),
|
2024-03-04 10:28:16 -07:00
|
|
|
EditLocation(Option<Entity>),
|
2024-02-09 07:09:51 -07:00
|
|
|
Key(Modifiers, Key),
|
2024-02-28 09:29:05 -07:00
|
|
|
LaunchUrl(String),
|
2024-02-01 15:14:14 -07:00
|
|
|
Modifiers(Modifiers),
|
2024-03-04 10:28:16 -07:00
|
|
|
MoveToTrash(Option<Entity>),
|
2024-04-22 13:14:25 -06:00
|
|
|
MounterItems(MounterKey, MounterItems),
|
2024-04-24 13:59:55 -06:00
|
|
|
NavBarClose(Entity),
|
2024-04-22 23:14:44 +02:00
|
|
|
NavBarContext(Entity),
|
|
|
|
|
NavMenuAction(NavMenuAction),
|
2024-03-04 10:28:16 -07:00
|
|
|
NewItem(Option<Entity>, bool),
|
2024-03-20 11:54:37 -06:00
|
|
|
NotifyEvents(Vec<DebouncedEvent>),
|
2024-02-01 15:14:14 -07:00
|
|
|
NotifyWatcher(WatcherWrapper),
|
2024-03-04 10:28:16 -07:00
|
|
|
OpenTerminal(Option<Entity>),
|
2024-03-04 10:39:48 -07:00
|
|
|
OpenWith(PathBuf, mime_app::MimeApp),
|
2024-03-04 10:28:16 -07:00
|
|
|
Paste(Option<Entity>),
|
2024-03-20 11:54:37 -06:00
|
|
|
PasteContents(PathBuf, ClipboardPaste),
|
2024-02-01 15:14:14 -07:00
|
|
|
PendingComplete(u64),
|
|
|
|
|
PendingError(u64, String),
|
|
|
|
|
PendingProgress(u64, f32),
|
2024-03-20 20:23:00 -04:00
|
|
|
RescanTrash,
|
2024-03-04 10:28:16 -07:00
|
|
|
Rename(Option<Entity>),
|
|
|
|
|
RestoreFromTrash(Option<Entity>),
|
2024-02-01 15:14:14 -07:00
|
|
|
SystemThemeModeChange(cosmic_theme::ThemeMode),
|
2024-03-04 10:28:16 -07:00
|
|
|
TabActivate(Entity),
|
2024-02-01 15:14:14 -07:00
|
|
|
TabNext,
|
|
|
|
|
TabPrev,
|
2024-03-04 10:28:16 -07:00
|
|
|
TabClose(Option<Entity>),
|
2024-02-17 02:07:21 -05:00
|
|
|
TabConfig(TabConfig),
|
2024-03-04 10:28:16 -07:00
|
|
|
TabMessage(Option<Entity>, tab::Message),
|
2024-02-01 15:14:14 -07:00
|
|
|
TabNew,
|
2024-03-04 10:28:16 -07:00
|
|
|
TabRescan(Entity, Vec<tab::Item>),
|
2024-02-01 15:14:14 -07:00
|
|
|
ToggleContextPage(ContextPage),
|
|
|
|
|
WindowClose,
|
|
|
|
|
WindowNew,
|
2024-04-10 11:41:25 -04:00
|
|
|
DndHoverLocTimeout(Location),
|
|
|
|
|
DndHoverTabTimeout(Entity),
|
|
|
|
|
DndEnterNav(Entity),
|
|
|
|
|
DndExitNav,
|
|
|
|
|
DndEnterTab(Entity),
|
|
|
|
|
DndExitTab,
|
|
|
|
|
DndDropTab(Entity, Option<ClipboardPaste>, DndAction),
|
|
|
|
|
DndDropNav(Entity, Option<ClipboardPaste>, DndAction),
|
2024-02-01 15:14:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
|
|
|
pub enum ContextPage {
|
2024-02-28 09:29:05 -07:00
|
|
|
About,
|
2024-03-01 16:10:30 -07:00
|
|
|
OpenWith,
|
2024-02-01 15:14:14 -07:00
|
|
|
Operations,
|
2024-04-22 23:14:44 +02:00
|
|
|
Properties(Option<ContextItem>),
|
2024-02-01 15:14:14 -07:00
|
|
|
Settings,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ContextPage {
|
|
|
|
|
fn title(&self) -> String {
|
|
|
|
|
match self {
|
2024-02-28 09:33:36 -07:00
|
|
|
Self::About => String::new(),
|
2024-03-01 16:10:30 -07:00
|
|
|
Self::OpenWith => fl!("open-with"),
|
2024-02-01 15:14:14 -07:00
|
|
|
Self::Operations => fl!("operations"),
|
2024-04-22 23:14:44 +02:00
|
|
|
Self::Properties(..) => fl!("properties"),
|
2024-02-01 15:14:14 -07:00
|
|
|
Self::Settings => fl!("settings"),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-27 13:03:39 -07:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
|
|
|
pub enum DialogPage {
|
2024-05-09 13:24:06 -06:00
|
|
|
EmptyTrash,
|
2024-02-27 13:48:12 -07:00
|
|
|
FailedOperation(u64),
|
2024-02-27 13:03:39 -07:00
|
|
|
NewItem {
|
|
|
|
|
parent: PathBuf,
|
|
|
|
|
name: String,
|
|
|
|
|
dir: bool,
|
|
|
|
|
},
|
2024-02-28 15:07:50 -07:00
|
|
|
RenameItem {
|
|
|
|
|
from: PathBuf,
|
|
|
|
|
parent: PathBuf,
|
|
|
|
|
name: String,
|
|
|
|
|
dir: bool,
|
|
|
|
|
},
|
2024-02-27 13:03:39 -07:00
|
|
|
}
|
|
|
|
|
|
2024-05-09 12:26:55 -06:00
|
|
|
pub struct FavoriteIndex(usize);
|
|
|
|
|
|
2024-04-22 13:14:25 -06:00
|
|
|
pub struct MounterData(MounterKey, MounterItem);
|
|
|
|
|
|
2024-02-01 15:14:14 -07:00
|
|
|
pub struct WatcherWrapper {
|
2024-03-20 11:54:37 -06:00
|
|
|
watcher_opt: Option<Debouncer<RecommendedWatcher, FileIdMap>>,
|
2024-02-01 15:14:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Clone for WatcherWrapper {
|
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
|
Self { watcher_opt: None }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-20 11:54:37 -06:00
|
|
|
impl fmt::Debug for WatcherWrapper {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
f.debug_struct("WatcherWrapper").finish()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-01 15:14:14 -07:00
|
|
|
impl PartialEq for WatcherWrapper {
|
|
|
|
|
fn eq(&self, _other: &Self) -> bool {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The [`App`] stores application-specific state.
|
|
|
|
|
pub struct App {
|
|
|
|
|
core: Core,
|
2024-04-22 23:14:44 +02:00
|
|
|
nav_bar_context_id: segmented_button::Entity,
|
2024-02-01 15:14:14 -07:00
|
|
|
nav_model: segmented_button::SingleSelectModel,
|
|
|
|
|
tab_model: segmented_button::Model<segmented_button::SingleSelect>,
|
|
|
|
|
config_handler: Option<cosmic_config::Config>,
|
|
|
|
|
config: Config,
|
|
|
|
|
app_themes: Vec<String>,
|
2024-03-14 02:17:27 -04:00
|
|
|
sort_by_names: Vec<String>,
|
2024-03-16 02:39:56 -04:00
|
|
|
sort_direction: Vec<String>,
|
2024-02-01 15:14:14 -07:00
|
|
|
context_page: ContextPage,
|
2024-02-27 13:48:12 -07:00
|
|
|
dialog_pages: VecDeque<DialogPage>,
|
2024-02-27 13:03:39 -07:00
|
|
|
dialog_text_input: widget::Id,
|
2024-02-01 15:14:14 -07:00
|
|
|
key_binds: HashMap<KeyBind, Action>,
|
|
|
|
|
modifiers: Modifiers,
|
2024-04-22 09:54:00 -06:00
|
|
|
mounters: Mounters,
|
2024-04-22 13:14:25 -06:00
|
|
|
mounter_items: HashMap<MounterKey, MounterItems>,
|
2024-02-01 15:14:14 -07:00
|
|
|
pending_operation_id: u64,
|
|
|
|
|
pending_operations: BTreeMap<u64, (Operation, f32)>,
|
|
|
|
|
complete_operations: BTreeMap<u64, Operation>,
|
|
|
|
|
failed_operations: BTreeMap<u64, (Operation, String)>,
|
2024-03-20 11:54:37 -06:00
|
|
|
watcher_opt: Option<(Debouncer<RecommendedWatcher, FileIdMap>, HashSet<PathBuf>)>,
|
2024-04-10 11:41:25 -04:00
|
|
|
nav_dnd_hover: Option<(Location, Instant)>,
|
|
|
|
|
tab_dnd_hover: Option<(Entity, Instant)>,
|
|
|
|
|
nav_drag_id: DragId,
|
|
|
|
|
tab_drag_id: DragId,
|
2024-02-01 15:14:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl App {
|
|
|
|
|
fn open_tab(&mut self, location: Location) -> Command<Message> {
|
2024-02-11 01:16:51 -05:00
|
|
|
let tab = Tab::new(location.clone(), self.config.tab.clone());
|
2024-02-01 15:14:14 -07:00
|
|
|
let entity = self
|
|
|
|
|
.tab_model
|
|
|
|
|
.insert()
|
|
|
|
|
.text(tab.title())
|
|
|
|
|
.data(tab)
|
|
|
|
|
.closable()
|
|
|
|
|
.activate()
|
|
|
|
|
.id();
|
|
|
|
|
Command::batch([
|
|
|
|
|
self.update_title(),
|
|
|
|
|
self.update_watcher(),
|
|
|
|
|
self.rescan_tab(entity, location),
|
|
|
|
|
])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn operation(&mut self, operation: Operation) {
|
|
|
|
|
let id = self.pending_operation_id;
|
|
|
|
|
self.pending_operation_id += 1;
|
|
|
|
|
self.pending_operations.insert(id, (operation, 0.0));
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-04 10:28:16 -07:00
|
|
|
fn rescan_tab(&mut self, entity: Entity, location: Location) -> Command<Message> {
|
2024-02-18 02:44:54 -05:00
|
|
|
let icon_sizes = self.config.tab.icon_sizes;
|
2024-02-01 15:14:14 -07:00
|
|
|
Command::perform(
|
|
|
|
|
async move {
|
2024-02-19 03:44:19 -05:00
|
|
|
match tokio::task::spawn_blocking(move || location.scan(icon_sizes)).await {
|
2024-02-01 15:14:14 -07:00
|
|
|
Ok(items) => message::app(Message::TabRescan(entity, items)),
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::warn!("failed to rescan: {}", err);
|
|
|
|
|
message::none()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|x| x,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-28 14:42:46 -07:00
|
|
|
fn rescan_trash(&mut self) -> Command<Message> {
|
|
|
|
|
let mut needs_reload = Vec::new();
|
|
|
|
|
for entity in self.tab_model.iter() {
|
|
|
|
|
if let Some(tab) = self.tab_model.data::<Tab>(entity) {
|
|
|
|
|
if let Location::Trash = &tab.location {
|
|
|
|
|
needs_reload.push((entity, Location::Trash));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut commands = Vec::with_capacity(needs_reload.len());
|
|
|
|
|
for (entity, location) in needs_reload {
|
|
|
|
|
commands.push(self.rescan_tab(entity, location));
|
|
|
|
|
}
|
|
|
|
|
Command::batch(commands)
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-20 09:56:54 -06:00
|
|
|
fn selected_paths(&self, entity_opt: Option<Entity>) -> Vec<PathBuf> {
|
|
|
|
|
let mut paths = Vec::new();
|
|
|
|
|
let entity = entity_opt.unwrap_or_else(|| self.tab_model.active());
|
|
|
|
|
if let Some(tab) = self.tab_model.data::<Tab>(entity) {
|
|
|
|
|
if let Some(ref items) = tab.items_opt() {
|
|
|
|
|
for item in items.iter() {
|
|
|
|
|
if item.selected {
|
|
|
|
|
if let Some(path) = &item.path_opt {
|
|
|
|
|
paths.push(path.clone());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
paths
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-01 15:14:14 -07:00
|
|
|
fn update_config(&mut self) -> Command<Message> {
|
2024-05-09 12:26:55 -06:00
|
|
|
self.update_nav_model();
|
2024-02-01 15:14:14 -07:00
|
|
|
cosmic::app::command::set_theme(self.config.app_theme.theme())
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-09 12:26:55 -06:00
|
|
|
fn update_nav_model(&mut self) {
|
|
|
|
|
let mut nav_model = segmented_button::ModelBuilder::default();
|
|
|
|
|
for (favorite_i, favorite) in self.config.favorites.iter().enumerate() {
|
2024-05-09 12:48:33 -06:00
|
|
|
if let Some(path) = favorite.path_opt() {
|
2024-05-09 12:26:55 -06:00
|
|
|
let name = if matches!(favorite, Favorite::Home) {
|
|
|
|
|
fl!("home")
|
2024-05-09 12:48:33 -06:00
|
|
|
} else if let Some(file_name) = path.file_name().and_then(|x| x.to_str()) {
|
2024-05-09 12:26:55 -06:00
|
|
|
file_name.to_string()
|
|
|
|
|
} else {
|
|
|
|
|
continue;
|
|
|
|
|
};
|
|
|
|
|
nav_model = nav_model.insert(move |b| {
|
|
|
|
|
b.text(name.clone())
|
2024-05-09 12:48:33 -06:00
|
|
|
.icon(
|
|
|
|
|
widget::icon::icon(if path.is_dir() {
|
|
|
|
|
tab::folder_icon_symbolic(&path, 16)
|
|
|
|
|
} else {
|
|
|
|
|
widget::icon::from_name("text-x-generic-symbolic")
|
|
|
|
|
.size(16)
|
|
|
|
|
.handle()
|
|
|
|
|
})
|
|
|
|
|
.size(16),
|
|
|
|
|
)
|
|
|
|
|
.data(Location::Path(path.clone()))
|
2024-05-09 12:26:55 -06:00
|
|
|
.data(FavoriteIndex(favorite_i))
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
nav_model = nav_model.insert(|b| {
|
|
|
|
|
b.text(fl!("trash"))
|
|
|
|
|
.icon(widget::icon::icon(tab::trash_icon_symbolic(16)))
|
|
|
|
|
.data(Location::Trash)
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Collect all mounter items
|
|
|
|
|
let mut nav_items = Vec::new();
|
|
|
|
|
for (key, items) in self.mounter_items.iter() {
|
|
|
|
|
for item in items.iter() {
|
|
|
|
|
nav_items.push((*key, item));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Sort by name lexically
|
2024-05-09 12:31:28 -06:00
|
|
|
nav_items.sort_by(|a, b| lexical_sort::natural_lexical_cmp(&a.1.name(), &b.1.name()));
|
2024-05-09 12:26:55 -06:00
|
|
|
// Add items to nav model
|
|
|
|
|
for (key, item) in nav_items {
|
|
|
|
|
nav_model = nav_model.insert(|mut b| {
|
2024-05-09 12:31:28 -06:00
|
|
|
b = b.text(item.name()).data(MounterData(key, item.clone()));
|
|
|
|
|
if let Some(path) = item.path() {
|
|
|
|
|
b = b.data(Location::Path(path.clone()));
|
|
|
|
|
}
|
|
|
|
|
if let Some(icon) = item.icon() {
|
|
|
|
|
b = b.icon(widget::icon::icon(icon).size(16));
|
|
|
|
|
}
|
|
|
|
|
if item.is_mounted() {
|
|
|
|
|
b = b.closable();
|
|
|
|
|
}
|
|
|
|
|
b
|
2024-05-09 12:26:55 -06:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.nav_model = nav_model.build();
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-01 15:14:14 -07:00
|
|
|
fn update_title(&mut self) -> Command<Message> {
|
2024-03-01 09:18:57 -07:00
|
|
|
let window_title = match self.tab_model.text(self.tab_model.active()) {
|
|
|
|
|
Some(tab_title) => format!("{tab_title} — {}", fl!("cosmic-files")),
|
|
|
|
|
None => fl!("cosmic-files"),
|
2024-02-01 15:14:14 -07:00
|
|
|
};
|
2024-02-01 19:40:37 -07:00
|
|
|
self.set_window_title(window_title, window::Id::MAIN)
|
2024-02-01 15:14:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn update_watcher(&mut self) -> Command<Message> {
|
|
|
|
|
if let Some((mut watcher, old_paths)) = self.watcher_opt.take() {
|
|
|
|
|
let mut new_paths = HashSet::new();
|
|
|
|
|
for entity in self.tab_model.iter() {
|
|
|
|
|
if let Some(tab) = self.tab_model.data::<Tab>(entity) {
|
|
|
|
|
if let Location::Path(path) = &tab.location {
|
|
|
|
|
new_paths.insert(path.clone());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Unwatch paths no longer used
|
|
|
|
|
for path in old_paths.iter() {
|
|
|
|
|
if !new_paths.contains(path) {
|
2024-03-20 11:54:37 -06:00
|
|
|
match watcher.watcher().unwatch(path) {
|
2024-02-01 15:14:14 -07:00
|
|
|
Ok(()) => {
|
|
|
|
|
log::debug!("unwatching {:?}", path);
|
|
|
|
|
}
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::debug!("failed to unwatch {:?}: {}", path, err);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Watch new paths
|
|
|
|
|
for path in new_paths.iter() {
|
|
|
|
|
if !old_paths.contains(path) {
|
|
|
|
|
//TODO: should this be recursive?
|
2024-03-20 11:54:37 -06:00
|
|
|
match watcher
|
|
|
|
|
.watcher()
|
|
|
|
|
.watch(path, notify::RecursiveMode::NonRecursive)
|
|
|
|
|
{
|
2024-02-01 15:14:14 -07:00
|
|
|
Ok(()) => {
|
|
|
|
|
log::debug!("watching {:?}", path);
|
|
|
|
|
}
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::debug!("failed to watch {:?}: {}", path, err);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.watcher_opt = Some((watcher, new_paths));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//TODO: should any of this run in a command?
|
|
|
|
|
Command::none()
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-28 09:29:05 -07:00
|
|
|
fn about(&self) -> Element<Message> {
|
2024-02-29 12:26:45 -07:00
|
|
|
let cosmic_theme::Spacing { space_xxs, .. } = theme::active().cosmic().spacing;
|
2024-02-28 09:29:05 -07:00
|
|
|
let repository = "https://github.com/pop-os/cosmic-files";
|
|
|
|
|
let hash = env!("VERGEN_GIT_SHA");
|
2024-02-28 13:35:29 -07:00
|
|
|
let short_hash: String = hash.chars().take(7).collect();
|
2024-02-28 09:29:05 -07:00
|
|
|
let date = env!("VERGEN_GIT_COMMIT_DATE");
|
|
|
|
|
widget::column::with_children(vec![
|
|
|
|
|
widget::svg(widget::svg::Handle::from_memory(
|
|
|
|
|
&include_bytes!(
|
|
|
|
|
"../res/icons/hicolor/128x128/apps/com.system76.CosmicFiles.svg"
|
|
|
|
|
)[..],
|
|
|
|
|
))
|
|
|
|
|
.into(),
|
|
|
|
|
widget::text::title3(fl!("cosmic-files")).into(),
|
|
|
|
|
widget::button::link(repository)
|
|
|
|
|
.on_press(Message::LaunchUrl(repository.to_string()))
|
|
|
|
|
.padding(0)
|
|
|
|
|
.into(),
|
|
|
|
|
widget::button::link(fl!(
|
|
|
|
|
"git-description",
|
2024-02-28 13:35:29 -07:00
|
|
|
hash = short_hash.as_str(),
|
2024-02-28 09:29:05 -07:00
|
|
|
date = date
|
|
|
|
|
))
|
|
|
|
|
.on_press(Message::LaunchUrl(format!("{}/commits/{}", repository, hash)))
|
|
|
|
|
.padding(0)
|
|
|
|
|
.into(),
|
|
|
|
|
])
|
|
|
|
|
.align_items(Alignment::Center)
|
|
|
|
|
.spacing(space_xxs)
|
|
|
|
|
.into()
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-01 16:10:30 -07:00
|
|
|
fn open_with(&self) -> Element<Message> {
|
|
|
|
|
let mut children = Vec::new();
|
|
|
|
|
let entity = self.tab_model.active();
|
|
|
|
|
if let Some(tab) = self.tab_model.data::<Tab>(entity) {
|
|
|
|
|
if let Some(items) = tab.items_opt() {
|
|
|
|
|
for item in items.iter() {
|
|
|
|
|
if item.selected {
|
|
|
|
|
children.push(item.open_with_view(tab.config.icon_sizes));
|
|
|
|
|
// Only show one property view to avoid issues like hangs when generating
|
|
|
|
|
// preview images on thousands of files
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
widget::settings::view_column(children).into()
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-01 15:14:14 -07:00
|
|
|
fn operations(&self) -> Element<Message> {
|
|
|
|
|
let mut children = Vec::new();
|
|
|
|
|
|
|
|
|
|
//TODO: get height from theme?
|
|
|
|
|
let progress_bar_height = Length::Fixed(4.0);
|
|
|
|
|
|
|
|
|
|
if !self.pending_operations.is_empty() {
|
|
|
|
|
let mut section = widget::settings::view_section(fl!("pending"));
|
2024-02-27 09:58:22 -07:00
|
|
|
for (_id, (op, progress)) in self.pending_operations.iter().rev() {
|
2024-02-01 15:14:14 -07:00
|
|
|
section = section.add(widget::column::with_children(vec![
|
|
|
|
|
widget::text(format!("{:?}", op)).into(),
|
|
|
|
|
widget::progress_bar(0.0..=100.0, *progress)
|
|
|
|
|
.height(progress_bar_height)
|
|
|
|
|
.into(),
|
|
|
|
|
]));
|
|
|
|
|
}
|
|
|
|
|
children.push(section.into());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !self.failed_operations.is_empty() {
|
|
|
|
|
let mut section = widget::settings::view_section(fl!("failed"));
|
2024-02-27 09:58:22 -07:00
|
|
|
for (_id, (op, error)) in self.failed_operations.iter().rev() {
|
2024-02-01 15:14:14 -07:00
|
|
|
section = section.add(widget::column::with_children(vec![
|
|
|
|
|
widget::text(format!("{:?}", op)).into(),
|
|
|
|
|
widget::text(error).into(),
|
|
|
|
|
]));
|
|
|
|
|
}
|
|
|
|
|
children.push(section.into());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !self.complete_operations.is_empty() {
|
|
|
|
|
let mut section = widget::settings::view_section(fl!("complete"));
|
2024-02-27 09:58:22 -07:00
|
|
|
for (_id, op) in self.complete_operations.iter().rev() {
|
2024-02-01 15:14:14 -07:00
|
|
|
section = section.add(widget::text(format!("{:?}", op)));
|
|
|
|
|
}
|
|
|
|
|
children.push(section.into());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
widget::settings::view_column(children).into()
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-22 23:14:44 +02:00
|
|
|
fn properties(&self, entity: Option<ContextItem>) -> Element<Message> {
|
|
|
|
|
match entity {
|
|
|
|
|
None => self.tab_properties(self.tab_model.active()),
|
|
|
|
|
|
|
|
|
|
Some(ContextItem::TabBar(entity)) => self.tab_properties(entity),
|
|
|
|
|
|
|
|
|
|
Some(ContextItem::NavBar(item)) => {
|
|
|
|
|
let mut children = Vec::new();
|
|
|
|
|
|
|
|
|
|
if let Some(location) = self.nav_model.data::<Location>(item) {
|
|
|
|
|
if let Location::Path(path) = location {
|
|
|
|
|
let parent = path.parent().unwrap_or(path);
|
|
|
|
|
|
|
|
|
|
for item in Location::Path(parent.to_owned()).scan(IconSizes::default()) {
|
|
|
|
|
if item.path_opt.as_deref() == Some(path) {
|
|
|
|
|
children.push(item.property_view(IconSizes::default()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
widget::settings::view_column(children).into()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn tab_properties(&self, entity: segmented_button::Entity) -> Element<Message> {
|
2024-02-01 15:14:14 -07:00
|
|
|
let mut children = Vec::new();
|
2024-04-22 23:14:44 +02:00
|
|
|
|
2024-02-01 15:14:14 -07:00
|
|
|
if let Some(tab) = self.tab_model.data::<Tab>(entity) {
|
2024-02-29 13:42:13 -07:00
|
|
|
if let Some(items) = tab.items_opt() {
|
2024-02-01 15:14:14 -07:00
|
|
|
for item in items.iter() {
|
|
|
|
|
if item.selected {
|
2024-02-29 12:26:45 -07:00
|
|
|
children.push(item.property_view(tab.config.icon_sizes));
|
2024-02-22 16:17:39 -07:00
|
|
|
// Only show one property view to avoid issues like hangs when generating
|
|
|
|
|
// preview images on thousands of files
|
|
|
|
|
break;
|
2024-02-01 15:14:14 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-22 23:14:44 +02:00
|
|
|
|
2024-02-01 15:14:14 -07:00
|
|
|
widget::settings::view_column(children).into()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn settings(&self) -> Element<Message> {
|
2024-02-19 03:44:19 -05:00
|
|
|
// TODO: Should dialog be updated here too?
|
2024-02-17 02:07:21 -05:00
|
|
|
widget::settings::view_column(vec![
|
|
|
|
|
widget::settings::view_section(fl!("appearance"))
|
2024-02-29 20:18:34 -07:00
|
|
|
.add({
|
|
|
|
|
let app_theme_selected = match self.config.app_theme {
|
|
|
|
|
AppTheme::Dark => 1,
|
|
|
|
|
AppTheme::Light => 2,
|
|
|
|
|
AppTheme::System => 0,
|
|
|
|
|
};
|
2024-02-17 02:07:21 -05:00
|
|
|
widget::settings::item::builder(fl!("theme")).control(widget::dropdown(
|
|
|
|
|
&self.app_themes,
|
|
|
|
|
Some(app_theme_selected),
|
|
|
|
|
move |index| {
|
|
|
|
|
Message::AppTheme(match index {
|
|
|
|
|
1 => AppTheme::Dark,
|
|
|
|
|
2 => AppTheme::Light,
|
|
|
|
|
_ => AppTheme::System,
|
|
|
|
|
})
|
|
|
|
|
},
|
2024-02-29 20:18:34 -07:00
|
|
|
))
|
|
|
|
|
})
|
|
|
|
|
.add({
|
|
|
|
|
let tab_config = self.config.tab.clone();
|
|
|
|
|
let list: u16 = tab_config.icon_sizes.list.into();
|
|
|
|
|
widget::settings::item::builder(fl!("icon-size-list"))
|
|
|
|
|
.description(format!("{}%", list))
|
|
|
|
|
.control(
|
2024-02-29 20:39:59 -07:00
|
|
|
widget::slider(100..=500, list, move |list| {
|
2024-02-29 20:18:34 -07:00
|
|
|
Message::TabConfig(TabConfig {
|
|
|
|
|
icon_sizes: IconSizes {
|
|
|
|
|
list: NonZeroU16::new(list).unwrap(),
|
|
|
|
|
..tab_config.icon_sizes
|
|
|
|
|
},
|
|
|
|
|
..tab_config
|
2024-02-19 03:44:19 -05:00
|
|
|
})
|
2024-02-29 20:18:34 -07:00
|
|
|
})
|
2024-03-01 09:18:57 -07:00
|
|
|
.step(25u16),
|
2024-02-29 20:18:34 -07:00
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
.add({
|
|
|
|
|
let tab_config = self.config.tab.clone();
|
|
|
|
|
let grid: u16 = tab_config.icon_sizes.grid.into();
|
|
|
|
|
widget::settings::item::builder(fl!("icon-size-grid"))
|
|
|
|
|
.description(format!("{}%", grid))
|
|
|
|
|
.control(
|
2024-02-29 20:35:40 -07:00
|
|
|
widget::slider(50..=500, grid, move |grid| {
|
2024-02-29 20:18:34 -07:00
|
|
|
Message::TabConfig(TabConfig {
|
|
|
|
|
icon_sizes: IconSizes {
|
|
|
|
|
grid: NonZeroU16::new(grid).unwrap(),
|
|
|
|
|
..tab_config.icon_sizes
|
|
|
|
|
},
|
|
|
|
|
..tab_config
|
2024-02-19 03:44:19 -05:00
|
|
|
})
|
2024-02-29 20:18:34 -07:00
|
|
|
})
|
2024-03-01 09:18:57 -07:00
|
|
|
.step(25u16),
|
2024-02-29 20:18:34 -07:00
|
|
|
)
|
|
|
|
|
})
|
2024-03-14 02:17:27 -04:00
|
|
|
.add({
|
|
|
|
|
let tab_config = self.config.tab;
|
|
|
|
|
let sort_by_selected = tab_config.sort_name as _;
|
|
|
|
|
|
2024-03-16 02:39:56 -04:00
|
|
|
widget::settings::item::builder(fl!("sorting-name")).control(widget::dropdown(
|
|
|
|
|
&self.sort_by_names,
|
|
|
|
|
Some(sort_by_selected),
|
|
|
|
|
move |index| {
|
|
|
|
|
Message::TabConfig(TabConfig {
|
|
|
|
|
sort_name: match index {
|
|
|
|
|
0 => HeadingOptions::Name,
|
|
|
|
|
1 => HeadingOptions::Modified,
|
|
|
|
|
2 => HeadingOptions::Size,
|
|
|
|
|
_ => HeadingOptions::Name,
|
|
|
|
|
},
|
|
|
|
|
..tab_config
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
))
|
|
|
|
|
})
|
|
|
|
|
.add({
|
|
|
|
|
let tab_config = self.config.tab;
|
|
|
|
|
// Ascending is true. Descending is false
|
|
|
|
|
let direction = tab_config.sort_direction.into();
|
|
|
|
|
|
|
|
|
|
widget::settings::item::builder(fl!("direction")).control(widget::dropdown(
|
|
|
|
|
&self.sort_direction,
|
|
|
|
|
Some(direction),
|
|
|
|
|
move |index| {
|
|
|
|
|
Message::TabConfig(TabConfig {
|
|
|
|
|
sort_direction: index == 1,
|
|
|
|
|
..tab_config
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
))
|
2024-03-14 02:17:27 -04:00
|
|
|
})
|
2024-02-17 02:07:21 -05:00
|
|
|
.into(),
|
|
|
|
|
widget::settings::view_section(fl!("settings-tab"))
|
2024-02-29 20:18:34 -07:00
|
|
|
.add({
|
|
|
|
|
let tab_config = self.config.tab.clone();
|
|
|
|
|
widget::settings::item::builder(fl!("settings-show-hidden")).toggler(
|
|
|
|
|
tab_config.show_hidden,
|
|
|
|
|
move |show_hidden| {
|
|
|
|
|
Message::TabConfig(TabConfig {
|
|
|
|
|
show_hidden,
|
|
|
|
|
..tab_config
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
})
|
2024-02-17 02:07:21 -05:00
|
|
|
.into(),
|
|
|
|
|
])
|
2024-02-01 15:14:14 -07:00
|
|
|
.into()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Implement [`Application`] to integrate with COSMIC.
|
|
|
|
|
impl Application for App {
|
|
|
|
|
/// Default async executor to use with the app.
|
|
|
|
|
type Executor = executor::Default;
|
|
|
|
|
|
|
|
|
|
/// Argument received
|
|
|
|
|
type Flags = Flags;
|
|
|
|
|
|
|
|
|
|
/// Message type specific to our [`App`].
|
|
|
|
|
type Message = Message;
|
|
|
|
|
|
|
|
|
|
/// The unique application ID to supply to the window manager.
|
|
|
|
|
const APP_ID: &'static str = "com.system76.CosmicFiles";
|
|
|
|
|
|
|
|
|
|
fn core(&self) -> &Core {
|
|
|
|
|
&self.core
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn core_mut(&mut self) -> &mut Core {
|
|
|
|
|
&mut self.core
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-10 11:41:25 -04:00
|
|
|
fn nav_bar(&self) -> Option<Element<message::Message<Self::Message>>> {
|
|
|
|
|
if !self.core().nav_bar_active() {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let nav_model = self.nav_model()?;
|
|
|
|
|
|
2024-04-22 23:14:44 +02:00
|
|
|
let mut nav = cosmic::widget::nav_bar(nav_model, |entity| {
|
|
|
|
|
cosmic::app::Message::Cosmic(cosmic::app::cosmic::Message::NavBar(entity))
|
|
|
|
|
})
|
|
|
|
|
.drag_id(self.nav_drag_id)
|
|
|
|
|
.on_dnd_enter(|entity, _| cosmic::app::Message::App(Message::DndEnterNav(entity)))
|
|
|
|
|
.on_dnd_leave(|_| cosmic::app::Message::App(Message::DndExitNav))
|
|
|
|
|
.on_dnd_drop(|entity, data, action| {
|
|
|
|
|
cosmic::app::Message::App(Message::DndDropNav(entity, data, action))
|
|
|
|
|
})
|
|
|
|
|
.on_context(|entity| cosmic::app::Message::App(Message::NavBarContext(entity)))
|
2024-04-24 13:59:55 -06:00
|
|
|
.on_close(|entity| cosmic::app::Message::App(Message::NavBarClose(entity)))
|
2024-04-22 23:14:44 +02:00
|
|
|
.context_menu(self.nav_context_menu(self.nav_bar_context_id))
|
2024-04-24 13:59:55 -06:00
|
|
|
.close_icon(
|
|
|
|
|
widget::icon::from_name("media-eject-symbolic")
|
|
|
|
|
.size(16)
|
|
|
|
|
.icon(),
|
|
|
|
|
)
|
2024-04-22 23:14:44 +02:00
|
|
|
.into_container();
|
2024-04-10 11:41:25 -04:00
|
|
|
|
|
|
|
|
if !self.core().is_condensed() {
|
|
|
|
|
nav = nav.max_width(280);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Some(Element::from(
|
|
|
|
|
// XXX both must be shrink to avoid flex layout from ignoring it
|
|
|
|
|
nav.width(Length::Shrink).height(Length::Shrink),
|
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-01 15:14:14 -07:00
|
|
|
/// Creates the application, and optionally emits command on initialize.
|
2024-02-29 19:47:27 -07:00
|
|
|
fn init(mut core: Core, flags: Self::Flags) -> (Self, Command<Self::Message>) {
|
|
|
|
|
//TODO: make set_nav_bar_toggle_condensed pub
|
|
|
|
|
core.nav_bar_toggle_condensed();
|
|
|
|
|
|
2024-02-01 15:14:14 -07:00
|
|
|
let app_themes = vec![fl!("match-desktop"), fl!("dark"), fl!("light")];
|
|
|
|
|
|
|
|
|
|
let mut app = App {
|
|
|
|
|
core,
|
2024-04-22 23:14:44 +02:00
|
|
|
nav_bar_context_id: segmented_button::Entity::null(),
|
2024-05-09 12:26:55 -06:00
|
|
|
nav_model: segmented_button::ModelBuilder::default().build(),
|
2024-02-01 15:14:14 -07:00
|
|
|
tab_model: segmented_button::ModelBuilder::default().build(),
|
|
|
|
|
config_handler: flags.config_handler,
|
|
|
|
|
config: flags.config,
|
|
|
|
|
app_themes,
|
2024-03-14 02:17:27 -04:00
|
|
|
sort_by_names: HeadingOptions::names(),
|
2024-03-16 02:39:56 -04:00
|
|
|
sort_direction: vec![fl!("descending"), fl!("ascending")],
|
2024-02-01 15:14:14 -07:00
|
|
|
context_page: ContextPage::Settings,
|
2024-02-27 13:48:12 -07:00
|
|
|
dialog_pages: VecDeque::new(),
|
2024-02-27 13:03:39 -07:00
|
|
|
dialog_text_input: widget::Id::unique(),
|
2024-02-01 15:14:14 -07:00
|
|
|
key_binds: key_binds(),
|
|
|
|
|
modifiers: Modifiers::empty(),
|
2024-04-22 13:14:25 -06:00
|
|
|
mounters: mounters(),
|
|
|
|
|
mounter_items: HashMap::new(),
|
2024-02-01 15:14:14 -07:00
|
|
|
pending_operation_id: 0,
|
|
|
|
|
pending_operations: BTreeMap::new(),
|
|
|
|
|
complete_operations: BTreeMap::new(),
|
|
|
|
|
failed_operations: BTreeMap::new(),
|
|
|
|
|
watcher_opt: None,
|
2024-04-10 11:41:25 -04:00
|
|
|
nav_dnd_hover: None,
|
|
|
|
|
tab_dnd_hover: None,
|
|
|
|
|
nav_drag_id: DragId::new(),
|
|
|
|
|
tab_drag_id: DragId::new(),
|
2024-02-01 15:14:14 -07:00
|
|
|
};
|
|
|
|
|
|
2024-05-09 12:26:55 -06:00
|
|
|
let mut commands = vec![app.update_config()];
|
2024-02-01 15:14:14 -07:00
|
|
|
|
|
|
|
|
for arg in env::args().skip(1) {
|
2024-03-17 03:03:49 +01:00
|
|
|
let location = if &arg == "--trash" {
|
|
|
|
|
Location::Trash
|
|
|
|
|
} else {
|
|
|
|
|
match fs::canonicalize(&arg) {
|
|
|
|
|
Ok(absolute) => Location::Path(absolute),
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::warn!("failed to canonicalize {:?}: {}", arg, err);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2024-02-01 15:14:14 -07:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
commands.push(app.open_tab(location));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if app.tab_model.iter().next().is_none() {
|
2024-03-04 10:30:59 -07:00
|
|
|
if let Ok(current_dir) = env::current_dir() {
|
|
|
|
|
commands.push(app.open_tab(Location::Path(current_dir)));
|
|
|
|
|
} else {
|
|
|
|
|
commands.push(app.open_tab(Location::Path(home_dir())));
|
|
|
|
|
}
|
2024-02-01 15:14:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
(app, Command::batch(commands))
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-22 23:14:44 +02:00
|
|
|
fn nav_context_menu(
|
|
|
|
|
&self,
|
|
|
|
|
id: widget::nav_bar::Id,
|
|
|
|
|
) -> Option<Vec<widget::menu::Tree<cosmic::app::Message<Self::Message>>>> {
|
|
|
|
|
Some(cosmic::widget::menu::items(
|
|
|
|
|
&HashMap::new(),
|
|
|
|
|
vec![
|
|
|
|
|
cosmic::widget::menu::Item::Button(
|
|
|
|
|
fl!("open-in-new-tab"),
|
|
|
|
|
NavMenuAction::OpenInNewTab(id),
|
|
|
|
|
),
|
|
|
|
|
cosmic::widget::menu::Item::Button(
|
|
|
|
|
fl!("open-in-new-window"),
|
|
|
|
|
NavMenuAction::OpenInNewWindow(id),
|
|
|
|
|
),
|
|
|
|
|
cosmic::widget::menu::Item::Divider,
|
|
|
|
|
cosmic::widget::menu::Item::Button(
|
|
|
|
|
fl!("properties"),
|
|
|
|
|
NavMenuAction::Properties(id),
|
|
|
|
|
),
|
2024-05-09 12:26:55 -06:00
|
|
|
cosmic::widget::menu::Item::Divider,
|
|
|
|
|
cosmic::widget::menu::Item::Button(
|
|
|
|
|
fl!("remove-from-sidebar"),
|
|
|
|
|
NavMenuAction::RemoveFromSidebar(id),
|
|
|
|
|
),
|
2024-04-22 23:14:44 +02:00
|
|
|
],
|
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-01 15:14:14 -07:00
|
|
|
fn nav_model(&self) -> Option<&segmented_button::SingleSelectModel> {
|
|
|
|
|
Some(&self.nav_model)
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-04 10:28:16 -07:00
|
|
|
fn on_nav_select(&mut self, entity: Entity) -> Command<Self::Message> {
|
2024-04-22 13:14:25 -06:00
|
|
|
if let Some(location) = self.nav_model.data::<Location>(entity) {
|
2024-02-01 15:14:14 -07:00
|
|
|
let message = Message::TabMessage(None, tab::Message::Location(location.clone()));
|
|
|
|
|
return self.update(message);
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-22 13:14:25 -06:00
|
|
|
if let Some(data) = self.nav_model.data::<MounterData>(entity).clone() {
|
|
|
|
|
if let Some(mounter) = self.mounters.get(&data.0) {
|
|
|
|
|
return mounter.mount(data.1.clone()).map(|_| message::none());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-01 15:14:14 -07:00
|
|
|
Command::none()
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-21 23:54:22 -05:00
|
|
|
fn on_escape(&mut self) -> Command<Self::Message> {
|
|
|
|
|
let entity = self.tab_model.active();
|
|
|
|
|
|
2024-02-27 13:03:39 -07:00
|
|
|
// Close dialog if open
|
2024-02-27 13:48:12 -07:00
|
|
|
if self.dialog_pages.pop_front().is_some() {
|
2024-02-27 13:03:39 -07:00
|
|
|
return Command::none();
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-21 23:54:22 -05:00
|
|
|
// Close menus and context panes in order per message
|
|
|
|
|
// Why: It'd be weird to close everything all at once
|
|
|
|
|
// Usually, the Escape key (for example) closes menus and panes one by one instead
|
|
|
|
|
// of closing everything on one press
|
|
|
|
|
// TODO: Close MenuBar too
|
2024-02-28 16:16:59 -07:00
|
|
|
if let Some(tab) = self.tab_model.data_mut::<Tab>(entity) {
|
|
|
|
|
if tab.context_menu.is_some() {
|
|
|
|
|
tab.context_menu = None;
|
|
|
|
|
return Command::none();
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-29 20:53:15 -07:00
|
|
|
let had_focused_button = tab.select_focus_id().is_some();
|
2024-02-29 13:42:13 -07:00
|
|
|
if tab.select_none() {
|
2024-02-29 20:53:15 -07:00
|
|
|
if had_focused_button {
|
|
|
|
|
// Unfocus if there was a focused button
|
|
|
|
|
return widget::button::focus(widget::Id::unique());
|
|
|
|
|
}
|
2024-02-29 13:42:13 -07:00
|
|
|
return Command::none();
|
2024-02-28 16:16:59 -07:00
|
|
|
}
|
2024-02-21 23:54:22 -05:00
|
|
|
}
|
2024-02-28 16:16:59 -07:00
|
|
|
self.core.window.show_context = false;
|
2024-02-21 23:54:22 -05:00
|
|
|
|
|
|
|
|
Command::none()
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-01 15:14:14 -07:00
|
|
|
/// Handle application events here.
|
|
|
|
|
fn update(&mut self, message: Self::Message) -> Command<Self::Message> {
|
|
|
|
|
// Helper for updating config values efficiently
|
|
|
|
|
macro_rules! config_set {
|
|
|
|
|
($name: ident, $value: expr) => {
|
|
|
|
|
match &self.config_handler {
|
|
|
|
|
Some(config_handler) => {
|
|
|
|
|
match paste::paste! { self.config.[<set_ $name>](config_handler, $value) } {
|
|
|
|
|
Ok(_) => {}
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::warn!(
|
|
|
|
|
"failed to save config {:?}: {}",
|
|
|
|
|
stringify!($name),
|
|
|
|
|
err
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
None => {
|
|
|
|
|
self.config.$name = $value;
|
|
|
|
|
log::warn!(
|
|
|
|
|
"failed to save config {:?}: no config handler",
|
|
|
|
|
stringify!($name)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match message {
|
2024-05-09 12:48:33 -06:00
|
|
|
Message::AddToSidebar(entity_opt) => {
|
|
|
|
|
let mut favorites = self.config.favorites.clone();
|
|
|
|
|
for path in self.selected_paths(entity_opt) {
|
|
|
|
|
favorites.push(Favorite::from_path(path));
|
|
|
|
|
}
|
|
|
|
|
config_set!(favorites, favorites);
|
|
|
|
|
return self.update_config();
|
|
|
|
|
}
|
2024-02-01 15:14:14 -07:00
|
|
|
Message::AppTheme(app_theme) => {
|
|
|
|
|
config_set!(app_theme, app_theme);
|
|
|
|
|
return self.update_config();
|
|
|
|
|
}
|
|
|
|
|
Message::Config(config) => {
|
|
|
|
|
if config != self.config {
|
|
|
|
|
log::info!("update config");
|
|
|
|
|
//TODO: update syntax theme by clearing tabs, only if needed
|
|
|
|
|
self.config = config;
|
|
|
|
|
return self.update_config();
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-20 08:42:28 -06:00
|
|
|
Message::Copy(entity_opt) => {
|
2024-03-20 09:56:54 -06:00
|
|
|
let paths = self.selected_paths(entity_opt);
|
2024-03-20 10:27:35 -06:00
|
|
|
let contents = ClipboardCopy::new(ClipboardKind::Copy, &paths);
|
2024-03-20 08:42:28 -06:00
|
|
|
return clipboard::write_data(contents);
|
2024-02-01 15:14:14 -07:00
|
|
|
}
|
2024-03-20 08:53:08 -06:00
|
|
|
Message::Cut(entity_opt) => {
|
2024-03-20 09:56:54 -06:00
|
|
|
let paths = self.selected_paths(entity_opt);
|
2024-03-20 10:27:35 -06:00
|
|
|
let contents = ClipboardCopy::new(ClipboardKind::Cut, &paths);
|
2024-03-20 08:53:08 -06:00
|
|
|
return clipboard::write_data(contents);
|
2024-02-01 15:14:14 -07:00
|
|
|
}
|
2024-02-27 13:03:39 -07:00
|
|
|
Message::DialogCancel => {
|
2024-02-27 13:48:12 -07:00
|
|
|
self.dialog_pages.pop_front();
|
2024-02-27 13:03:39 -07:00
|
|
|
}
|
|
|
|
|
Message::DialogComplete => {
|
2024-02-27 13:48:12 -07:00
|
|
|
if let Some(dialog_page) = self.dialog_pages.pop_front() {
|
2024-02-27 13:03:39 -07:00
|
|
|
match dialog_page {
|
2024-05-09 13:24:06 -06:00
|
|
|
DialogPage::EmptyTrash => {
|
|
|
|
|
self.operation(Operation::EmptyTrash);
|
|
|
|
|
}
|
2024-02-27 13:48:12 -07:00
|
|
|
DialogPage::FailedOperation(id) => {
|
|
|
|
|
log::warn!("TODO: retry operation {}", id);
|
|
|
|
|
}
|
2024-02-27 13:03:39 -07:00
|
|
|
DialogPage::NewItem { parent, name, dir } => {
|
|
|
|
|
let path = parent.join(name);
|
2024-02-27 13:25:50 -07:00
|
|
|
self.operation(if dir {
|
|
|
|
|
Operation::NewFolder { path }
|
2024-02-27 13:03:39 -07:00
|
|
|
} else {
|
2024-02-27 13:25:50 -07:00
|
|
|
Operation::NewFile { path }
|
|
|
|
|
});
|
2024-02-27 13:03:39 -07:00
|
|
|
}
|
2024-02-28 15:07:50 -07:00
|
|
|
DialogPage::RenameItem {
|
|
|
|
|
from, parent, name, ..
|
|
|
|
|
} => {
|
|
|
|
|
let to = parent.join(name);
|
|
|
|
|
self.operation(Operation::Rename { from, to });
|
|
|
|
|
}
|
2024-02-27 13:03:39 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-27 13:48:12 -07:00
|
|
|
Message::DialogUpdate(dialog_page) => {
|
|
|
|
|
//TODO: panicless way to do this?
|
|
|
|
|
self.dialog_pages[0] = dialog_page;
|
2024-02-27 13:03:39 -07:00
|
|
|
}
|
2024-02-28 15:19:07 -07:00
|
|
|
Message::EditLocation(entity_opt) => {
|
|
|
|
|
let entity = entity_opt.unwrap_or_else(|| self.tab_model.active());
|
|
|
|
|
if let Some(location) = self.tab_model.data::<Tab>(entity).and_then(|tab| {
|
|
|
|
|
if tab.edit_location.is_none() {
|
|
|
|
|
Some(tab.location.clone())
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}) {
|
|
|
|
|
return self.update(Message::TabMessage(
|
|
|
|
|
Some(entity),
|
|
|
|
|
tab::Message::EditLocation(Some(location)),
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-09 07:09:51 -07:00
|
|
|
Message::Key(modifiers, key) => {
|
2024-02-01 15:14:14 -07:00
|
|
|
let entity = self.tab_model.active();
|
|
|
|
|
for (key_bind, action) in self.key_binds.iter() {
|
2024-02-09 07:09:51 -07:00
|
|
|
if key_bind.matches(modifiers, &key) {
|
2024-02-01 15:14:14 -07:00
|
|
|
return self.update(action.message(Some(entity)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-28 09:29:05 -07:00
|
|
|
Message::LaunchUrl(url) => match open::that_detached(&url) {
|
|
|
|
|
Ok(()) => {}
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::warn!("failed to open {:?}: {}", url, err);
|
|
|
|
|
}
|
|
|
|
|
},
|
2024-02-01 15:14:14 -07:00
|
|
|
Message::Modifiers(modifiers) => {
|
|
|
|
|
self.modifiers = modifiers;
|
|
|
|
|
}
|
|
|
|
|
Message::MoveToTrash(entity_opt) => {
|
2024-03-20 09:56:54 -06:00
|
|
|
let paths = self.selected_paths(entity_opt);
|
2024-02-01 15:14:14 -07:00
|
|
|
if !paths.is_empty() {
|
|
|
|
|
self.operation(Operation::Delete { paths });
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-22 13:14:25 -06:00
|
|
|
Message::MounterItems(mounter_key, mounter_items) => {
|
|
|
|
|
// Insert new items
|
|
|
|
|
self.mounter_items.insert(mounter_key, mounter_items);
|
|
|
|
|
|
2024-05-09 12:26:55 -06:00
|
|
|
// Update nav bar
|
2024-05-09 12:48:33 -06:00
|
|
|
//TODO: this could change favorites IDs while they are in use
|
2024-05-09 12:26:55 -06:00
|
|
|
self.update_nav_model();
|
2024-04-22 13:14:25 -06:00
|
|
|
}
|
2024-02-27 13:03:39 -07:00
|
|
|
Message::NewItem(entity_opt, dir) => {
|
2024-02-27 13:48:12 -07:00
|
|
|
let entity = entity_opt.unwrap_or_else(|| self.tab_model.active());
|
|
|
|
|
if let Some(tab) = self.tab_model.data_mut::<Tab>(entity) {
|
|
|
|
|
if let Location::Path(path) = &tab.location {
|
|
|
|
|
self.dialog_pages.push_back(DialogPage::NewItem {
|
|
|
|
|
parent: path.clone(),
|
|
|
|
|
name: String::new(),
|
|
|
|
|
dir,
|
|
|
|
|
});
|
|
|
|
|
return widget::text_input::focus(self.dialog_text_input.clone());
|
2024-02-27 13:03:39 -07:00
|
|
|
}
|
|
|
|
|
}
|
2024-02-01 15:14:14 -07:00
|
|
|
}
|
2024-03-20 11:54:37 -06:00
|
|
|
Message::NotifyEvents(events) => {
|
|
|
|
|
log::debug!("{:?}", events);
|
2024-02-01 15:14:14 -07:00
|
|
|
|
|
|
|
|
let mut needs_reload = Vec::new();
|
2024-03-20 16:27:00 -06:00
|
|
|
let entities: Vec<_> = self.tab_model.iter().collect();
|
|
|
|
|
for entity in entities {
|
|
|
|
|
if let Some(tab) = self.tab_model.data_mut::<Tab>(entity) {
|
2024-02-01 15:14:14 -07:00
|
|
|
//TODO: support reloading trash, somehow
|
|
|
|
|
if let Location::Path(path) = &tab.location {
|
|
|
|
|
let mut contains_change = false;
|
2024-03-20 11:54:37 -06:00
|
|
|
for event in events.iter() {
|
|
|
|
|
for event_path in event.paths.iter() {
|
|
|
|
|
if event_path.starts_with(&path) {
|
2024-03-20 16:27:00 -06:00
|
|
|
match event.kind {
|
|
|
|
|
notify::EventKind::Modify(
|
|
|
|
|
notify::event::ModifyKind::Metadata(_),
|
|
|
|
|
)
|
|
|
|
|
| notify::EventKind::Modify(
|
|
|
|
|
notify::event::ModifyKind::Data(_),
|
|
|
|
|
) => {
|
|
|
|
|
// If metadata or data changed, find the matching item and reload it
|
|
|
|
|
//TODO: this could be further optimized by looking at what exactly changed
|
|
|
|
|
if let Some(items) = &mut tab.items_opt {
|
|
|
|
|
for item in items.iter_mut() {
|
|
|
|
|
if item.path_opt.as_ref()
|
|
|
|
|
== Some(event_path)
|
|
|
|
|
{
|
|
|
|
|
//TODO: reload more, like mime types?
|
|
|
|
|
match fs::metadata(&event_path) {
|
|
|
|
|
Ok(new_metadata) => match &mut item
|
|
|
|
|
.metadata
|
|
|
|
|
{
|
|
|
|
|
ItemMetadata::Path {
|
|
|
|
|
metadata,
|
|
|
|
|
..
|
|
|
|
|
} => *metadata = new_metadata,
|
|
|
|
|
_ => {}
|
|
|
|
|
},
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::warn!("failed to reload metadata for {:?}: {}", path, err);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//TODO item.thumbnail_opt =
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_ => {
|
|
|
|
|
// Any other events reload the whole tab
|
|
|
|
|
contains_change = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-20 11:54:37 -06:00
|
|
|
}
|
2024-02-01 15:14:14 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if contains_change {
|
|
|
|
|
needs_reload.push((entity, tab.location.clone()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut commands = Vec::with_capacity(needs_reload.len());
|
|
|
|
|
for (entity, location) in needs_reload {
|
|
|
|
|
commands.push(self.rescan_tab(entity, location));
|
|
|
|
|
}
|
|
|
|
|
return Command::batch(commands);
|
|
|
|
|
}
|
|
|
|
|
Message::NotifyWatcher(mut watcher_wrapper) => match watcher_wrapper.watcher_opt.take()
|
|
|
|
|
{
|
2024-02-20 11:58:39 -07:00
|
|
|
Some(watcher) => {
|
2024-02-01 15:14:14 -07:00
|
|
|
self.watcher_opt = Some((watcher, HashSet::new()));
|
|
|
|
|
return self.update_watcher();
|
|
|
|
|
}
|
|
|
|
|
None => {
|
|
|
|
|
log::warn!("message did not contain notify watcher");
|
|
|
|
|
}
|
|
|
|
|
},
|
2024-03-04 10:28:16 -07:00
|
|
|
Message::OpenTerminal(entity_opt) => {
|
|
|
|
|
if let Some(terminal) = mime_app::terminal() {
|
|
|
|
|
let mut paths = Vec::new();
|
|
|
|
|
let entity = entity_opt.unwrap_or_else(|| self.tab_model.active());
|
|
|
|
|
if let Some(tab) = self.tab_model.data_mut::<Tab>(entity) {
|
|
|
|
|
if let Location::Path(path) = &tab.location {
|
|
|
|
|
if let Some(items) = tab.items_opt() {
|
|
|
|
|
for item in items.iter() {
|
|
|
|
|
if item.selected {
|
2024-03-20 09:56:54 -06:00
|
|
|
if let Some(path) = &item.path_opt {
|
|
|
|
|
paths.push(path.clone());
|
|
|
|
|
}
|
2024-03-04 10:28:16 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if paths.is_empty() {
|
|
|
|
|
paths.push(path.clone());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for path in paths {
|
|
|
|
|
if let Some(mut command) = terminal.command(None) {
|
|
|
|
|
command.current_dir(&path);
|
|
|
|
|
match spawn_detached(&mut command) {
|
|
|
|
|
Ok(()) => {}
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::warn!(
|
2024-03-04 10:39:48 -07:00
|
|
|
"failed to open {:?} with terminal {:?}: {}",
|
2024-03-04 10:28:16 -07:00
|
|
|
path,
|
2024-03-04 10:39:48 -07:00
|
|
|
terminal.id,
|
2024-03-04 10:28:16 -07:00
|
|
|
err
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
log::warn!("failed to get command for {:?}", terminal.id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-04 10:39:48 -07:00
|
|
|
Message::OpenWith(path, app) => {
|
|
|
|
|
if let Some(mut command) = app.command(Some(path.clone())) {
|
|
|
|
|
match spawn_detached(&mut command) {
|
|
|
|
|
Ok(()) => {}
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::warn!("failed to open {:?} with {:?}: {}", path, app.id, err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
log::warn!("failed to get command for {:?}", app.id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Close Open With context view
|
|
|
|
|
self.core.window.show_context = false;
|
|
|
|
|
}
|
2024-03-20 10:27:35 -06:00
|
|
|
Message::Paste(entity_opt) => {
|
2024-03-20 11:54:37 -06:00
|
|
|
let entity = entity_opt.unwrap_or_else(|| self.tab_model.active());
|
|
|
|
|
if let Some(tab) = self.tab_model.data_mut::<Tab>(entity) {
|
|
|
|
|
if let Location::Path(path) = &tab.location {
|
|
|
|
|
let to = path.clone();
|
|
|
|
|
return clipboard::read_data::<ClipboardPaste, _>(move |contents_opt| {
|
|
|
|
|
match contents_opt {
|
|
|
|
|
Some(contents) => {
|
|
|
|
|
message::app(Message::PasteContents(to.clone(), contents))
|
|
|
|
|
}
|
|
|
|
|
None => message::none(),
|
|
|
|
|
}
|
|
|
|
|
});
|
2024-03-20 10:27:35 -06:00
|
|
|
}
|
2024-03-20 11:54:37 -06:00
|
|
|
}
|
2024-03-20 10:27:35 -06:00
|
|
|
}
|
2024-04-10 13:36:07 -04:00
|
|
|
Message::PasteContents(to, mut contents) => {
|
|
|
|
|
contents.paths.retain(|p| p != &to);
|
2024-03-20 11:54:37 -06:00
|
|
|
if !contents.paths.is_empty() {
|
|
|
|
|
match contents.kind {
|
|
|
|
|
ClipboardKind::Copy => {
|
|
|
|
|
self.operation(Operation::Copy {
|
|
|
|
|
paths: contents.paths,
|
|
|
|
|
to,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
ClipboardKind::Cut => {
|
|
|
|
|
self.operation(Operation::Move {
|
|
|
|
|
paths: contents.paths,
|
|
|
|
|
to,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-01 15:14:14 -07:00
|
|
|
}
|
|
|
|
|
Message::PendingComplete(id) => {
|
|
|
|
|
if let Some((op, _)) = self.pending_operations.remove(&id) {
|
|
|
|
|
self.complete_operations.insert(id, op);
|
|
|
|
|
}
|
2024-02-28 14:42:46 -07:00
|
|
|
// Manually rescan any trash tabs after any operation is completed
|
|
|
|
|
return self.rescan_trash();
|
2024-02-01 15:14:14 -07:00
|
|
|
}
|
|
|
|
|
Message::PendingError(id, err) => {
|
|
|
|
|
if let Some((op, _)) = self.pending_operations.remove(&id) {
|
|
|
|
|
self.failed_operations.insert(id, (op, err));
|
2024-02-27 13:48:12 -07:00
|
|
|
self.dialog_pages.push_back(DialogPage::FailedOperation(id));
|
2024-02-01 15:14:14 -07:00
|
|
|
}
|
2024-02-28 14:42:46 -07:00
|
|
|
// Manually rescan any trash tabs after any operation is completed
|
|
|
|
|
return self.rescan_trash();
|
2024-02-01 15:14:14 -07:00
|
|
|
}
|
|
|
|
|
Message::PendingProgress(id, new_progress) => {
|
|
|
|
|
if let Some((_, progress)) = self.pending_operations.get_mut(&id) {
|
|
|
|
|
*progress = new_progress;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-21 00:44:40 -04:00
|
|
|
Message::RescanTrash => {
|
|
|
|
|
// Update trash icon if empty/full
|
|
|
|
|
let maybe_entity = self.nav_model.iter().find(|&entity| {
|
|
|
|
|
self.nav_model
|
|
|
|
|
.data::<Location>(entity)
|
|
|
|
|
.map(|loc| *loc == Location::Trash)
|
|
|
|
|
.unwrap_or_default()
|
|
|
|
|
});
|
|
|
|
|
if let Some(entity) = maybe_entity {
|
|
|
|
|
self.nav_model
|
|
|
|
|
.icon_set(entity, widget::icon::icon(tab::trash_icon_symbolic(16)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return self.rescan_trash();
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-28 15:07:50 -07:00
|
|
|
Message::Rename(entity_opt) => {
|
|
|
|
|
let entity = entity_opt.unwrap_or_else(|| self.tab_model.active());
|
|
|
|
|
if let Some(tab) = self.tab_model.data_mut::<Tab>(entity) {
|
|
|
|
|
if let Location::Path(parent) = &tab.location {
|
2024-02-29 13:42:13 -07:00
|
|
|
if let Some(items) = tab.items_opt() {
|
2024-02-28 15:07:50 -07:00
|
|
|
let mut selected = Vec::new();
|
|
|
|
|
for item in items.iter() {
|
|
|
|
|
if item.selected {
|
2024-03-20 09:56:54 -06:00
|
|
|
if let Some(path) = &item.path_opt {
|
|
|
|
|
selected.push(path.clone());
|
|
|
|
|
}
|
2024-02-28 15:07:50 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !selected.is_empty() {
|
|
|
|
|
//TODO: batch rename
|
|
|
|
|
for path in selected {
|
|
|
|
|
let name = match path.file_name().and_then(|x| x.to_str()) {
|
|
|
|
|
Some(some) => some.to_string(),
|
|
|
|
|
None => continue,
|
|
|
|
|
};
|
|
|
|
|
let dir = path.is_dir();
|
|
|
|
|
self.dialog_pages.push_back(DialogPage::RenameItem {
|
|
|
|
|
from: path,
|
|
|
|
|
parent: parent.clone(),
|
|
|
|
|
name,
|
|
|
|
|
dir,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return widget::text_input::focus(self.dialog_text_input.clone());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-01 15:14:14 -07:00
|
|
|
Message::RestoreFromTrash(entity_opt) => {
|
|
|
|
|
let mut paths = Vec::new();
|
|
|
|
|
let entity = entity_opt.unwrap_or_else(|| self.tab_model.active());
|
|
|
|
|
if let Some(tab) = self.tab_model.data_mut::<Tab>(entity) {
|
2024-02-29 13:42:13 -07:00
|
|
|
if let Some(items) = tab.items_opt() {
|
|
|
|
|
for item in items.iter() {
|
2024-02-01 15:14:14 -07:00
|
|
|
if item.selected {
|
|
|
|
|
match &item.metadata {
|
|
|
|
|
ItemMetadata::Trash { entry, .. } => {
|
|
|
|
|
paths.push(entry.clone());
|
|
|
|
|
}
|
|
|
|
|
_ => {
|
|
|
|
|
//TODO: error on trying to restore non-trash file?
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !paths.is_empty() {
|
|
|
|
|
self.operation(Operation::Restore { paths });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Message::SystemThemeModeChange(_theme_mode) => {
|
|
|
|
|
return self.update_config();
|
|
|
|
|
}
|
|
|
|
|
Message::TabActivate(entity) => {
|
|
|
|
|
self.tab_model.activate(entity);
|
|
|
|
|
return self.update_title();
|
|
|
|
|
}
|
|
|
|
|
Message::TabNext => {
|
|
|
|
|
let len = self.tab_model.iter().count();
|
|
|
|
|
let pos = self
|
|
|
|
|
.tab_model
|
|
|
|
|
.position(self.tab_model.active())
|
|
|
|
|
// Wraparound to 0 if i + 1 > num of tabs
|
|
|
|
|
.map(|i| (i as usize + 1) % len)
|
|
|
|
|
.expect("should always be at least one tab open");
|
|
|
|
|
|
|
|
|
|
let entity = self.tab_model.iter().nth(pos);
|
|
|
|
|
if let Some(entity) = entity {
|
|
|
|
|
return self.update(Message::TabActivate(entity));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Message::TabPrev => {
|
|
|
|
|
let pos = self
|
|
|
|
|
.tab_model
|
|
|
|
|
.position(self.tab_model.active())
|
|
|
|
|
.and_then(|i| (i as usize).checked_sub(1))
|
|
|
|
|
// Subtraction underflow => last tab; i.e. it wraps around
|
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
|
self.tab_model
|
|
|
|
|
.iter()
|
|
|
|
|
.count()
|
|
|
|
|
.checked_sub(1)
|
|
|
|
|
.unwrap_or_default()
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let entity = self.tab_model.iter().nth(pos);
|
|
|
|
|
if let Some(entity) = entity {
|
|
|
|
|
return self.update(Message::TabActivate(entity));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Message::TabClose(entity_opt) => {
|
|
|
|
|
let entity = entity_opt.unwrap_or_else(|| self.tab_model.active());
|
|
|
|
|
|
|
|
|
|
// Activate closest item
|
|
|
|
|
if let Some(position) = self.tab_model.position(entity) {
|
|
|
|
|
if position > 0 {
|
|
|
|
|
self.tab_model.activate_position(position - 1);
|
|
|
|
|
} else {
|
|
|
|
|
self.tab_model.activate_position(position + 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove item
|
|
|
|
|
self.tab_model.remove(entity);
|
|
|
|
|
|
|
|
|
|
// If that was the last tab, close window
|
|
|
|
|
if self.tab_model.iter().next().is_none() {
|
|
|
|
|
return window::close(window::Id::MAIN);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Command::batch([self.update_title(), self.update_watcher()]);
|
|
|
|
|
}
|
2024-02-17 02:07:21 -05:00
|
|
|
Message::TabConfig(config) => {
|
2024-02-19 03:44:19 -05:00
|
|
|
if config != self.config.tab {
|
|
|
|
|
// Tabs are collected first to placate the borrowck
|
|
|
|
|
let tabs: Vec<_> = self.tab_model.iter().collect();
|
|
|
|
|
// Update main conf and each tab with the new config
|
|
|
|
|
let commands: Vec<_> = std::iter::once(self.update_config())
|
|
|
|
|
.chain(tabs.into_iter().map(|entity| {
|
|
|
|
|
let config = config.clone();
|
|
|
|
|
self.update(Message::TabMessage(
|
|
|
|
|
Some(entity),
|
|
|
|
|
tab::Message::Config(config),
|
|
|
|
|
))
|
|
|
|
|
}))
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
config_set!(tab, config);
|
|
|
|
|
return Command::batch(commands);
|
|
|
|
|
}
|
2024-02-17 02:07:21 -05:00
|
|
|
}
|
2024-02-01 15:14:14 -07:00
|
|
|
Message::TabMessage(entity_opt, tab_message) => {
|
|
|
|
|
let entity = entity_opt.unwrap_or_else(|| self.tab_model.active());
|
|
|
|
|
|
2024-02-26 14:11:45 -07:00
|
|
|
//TODO: move to Command?
|
2024-02-26 12:05:29 -07:00
|
|
|
if let tab::Message::ContextMenu(_point_opt) = tab_message {
|
|
|
|
|
// Disable side context page
|
|
|
|
|
self.core.window.show_context = false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-26 14:11:45 -07:00
|
|
|
let tab_commands = match self.tab_model.data_mut::<Tab>(entity) {
|
2024-02-26 12:51:22 -07:00
|
|
|
Some(tab) => tab.update(tab_message, self.modifiers),
|
2024-02-26 14:11:45 -07:00
|
|
|
_ => Vec::new(),
|
2024-02-26 12:51:22 -07:00
|
|
|
};
|
2024-02-26 14:11:45 -07:00
|
|
|
|
|
|
|
|
let mut commands = Vec::new();
|
|
|
|
|
for tab_command in tab_commands {
|
|
|
|
|
match tab_command {
|
|
|
|
|
tab::Command::Action(action) => {
|
|
|
|
|
commands.push(self.update(action.message(Some(entity))));
|
|
|
|
|
}
|
|
|
|
|
tab::Command::ChangeLocation(tab_title, tab_path) => {
|
|
|
|
|
self.tab_model.text_set(entity, tab_title);
|
|
|
|
|
commands.push(Command::batch([
|
|
|
|
|
self.update_title(),
|
|
|
|
|
self.update_watcher(),
|
|
|
|
|
self.rescan_tab(entity, tab_path),
|
|
|
|
|
]));
|
|
|
|
|
}
|
2024-05-09 13:24:06 -06:00
|
|
|
tab::Command::EmptyTrash => {
|
|
|
|
|
self.dialog_pages.push_back(DialogPage::EmptyTrash);
|
|
|
|
|
}
|
2024-02-29 15:38:03 -07:00
|
|
|
tab::Command::FocusButton(id) => {
|
|
|
|
|
commands.push(widget::button::focus(id));
|
|
|
|
|
}
|
2024-02-28 15:19:07 -07:00
|
|
|
tab::Command::FocusTextInput(id) => {
|
|
|
|
|
commands.push(widget::text_input::focus(id));
|
|
|
|
|
}
|
2024-02-26 14:11:45 -07:00
|
|
|
tab::Command::OpenFile(item_path) => {
|
2024-02-28 09:29:05 -07:00
|
|
|
match open::that_detached(&item_path) {
|
|
|
|
|
Ok(()) => (),
|
2024-02-26 14:11:45 -07:00
|
|
|
Err(err) => {
|
2024-02-26 14:31:50 -07:00
|
|
|
log::warn!("failed to open {:?}: {}", item_path, err);
|
2024-02-26 14:11:45 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-29 16:25:54 -07:00
|
|
|
tab::Command::Scroll(id, offset) => {
|
|
|
|
|
commands.push(scrollable::scroll_to(id, offset));
|
|
|
|
|
}
|
2024-04-10 11:41:25 -04:00
|
|
|
tab::Command::DropFiles(to, from) => {
|
|
|
|
|
commands.push(self.update(Message::PasteContents(to, from)));
|
|
|
|
|
}
|
|
|
|
|
tab::Command::Timeout(d, tab_msg) => {
|
|
|
|
|
commands.push(Command::perform(
|
|
|
|
|
async move {
|
|
|
|
|
tokio::time::sleep(d).await;
|
|
|
|
|
tab_msg
|
|
|
|
|
},
|
|
|
|
|
move |msg| {
|
|
|
|
|
cosmic::app::Message::App(Message::TabMessage(
|
|
|
|
|
Some(entity),
|
|
|
|
|
msg,
|
|
|
|
|
))
|
|
|
|
|
},
|
|
|
|
|
));
|
|
|
|
|
}
|
2024-04-10 13:56:43 -04:00
|
|
|
tab::Command::MoveToTrash(paths) => {
|
|
|
|
|
self.operation(Operation::Delete { paths });
|
|
|
|
|
}
|
2024-02-01 15:14:14 -07:00
|
|
|
}
|
|
|
|
|
}
|
2024-02-26 14:11:45 -07:00
|
|
|
return Command::batch(commands);
|
2024-02-01 15:14:14 -07:00
|
|
|
}
|
|
|
|
|
Message::TabNew => {
|
|
|
|
|
let active = self.tab_model.active();
|
|
|
|
|
let location = match self.tab_model.data::<Tab>(active) {
|
|
|
|
|
Some(tab) => tab.location.clone(),
|
|
|
|
|
None => Location::Path(home_dir()),
|
|
|
|
|
};
|
|
|
|
|
return self.open_tab(location);
|
|
|
|
|
}
|
|
|
|
|
Message::TabRescan(entity, items) => match self.tab_model.data_mut::<Tab>(entity) {
|
|
|
|
|
Some(tab) => {
|
2024-02-29 13:42:13 -07:00
|
|
|
tab.set_items(items);
|
2024-02-01 15:14:14 -07:00
|
|
|
}
|
|
|
|
|
_ => (),
|
|
|
|
|
},
|
|
|
|
|
//TODO: TABRELOAD
|
|
|
|
|
Message::ToggleContextPage(context_page) => {
|
|
|
|
|
//TODO: ensure context menus are closed
|
|
|
|
|
if self.context_page == context_page {
|
|
|
|
|
self.core.window.show_context = !self.core.window.show_context;
|
|
|
|
|
} else {
|
|
|
|
|
self.context_page = context_page;
|
|
|
|
|
self.core.window.show_context = true;
|
|
|
|
|
}
|
|
|
|
|
self.set_context_title(context_page.title());
|
|
|
|
|
}
|
|
|
|
|
Message::WindowClose => {
|
|
|
|
|
return window::close(window::Id::MAIN);
|
|
|
|
|
}
|
|
|
|
|
Message::WindowNew => match env::current_exe() {
|
|
|
|
|
Ok(exe) => match process::Command::new(&exe).spawn() {
|
|
|
|
|
Ok(_child) => {}
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::error!("failed to execute {:?}: {}", exe, err);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::error!("failed to get current executable path: {}", err);
|
|
|
|
|
}
|
|
|
|
|
},
|
2024-04-10 11:41:25 -04:00
|
|
|
Message::DndEnterNav(entity) => {
|
|
|
|
|
if let Some(location) = self.nav_model.data::<Location>(entity) {
|
|
|
|
|
self.nav_dnd_hover = Some((location.clone(), Instant::now()));
|
|
|
|
|
let location = location.clone();
|
|
|
|
|
return Command::perform(tokio::time::sleep(HOVER_DURATION), move |_| {
|
|
|
|
|
cosmic::app::Message::App(Message::DndHoverLocTimeout(location))
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Message::DndExitNav => {
|
|
|
|
|
self.nav_dnd_hover = None;
|
|
|
|
|
}
|
|
|
|
|
Message::DndDropNav(entity, data, action) => {
|
|
|
|
|
self.nav_dnd_hover = None;
|
|
|
|
|
if let Some((location, data)) = self.nav_model.data::<Location>(entity).zip(data) {
|
|
|
|
|
let kind = match action {
|
|
|
|
|
DndAction::Move => ClipboardKind::Cut,
|
|
|
|
|
_ => ClipboardKind::Copy,
|
|
|
|
|
};
|
|
|
|
|
let ret = match location {
|
|
|
|
|
Location::Path(p) => self.update(Message::PasteContents(
|
|
|
|
|
p.clone(),
|
|
|
|
|
ClipboardPaste {
|
|
|
|
|
kind,
|
|
|
|
|
paths: data.paths,
|
|
|
|
|
},
|
|
|
|
|
)),
|
2024-04-10 13:56:43 -04:00
|
|
|
Location::Trash if matches!(action, DndAction::Move) => {
|
|
|
|
|
self.operation(Operation::Delete { paths: data.paths });
|
|
|
|
|
Command::none()
|
|
|
|
|
}
|
|
|
|
|
_ => {
|
|
|
|
|
log::warn!("Copy to trash is not supported.");
|
|
|
|
|
Command::none()
|
2024-04-10 11:41:25 -04:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Message::DndHoverLocTimeout(location) => {
|
|
|
|
|
if self
|
|
|
|
|
.nav_dnd_hover
|
|
|
|
|
.as_ref()
|
|
|
|
|
.is_some_and(|(loc, i)| *loc == location && i.elapsed() >= HOVER_DURATION)
|
|
|
|
|
{
|
|
|
|
|
self.nav_dnd_hover = None;
|
|
|
|
|
let entity = self.tab_model.active();
|
|
|
|
|
let title = location.to_string();
|
|
|
|
|
self.tab_model.text_set(entity, title);
|
|
|
|
|
return Command::batch([
|
|
|
|
|
self.update_title(),
|
|
|
|
|
self.update_watcher(),
|
|
|
|
|
self.rescan_tab(entity, location),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Message::DndEnterTab(entity) => {
|
|
|
|
|
self.tab_dnd_hover = Some((entity, Instant::now()));
|
|
|
|
|
return Command::perform(tokio::time::sleep(HOVER_DURATION), move |_| {
|
|
|
|
|
cosmic::app::Message::App(Message::DndHoverTabTimeout(entity))
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
Message::DndExitTab => {
|
|
|
|
|
self.nav_dnd_hover = None;
|
|
|
|
|
}
|
|
|
|
|
Message::DndDropTab(entity, data, action) => {
|
|
|
|
|
self.nav_dnd_hover = None;
|
|
|
|
|
if let Some((tab, data)) = self.tab_model.data::<Tab>(entity).zip(data) {
|
|
|
|
|
let kind = match action {
|
|
|
|
|
DndAction::Move => ClipboardKind::Cut,
|
|
|
|
|
_ => ClipboardKind::Copy,
|
|
|
|
|
};
|
|
|
|
|
let ret = match &tab.location {
|
|
|
|
|
Location::Path(p) => self.update(Message::PasteContents(
|
|
|
|
|
p.clone(),
|
|
|
|
|
ClipboardPaste {
|
|
|
|
|
kind,
|
|
|
|
|
paths: data.paths,
|
|
|
|
|
},
|
|
|
|
|
)),
|
2024-04-10 13:56:43 -04:00
|
|
|
Location::Trash if matches!(action, DndAction::Move) => {
|
|
|
|
|
self.operation(Operation::Delete { paths: data.paths });
|
|
|
|
|
Command::none()
|
|
|
|
|
}
|
|
|
|
|
_ => {
|
|
|
|
|
log::warn!("Copy to trash is not supported.");
|
|
|
|
|
Command::none()
|
2024-04-10 11:41:25 -04:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Message::DndHoverTabTimeout(entity) => {
|
|
|
|
|
if self
|
|
|
|
|
.tab_dnd_hover
|
|
|
|
|
.as_ref()
|
|
|
|
|
.is_some_and(|(e, i)| *e == entity && i.elapsed() >= HOVER_DURATION)
|
|
|
|
|
{
|
|
|
|
|
self.tab_dnd_hover = None;
|
|
|
|
|
return self.update(Message::TabActivate(entity));
|
|
|
|
|
}
|
2024-04-24 13:59:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Message::NavBarClose(entity) => {
|
|
|
|
|
if let Some(data) = self.nav_model.data::<MounterData>(entity) {
|
|
|
|
|
if let Some(mounter) = self.mounters.get(&data.0) {
|
|
|
|
|
return mounter.unmount(data.1.clone()).map(|_| message::none());
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-10 11:41:25 -04:00
|
|
|
}
|
2024-04-22 23:14:44 +02:00
|
|
|
|
|
|
|
|
// Tracks which nav bar item to show a context menu for.
|
|
|
|
|
Message::NavBarContext(entity) => {
|
|
|
|
|
self.nav_bar_context_id = entity;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Applies selected nav bar context menu operation.
|
|
|
|
|
Message::NavMenuAction(action) => match action {
|
|
|
|
|
NavMenuAction::OpenInNewTab(entity) => {
|
|
|
|
|
match self.nav_model.data::<Location>(entity) {
|
|
|
|
|
Some(Location::Path(ref path)) => {
|
|
|
|
|
return self.open_tab(Location::Path(path.clone()));
|
|
|
|
|
}
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Open the selected path in a new cosmic-files window.
|
|
|
|
|
NavMenuAction::OpenInNewWindow(entity) => {
|
|
|
|
|
if let Some(&Location::Path(ref path)) = self.nav_model.data::<Location>(entity)
|
|
|
|
|
{
|
|
|
|
|
match env::current_exe() {
|
|
|
|
|
Ok(exe) => match process::Command::new(&exe).arg(path).spawn() {
|
|
|
|
|
Ok(_child) => {}
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::error!("failed to execute {:?}: {}", exe, err);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::error!("failed to get current executable path: {}", err);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NavMenuAction::Properties(entity) => {
|
|
|
|
|
self.context_page = ContextPage::Properties(Some(ContextItem::NavBar(entity)));
|
|
|
|
|
self.core.window.show_context = true;
|
|
|
|
|
self.set_context_title(self.context_page.title());
|
|
|
|
|
}
|
2024-05-09 12:26:55 -06:00
|
|
|
|
|
|
|
|
NavMenuAction::RemoveFromSidebar(entity) => {
|
2024-05-09 12:31:28 -06:00
|
|
|
if let Some(FavoriteIndex(favorite_i)) =
|
|
|
|
|
self.nav_model.data::<FavoriteIndex>(entity)
|
2024-05-09 12:26:55 -06:00
|
|
|
{
|
|
|
|
|
let mut favorites = self.config.favorites.clone();
|
|
|
|
|
favorites.remove(*favorite_i);
|
|
|
|
|
config_set!(favorites, favorites);
|
|
|
|
|
return self.update_config();
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-22 23:14:44 +02:00
|
|
|
},
|
2024-02-01 15:14:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Command::none()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn context_drawer(&self) -> Option<Element<Message>> {
|
|
|
|
|
if !self.core.window.show_context {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Some(match self.context_page {
|
2024-02-28 09:29:05 -07:00
|
|
|
ContextPage::About => self.about(),
|
2024-03-01 16:10:30 -07:00
|
|
|
ContextPage::OpenWith => self.open_with(),
|
2024-02-01 15:14:14 -07:00
|
|
|
ContextPage::Operations => self.operations(),
|
2024-04-22 23:14:44 +02:00
|
|
|
ContextPage::Properties(entity) => self.properties(entity),
|
2024-02-01 15:14:14 -07:00
|
|
|
ContextPage::Settings => self.settings(),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-27 13:03:39 -07:00
|
|
|
fn dialog(&self) -> Option<Element<Message>> {
|
2024-02-27 13:48:12 -07:00
|
|
|
let dialog_page = match self.dialog_pages.front() {
|
2024-02-27 13:03:39 -07:00
|
|
|
Some(some) => some,
|
|
|
|
|
None => return None,
|
|
|
|
|
};
|
|
|
|
|
|
2024-02-29 12:26:45 -07:00
|
|
|
let cosmic_theme::Spacing { space_xxs, .. } = theme::active().cosmic().spacing;
|
2024-02-27 13:03:39 -07:00
|
|
|
|
|
|
|
|
let dialog = match dialog_page {
|
2024-05-09 13:24:06 -06:00
|
|
|
DialogPage::EmptyTrash => widget::dialog(fl!("empty-trash"))
|
|
|
|
|
.body(fl!("empty-trash-warning"))
|
|
|
|
|
.primary_action(
|
|
|
|
|
widget::button::suggested(fl!("empty-trash")).on_press(Message::DialogComplete),
|
|
|
|
|
)
|
|
|
|
|
.secondary_action(
|
|
|
|
|
widget::button::standard(fl!("cancel")).on_press(Message::DialogCancel),
|
|
|
|
|
),
|
2024-02-27 13:48:12 -07:00
|
|
|
DialogPage::FailedOperation(id) => {
|
|
|
|
|
//TODO: try next dialog page (making sure index is used by Dialog messages)?
|
|
|
|
|
let (operation, err) = self.failed_operations.get(id)?;
|
|
|
|
|
|
|
|
|
|
//TODO: nice description of error
|
|
|
|
|
widget::dialog("Failed operation")
|
|
|
|
|
.body(format!("{:#?}\n{}", operation, err))
|
|
|
|
|
.icon(widget::icon::from_name("dialog-error").size(64))
|
|
|
|
|
//TODO: retry action
|
|
|
|
|
.primary_action(
|
|
|
|
|
widget::button::standard(fl!("cancel")).on_press(Message::DialogCancel),
|
|
|
|
|
)
|
|
|
|
|
}
|
2024-02-27 13:03:39 -07:00
|
|
|
DialogPage::NewItem { parent, name, dir } => {
|
|
|
|
|
let mut dialog = widget::dialog(if *dir {
|
|
|
|
|
fl!("create-new-folder")
|
|
|
|
|
} else {
|
|
|
|
|
fl!("create-new-file")
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let complete_maybe = if name.is_empty() {
|
|
|
|
|
None
|
|
|
|
|
} else if name == "." || name == ".." {
|
|
|
|
|
dialog = dialog.tertiary_action(widget::text::body(fl!(
|
|
|
|
|
"name-invalid",
|
|
|
|
|
filename = name.as_str()
|
|
|
|
|
)));
|
|
|
|
|
None
|
|
|
|
|
} else if name.contains('/') {
|
|
|
|
|
dialog = dialog.tertiary_action(widget::text::body(fl!("name-no-slashes")));
|
|
|
|
|
None
|
|
|
|
|
} else {
|
|
|
|
|
let path = parent.join(name);
|
|
|
|
|
if path.exists() {
|
|
|
|
|
if path.is_dir() {
|
|
|
|
|
dialog = dialog
|
|
|
|
|
.tertiary_action(widget::text::body(fl!("folder-already-exists")));
|
|
|
|
|
} else {
|
|
|
|
|
dialog = dialog
|
|
|
|
|
.tertiary_action(widget::text::body(fl!("file-already-exists")));
|
|
|
|
|
}
|
|
|
|
|
None
|
|
|
|
|
} else {
|
|
|
|
|
if name.starts_with('.') {
|
|
|
|
|
dialog = dialog.tertiary_action(widget::text::body(fl!("name-hidden")));
|
|
|
|
|
}
|
|
|
|
|
Some(Message::DialogComplete)
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
dialog
|
|
|
|
|
.primary_action(
|
|
|
|
|
widget::button::suggested(fl!("save"))
|
|
|
|
|
.on_press_maybe(complete_maybe.clone()),
|
|
|
|
|
)
|
|
|
|
|
.secondary_action(
|
|
|
|
|
widget::button::standard(fl!("cancel")).on_press(Message::DialogCancel),
|
|
|
|
|
)
|
|
|
|
|
.control(
|
|
|
|
|
widget::column::with_children(vec![
|
|
|
|
|
widget::text::body(if *dir {
|
|
|
|
|
fl!("folder-name")
|
|
|
|
|
} else {
|
|
|
|
|
fl!("file-name")
|
|
|
|
|
})
|
|
|
|
|
.into(),
|
|
|
|
|
widget::text_input("", name.as_str())
|
|
|
|
|
.id(self.dialog_text_input.clone())
|
|
|
|
|
.on_input(move |name| {
|
2024-02-27 13:48:12 -07:00
|
|
|
Message::DialogUpdate(DialogPage::NewItem {
|
2024-02-27 13:03:39 -07:00
|
|
|
parent: parent.clone(),
|
|
|
|
|
name,
|
|
|
|
|
dir: *dir,
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
.on_submit_maybe(complete_maybe)
|
2024-02-28 15:07:50 -07:00
|
|
|
.into(),
|
|
|
|
|
])
|
|
|
|
|
.spacing(space_xxs),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
DialogPage::RenameItem {
|
|
|
|
|
from,
|
|
|
|
|
parent,
|
|
|
|
|
name,
|
|
|
|
|
dir,
|
|
|
|
|
} => {
|
|
|
|
|
//TODO: combine logic with NewItem
|
|
|
|
|
let mut dialog = widget::dialog(if *dir {
|
|
|
|
|
fl!("rename-folder")
|
|
|
|
|
} else {
|
|
|
|
|
fl!("rename-file")
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let complete_maybe = if name.is_empty() {
|
|
|
|
|
None
|
|
|
|
|
} else if name == "." || name == ".." {
|
|
|
|
|
dialog = dialog.tertiary_action(widget::text::body(fl!(
|
|
|
|
|
"name-invalid",
|
|
|
|
|
filename = name.as_str()
|
|
|
|
|
)));
|
|
|
|
|
None
|
|
|
|
|
} else if name.contains('/') {
|
|
|
|
|
dialog = dialog.tertiary_action(widget::text::body(fl!("name-no-slashes")));
|
|
|
|
|
None
|
|
|
|
|
} else {
|
|
|
|
|
let path = parent.join(name);
|
|
|
|
|
if path.exists() {
|
|
|
|
|
if path.is_dir() {
|
|
|
|
|
dialog = dialog
|
|
|
|
|
.tertiary_action(widget::text::body(fl!("folder-already-exists")));
|
|
|
|
|
} else {
|
|
|
|
|
dialog = dialog
|
|
|
|
|
.tertiary_action(widget::text::body(fl!("file-already-exists")));
|
|
|
|
|
}
|
|
|
|
|
None
|
|
|
|
|
} else {
|
|
|
|
|
if name.starts_with('.') {
|
|
|
|
|
dialog = dialog.tertiary_action(widget::text::body(fl!("name-hidden")));
|
|
|
|
|
}
|
|
|
|
|
Some(Message::DialogComplete)
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
dialog
|
|
|
|
|
.primary_action(
|
|
|
|
|
widget::button::suggested(fl!("rename"))
|
|
|
|
|
.on_press_maybe(complete_maybe.clone()),
|
|
|
|
|
)
|
|
|
|
|
.secondary_action(
|
|
|
|
|
widget::button::standard(fl!("cancel")).on_press(Message::DialogCancel),
|
|
|
|
|
)
|
|
|
|
|
.control(
|
|
|
|
|
widget::column::with_children(vec![
|
|
|
|
|
widget::text::body(if *dir {
|
|
|
|
|
fl!("folder-name")
|
|
|
|
|
} else {
|
|
|
|
|
fl!("file-name")
|
|
|
|
|
})
|
|
|
|
|
.into(),
|
|
|
|
|
widget::text_input("", name.as_str())
|
|
|
|
|
.id(self.dialog_text_input.clone())
|
|
|
|
|
.on_input(move |name| {
|
|
|
|
|
Message::DialogUpdate(DialogPage::RenameItem {
|
|
|
|
|
from: from.clone(),
|
|
|
|
|
parent: parent.clone(),
|
|
|
|
|
name,
|
|
|
|
|
dir: *dir,
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
.on_submit_maybe(complete_maybe)
|
2024-02-27 13:03:39 -07:00
|
|
|
.into(),
|
|
|
|
|
])
|
|
|
|
|
.spacing(space_xxs),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Some(dialog.into())
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-01 15:14:14 -07:00
|
|
|
fn header_start(&self) -> Vec<Element<Self::Message>> {
|
|
|
|
|
vec![menu::menu_bar(&self.key_binds).into()]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn header_end(&self) -> Vec<Element<Self::Message>> {
|
|
|
|
|
vec![]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Creates a view after each update.
|
|
|
|
|
fn view(&self) -> Element<Self::Message> {
|
2024-02-29 12:26:45 -07:00
|
|
|
let cosmic_theme::Spacing { space_xxs, .. } = theme::active().cosmic().spacing;
|
2024-02-01 15:14:14 -07:00
|
|
|
|
|
|
|
|
let mut tab_column = widget::column::with_capacity(1);
|
|
|
|
|
|
|
|
|
|
if self.tab_model.iter().count() > 1 {
|
|
|
|
|
tab_column = tab_column.push(
|
|
|
|
|
widget::container(
|
2024-02-26 15:15:49 -07:00
|
|
|
widget::tab_bar::horizontal(&self.tab_model)
|
2024-02-01 15:14:14 -07:00
|
|
|
.button_height(32)
|
|
|
|
|
.button_spacing(space_xxs)
|
|
|
|
|
.on_activate(Message::TabActivate)
|
2024-04-10 11:41:25 -04:00
|
|
|
.on_close(|entity| Message::TabClose(Some(entity)))
|
|
|
|
|
.on_dnd_enter(|entity, _| Message::DndEnterTab(entity))
|
|
|
|
|
.on_dnd_leave(|_| Message::DndExitTab)
|
|
|
|
|
.on_dnd_drop(|entity, data, action| {
|
|
|
|
|
Message::DndDropTab(entity, data, action)
|
|
|
|
|
})
|
|
|
|
|
.drag_id(self.tab_drag_id),
|
2024-02-01 15:14:14 -07:00
|
|
|
)
|
|
|
|
|
.style(style::Container::Background)
|
|
|
|
|
.width(Length::Fill),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let entity = self.tab_model.active();
|
|
|
|
|
match self.tab_model.data::<Tab>(entity) {
|
|
|
|
|
Some(tab) => {
|
2024-02-26 12:05:29 -07:00
|
|
|
let tab_view = tab
|
2024-02-29 12:26:45 -07:00
|
|
|
.view(&self.key_binds)
|
2024-02-26 12:05:29 -07:00
|
|
|
.map(move |message| Message::TabMessage(Some(entity), message));
|
|
|
|
|
tab_column = tab_column.push(tab_view);
|
2024-02-01 15:14:14 -07:00
|
|
|
}
|
|
|
|
|
None => {
|
|
|
|
|
//TODO
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let content: Element<_> = tab_column.into();
|
|
|
|
|
|
|
|
|
|
// Uncomment to debug layout:
|
|
|
|
|
//content.explain(cosmic::iced::Color::WHITE)
|
|
|
|
|
content
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn subscription(&self) -> Subscription<Self::Message> {
|
|
|
|
|
struct ConfigSubscription;
|
|
|
|
|
struct ThemeSubscription;
|
|
|
|
|
struct WatcherSubscription;
|
2024-03-20 01:50:43 -04:00
|
|
|
struct TrashWatcherSubscription;
|
2024-02-01 15:14:14 -07:00
|
|
|
|
|
|
|
|
let mut subscriptions = vec![
|
2024-02-28 15:25:25 -07:00
|
|
|
event::listen_with(|event, status| match event {
|
|
|
|
|
Event::Keyboard(KeyEvent::KeyPressed { key, modifiers, .. }) => match status {
|
|
|
|
|
event::Status::Ignored => Some(Message::Key(modifiers, key)),
|
|
|
|
|
event::Status::Captured => None,
|
|
|
|
|
},
|
2024-02-01 15:14:14 -07:00
|
|
|
Event::Keyboard(KeyEvent::ModifiersChanged(modifiers)) => {
|
|
|
|
|
Some(Message::Modifiers(modifiers))
|
|
|
|
|
}
|
|
|
|
|
_ => None,
|
|
|
|
|
}),
|
|
|
|
|
cosmic_config::config_subscription(
|
|
|
|
|
TypeId::of::<ConfigSubscription>(),
|
|
|
|
|
Self::APP_ID.into(),
|
|
|
|
|
CONFIG_VERSION,
|
|
|
|
|
)
|
|
|
|
|
.map(|update| {
|
|
|
|
|
if !update.errors.is_empty() {
|
|
|
|
|
log::info!(
|
|
|
|
|
"errors loading config {:?}: {:?}",
|
|
|
|
|
update.keys,
|
|
|
|
|
update.errors
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
Message::Config(update.config)
|
|
|
|
|
}),
|
|
|
|
|
cosmic_config::config_subscription::<_, cosmic_theme::ThemeMode>(
|
|
|
|
|
TypeId::of::<ThemeSubscription>(),
|
|
|
|
|
cosmic_theme::THEME_MODE_ID.into(),
|
|
|
|
|
cosmic_theme::ThemeMode::version(),
|
|
|
|
|
)
|
|
|
|
|
.map(|update| {
|
|
|
|
|
if !update.errors.is_empty() {
|
|
|
|
|
log::info!(
|
|
|
|
|
"errors loading theme mode {:?}: {:?}",
|
|
|
|
|
update.keys,
|
|
|
|
|
update.errors
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
Message::SystemThemeModeChange(update.config)
|
|
|
|
|
}),
|
|
|
|
|
subscription::channel(
|
|
|
|
|
TypeId::of::<WatcherSubscription>(),
|
|
|
|
|
100,
|
|
|
|
|
|mut output| async move {
|
|
|
|
|
let watcher_res = {
|
|
|
|
|
let mut output = output.clone();
|
2024-03-20 11:54:37 -06:00
|
|
|
new_debouncer(
|
2024-03-20 15:43:44 -06:00
|
|
|
time::Duration::from_millis(250),
|
|
|
|
|
Some(time::Duration::from_millis(250)),
|
2024-03-20 11:54:37 -06:00
|
|
|
move |events_res: notify_debouncer_full::DebounceEventResult| {
|
|
|
|
|
match events_res {
|
|
|
|
|
Ok(mut events) => {
|
|
|
|
|
events.retain(|event| {
|
|
|
|
|
match &event.kind {
|
|
|
|
|
notify::EventKind::Access(_) => {
|
|
|
|
|
// Data not mutated
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
notify::EventKind::Modify(
|
|
|
|
|
notify::event::ModifyKind::Metadata(e),
|
|
|
|
|
) if (*e != notify::event::MetadataKind::Any
|
|
|
|
|
&& *e
|
|
|
|
|
!= notify::event::MetadataKind::WriteTime) =>
|
|
|
|
|
{
|
|
|
|
|
// Data not mutated nor modify time changed
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
_ => true
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if !events.is_empty() {
|
|
|
|
|
match futures::executor::block_on(async {
|
|
|
|
|
output.send(Message::NotifyEvents(events)).await
|
|
|
|
|
}) {
|
|
|
|
|
Ok(()) => {}
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::warn!(
|
|
|
|
|
"failed to send notify events: {:?}",
|
|
|
|
|
err
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-10 01:23:26 -05:00
|
|
|
}
|
2024-02-01 15:14:14 -07:00
|
|
|
}
|
2024-03-20 11:54:37 -06:00
|
|
|
Err(err) => {
|
|
|
|
|
log::warn!("failed to watch files: {:?}", err);
|
2024-02-01 15:14:14 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
match watcher_res {
|
|
|
|
|
Ok(watcher) => {
|
|
|
|
|
match output
|
|
|
|
|
.send(Message::NotifyWatcher(WatcherWrapper {
|
|
|
|
|
watcher_opt: Some(watcher),
|
|
|
|
|
}))
|
|
|
|
|
.await
|
|
|
|
|
{
|
|
|
|
|
Ok(()) => {}
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::warn!("failed to send notify watcher: {:?}", err);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::warn!("failed to create file watcher: {:?}", err);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-22 13:14:25 -06:00
|
|
|
std::future::pending().await
|
2024-02-01 15:14:14 -07:00
|
|
|
},
|
|
|
|
|
),
|
2024-03-20 01:50:43 -04:00
|
|
|
subscription::channel(
|
|
|
|
|
TypeId::of::<TrashWatcherSubscription>(),
|
|
|
|
|
25,
|
|
|
|
|
|mut output| async move {
|
2024-03-20 23:16:15 -04:00
|
|
|
let watcher_res = new_debouncer(
|
|
|
|
|
time::Duration::from_millis(250),
|
|
|
|
|
Some(time::Duration::from_millis(250)),
|
|
|
|
|
move |event_res: notify_debouncer_full::DebounceEventResult| match event_res
|
|
|
|
|
{
|
|
|
|
|
Ok(mut events) => {
|
|
|
|
|
events.retain(|event| {
|
|
|
|
|
matches!(
|
|
|
|
|
event.kind,
|
|
|
|
|
notify::EventKind::Create(_) | notify::EventKind::Remove(_)
|
|
|
|
|
)
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if !events.is_empty() {
|
|
|
|
|
if let Err(e) = futures::executor::block_on(async {
|
|
|
|
|
output.send(Message::RescanTrash).await
|
|
|
|
|
}) {
|
|
|
|
|
log::warn!("trash needs to be rescanned but sending message failed: {e:?}");
|
|
|
|
|
}
|
2024-03-20 20:23:00 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Err(e) => {
|
2024-03-20 23:16:15 -04:00
|
|
|
log::warn!("failed to watch trash bin for changes: {e:?}")
|
2024-03-20 20:23:00 -04:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
);
|
2024-03-20 01:50:43 -04:00
|
|
|
|
2024-03-20 20:23:00 -04:00
|
|
|
// TODO: Trash watching support for Windows, macOS, and other OSes
|
|
|
|
|
#[cfg(all(
|
|
|
|
|
unix,
|
|
|
|
|
not(target_os = "macos"),
|
|
|
|
|
not(target_os = "ios"),
|
|
|
|
|
not(target_os = "android")
|
|
|
|
|
))]
|
2024-03-20 01:50:43 -04:00
|
|
|
match (watcher_res, trash::os_limited::trash_folders()) {
|
|
|
|
|
(Ok(mut watcher), Ok(trash_bins)) => {
|
|
|
|
|
for path in trash_bins {
|
2024-03-20 23:16:15 -04:00
|
|
|
if let Err(e) = watcher
|
|
|
|
|
.watcher()
|
|
|
|
|
.watch(&path, notify::RecursiveMode::Recursive)
|
2024-03-20 01:50:43 -04:00
|
|
|
{
|
|
|
|
|
log::warn!(
|
|
|
|
|
"failed to add trash bin `{}` to watcher: {e:?}",
|
|
|
|
|
path.display()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Don't drop the watcher
|
|
|
|
|
std::future::pending().await
|
|
|
|
|
}
|
|
|
|
|
(Err(e), _) => {
|
|
|
|
|
log::warn!("failed to create new watcher for trash bin: {e:?}")
|
|
|
|
|
}
|
|
|
|
|
(_, Err(e)) => {
|
|
|
|
|
log::warn!("could not find any valid trash bins to watch: {e:?}")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::future::pending().await
|
|
|
|
|
},
|
|
|
|
|
),
|
2024-02-01 15:14:14 -07:00
|
|
|
];
|
|
|
|
|
|
2024-04-22 13:14:25 -06:00
|
|
|
for (key, mounter) in self.mounters.iter() {
|
|
|
|
|
let key = *key;
|
|
|
|
|
subscriptions.push(
|
|
|
|
|
mounter
|
|
|
|
|
.subscription()
|
|
|
|
|
.map(move |items| Message::MounterItems(key, items)),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-01 15:14:14 -07:00
|
|
|
for (id, (pending_operation, _)) in self.pending_operations.iter() {
|
|
|
|
|
//TODO: use recipe?
|
|
|
|
|
let id = *id;
|
|
|
|
|
let pending_operation = pending_operation.clone();
|
2024-03-20 11:54:37 -06:00
|
|
|
subscriptions.push(subscription::channel(id, 16, move |msg_tx| async move {
|
|
|
|
|
let msg_tx = Arc::new(tokio::sync::Mutex::new(msg_tx));
|
|
|
|
|
match pending_operation.perform(id, &msg_tx).await {
|
|
|
|
|
Ok(()) => {
|
|
|
|
|
let _ = msg_tx.lock().await.send(Message::PendingComplete(id)).await;
|
2024-02-01 15:14:14 -07:00
|
|
|
}
|
2024-03-20 11:54:37 -06:00
|
|
|
Err(err) => {
|
|
|
|
|
let _ = msg_tx
|
|
|
|
|
.lock()
|
|
|
|
|
.await
|
|
|
|
|
.send(Message::PendingError(id, err.to_string()))
|
|
|
|
|
.await;
|
2024-02-01 15:14:14 -07:00
|
|
|
}
|
2024-03-20 11:54:37 -06:00
|
|
|
}
|
|
|
|
|
|
2024-04-22 13:14:25 -06:00
|
|
|
std::future::pending().await
|
2024-03-20 11:54:37 -06:00
|
|
|
}));
|
2024-02-01 15:14:14 -07:00
|
|
|
}
|
|
|
|
|
|
2024-02-22 16:17:39 -07:00
|
|
|
for entity in self.tab_model.iter() {
|
|
|
|
|
if let Some(tab) = self.tab_model.data::<Tab>(entity) {
|
|
|
|
|
subscriptions.push(
|
|
|
|
|
tab.subscription()
|
|
|
|
|
.with(entity)
|
|
|
|
|
.map(|(entity, tab_msg)| Message::TabMessage(Some(entity), tab_msg)),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-01 15:14:14 -07:00
|
|
|
Subscription::batch(subscriptions)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Utilities to build a temporary file hierarchy for tests.
|
|
|
|
|
//
|
|
|
|
|
// Ideally, tests would use the cap-std crate which limits path traversal.
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
pub(crate) mod test_utils {
|
|
|
|
|
use std::{
|
|
|
|
|
cmp::Ordering,
|
|
|
|
|
fs::File,
|
|
|
|
|
io::{self, Write},
|
|
|
|
|
iter,
|
|
|
|
|
path::Path,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
use log::{debug, trace};
|
|
|
|
|
use tempfile::{tempdir, TempDir};
|
|
|
|
|
|
2024-02-19 03:44:19 -05:00
|
|
|
use crate::{
|
|
|
|
|
config::{IconSizes, TabConfig},
|
|
|
|
|
tab::Item,
|
|
|
|
|
};
|
2024-02-01 15:14:14 -07:00
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
// Default number of files, directories, and nested directories for test file system
|
|
|
|
|
pub const NUM_FILES: usize = 2;
|
2024-02-01 23:31:50 -05:00
|
|
|
pub const NUM_HIDDEN: usize = 1;
|
2024-02-01 15:14:14 -07:00
|
|
|
pub const NUM_DIRS: usize = 2;
|
|
|
|
|
pub const NUM_NESTED: usize = 1;
|
|
|
|
|
pub const NAME_LEN: usize = 5;
|
|
|
|
|
|
|
|
|
|
/// Add `n` temporary files in `dir`
|
|
|
|
|
///
|
2024-02-01 23:31:50 -05:00
|
|
|
/// Each file is assigned a numeric name from [0, n) with a prefix.
|
|
|
|
|
pub fn file_flat_hier<D: AsRef<Path>>(dir: D, n: usize, prefix: &str) -> io::Result<Vec<File>> {
|
2024-02-01 15:14:14 -07:00
|
|
|
let dir = dir.as_ref();
|
|
|
|
|
(0..n)
|
|
|
|
|
.map(|i| -> io::Result<File> {
|
2024-02-01 23:31:50 -05:00
|
|
|
let name = format!("{prefix}{i}");
|
2024-02-01 15:14:14 -07:00
|
|
|
let path = dir.join(&name);
|
|
|
|
|
|
|
|
|
|
let mut file = File::create(path)?;
|
|
|
|
|
file.write_all(name.as_bytes())?;
|
|
|
|
|
|
|
|
|
|
Ok(file)
|
|
|
|
|
})
|
|
|
|
|
.collect()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Random alphanumeric String of length `len`
|
|
|
|
|
fn rand_string(len: usize) -> String {
|
|
|
|
|
(0..len).map(|_| fastrand::alphanumeric()).collect()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Create a small, temporary file hierarchy.
|
2024-02-01 23:31:50 -05:00
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
///
|
|
|
|
|
/// * `files` - Number of files to create in temp directories
|
|
|
|
|
/// * `hidden` - Number of hidden files to create
|
|
|
|
|
/// * `dirs` - Number of directories to create
|
|
|
|
|
/// * `nested` - Number of nested directories to create in new dirs
|
|
|
|
|
/// * `name_len` - Length of randomized directory names
|
2024-02-01 15:14:14 -07:00
|
|
|
pub fn simple_fs(
|
|
|
|
|
files: usize,
|
2024-02-01 23:31:50 -05:00
|
|
|
hidden: usize,
|
2024-02-01 15:14:14 -07:00
|
|
|
dirs: usize,
|
|
|
|
|
nested: usize,
|
|
|
|
|
name_len: usize,
|
|
|
|
|
) -> io::Result<TempDir> {
|
|
|
|
|
// Files created inside of a TempDir are deleted with the directory
|
|
|
|
|
// TempDir won't leak resources as long as the destructor runs
|
|
|
|
|
let root = tempdir()?;
|
|
|
|
|
debug!("Root temp directory: {}", root.as_ref().display());
|
2024-02-01 23:31:50 -05:00
|
|
|
trace!("Creating {files} files and {hidden} hidden files in {dirs} temp dirs with {nested} nested temp dirs");
|
2024-02-01 15:14:14 -07:00
|
|
|
|
|
|
|
|
// All paths for directories and nested directories
|
|
|
|
|
let paths = (0..dirs).flat_map(|_| {
|
|
|
|
|
let root = root.as_ref();
|
|
|
|
|
let current = rand_string(name_len);
|
|
|
|
|
|
|
|
|
|
iter::once(root.join(¤t)).chain(
|
|
|
|
|
(0..nested).map(move |_| root.join(format!("{current}/{}", rand_string(name_len)))),
|
|
|
|
|
)
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Create directories from `paths` and add a few files
|
|
|
|
|
for path in paths {
|
|
|
|
|
fs::create_dir_all(&path)?;
|
2024-02-01 23:31:50 -05:00
|
|
|
|
|
|
|
|
// Normal files
|
|
|
|
|
file_flat_hier(&path, files, "")?;
|
|
|
|
|
// Hidden files
|
|
|
|
|
file_flat_hier(&path, hidden, ".")?;
|
2024-02-01 15:14:14 -07:00
|
|
|
|
|
|
|
|
for entry in path.read_dir()? {
|
|
|
|
|
let entry = entry?;
|
|
|
|
|
if entry.file_type()?.is_file() {
|
|
|
|
|
trace!("Created file: {}", entry.path().display());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(root)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Empty file hierarchy
|
|
|
|
|
pub fn empty_fs() -> io::Result<TempDir> {
|
|
|
|
|
tempdir()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Sort files.
|
|
|
|
|
///
|
|
|
|
|
/// Directories are placed before files.
|
|
|
|
|
/// Files are lexically sorted.
|
|
|
|
|
/// This is more or less copied right from the [Tab] code
|
|
|
|
|
pub fn sort_files(a: &Path, b: &Path) -> Ordering {
|
|
|
|
|
match (a.is_dir(), b.is_dir()) {
|
|
|
|
|
(true, false) => Ordering::Less,
|
|
|
|
|
(false, true) => Ordering::Greater,
|
|
|
|
|
_ => lexical_sort::natural_lexical_cmp(
|
|
|
|
|
a.file_name()
|
|
|
|
|
.expect("temp entries should have names")
|
|
|
|
|
.to_str()
|
|
|
|
|
.expect("temp entries should be valid UTF-8"),
|
|
|
|
|
b.file_name()
|
|
|
|
|
.expect("temp entries should have names")
|
|
|
|
|
.to_str()
|
|
|
|
|
.expect("temp entries should be valid UTF-8"),
|
|
|
|
|
),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-03 02:31:08 -05:00
|
|
|
/// Read directory entries from `path` and sort.
|
|
|
|
|
pub fn read_dir_sorted(path: &Path) -> io::Result<Vec<PathBuf>> {
|
|
|
|
|
let mut entries: Vec<_> = path
|
|
|
|
|
.read_dir()?
|
|
|
|
|
.map(|maybe_entry| maybe_entry.map(|entry| entry.path()))
|
|
|
|
|
.collect::<io::Result<_>>()?;
|
|
|
|
|
entries.sort_by(|a, b| sort_files(a, b));
|
|
|
|
|
|
|
|
|
|
Ok(entries)
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-04 07:28:51 -05:00
|
|
|
/// Filter `path` for directories
|
|
|
|
|
pub fn filter_dirs(path: &Path) -> io::Result<impl Iterator<Item = PathBuf>> {
|
|
|
|
|
Ok(path.read_dir()?.filter_map(|entry| {
|
|
|
|
|
entry.ok().and_then(|entry| {
|
|
|
|
|
let path = entry.path();
|
|
|
|
|
if path.is_dir() {
|
|
|
|
|
Some(path)
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Boiler plate for Tab tests
|
|
|
|
|
pub fn tab_click_new(
|
|
|
|
|
files: usize,
|
|
|
|
|
hidden: usize,
|
|
|
|
|
dirs: usize,
|
|
|
|
|
nested: usize,
|
|
|
|
|
name_len: usize,
|
|
|
|
|
) -> io::Result<(TempDir, Tab)> {
|
|
|
|
|
let fs = simple_fs(files, hidden, dirs, nested, name_len)?;
|
|
|
|
|
let path = fs.path();
|
|
|
|
|
|
|
|
|
|
// New tab with items
|
|
|
|
|
let location = Location::Path(path.to_owned());
|
2024-02-18 02:44:54 -05:00
|
|
|
let items = location.scan(IconSizes::default());
|
2024-02-10 02:13:13 -05:00
|
|
|
let mut tab = Tab::new(location, TabConfig::default());
|
2024-02-29 13:42:13 -07:00
|
|
|
tab.set_items(items);
|
2024-02-04 07:28:51 -05:00
|
|
|
|
|
|
|
|
// Ensure correct number of directories as a sanity check
|
2024-02-29 13:42:13 -07:00
|
|
|
let items = tab.items_opt().expect("tab should be populated with Items");
|
2024-02-04 07:28:51 -05:00
|
|
|
assert_eq!(NUM_DIRS, items.len());
|
|
|
|
|
|
|
|
|
|
Ok((fs, tab))
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-01 15:14:14 -07:00
|
|
|
/// Equality for [Path] and [Item].
|
|
|
|
|
pub fn eq_path_item(path: &Path, item: &Item) -> bool {
|
|
|
|
|
let name = path
|
|
|
|
|
.file_name()
|
|
|
|
|
.expect("temp entries should have names")
|
|
|
|
|
.to_str()
|
|
|
|
|
.expect("temp entries should be valid UTF-8");
|
2024-02-01 23:31:50 -05:00
|
|
|
let is_dir = path.is_dir();
|
|
|
|
|
|
|
|
|
|
// NOTE: I don't want to change `tab::hidden_attribute` to `pub(crate)` for
|
|
|
|
|
// tests without asking
|
|
|
|
|
#[cfg(not(target_os = "windows"))]
|
|
|
|
|
let is_hidden = name.starts_with('.');
|
|
|
|
|
|
|
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
|
let is_hidden = {
|
|
|
|
|
use std::os::windows::fs::MetadataExt;
|
|
|
|
|
const FILE_ATTRIBUTE_HIDDEN: u32 = 2;
|
|
|
|
|
let metadata = path.metadata().expect("fetching file metadata");
|
|
|
|
|
metadata.file_attributes() & FILE_ATTRIBUTE_HIDDEN == FILE_ATTRIBUTE_HIDDEN
|
|
|
|
|
};
|
2024-02-01 15:14:14 -07:00
|
|
|
|
2024-02-01 23:31:50 -05:00
|
|
|
name == item.name
|
|
|
|
|
&& is_dir == item.metadata.is_dir()
|
2024-03-22 09:48:57 -04:00
|
|
|
&& path == item.path_opt.as_ref().expect("item should have path")
|
2024-02-01 23:31:50 -05:00
|
|
|
&& is_hidden == item.hidden
|
2024-02-01 15:14:14 -07:00
|
|
|
}
|
2024-02-03 02:31:08 -05:00
|
|
|
|
|
|
|
|
/// Asserts `tab`'s location changed to `path`
|
|
|
|
|
pub fn assert_eq_tab_path(tab: &Tab, path: &Path) {
|
|
|
|
|
// Paths should be the same
|
|
|
|
|
let Location::Path(ref tab_path) = tab.location else {
|
|
|
|
|
panic!("Expected tab's location to be a path");
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
path,
|
|
|
|
|
tab_path,
|
|
|
|
|
"Tab's path is {} instead of being updated to {}",
|
|
|
|
|
tab_path.display(),
|
|
|
|
|
path.display()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Assert that tab's items are equal to a path's entries.
|
|
|
|
|
pub fn assert_eq_tab_path_contents(tab: &Tab, path: &Path) {
|
|
|
|
|
let Location::Path(ref tab_path) = tab.location else {
|
|
|
|
|
panic!("Expected tab's location to be a path");
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Tab items are sorted so paths from read_dir must be too
|
|
|
|
|
let entries = read_dir_sorted(path).expect("should be able to read paths from temp dir");
|
|
|
|
|
|
|
|
|
|
// Check lengths.
|
|
|
|
|
// `items_opt` is optional and the directory at `path` may have zero entries
|
|
|
|
|
// Therefore, this doesn't panic if `items_opt` is None
|
2024-02-29 13:42:13 -07:00
|
|
|
let items_len = tab.items_opt().map(|items| items.len()).unwrap_or_default();
|
2024-02-03 02:31:08 -05:00
|
|
|
assert_eq!(entries.len(), items_len);
|
|
|
|
|
|
2024-02-29 13:42:13 -07:00
|
|
|
let empty = Vec::new();
|
2024-02-03 02:31:08 -05:00
|
|
|
assert!(
|
|
|
|
|
entries
|
|
|
|
|
.into_iter()
|
2024-02-29 13:42:13 -07:00
|
|
|
.zip(tab.items_opt().clone().unwrap_or(&empty))
|
2024-02-03 02:31:08 -05:00
|
|
|
.all(|(a, b)| eq_path_item(&a, &b)),
|
|
|
|
|
"Path ({}) and Tab path ({}) don't have equal contents",
|
|
|
|
|
path.display(),
|
|
|
|
|
tab_path.display()
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-02-01 15:14:14 -07:00
|
|
|
}
|