Rescan in background and describe empty folders

This commit is contained in:
Jeremy Soller 2024-01-05 08:55:18 -07:00
parent dcf871e538
commit 7efe6efa30
No known key found for this signature in database
GPG key ID: DCFCA852D3906975
5 changed files with 211 additions and 122 deletions

View file

@ -9,7 +9,7 @@ env_logger = "0.10"
lazy_static = "1" lazy_static = "1"
log = "0.4" log = "0.4"
serde = { version = "1", features = ["serde_derive"] } serde = { version = "1", features = ["serde_derive"] }
tokio = { version = "1", features = ["sync"] } tokio = { version = "1" }
# Internationalization # Internationalization
i18n-embed = { version = "0.13", features = ["fluent-system", "desktop-requester"] } i18n-embed = { version = "0.13", features = ["fluent-system", "desktop-requester"] }
i18n-embed-fl = "0.6" i18n-embed-fl = "0.6"

View file

@ -1,3 +1,6 @@
empty-folder = Empty folder
empty-folder-hidden = Empty folder (has hidden items)
# Context Pages # Context Pages
## Settings ## Settings

View file

@ -2,7 +2,7 @@
// SPDX-License-Identifier: GPL-3.0-only // SPDX-License-Identifier: GPL-3.0-only
use cosmic::{ use cosmic::{
app::{Command, Core, Settings}, app::{message, Command, Core, Settings},
cosmic_config::{self, CosmicConfigEntry}, cosmic_config::{self, CosmicConfigEntry},
cosmic_theme, executor, cosmic_theme, executor,
iced::{subscription::Subscription, widget::row, window, Alignment, Length, Point}, iced::{subscription::Subscription, widget::row, window, Alignment, Length, Point},
@ -131,6 +131,7 @@ pub enum Message {
TabContextMenu(segmented_button::Entity, Option<Point>), TabContextMenu(segmented_button::Entity, Option<Point>),
TabMessage(segmented_button::Entity, tab::Message), TabMessage(segmented_button::Entity, tab::Message),
TabNew, TabNew,
TabRescan(segmented_button::Entity, Vec<tab::Item>),
ToggleContextPage(ContextPage), ToggleContextPage(ContextPage),
} }
@ -159,14 +160,37 @@ pub struct App {
impl App { impl App {
fn open_tab<P: Into<PathBuf>>(&mut self, path: P) -> Command<Message> { fn open_tab<P: Into<PathBuf>>(&mut self, path: P) -> Command<Message> {
let tab = Tab::new(path); let path = path.into();
self.tab_model let tab = Tab::new(path.clone());
let entity = self
.tab_model
.insert() .insert()
.text(tab.title()) .text(tab.title())
.data(tab) .data(tab)
.closable() .closable()
.activate(); .activate()
self.update_title() .id();
Command::batch([self.update_title(), self.rescan_tab(entity, path)])
}
fn rescan_tab<P: Into<PathBuf>>(
&mut self,
entity: segmented_button::Entity,
path: P,
) -> Command<Message> {
let path = path.into();
Command::perform(
async move {
match tokio::task::spawn_blocking(move || tab::rescan(path)).await {
Ok(items) => message::app(Message::TabRescan(entity, items)),
Err(err) => {
log::warn!("failed to rescan: {}", err);
message::none()
}
}
},
|x| x,
)
} }
fn update_config(&mut self) -> Command<Message> { fn update_config(&mut self) -> Command<Message> {
@ -258,17 +282,17 @@ impl Application for App {
context_page: ContextPage::Settings, context_page: ContextPage::Settings,
}; };
let mut commands = Vec::new();
for arg in env::args().skip(1) { for arg in env::args().skip(1) {
let _ = app.open_tab(arg); commands.push(app.open_tab(arg));
} }
if app.tab_model.iter().next().is_none() { if app.tab_model.iter().next().is_none() {
let _ = app.open_tab(home_dir()); commands.push(app.open_tab(home_dir()));
} }
let command = app.update_title(); (app, Command::batch(commands))
(app, command)
} }
/// Handle application events here. /// Handle application events here.
@ -339,18 +363,21 @@ impl Application for App {
} }
} }
Message::TabMessage(entity, tab_message) => { Message::TabMessage(entity, tab_message) => {
let mut tab_title_opt = None; let mut update_opt = None;
match self.tab_model.data_mut::<Tab>(entity) { match self.tab_model.data_mut::<Tab>(entity) {
Some(tab) => { Some(tab) => {
if tab.update(tab_message) { if tab.update(tab_message) {
tab_title_opt = Some(tab.title()); update_opt = Some((tab.title(), tab.path.clone()));
} }
} }
_ => (), _ => (),
} }
if let Some(tab_title) = tab_title_opt { if let Some((tab_title, tab_path)) = update_opt {
self.tab_model.text_set(entity, tab_title); self.tab_model.text_set(entity, tab_title);
return self.update_title(); return Command::batch([
self.update_title(),
self.rescan_tab(entity, tab_path),
]);
} }
} }
Message::TabNew => { Message::TabNew => {
@ -361,6 +388,13 @@ impl Application for App {
}; };
return self.open_tab(path); return self.open_tab(path);
} }
Message::TabRescan(entity, items) => match self.tab_model.data_mut::<Tab>(entity) {
Some(tab) => {
tab.items_opt = Some(items);
}
_ => (),
},
//TODO: TABRELOAD
Message::ToggleContextPage(context_page) => { Message::ToggleContextPage(context_page) => {
if self.context_page == context_page { if self.context_page == context_page {
self.core.window.show_context = !self.core.window.show_context; self.core.window.show_context = !self.core.window.show_context;

View file

@ -43,7 +43,7 @@ lazy_static::lazy_static! {
static ref MIME_ICON_CACHE: Mutex<MimeIconCache> = Mutex::new(MimeIconCache::new()); static ref MIME_ICON_CACHE: Mutex<MimeIconCache> = Mutex::new(MimeIconCache::new());
} }
pub fn mime_icon<P: AsRef<Path>>(path: P, size: u16) -> icon::Icon { pub fn mime_icon<P: AsRef<Path>>(path: P, size: u16) -> icon::Handle {
//TODO: smarter path handling //TODO: smarter path handling
let path = path let path = path
.as_ref() .as_ref()
@ -52,7 +52,7 @@ pub fn mime_icon<P: AsRef<Path>>(path: P, size: u16) -> icon::Icon {
.to_owned(); .to_owned();
let mut mime_icon_cache = MIME_ICON_CACHE.lock().unwrap(); let mut mime_icon_cache = MIME_ICON_CACHE.lock().unwrap();
match mime_icon_cache.get(MimeIconKey { path, size }) { match mime_icon_cache.get(MimeIconKey { path, size }) {
Some(handle) => icon::icon(handle).size(size), Some(handle) => handle,
None => icon::from_name(FALLBACK_MIME_ICON).size(size).icon(), None => icon::from_name(FALLBACK_MIME_ICON).size(size).handle(),
} }
} }

View file

@ -1,19 +1,22 @@
use cosmic::{ use cosmic::{
app::Core, app::Core,
cosmic_theme, cosmic_theme,
iced::{Alignment, Length, Point}, iced::{
alignment::{Horizontal, Vertical},
Alignment, Length, Point,
},
theme, widget, Element, theme, widget, Element,
}; };
use std::{ use std::{
cmp::Ordering, cmp::Ordering,
collections::HashMap, collections::HashMap,
fs, fmt, fs,
path::PathBuf, path::PathBuf,
process, process,
time::{Duration, Instant}, time::{Duration, Instant},
}; };
use crate::mime_icon::mime_icon; use crate::{fl, mime_icon::mime_icon};
const DOUBLE_CLICK_DURATION: Duration = Duration::from_millis(500); const DOUBLE_CLICK_DURATION: Duration = Duration::from_millis(500);
@ -51,10 +54,10 @@ lazy_static::lazy_static! {
}; };
} }
fn folder_icon(path: &PathBuf, icon_size: u16) -> widget::icon::Icon { fn folder_icon(path: &PathBuf, icon_size: u16) -> widget::icon::Handle {
widget::icon::from_name(SPECIAL_DIRS.get(path).map_or("folder", |x| *x)) widget::icon::from_name(SPECIAL_DIRS.get(path).map_or("folder", |x| *x))
.size(icon_size) .size(icon_size)
.icon() .handle()
} }
#[cfg(not(target_os = "windows"))] #[cfg(not(target_os = "windows"))]
@ -115,26 +118,98 @@ pub enum Message {
Parent, Parent,
} }
#[derive(Clone)]
pub struct Item { pub struct Item {
pub name: String, pub name: String,
pub path: PathBuf, pub path: PathBuf,
pub hidden: bool, pub hidden: bool,
pub is_dir: bool, pub is_dir: bool,
pub icon: widget::icon::Icon, pub icon_handle: widget::icon::Handle,
pub select_time: Option<Instant>, pub select_time: Option<Instant>,
} }
impl fmt::Debug for Item {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Item")
.field("name", &self.name)
.field("path", &self.path)
.field("hidden", &self.hidden)
.field("is_dir", &self.is_dir)
//icon_handle
.field("select_time", &self.select_time)
.finish()
}
}
#[derive(Clone, Debug)]
pub struct Tab { pub struct Tab {
pub path: PathBuf, pub path: PathBuf,
//TODO //TODO
pub context_menu: Option<Point>, pub context_menu: Option<Point>,
pub items: Vec<Item>, pub items_opt: Option<Vec<Item>>,
}
pub fn rescan(tab_path: PathBuf) -> Vec<Item> {
let mut items = Vec::new();
match fs::read_dir(&tab_path) {
Ok(entries) => {
for entry_res in entries {
let entry = match entry_res {
Ok(ok) => ok,
Err(err) => {
log::warn!("failed to read entry in {:?}: {}", tab_path, err);
continue;
}
};
let name = match entry.file_name().into_string() {
Ok(some) => some,
Err(name_os) => {
log::warn!(
"failed to parse entry in {:?}: {:?} is not valid UTF-8",
tab_path,
name_os,
);
continue;
}
};
let path = entry.path();
let hidden = name.starts_with(".") || hidden_attribute(&path);
let is_dir = path.is_dir();
//TODO: configurable size
let icon_size = 32;
let icon_handle = if is_dir {
folder_icon(&path, icon_size)
} else {
mime_icon(&path, icon_size)
};
items.push(Item {
name,
path,
hidden,
is_dir,
icon_handle,
select_time: None,
});
}
}
Err(err) => {
log::warn!("failed to read directory {:?}: {}", tab_path, err);
}
}
items.sort_by(|a, b| match (a.is_dir, b.is_dir) {
(true, false) => Ordering::Less,
(false, true) => Ordering::Greater,
_ => a.name.cmp(&b.name),
});
items
} }
impl Tab { impl Tab {
pub fn new<P: Into<PathBuf>>(path: P) -> Self { pub fn new(path: PathBuf) -> Self {
let path = path.into(); Self {
let mut tab = Self {
path: match fs::canonicalize(&path) { path: match fs::canonicalize(&path) {
Ok(absolute) => absolute, Ok(absolute) => absolute,
Err(err) => { Err(err) => {
@ -143,67 +218,8 @@ impl Tab {
} }
}, },
context_menu: None, context_menu: None,
items: Vec::new(), items_opt: None,
};
tab.rescan();
tab
}
pub fn rescan(&mut self) {
self.items.clear();
match fs::read_dir(&self.path) {
Ok(entries) => {
for entry_res in entries {
let entry = match entry_res {
Ok(ok) => ok,
Err(err) => {
log::warn!("failed to read entry in {:?}: {}", self.path, err);
continue;
}
};
let name = match entry.file_name().into_string() {
Ok(some) => some,
Err(name_os) => {
log::warn!(
"failed to parse entry in {:?}: {:?} is not valid UTF-8",
self.path,
name_os,
);
continue;
}
};
let path = entry.path();
let hidden = name.starts_with(".") || hidden_attribute(&path);
let is_dir = path.is_dir();
//TODO: configurable size
let icon_size = 32;
let icon = if is_dir {
folder_icon(&path, icon_size)
} else {
mime_icon(&path, icon_size)
};
self.items.push(Item {
name,
path,
hidden,
is_dir,
icon,
select_time: None,
});
}
}
Err(err) => {
log::warn!("failed to read directory {:?}: {}", self.path, err);
}
} }
self.items.sort_by(|a, b| match (a.is_dir, b.is_dir) {
(true, false) => Ordering::Less,
(false, true) => Ordering::Greater,
_ => a.name.cmp(&b.name),
});
} }
pub fn title(&self) -> String { pub fn title(&self) -> String {
@ -215,27 +231,33 @@ impl Tab {
let mut cd = None; let mut cd = None;
match message { match message {
Message::Click(click_i) => { Message::Click(click_i) => {
for (i, item) in self.items.iter_mut().enumerate() { if let Some(ref mut items) = self.items_opt {
if i == click_i { for (i, item) in items.iter_mut().enumerate() {
if let Some(select_time) = item.select_time { if i == click_i {
if select_time.elapsed() < DOUBLE_CLICK_DURATION { if let Some(select_time) = item.select_time {
if item.is_dir { if select_time.elapsed() < DOUBLE_CLICK_DURATION {
cd = Some(item.path.clone()); if item.is_dir {
} else { cd = Some(item.path.clone());
let mut command = open_command(&item.path); } else {
match command.spawn() { let mut command = open_command(&item.path);
Ok(_) => (), match command.spawn() {
Err(err) => { Ok(_) => (),
log::warn!("failed to open {:?}: {}", item.path, err); Err(err) => {
log::warn!(
"failed to open {:?}: {}",
item.path,
err
);
}
} }
} }
} }
} }
//TODO: prevent triple-click and beyond from opening file
item.select_time = Some(Instant::now());
} else {
item.select_time = None;
} }
//TODO: prevent triple-click and beyond from opening file
item.select_time = Some(Instant::now());
} else {
item.select_time = None;
} }
} }
} }
@ -250,7 +272,7 @@ impl Tab {
} }
if let Some(path) = cd { if let Some(path) = cd {
self.path = path; self.path = path;
self.rescan(); self.items_opt = None;
true true
} else { } else {
false false
@ -261,30 +283,60 @@ impl Tab {
let cosmic_theme::Spacing { space_xxs, .. } = core.system_theme().cosmic().spacing; let cosmic_theme::Spacing { space_xxs, .. } = core.system_theme().cosmic().spacing;
let mut column = widget::column(); let mut column = widget::column();
for (i, item) in self.items.iter().enumerate() { if let Some(ref items) = self.items_opt {
if item.hidden { let mut count = 0;
//TODO: SHOW HIDDEN OPTION let mut hidden = 0;
continue; for (i, item) in items.iter().enumerate() {
if item.hidden {
hidden += 1;
//TODO: SHOW HIDDEN OPTION
continue;
}
column = column.push(
widget::button(
widget::row::with_children(vec![
widget::icon::icon(item.icon_handle.clone()).into(),
widget::text(item.name.clone()).into(),
])
.align_items(Alignment::Center)
.spacing(space_xxs),
)
//TODO: improve style
.style(if item.select_time.is_some() {
theme::Button::Standard
} else {
theme::Button::AppletMenu
})
.width(Length::Fill)
.on_press(Message::Click(i)),
);
count += 1;
} }
column = column.push( if count == 0 {
widget::button( return widget::container(
widget::row::with_children(vec![ widget::column::with_children(vec![
item.icon.clone().into(), widget::icon::from_name("folder-symbolic")
widget::text(item.name.clone()).into(), .size(64)
.icon()
.into(),
widget::text(if hidden > 0 {
fl!("empty-folder-hidden")
} else {
fl!("empty-folder")
})
.into(),
]) ])
.align_items(Alignment::Center) .align_items(Alignment::Center)
.spacing(space_xxs), .spacing(space_xxs),
) )
//TODO: improve style .align_x(Horizontal::Center)
.style(if item.select_time.is_some() { .align_y(Vertical::Center)
theme::Button::Standard .height(Length::Fill)
} else {
theme::Button::AppletMenu
})
.width(Length::Fill) .width(Length::Fill)
.on_press(Message::Click(i)), .into();
); }
} }
widget::scrollable(column.width(Length::Fill)).into() widget::scrollable(column.width(Length::Fill)).into()
} }