zng/
scroll.rs

1#![cfg(feature = "scroll")]
2
3//! Scroll widgets, commands and properties.
4//!
5//! The [`Scroll!`](struct@Scroll) widget accepts a single child of any size, overflow is clipped and can be brought
6//! into view by scrolling, the widget also supports content zooming and panning. The
7//! [`mode`](struct@Scroll#method.mode) property can be used to dynamically change the [`ScrollMode`].
8//!
9//! ```
10//! # fn main() { }
11//! use zng::prelude::*;
12//!
13//! # fn demo() { let _ =
14//! Scroll! {
15//!     // ZOOM includes PAN that includes VERTICAL and HORIZONTAL
16//!     mode = zng::scroll::ScrollMode::ZOOM;
17//!     // mouse press and drag scrolls
18//!     mouse_pan = true;
19//!     
20//!     child = Image! {
21//!         // center_viewport uses the SCROLL service
22//!         img_loading_fn = wgt_fn!(|_| center_viewport(Text!("loading..")));
23//!         
24//!         // content is a large image
25//!         source = "https://upload.wikimedia.org/wikipedia/commons/e/ea/Van_Gogh_-_Starry_Night_-_Google_Art_Project.jpg";
26//!         img_limits = zng::image::ImageLimits::none();
27//!         img_downscale = zng::image::ImageDownscale::from(layout::Px(8000));
28//!     }
29//! }
30//! # ; }
31//!
32//! fn center_viewport(msg: impl UiNode) -> impl UiNode {
33//!     use zng::scroll::SCROLL;
34//!     Container! {
35//!         // center the message on the scroll viewport:
36//!         //
37//!         // the large images can take a moment to decode in debug builds, but the size
38//!         // is already known after read, so the "loading.." message ends-up off-screen
39//!         // because it is centered on the image.
40//!         layout::x = merge_var!(SCROLL.horizontal_offset(), SCROLL.zoom_scale(), |&h, &s| h.0.fct_l() - 1.vw() / s * h);
41//!         layout::y = merge_var!(SCROLL.vertical_offset(), SCROLL.zoom_scale(), |&v, &s| v.0.fct_l() - 1.vh() / s * v);
42//!         layout::scale = SCROLL.zoom_scale().map(|&fct| 1.fct() / fct);
43//!         layout::transform_origin = 0;
44//!         widget::auto_hide = false;
45//!         layout::max_size = (1.vw(), 1.vh());
46//!         
47//!         child_align = Align::CENTER;
48//!         child = msg;
49//!     }
50//! }
51//! ```
52//!
53//! The example above declares a scroll with zoom and mouse pan features enabled, is also makes use of the [`SCROLL`] service
54//! to implement the `center_viewport` widget that is place in the content, but transforms to always be in the viewport.
55//!
56//! The `SCROLL` service can be used to interact with the parent `Scroll!`, you can also use commands in [`cmd`] to
57//! control any `Scroll!` widget.
58//!
59//! # Full API
60//!
61//! See [`zng_wgt_scroll`] for the full widget API.
62
63pub use zng_wgt_scroll::{
64    LazyMode, SCROLL, Scroll, ScrollBarArgs, ScrollFrom, ScrollInfo, ScrollMode, ScrollUnitsMix, Scrollbar, ScrollbarFnMix,
65    SmoothScrolling, Thumb, WidgetInfoExt, alt_factor, auto_hide_extra, clip_to_viewport, define_viewport_unit, h_line_unit, h_page_unit,
66    h_scrollbar_fn, h_wheel_unit, lazy, line_units, max_zoom, min_zoom, mode, mouse_pan, overscroll_color, page_units,
67    scroll_to_focused_mode, scrollbar_fn, scrollbar_joiner_fn, smooth_scrolling, v_line_unit, v_page_unit, v_scrollbar_fn, v_wheel_unit,
68    wheel_units, zoom_origin, zoom_size_only, zoom_touch_origin, zoom_wheel_origin, zoom_wheel_unit,
69};
70
71/// Scrollbar thumb widget.
72pub mod thumb {
73    pub use zng_wgt_scroll::thumb::{Thumb, cross_length, offset, viewport_ratio};
74}
75
76/// Scroll widget.
77pub mod scrollbar {
78    pub use zng_wgt_scroll::scrollbar::{Orientation, SCROLLBAR, Scrollbar, orientation};
79}
80
81/// Scroll commands.
82pub mod cmd {
83    pub use zng_wgt_scroll::cmd::{
84        PAGE_DOWN_CMD, PAGE_LEFT_CMD, PAGE_RIGHT_CMD, PAGE_UP_CMD, SCROLL_DOWN_CMD, SCROLL_LEFT_CMD, SCROLL_RIGHT_CMD,
85        SCROLL_TO_BOTTOM_CMD, SCROLL_TO_CMD, SCROLL_TO_LEFTMOST_CMD, SCROLL_TO_RIGHTMOST_CMD, SCROLL_TO_TOP_CMD, SCROLL_UP_CMD,
86        ScrollRequest, ScrollToMode, ScrollToRequest, ScrollToTarget, ZOOM_IN_CMD, ZOOM_OUT_CMD, ZOOM_RESET_CMD, ZOOM_TO_FIT_CMD,
87        scroll_to, scroll_to_zoom,
88    };
89}