检查应用程序是否正在全屏运行nodeJS

问题描述 投票:0回答:1

我正在电子中制作这个小部件,当您按 ctrl+space 时,它会显示在屏幕上。 我只希望当没有应用程序在全屏模式下运行时小部件能够显示。

我尝试过使用 powershell 但似乎不起作用

const { exec } = require('child_process')
function isFullscreenAppRunning() {
    return new Promise((resolve, reject) => {
        const script = `
        function Is-AppFullScreen {
                param(
                [Parameter(Mandatory = $true)]
                [string] $ProcessName
                )
            
                # Get main screen resolution
                $screenWidth = (Get-Screen).WorkingArea.Width
                $screenHeight = (Get-Screen).WorkingArea.Height
            
                # Get window of the process
                $window = Get-Process -Name $ProcessName | Where-Object {$_.MainWindowTitle -ne $null} | Select-Object -ExpandProperty MainWindowHandle
            
                if ($window) {
                # Get window dimensions (accounting for borders)
                $windowRect = [System.Drawing.Rectangle]::FromLTRB((Get-Window -Handle $window).Left, (Get-Window -Handle $window).Top, (Get-Window -Handle $window).Right, (Get-Window -Handle $window).Bottom)
                $windowWidth = $windowRect.Width - (Get-Window -Handle $window).BorderWidth
                $windowHeight = $windowRect.Height - (Get-Window -Handle $window).BorderHeight
            
                # Check if window dimensions match screen resolution (considering potential rounding errors)
                return ($windowWidth -eq $screenWidth -and $windowHeight -eq $screenHeight)
                } else {
                # Process not found or no main window
                return $false
                }
            }
            
            # Check all processes with a main window title
            $isAnyAppFullScreen = $false
            Get-Process | Where-Object {$_.MainWindowTitle -ne $null} | ForEach-Object {
                $processName = $_.Name
                if (Is-AppFullScreen -ProcessName $processName) {
                $isAnyAppFullScreen = $true
                # Exit loop once a fullscreen app is found (optional for performance)
                break
                }
            }
            
            # Return true if any app is fullscreen, false otherwise
            return $isAnyAppFullScreen
            `
        
        exec(`powershell -command "${script}"`, (err, stdout, stderr) => {
            if (err) {
            reject(err)
            return
            }
            resolve(stdout.trim() === 'True')
        })
    })
}
javascript node.js electron
1个回答
0
投票

这是一个 JS 工作,而不是 Node.js...

// Detect when in/out of full screen

W.contentWindow.visualViewport.onresize=function(e){

 if((W.offsetWidth*W.offsetHeight)==(screen.availWidth*screen.availHeight)){

  // gone full screen    

 } else {

  // exited full screen

 }

};

注意:“W”是您正在操作的框架/窗口。

更多视觉视口信息...

https://developer.mozilla.org/en-US/docs/Web/API/VisualViewport

© www.soinside.com 2019 - 2024. All rights reserved.