| 1 | use std::rc::Rc; |
| 2 | |
| 3 | use gpui::{App, Pixels, Size}; |
| 4 | |
| 5 | use crate::IndexPath; |
| 6 | |
| 7 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 8 | pub(crate) enum RowEntry { |
| 9 | Entry(IndexPath), |
| 10 | SectionHeader(usize), |
| 11 | SectionFooter(usize), |
| 12 | } |
| 13 | |
| 14 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 15 | pub(crate) struct MeasuredEntrySize { |
| 16 | pub(crate) item_size: Size<Pixels>, |
| 17 | pub(crate) section_header_size: Size<Pixels>, |
| 18 | pub(crate) section_footer_size: Size<Pixels>, |
| 19 | } |
| 20 | |
| 21 | impl RowEntry { |
| 22 | #[inline] |
| 23 | #[allow(unused)] |
| 24 | pub(crate) fn is_section_header(&self) -> bool { |
| 25 | matches!(self, RowEntry::SectionHeader(_)) |
| 26 | } |
| 27 | |
| 28 | pub(crate) fn eq_index_path(&self, path: &IndexPath) -> bool { |
| 29 | match self { |
| 30 | RowEntry::Entry(index_path) => index_path == path, |
| 31 | RowEntry::SectionHeader(_) | RowEntry::SectionFooter(_) => false, |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | #[allow(unused)] |
| 36 | pub(crate) fn index(&self) -> IndexPath { |
| 37 | match self { |
| 38 | RowEntry::Entry(index_path) => *index_path, |
| 39 | RowEntry::SectionHeader(ix) => IndexPath::default().section(*ix), |
| 40 | RowEntry::SectionFooter(ix) => IndexPath::default().section(*ix), |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | #[inline] |
| 45 | #[allow(unused)] |
| 46 | pub(crate) fn is_section_footer(&self) -> bool { |
| 47 | matches!(self, RowEntry::SectionFooter(_)) |
| 48 | } |
| 49 | |
| 50 | #[inline] |
| 51 | pub(crate) fn is_entry(&self) -> bool { |
| 52 | matches!(self, RowEntry::Entry(_)) |
| 53 | } |
| 54 | |
| 55 | #[inline] |
| 56 | #[allow(unused)] |
| 57 | pub(crate) fn section_ix(&self) -> Option<usize> { |
| 58 | match self { |
| 59 | RowEntry::SectionHeader(ix) | RowEntry::SectionFooter(ix) => Some(*ix), |
| 60 | _ => None, |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | #[derive(Default, Clone)] |
| 66 | pub(crate) struct RowsCache { |
| 67 | /// Only have section's that have rows. |
| 68 | pub(crate) entities: Rc<Vec<RowEntry>>, |
| 69 | pub(crate) items_count: usize, |
| 70 | /// The sections, the item is number of rows in each section. |
| 71 | pub(crate) sections: Rc<Vec<usize>>, |
| 72 | pub(crate) entries_sizes: Rc<Vec<Size<Pixels>>>, |
| 73 | measured_size: MeasuredEntrySize, |
| 74 | } |
| 75 | |
| 76 | impl RowsCache { |
| 77 | pub(crate) fn get(&self, flatten_ix: usize) -> Option<RowEntry> { |
| 78 | self.entities.get(flatten_ix).cloned() |
| 79 | } |
| 80 | |
| 81 | /// Returns the number of flattened rows (Includes header, item, footer). |
| 82 | pub(crate) fn len(&self) -> usize { |
| 83 | self.entities.len() |
| 84 | } |
| 85 | |
| 86 | /// Return the number of items in the cache. |
| 87 | pub(crate) fn items_count(&self) -> usize { |
| 88 | self.items_count |
| 89 | } |
| 90 | |
| 91 | /// Returns the index of the Entry with given path in the flattened rows. |
| 92 | pub(crate) fn position_of(&self, path: &IndexPath) -> Option<usize> { |
| 93 | self.entities |
| 94 | .iter() |
| 95 | .position(|p| p.is_entry() && p.eq_index_path(path)) |
| 96 | } |
| 97 | |
| 98 | /// Returns the flattened position of the first item row, skipping section |
| 99 | /// headers and footers. |
| 100 | pub(crate) fn first_entry_position(&self) -> Option<usize> { |
| 101 | self.entities.iter().position(RowEntry::is_entry) |
| 102 | } |
| 103 | |
| 104 | /// Return prev row, if the row is the first in the first section, goes to the last row. |
| 105 | /// |
| 106 | /// Empty rows section are skipped. |
| 107 | pub(crate) fn prev(&self, path: Option<IndexPath>) -> IndexPath { |
| 108 | let path = path.unwrap_or_default(); |
| 109 | let Some(pos) = self.position_of(&path) else { |
| 110 | return self |
| 111 | .entities |
| 112 | .iter() |
| 113 | .rfind(|entry| entry.is_entry()) |
| 114 | .map(|entry| entry.index()) |
| 115 | .unwrap_or_default(); |
| 116 | }; |
| 117 | |
| 118 | if let Some(path) = self |
| 119 | .entities |
| 120 | .iter() |
| 121 | .take(pos) |
| 122 | .rev() |
| 123 | .find(|entry| entry.is_entry()) |
| 124 | .map(|entry| entry.index()) |
| 125 | { |
| 126 | path |
| 127 | } else { |
| 128 | self.entities |
| 129 | .iter() |
| 130 | .rfind(|entry| entry.is_entry()) |
| 131 | .map(|entry| entry.index()) |
| 132 | .unwrap_or_default() |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /// Returns the next row, if the row is the last in the last section, goes to the first row. |
| 137 | /// |
| 138 | /// Empty rows section are skipped. |
| 139 | pub(crate) fn next(&self, path: Option<IndexPath>) -> IndexPath { |
| 140 | let Some(mut path) = path else { |
| 141 | return IndexPath::default(); |
| 142 | }; |
| 143 | |
| 144 | let Some(pos) = self.position_of(&path) else { |
| 145 | return self |
| 146 | .entities |
| 147 | .iter() |
| 148 | .find(|entry| entry.is_entry()) |
| 149 | .map(|entry| entry.index()) |
| 150 | .unwrap_or_default(); |
| 151 | }; |
| 152 | |
| 153 | if let Some(next_path) = self |
| 154 | .entities |
| 155 | .iter() |
| 156 | .skip(pos + 1) |
| 157 | .find(|entry| entry.is_entry()) |
| 158 | .map(|entry| entry.index()) |
| 159 | { |
| 160 | path = next_path; |
| 161 | } else { |
| 162 | path = self |
| 163 | .entities |
| 164 | .iter() |
| 165 | .find(|entry| entry.is_entry()) |
| 166 | .map(|entry| entry.index()) |
| 167 | .unwrap_or_default() |
| 168 | } |
| 169 | |
| 170 | path |
| 171 | } |
| 172 | |
| 173 | pub(crate) fn prepare_if_needed<F>( |
| 174 | &mut self, |
| 175 | sections_count: usize, |
| 176 | measured_size: MeasuredEntrySize, |
| 177 | cx: &App, |
| 178 | rows_count_f: F, |
| 179 | ) where |
| 180 | F: Fn(usize, &App) -> usize, |
| 181 | { |
| 182 | let mut new_sections = vec![]; |
| 183 | for section_ix in 0..sections_count { |
| 184 | new_sections.push(rows_count_f(section_ix, cx)); |
| 185 | } |
| 186 | |
| 187 | let need_update = new_sections != *self.sections || self.measured_size != measured_size; |
| 188 | |
| 189 | if !need_update { |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | let mut entries_sizes = vec![]; |
| 194 | let mut total_items_count = 0; |
| 195 | self.measured_size = measured_size; |
| 196 | self.sections = Rc::new(new_sections); |
| 197 | self.entities = Rc::new( |
| 198 | self.sections |
| 199 | .iter() |
| 200 | .enumerate() |
| 201 | .flat_map(|(section, items_count)| { |
| 202 | total_items_count += items_count; |
| 203 | let mut children = vec![]; |
| 204 | if *items_count == 0 { |
| 205 | return children; |
| 206 | } |
| 207 | |
| 208 | children.push(RowEntry::SectionHeader(section)); |
| 209 | entries_sizes.push(measured_size.section_header_size); |
| 210 | for row in 0..*items_count { |
| 211 | children.push(RowEntry::Entry(IndexPath { |
| 212 | section, |
| 213 | row, |
| 214 | ..Default::default() |
| 215 | })); |
| 216 | entries_sizes.push(measured_size.item_size); |
| 217 | } |
| 218 | children.push(RowEntry::SectionFooter(section)); |
| 219 | entries_sizes.push(measured_size.section_footer_size); |
| 220 | children |
| 221 | }) |
| 222 | .collect(), |
| 223 | ); |
| 224 | self.entries_sizes = Rc::new(entries_sizes); |
| 225 | self.items_count = total_items_count; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | #[cfg(test)] |
| 230 | mod tests { |
| 231 | use std::rc::Rc; |
| 232 | |
| 233 | use crate::{ |
| 234 | IndexPath, |
| 235 | list::cache::{RowEntry, RowsCache}, |
| 236 | }; |
| 237 | |
| 238 | fn build_entities(sections: &[usize]) -> Vec<RowEntry> { |
| 239 | sections |
| 240 | .iter() |
| 241 | .enumerate() |
| 242 | .flat_map(|(section, items_count)| { |
| 243 | let mut children = vec![]; |
| 244 | if *items_count == 0 { |
| 245 | return children; |
| 246 | } |
| 247 | |
| 248 | children.push(RowEntry::SectionHeader(section)); |
| 249 | for row in 0..*items_count { |
| 250 | children.push(RowEntry::Entry(IndexPath { |
| 251 | section, |
| 252 | row, |
| 253 | ..Default::default() |
| 254 | })); |
| 255 | } |
| 256 | children.push(RowEntry::SectionFooter(section)); |
| 257 | children |
| 258 | }) |
| 259 | .collect() |
| 260 | } |
| 261 | |
| 262 | #[test] |
| 263 | fn first_entry_position_skips_section_headers() { |
| 264 | let mut row_cache = RowsCache::default(); |
| 265 | row_cache.entities = Rc::new(build_entities(&[2])); |
| 266 | |
| 267 | assert_eq!(row_cache.first_entry_position(), Some(1)); |
| 268 | } |
| 269 | |
| 270 | #[test] |
| 271 | fn test_prev_next() { |
| 272 | let mut row_cache = RowsCache::default(); |
| 273 | // section 0 |
| 274 | // row 0 |
| 275 | // row 1 |
| 276 | // section 1 |
| 277 | // row 0 |
| 278 | // row 1 |
| 279 | // row 2 |
| 280 | // row 3 |
| 281 | // section 2 |
| 282 | // row 0 |
| 283 | // row 1 |
| 284 | // row 2 |
| 285 | row_cache.sections = Rc::new(vec![2, 4, 3]); |
| 286 | row_cache.entities = Rc::new(build_entities(&[2, 4, 3])); |
| 287 | |
| 288 | assert_eq!( |
| 289 | row_cache.next(Some(IndexPath::new(0).section(0))), |
| 290 | IndexPath::new(1).section(0) |
| 291 | ); |
| 292 | assert_eq!( |
| 293 | row_cache.next(Some(IndexPath::new(1).section(0))), |
| 294 | IndexPath::new(0).section(1) |
| 295 | ); |
| 296 | assert_eq!( |
| 297 | row_cache.next(Some(IndexPath::new(0).section(1))), |
| 298 | IndexPath::new(1).section(1) |
| 299 | ); |
| 300 | assert_eq!( |
| 301 | row_cache.next(Some(IndexPath::new(3).section(1))), |
| 302 | IndexPath::new(0).section(2) |
| 303 | ); |
| 304 | assert_eq!( |
| 305 | row_cache.next(Some(IndexPath::new(0).section(2))), |
| 306 | IndexPath::new(1).section(2) |
| 307 | ); |
| 308 | assert_eq!( |
| 309 | row_cache.next(Some(IndexPath::new(1).section(2))), |
| 310 | IndexPath::new(2).section(2) |
| 311 | ); |
| 312 | assert_eq!( |
| 313 | row_cache.next(Some(IndexPath::new(2).section(2))), |
| 314 | IndexPath::new(0).section(0) |
| 315 | ); |
| 316 | |
| 317 | assert_eq!( |
| 318 | row_cache.prev(Some(IndexPath::new(0).section(0))), |
| 319 | IndexPath::new(2).section(2) |
| 320 | ); |
| 321 | assert_eq!( |
| 322 | row_cache.prev(Some(IndexPath::new(1).section(0))), |
| 323 | IndexPath::new(0).section(0) |
| 324 | ); |
| 325 | assert_eq!( |
| 326 | row_cache.prev(Some(IndexPath::new(0).section(1))), |
| 327 | IndexPath::new(1).section(0) |
| 328 | ); |
| 329 | assert_eq!( |
| 330 | row_cache.prev(Some(IndexPath::new(1).section(1))), |
| 331 | IndexPath::new(0).section(1) |
| 332 | ); |
| 333 | assert_eq!( |
| 334 | row_cache.prev(Some(IndexPath::new(3).section(1))), |
| 335 | IndexPath::new(2).section(1) |
| 336 | ); |
| 337 | assert_eq!( |
| 338 | row_cache.prev(Some(IndexPath::new(0).section(2))), |
| 339 | IndexPath::new(3).section(1) |
| 340 | ); |
| 341 | } |
| 342 | |
| 343 | #[test] |
| 344 | fn test_prev_next_with_empty_sections() { |
| 345 | let mut row_cache = RowsCache::default(); |
| 346 | // section 0: 2 items |
| 347 | // section 1: 0 items (empty, should be skipped) |
| 348 | // section 2: 3 items |
| 349 | // section 3: 0 items (empty, should be skipped) |
| 350 | // section 4: 1 item |
| 351 | row_cache.sections = Rc::new(vec![2, 0, 3, 0, 1]); |
| 352 | row_cache.entities = Rc::new(build_entities(&[2, 0, 3, 0, 1])); |
| 353 | |
| 354 | // Test next: should skip empty sections |
| 355 | assert_eq!( |
| 356 | row_cache.next(Some(IndexPath::new(0).section(0))), |
| 357 | IndexPath::new(1).section(0) |
| 358 | ); |
| 359 | assert_eq!( |
| 360 | row_cache.next(Some(IndexPath::new(1).section(0))), |
| 361 | IndexPath::new(0).section(2) // Skip section 1 (empty) |
| 362 | ); |
| 363 | assert_eq!( |
| 364 | row_cache.next(Some(IndexPath::new(0).section(2))), |
| 365 | IndexPath::new(1).section(2) |
| 366 | ); |
| 367 | assert_eq!( |
| 368 | row_cache.next(Some(IndexPath::new(2).section(2))), |
| 369 | IndexPath::new(0).section(4) // Skip section 3 (empty) |
| 370 | ); |
| 371 | assert_eq!( |
| 372 | row_cache.next(Some(IndexPath::new(0).section(4))), |
| 373 | IndexPath::new(0).section(0) // Wrap around to first item |
| 374 | ); |
| 375 | |
| 376 | // Test prev: should skip empty sections |
| 377 | assert_eq!( |
| 378 | row_cache.prev(Some(IndexPath::new(0).section(0))), |
| 379 | IndexPath::new(0).section(4) // Wrap around to last item, skip empty sections |
| 380 | ); |
| 381 | assert_eq!( |
| 382 | row_cache.prev(Some(IndexPath::new(0).section(2))), |
| 383 | IndexPath::new(1).section(0) // Skip section 1 (empty) |
| 384 | ); |
| 385 | assert_eq!( |
| 386 | row_cache.prev(Some(IndexPath::new(0).section(4))), |
| 387 | IndexPath::new(2).section(2) // Skip section 3 (empty) |
| 388 | ); |
| 389 | assert_eq!( |
| 390 | row_cache.prev(Some(IndexPath::new(1).section(2))), |
| 391 | IndexPath::new(0).section(2) |
| 392 | ); |
| 393 | assert_eq!( |
| 394 | row_cache.prev(Some(IndexPath::new(2).section(2))), |
| 395 | IndexPath::new(1).section(2) |
| 396 | ); |
| 397 | } |
| 398 | } |