|
| 1 | +use iced::Task; |
| 2 | +use iced::keyboard::{self, Key, key}; |
| 3 | +use iced::widget::operation::{self, AbsoluteOffset}; |
| 4 | + |
| 5 | +use crate::messages::Message; |
| 6 | + |
| 7 | +pub(crate) const CONTENT_SCROLL_ID: &str = "configurator-content-scroll"; |
| 8 | + |
| 9 | +const LINE_SCROLL_DELTA_Y: f32 = 64.0; |
| 10 | +const PAGE_SCROLL_DELTA_Y: f32 = 360.0; |
| 11 | +const EDGE_SCROLL_DELTA_Y: f32 = 1_000_000.0; |
| 12 | + |
| 13 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 14 | +pub(crate) enum ContentScrollAction { |
| 15 | + Top, |
| 16 | + Bottom, |
| 17 | + LineUp, |
| 18 | + LineDown, |
| 19 | + PageUp, |
| 20 | + PageDown, |
| 21 | +} |
| 22 | + |
| 23 | +impl ContentScrollAction { |
| 24 | + pub(crate) fn can_scroll_when_captured(self) -> bool { |
| 25 | + matches!(self, ContentScrollAction::Top | ContentScrollAction::Bottom) |
| 26 | + } |
| 27 | + |
| 28 | + pub(crate) fn task(self) -> Task<Message> { |
| 29 | + match self { |
| 30 | + ContentScrollAction::Top => scroll_by_y(-EDGE_SCROLL_DELTA_Y), |
| 31 | + ContentScrollAction::Bottom => scroll_by_y(EDGE_SCROLL_DELTA_Y), |
| 32 | + ContentScrollAction::LineUp => scroll_by_y(-LINE_SCROLL_DELTA_Y), |
| 33 | + ContentScrollAction::LineDown => scroll_by_y(LINE_SCROLL_DELTA_Y), |
| 34 | + ContentScrollAction::PageUp => scroll_by_y(-PAGE_SCROLL_DELTA_Y), |
| 35 | + ContentScrollAction::PageDown => scroll_by_y(PAGE_SCROLL_DELTA_Y), |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +pub(crate) fn content_scroll_action_for_event( |
| 41 | + event: &keyboard::Event, |
| 42 | +) -> Option<ContentScrollAction> { |
| 43 | + let keyboard::Event::KeyPressed { |
| 44 | + key, |
| 45 | + physical_key, |
| 46 | + modifiers, |
| 47 | + .. |
| 48 | + } = event |
| 49 | + else { |
| 50 | + return None; |
| 51 | + }; |
| 52 | + |
| 53 | + if !modifiers.is_empty() { |
| 54 | + return None; |
| 55 | + } |
| 56 | + |
| 57 | + content_scroll_action_for_key(key.as_ref()) |
| 58 | + .or_else(|| content_scroll_action_for_physical_key(*physical_key)) |
| 59 | +} |
| 60 | + |
| 61 | +fn content_scroll_action_for_key(key: Key<&str>) -> Option<ContentScrollAction> { |
| 62 | + match key { |
| 63 | + Key::Named(key::Named::Home) => Some(ContentScrollAction::Top), |
| 64 | + Key::Named(key::Named::End) => Some(ContentScrollAction::Bottom), |
| 65 | + Key::Named(key::Named::ArrowUp) => Some(ContentScrollAction::LineUp), |
| 66 | + Key::Named(key::Named::ArrowDown) => Some(ContentScrollAction::LineDown), |
| 67 | + Key::Named(key::Named::PageUp) => Some(ContentScrollAction::PageUp), |
| 68 | + Key::Named(key::Named::PageDown) => Some(ContentScrollAction::PageDown), |
| 69 | + _ => None, |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +fn content_scroll_action_for_physical_key( |
| 74 | + physical_key: key::Physical, |
| 75 | +) -> Option<ContentScrollAction> { |
| 76 | + match physical_key { |
| 77 | + key::Physical::Code(key::Code::Home) => Some(ContentScrollAction::Top), |
| 78 | + key::Physical::Code(key::Code::End) => Some(ContentScrollAction::Bottom), |
| 79 | + key::Physical::Code(key::Code::ArrowUp) => Some(ContentScrollAction::LineUp), |
| 80 | + key::Physical::Code(key::Code::ArrowDown) => Some(ContentScrollAction::LineDown), |
| 81 | + key::Physical::Code(key::Code::PageUp) => Some(ContentScrollAction::PageUp), |
| 82 | + key::Physical::Code(key::Code::PageDown) => Some(ContentScrollAction::PageDown), |
| 83 | + _ => None, |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +fn scroll_by_y(y: f32) -> Task<Message> { |
| 88 | + operation::scroll_by(CONTENT_SCROLL_ID, AbsoluteOffset { x: 0.0, y }) |
| 89 | +} |
| 90 | + |
| 91 | +#[cfg(test)] |
| 92 | +mod tests { |
| 93 | + use super::*; |
| 94 | + use iced::keyboard::{Location, Modifiers, key}; |
| 95 | + |
| 96 | + fn key_press(key: Key) -> keyboard::Event { |
| 97 | + keyboard::Event::KeyPressed { |
| 98 | + key: key.clone(), |
| 99 | + modified_key: key, |
| 100 | + physical_key: key::Physical::Unidentified(key::NativeCode::Unidentified), |
| 101 | + location: Location::Standard, |
| 102 | + modifiers: Modifiers::empty(), |
| 103 | + text: None, |
| 104 | + repeat: false, |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + fn physical_key_press(physical_key: key::Physical) -> keyboard::Event { |
| 109 | + keyboard::Event::KeyPressed { |
| 110 | + key: Key::Unidentified, |
| 111 | + modified_key: Key::Unidentified, |
| 112 | + physical_key, |
| 113 | + location: Location::Standard, |
| 114 | + modifiers: Modifiers::empty(), |
| 115 | + text: None, |
| 116 | + repeat: false, |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + #[test] |
| 121 | + fn navigation_keys_map_to_content_scroll_actions() { |
| 122 | + let cases = [ |
| 123 | + (Key::Named(key::Named::Home), ContentScrollAction::Top), |
| 124 | + (Key::Named(key::Named::End), ContentScrollAction::Bottom), |
| 125 | + (Key::Named(key::Named::ArrowUp), ContentScrollAction::LineUp), |
| 126 | + ( |
| 127 | + Key::Named(key::Named::ArrowDown), |
| 128 | + ContentScrollAction::LineDown, |
| 129 | + ), |
| 130 | + (Key::Named(key::Named::PageUp), ContentScrollAction::PageUp), |
| 131 | + ( |
| 132 | + Key::Named(key::Named::PageDown), |
| 133 | + ContentScrollAction::PageDown, |
| 134 | + ), |
| 135 | + ]; |
| 136 | + |
| 137 | + for (key, expected) in cases { |
| 138 | + assert_eq!( |
| 139 | + content_scroll_action_for_event(&key_press(key)), |
| 140 | + Some(expected) |
| 141 | + ); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + #[test] |
| 146 | + fn modified_navigation_keys_do_not_scroll_content() { |
| 147 | + let event = keyboard::Event::KeyPressed { |
| 148 | + key: Key::Named(key::Named::Home), |
| 149 | + modified_key: Key::Named(key::Named::Home), |
| 150 | + physical_key: key::Physical::Code(key::Code::Home), |
| 151 | + location: Location::Standard, |
| 152 | + modifiers: Modifiers::CTRL, |
| 153 | + text: None, |
| 154 | + repeat: false, |
| 155 | + }; |
| 156 | + |
| 157 | + assert_eq!(content_scroll_action_for_event(&event), None); |
| 158 | + } |
| 159 | + |
| 160 | + #[test] |
| 161 | + fn physical_navigation_keys_map_to_content_scroll_actions() { |
| 162 | + let cases = [ |
| 163 | + ( |
| 164 | + key::Physical::Code(key::Code::Home), |
| 165 | + ContentScrollAction::Top, |
| 166 | + ), |
| 167 | + ( |
| 168 | + key::Physical::Code(key::Code::End), |
| 169 | + ContentScrollAction::Bottom, |
| 170 | + ), |
| 171 | + ( |
| 172 | + key::Physical::Code(key::Code::ArrowUp), |
| 173 | + ContentScrollAction::LineUp, |
| 174 | + ), |
| 175 | + ( |
| 176 | + key::Physical::Code(key::Code::ArrowDown), |
| 177 | + ContentScrollAction::LineDown, |
| 178 | + ), |
| 179 | + ( |
| 180 | + key::Physical::Code(key::Code::PageUp), |
| 181 | + ContentScrollAction::PageUp, |
| 182 | + ), |
| 183 | + ( |
| 184 | + key::Physical::Code(key::Code::PageDown), |
| 185 | + ContentScrollAction::PageDown, |
| 186 | + ), |
| 187 | + ]; |
| 188 | + |
| 189 | + for (physical_key, expected) in cases { |
| 190 | + assert_eq!( |
| 191 | + content_scroll_action_for_event(&physical_key_press(physical_key)), |
| 192 | + Some(expected) |
| 193 | + ); |
| 194 | + } |
| 195 | + } |
| 196 | +} |
0 commit comments