diff --git a/crates/tunal-core/src/trash.rs b/crates/tunal-core/src/trash.rs index 5d46c5f..a0f0b6a 100644 --- a/crates/tunal-core/src/trash.rs +++ b/crates/tunal-core/src/trash.rs @@ -126,6 +126,31 @@ pub fn items() -> Vec { items } +/// How many items the trash holds and their size in bytes, folders counted with what is in +/// them (links not followed). Walks every file: call it off the UI thread. +pub fn usage() -> (usize, u64) { + fn size(path: &Path) -> u64 { + let Ok(meta) = fs::symlink_metadata(path) else { + return 0; + }; + if !meta.is_dir() { + return meta.len(); + } + fs::read_dir(path) + .map(|d| d.flatten().map(|e| size(&e.path())).sum()) + .unwrap_or(0) + } + let mut count = 0; + let mut bytes = 0; + for dir in trash_dirs() { + for file in fs::read_dir(dir.files()).into_iter().flatten().flatten() { + count += 1; + bytes += size(&file.path()); + } + } + (count, bytes) +} + /// Original location of a top-level trash item (`NotFound` for paths inside one). pub fn original(uri: &Uri) -> Result { let (dir, name, sub) = resolve(uri)?; @@ -470,7 +495,7 @@ fn resolve(uri: &Uri) -> Result<(TrashDir, Vec, Vec)> { } /// Local path behind an item URI. -fn local_path(uri: &Uri) -> Result { +pub fn local_path(uri: &Uri) -> Result { let (dir, name, sub) = resolve(uri)?; let mut path = dir.files().join(OsStr::from_bytes(&name)); if !sub.is_empty() { diff --git a/crates/tunal/src/folder/mod.rs b/crates/tunal/src/folder/mod.rs index 218238c..40af68e 100644 --- a/crates/tunal/src/folder/mod.rs +++ b/crates/tunal/src/folder/mod.rs @@ -19,7 +19,7 @@ use std::time::{Duration, Instant}; use slint::{Model, ModelNotify, ModelRc, ModelTracker, SharedString, VecModel}; use tunal_core::playlist::Playlist; -use tunal_core::{Entry, Error, Locations, Uri, Watch}; +use tunal_core::{Entry, Error, FILE, Locations, TRASH, Uri, Watch}; use tunal_thumbs::{Priority, ThumbSize}; use crate::config; @@ -931,17 +931,24 @@ impl FolderModel { if s.previews.epoch != epoch { s.previews.restart(epoch); } - let dir = s.dir.as_ref().and_then(Uri::to_path); - let Some(dir) = dir - .filter(|_| s.settings.previews && matches!(s.status, Status::Ready | Status::Loading)) - else { + // Trashed files have a local path each (their trash folders differ). + let dir = s.dir.clone().filter(|d| { + (d.scheme() == FILE || d.scheme() == TRASH) + && s.settings.previews + && matches!(s.status, Status::Ready | Status::Loading) + }); + let Some(dir) = dir else { s.previews.cancel_all(); return; }; - let remote = *s - .previews - .remote - .get_or_insert_with(|| thumbs_bridge::on_network_fs(&dir)); + let local = dir.to_path(); + let remote = match &local { + Some(path) => *s + .previews + .remote + .get_or_insert_with(|| thumbs_bridge::on_network_fs(path)), + None => false, + }; let size = match s.settings.mode { ViewMode::Details => ThumbSize::Normal, ViewMode::Grid => ThumbSize::Large, @@ -971,12 +978,19 @@ impl FolderModel { if !s.previews.needs(&name, size) { continue; } + let path = match &local { + Some(dir) => Some(dir.join(std::ffi::OsStr::from_bytes(&name))), + None => tunal_core::trash::local_path(&dir.join(&name)).ok(), + }; + let Some(path) = path else { + continue; + }; let id = s.previews.next_id(); let inbox = s.previews.inbox.clone(); let deliver = self.deliver.clone(); let key = name.clone(); let ticket = self.thumbs.request( - dir.join(std::ffi::OsStr::from_bytes(&name)), + path, size, remote, if near { diff --git a/crates/tunal/src/shell/mod.rs b/crates/tunal/src/shell/mod.rs index 2426909..cf366ba 100644 --- a/crates/tunal/src/shell/mod.rs +++ b/crates/tunal/src/shell/mod.rs @@ -46,6 +46,7 @@ enum PlaceAction { MoveUp, MoveDown, Remove, + EmptyTrash, } impl PlaceAction { @@ -56,6 +57,7 @@ impl PlaceAction { Self::MoveUp => ("Move up", "\u{e5d8}"), Self::MoveDown => ("Move down", "\u{e5db}"), Self::Remove => ("Remove", "\u{e872}"), + Self::EmptyTrash => ("Empty Trash", "\u{e92b}"), } } } @@ -99,6 +101,8 @@ pub struct Shell { closed: RefCell>>, /// The tab whose menu is open. tab_menu: Cell>, + /// Items of the Trash pane when its size was last counted. + trash_seen: Cell>, } impl Shell { @@ -139,6 +143,7 @@ impl Shell { keymap: RefCell::new(keymap), prefs: RefCell::new(prefs), closed: RefCell::new(Vec::new()), + trash_seen: Cell::new(None), tab_menu: Cell::new(None), }); let actions = Actions::attach(ui, &shell, shell.locations.clone()); @@ -640,9 +645,41 @@ impl Shell { ui.set_path(dir.as_ref().map(path::text).unwrap_or_default().into()); ui.set_sort_menu(ModelRc::new(VecModel::from(pane.model.sort_menu()))); ui.set_status(status(&pane).into()); + self.show_trash(&ui, &pane); self.show_sidebar(&ui, dir.as_ref()); } + /// The status line, after the selection of `pane` changed. + fn show_status(&self, pane: &Pane) { + if let Some(ui) = self.ui.upgrade() { + ui.set_status(status(pane).into()); + } + } + + /// The Trash bar while the Trash is shown; its size is counted again when its items change. + fn show_trash(&self, ui: &MainWindow, pane: &Pane) { + let shown = pane.dir().is_some_and(|d| d.scheme() == tunal_core::TRASH); + ui.set_trash_shown(shown); + if !shown { + self.trash_seen.set(None); + return; + } + let items = pane.model.state().items as usize; + if self.trash_seen.replace(Some(items)) == Some(items) { + return; + } + let ui = ui.as_weak(); + std::thread::spawn(move || { + let (count, bytes) = tunal_core::trash::usage(); + let text = match count { + 0 => String::new(), + 1 => format!("1 item, {}", format::size(bytes)), + n => format!("{n} items, {}", format::size(bytes)), + }; + let _ = ui.upgrade_in_event_loop(move |ui| ui.set_trash_text(text.into())); + }); + } + fn show_sidebar(&self, ui: &MainWindow, dir: Option<&Uri>) { let places = self.places.borrow(); let current = self.current_place(&places, dir); @@ -816,16 +853,17 @@ impl Shell { /// The menu of an entry the user arranges, after "Open in new tab". fn place_actions(&self, section: Section, index: usize) -> Vec { use PlaceAction::*; - let standard = self - .places - .borrow() - .get(section, index) - .is_some_and(|p| p.standard); - if standard { + let places = self.places.borrow(); + let place = places.get(section, index); + let mut actions = if place.is_some_and(|p| p.standard) { vec![ChangeIcon, MoveUp, MoveDown] } else { vec![Rename, ChangeIcon, MoveUp, MoveDown, Remove] + }; + if place.is_some_and(|p| p.uri == tunal_core::trash::root()) { + actions.push(EmptyTrash); } + actions } fn device_uri(&self, section: Section, index: usize) -> Option { @@ -898,6 +936,11 @@ impl Shell { self.move_place((section, index), (section, Some(index + 2))); } Some(PlaceAction::Remove) => self.places.borrow_mut().remove(section, index), + Some(PlaceAction::EmptyTrash) => { + self.menu_target.set(None); + self.run_action(crate::actions::Action::EmptyTrash); + return; + } None => {} } } @@ -1099,6 +1142,7 @@ impl Shell { // >= 0; Enter sends -1) is then ignored. on_pane!(on_row_clicked, |s, p, row, ctrl, shift| { p.model.click(row.max(0) as usize, ctrl, shift); + s.show_status(&p); if !ctrl && !shift && s.prefs().click == Click::Single { s.activate_row(&p, usize::try_from(row).ok()); } @@ -1109,19 +1153,23 @@ impl Shell { s.activate_row(&p, usize::try_from(row).ok()) } ); - on_pane!(on_select_all, |_s, p| p.model.select_all()); + on_pane!(on_select_all, |s, p| { + p.model.select_all(); + s.show_status(&p); + }); on_pane!(on_band_selected, |s, p, c1, y1, c2, y2, add, start| { p.model.band((c1, c2), (y1, y2), add, start); - if let Some(ui) = s.ui.upgrade() { - ui.set_status(status(&p).into()); - } + s.show_status(&p); }); on_pane!(on_grid_columns_changed, |_s, p, n| p .model .set_columns(n.max(1) as usize)); on_pane!(on_motion_settled, |_s, p| p.model.motion_settled()); on_pane!(on_view_mode_selected, |_s, p, mode| p.model.set_mode(mode)); - on_pane!(on_clear_selection, |_s, p| p.model.clear_selection()); + on_pane!(on_clear_selection, |s, p| { + p.model.clear_selection(); + s.show_status(&p); + }); let me = self.me.clone(); ui.on_navigate(move |i, key, shift, page| { me.upgrade() @@ -1147,6 +1195,8 @@ impl Shell { s.eject_place(sec, index.max(0) as usize) } ); + on!(on_empty_trash, |s| s + .run_action(crate::actions::Action::EmptyTrash)); on!(on_section_toggled, |s, i| if let Some(sec) = section(i) { s.places.borrow_mut().toggle(sec); s.show_chrome() diff --git a/crates/tunal/ui/app.slint b/crates/tunal/ui/app.slint index b54404d..9ff8f53 100644 --- a/crates/tunal/ui/app.slint +++ b/crates/tunal/ui/app.slint @@ -14,7 +14,7 @@ import { Toolbar } from "shell/toolbar.slint"; import { Crumb, Completion, CompletionList } from "shell/breadcrumb.slint"; import { Pane, PaneData } from "shell/pane.slint"; import { Sidebar, SidebarSection, PlaceItem } from "shell/sidebar.slint"; -import { StatusBar } from "shell/status-bar.slint"; +import { StatusBar, TrashBar } from "shell/status-bar.slint"; import { PlayerBar, PlayerData } from "shell/player-bar.slint"; import { ToastStack, ToastData, ToastProgress } from "components/toast.slint"; import { TooltipLayer } from "components/tooltip.slint"; @@ -273,6 +273,10 @@ export component MainWindow inherits Window { callback place-icon-saved(); in property status; + // The Trash is shown: what it holds ("" when empty), and its Empty Trash button. + in property trash-shown; + in property trash-text; + callback empty-trash(); // Network connections (net_bridge.rs): the connection editor, an unknown or changed host // key, and a password or passphrase to ask for. @@ -687,6 +691,14 @@ export component MainWindow inherits Window { } } + TrashBar { + shown: root.trash-shown; + text: root.trash-text; + empty => { + root.empty-trash(); + } + } + StatusBar { text: root.status; } diff --git a/crates/tunal/ui/components/button.slint b/crates/tunal/ui/components/button.slint index a573e01..ef572fe 100644 --- a/crates/tunal/ui/components/button.slint +++ b/crates/tunal/ui/components/button.slint @@ -4,7 +4,9 @@ import { Icon } from "icon.slint"; import { StateLayer } from "state-layer.slint"; // Emphasis, from the main action of a screen (filled) down to the least (text). -export enum ButtonKind { filled, tonal, outlined, text } +// `danger`: filled red (the error colour; its deep container in dark mode, where the error +// colour itself is pale), for an action that destroys data. +export enum ButtonKind { filled, tonal, outlined, text, danger } // A labelled button with an optional leading icon. export component Button inherits StateLayer { @@ -13,9 +15,10 @@ export component Button inherits StateLayer { in property icon; in property kind: ButtonKind.filled; - property contained: root.kind == ButtonKind.filled || root.kind == ButtonKind.tonal; + property contained: root.kind == ButtonKind.filled || root.kind == ButtonKind.tonal || root.kind == ButtonKind.danger; property content: !root.enabled ? Theme.on-surface.with-alpha(Theme.disabled-content) : root.kind == ButtonKind.filled ? Theme.on-primary + : root.kind == ButtonKind.danger ? (Theme.dark ? Theme.on-error-container : Theme.on-error) : root.kind == ButtonKind.tonal ? Theme.on-secondary-container : Theme.primary; @@ -24,6 +27,7 @@ export component Button inherits StateLayer { background: !root.contained ? transparent : !root.enabled ? Theme.on-surface.with-alpha(Theme.disabled-container) : root.kind == ButtonKind.filled ? Theme.primary + : root.kind == ButtonKind.danger ? (Theme.dark ? Theme.error-container : Theme.error) : Theme.secondary-container; border-width: root.kind == ButtonKind.outlined ? Theme.hairline : 0; border-color: root.enabled ? Theme.outline : Theme.on-surface.with-alpha(Theme.disabled-container); diff --git a/crates/tunal/ui/components/icon.slint b/crates/tunal/ui/components/icon.slint index 4bd783d..e9f9b89 100644 --- a/crates/tunal/ui/components/icon.slint +++ b/crates/tunal/ui/components/icon.slint @@ -23,6 +23,7 @@ export component Icon inherits Text { export global Icons { out property folder: "\u{e2c7}"; out property eject: "\u{e8fb}"; + out property delete-forever: "\u{e92b}"; out property home: "\u{e88a}"; out property check: "\u{e5ca}"; out property close: "\u{e5cd}"; diff --git a/crates/tunal/ui/components/state-layer.slint b/crates/tunal/ui/components/state-layer.slint index 32bbbc8..555d465 100644 --- a/crates/tunal/ui/components/state-layer.slint +++ b/crates/tunal/ui/components/state-layer.slint @@ -45,6 +45,7 @@ export component StateLayer inherits Rectangle { out property mouse-x: touch.mouse-x; out property mouse-y: touch.mouse-y; out property pressed-x: touch.pressed-x; + out property pressed-y: touch.pressed-y; property keyboard-focus; property ring-inset: Theme.focus-ring-width + root.ring-offset; diff --git a/crates/tunal/ui/dialogs/confirm.slint b/crates/tunal/ui/dialogs/confirm.slint index abda2fc..ad9ad1a 100644 --- a/crates/tunal/ui/dialogs/confirm.slint +++ b/crates/tunal/ui/dialogs/confirm.slint @@ -8,7 +8,7 @@ export component ConfirmDialog inherits Dialog { in property action-text: "Delete"; callback answered(bool); - actions: [{ text: "Cancel", kind: ButtonKind.text }, { text: root.action-text, kind: ButtonKind.filled }]; + actions: [{ text: "Cancel", kind: ButtonKind.text }, { text: root.action-text, kind: ButtonKind.danger }]; default-action: 0; cancel-action: 0; action(i) => { diff --git a/crates/tunal/ui/shell/pane.slint b/crates/tunal/ui/shell/pane.slint index 57e1ca6..8206a44 100644 --- a/crates/tunal/ui/shell/pane.slint +++ b/crates/tunal/ui/shell/pane.slint @@ -97,6 +97,10 @@ export component Pane inherits Rectangle { } root.activated(); root.focus-view(); + // A plain click on the list's empty space lets go of the selection. + if !self.add && details.in-list(self.absolute-position.x + self.mouse-x, self.absolute-position.y + self.mouse-y) { + root.clear-selection(); + } } double-clicked => { FolderActions.empty-double-clicked(root.index); diff --git a/crates/tunal/ui/shell/status-bar.slint b/crates/tunal/ui/shell/status-bar.slint index 997eda6..46a1cdb 100644 --- a/crates/tunal/ui/shell/status-bar.slint +++ b/crates/tunal/ui/shell/status-bar.slint @@ -1,4 +1,7 @@ import { Theme } from "../theme.slint"; +import { Motion } from "../motion.slint"; +import { Icon, Icons } from "../components/icon.slint"; +import { Button, ButtonKind } from "../components/button.slint"; // One line under the panes: what the active pane shows and what is selected in it. export component StatusBar inherits Rectangle { @@ -17,3 +20,54 @@ export component StatusBar inherits Rectangle { overflow: elide; } } + +// Over the status bar while the Trash is shown: what it holds, and Empty Trash. +export component TrashBar inherits Rectangle { + in property shown; + // "4 items, 8.0 KiB"; empty for an empty Trash. + in property text; + callback empty(); + + height: root.shown ? 56px : 0; + clip: true; + animate height { duration: Motion.time(Motion.medium2); easing: Motion.emphasized; } + + HorizontalLayout { + padding-left: Theme.space-4; + padding-right: Theme.space-3; + padding-top: Theme.space-2; + padding-bottom: Theme.space-2; + spacing: Theme.space-3; + + VerticalLayout { + alignment: center; + Icon { + glyph: Icons.delete; + size: 20px; + color: Theme.on-surface-variant; + } + } + + Text { + horizontal-stretch: 1; + text: root.text == "" ? "The Trash is empty" : "Trash: " + root.text; + color: Theme.on-surface; + font-size: Theme.body-medium.size; + font-weight: Theme.body-medium.weight; + vertical-alignment: center; + overflow: elide; + } + + if root.text != "": VerticalLayout { + alignment: center; + Button { + kind: ButtonKind.danger; + icon: Icons.delete-forever; + text: "Empty Trash"; + clicked => { + root.empty(); + } + } + } + } +} diff --git a/crates/tunal/ui/views/details-view.slint b/crates/tunal/ui/views/details-view.slint index 8893a1f..d7d6548 100644 --- a/crates/tunal/ui/views/details-view.slint +++ b/crates/tunal/ui/views/details-view.slint @@ -441,7 +441,7 @@ export component BandBox inherits Rectangle { let x = ax - root.origin-x; let y = ay - root.origin-y; if start { - if x < 0 || y < 0 || x > root.view-width || y > root.view-height { + if !root.inside(ax, ay) { return false; } root.add = add; @@ -464,6 +464,12 @@ export component BandBox inherits Rectangle { root.shown = false; } + public function inside(ax: length, ay: length) -> bool { + let x = ax - root.origin-x; + let y = ay - root.origin-y; + x >= 0 && y >= 0 && x <= root.view-width && y <= root.view-height + } + // Drawn clipped to what is in view. property top: max(0px, min(root.y0, root.y1) + root.scroll); property bottom: min(root.view-height, max(root.y0, root.y1) + root.scroll); @@ -543,6 +549,22 @@ export component DetailsView inherits Rectangle { band.stop(); } + // True for a window point inside the list. + public function in-list(ax: length, ay: length) -> bool { + band.inside(ax, ay) + } + + // Where a row's name column ends: a drag that starts past it draws the selection box + // instead of moving the file. + property name-end: list.visible-width - 2 * Columns.inset - Columns.tools - Theme.space-2 + - (root.narrow ? 0px : Columns.size + Columns.modified + 2 * Theme.space-3) + - (root.compact ? 0px : Columns.kind + Theme.space-3); + // A selection box from a row's blank columns: running, ran since the press (that release + // is no click), and whether it adds. + property row-banding; + property row-banded; + property row-band-add; + // Scrolls just enough to show `row`. function reveal(row: int) { if row < 0 { @@ -750,7 +772,7 @@ export component DetailsView inherits Rectangle { radius: item.border-radius; DragArea { - enabled: !row.editing; + enabled: !row.editing && !(item.pressed && item.pressed-x > root.name-end); allow-move: true; allow-copy: true; allow-link: true; @@ -784,6 +806,14 @@ export component DetailsView inherits Rectangle { root.rename-cancelled(); } pointer(event) => { + if event.button == PointerEventButton.left && event.kind == PointerEventKind.down { + root.row-banded = false; + root.row-band-add = event.modifiers.control || event.modifiers.shift; + } + if event.button == PointerEventButton.left && event.kind == PointerEventKind.up && root.row-banding { + root.row-banding = false; + root.band-end(); + } if event.button == PointerEventButton.right && event.kind == PointerEventKind.down { root.context-menu(i, self.absolute-position.x + self.mouse-x, self.absolute-position.y + self.mouse-y); } @@ -791,7 +821,27 @@ export component DetailsView inherits Rectangle { FolderActions.middle-clicked(root.pane, i); } } + moved => { + if !self.pressed || self.pressed-x <= root.name-end { + return; + } + let ax = self.absolute-position.x + self.mouse-x; + let ay = self.absolute-position.y + self.mouse-y; + if root.row-banding { + root.band-to(ax, ay, root.row-band-add, false); + } else if !root.row-banded && (abs(self.mouse-x - self.pressed-x) > Theme.drag-slop || abs(self.mouse-y - self.pressed-y) > Theme.drag-slop) { + root.row-banded = true; + keys.focus(); + root.row-banding = root.band-to(self.absolute-position.x + self.pressed-x, self.absolute-position.y + self.pressed-y, root.row-band-add, true); + if root.row-banding { + root.band-to(ax, ay, root.row-band-add, false); + } + } + } clicked => { + if root.row-banded { + return; + } keys.focus(); root.row-clicked(i, self.modifiers.control, self.modifiers.shift); } diff --git a/crates/tunal/ui/views/grid-view.slint b/crates/tunal/ui/views/grid-view.slint index 9b6c9a1..6df0451 100644 --- a/crates/tunal/ui/views/grid-view.slint +++ b/crates/tunal/ui/views/grid-view.slint @@ -258,6 +258,10 @@ export component GridView inherits Rectangle { band.stop(); } + public function in-list(ax: length, ay: length) -> bool { + band.inside(ax, ay) + } + // Scrolls just enough to show `line` (header lines above it are shorter). function reveal(line: int) { if line < 0 || line >= root.lines.length { diff --git a/crates/tunal/ui/views/view-switch.slint b/crates/tunal/ui/views/view-switch.slint index 2d2fb7a..5ecd0fc 100644 --- a/crates/tunal/ui/views/view-switch.slint +++ b/crates/tunal/ui/views/view-switch.slint @@ -141,6 +141,10 @@ export component FolderView inherits Rectangle { details.band-end(); } + public function in-list(ax: length, ay: length) -> bool { + root.grid ? grid.in-list(ax, ay) : details.in-list(ax, ay) + } + list-clock := MotionClock { epoch: root.state.motion; duration: MotionPrefs.list-change; diff --git a/docs/PROGRESS.md b/docs/PROGRESS.md index 2e505d5..7090813 100644 --- a/docs/PROGRESS.md +++ b/docs/PROGRESS.md @@ -321,7 +321,12 @@ One entry per session, newest last, ≤ 15 lines: done, verified how, not done, ## Selection box (user request) - Done: a drag from empty space in a folder draws a box and selects what it covers (`BandBox` in details-view.slint, `FolderModel::band`); Ctrl/Shift flips items in the old selection; the list scrolls past its top or bottom edge; status line follows. Probe: `xdnd_probe press`. - Verified: clippy, release build; box in grid and details view under Xvfb, status "3 of 4 selected". -- Not done: in the details view a drag on a row still drags the file (no box from a row's blank columns); a plain click on empty space still keeps the selection; no test run. +- Not done: no test run. + +## Selection box from rows, Trash bar (user request) +- Done: in the details view a drag that starts past a row's name column draws the box; a plain click on empty space clears the selection; the status line follows clicks, Ctrl+A and clearing (it lagged before). Trash: bar over the status line with item count, size (`trash::usage`) and a red Empty Trash button (`ButtonKind.danger`, also on the confirm dialog); Empty Trash in the sidebar Trash menu; previews of trashed images, audio and video (`trash::local_path` now public). +- Verified: clippy (tunal, tunal-core), release build; under Xvfb: box from the Type column, empty click clears, Trash bar, sidebar menu, Empty Trash on a scratch trash, red button in dark mode. +- Not done: content-based icons (files whose name doesn't say their type) still don't apply in the Trash; no test run. ## Later -