Struct ViewportState
pub struct ViewportState { /* private fields */ }Expand description
The pure zoom/pan state kernel. Owns the zoom factor and the scroll offsets, derives the content↔screen transform, and reduces input events (wheel, drag, arrow keys) into new state. Render-agnostic and cheap to clone — a host stores one of these and drives it from its own event + draw paths. See the module docs for the two usage shapes.
Implementations§
§impl ViewportState
impl ViewportState
pub fn new() -> ViewportState
pub fn new() -> ViewportState
A fresh viewport at 100 % zoom, no scroll, default bounds + padding,
content centred horizontally and top-aligned vertically (the common
document feel). Set the content size with
Self::set_content_size and the rect with Self::set_rect
before drawing.
pub fn with_zoom_bounds(self, min: f64, max: f64) -> ViewportState
pub fn with_zoom_bounds(self, min: f64, max: f64) -> ViewportState
Override the inclusive zoom bounds. min is clamped > 0 and max
is raised to at least min.
pub fn with_padding(self, pad: f64) -> ViewportState
pub fn with_padding(self, pad: f64) -> ViewportState
Override the screen-px padding reserved around the content. A pinned
value stays put across layouts; without it the padding is the
theme’s spacing.s6 (24 px at the default density).
pub fn with_alignment(
self,
align_x: SmallContentAlign,
align_y: SmallContentAlign,
) -> ViewportState
pub fn with_alignment( self, align_x: SmallContentAlign, align_y: SmallContentAlign, ) -> ViewportState
Override how content is aligned along each axis when it is smaller than the viewport on that axis.
pub fn set_rect(&mut self, rect: Rect)
pub fn set_rect(&mut self, rect: Rect)
Set the on-screen viewport rect. Re-clamps the scroll so the content stays reachable after a resize. Call once per layout pass.
pub fn set_content_size(&mut self, width: f64, height: f64)
pub fn set_content_size(&mut self, width: f64, height: f64)
Declare the content’s intrinsic size (content units). Re-clamps the scroll. Call whenever the document size changes (page added, image swapped, …).
pub fn rect(&self) -> Rect
pub fn zoom_bounds(&self) -> (f64, f64)
pub fn zoom_bounds(&self) -> (f64, f64)
Inclusive zoom bounds (min, max).
pub fn content_size(&self) -> (f64, f64)
pub fn content_size(&self) -> (f64, f64)
Declared content size (width, height) in content units.
pub fn scaled_content_width(&self) -> f64
pub fn scaled_content_width(&self) -> f64
Total screen width the content occupies at the current zoom, plus both side pads.
pub fn scaled_content_height(&self) -> f64
pub fn scaled_content_height(&self) -> f64
Total screen height the content occupies at the current zoom, plus both pads.
pub fn max_scroll_x(&self) -> f64
pub fn max_scroll_x(&self) -> f64
Maximum horizontal scroll (screen px). Zero when the content fits.
pub fn max_scroll_y(&self) -> f64
pub fn max_scroll_y(&self) -> f64
Maximum vertical scroll (screen px). Zero when the content fits.
pub fn overflows_x(&self) -> bool
pub fn overflows_x(&self) -> bool
Whether the content overflows the viewport horizontally (scrollbar + horizontal pan become live).
pub fn overflows_y(&self) -> bool
pub fn overflows_y(&self) -> bool
Whether the content overflows the viewport vertically.
pub fn clamp_scroll(&mut self)
pub fn clamp_scroll(&mut self)
Clamp both scroll offsets into their valid ranges. Idempotent; called internally after every mutation.
pub fn content_origin(&self) -> Point
pub fn content_origin(&self) -> Point
Screen-space top-left of the content (content (0,0) mapped to
screen). Useful for hosts that want the origin directly.
pub fn content_to_screen(&self) -> Affine
pub fn content_to_screen(&self) -> Affine
The affine that maps content space → screen space: scale by
zoom then translate to the content origin. A host draws its content
under transform * content_to_screen() and everything zooms/pans
correctly. Pure function of the current state.
pub fn content_point_to_screen(&self, p: Point) -> Point
pub fn content_point_to_screen(&self, p: Point) -> Point
Map a single content-space point to screen space.
pub fn screen_to_content(&self, p: Point) -> Point
pub fn screen_to_content(&self, p: Point) -> Point
Map a screen-space point back into content space — the inverse of
Self::content_to_screen. Use this to hit-test a pointer event
against content. Robust even at extreme zooms (zoom is always
>= min_zoom > 0).
pub fn content_contains_screen(&self, p: Point) -> bool
pub fn content_contains_screen(&self, p: Point) -> bool
Whether a screen point lands within the content’s drawn area (its scaled rect). Hosts can use it to decide whether a click hits the document or empty viewport margin.
pub fn zoom_at(&mut self, factor: f64, focal: Point)
pub fn zoom_at(&mut self, factor: f64, focal: Point)
Zoom by factor (e.g. 1.1 to zoom in 10 %) holding the content
point currently under focal (a screen point) stationary, so a
Ctrl+wheel zoom tracks the cursor and the content under it doesn’t
drift. Clamped to the zoom bounds; a no-op at the bound.
pub fn zoom_by(&mut self, factor: f64)
pub fn zoom_by(&mut self, factor: f64)
Zoom by factor holding the viewport centre fixed — the +/-
buttons and Ctrl+Plus / Ctrl+Minus path.
pub fn set_zoom(&mut self, zoom: f64)
pub fn set_zoom(&mut self, zoom: f64)
Set the zoom to an absolute value, holding the viewport centre fixed.
pub fn fit_zoom(&self) -> f64
pub fn fit_zoom(&self) -> f64
The zoom that fits the whole content inside the viewport (both axes
visible), honouring padding. Clamped to the zoom bounds. Returns
1.0 if the viewport or content is degenerate.
pub fn fit_width_zoom(&self) -> f64
pub fn fit_width_zoom(&self) -> f64
The zoom that fits the content’s width to the viewport.
pub fn reset_to_fit(&mut self)
pub fn reset_to_fit(&mut self)
Reset to fit-the-whole-content zoom, scrolled to the top-left. The Ctrl+0 path.
pub fn zoom_to_rect(&mut self, area: Rect)
pub fn zoom_to_rect(&mut self, area: Rect)
Zoom so area (content units) fills the viewport, honouring
padding, and centre it as far as the scroll range allows: the “zoom to
selection” path. A degenerate area keeps the zoom and only centres.
pub fn pan_by(&mut self, dx: f64, dy: f64)
pub fn pan_by(&mut self, dx: f64, dy: f64)
Pan by a screen-space delta (positive dx/dy reveal content to the
right / below). Clamped so the content stays reachable.
pub fn set_scroll(&mut self, scroll_x: f64, scroll_y: f64)
pub fn set_scroll(&mut self, scroll_x: f64, scroll_y: f64)
Set the scroll offsets directly (screen px), clamped.
pub fn begin_pan(&mut self, start: Point)
pub fn begin_pan(&mut self, start: Point)
Begin a drag-to-pan at start (screen point). Captures the current
scroll so motion maps 1:1.
pub fn update_pan(&mut self, cursor: Point) -> bool
pub fn update_pan(&mut self, cursor: Point) -> bool
Continue a drag-to-pan: move so the content tracks the cursor. Past
PAN_SLOP the drag is flagged as a real pan. Returns true while a
pan is in progress. No-op when no pan is active.
pub fn end_pan(&mut self) -> bool
pub fn end_pan(&mut self) -> bool
End a drag-to-pan. Returns true if the gesture actually moved the
content (so the host can treat a no-move release as a click).
pub fn is_panning(&self) -> bool
pub fn is_panning(&self) -> bool
Whether a drag-to-pan is currently in progress.
pub fn on_wheel(
&mut self,
delta: &MouseScrollDelta,
modifiers: &ModifiersState,
cursor: Point,
scale_factor: f64,
) -> bool
pub fn on_wheel( &mut self, delta: &MouseScrollDelta, modifiers: &ModifiersState, cursor: Point, scale_factor: f64, ) -> bool
Reduce a mouse-wheel event into zoom/pan, mirroring the PDF reader’s mapping:
- Ctrl/Cmd + wheel → zoom centred on
cursor(focal). - Shift + wheel (or a horizontal wheel) → horizontal pan.
- otherwise → vertical pan (with any horizontal wheel component).
cursor is the current pointer position in screen space. Returns
true if the viewport consumed the event (it always does when it can
scroll/zoom in the requested direction).
pub fn on_key(&mut self, key: &Key, modifiers: &ModifiersState) -> bool
pub fn on_key(&mut self, key: &Key, modifiers: &ModifiersState) -> bool
Reduce an arrow-key press into a pan / zoom action. Returns true if
the key was consumed. Mapping:
- arrows → pan by
DEFAULT_PAN_STEPin the corresponding direction; - Ctrl+
+/=→ zoom in (centre); Ctrl+-→ zoom out (centre); - Ctrl+
0→Self::reset_to_fit.
A host that wants the arrow keys to also page-step when the content
doesn’t overflow can check Self::overflows_x / Self::overflows_y
before delegating here.
pub fn frame_dt(&mut self) -> f64
pub fn frame_dt(&mut self) -> f64
Record a frame tick and return the clamped seconds since the last
call. The viewport has no easing of its own, but exposing the clamped
delta lets a host layer smooth-scroll / kinetic motion on top without
re-deriving the clock. The first call returns 0.0.
pub fn h_scrollbar_thumb(&self) -> Option<Rect>
pub fn h_scrollbar_thumb(&self) -> Option<Rect>
The horizontal scrollbar thumb rect (screen space), or None when
the content fits horizontally. A host can paint it itself, or rely on
DocumentViewport to do so.
pub fn v_scrollbar_thumb(&self) -> Option<Rect>
pub fn v_scrollbar_thumb(&self) -> Option<Rect>
The vertical scrollbar thumb rect (screen space), or None when the
content fits vertically.
pub fn press_scrollbar(&mut self, cursor: Point) -> bool
pub fn press_scrollbar(&mut self, cursor: Point) -> bool
Hit-test a left-press against either scrollbar. A press on a thumb
begins a direct thumb drag; a press on the track pages the view
toward the click. Returns true when the press landed on a bar, so
the caller suppresses drag-to-pan / child routing beneath it.
pub fn drag_scrollbar(&mut self, cursor: Point) -> bool
pub fn drag_scrollbar(&mut self, cursor: Point) -> bool
Continue an active scrollbar thumb drag, mapping the cursor to a new
scroll offset. Returns true while a bar is being dragged.
pub fn scrollbar_dragging(&self) -> bool
pub fn scrollbar_dragging(&self) -> bool
Whether a scrollbar thumb drag is in progress.
pub fn release_scrollbar(&mut self) -> bool
pub fn release_scrollbar(&mut self) -> bool
End any active scrollbar drag. Returns true when one was in
progress.
pub fn update_scrollbar_hover(&mut self, cursor: Point) -> bool
pub fn update_scrollbar_hover(&mut self, cursor: Point) -> bool
Recompute thumb hover from the cursor; returns true when a hover
edge changed (so the caller can schedule a redraw).
pub fn draw_scrollbars(
&self,
scene: &mut Scene,
transform: Affine,
theme: &Theme,
)
pub fn draw_scrollbars( &self, scene: &mut Scene, transform: Affine, theme: &Theme, )
Paint the scrollbars (when present) into scene. Reads the
scrollbar/thumb color from the theme so it matches OS chrome. Both
DocumentViewport and a canvas-root host call this after drawing
content. The thumb brightens while hovered or dragged.
Trait Implementations§
§impl Clone for ViewportState
impl Clone for ViewportState
§fn clone(&self) -> ViewportState
fn clone(&self) -> ViewportState
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more§impl Debug for ViewportState
impl Debug for ViewportState
§impl Default for ViewportState
impl Default for ViewportState
§fn default() -> ViewportState
fn default() -> ViewportState
Auto Trait Implementations§
impl Freeze for ViewportState
impl RefUnwindSafe for ViewportState
impl Send for ViewportState
impl Sync for ViewportState
impl Unpin for ViewportState
impl UnsafeUnpin for ViewportState
impl UnwindSafe for ViewportState
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.§impl<T> DowncastSync for T
impl<T> DowncastSync for T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more