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 IntoUiNode) -> 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()
41//! - 1.vw() / s * h);
42//! layout::y = merge_var!(SCROLL.vertical_offset(), SCROLL.zoom_scale(), |&v, &s| v.0.fct_l() - 1.vh() / s * v);
43//! layout::scale = SCROLL.zoom_scale().map(|&fct| 1.fct() / fct);
44//! layout::transform_origin = 0;
45//! widget::auto_hide = false;
46//! layout::max_size = (1.vw(), 1.vh());
47//!
48//! child_align = Align::CENTER;
49//! child = msg;
50//! }
51//! }
52//! ```
53//!
54//! The example above declares a scroll with zoom and mouse pan features enabled, is also makes use of the [`SCROLL`] service
55//! to implement the `center_viewport` widget that is place in the content, but transforms to always be in the viewport.
56//!
57//! The `SCROLL` service can be used to interact with the parent `Scroll!`, you can also use commands in [`cmd`] to
58//! control any `Scroll!` widget.
59//!
60//! # Full API
61//!
62//! See [`zng_wgt_scroll`] for the full widget API.
63
64pub use zng_wgt_scroll::{
65 LazyMode, SCROLL, Scroll, ScrollBarArgs, ScrollFrom, ScrollInfo, ScrollMode, ScrollUnitsMix, Scrollbar, ScrollbarFnMix,
66 SmoothScrolling, Thumb, WidgetInfoExt, ZoomToFitMode, alt_factor, auto_hide_extra, clip_to_viewport, define_viewport_unit, h_line_unit,
67 h_page_unit, h_scrollbar_fn, h_wheel_unit, lazy, line_units, max_zoom, min_zoom, mode, mouse_pan, overscroll_color, page_units,
68 scroll_to_focused_mode, scrollbar_fn, scrollbar_joiner_fn, smooth_scrolling, v_line_unit, v_page_unit, v_scrollbar_fn, v_wheel_unit,
69 wheel_units, zoom_origin, zoom_size_only, zoom_to_fit_mode, zoom_touch_origin, zoom_wheel_origin, zoom_wheel_unit,
70};
71
72/// Scrollbar thumb widget.
73pub mod thumb {
74 pub use zng_wgt_scroll::thumb::{Thumb, cross_length, offset, viewport_ratio};
75}
76
77/// Scroll widget.
78pub mod scrollbar {
79 pub use zng_wgt_scroll::scrollbar::{Orientation, SCROLLBAR, Scrollbar, orientation};
80}
81
82/// Scroll commands.
83pub mod cmd {
84 pub use zng_wgt_scroll::cmd::{
85 PAGE_DOWN_CMD, PAGE_LEFT_CMD, PAGE_RIGHT_CMD, PAGE_UP_CMD, SCROLL_DOWN_CMD, SCROLL_LEFT_CMD, SCROLL_RIGHT_CMD,
86 SCROLL_TO_BOTTOM_CMD, SCROLL_TO_CMD, SCROLL_TO_LEFTMOST_CMD, SCROLL_TO_RIGHTMOST_CMD, SCROLL_TO_TOP_CMD, SCROLL_UP_CMD,
87 ScrollRequest, ScrollToMode, ScrollToRequest, ScrollToTarget, ZOOM_IN_CMD, ZOOM_OUT_CMD, ZOOM_RESET_CMD, ZOOM_TO_FIT_CMD,
88 ZoomToFitRequest, scroll_to, scroll_to_zoom,
89 };
90}