From 878c179761cbc536ad24906486430ed2bb1d6092 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Hornick=C3=BD?= Date: Fri, 15 May 2020 20:58:12 +0200 Subject: [PATCH] Implement Clone for 'static events (#1478) --- CHANGELOG.md | 1 + src/event.rs | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 120 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 284b9961..4086eefb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ # Unreleased +- Added Clone implementation for 'static events. - On Windows, fix window intermittently hanging when `ControlFlow` was set to `Poll`. - On Windows, fix `WindowBuilder::with_maximized` being ignored. - On Android, minimal platform support. diff --git a/src/event.rs b/src/event.rs index 6b9fe1a6..47119fa8 100644 --- a/src/event.rs +++ b/src/event.rs @@ -118,6 +118,30 @@ pub enum Event<'a, T: 'static> { LoopDestroyed, } +impl Clone for Event<'static, T> { + fn clone(&self) -> Self { + use self::Event::*; + match self { + WindowEvent { window_id, event } => WindowEvent { + window_id: *window_id, + event: event.clone(), + }, + UserEvent(event) => UserEvent(event.clone()), + DeviceEvent { device_id, event } => DeviceEvent { + device_id: *device_id, + event: event.clone(), + }, + NewEvents(cause) => NewEvents(cause.clone()), + MainEventsCleared => MainEventsCleared, + RedrawRequested(wid) => RedrawRequested(*wid), + RedrawEventsCleared => RedrawEventsCleared, + LoopDestroyed => LoopDestroyed, + Suspended => Suspended, + Resumed => Resumed, + } + } +} + impl<'a, T> Event<'a, T> { pub fn map_nonuser_event(self) -> Result, Event<'a, T>> { use self::Event::*; @@ -330,6 +354,97 @@ pub enum WindowEvent<'a> { ThemeChanged(Theme), } +impl Clone for WindowEvent<'static> { + fn clone(&self) -> Self { + use self::WindowEvent::*; + return match self { + Resized(size) => Resized(size.clone()), + Moved(pos) => Moved(pos.clone()), + CloseRequested => CloseRequested, + Destroyed => Destroyed, + DroppedFile(file) => DroppedFile(file.clone()), + HoveredFile(file) => HoveredFile(file.clone()), + HoveredFileCancelled => HoveredFileCancelled, + ReceivedCharacter(c) => ReceivedCharacter(*c), + Focused(f) => Focused(*f), + KeyboardInput { + device_id, + input, + is_synthetic, + } => KeyboardInput { + device_id: *device_id, + input: *input, + is_synthetic: *is_synthetic, + }, + + ModifiersChanged(modifiers) => ModifiersChanged(modifiers.clone()), + #[allow(deprecated)] + CursorMoved { + device_id, + position, + modifiers, + } => CursorMoved { + device_id: *device_id, + position: *position, + modifiers: *modifiers, + }, + CursorEntered { device_id } => CursorEntered { + device_id: *device_id, + }, + CursorLeft { device_id } => CursorLeft { + device_id: *device_id, + }, + #[allow(deprecated)] + MouseWheel { + device_id, + delta, + phase, + modifiers, + } => MouseWheel { + device_id: *device_id, + delta: *delta, + phase: *phase, + modifiers: *modifiers, + }, + #[allow(deprecated)] + MouseInput { + device_id, + state, + button, + modifiers, + } => MouseInput { + device_id: *device_id, + state: *state, + button: *button, + modifiers: *modifiers, + }, + TouchpadPressure { + device_id, + pressure, + stage, + } => TouchpadPressure { + device_id: *device_id, + pressure: *pressure, + stage: *stage, + }, + AxisMotion { + device_id, + axis, + value, + } => AxisMotion { + device_id: *device_id, + axis: *axis, + value: *value, + }, + Touch(touch) => Touch(*touch), + ThemeChanged(theme) => ThemeChanged(theme.clone()), + ScaleFactorChanged { .. } => { + unreachable!("Static event can't be about scale factor changing") + } + }; + } +} + impl<'a> WindowEvent<'a> { pub fn to_static(self) -> Option> { use self::WindowEvent::*; @@ -809,8 +924,10 @@ pub enum VirtualKeyCode { Multiply, Mute, MyComputer, - NavigateForward, // also called "Prior" - NavigateBackward, // also called "Next" + // also called "Next" + NavigateForward, + // also called "Prior" + NavigateBackward, NextTrack, NoConvert, NumpadComma,