Skip to main content

Slider

Struct Slider 

Source
pub struct Slider<M> { /* private fields */ }
Expand description

A horizontal slider over min..=max.

The message carries the value itself rather than a fraction, so a setpoint reads as one:

enum Message { Setpoint(f32) }
Slider::new(16.0, 30.0, 21.5, Message::Setpoint).with_step(0.5);

§Dragging keeps the pointer even when it leaves

This is the whole reason a slider belongs in a toolkit rather than in every application. The tree already routes moves to the pressed widget wherever the pointer goes — but it clears VisualState::PRESSED the moment the pointer leaves the widget, because that is what makes a button’s drag-off cancel. A slider that read its drag state from PRESSED would therefore stop tracking at exactly the edge it most needs to keep tracking past.

So the drag is a flag of this widget’s own, set on press and cleared on release, and the pressed look comes from that flag rather than from the tree’s. A drag that runs off either end clamps and keeps following, and comes back when the pointer does.

The one thing that flag assumes is that every press is eventually followed by a release. That is the input stream’s contract, and every backend here honours it; there is no event that means “your capture was taken away”.

§Pressing the track jumps

Rather than stepping towards the press. A panel is touch-first, and a finger landing on a point of the track means go there — there is no cursor hovering to suggest anything else. It also makes the whole track a target instead of just the knob, which matters when the knob is 20 pixels wide and the finger is not.

Implementations§

Source§

impl<M> Slider<M>

Source

pub fn new(min: f32, max: f32, value: f32, message: fn(f32) -> M) -> Self

A slider over min..=max, starting at value.

A reversed range is put the right way round rather than refused: it is a caller’s argument order, not a state the widget has to model.

Source

pub fn inert(min: f32, max: f32, value: f32) -> Self

A slider that emits nothing, for a value the application reads rather than reacts to.

Source

pub fn with_step(self, step: f32) -> Self

Snaps to multiples of step from min.

Off by default, because a continuous slider is what a brightness or a volume wants. A step that is zero, negative or not a number is ignored rather than dividing by it.

Source

pub fn with_role(self, role: Role) -> Self

Sets the colour role of the filled portion and the knob.

Source

pub const fn value(&self) -> f32

The current value, always within the range and on a step if there is one.

Source

pub const fn range(&self) -> (f32, f32)

The range, in order.

Source

pub fn set_value(&mut self, value: f32)

Sets the value without emitting anything, clamped and snapped.

Silent for the same reason Checkbox::set_checked is: the message reports what a person did.

Source

pub fn update(&mut self, value: f32) -> bool

Sets the value, reporting whether it actually changed.

Source

pub fn set_range(&mut self, min: f32, max: f32)

Replaces the range, keeping the value inside it.

Source

pub fn set_role(&mut self, role: Role)

Replaces the colour role.

Source

pub const fn dragging(&self) -> bool

Whether a drag is in progress.

Trait Implementations§

Source§

impl<M: Clone> Clone for Slider<M>

Source§

fn clone(&self) -> Slider<M>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<M: Debug> Debug for Slider<M>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<M> Describe for Slider<M>

Source§

const KIND: &'static str = "slider"

The name a form file uses for this widget. Kebab-case.
Source§

const DOC: &'static str = "A value in a range, dragged along a track."

One line saying what this widget is, for somebody choosing one. Read more
Source§

const GROUP: Group = Group::Input

Which shelf of the catalogue this belongs on.
Source§

const ICON: &'static Icon

The widget’s glyph: a small portrait of the thing, for a palette to draw beside — or instead of — its name. Read more
Source§

const PROPERTIES: &'static [Property]

Every property, in the order an inspector should show them.
Source§

fn get(&self, name: &str) -> Option<Value>

The current value. Read more
Source§

fn apply(&mut self, name: &str, value: Value) -> Result<(), Mismatch>

Applies a value, reporting only what went wrong. Read more
Source§

fn set(&mut self, name: &str, value: Value) -> Result<(), PropertyError>

Applies a value, reporting what went wrong and where.
Source§

impl<M: 'static> Widget<M> for Slider<M>

Source§

fn describe(&self) -> Option<&dyn DynDescribe>

This widget’s property description, if it has one. Read more
Source§

fn describe_mut(&mut self) -> Option<&mut dyn DynDescribe>

The mutable half of describe.
Source§

fn paint(&self, ctx: &mut PaintCtx<'_>, canvas: &mut Pen<'_>)

Draws into canvas, which is already clipped to this widget’s bounds intersected with the damage region being repainted. Read more
Source§

fn on_event(&mut self, event: &Event<'_>, ctx: &mut EventCtx<'_, M>) -> Handled

Reacts to an event routed to this widget.
Source§

fn accepts_pointer(&self) -> bool

Returns true if the pointer can hit this widget. Read more
Source§

fn focusable(&self) -> bool

Returns true if this widget can take keyboard focus.
Source§

fn measure(&self, ctx: &mut MeasureCtx<'_>, offered: Offer) -> Measured

How big this widget would like to be, given what the caller can promise. Read more
Source§

fn preserves_focus(&self) -> bool

Returns true if a press on this widget should leave focus exactly where it is. Read more
Source§

fn animate(&mut self, now_ms: u64) -> Animation

Advances time-based state. Called only while this widget has asked to animate — see EventCtx::request_animation — and stops being called the moment it answers Wake::Never. Read more
Source§

fn snap(&mut self, now_ms: u64) -> Animation

Lands whatever is in flight at its end state, without animating it. Read more

Auto Trait Implementations§

§

impl<M> Freeze for Slider<M>
where Option<fn(f32) -> M>: Freeze,

§

impl<M> RefUnwindSafe for Slider<M>
where Option<fn(f32) -> M>: RefUnwindSafe,

§

impl<M> Send for Slider<M>
where Option<fn(f32) -> M>: Send,

§

impl<M> Sync for Slider<M>
where Option<fn(f32) -> M>: Sync,

§

impl<M> Unpin for Slider<M>
where Option<fn(f32) -> M>: Unpin,

§

impl<M> UnsafeUnpin for Slider<M>
where Option<fn(f32) -> M>: UnsafeUnpin,

§

impl<M> UnwindSafe for Slider<M>
where Option<fn(f32) -> M>: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> AsAny for T
where T: Any,

Source§

fn as_any(&self) -> &(dyn Any + 'static)

Borrows as dyn Any.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Mutably borrows as dyn Any.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynDescribe for T
where T: Describe,

Source§

fn kind(&self) -> &'static str

Source§

fn properties(&self) -> &'static [Property]

Source§

fn get_property(&self, name: &str) -> Option<Value>

Source§

fn set_property( &mut self, name: &str, value: Value, ) -> Result<(), PropertyError>

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.