🌱 监听&获取文本缩放

#192
This commit is contained in:
BTMuli
2025-12-29 21:15:52 +08:00
parent 66b54dfc5e
commit cc3655a700
9 changed files with 132 additions and 63 deletions

View File

@@ -0,0 +1,43 @@
// 监听系统文本放缩
// @since Beta v0.9.1
use crate::utils;
use std::{thread, time::Duration};
use tauri::{AppHandle, Emitter};
use widestring::U16CString;
use windows_sys::Win32::Foundation::ERROR_SUCCESS;
use windows_sys::Win32::System::Registry::{
RegNotifyChangeKeyValue, RegOpenKeyExW, HKEY, HKEY_CURRENT_USER, KEY_NOTIFY, KEY_READ,
REG_NOTIFY_CHANGE_LAST_SET,
};
pub fn init(app: AppHandle) {
thread::spawn(move || unsafe {
let subkey = U16CString::from_str("Software\\Microsoft\\Accessibility").unwrap();
let mut hkey: HKEY = std::ptr::null_mut();
let status =
RegOpenKeyExW(HKEY_CURRENT_USER, subkey.as_ptr(), 0, KEY_READ | KEY_NOTIFY, &mut hkey);
if status != ERROR_SUCCESS {
eprintln!("❌ 无法打开注册表键: {}", status);
return;
}
loop {
let notify_status =
RegNotifyChangeKeyValue(hkey, 0, REG_NOTIFY_CHANGE_LAST_SET, std::ptr::null_mut(), 0);
if notify_status == ERROR_SUCCESS {
if let Ok(scale) = utils::read_text_scale_factor() {
let _ = app.emit("text_scale_change", scale);
}
} else {
eprintln!("❌ 注册表监听失败: {}", notify_status);
break;
}
thread::sleep(Duration::from_secs(1));
}
});
}