Skip to content

Commit

Permalink
Merge pull request #3 from gertdreyer/features/activeWindowTitle
Browse files Browse the repository at this point in the history
Added Window Title on Windows
  • Loading branch information
dimusic committed Jul 20, 2022
2 parents 3ea31d5 + 3d55c87 commit 7a1f4d4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/common/active_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::window_position::WindowPosition;

#[derive(Debug)]
pub struct ActiveWindow {
pub title: String,
pub window_id: String,
pub process_id: u64,
pub position: WindowPosition
Expand Down
1 change: 1 addition & 0 deletions src/linux/platform_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ impl PlatformApi for LinuxPlatformApi {
process_id: window_pid.try_into().unwrap(),
window_id: active_window.resource_id().to_string(),
position,
title: String::from(""),
})
}
}
3 changes: 2 additions & 1 deletion src/mac/platform_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ impl PlatformApi for MacPlatformApi {
let active_window = ActiveWindow {
window_id: window_id.to_string(),
process_id: active_window_pid as u64,
position: win_pos
position: win_pos,
title: String::from(""),
};

return Ok(active_window)
Expand Down
18 changes: 16 additions & 2 deletions src/win/platform_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use winapi::shared::windef::HWND__;
use winapi::um::winuser::{GetWindowThreadProcessId};
use winapi::{
shared::windef::{ RECT },
um::{winuser::{ GetForegroundWindow, GetWindowRect }}
um::winuser::{GetForegroundWindow, GetWindowRect, GetWindowTextW},
};
use super::window_position::FromWinRect;

Expand All @@ -26,13 +26,14 @@ impl PlatformApi for WindowsPlatformApi {
fn get_active_window(&self) -> Result<ActiveWindow, ()> {
let active_window = get_foreground_window()?;
let active_window_position = self.get_position()?;

let active_window_title = get_window_title(active_window)?;
let lpdw_process_id = unsafe {
let pid_ptr: *mut u32 = std::mem::zeroed();
GetWindowThreadProcessId(active_window, pid_ptr)
};

let active_window = ActiveWindow {
title: active_window_title,
position: active_window_position,
process_id: lpdw_process_id as u64,
window_id: format!("{:?}", active_window),
Expand All @@ -42,6 +43,19 @@ impl PlatformApi for WindowsPlatformApi {
}
}

fn get_window_title(hwnd: *mut HWND__) -> Result<String, ()> {
let title: String;
unsafe {
let mut v: [u16; 255] = std::mem::zeroed();
let title_len = GetWindowTextW(hwnd, v.as_mut_ptr(), 255) as usize;
if title_len == 0 {
return Err(());
}
title = String::from_utf16_lossy(&v[0..title_len]);
};
Ok(title)
}

fn get_foreground_window() -> Result<*mut HWND__, ()> {
let active_window = unsafe { GetForegroundWindow() };

Expand Down

0 comments on commit 7a1f4d4

Please sign in to comment.