fix extracted forground window not foreground (#8521)

Signed-off-by: 21pages <sunboeasy@gmail.com>
This commit is contained in:
21pages
2024-06-28 22:36:29 +08:00
committed by GitHub
parent 96aff38862
commit 3ae1638125
5 changed files with 113 additions and 14 deletions

View File

@@ -853,7 +853,32 @@ pub fn run_me<T: AsRef<std::ffi::OsStr>>(args: Vec<T>) -> std::io::Result<std::p
}
}
let cmd = std::env::current_exe()?;
return std::process::Command::new(cmd).args(&args).spawn();
let mut cmd = std::process::Command::new(cmd);
#[cfg(windows)]
let mut force_foreground = false;
#[cfg(windows)]
{
let arg_strs = args
.iter()
.map(|x| x.as_ref().to_string_lossy())
.collect::<Vec<_>>();
if arg_strs == vec!["--install"] || arg_strs == &["--noinstall"] {
cmd.env(crate::platform::SET_FOREGROUND_WINDOW, "1");
force_foreground = true;
}
}
let result = cmd.args(&args).spawn();
match result.as_ref() {
Ok(_child) =>
{
#[cfg(windows)]
if force_foreground {
unsafe { winapi::um::winuser::AllowSetForegroundWindow(_child.id() as u32) };
}
}
Err(err) => log::error!("run_me: {err:?}"),
}
result
}
#[inline]

View File

@@ -68,6 +68,7 @@ use winreg::RegKey;
pub const FLUTTER_RUNNER_WIN32_WINDOW_CLASS: &'static str = "FLUTTER_RUNNER_WIN32_WINDOW"; // main window, install window
pub const EXPLORER_EXE: &'static str = "explorer.exe";
pub const SET_FOREGROUND_WINDOW: &'static str = "SET_FOREGROUND_WINDOW";
pub fn get_focused_display(displays: Vec<DisplayInfo>) -> Option<usize> {
unsafe {
@@ -461,8 +462,18 @@ const SERVICE_TYPE: ServiceType = ServiceType::OWN_PROCESS;
extern "C" {
fn get_current_session(rdp: BOOL) -> DWORD;
fn LaunchProcessWin(cmd: *const u16, session_id: DWORD, as_user: BOOL, token_pid: &mut DWORD) -> HANDLE;
fn GetSessionUserTokenWin(lphUserToken: LPHANDLE, dwSessionId: DWORD, as_user: BOOL, token_pid: &mut DWORD) -> BOOL;
fn LaunchProcessWin(
cmd: *const u16,
session_id: DWORD,
as_user: BOOL,
token_pid: &mut DWORD,
) -> HANDLE;
fn GetSessionUserTokenWin(
lphUserToken: LPHANDLE,
dwSessionId: DWORD,
as_user: BOOL,
token_pid: &mut DWORD,
) -> BOOL;
fn selectInputDesktop() -> BOOL;
fn inputDesktopSelected() -> BOOL;
fn is_windows_server() -> BOOL;
@@ -2517,3 +2528,15 @@ fn nt_terminate_process(process_id: DWORD) -> ResultType<()> {
}
}
}
pub fn try_set_window_foreground(window: HWND) {
let env_key = SET_FOREGROUND_WINDOW;
if let Ok(value) = std::env::var(env_key) {
if value == "1" {
unsafe {
SetForegroundWindow(window);
}
std::env::remove_var(env_key);
}
}
}

View File

@@ -87,6 +87,8 @@ pub fn start(args: &mut [String]) {
frame.set_title(&crate::get_app_name());
#[cfg(target_os = "macos")]
crate::platform::delegate::make_menubar(frame.get_host(), args.is_empty());
#[cfg(windows)]
crate::platform::try_set_window_foreground(frame.get_hwnd() as _);
let page;
if args.len() > 1 && args[0] == "--play" {
args[0] = "--connect".to_owned();