Skip to content

Commit a857fe6

Browse files
arkus7ogoffart
authored andcommitted
Refactor Apple platform detection for keyboard input handling
- Replaced the previous `cfg_if` macro usage for detecting Apple platforms with a new `is_apple_platform` function in `lib.rs`, improving code readability - Updated the `window_event` function in `event_loop.rs` to utilize the new `is_apple_platform` function for determining the `swap_cmd_ctrl` variable. - Simplified the logic in `wasm_input_helper.rs` for checking if the platform is Apple by using the new function. - Adjusted the `text_shortcut` method in `input.rs` to leverage the new platform detection, ensuring consistent behavior for keyboard shortcuts across Apple devices.
1 parent a9ddd51 commit a857fe6

File tree

4 files changed

+24
-54
lines changed

4 files changed

+24
-54
lines changed

internal/backends/winit/event_loop.rs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -203,22 +203,7 @@ impl winit::application::ApplicationHandler<SlintEvent> for EventLoopState {
203203
WindowEvent::KeyboardInput { event, is_synthetic, .. } => {
204204
let key_code = event.logical_key;
205205
// For now: Match Qt's behavior of mapping command to control and control to meta (LWin/RWin).
206-
cfg_if::cfg_if!(
207-
if #[cfg(target_vendor = "apple")] {
208-
let swap_cmd_ctrl = true;
209-
} else if #[cfg(target_family = "wasm")] {
210-
let swap_cmd_ctrl = web_sys::window()
211-
.and_then(|window| window.navigator().platform().ok())
212-
.is_some_and(|platform| {
213-
let platform = platform.to_ascii_lowercase();
214-
platform.contains("mac")
215-
|| platform.contains("iphone")
216-
|| platform.contains("ipad")
217-
});
218-
} else {
219-
let swap_cmd_ctrl = false;
220-
}
221-
);
206+
let swap_cmd_ctrl = i_slint_core::is_apple_platform();
222207

223208
let key_code = if swap_cmd_ctrl {
224209
match key_code {

internal/backends/winit/wasm_input_helper.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,7 @@ impl WasmInputHelper {
6666
let mut h = Self { input, canvas: canvas.clone() };
6767

6868
// macos, or ipad with an attached keyboard, etc.
69-
let is_apple = window.navigator().platform().ok().map_or(false, |platform| {
70-
let platform = platform.to_ascii_lowercase();
71-
platform.contains("mac") || platform.contains("iphone") || platform.contains("ipad")
72-
});
69+
let is_apple = i_slint_core::is_apple_platform();
7370

7471
let shared_state = Rc::new(RefCell::new(WasmInputState::default()));
7572
#[cfg(web_sys_unstable_apis)]

internal/core/input.rs

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -354,22 +354,7 @@ impl KeyEvent {
354354
pub fn text_shortcut(&self) -> Option<TextShortcut> {
355355
let keycode = self.text.chars().next()?;
356356

357-
cfg_if::cfg_if!(
358-
if #[cfg(target_vendor = "apple")] {
359-
let is_apple = true;
360-
} else if #[cfg(target_family = "wasm")] {
361-
let is_apple = web_sys::window()
362-
.and_then(|window| window.navigator().platform().ok())
363-
.is_some_and(|platform| {
364-
let platform = platform.to_ascii_lowercase();
365-
platform.contains("mac")
366-
|| platform.contains("iphone")
367-
|| platform.contains("ipad")
368-
});
369-
} else {
370-
let is_apple = false;
371-
}
372-
);
357+
let is_apple = crate::is_apple_platform();
373358

374359
let move_mod = if is_apple {
375360
self.modifiers.alt && !self.modifiers.control && !self.modifiers.meta
@@ -416,24 +401,22 @@ impl KeyEvent {
416401
}
417402
}
418403

419-
if is_apple {
420-
if self.modifiers.control {
421-
match keycode {
422-
key_codes::LeftArrow => {
423-
return Some(TextShortcut::Move(TextCursorDirection::StartOfLine))
424-
}
425-
key_codes::RightArrow => {
426-
return Some(TextShortcut::Move(TextCursorDirection::EndOfLine))
427-
}
428-
key_codes::UpArrow => {
429-
return Some(TextShortcut::Move(TextCursorDirection::StartOfText))
430-
}
431-
key_codes::DownArrow => {
432-
return Some(TextShortcut::Move(TextCursorDirection::EndOfText))
433-
}
434-
_ => (),
435-
};
436-
}
404+
if is_apple && self.modifiers.control {
405+
match keycode {
406+
key_codes::LeftArrow => {
407+
return Some(TextShortcut::Move(TextCursorDirection::StartOfLine))
408+
}
409+
key_codes::RightArrow => {
410+
return Some(TextShortcut::Move(TextCursorDirection::EndOfLine))
411+
}
412+
key_codes::UpArrow => {
413+
return Some(TextShortcut::Move(TextCursorDirection::StartOfText))
414+
}
415+
key_codes::DownArrow => {
416+
return Some(TextShortcut::Move(TextCursorDirection::EndOfText))
417+
}
418+
_ => (),
419+
};
437420
}
438421

439422
if let Ok(direction) = TextCursorDirection::try_from(keycode) {

internal/core/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,8 @@ pub fn detect_operating_system() -> OperatingSystemType {
140140
OperatingSystemType::Other
141141
}
142142
}
143+
144+
/// Returns true if the current platform is an Apple platform (macOS, iOS, iPadOS)
145+
pub fn is_apple_platform() -> bool {
146+
matches!(detect_operating_system(), OperatingSystemType::Macos | OperatingSystemType::Ios)
147+
}

0 commit comments

Comments
 (0)