gpui: Refactor PlatformKeyboardLayout (#29653)

Release Notes:

- N/A
This commit is contained in:
张小白 2025-04-30 16:17:26 +08:00 committed by GitHub
parent edda386827
commit 074b6965e2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 131 additions and 106 deletions

View file

@ -299,9 +299,9 @@ impl Platform for WindowsPlatform {
fn keyboard_layout(&self) -> Box<dyn PlatformKeyboardLayout> {
Box::new(
KeyboardLayout::new()
WindowsKeyboardLayout::new()
.log_err()
.unwrap_or(KeyboardLayout::unknown()),
.unwrap_or(WindowsKeyboardLayout::unknown()),
)
}
@ -840,42 +840,6 @@ fn should_auto_hide_scrollbars() -> Result<bool> {
Ok(ui_settings.AutoHideScrollBars()?)
}
struct KeyboardLayout {
id: String,
name: String,
}
impl PlatformKeyboardLayout for KeyboardLayout {
fn id(&self) -> &str {
&self.id
}
fn name(&self) -> &str {
&self.name
}
}
impl KeyboardLayout {
fn new() -> Result<Self> {
let mut buffer = [0u16; KL_NAMELENGTH as usize];
unsafe { GetKeyboardLayoutNameW(&mut buffer)? };
let id = HSTRING::from_wide(&buffer).to_string();
let entry = windows_registry::LOCAL_MACHINE.open(format!(
"System\\CurrentControlSet\\Control\\Keyboard Layouts\\{}",
id
))?;
let name = entry.get_hstring("Layout Text")?.to_string();
Ok(Self { id, name })
}
fn unknown() -> Self {
Self {
id: "unknown".to_string(),
name: "unknown".to_string(),
}
}
}
#[cfg(test)]
mod tests {
use crate::{ClipboardItem, read_from_clipboard, write_to_clipboard};