edith
Clone
https://git.tahinli.com/Tahinli/edith.git
0 watching·0 forks·Rust·18.3 MiB·ff99ad6·Tahinli·6 days ago·1055 commits
| 1 | //! The media library's bins and favourites (project format v51): named folders that hold |
| 2 | //! sources, and a star per source. Everything here is an edit of the project -- one undo step |
| 3 | //! each -- and a label only: no picture or sound is rebuilt. |
| 4 | |
| 5 | use crate::*; |
| 6 | use engine::project::MAX_BIN_NAME_CHARS; |
| 7 | |
| 8 | impl Player { |
| 9 | /// The source a library row stands for, if it is one (a file's further audio streams are |
| 10 | /// listed before they are sources). |
| 11 | fn source_of_row(&self, path: &Path, stream: usize) -> Option<usize> { |
| 12 | self.session.as_ref()?.sources().iter().position(|s| s.path == path && s.audio_stream == stream) |
| 13 | } |
| 14 | |
| 15 | /// The rows an action on "the picked media" means: the multi-pick when there is one, else |
| 16 | /// the one row selected. |
| 17 | pub(crate) fn picked_assets(&self) -> Vec<(PathBuf, usize)> { |
| 18 | if self.asset_picks.is_empty() { |
| 19 | self.selected_asset.iter().cloned().collect() |
| 20 | } else { |
| 21 | self.asset_picks.clone() |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | /// Rows to act on when `row` is the one a gesture landed on: the whole multi-pick if the row |
| 26 | /// is in it, else the row alone. |
| 27 | pub(crate) fn rows_for(&self, row: (PathBuf, usize)) -> Vec<(PathBuf, usize)> { |
| 28 | if self.asset_picks.contains(&row) { self.asset_picks.clone() } else { vec![row] } |
| 29 | } |
| 30 | |
| 31 | fn library_editable(&mut self, cx: &mut Context<Self>) -> bool { |
| 32 | if self.session.is_none() { |
| 33 | return false; |
| 34 | } |
| 35 | if self.exporting().is_some() { |
| 36 | self.notify_user("The library cannot change while an export runs".into()); |
| 37 | cx.notify(); |
| 38 | return false; |
| 39 | } |
| 40 | true |
| 41 | } |
| 42 | |
| 43 | /// Makes a bin inside bin `parent` (`0`: at the top), holding `with` if any, named in place so |
| 44 | /// typing replaces "New bin". One undo step. |
| 45 | pub(crate) fn new_bin(&mut self, parent: u32, with: Vec<(PathBuf, usize)>, cx: &mut Context<Self>) { |
| 46 | if !self.library_editable(cx) { |
| 47 | return; |
| 48 | } |
| 49 | let Some(session) = self.session.as_mut() else { return }; |
| 50 | let taken = |name: &str, bins: &[engine::Bin]| bins.iter().any(|b| b.parent == parent && b.name.eq_ignore_ascii_case(name)); |
| 51 | let mut name = "New bin".to_string(); |
| 52 | let mut n = 1; |
| 53 | while taken(&name, session.bins()) { |
| 54 | n += 1; |
| 55 | name = format!("New bin {n}"); |
| 56 | } |
| 57 | let indexes: Vec<usize> = with |
| 58 | .iter() |
| 59 | .filter_map(|(path, stream)| session.sources().iter().position(|s| s.path == *path && s.audio_stream == *stream)) |
| 60 | .collect(); |
| 61 | session.begin_batch(); |
| 62 | let made = session.create_bin(&name, parent).and_then(|id| session.move_to_bin(&indexes, id).map(|_| id)); |
| 63 | session.end_batch(); |
| 64 | match made { |
| 65 | Ok(id) => { |
| 66 | self.mark_dirty(); |
| 67 | self.collapsed_bins.remove(&id); |
| 68 | self.collapsed_bins.remove(&parent); |
| 69 | self.selected_bin = Some(id); |
| 70 | self.bin_edit = Some((id, crate::player::text_field::TextField::selected(name))); |
| 71 | self.context_menu = None; |
| 72 | self.library_menu = None; |
| 73 | } |
| 74 | Err(e) => self.notify_user(format!("No new bin — {e}").into()), |
| 75 | } |
| 76 | cx.notify(); |
| 77 | } |
| 78 | |
| 79 | /// Starts retyping bin `id`'s name on its row. |
| 80 | pub(crate) fn begin_bin_edit(&mut self, id: u32, cx: &mut Context<Self>) { |
| 81 | if !self.library_editable(cx) { |
| 82 | return; |
| 83 | } |
| 84 | let name = self.session.as_ref().and_then(|s| s.bins().iter().find(|b| b.id == id)).map(|b| b.name.clone()); |
| 85 | if let Some(name) = name { |
| 86 | self.selected_bin = Some(id); |
| 87 | self.context_menu = None; |
| 88 | self.library_menu = None; |
| 89 | self.bin_edit = Some((id, crate::player::text_field::TextField::selected(name))); |
| 90 | } |
| 91 | cx.notify(); |
| 92 | } |
| 93 | |
| 94 | /// Writes what was typed as the bin's name (one undo step); an empty or taken name is said |
| 95 | /// out loud and the old one stays. |
| 96 | pub(crate) fn commit_bin_edit(&mut self, cx: &mut Context<Self>) { |
| 97 | let Some((id, field)) = self.bin_edit.take() else { return }; |
| 98 | if let Some(session) = self.session.as_mut() { |
| 99 | match session.rename_bin(id, &field.text) { |
| 100 | Ok(true) => self.mark_dirty(), |
| 101 | Ok(false) => {} |
| 102 | Err(e) => self.notify_user(format!("Not renamed — {e}").into()), |
| 103 | } |
| 104 | } |
| 105 | cx.notify(); |
| 106 | } |
| 107 | |
| 108 | /// The keyboard while a bin's name is being retyped: enter saves, escape gives up, the rest is text. |
| 109 | pub(crate) fn bin_edit_key(&mut self, key: &str, ctrl: bool, alt: bool, key_char: Option<&str>, cx: &mut Context<Self>) { |
| 110 | use crate::player::text_field::FieldKey; |
| 111 | let Some((_, field)) = self.bin_edit.as_mut() else { return }; |
| 112 | match field.key(key, ctrl, alt, key_char, MAX_BIN_NAME_CHARS) { |
| 113 | FieldKey::Cancel => self.bin_edit = None, |
| 114 | FieldKey::Commit => self.commit_bin_edit(cx), |
| 115 | FieldKey::Edited => {} |
| 116 | } |
| 117 | cx.notify(); |
| 118 | } |
| 119 | |
| 120 | /// Folds or opens a bin's rows; remembered while the project is open. |
| 121 | pub(crate) fn toggle_bin_open(&mut self, id: u32, cx: &mut Context<Self>) { |
| 122 | if !self.collapsed_bins.remove(&id) { |
| 123 | self.collapsed_bins.insert(id); |
| 124 | } |
| 125 | // Remembered with the project's view, like the viewer zoom. |
| 126 | self.save_view_state(); |
| 127 | cx.notify(); |
| 128 | } |
| 129 | |
| 130 | /// The New bin inside action: a bin in the bin picked in the list. Only a top-level bin |
| 131 | /// holds bins, so inside a nested one it makes a sibling. |
| 132 | pub(crate) fn new_bin_inside_selected(&mut self, cx: &mut Context<Self>) { |
| 133 | let parent = self |
| 134 | .selected_bin |
| 135 | .and_then(|id| self.session.as_ref()?.bins().iter().find(|b| b.id == id).map(|b| if b.parent == 0 { b.id } else { b.parent })); |
| 136 | match parent { |
| 137 | Some(parent) => self.new_bin(parent, Vec::new(), cx), |
| 138 | None => { |
| 139 | self.notify_user("No bin is selected".into()); |
| 140 | cx.notify(); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /// Opens a bin row's menu at the pointer; the bin is picked first, so the verbs act on it. |
| 146 | pub(crate) fn open_bin_menu(&mut self, id: u32, at: Point<Pixels>, cx: &mut Context<Self>) { |
| 147 | if self.modal() || self.session.is_none() { |
| 148 | return; |
| 149 | } |
| 150 | self.selected_bin = Some(id); |
| 151 | self.library_menu = None; |
| 152 | self.context_menu = Some(ContextMenu { |
| 153 | lane: Lane::V1, |
| 154 | on: MenuOn::Bin, |
| 155 | at, |
| 156 | details: false, |
| 157 | sub: Sub::Closed, |
| 158 | }); |
| 159 | cx.notify(); |
| 160 | } |
| 161 | |
| 162 | /// The Rename bin action: the bin picked in the list. |
| 163 | pub(crate) fn rename_selected_bin(&mut self, cx: &mut Context<Self>) { |
| 164 | match self.selected_bin { |
| 165 | Some(id) => self.begin_bin_edit(id, cx), |
| 166 | None => { |
| 167 | self.notify_user("No bin is selected".into()); |
| 168 | cx.notify(); |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /// The Remove bin action: the bin picked in the list. |
| 174 | pub(crate) fn remove_selected_bin(&mut self, cx: &mut Context<Self>) { |
| 175 | match self.selected_bin { |
| 176 | Some(id) => self.remove_bin(id, cx), |
| 177 | None => { |
| 178 | self.notify_user("No bin is selected".into()); |
| 179 | cx.notify(); |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /// Removes bin `id`; what it held stays in the library, one level up. One undo step. |
| 185 | pub(crate) fn remove_bin(&mut self, id: u32, cx: &mut Context<Self>) { |
| 186 | if !self.library_editable(cx) { |
| 187 | return; |
| 188 | } |
| 189 | let Some(session) = self.session.as_mut() else { return }; |
| 190 | let name = session.bins().iter().find(|b| b.id == id).map(|b| b.name.clone()).unwrap_or_default(); |
| 191 | match session.remove_bin(id) { |
| 192 | Ok(moved) => { |
| 193 | self.mark_dirty(); |
| 194 | if self.selected_bin == Some(id) { |
| 195 | self.selected_bin = None; |
| 196 | } |
| 197 | if self.bin_edit.as_ref().is_some_and(|(b, _)| *b == id) { |
| 198 | self.bin_edit = None; |
| 199 | } |
| 200 | self.context_menu = None; |
| 201 | let tail = match moved { |
| 202 | 0 => String::new(), |
| 203 | 1 => " — its file moved up".to_string(), |
| 204 | n => format!(" — its {n} files moved up"), |
| 205 | }; |
| 206 | self.notify_user(format!("Removed bin {name}{tail}").into()); |
| 207 | } |
| 208 | Err(e) => self.notify_user(format!("Bin not removed — {e}").into()), |
| 209 | } |
| 210 | cx.notify(); |
| 211 | } |
| 212 | |
| 213 | /// Files `rows` in bin `bin` (`0`: out of every bin). One undo step. |
| 214 | pub(crate) fn move_assets_to_bin(&mut self, rows: Vec<(PathBuf, usize)>, bin: u32, cx: &mut Context<Self>) { |
| 215 | if !self.library_editable(cx) { |
| 216 | return; |
| 217 | } |
| 218 | let indexes: Vec<usize> = rows.iter().filter_map(|(p, s)| self.source_of_row(p, *s)).collect(); |
| 219 | let Some(session) = self.session.as_mut() else { return }; |
| 220 | let name = session.bins().iter().find(|b| b.id == bin).map(|b| b.name.clone()); |
| 221 | match session.move_to_bin(&indexes, bin) { |
| 222 | Ok(0) => {} |
| 223 | Ok(n) => { |
| 224 | self.mark_dirty(); |
| 225 | self.collapsed_bins.remove(&bin); |
| 226 | let what = if n == 1 { "1 file".to_string() } else { format!("{n} files") }; |
| 227 | let msg = match name { |
| 228 | Some(name) => format!("Moved {what} to {name}"), |
| 229 | None => format!("Moved {what} out of its bin"), |
| 230 | }; |
| 231 | self.notify_user(msg.into()); |
| 232 | } |
| 233 | Err(e) => self.notify_user(format!("Not moved — {e}").into()), |
| 234 | } |
| 235 | cx.notify(); |
| 236 | } |
| 237 | |
| 238 | /// Stars `rows`, or takes the star off when every one has it. One undo step. |
| 239 | pub(crate) fn toggle_favourite_rows(&mut self, rows: Vec<(PathBuf, usize)>, cx: &mut Context<Self>) { |
| 240 | if !self.library_editable(cx) { |
| 241 | return; |
| 242 | } |
| 243 | let indexes: Vec<usize> = rows.iter().filter_map(|(p, s)| self.source_of_row(p, *s)).collect(); |
| 244 | let Some(session) = self.session.as_mut() else { return }; |
| 245 | if indexes.is_empty() { |
| 246 | return; |
| 247 | } |
| 248 | let all_starred = indexes.iter().all(|&i| session.sources().get(i).is_some_and(|s| s.fav)); |
| 249 | match session.set_favourite(&indexes, !all_starred) { |
| 250 | Ok(0) => {} |
| 251 | Ok(_) => self.mark_dirty(), |
| 252 | Err(e) => self.notify_user(format!("Not starred — {e}").into()), |
| 253 | } |
| 254 | cx.notify(); |
| 255 | } |
| 256 | |
| 257 | /// The star on one row (or on the whole multi-pick it is part of). |
| 258 | pub(crate) fn toggle_favourite_of(&mut self, path: PathBuf, stream: usize, cx: &mut Context<Self>) { |
| 259 | let rows = self.rows_for((path, stream)); |
| 260 | self.toggle_favourite_rows(rows, cx); |
| 261 | } |
| 262 | |
| 263 | /// The Favourite media action: the picked media. |
| 264 | pub(crate) fn toggle_favourite_picked(&mut self, cx: &mut Context<Self>) { |
| 265 | let rows = self.picked_assets(); |
| 266 | if rows.is_empty() { |
| 267 | self.notify_user("No media is selected".into()); |
| 268 | cx.notify(); |
| 269 | return; |
| 270 | } |
| 271 | self.toggle_favourite_rows(rows, cx); |
| 272 | } |
| 273 | |
| 274 | /// The list of bins to file the picked media in, hung at `at`. |
| 275 | pub(crate) fn bin_picker(&mut self, at: Point<Pixels>, cx: &mut Context<Self>) { |
| 276 | self.picker_under(Pick::Bin, at, cx); |
| 277 | } |
| 278 | |
| 279 | /// [`Self::bin_picker`] from the palette: at the dock, for the picked media. |
| 280 | pub(crate) fn bin_picker_here(&mut self, cx: &mut Context<Self>) { |
| 281 | if self.picked_assets().is_empty() { |
| 282 | self.notify_user("No media is selected".into()); |
| 283 | cx.notify(); |
| 284 | return; |
| 285 | } |
| 286 | self.bin_picker(point(px(24.), px(200.)), cx); |
| 287 | } |
| 288 | |
| 289 | /// The media list shows only the starred sources (`true`) or all of them. |
| 290 | pub(crate) fn show_favourites(&mut self, only: bool, cx: &mut Context<Self>) { |
| 291 | crate::ui::dock_stance::set_fav_only(only); |
| 292 | cx.notify(); |
| 293 | } |
| 294 | |
| 295 | /// The rows of the list `Pick::Bin` offers: "No bin", then every bin by its path, the one |
| 296 | /// the first picked row is in marked. |
| 297 | pub(crate) fn bin_choices(&self) -> Vec<ChoiceRow> { |
| 298 | let Some(session) = &self.session else { return Vec::new() }; |
| 299 | let here = self |
| 300 | .picked_assets() |
| 301 | .first() |
| 302 | .and_then(|(p, s)| session.sources().iter().find(|x| x.path == *p && x.audio_stream == *s)) |
| 303 | .map_or(0, |s| s.bin); |
| 304 | let bins = session.bins(); |
| 305 | let path_of = |id: u32| { |
| 306 | let b = bins.iter().find(|b| b.id == id); |
| 307 | match b.map(|b| b.parent) { |
| 308 | Some(p) if p != 0 => format!("{} / {}", bins.iter().find(|x| x.id == p).map_or("", |x| x.name.as_str()), b.map_or("", |b| b.name.as_str())), |
| 309 | _ => b.map(|b| b.name.clone()).unwrap_or_default(), |
| 310 | } |
| 311 | }; |
| 312 | let mut ordered: Vec<&engine::Bin> = bins.iter().filter(|b| b.parent == 0).collect(); |
| 313 | let mut out = Vec::new(); |
| 314 | out.push((Choice::Bin(0), "No bin".into(), "top level".into(), here == 0)); |
| 315 | for top in ordered.drain(..) { |
| 316 | out.push((Choice::Bin(top.id), top.name.clone().into(), "".into(), here == top.id)); |
| 317 | for child in bins.iter().filter(|b| b.parent == top.id) { |
| 318 | out.push((Choice::Bin(child.id), path_of(child.id).into(), "".into(), here == child.id)); |
| 319 | } |
| 320 | } |
| 321 | out |
| 322 | } |
| 323 | } |