Enum ElementState

Source
pub enum ElementState {
    Pressed,
    Released,
}
Expand description

Describes the input state of a key.

Variants§

§

Pressed

§

Released

Implementations§

Source§

impl ElementState

Source

pub fn is_pressed(self) -> bool

True if self == Pressed.

Examples found in repository?
examples/window.rs (line 367)
310    fn window_event(
311        &mut self,
312        event_loop: &ActiveEventLoop,
313        window_id: WindowId,
314        event: WindowEvent,
315    ) {
316        let window = match self.windows.get_mut(&window_id) {
317            Some(window) => window,
318            None => return,
319        };
320
321        match event {
322            WindowEvent::Resized(size) => {
323                window.resize(size);
324            },
325            WindowEvent::Focused(focused) => {
326                if focused {
327                    info!("Window={window_id:?} focused");
328                } else {
329                    info!("Window={window_id:?} unfocused");
330                }
331            },
332            WindowEvent::ScaleFactorChanged { scale_factor, .. } => {
333                info!("Window={window_id:?} changed scale to {scale_factor}");
334            },
335            WindowEvent::ThemeChanged(theme) => {
336                info!("Theme changed to {theme:?}");
337                window.set_theme(theme);
338            },
339            WindowEvent::RedrawRequested => {
340                if let Err(err) = window.draw() {
341                    error!("Error drawing window: {err}");
342                }
343            },
344            WindowEvent::Occluded(occluded) => {
345                window.set_occluded(occluded);
346            },
347            WindowEvent::CloseRequested => {
348                info!("Closing Window={window_id:?}");
349                self.windows.remove(&window_id);
350            },
351            WindowEvent::ModifiersChanged(modifiers) => {
352                window.modifiers = modifiers.state();
353                info!("Modifiers changed to {:?}", window.modifiers);
354            },
355            WindowEvent::MouseWheel { delta, .. } => match delta {
356                MouseScrollDelta::LineDelta(x, y) => {
357                    info!("Mouse wheel Line Delta: ({x},{y})");
358                },
359                MouseScrollDelta::PixelDelta(px) => {
360                    info!("Mouse wheel Pixel Delta: ({},{})", px.x, px.y);
361                },
362            },
363            WindowEvent::KeyboardInput { event, is_synthetic: false, .. } => {
364                let mods = window.modifiers;
365
366                // Dispatch actions only on press.
367                if event.state.is_pressed() {
368                    let action = if let Key::Character(ch) = event.logical_key.as_ref() {
369                        Self::process_key_binding(&ch.to_uppercase(), &mods)
370                    } else {
371                        None
372                    };
373
374                    if let Some(action) = action {
375                        self.handle_action(event_loop, window_id, action);
376                    }
377                }
378            },
379            WindowEvent::MouseInput { button, state, .. } => {
380                let mods = window.modifiers;
381                if let Some(action) =
382                    state.is_pressed().then(|| Self::process_mouse_binding(button, &mods)).flatten()
383                {
384                    self.handle_action(event_loop, window_id, action);
385                }
386            },
387            WindowEvent::CursorLeft { .. } => {
388                info!("Cursor left Window={window_id:?}");
389                window.cursor_left();
390            },
391            WindowEvent::CursorMoved { position, .. } => {
392                info!("Moved cursor to {position:?}");
393                window.cursor_moved(position);
394            },
395            WindowEvent::ActivationTokenDone { token: _token, .. } => {
396                #[cfg(any(x11_platform, wayland_platform))]
397                {
398                    startup_notify::set_activation_token_env(_token);
399                    if let Err(err) = self.create_window(event_loop, None) {
400                        error!("Error creating new window: {err}");
401                    }
402                }
403            },
404            WindowEvent::Ime(event) => match event {
405                Ime::Enabled => info!("IME enabled for Window={window_id:?}"),
406                Ime::Preedit(text, caret_pos) => {
407                    info!("Preedit: {}, with caret at {:?}", text, caret_pos);
408                },
409                Ime::Commit(text) => {
410                    info!("Committed: {}", text);
411                },
412                Ime::Disabled => info!("IME disabled for Window={window_id:?}"),
413            },
414            WindowEvent::PinchGesture { delta, .. } => {
415                window.zoom += delta;
416                let zoom = window.zoom;
417                if delta > 0.0 {
418                    info!("Zoomed in {delta:.5} (now: {zoom:.5})");
419                } else {
420                    info!("Zoomed out {delta:.5} (now: {zoom:.5})");
421                }
422            },
423            WindowEvent::RotationGesture { delta, .. } => {
424                window.rotated += delta;
425                let rotated = window.rotated;
426                if delta > 0.0 {
427                    info!("Rotated counterclockwise {delta:.5} (now: {rotated:.5})");
428                } else {
429                    info!("Rotated clockwise {delta:.5} (now: {rotated:.5})");
430                }
431            },
432            WindowEvent::PanGesture { delta, phase, .. } => {
433                window.panned.x += delta.x;
434                window.panned.y += delta.y;
435                info!("Panned ({delta:?})) (now: {:?}), {phase:?}", window.panned);
436            },
437            WindowEvent::DoubleTapGesture { .. } => {
438                info!("Smart zoom");
439            },
440            WindowEvent::TouchpadPressure { .. }
441            | WindowEvent::HoveredFileCancelled
442            | WindowEvent::KeyboardInput { .. }
443            | WindowEvent::CursorEntered { .. }
444            | WindowEvent::AxisMotion { .. }
445            | WindowEvent::DroppedFile(_)
446            | WindowEvent::HoveredFile(_)
447            | WindowEvent::Destroyed
448            | WindowEvent::Touch(_)
449            | WindowEvent::Moved(_) => (),
450        }
451    }

Trait Implementations§

Source§

impl Clone for ElementState

Source§

fn clone(&self) -> ElementState

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for ElementState

Source§

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

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

impl<'de> Deserialize<'de> for ElementState

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Hash for ElementState

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for ElementState

Source§

fn eq(&self, other: &ElementState) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for ElementState

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Copy for ElementState

Source§

impl Eq for ElementState

Source§

impl StructuralPartialEq for ElementState

Auto Trait Implementations§

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> 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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,