summaryrefslogtreecommitdiff
path: root/src/platform/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/platform/mod.rs')
-rw-r--r--src/platform/mod.rs43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/platform/mod.rs b/src/platform/mod.rs
index a836ed81..12c275f0 100644
--- a/src/platform/mod.rs
+++ b/src/platform/mod.rs
@@ -96,6 +96,49 @@ mod fallback;
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
pub use fallback::*;
+#[cfg(not(target_os = "macos"))]
+#[derive(Debug)]
+pub(crate) struct InputSourceRestore;
+
+#[cfg(not(target_os = "macos"))]
+pub(crate) fn switch_to_ascii_input_source() -> Option<InputSourceRestore> {
+ None
+}
+
+/// Switches the host keyboard input source while prefix mode is active.
+///
+/// `App` drives this through a trait so the prefix-mode transitions can be
+/// tested with a fake, without touching the real macOS APIs or leaking a
+/// platform-specific restore type into `App`.
+pub(crate) trait PrefixInputSource {
+ /// Switch to an ASCII-capable input source for prefix commands. No-op if
+ /// the current source is already ASCII-capable, the platform is
+ /// unsupported, or the switch fails. Calling it again before `restore`
+ /// keeps the source saved by the first call.
+ fn switch_to_ascii(&mut self);
+
+ /// Restore whatever `switch_to_ascii` saved. No-op if nothing was switched.
+ fn restore(&mut self);
+}
+
+/// Production [`PrefixInputSource`] backed by the per-platform API.
+#[derive(Default)]
+pub(crate) struct RealPrefixInputSource {
+ restore: Option<InputSourceRestore>,
+}
+
+impl PrefixInputSource for RealPrefixInputSource {
+ fn switch_to_ascii(&mut self) {
+ if self.restore.is_none() {
+ self.restore = switch_to_ascii_input_source();
+ }
+ }
+
+ fn restore(&mut self) {
+ let _ = self.restore.take();
+ }
+}
+
#[cfg(test)]
mod tests {
use super::*;