1use crate::{
9 AnyElement, App, AvailableSpace, Bounds, ContentMask, Element, ElementId, Entity,
10 GlobalElementId, Hitbox, InspectorElementId, InteractiveElement, Interactivity, IntoElement,
11 IsZero, LayoutId, ListSizingBehavior, Overflow, Pixels, Point, ScrollHandle, Size,
12 StyleRefinement, Styled, Window, point, px, size,
13};
14use smallvec::SmallVec;
15use std::{cell::RefCell, cmp, ops::Range, rc::Rc};
16
17use super::ListHorizontalSizingBehavior;
18
19#[track_caller]
23pub fn uniform_list<R>(
24 id: impl Into<ElementId>,
25 item_count: usize,
26 f: impl 'static + Fn(Range<usize>, &mut Window, &mut App) -> Vec<R>,
27) -> UniformList
28where
29 R: IntoElement,
30{
31 let id = id.into();
32 let mut base_style = StyleRefinement::default();
33 base_style.overflow.y = Some(Overflow::Scroll);
34
35 let render_range = move |range: Range<usize>, window: &mut Window, cx: &mut App| {
36 f(range, window, cx)
37 .into_iter()
38 .map(|component| component.into_any_element())
39 .collect()
40 };
41
42 UniformList {
43 item_count,
44 item_to_measure_index: 0,
45 render_items: Box::new(render_range),
46 decorations: Vec::new(),
47 interactivity: Interactivity {
48 element_id: Some(id),
49 base_style: Box::new(base_style),
50 ..Interactivity::new()
51 },
52 scroll_handle: None,
53 sizing_behavior: ListSizingBehavior::default(),
54 horizontal_sizing_behavior: ListHorizontalSizingBehavior::default(),
55 }
56}
57
58pub struct UniformList {
60 item_count: usize,
61 item_to_measure_index: usize,
62 render_items: Box<
63 dyn for<'a> Fn(Range<usize>, &'a mut Window, &'a mut App) -> SmallVec<[AnyElement; 64]>,
64 >,
65 decorations: Vec<Box<dyn UniformListDecoration>>,
66 interactivity: Interactivity,
67 scroll_handle: Option<UniformListScrollHandle>,
68 sizing_behavior: ListSizingBehavior,
69 horizontal_sizing_behavior: ListHorizontalSizingBehavior,
70}
71
72pub struct UniformListFrameState {
74 items: SmallVec<[AnyElement; 32]>,
75 decorations: SmallVec<[AnyElement; 2]>,
76}
77
78#[derive(Clone, Debug, Default)]
81pub struct UniformListScrollHandle(pub Rc<RefCell<UniformListScrollState>>);
82
83#[derive(Clone, Copy, Debug, PartialEq, Eq)]
85pub enum ScrollStrategy {
86 Top,
88 Center,
92 Bottom,
96 Nearest,
100}
101
102#[derive(Clone, Copy, Debug)]
103#[allow(missing_docs)]
104pub struct DeferredScrollToItem {
105 pub item_index: usize,
107 pub strategy: ScrollStrategy,
109 pub offset: usize,
111 pub scroll_strict: bool,
112}
113
114#[derive(Clone, Debug, Default)]
115#[allow(missing_docs)]
116pub struct UniformListScrollState {
117 pub base_handle: ScrollHandle,
118 pub deferred_scroll_to_item: Option<DeferredScrollToItem>,
119 pub last_item_size: Option<ItemSize>,
121 pub y_flipped: bool,
123}
124
125#[derive(Copy, Clone, Debug, Default)]
126pub struct ItemSize {
128 pub item: Size<Pixels>,
130 pub contents: Size<Pixels>,
133}
134
135impl UniformListScrollHandle {
136 pub fn new() -> Self {
138 Self(Rc::new(RefCell::new(UniformListScrollState {
139 base_handle: ScrollHandle::new(),
140 deferred_scroll_to_item: None,
141 last_item_size: None,
142 y_flipped: false,
143 })))
144 }
145
146 pub fn scroll_to_item(&self, ix: usize, strategy: ScrollStrategy) {
152 crate::fast::dependencies::scroll_handle_changed(&self.0.borrow().base_handle);
153 self.0.borrow_mut().deferred_scroll_to_item = Some(DeferredScrollToItem {
154 item_index: ix,
155 strategy,
156 offset: 0,
157 scroll_strict: false,
158 });
159 }
160
161 pub fn scroll_to_item_strict(&self, ix: usize, strategy: ScrollStrategy) {
166 crate::fast::dependencies::scroll_handle_changed(&self.0.borrow().base_handle);
167 self.0.borrow_mut().deferred_scroll_to_item = Some(DeferredScrollToItem {
168 item_index: ix,
169 strategy,
170 offset: 0,
171 scroll_strict: true,
172 });
173 }
174
175 pub fn scroll_to_item_with_offset(&self, ix: usize, strategy: ScrollStrategy, offset: usize) {
186 crate::fast::dependencies::scroll_handle_changed(&self.0.borrow().base_handle);
187 self.0.borrow_mut().deferred_scroll_to_item = Some(DeferredScrollToItem {
188 item_index: ix,
189 strategy,
190 offset,
191 scroll_strict: false,
192 });
193 }
194
195 pub fn scroll_to_item_strict_with_offset(
206 &self,
207 ix: usize,
208 strategy: ScrollStrategy,
209 offset: usize,
210 ) {
211 crate::fast::dependencies::scroll_handle_changed(&self.0.borrow().base_handle);
212 self.0.borrow_mut().deferred_scroll_to_item = Some(DeferredScrollToItem {
213 item_index: ix,
214 strategy,
215 offset,
216 scroll_strict: true,
217 });
218 }
219
220 pub fn y_flipped(&self) -> bool {
222 self.0.borrow().y_flipped
223 }
224
225 #[cfg(any(test, feature = "test-support"))]
227 pub fn logical_scroll_top_index(&self) -> usize {
228 let this = self.0.borrow();
229 this.deferred_scroll_to_item
230 .as_ref()
231 .map(|deferred| deferred.item_index)
232 .unwrap_or_else(|| this.base_handle.logical_scroll_top().0)
233 }
234
235 pub fn is_scrollable(&self) -> bool {
237 if let Some(size) = self.0.borrow().last_item_size {
238 size.contents.height > size.item.height
239 } else {
240 false
241 }
242 }
243
244 pub fn is_scrolled_to_end(&self) -> Option<bool> {
247 let state = self.0.borrow();
248 let max_offset = state.base_handle.max_offset();
249 if max_offset.y <= px(0.) {
250 return None;
251 }
252 let offset = state.base_handle.offset();
253 Some(-offset.y >= max_offset.y)
254 }
255
256 pub fn scroll_to_bottom(&self) {
258 self.scroll_to_item(usize::MAX, ScrollStrategy::Bottom);
259 }
260}
261
262impl Styled for UniformList {
263 fn style(&mut self) -> &mut StyleRefinement {
264 &mut self.interactivity.base_style
265 }
266}
267
268impl Element for UniformList {
269 type RequestLayoutState = UniformListFrameState;
270 type PrepaintState = Option<Hitbox>;
271
272 fn id(&self) -> Option<ElementId> {
273 self.interactivity.element_id.clone()
274 }
275
276 fn source_location(&self) -> Option<&'static core::panic::Location<'static>> {
277 None
278 }
279
280 fn request_layout(
281 &mut self,
282 global_id: Option<&GlobalElementId>,
283 inspector_id: Option<&InspectorElementId>,
284 window: &mut Window,
285 cx: &mut App,
286 ) -> (LayoutId, Self::RequestLayoutState) {
287 let max_items = self.item_count;
288 let item_size = self.measure_item(None, window, cx);
289 let layout_id = self.interactivity.request_layout(
290 global_id,
291 inspector_id,
292 window,
293 cx,
294 |style, window, cx| match self.sizing_behavior {
295 ListSizingBehavior::Infer => {
296 window.with_text_style(style.text_style().cloned(), |window| {
297 window.request_measured_layout(
298 style,
299 move |known_dimensions, available_space, _window, _cx| {
300 let desired_height = item_size.height * max_items;
301 let width = known_dimensions.width.unwrap_or(match available_space
302 .width
303 {
304 AvailableSpace::Definite(x) => x,
305 AvailableSpace::MinContent | AvailableSpace::MaxContent => {
306 item_size.width
307 }
308 });
309 let height = match available_space.height {
310 AvailableSpace::Definite(height) => desired_height.min(height),
311 AvailableSpace::MinContent | AvailableSpace::MaxContent => {
312 desired_height
313 }
314 };
315 size(width, height)
316 },
317 )
318 })
319 }
320 ListSizingBehavior::Auto => window
321 .with_text_style(style.text_style().cloned(), |window| {
322 window.request_layout(style, None, cx)
323 }),
324 },
325 );
326
327 (
328 layout_id,
329 UniformListFrameState {
330 items: SmallVec::new(),
331 decorations: SmallVec::new(),
332 },
333 )
334 }
335
336 fn prepaint(
337 &mut self,
338 global_id: Option<&GlobalElementId>,
339 inspector_id: Option<&InspectorElementId>,
340 bounds: Bounds<Pixels>,
341 frame_state: &mut Self::RequestLayoutState,
342 window: &mut Window,
343 cx: &mut App,
344 ) -> Option<Hitbox> {
345 let style = self
346 .interactivity
347 .compute_style(global_id, None, window, cx);
348 let border = style.border_widths.to_pixels(window.rem_size());
349 let padding = style
350 .padding
351 .to_pixels(bounds.size.into(), window.rem_size());
352
353 let padded_bounds = Bounds::from_corners(
354 bounds.origin + point(border.left + padding.left, border.top + padding.top),
355 bounds.bottom_right()
356 - point(border.right + padding.right, border.bottom + padding.bottom),
357 );
358
359 let can_scroll_horizontally = matches!(
360 self.horizontal_sizing_behavior,
361 ListHorizontalSizingBehavior::Unconstrained
362 );
363
364 let longest_item_size =
365 crate::fast::layers::lists::measure_item(window, cx, global_id, |window, cx| {
366 self.measure_item(None, window, cx)
367 });
368 let content_width = if can_scroll_horizontally {
369 padded_bounds.size.width.max(longest_item_size.width)
370 } else {
371 padded_bounds.size.width
372 };
373 let content_size = Size {
374 width: content_width,
375 height: longest_item_size.height * self.item_count,
376 };
377
378 let shared_scroll_offset = self.interactivity.scroll_offset.clone().unwrap();
379 let item_height = longest_item_size.height;
380 let shared_scroll_to_item = self.scroll_handle.as_mut().and_then(|handle| {
381 let mut handle = handle.0.borrow_mut();
382 handle.last_item_size = Some(ItemSize {
383 item: padded_bounds.size,
384 contents: content_size,
385 });
386 handle.deferred_scroll_to_item.take()
387 });
388
389 self.interactivity.prepaint(
390 global_id,
391 inspector_id,
392 bounds,
393 content_size,
394 window,
395 cx,
396 |_style, mut scroll_offset, hitbox, window, cx| {
397 let y_flipped = if let Some(scroll_handle) = &self.scroll_handle {
398 let scroll_state = scroll_handle.0.borrow();
399 scroll_state.y_flipped
400 } else {
401 false
402 };
403
404 if self.item_count > 0 {
405 let content_height = item_height * self.item_count;
406
407 let is_scrolled_vertically = !scroll_offset.y.is_zero();
408 let max_scroll_offset = padded_bounds.size.height - content_height;
409
410 if is_scrolled_vertically && scroll_offset.y < max_scroll_offset {
411 shared_scroll_offset.borrow_mut().y = max_scroll_offset;
412 scroll_offset.y = max_scroll_offset;
413 }
414
415 let content_width = content_size.width + padding.left + padding.right;
416 let is_scrolled_horizontally =
417 can_scroll_horizontally && !scroll_offset.x.is_zero();
418 if is_scrolled_horizontally && content_width <= padded_bounds.size.width {
419 shared_scroll_offset.borrow_mut().x = Pixels::ZERO;
420 scroll_offset.x = Pixels::ZERO;
421 }
422
423 if let Some(DeferredScrollToItem {
424 mut item_index,
425 mut strategy,
426 offset,
427 scroll_strict,
428 }) = shared_scroll_to_item
429 {
430 if y_flipped {
431 item_index = self.item_count.saturating_sub(item_index + 1);
432 }
433 let list_height = padded_bounds.size.height;
434 let mut updated_scroll_offset = shared_scroll_offset.borrow_mut();
435 let item_top = item_height * item_index;
436 let item_bottom = item_top + item_height;
437 let scroll_top = -updated_scroll_offset.y;
438 let offset_pixels = item_height * offset;
439
440 let is_above = item_top < scroll_top + offset_pixels;
442 let is_below = item_bottom > scroll_top + list_height;
443
444 if scroll_strict || is_above || is_below {
445 if strategy == ScrollStrategy::Nearest {
446 if is_above {
447 strategy = ScrollStrategy::Top;
448 } else if is_below {
449 strategy = ScrollStrategy::Bottom;
450 }
451 }
452
453 let max_scroll_offset =
454 (content_height - list_height).max(Pixels::ZERO);
455 match strategy {
456 ScrollStrategy::Top => {
457 updated_scroll_offset.y = -(item_top - offset_pixels)
458 .clamp(Pixels::ZERO, max_scroll_offset);
459 }
460 ScrollStrategy::Center => {
461 let item_center = item_top + item_height / 2.0;
462
463 let viewport_height = list_height - offset_pixels;
464 let viewport_center = offset_pixels + viewport_height / 2.0;
465 let target_scroll_top = item_center - viewport_center;
466 updated_scroll_offset.y =
467 -target_scroll_top.clamp(Pixels::ZERO, max_scroll_offset);
468 }
469 ScrollStrategy::Bottom => {
470 updated_scroll_offset.y = -(item_bottom - list_height)
471 .clamp(Pixels::ZERO, max_scroll_offset);
472 }
473 ScrollStrategy::Nearest => {
474 }
476 }
477 }
478 scroll_offset = *updated_scroll_offset
479 }
480
481 let scroll_offset =
482 crate::fast::layers::lists::snap_item_offset(window, scroll_offset);
483 let first_visible_element_ix =
484 (-(scroll_offset.y + padding.top) / item_height).floor() as usize;
485 let last_visible_element_ix = ((-scroll_offset.y + padded_bounds.size.height)
486 / item_height)
487 .ceil() as usize;
488
489 let visible_range = first_visible_element_ix
490 ..cmp::min(last_visible_element_ix, self.item_count);
491 let fast_rows = crate::fast::layers::lists::begin_uniform_list(
492 window,
493 cx,
494 global_id,
495 padded_bounds,
496 scroll_offset,
497 item_height,
498 self.item_count,
499 &visible_range,
500 y_flipped,
501 );
502
503 let items = if y_flipped {
504 let flipped_range = self.item_count.saturating_sub(visible_range.end)
505 ..self.item_count.saturating_sub(visible_range.start);
506 let mut items = (self.render_items)(flipped_range, window, cx);
507 items.reverse();
508 items
509 } else {
510 crate::fast::layers::lists::render_rows(
511 &fast_rows,
512 visible_range.clone(),
513 |range| (self.render_items)(range, window, cx),
514 )
515 };
516
517 let fast_indices =
518 crate::fast::layers::lists::row_indices(&fast_rows, visible_range.clone());
519 let content_mask = ContentMask { bounds };
520 window.with_content_mask(Some(content_mask), |window| {
521 for (mut item, ix) in items.into_iter().zip(fast_indices) {
522 let item_origin = padded_bounds.origin
523 + scroll_offset
524 + point(Pixels::ZERO, item_height * ix);
525
526 let available_width = if can_scroll_horizontally {
527 padded_bounds.size.width + scroll_offset.x.abs()
528 } else {
529 padded_bounds.size.width
530 };
531 let available_space = size(
532 AvailableSpace::Definite(available_width),
533 AvailableSpace::Definite(item_height),
534 );
535 crate::fast::layout_key::layout_as_list_item(
536 &mut item,
537 ix,
538 available_space,
539 window,
540 cx,
541 );
542 crate::fast::layers::lists::prepaint_row(
543 window,
544 cx,
545 ix,
546 |window, cx| item.prepaint_at(item_origin, window, cx),
547 );
548 frame_state.items.push(item);
549 }
550 crate::fast::layers::lists::end_rows(window, cx, fast_rows);
551
552 let bounds =
553 Bounds::new(padded_bounds.origin + scroll_offset, padded_bounds.size);
554 for decoration in &self.decorations {
555 let mut decoration = decoration.as_ref().compute(
556 visible_range.clone(),
557 bounds,
558 scroll_offset,
559 item_height,
560 self.item_count,
561 window,
562 cx,
563 );
564 let available_space = size(
565 AvailableSpace::Definite(bounds.size.width),
566 AvailableSpace::Definite(bounds.size.height),
567 );
568 decoration.layout_as_root(available_space, window, cx);
569 decoration.prepaint_at(bounds.origin, window, cx);
570 frame_state.decorations.push(decoration);
571 }
572 });
573 }
574
575 hitbox
576 },
577 )
578 }
579
580 fn paint(
581 &mut self,
582 global_id: Option<&GlobalElementId>,
583 inspector_id: Option<&InspectorElementId>,
584 bounds: Bounds<crate::Pixels>,
585 request_layout: &mut Self::RequestLayoutState,
586 hitbox: &mut Option<Hitbox>,
587 window: &mut Window,
588 cx: &mut App,
589 ) {
590 self.interactivity.paint(
591 global_id,
592 inspector_id,
593 bounds,
594 hitbox.as_ref(),
595 window,
596 cx,
597 |_, window, cx| {
598 crate::fast::layers::lists::begin_paint_rows(window, cx, global_id);
599 for item in &mut request_layout.items {
600 crate::fast::layers::lists::paint_row(window, cx, None, |window, cx| {
601 item.paint(window, cx)
602 });
603 }
604 crate::fast::layers::lists::end_paint_rows(window, cx, global_id);
605 for decoration in &mut request_layout.decorations {
606 decoration.paint(window, cx);
607 }
608 },
609 )
610 }
611}
612
613impl IntoElement for UniformList {
614 type Element = Self;
615
616 fn into_element(self) -> Self::Element {
617 self
618 }
619}
620
621pub trait UniformListDecoration {
624 fn compute(
627 &self,
628 visible_range: Range<usize>,
629 bounds: Bounds<Pixels>,
630 scroll_offset: Point<Pixels>,
631 item_height: Pixels,
632 item_count: usize,
633 window: &mut Window,
634 cx: &mut App,
635 ) -> AnyElement;
636}
637
638impl<T: UniformListDecoration + 'static> UniformListDecoration for Entity<T> {
639 fn compute(
640 &self,
641 visible_range: Range<usize>,
642 bounds: Bounds<Pixels>,
643 scroll_offset: Point<Pixels>,
644 item_height: Pixels,
645 item_count: usize,
646 window: &mut Window,
647 cx: &mut App,
648 ) -> AnyElement {
649 self.update(cx, |inner, cx| {
650 inner.compute(
651 visible_range,
652 bounds,
653 scroll_offset,
654 item_height,
655 item_count,
656 window,
657 cx,
658 )
659 })
660 }
661}
662
663impl UniformList {
664 pub fn with_width_from_item(mut self, item_index: Option<usize>) -> Self {
666 self.item_to_measure_index = item_index.unwrap_or(0);
667 self
668 }
669
670 pub fn with_sizing_behavior(mut self, behavior: ListSizingBehavior) -> Self {
672 self.sizing_behavior = behavior;
673 self
674 }
675
676 pub fn with_horizontal_sizing_behavior(
680 mut self,
681 behavior: ListHorizontalSizingBehavior,
682 ) -> Self {
683 self.horizontal_sizing_behavior = behavior;
684 match behavior {
685 ListHorizontalSizingBehavior::FitList => {
686 self.interactivity.base_style.overflow.x = None;
687 }
688 ListHorizontalSizingBehavior::Unconstrained => {
689 self.interactivity.base_style.overflow.x = Some(Overflow::Scroll);
690 }
691 }
692 self
693 }
694
695 pub fn with_decoration(mut self, decoration: impl UniformListDecoration + 'static) -> Self {
697 self.decorations.push(Box::new(decoration));
698 self
699 }
700
701 fn measure_item(
702 &self,
703 list_width: Option<Pixels>,
704 window: &mut Window,
705 cx: &mut App,
706 ) -> Size<Pixels> {
707 if self.item_count == 0 {
708 return Size::default();
709 }
710
711 let item_ix = cmp::min(self.item_to_measure_index, self.item_count - 1);
712 let mut items = (self.render_items)(item_ix..item_ix + 1, window, cx);
713 let Some(mut item_to_measure) = items.pop() else {
714 return Size::default();
715 };
716 let available_space = size(
717 list_width.map_or(AvailableSpace::MaxContent, |width| {
718 AvailableSpace::Definite(width)
719 }),
720 AvailableSpace::MinContent,
721 );
722 item_to_measure.layout_as_root(available_space, window, cx)
723 }
724
725 pub fn track_scroll(mut self, handle: &UniformListScrollHandle) -> Self {
727 self.interactivity.tracked_scroll_handle = Some(handle.0.borrow().base_handle.clone());
728 self.scroll_handle = Some(handle.clone());
729 self
730 }
731
732 pub fn y_flipped(mut self, y_flipped: bool) -> Self {
734 if let Some(ref scroll_handle) = self.scroll_handle {
735 let mut scroll_state = scroll_handle.0.borrow_mut();
736 let mut base_handle = &scroll_state.base_handle;
737 let offset = base_handle.offset();
738 match scroll_state.last_item_size {
739 Some(last_size) if scroll_state.y_flipped != y_flipped => {
740 let new_y_offset =
741 -(offset.y + last_size.contents.height - last_size.item.height);
742 base_handle.set_offset(point(offset.x, new_y_offset));
743 scroll_state.y_flipped = y_flipped;
744 }
745 None if y_flipped => {
747 base_handle.set_offset(point(offset.x, Pixels::MIN));
748 scroll_state.y_flipped = y_flipped;
749 }
750 _ => {}
751 }
752 }
753 self
754 }
755}
756
757impl InteractiveElement for UniformList {
758 fn interactivity(&mut self) -> &mut crate::Interactivity {
759 &mut self.interactivity
760 }
761}
762
763#[cfg(test)]
764mod test {
765 use crate::TestAppContext;
766
767 #[gpui::test]
768 fn test_scroll_strategy_nearest(cx: &mut TestAppContext) {
769 use crate::{
770 Context, FocusHandle, ScrollStrategy, UniformListScrollHandle, Window, div, prelude::*,
771 px, uniform_list,
772 };
773 use std::ops::Range;
774
775 actions!(example, [SelectNext, SelectPrev]);
776
777 struct TestView {
778 index: usize,
779 length: usize,
780 scroll_handle: UniformListScrollHandle,
781 focus_handle: FocusHandle,
782 visible_range: Range<usize>,
783 }
784
785 impl TestView {
786 pub fn select_next(
787 &mut self,
788 _: &SelectNext,
789 window: &mut Window,
790 _: &mut Context<Self>,
791 ) {
792 if self.index + 1 == self.length {
793 self.index = 0
794 } else {
795 self.index += 1;
796 }
797 self.scroll_handle
798 .scroll_to_item(self.index, ScrollStrategy::Nearest);
799 window.refresh();
800 }
801
802 pub fn select_previous(
803 &mut self,
804 _: &SelectPrev,
805 window: &mut Window,
806 _: &mut Context<Self>,
807 ) {
808 if self.index == 0 {
809 self.index = self.length - 1
810 } else {
811 self.index -= 1;
812 }
813 self.scroll_handle
814 .scroll_to_item(self.index, ScrollStrategy::Nearest);
815 window.refresh();
816 }
817 }
818
819 impl Render for TestView {
820 fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
821 div()
822 .id("list-example")
823 .track_focus(&self.focus_handle)
824 .on_action(cx.listener(Self::select_next))
825 .on_action(cx.listener(Self::select_previous))
826 .size_full()
827 .child(
828 uniform_list(
829 "entries",
830 self.length,
831 cx.processor(|this, range: Range<usize>, _window, _cx| {
832 this.visible_range = range.clone();
833 range
834 .map(|ix| div().id(ix).h(px(20.0)).child(format!("Item {ix}")))
835 .collect()
836 }),
837 )
838 .track_scroll(&self.scroll_handle)
839 .h(px(200.0)),
840 )
841 }
842 }
843
844 let (view, cx) = cx.add_window_view(|window, cx| {
845 let focus_handle = cx.focus_handle();
846 window.focus(&focus_handle, cx);
847 TestView {
848 scroll_handle: UniformListScrollHandle::new(),
849 index: 0,
850 focus_handle,
851 length: 47,
852 visible_range: 0..0,
853 }
854 });
855
856 for ix in 1..10 {
860 cx.dispatch_action(SelectNext);
861 view.read_with(cx, |view, _| {
862 assert_eq!(view.index, ix);
863 assert_eq!(view.visible_range, 0..10);
864 })
865 }
866
867 for ix in 10..47 {
869 cx.dispatch_action(SelectNext);
870 view.read_with(cx, |view, _| {
871 assert_eq!(view.index, ix);
872 assert_eq!(view.visible_range, ix - 9..ix + 1);
873 })
874 }
875
876 cx.dispatch_action(SelectNext);
878 view.read_with(cx, |view, _| {
879 assert_eq!(view.index, 0);
880 assert_eq!(view.visible_range, 0..10);
881 });
882
883 cx.dispatch_action(SelectPrev);
885 view.read_with(cx, |view, _| {
886 assert_eq!(view.index, 46);
887 assert_eq!(view.visible_range, 37..47);
888 });
889
890 for ix in (37..46).rev() {
892 cx.dispatch_action(SelectPrev);
893 view.read_with(cx, |view, _| {
894 assert_eq!(view.index, ix);
895 assert_eq!(view.visible_range, 37..47);
896 })
897 }
898
899 for ix in (0..37).rev() {
901 cx.dispatch_action(SelectPrev);
902 view.read_with(cx, |view, _| {
903 assert_eq!(view.index, ix);
904 assert_eq!(view.visible_range, ix..ix + 10);
905 })
906 }
907 }
908}