Skip to content

Commit

Permalink
Simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
MolotovCherry committed Oct 22, 2024
1 parent 90aeb08 commit 13a8f2e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
1 change: 0 additions & 1 deletion crates/yabg3nml/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
mod cli;
mod console;
mod event;
mod event_loop;
mod is_admin;
mod loader;
mod logging;
Expand Down
29 changes: 15 additions & 14 deletions crates/yabg3nml/src/tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ use windows::Win32::{
};

use crate::{
event_loop::EventLoop, stop_token::StopToken, wapi::enum_windows::EnumWindowsRs, RunType,
stop_token::StopToken,
wapi::{enum_windows::EnumWindowsRs, event_loop::EventLoop},
RunType,
};

pub struct AppTray;
Expand Down Expand Up @@ -70,7 +72,7 @@ impl AppTray {
.unwrap(),
);

EventLoop::new().run(move |event_loop| {
EventLoop::new().run(move |event_loop, _| {
if let Ok(event) = MenuEvent::receiver().try_recv() {
if event.id == quit_i.id() {
if let Some(token) = timeout_token.as_ref() {
Expand All @@ -84,27 +86,26 @@ impl AppTray {

// this will close dialog popup window in injector mode so it doesn't hang process watcher
// when we try to quit. Would work for anything else hanging a thread too
// TODO: better impl of this, maybe have the messageboxes spawn from a top level window,
// then PostMessageW WM_CLOSE to that hwnd instead
let mut string_buf = String::new();
let mut buf = [0u16; 256];

// "#32770" - this lets us avoid string conversions
let class = [35u16, 51, 50, 55, 55, 48];

let mut buf = [0u16; 7];
EnumWindowsRs(|hwnd| {
let len = unsafe { GetClassNameW(hwnd, &mut buf) };
let buf = &buf[..len as usize];
if len == 0 {
// fn call failed, but it doesn't matter
return Ok(());
}

// copied from String::from_utf16_lossy, but edited to use an existing string buffer
let iter = char::decode_utf16(buf.iter().cloned())
.map(|r| r.unwrap_or(char::REPLACEMENT_CHARACTER));
string_buf.extend(iter);
let buf = &buf[..len as usize];

// looking for any open dialog box
if string_buf == "#32770" {
if buf == class {
// close the window
_ = unsafe { PostMessageW(hwnd, WM_CLOSE, WPARAM(0), LPARAM(0)) };
}

string_buf.clear();

Ok(())
});
}
Expand Down
1 change: 1 addition & 0 deletions crates/yabg3nml/src/wapi.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod enum_process_modules;
pub mod enum_processes;
pub mod enum_windows;
pub mod event_loop;
pub mod get_module_base_ex;
pub mod get_module_file_name_ex;
pub mod query_full_process_image_name;
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ use windows::Win32::UI::WindowsAndMessaging::{
DispatchMessageW, GetMessageW, PostQuitMessage, TranslateMessage, MSG,
};

/// Worlds simplest win32 event loop
pub struct EventLoop;

impl EventLoop {
pub fn new() -> Self {
Self
}

pub fn run(&self, mut cb: impl FnMut(&Self)) {
pub fn run(&self, mut cb: impl FnMut(&Self, &MSG)) {
let mut msg = MSG::default();

fn get_msg(msg: &mut MSG) -> bool {
Expand All @@ -24,7 +23,7 @@ impl EventLoop {
DispatchMessageW(&msg);
}

cb(self);
cb(self, &msg);
}
}

Expand Down

0 comments on commit 13a8f2e

Please sign in to comment.