use crate::proc_snapshot::ProcSnapshot;
use crate::windows_bindings::{GetConsoleWindow, ShowWindow, SW_HIDE, SW_SHOW};
#[inline]
fn is_explorer_exe(ascii: &[i8; 260]) -> bool {
let explorer = b"explorer.exe\0";
ascii[..explorer.len()]
.iter()
.zip(explorer.iter())
.all(|(lhs, rhs)| *lhs as u8 == *rhs)
}
pub(crate) fn show_window(action: Action) {
let window = unsafe { GetConsoleWindow() };
if window.is_null() {
return;
}
let action = match action {
Action::Show => SW_SHOW,
Action::Hide => SW_HIDE,
Action::HideFromExplorer => {
let my_pid = std::process::id();
let Some(parent_pid) = ProcSnapshot::new()
.and_then(|p| p.find(|e| e.th32ProcessID == my_pid))
.map(|x| x.th32ParentProcessID)
else {
return;
};
let Some(parent) =
ProcSnapshot::new().and_then(|p| p.find(|e| e.th32ProcessID == parent_pid))
else {
return;
};
if !is_explorer_exe(&parent.szExeFile) {
return;
}
SW_HIDE
}
};
unsafe { ShowWindow(window, action) };
}
pub(crate) enum Action {
Show,
Hide,
HideFromExplorer,
}