Skip to content
Merged
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
70 changes: 36 additions & 34 deletions native/taskbar-lyric/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{
atomic::{AtomicBool, Ordering},
mpsc::{self, Receiver, Sender},
},
thread::{self, JoinHandle},
thread::{self},
};

use napi::{
Expand Down Expand Up @@ -62,7 +62,6 @@ enum TaskbarCommand {
#[napi]
pub struct TaskbarService {
sender: Sender<TaskbarCommand>,
thread_handle: Option<JoinHandle<()>>,
}

#[napi]
Expand All @@ -71,14 +70,11 @@ impl TaskbarService {
pub fn new(callback: ThreadsafeFunction<TaskbarLayout>) -> napi::Result<Self> {
let (tx, rx) = mpsc::channel();

let handle = thread::spawn(move || {
thread::spawn(move || {
worker_loop(rx, callback);
});

Ok(Self {
sender: tx,
thread_handle: Some(handle),
})
Ok(Self { sender: tx })
}

#[napi]
Expand All @@ -102,9 +98,6 @@ impl TaskbarService {
#[napi]
pub fn stop(&mut self) {
let _ = self.sender.send(TaskbarCommand::Stop);
if let Some(handle) = self.thread_handle.take() {
let _ = handle.join();
}
}
}

Expand Down Expand Up @@ -212,10 +205,24 @@ fn create_strategy() -> Option<Box<dyn TaskbarStrategy>> {
None
}

/// 用于关闭句柄的 RAII 包装器
struct EventHandle(HANDLE);

impl Drop for EventHandle {
fn drop(&mut self) {
unsafe {
let _ = CloseHandle(self.0);
}
}
}

// Safety: 跨线程访问句柄是安全的
unsafe impl Send for EventHandle {}
unsafe impl Sync for EventHandle {}

#[napi]
pub struct RegistryWatcher {
stop_event: usize,
thread_handle: Option<JoinHandle<()>>,
stop_event: Arc<EventHandle>,
is_running: Arc<AtomicBool>,
}

Expand All @@ -230,50 +237,39 @@ impl RegistryWatcher {
/// 创建停止事件失败时抛出错误
#[napi(constructor)]
pub fn new(callback: ThreadsafeFunction<()>) -> napi::Result<Self> {
let stop_event = unsafe { CreateEventW(None, true, false, None) }
let raw_event = unsafe { CreateEventW(None, true, false, None) }
.map_err(|e| napi::Error::from_reason(format!("创建停止事件失败: {e}")))?;

let stop_event = Arc::new(EventHandle(raw_event));
let is_running = Arc::new(AtomicBool::new(true));
let stop_event_raw = stop_event.0 as usize;
let thread_event = stop_event.clone();

let handle = thread::spawn(move || unsafe {
Self::watch_loop(stop_event_raw, &callback);
thread::spawn(move || unsafe {
Self::watch_loop(&thread_event, &callback);
});

Ok(Self {
stop_event: stop_event_raw,
thread_handle: Some(handle),
stop_event,
is_running,
})
}

#[napi]
#[allow(clippy::missing_errors_doc)]
pub fn stop(&mut self) -> napi::Result<()> {
pub fn stop(&mut self) {
if !self.is_running.load(Ordering::SeqCst) {
return Ok(());
}

let event = HANDLE(self.stop_event as *mut _);
unsafe {
let _ = SetEvent(event);
}

if let Some(handle) = self.thread_handle.take() {
let _ = handle.join();
return;
}

unsafe {
let _ = CloseHandle(event);
let _ = SetEvent(self.stop_event.0);
}

self.is_running.store(false, Ordering::SeqCst);
info!("注册表监听已停止");
Ok(())
}

unsafe fn watch_loop(stop_event_val: usize, callback: &ThreadsafeFunction<()>) {
let stop_event = HANDLE(stop_event_val as *mut _);
unsafe fn watch_loop(stop_event_wrapper: &Arc<EventHandle>, callback: &ThreadsafeFunction<()>) {
let stop_event = stop_event_wrapper.0;

let mut h_key = HKEY::default();
let sub_key = w!("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Advanced");
Expand Down Expand Up @@ -340,3 +336,9 @@ impl RegistryWatcher {
}
}
}

impl Drop for RegistryWatcher {
fn drop(&mut self) {
self.stop();
}
}
12 changes: 6 additions & 6 deletions native/taskbar-lyric/src/tray_watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ unsafe extern "system" fn win_event_proc(

pub struct NativeTrayWatcher {
thread_id: Option<u32>,
thread_handle: Option<thread::JoinHandle<()>>,
}

impl NativeTrayWatcher {
Expand All @@ -69,7 +68,7 @@ impl NativeTrayWatcher {

let (tx, rx) = std::sync::mpsc::channel();

let handle = thread::spawn(move || unsafe {
thread::spawn(move || unsafe {
let current_tid = GetCurrentThreadId();
let _ = tx.send(current_tid);

Expand Down Expand Up @@ -111,7 +110,6 @@ impl NativeTrayWatcher {

Ok(Self {
thread_id: Some(thread_id),
thread_handle: Some(handle),
})
}

Expand All @@ -122,10 +120,12 @@ impl NativeTrayWatcher {
}
self.thread_id = None;
}
}
}

if let Some(handle) = self.thread_handle.take() {
let _ = handle.join();
}
impl Drop for NativeTrayWatcher {
fn drop(&mut self) {
self.stop();
}
}

Expand Down
8 changes: 1 addition & 7 deletions native/taskbar-lyric/src/uia_watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,14 @@ impl IUIAutomationStructureChangedEventHandler_Impl for TaskbarEventHandler_Impl

pub struct NativeUiaWatcher {
thread_id: Option<u32>,
thread_handle: Option<thread::JoinHandle<()>>,
}

impl NativeUiaWatcher {
pub fn new(callback: LayoutChangedCallback) -> Result<Self> {
let (tx, rx) = mpsc::channel::<u32>();
let callback_arc = Arc::new(callback);

let handle = thread::spawn(move || unsafe {
thread::spawn(move || unsafe {
let _ = CoInitializeEx(None, COINIT_MULTITHREADED);

let thread_id = GetCurrentThreadId();
Expand Down Expand Up @@ -156,7 +155,6 @@ impl NativeUiaWatcher {

Ok(Self {
thread_id: Some(thread_id),
thread_handle: Some(handle),
})
}

Expand All @@ -167,10 +165,6 @@ impl NativeUiaWatcher {
}
self.thread_id = None;
}

if let Some(handle) = self.thread_handle.take() {
let _ = handle.join();
}
}
}

Expand Down