production-taskbar-client / src / main / utils / winapi / winActivate.js
winActivate.js
Raw
import ffi from "@inigolabs/ffi-napi";

import { BOOL, HWND, INT, LONG } from "./types";

const kWinStateRestore = 9;
const kWinShowNormal = 1;

const user32 = new ffi.Library("user32", {
  IsIconic: [BOOL, [LONG]],
  GetLastActivePopup: [LONG, [LONG]],
  SetForegroundWindow: [BOOL, [LONG]],
  ShowWindow: [BOOL, [LONG, INT]],
  SetActiveWindow: [HWND, [HWND]],
  SetFocus: [HWND, [HWND]],
});

// deprecated due issue with SetForegroundWindow when self is not foreground
export default function winActivate(hwnd) {
  // use last active to focus modal windows if exists
  const lastActiveWindow = user32.GetLastActivePopup(hwnd);
  // use isIconic to detect if win is minimized
  if (user32.IsIconic(lastActiveWindow)) {
    user32.ShowWindow(lastActiveWindow, kWinStateRestore);
  } else {
    user32.ShowWindow(lastActiveWindow, kWinShowNormal);
    user32.SetForegroundWindow(lastActiveWindow);
  }
}