zng

Module scroll

source
Expand description

Scroll widgets, commands and properties.

The Scroll! widget accepts a single child of any size, overflow is clipped and can be brought into view by scrolling, the widget also supports content zooming and panning. The mode property can be used to dynamically change the ScrollMode.

use zng::prelude::*;

Scroll! {
    // ZOOM includes PAN that includes VERTICAL and HORIZONTAL
    mode = zng::scroll::ScrollMode::ZOOM;
    // mouse press and drag scrolls
    mouse_pan = true;
     
    child = Image! {
        // center_viewport uses the SCROLL service
        img_loading_fn = wgt_fn!(|_| center_viewport(Text!("loading..")));
         
        // content is a large image
        source = "https://upload.wikimedia.org/wikipedia/commons/e/ea/Van_Gogh_-_Starry_Night_-_Google_Art_Project.jpg";
        img_limits = zng::image::ImageLimits::none();
        img_downscale = zng::image::ImageDownscale::from(layout::Px(8000));
    }
}

fn center_viewport(msg: impl UiNode) -> impl UiNode {
    use zng::scroll::SCROLL;
    Container! {
        // center the message on the scroll viewport:
        //
        // the large images can take a moment to decode in debug builds, but the size
        // is already known after read, so the "loading.." message ends-up off-screen
        // because it is centered on the image.
        layout::x = merge_var!(SCROLL.horizontal_offset(), SCROLL.zoom_scale(), |&h, &s| h.0.fct_l() - 1.vw() / s * h);
        layout::y = merge_var!(SCROLL.vertical_offset(), SCROLL.zoom_scale(), |&v, &s| v.0.fct_l() - 1.vh() / s * v);
        layout::scale = SCROLL.zoom_scale().map(|&fct| 1.fct() / fct);
        layout::transform_origin = 0;
        widget::auto_hide = false;
        layout::max_size = (1.vw(), 1.vh());
         
        child_align = Align::CENTER;
        child = msg;
    }
}

The example above declares a scroll with zoom and mouse pan features enabled, is also makes use of the SCROLL service to implement the center_viewport widget that is place in the content, but transforms to always be in the viewport.

The SCROLL service can be used to interact with the parent Scroll!, you can also use commands in cmd to control any Scroll! widget.

§Full API

See zng_wgt_scroll for the full widget API.

Modules§

Structs§

Enums§

  • Lazy loading mode of a widget.
  • Defines a scroll delta and to what value source it is applied.

Traits§

Functions§