Skip to content

fix: fix keyboard provider displaying stale layout values #197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/desktop/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ fn listen_events(
},
Some(provider_emission) = emit_rx.recv() => {
info!("Provider emission: {:?}", provider_emission);
app_handle.emit("provider-emit", provider_emission.clone());
let _ = app_handle.emit("provider-emit", provider_emission.clone());
manager.update_cache(provider_emission).await;
Ok(())
},
Expand Down
22 changes: 17 additions & 5 deletions packages/desktop/src/providers/keyboard/keyboard_provider.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use anyhow::bail;
use serde::{Deserialize, Serialize};
use windows::Win32::{
Foundation::HWND,
Globalization::{LCIDToLocaleName, LOCALE_ALLOW_NEUTRAL_NAMES},
System::SystemServices::LOCALE_NAME_MAX_LENGTH,
UI::{
Input::KeyboardAndMouse::GetKeyboardLayout,
WindowsAndMessaging::{GetForegroundWindow, GetWindowThreadProcessId},
WindowsAndMessaging::{
GetGUIThreadInfo, GetWindowThreadProcessId, GUITHREADINFO,
},
},
};

Expand Down Expand Up @@ -41,12 +44,21 @@ impl KeyboardProvider {
KeyboardProvider { config, common }
}

unsafe fn get_focused_hwnd() -> anyhow::Result<HWND> {
// see: https://stackoverflow.com/questions/51945835/how-to-obtain-keyboard-layout-for-microsoft-edge-and-other-windows-hosted-in-app
let mut gui_thread_info = GUITHREADINFO {
cbSize: std::mem::size_of::<GUITHREADINFO>() as u32,
..Default::default()
};

GetGUIThreadInfo(0, &mut gui_thread_info)?;
return Ok(gui_thread_info.hwndFocus);
}

fn run_interval(&mut self) -> anyhow::Result<KeyboardOutput> {
let keyboard_layout = unsafe {
GetKeyboardLayout(GetWindowThreadProcessId(
GetForegroundWindow(),
None,
))
let hwnd = KeyboardProvider::get_focused_hwnd()?;
GetKeyboardLayout(GetWindowThreadProcessId(hwnd, None))
};

let lang_id = (keyboard_layout.0 as u32) & 0xffff;
Expand Down
5 changes: 3 additions & 2 deletions packages/desktop/src/sys_tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,9 @@ impl SysTray {
// Show the settings window on left click (Windows-only).
#[cfg(windows)]
{
tray_icon =
tray_icon.menu_on_left_click(false).on_tray_icon_event({
tray_icon = tray_icon
.show_menu_on_left_click(false)
.on_tray_icon_event({
let app_handle = self.app_handle.clone();
let config = self.config.clone();
let widget_factory = self.widget_factory.clone();
Expand Down
Loading