From 42aa59906642699de30d4394c4057a036868c62a Mon Sep 17 00:00:00 2001 From: astar76 Date: Thu, 19 Nov 2026 10:00:51 -0600 Subject: [PATCH] feat: icon sizes, default view that applies, sort menus; fix grid columns - Default view (Details, Icons, Large icons) now applies to every folder that keeps no view of its own: on opening, on leaving a folder with a saved view, and at once in open panes when the setting changes. - Grid icon sizes small / medium / large / extra large: Ctrl+scroll over a pane, Ctrl+plus, Ctrl+minus, Ctrl+0; per pane, kept in `.directory` (IconSize) with the rest of a remembered view. - A Sort button in the toolbar opens the sort menu (keys, ascending/descending, folders first, group by); the context menu of empty space and folders gets Sort by and View mode submenus (new action ids, also in the menu editor). - Grid in a new tab showed one item per row: the reused pane view never reported its column count to the new folder model. FolderState now carries the model's columns and the grid reports again when they differ. - Item and the sort functions are pure modules now (folder/item.rs, sort.rs). Co-Authored-By: Claude Opus 5.5 --- crates/tunal-dirmeta/src/lib.rs | 7 +- crates/tunal-dirmeta/tests/dirmeta.rs | 1 + crates/tunal/src/actions.rs | 42 ++- crates/tunal/src/config.rs | 3 +- crates/tunal/src/folder/item.rs | 65 +++++ crates/tunal/src/folder/look.rs | 11 +- crates/tunal/src/folder/mod.rs | 345 +++++++++++++++++------ crates/tunal/src/folder/sort.rs | 51 +--- crates/tunal/src/menus.rs | 6 + crates/tunal/src/settings.rs | 2 +- crates/tunal/src/shell/mod.rs | 68 +++-- crates/tunal/ui/app.slint | 21 +- crates/tunal/ui/dialogs/settings.slint | 2 +- crates/tunal/ui/shell/toolbar.slint | 16 +- crates/tunal/ui/theme.slint | 2 +- crates/tunal/ui/views/details-view.slint | 5 + crates/tunal/ui/views/grid-view.slint | 48 +++- 17 files changed, 506 insertions(+), 189 deletions(-) create mode 100644 crates/tunal/src/folder/item.rs diff --git a/crates/tunal-dirmeta/src/lib.rs b/crates/tunal-dirmeta/src/lib.rs index 7da3d81..a9deb16 100644 --- a/crates/tunal-dirmeta/src/lib.rs +++ b/crates/tunal-dirmeta/src/lib.rs @@ -219,6 +219,7 @@ impl DirMeta { group: get("Group"), hidden: self.tunal("ShowHidden").map(|v| v == "true"), previews: self.tunal("Previews").map(|v| v == "true"), + zoom: get("IconSize"), playlist: get("Playlist"), } } @@ -235,6 +236,7 @@ impl DirMeta { self.set_tunal("Group", view.group.as_deref()); self.set_tunal("ShowHidden", view.hidden.map(flag)); self.set_tunal("Previews", view.previews.map(flag)); + self.set_tunal("IconSize", view.zoom.as_deref()); self.set_tunal("Playlist", view.playlist.as_deref()); } @@ -248,13 +250,14 @@ impl DirMeta { } } -const VIEW_KEYS: [&str; 7] = [ +const VIEW_KEYS: [&str; 8] = [ "View", "Sort", "SortOrder", "Group", "ShowHidden", "Previews", + "IconSize", "Playlist", ]; @@ -268,6 +271,8 @@ pub struct View { pub group: Option, pub hidden: Option, pub previews: Option, + /// Icon size of the grid: `small`, `medium`, `large` or `extra-large`. + pub zoom: Option, /// File name of the playlist the folder is sorted by (D-021). pub playlist: Option, } diff --git a/crates/tunal-dirmeta/tests/dirmeta.rs b/crates/tunal-dirmeta/tests/dirmeta.rs index c0be17a..1cf9596 100644 --- a/crates/tunal-dirmeta/tests/dirmeta.rs +++ b/crates/tunal-dirmeta/tests/dirmeta.rs @@ -155,6 +155,7 @@ fn view_settings_round_trip() { group: Some("type".into()), hidden: Some(false), previews: Some(true), + zoom: Some("large".into()), playlist: Some("best of.m3u".into()), }; let mut meta = DirMeta::parse(SAMPLE); diff --git a/crates/tunal/src/actions.rs b/crates/tunal/src/actions.rs index c6687d4..5db39f3 100644 --- a/crates/tunal/src/actions.rs +++ b/crates/tunal/src/actions.rs @@ -42,6 +42,9 @@ pub enum Action { Appearance, EditTags, Play, + /// Submenus over the pane's sort and view settings. + SortBy, + ViewMode, } enum Shortcut { @@ -50,7 +53,7 @@ enum Shortcut { } impl Action { - pub const ALL: [Action; 17] = [ + pub const ALL: [Action; 19] = [ Self::Cut, Self::Copy, Self::Paste, @@ -68,6 +71,8 @@ impl Action { Self::Appearance, Self::EditTags, Self::Play, + Self::SortBy, + Self::ViewMode, ]; pub fn id(self) -> &'static str { @@ -89,6 +94,8 @@ impl Action { Self::Appearance => "appearance", Self::EditTags => "edit-tags", Self::Play => "play", + Self::SortBy => "sort-by", + Self::ViewMode => "view-mode", } } @@ -115,6 +122,8 @@ impl Action { Self::Appearance => "Folder appearance…", Self::EditTags => "Edit tags…", Self::Play => "Play", + Self::SortBy => "Sort by", + Self::ViewMode => "View mode", } } @@ -138,6 +147,8 @@ impl Action { Self::Appearance => "\u{e40a}", Self::EditTags => "\u{e892}", Self::Play => "\u{e037}", + Self::SortBy => "\u{e164}", + Self::ViewMode => "\u{e9b0}", } } @@ -160,7 +171,9 @@ impl Action { | Self::OpenWith | Self::Properties | Self::Appearance - | Self::EditTags => { + | Self::EditTags + | Self::SortBy + | Self::ViewMode => { return None; } }) @@ -213,7 +226,7 @@ impl Action { Self::EmptyTrash => ctx.in_trash, Self::Settings => true, Self::Open | Self::OpenWith => files && ctx.selected > 0, - Self::Properties => true, + Self::Properties | Self::SortBy | Self::ViewMode => true, Self::Appearance => files && ctx.selected <= 1, Self::EditTags | Self::Play => files && ctx.audio, } @@ -409,7 +422,11 @@ impl Actions { shortcut: action.shortcut_text().into(), disabled: !action.enabled(&ctx), separator: false, - submenu: ModelRc::new(VecModel::::default()), + submenu: ModelRc::new(VecModel::from(match action { + Action::SortBy => pane.model.sort_submenu(), + Action::ViewMode => pane.model.view_menu(), + _ => Vec::new(), + })), }, None => MenuItem { text: Default::default(), @@ -450,10 +467,15 @@ impl Actions { } } - fn menu_activated(&self, index: usize) { + /// Item `index` of the open context menu was chosen (`sub` in its submenu, else -1). + fn menu_activated(&self, index: usize, sub: i32) { let action = self.menu.borrow().get(index).copied().flatten(); - if let Some(action) = action { - self.run(action); + let pane = self.shell().and_then(|s| s.active()); + match (action, usize::try_from(sub), pane) { + (Some(Action::SortBy), Ok(sub), Some(pane)) => pane.model.sort_menu_activated(sub, -1), + (Some(Action::ViewMode), Ok(sub), Some(pane)) => pane.model.view_menu_activated(sub), + (Some(action), _, _) => self.run(action), + _ => {} } } @@ -589,6 +611,8 @@ impl Actions { } return; } + // Only submenus: their entries run in `menu_activated`. + Action::SortBy | Action::ViewMode => return, Action::EmptyTrash => { let me = self.me.clone(); self.bridge.confirm( @@ -780,9 +804,9 @@ impl Actions { } }); let me = self.me.clone(); - ui.on_context_menu_activated(move |i| { + ui.on_context_menu_activated(move |i, sub| { if let Some(a) = me.upgrade() { - a.menu_activated(i.max(0) as usize); + a.menu_activated(i.max(0) as usize, sub); } }); let me = self.me.clone(); diff --git a/crates/tunal/src/config.rs b/crates/tunal/src/config.rs index 293ddc4..2958bf1 100644 --- a/crates/tunal/src/config.rs +++ b/crates/tunal/src/config.rs @@ -60,13 +60,14 @@ pub fn store(name: &str, value: &T) { } } -/// Details list or icon grid. +/// Details list, icon grid, or the grid with large icons. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "lowercase")] pub enum View { #[default] Details, Grid, + Large, } #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] diff --git a/crates/tunal/src/folder/item.rs b/crates/tunal/src/folder/item.rs new file mode 100644 index 0000000..3806575 --- /dev/null +++ b/crates/tunal/src/folder/item.rs @@ -0,0 +1,65 @@ +//! One entry of a folder with what sorting and drawing need, computed once. Pure: no UI or +//! disk access (the integration tests build these by hand). + +use std::path::PathBuf; + +use tunal_core::Entry; + +use crate::icons::Category; + +/// A folder's own icon and name from its `.directory`; each part optional. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct Look { + pub name: Option, + /// A Material glyph (`Glyph` key). + pub glyph: Option, + /// An image file: the `Icon` key, resolved through the icon theme. + pub picture: Option, +} + +pub struct Item { + pub entry: Entry, + /// Lower-cased display name, for natural order and the filter. + pub key: String, + pub is_dir: bool, + pub category: Category, + /// The folder's own icon and name from its `.directory`. + pub look: Option, +} + +impl Item { + pub fn new(entry: Entry) -> Self { + Self { + key: entry.display_name.to_lowercase(), + is_dir: entry.kind.is_dir(), + category: Category::of(&entry), + entry, + look: None, + } + } + + pub fn with_look(mut self, look: Option) -> Self { + self.set_look(look); + self + } + + /// Sets the look; a display name from it sorts and filters in place of the real name. + pub fn set_look(&mut self, look: Option) { + let name = look.as_ref().and_then(|l| l.name.as_deref()); + self.key = name.unwrap_or(&self.entry.display_name).to_lowercase(); + self.look = look; + } + + /// The name shown for the entry. + pub fn shown_name(&self) -> &str { + self.look + .as_ref() + .and_then(|l| l.name.as_deref()) + .unwrap_or(&self.entry.display_name) + } + + /// Folders count as empty for size order. + pub fn size(&self) -> u64 { + if self.is_dir { 0 } else { self.entry.size } + } +} diff --git a/crates/tunal/src/folder/look.rs b/crates/tunal/src/folder/look.rs index 62bfcea..d07669c 100644 --- a/crates/tunal/src/folder/look.rs +++ b/crates/tunal/src/folder/look.rs @@ -8,18 +8,9 @@ use std::time::SystemTime; use tunal_dirmeta::{DirMeta, FILE_NAME, View}; +pub use super::item::Look; use crate::desktop_apps::IconTheme; -/// A folder's own icon and name; each part optional. -#[derive(Clone, Debug, Default, PartialEq, Eq)] -pub struct Look { - pub name: Option, - /// A Material glyph (`Glyph` key). - pub glyph: Option, - /// An image file: the `Icon` key, resolved through the icon theme. - pub picture: Option, -} - type Cache = Mutex)>>; fn cache() -> &'static Cache { diff --git a/crates/tunal/src/folder/mod.rs b/crates/tunal/src/folder/mod.rs index 331b5e2..1e04fc6 100644 --- a/crates/tunal/src/folder/mod.rs +++ b/crates/tunal/src/folder/mod.rs @@ -4,6 +4,7 @@ pub mod appearance; pub mod format; +pub mod item; mod load; pub mod look; mod motion; @@ -28,58 +29,111 @@ use crate::{ DetailRow, FolderState, FolderStatus, GridRow, Grouping, MenuEntry, MenuItem, NavKey, SortColumn, Travel, ViewMode, }; +pub use item::Item; pub use load::Deliver; use load::{Change, Msg}; -use look::Look; use motion::{Anim, Clock, Key, Old}; use previews::{Arrival, Previews, Viewport}; -pub use sort::{GroupBy, Settings, SortKey}; +pub use sort::{GroupBy, Order, SortKey}; -/// One entry with what sorting and drawing need, computed once. -pub struct Item { - pub entry: Entry, - /// Lower-cased display name, for natural order and the filter. - pub key: String, - pub is_dir: bool, - pub category: Category, - /// The folder's own icon and name from its `.directory`. - pub look: Option, +/// Icon size of the grid, smallest first; `FolderState.zoom` is its index. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub enum Zoom { + Small, + #[default] + Medium, + Large, + ExtraLarge, } -impl Item { - fn new(entry: Entry) -> Self { - Self { - key: entry.display_name.to_lowercase(), - is_dir: entry.kind.is_dir(), - category: Category::of(&entry), - entry, - look: None, +impl Zoom { + pub const ALL: [Zoom; 4] = [Self::Small, Self::Medium, Self::Large, Self::ExtraLarge]; + + /// `by` sizes larger (negative: smaller), stopping at either end. + pub fn step(self, by: i32) -> Self { + let i = (self as i32 + by).clamp(0, Self::ALL.len() as i32 - 1); + Self::ALL[i as usize] + } + + /// The word a folder's `.directory` keeps (`IconSize`). + fn word(self) -> &'static str { + match self { + Self::Small => "small", + Self::Medium => "medium", + Self::Large => "large", + Self::ExtraLarge => "extra-large", } } - fn with_look(mut self, look: Option) -> Self { - self.set_look(look); - self + fn from_word(word: &str) -> Option { + Self::ALL.into_iter().find(|z| z.word() == word) + } +} + +/// A pane's view settings. Sort, grouping and hidden files carry over between folders; view +/// mode and icon size go back to the defaults in a folder that keeps none of its own. +#[derive(Clone, Debug)] +pub struct Settings { + pub key: SortKey, + pub descending: bool, + pub folders_first: bool, + pub group: GroupBy, + pub show_hidden: bool, + /// Thumbnails in place of plain glyphs. + pub previews: bool, + pub filter: String, + /// Details or grid. + pub mode: ViewMode, + /// Icon size of the grid. + pub zoom: Zoom, +} + +impl Settings { + /// A new pane's settings: the defaults chosen in the settings dialog. + pub fn from_config(cfg: &config::Settings) -> Self { + let (mode, zoom) = default_view(cfg); + Self { + key: match cfg.sort { + config::Sort::Name => SortKey::Name, + config::Sort::Size => SortKey::Size, + config::Sort::Type => SortKey::Type, + config::Sort::Modified => SortKey::Modified, + }, + descending: cfg.sort_descending, + folders_first: cfg.folders_first, + group: match cfg.group { + config::Group::None => GroupBy::None, + config::Group::Type => GroupBy::Type, + config::Group::Modified => GroupBy::Modified, + config::Group::Size => GroupBy::Size, + }, + show_hidden: cfg.show_hidden, + previews: cfg.previews, + filter: String::new(), + mode, + zoom, + } } - /// Sets the look; a display name from it sorts and filters in place of the real name. - fn set_look(&mut self, look: Option) { - let name = look.as_ref().and_then(|l| l.name.as_deref()); - self.key = name.unwrap_or(&self.entry.display_name).to_lowercase(); - self.look = look; + pub fn order(&self) -> Order { + Order { + key: self.key, + descending: self.descending, + folders_first: self.folders_first, + } } +} - /// The name shown for the entry. - fn shown_name(&self) -> &str { - self.look - .as_ref() - .and_then(|l| l.name.as_deref()) - .unwrap_or(&self.entry.display_name) +/// The view mode and icon size of a folder that keeps none of its own. +fn default_view(cfg: &config::Settings) -> (ViewMode, Zoom) { + // `TUNAL_VIEW=grid` starts in the grid view (screenshots). + if std::env::var("TUNAL_VIEW").as_deref() == Ok("grid") { + return (ViewMode::Grid, Zoom::Medium); } - - /// Folders count as empty for size order. - pub fn size(&self) -> u64 { - if self.is_dir { 0 } else { self.entry.size } + match cfg.view { + config::View::Details => (ViewMode::Details, Zoom::Medium), + config::View::Grid => (ViewMode::Grid, Zoom::Medium), + config::View::Large => (ViewMode::Grid, Zoom::Large), } } @@ -138,6 +192,8 @@ struct State { previews: Previews, /// `previews` as the settings dialog last had it: a change of it applies to the pane. cfg_previews: bool, + /// The default view mode and icon size: what a folder without its own shows. + default_view: (ViewMode, Zoom), } struct Pending { @@ -193,6 +249,7 @@ impl FolderModel { pending: None, previews: Previews::default(), cfg_previews: cfg.previews, + default_view: default_view(&cfg), }), notify: ModelNotify::default(), locations, @@ -246,7 +303,8 @@ impl FolderModel { } /// Starts a new generation for `dir`: messages for the previous folder are dropped. - /// Sort, grouping and hidden files carry over; filter, playlist and selection don't. + /// Sort, grouping and hidden files carry over; filter, playlist and selection don't, and + /// view mode and icon size go back to the defaults until the folder's own arrive. fn reset_to(&self, dir: Uri) -> u64 { let mut s = self.state.borrow_mut(); s.generation += 1; @@ -266,6 +324,7 @@ impl FolderModel { s.settings = base; } s.remembered = false; + (s.settings.mode, s.settings.zoom) = s.default_view; s.settings.filter.clear(); s.playlist_path = None; if let SortKey::Playlist(_) = s.settings.key { @@ -530,6 +589,8 @@ impl FolderModel { items: shown as i32, hidden: hidden as i32, mode: s.settings.mode, + zoom: s.settings.zoom as i32, + columns: s.columns as i32, previews: s.settings.previews, motion: s.epoch, travel: s.travel, @@ -560,12 +621,36 @@ impl FolderModel { } pub fn set_mode(&self, mode: ViewMode) { + let zoom = self.state.borrow().settings.zoom; + self.set_view(mode, zoom); + } + + /// Icon size `by` steps larger (negative: smaller), or back to the default size with 0; + /// the grid only (the details rows keep their size). + pub fn zoom(&self, by: i32) { + let zoom = { + let s = self.state.borrow(); + if s.settings.mode != ViewMode::Grid { + return; + } + if by == 0 { + s.default_view.1 + } else { + s.settings.zoom.step(by) + } + }; + self.set_view(ViewMode::Grid, zoom); + } + + /// Grid view with icons of `zoom` (the View mode menu). + pub fn set_view(&self, mode: ViewMode, zoom: Zoom) { let ghosts = { let mut s = self.state.borrow_mut(); - if s.settings.mode == mode { + if (s.settings.mode, s.settings.zoom) == (mode, zoom) { return; } s.settings.mode = mode; + s.settings.zoom = zoom; self.write_view(&s); still(&mut s) }; @@ -576,6 +661,11 @@ impl FolderModel { self.pump(); } + pub fn mode(&self) -> (ViewMode, Zoom) { + let s = self.state.borrow(); + (s.settings.mode, s.settings.zoom) + } + /// The grid view's column count changed: the lines reflow at once. pub fn set_columns(&self, columns: usize) { let ghosts = { @@ -731,8 +821,17 @@ impl FolderModel { self.set_filter(&filter); } - /// The settings dialog changed what applies to panes already open. + /// The settings dialog changed what applies to panes already open: a new default view + /// shows at once in a folder that keeps none of its own. pub fn apply_config(&self, cfg: &config::Settings) { + let view = default_view(cfg); + let follow = { + let mut s = self.state.borrow_mut(); + std::mem::replace(&mut s.default_view, view) != view && !s.remembered + }; + if follow { + self.set_view(view.0, view.1); + } let (hidden, folders_first) = (cfg.show_hidden, cfg.folders_first); let current = { let s = self.state.borrow(); @@ -895,23 +994,26 @@ impl FolderModel { self.apply(Msg::Thumbs); } - /// The sort menu, built from the current settings and the folder's playlists. + /// The sort menu, built from the current settings and the folder's playlists: keys, + /// direction, then folders first, grouping and hidden files. pub fn sort_menu(&self) -> Vec { let s = self.state.borrow(); let set = &s.settings; - let check = |on: bool| SharedString::from(if on { "\u{e5ca}" } else { "" }); + let check = |on: bool| SharedString::from(if on { CHECK } else { "" }); let item = |text: &str, on: bool, shortcut: &str| MenuItem { text: text.into(), icon: check(on), shortcut: shortcut.into(), ..Default::default() }; - let mut menu = vec![ - item("Name", set.key == SortKey::Name, ""), - item("Size", set.key == SortKey::Size, ""), - item("Type", set.key == SortKey::Type, ""), - item("Modified", set.key == SortKey::Modified, ""), - ]; + let separator = || MenuItem { + separator: true, + ..Default::default() + }; + let mut menu: Vec = SORT_KEYS + .iter() + .map(|(text, key)| item(text, set.key == *key, "")) + .collect(); let several = s.playlists.len() > 1; for (i, p) in s.playlists.iter().enumerate() { let text = if several { @@ -921,13 +1023,7 @@ impl FolderModel { }; menu.push(item(&text, set.key == SortKey::Playlist(i), "")); } - let groups = [ - ("None", GroupBy::None), - ("Type", GroupBy::Type), - ("Modified", GroupBy::Modified), - ("Size", GroupBy::Size), - ]; - let submenu: Vec = groups + let submenu: Vec = GROUPS .iter() .map(|(text, g)| MenuEntry { text: (*text).into(), @@ -936,67 +1032,111 @@ impl FolderModel { }) .collect(); menu.extend([ - MenuItem { - separator: true, - ..Default::default() - }, + separator(), + item("Ascending", !set.descending, ""), item("Descending", set.descending, ""), + separator(), item("Folders first", set.folders_first, ""), MenuItem { text: "Group by".into(), submenu: ModelRc::new(VecModel::from(submenu)), ..Default::default() }, - MenuItem { - separator: true, - ..Default::default() - }, + separator(), item("Show hidden files", set.show_hidden, "Ctrl+H"), ]); menu } + /// The sort menu as a submenu (the context menu's Sort by): keys, direction and folders + /// first; the indices are the sort menu's. + pub fn sort_submenu(&self) -> Vec { + let menu = self.sort_menu(); + // Leaves out Group by (a submenu of its own), its separator and Show hidden files. + let end = menu.len() - 3; + menu.into_iter() + .take(end) + .map(|m| MenuEntry { + text: m.text, + icon: m.icon, + shortcut: m.shortcut, + disabled: m.disabled, + separator: m.separator, + }) + .collect() + } + /// Runs the sort menu item at `index` (`sub` in its submenu, else -1). pub fn sort_menu_activated(&self, index: usize, sub: i32) { let playlists = self.state.borrow().playlists.len(); - let keys = [ - SortKey::Name, - SortKey::Size, - SortKey::Type, - SortKey::Modified, - ]; match index { - i if i < 4 => { + i if i < SORT_KEYS.len() => { self.state.borrow_mut().playlist_path = None; - self.update(|s| s.key = keys[i]); + self.update(|s| s.key = SORT_KEYS[i].1); } - i if i < 4 + playlists => { + i if i < SORT_KEYS.len() + playlists => { + let p = i - SORT_KEYS.len(); { let mut s = self.state.borrow_mut(); - s.playlist_path = Some(s.playlists[i - 4].path.clone()); + s.playlist_path = Some(s.playlists[p].path.clone()); } - self.update(|s| s.key = SortKey::Playlist(i - 4)); + self.update(|s| s.key = SortKey::Playlist(p)); } - i => match i - 4 - playlists { - 1 => self.update(|s| s.descending = !s.descending), - 2 => self.update(|s| s.folders_first = !s.folders_first), - 3 => { - let group = [ - GroupBy::None, - GroupBy::Type, - GroupBy::Modified, - GroupBy::Size, - ]; - if let Some(g) = usize::try_from(sub).ok().and_then(|i| group.get(i)) { + i => match i - SORT_KEYS.len() - playlists { + 1 => self.update(|s| s.descending = false), + 2 => self.update(|s| s.descending = true), + 4 => self.update(|s| s.folders_first = !s.folders_first), + 5 => { + if let Some((_, g)) = usize::try_from(sub).ok().and_then(|i| GROUPS.get(i)) { self.update(|s| s.group = *g); } } - 5 => self.toggle_hidden(), + 7 => self.toggle_hidden(), _ => {} }, } } + /// The View mode submenu: details, icons, then the icon sizes (checked in the grid). + pub fn view_menu(&self) -> Vec { + let (mode, zoom) = self.mode(); + let grid = mode == ViewMode::Grid; + let check = |on: bool| SharedString::from(if on { CHECK } else { "" }); + let entry = |text: &str, on: bool| MenuEntry { + text: text.into(), + icon: check(on), + ..Default::default() + }; + let mut menu = vec![ + entry("Details", !grid), + entry("Icons", grid), + MenuEntry { + separator: true, + ..Default::default() + }, + ]; + menu.extend( + ZOOM_NAMES + .iter() + .zip(Zoom::ALL) + .map(|(text, z)| entry(text, grid && zoom == z)), + ); + menu + } + + pub fn view_menu_activated(&self, index: usize) { + let zoom = self.mode().1; + match index { + 0 => self.set_view(ViewMode::Details, zoom), + 1 => self.set_view(ViewMode::Grid, zoom), + i => { + if let Some(z) = i.checked_sub(3).and_then(|i| Zoom::ALL.get(i)) { + self.set_view(ViewMode::Grid, *z); + } + } + } + } + // ---- selection ------------------------------------------------------------------- /// Changes selection and cursor, then repaints only the rows whose look changed. @@ -1437,6 +1577,7 @@ fn rebuild(s: &mut State) { SortKey::Playlist(i) => s.playlists.get(i).map(|p| &p.ranks), _ => None, }; + let order = settings.order(); let calendar = sort::Calendar::now(); let groups: Vec<(u8, &'static str)> = s .items @@ -1447,7 +1588,7 @@ fn rebuild(s: &mut State) { groups[a] .0 .cmp(&groups[b].0) - .then_with(|| sort::compare(&s.items[a], &s.items[b], settings, ranks)) + .then_with(|| sort::compare(&s.items[a], &s.items[b], &order, ranks)) }); let mut rows = Vec::with_capacity(shown.len() + 8); @@ -1516,6 +1657,9 @@ fn apply_view(s: &mut State, view: &tunal_dirmeta::View) { Some("grid") => set.mode = ViewMode::Grid, _ => {} } + if let Some(zoom) = view.zoom.as_deref().and_then(Zoom::from_word) { + set.zoom = zoom; + } match view.sort.as_deref() { Some("name") => set.key = SortKey::Name, Some("size") => set.key = SortKey::Size, @@ -1589,6 +1733,7 @@ fn view_of(s: &State) -> tunal_dirmeta::View { ), hidden: Some(set.show_hidden), previews: Some(set.previews), + zoom: Some(set.zoom.word().into()), playlist, } } @@ -1651,6 +1796,32 @@ fn visible_rows(s: &State) -> Vec<(usize, bool)> { } } +/// The check mark of a chosen menu item (Material `check`). +const CHECK: &str = "\u{e5ca}"; + +/// The sort keys in menu order. +const SORT_KEYS: [(&str, SortKey); 4] = [ + ("Name", SortKey::Name), + ("Size", SortKey::Size), + ("Modified", SortKey::Modified), + ("Type", SortKey::Type), +]; + +const GROUPS: [(&str, GroupBy); 4] = [ + ("None", GroupBy::None), + ("Type", GroupBy::Type), + ("Modified", GroupBy::Modified), + ("Size", GroupBy::Size), +]; + +/// Names of the icon sizes, by `Zoom`. +const ZOOM_NAMES: [&str; 4] = [ + "Small icons", + "Medium icons", + "Large icons", + "Extra large icons", +]; + /// Rows beyond the view that get a (low priority) thumbnail request, details view. const PREFETCH_ROWS: i64 = 6; diff --git a/crates/tunal/src/folder/sort.rs b/crates/tunal/src/folder/sort.rs index dc5d124..a1056c9 100644 --- a/crates/tunal/src/folder/sort.rs +++ b/crates/tunal/src/folder/sort.rs @@ -1,13 +1,12 @@ -//! Row order: natural name compare, sort keys, group buckets. +//! Row order: natural name compare, sort keys, group buckets. Pure: the integration tests +//! (`tests/sort.rs`) use it through the library target. use std::cmp::Ordering; use std::time::{Duration, SystemTime, UNIX_EPOCH}; use tunal_core::playlist::Ranks; -use super::Item; -use crate::ViewMode; -use crate::config; +use super::item::Item; use crate::icons::Category; #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -28,48 +27,12 @@ pub enum GroupBy { Size, } -#[derive(Clone, Debug)] -pub struct Settings { +/// How entries are ordered: the part of a pane's view settings that sorting reads. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Order { pub key: SortKey, pub descending: bool, pub folders_first: bool, - pub group: GroupBy, - pub show_hidden: bool, - /// Thumbnails in place of plain glyphs. - pub previews: bool, - pub filter: String, - /// Details or grid; per pane like the rest, carried over between folders. - pub mode: ViewMode, -} - -impl Settings { - /// A new pane's settings: the defaults chosen in the settings dialog. - pub fn from_config(cfg: &config::Settings) -> Self { - Self { - key: match cfg.sort { - config::Sort::Name => SortKey::Name, - config::Sort::Size => SortKey::Size, - config::Sort::Type => SortKey::Type, - config::Sort::Modified => SortKey::Modified, - }, - descending: cfg.sort_descending, - folders_first: cfg.folders_first, - group: match cfg.group { - config::Group::None => GroupBy::None, - config::Group::Type => GroupBy::Type, - config::Group::Modified => GroupBy::Modified, - config::Group::Size => GroupBy::Size, - }, - show_hidden: cfg.show_hidden, - previews: cfg.previews, - filter: String::new(), - // `TUNAL_VIEW=grid` starts in the grid view (screenshots). - mode: match (std::env::var("TUNAL_VIEW").as_deref(), cfg.view) { - (Ok("grid"), _) | (_, config::View::Grid) => ViewMode::Grid, - _ => ViewMode::Details, - }, - } - } } /// Compares lower-cased names with digit runs as numbers: `file2` < `file10`. @@ -117,7 +80,7 @@ pub fn name_cmp(a: &Item, b: &Item) -> Ordering { } /// Order of two items under `settings` (without groups). `ranks` is the playlist in use. -pub fn compare(a: &Item, b: &Item, settings: &Settings, ranks: Option<&Ranks>) -> Ordering { +pub fn compare(a: &Item, b: &Item, settings: &Order, ranks: Option<&Ranks>) -> Ordering { if let SortKey::Playlist(_) = settings.key { let rank = |i: &Item| ranks.and_then(|r| r.rank(&i.entry.name, i.is_dir)); // Listed first in playlist order, the rest by name; descending reverses it all. diff --git a/crates/tunal/src/menus.rs b/crates/tunal/src/menus.rs index 5a2c08f..cbe6f7a 100644 --- a/crates/tunal/src/menus.rs +++ b/crates/tunal/src/menus.rs @@ -74,6 +74,9 @@ impl Context { "trash", "delete", "-", + "sort-by", + "view-mode", + "-", "appearance", "properties", ], @@ -94,6 +97,9 @@ impl Context { "paste", "new-folder", "-", + "sort-by", + "view-mode", + "-", "copy-path", "-", "appearance", diff --git a/crates/tunal/src/settings.rs b/crates/tunal/src/settings.rs index a9de3eb..71f9690 100644 --- a/crates/tunal/src/settings.rs +++ b/crates/tunal/src/settings.rs @@ -103,7 +103,7 @@ impl SettingsUi { cfg.restore_session = data.restore_session; cfg.show_hidden = data.show_hidden; cfg.folders_first = data.folders_first; - cfg.view = [View::Details, View::Grid][data.view.clamp(0, 1) as usize]; + cfg.view = [View::Details, View::Grid, View::Large][data.view.clamp(0, 2) as usize]; cfg.sort = [Sort::Name, Sort::Size, Sort::Type, Sort::Modified][data.sort.clamp(0, 3) as usize]; cfg.sort_descending = data.sort_descending; diff --git a/crates/tunal/src/shell/mod.rs b/crates/tunal/src/shell/mod.rs index 98a6a26..1425804 100644 --- a/crates/tunal/src/shell/mod.rs +++ b/crates/tunal/src/shell/mod.rs @@ -929,6 +929,7 @@ impl Shell { .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_zoom, |_s, p, by| p.model.zoom(by.signum())); on_pane!(on_clear_selection, |_s, p| p.model.clear_selection()); let me = self.me.clone(); ui.on_navigate(move |i, key, shift, page| { @@ -968,40 +969,69 @@ impl Shell { self.bind_mouse_buttons(ui); } - /// Mouse back and forward buttons go to the pane under the pointer. Slint's own touch - /// areas take every button press, so these are caught before Slint sees them. + /// Mouse back and forward buttons go to the pane under the pointer, and Ctrl with the + /// wheel zooms its grid. Slint's own touch areas take every button press (and its lists + /// every wheel turn), so these are caught before Slint sees them. fn bind_mouse_buttons(&self, ui: &MainWindow) { - use slint::winit_030::winit::event::{ElementState, MouseButton, WindowEvent}; + use slint::winit_030::winit::event::{ + ElementState, MouseButton, MouseScrollDelta, WindowEvent, + }; use slint::winit_030::{EventResult, WinitWindowAccessor}; let me = self.me.clone(); let weak = ui.as_weak(); + let ctrl = Cell::new(false); + // Wheel turns not yet worth a zoom step (touchpads send small pixel deltas). + let turned = Cell::new(0.0f64); ui.window().on_winit_window_event(move |_, event| { - let WindowEvent::MouseInput { - state: ElementState::Pressed, - button: button @ (MouseButton::Back | MouseButton::Forward), - .. - } = event - else { - return EventResult::Propagate; - }; let (Some(shell), Some(ui)) = (me.upgrade(), weak.upgrade()) else { return EventResult::Propagate; }; let hovered = ui.get_hovered_pane(); - if hovered >= 0 { - shell.set_active_pane(hovered as usize); + match event { + WindowEvent::ModifiersChanged(m) => { + ctrl.set(m.state().control_key()); + EventResult::Propagate + } + WindowEvent::MouseWheel { delta, .. } if ctrl.get() && hovered >= 0 => { + let lines = match delta { + MouseScrollDelta::LineDelta(_, y) => f64::from(*y), + MouseScrollDelta::PixelDelta(p) => p.y / WHEEL_STEP_PX, + }; + let total = turned.get() + lines; + let steps = total.trunc(); + turned.set(total - steps); + if steps != 0.0 + && let Some(pane) = shell.pane_at(hovered as usize) + { + pane.model.zoom(steps.signum() as i32); + } + EventResult::PreventDefault + } + WindowEvent::MouseInput { + state: ElementState::Pressed, + button: button @ (MouseButton::Back | MouseButton::Forward), + .. + } => { + if hovered >= 0 { + shell.set_active_pane(hovered as usize); + } + if *button == MouseButton::Back { + shell.with_active(|p| p.back()); + } else { + shell.with_active(|p| p.forward()); + } + EventResult::PreventDefault + } + _ => EventResult::Propagate, } - if *button == MouseButton::Back { - shell.with_active(|p| p.back()); - } else { - shell.with_active(|p| p.forward()); - } - EventResult::PreventDefault }); } } +/// Touchpad scroll distance (logical pixels) that makes one zoom step with Ctrl held. +const WHEEL_STEP_PX: f64 = 60.0; + /// `12 items`, `3 of 12 selected (4.2 MiB)`, plus hidden files left out. fn status(pane: &Pane) -> String { let state = pane.model.state(); diff --git a/crates/tunal/ui/app.slint b/crates/tunal/ui/app.slint index 10cecc6..28e3c01 100644 --- a/crates/tunal/ui/app.slint +++ b/crates/tunal/ui/app.slint @@ -85,6 +85,8 @@ export component MainWindow inherits Window { callback grid-columns-changed(int, int); callback motion-settled(int); callback view-mode-selected(int, ViewMode); + // Grid icon size of a pane: one size larger (1), smaller (-1) or the default (0). + callback zoom(int, int); in property <[MenuItem]> sort-menu; callback sort-menu-activated(int, int); @@ -96,7 +98,7 @@ export component MainWindow inherits Window { // A right click in pane `i` on row (-1: none) at window coordinates. callback context-menu-requested(int, int, length, length); callback actions-menu-requested(length, length); - callback context-menu-activated(int); + callback context-menu-activated(int, int); callback row-renamed(int, int, string); callback rename-cancelled(int); in property <[ToastData]> toasts; @@ -415,6 +417,18 @@ export component MainWindow inherits Window { root.add-bookmark(); return accept; } + if !toolbar.editing && (event.text == "+" || event.text == "=") { + root.zoom(root.active-pane, 1); + return accept; + } + if !toolbar.editing && (event.text == "-" || event.text == "_") { + root.zoom(root.active-pane, -1); + return accept; + } + if !toolbar.editing && event.text == "0" { + root.zoom(root.active-pane, 0); + return accept; + } } if event.modifiers.alt && !event.modifiers.control && !toolbar.editing { if event.text == Key.LeftArrow { @@ -524,6 +538,9 @@ export component MainWindow inherits Window { previews-toggled => { root.toggle-previews(root.active-pane); } + sort-menu(x, y) => { + sort-popup.open-at(x, y); + } back => { root.back(); } @@ -712,7 +729,7 @@ export component MainWindow inherits Window { items: root.context-menu-items; leading-icons: true; activated(index, sub) => { - root.context-menu-activated(index); + root.context-menu-activated(index, sub); } closed => { root.focus-pane(); diff --git a/crates/tunal/ui/dialogs/settings.slint b/crates/tunal/ui/dialogs/settings.slint index 9bf7634..cf7f9a1 100644 --- a/crates/tunal/ui/dialogs/settings.slint +++ b/crates/tunal/ui/dialogs/settings.slint @@ -317,7 +317,7 @@ export component SettingsDialog inherits Dialog { ChoiceSetting { title: "Default view"; - names: ["Details", "Icons"]; + names: ["Details", "Icons", "Large icons"]; current: root.data.view; picked(i) => { root.data.view = i; diff --git a/crates/tunal/ui/shell/toolbar.slint b/crates/tunal/ui/shell/toolbar.slint index adc11ba..2693af4 100644 --- a/crates/tunal/ui/shell/toolbar.slint +++ b/crates/tunal/ui/shell/toolbar.slint @@ -6,7 +6,7 @@ import { ViewSwitch } from "../views/view-switch.slint"; import { Breadcrumb, Crumb, Completion } from "breadcrumb.slint"; // Navigation for the active pane: sidebar toggle, back, forward, up, the breadcrumb, the -// view mode and the split view toggle. +// sort menu, the view mode and the split view toggle. export component Toolbar inherits Rectangle { in property can-back; in property can-forward; @@ -31,6 +31,8 @@ export component Toolbar inherits Rectangle { callback toggle-split(); callback view-mode-selected(ViewMode); callback previews-toggled(); + // The sort button was pressed; window coordinates of its bottom right. + callback sort-menu(length, length); callback toggle-sidebar(); callback go(int); callback path-edited(string); @@ -136,6 +138,18 @@ export component Toolbar inherits Rectangle { } } + VerticalLayout { + alignment: center; + IconButton { + icon: Icons.sort; + tonal: true; + tooltip: "Sort"; + clicked => { + root.sort-menu(self.absolute-position.x + self.width, self.absolute-position.y + self.height); + } + } + } + VerticalLayout { alignment: center; ViewSwitch { diff --git a/crates/tunal/ui/theme.slint b/crates/tunal/ui/theme.slint index f83323b..b90cad6 100644 --- a/crates/tunal/ui/theme.slint +++ b/crates/tunal/ui/theme.slint @@ -103,7 +103,7 @@ export global Theme { // Below this window height dialogs drop their cookie; below this width the toolbar // drops its secondary buttons. out property dialog-cookie-min-height: 560px; - out property toolbar-compact-width: 640px; + out property toolbar-compact-width: 688px; // Keyboard focus ring, drawn outside the control's shape. out property focus-ring-width: 2px; diff --git a/crates/tunal/ui/views/details-view.slint b/crates/tunal/ui/views/details-view.slint index 2e2b6ce..f3def79 100644 --- a/crates/tunal/ui/views/details-view.slint +++ b/crates/tunal/ui/views/details-view.slint @@ -70,6 +70,11 @@ export struct FolderState { items: int, hidden: int, mode: ViewMode, + // Icon size of the grid: 0 small, 1 medium, 2 large, 3 extra large. + zoom: int, + // Grid columns the folder model lays its lines out for; the grid view reports its own + // count again when they differ (a pane shown in a reused view, a new tab). + columns: int, // Thumbnails instead of plain glyphs in this folder. previews: bool, // Bumped when a list change starts that rows animate (see DetailRow). diff --git a/crates/tunal/ui/views/grid-view.slint b/crates/tunal/ui/views/grid-view.slint index 80c6de1..bf645e3 100644 --- a/crates/tunal/ui/views/grid-view.slint +++ b/crates/tunal/ui/views/grid-view.slint @@ -24,15 +24,29 @@ export struct GridRow { headers-before: int, } +// Cell geometry per icon size (`FolderState.zoom`: small, medium, large, extra large). global Grid { - out property min-cell-width: 112px; - out property cell-height: 116px; - out property icon-size: 56px; - // With previews the picture slot grows to hold the thumbnail tile, and the cell with it. - out property preview-size: 80px; - out property preview-cell-height: cell-height + preview-size - icon-size; + out property <[length]> icon-sizes: [40px, 56px, 80px, 112px]; + out property <[length]> preview-sizes: [56px, 80px, 112px, 160px]; + out property <[length]> min-cell-widths: [88px, 112px, 144px, 192px]; + // The name's two lines and the cell's padding, under the picture slot. + out property label-height: 60px; // Gap around each cell's shape, inside its slot. out property gap: Theme.space-1; + + public pure function icon-size(zoom: int) -> length { + root.icon-sizes[clamp(zoom, 0, 3)] + } + // With previews the picture slot grows to hold the thumbnail tile, and the cell with it. + public pure function slot(zoom: int, previews: bool) -> length { + previews ? root.preview-sizes[clamp(zoom, 0, 3)] : root.icon-size(zoom) + } + public pure function cell-height(zoom: int, previews: bool) -> length { + root.slot(zoom, previews) + root.label-height + } + public pure function min-cell-width(zoom: int) -> length { + root.min-cell-widths[clamp(zoom, 0, 3)] + } } // One entry of the grid: a rounded card with a shaped glyph over a two-line name. Selection grows a tonal fill like a @@ -41,8 +55,9 @@ component GridCell inherits StateLayer { in property entry; in property cursor-ring; in property edit-select; - // The picture slot: `Grid.icon-size`, or `Grid.preview-size` with previews. - in property slot: Grid.icon-size; + // The picture slot: `Grid.slot(...)`; the shape inside it is `icon`. + in property slot: Grid.icon-size(1); + in property icon: Grid.icon-size(1); callback renamed(text: string); callback rename-cancelled(); @@ -93,7 +108,7 @@ component GridCell inherits StateLayer { Shape { x: (parent.width - self.width) / 2; y: (parent.height - self.height) / 2; - size: Grid.icon-size; + size: root.icon; shape: FileShapes.shape(root.entry.shape-glyph); active-shape: FileShapes.active-shape(root.entry.shape-glyph); active: root.entry.selected; @@ -176,10 +191,10 @@ export component GridView inherits Rectangle { // What is in view (top, height) and the row and cell heights, for the previews. callback viewport-changed(length, length, length, length); - out property columns: max(1, floor((list.visible-width - 2 * Columns.inset) / Grid.min-cell-width)); + out property columns: max(1, floor((list.visible-width - 2 * Columns.inset) / Grid.min-cell-width(root.state.zoom))); property cell-width: (list.visible-width - 2 * Columns.inset) / root.columns; property settle: Motion.settle(root.motion); - property cell-height: root.state.previews ? Grid.preview-cell-height : Grid.cell-height; + property cell-height: Grid.cell-height(root.state.zoom, root.state.previews); // Vertical offset of a moving row from its slot (see DetailRow). function shift-y(row: DetailRow) -> length { @@ -190,6 +205,14 @@ export component GridView inherits Rectangle { changed columns => { root.columns-changed(root.columns); } + // The pane shown here can change under the view (another tab, a new pane in a reused + // view): its model may still lay out lines for another width. + property stale-columns: root.state.open && root.state.columns != root.columns; + changed stale-columns => { + if root.stale-columns { + root.columns-changed(root.columns); + } + } property scrolled: -list.content-y; property shown-height: list.visible-height; @@ -399,7 +422,8 @@ export component GridView inherits Rectangle { tile := GridCell { entry: cell.entry; - slot: root.state.previews ? Grid.preview-size : Grid.icon-size; + slot: Grid.slot(root.state.zoom, root.state.previews); + icon: Grid.icon-size(root.state.zoom); cursor-ring: cell.entry.cursor && keys.has-focus; edit-select: root.state.edit-select; opacity: cell.entry.dimmed ? Theme.disabled-content : 1;