pub enum ElementState {
Pressed,
Released,
}Expand description
Describes the input state of a key.
Variants§
Implementations§
Source§impl ElementState
impl ElementState
Sourcepub fn is_pressed(self) -> bool
pub fn is_pressed(self) -> bool
True if self == Pressed.
Examples found in repository?
examples/window.rs (line 397)
340 fn window_event(
341 &mut self,
342 event_loop: &ActiveEventLoop,
343 window_id: WindowId,
344 event: WindowEvent,
345 ) {
346 let window = match self.windows.get_mut(&window_id) {
347 Some(window) => window,
348 None => return,
349 };
350
351 match event {
352 WindowEvent::Resized(size) => {
353 window.resize(size);
354 },
355 WindowEvent::Focused(focused) => {
356 if focused {
357 info!("Window={window_id:?} focused");
358 } else {
359 info!("Window={window_id:?} unfocused");
360 }
361 },
362 WindowEvent::ScaleFactorChanged { scale_factor, .. } => {
363 info!("Window={window_id:?} changed scale to {scale_factor}");
364 },
365 WindowEvent::ThemeChanged(theme) => {
366 info!("Theme changed to {theme:?}");
367 window.set_draw_theme(theme);
368 },
369 WindowEvent::RedrawRequested => {
370 if let Err(err) = window.draw() {
371 error!("Error drawing window: {err}");
372 }
373 },
374 WindowEvent::Occluded(occluded) => {
375 window.set_occluded(occluded);
376 },
377 WindowEvent::CloseRequested => {
378 info!("Closing Window={window_id:?}");
379 self.windows.remove(&window_id);
380 },
381 WindowEvent::ModifiersChanged(modifiers) => {
382 window.modifiers = modifiers.state();
383 info!("Modifiers changed to {:?}", window.modifiers);
384 },
385 WindowEvent::MouseWheel { delta, .. } => match delta {
386 MouseScrollDelta::LineDelta(x, y) => {
387 info!("Mouse wheel Line Delta: ({x},{y})");
388 },
389 MouseScrollDelta::PixelDelta(px) => {
390 info!("Mouse wheel Pixel Delta: ({},{})", px.x, px.y);
391 },
392 },
393 WindowEvent::KeyboardInput { event, is_synthetic: false, .. } => {
394 let mods = window.modifiers;
395
396 // Dispatch actions only on press.
397 if event.state.is_pressed() {
398 let action = if let Key::Character(ch) = event.logical_key.as_ref() {
399 Self::process_key_binding(&ch.to_uppercase(), &mods)
400 } else {
401 None
402 };
403
404 if let Some(action) = action {
405 self.handle_action(event_loop, window_id, action);
406 }
407 }
408 },
409 WindowEvent::MouseInput { button, state, .. } => {
410 let mods = window.modifiers;
411 if let Some(action) =
412 state.is_pressed().then(|| Self::process_mouse_binding(button, &mods)).flatten()
413 {
414 self.handle_action(event_loop, window_id, action);
415 }
416 },
417 WindowEvent::CursorLeft { .. } => {
418 info!("Cursor left Window={window_id:?}");
419 window.cursor_left();
420 },
421 WindowEvent::CursorMoved { position, .. } => {
422 info!("Moved cursor to {position:?}");
423 window.cursor_moved(position);
424 },
425 WindowEvent::ActivationTokenDone { token: _token, .. } => {
426 #[cfg(any(x11_platform, wayland_platform))]
427 {
428 startup_notify::set_activation_token_env(_token);
429 if let Err(err) = self.create_window(event_loop, None) {
430 error!("Error creating new window: {err}");
431 }
432 }
433 },
434 WindowEvent::Ime(event) => match event {
435 Ime::Enabled => info!("IME enabled for Window={window_id:?}"),
436 Ime::Preedit(text, caret_pos) => {
437 info!("Preedit: {}, with caret at {:?}", text, caret_pos);
438 },
439 Ime::Commit(text) => {
440 info!("Committed: {}", text);
441 },
442 Ime::Disabled => info!("IME disabled for Window={window_id:?}"),
443 },
444 WindowEvent::PinchGesture { delta, .. } => {
445 window.zoom += delta;
446 let zoom = window.zoom;
447 if delta > 0.0 {
448 info!("Zoomed in {delta:.5} (now: {zoom:.5})");
449 } else {
450 info!("Zoomed out {delta:.5} (now: {zoom:.5})");
451 }
452 },
453 WindowEvent::RotationGesture { delta, .. } => {
454 window.rotated += delta;
455 let rotated = window.rotated;
456 if delta > 0.0 {
457 info!("Rotated counterclockwise {delta:.5} (now: {rotated:.5})");
458 } else {
459 info!("Rotated clockwise {delta:.5} (now: {rotated:.5})");
460 }
461 },
462 WindowEvent::PanGesture { delta, phase, .. } => {
463 window.panned.x += delta.x;
464 window.panned.y += delta.y;
465 info!("Panned ({delta:?})) (now: {:?}), {phase:?}", window.panned);
466 },
467 WindowEvent::DoubleTapGesture { .. } => {
468 info!("Smart zoom");
469 },
470 WindowEvent::TouchpadPressure { .. }
471 | WindowEvent::HoveredFileCancelled
472 | WindowEvent::KeyboardInput { .. }
473 | WindowEvent::CursorEntered { .. }
474 | WindowEvent::AxisMotion { .. }
475 | WindowEvent::DroppedFile(_)
476 | WindowEvent::HoveredFile(_)
477 | WindowEvent::Destroyed
478 | WindowEvent::Touch(_)
479 | WindowEvent::Moved(_) => (),
480 }
481 }Trait Implementations§
Source§impl Clone for ElementState
impl Clone for ElementState
Source§fn clone(&self) -> ElementState
fn clone(&self) -> ElementState
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for ElementState
impl Debug for ElementState
Source§impl<'de> Deserialize<'de> for ElementState
impl<'de> Deserialize<'de> for ElementState
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
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
impl Hash for ElementState
Source§impl PartialEq for ElementState
impl PartialEq for ElementState
Source§impl Serialize for ElementState
impl Serialize for ElementState
impl Copy for ElementState
impl Eq for ElementState
impl StructuralPartialEq for ElementState
Auto Trait Implementations§
impl Freeze for ElementState
impl RefUnwindSafe for ElementState
impl Send for ElementState
impl Sync for ElementState
impl Unpin for ElementState
impl UnsafeUnpin for ElementState
impl UnwindSafe for ElementState
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
Mutably borrows from an owned value. Read more