skip to Main Content

I’m making this widget in electron that shows up on the screen when you press ctrl+space.
I only want the widget be able to show up when there’s no app running in fullscreen mode.

I’ve tried using powershell but that doesn’t seem to work

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')
        })
    })
}

2

Answers


  1. This is a JS job, not node.js…

    // Detect when a frame/window is 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
    
     }
    
    };
    

    NB: "W" is the frame/window you’re operating on. To use this code in your own document replace "W.contentWindow" with just "window".

    More visualViewport info…

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

    Login or Signup to reply.
  2. You can also try these code

    Add-Type @"
        using System;
        using System.Runtime.InteropServices;
    
        public class Win32 {
            [DllImport("user32.dll")]
            [return: MarshalAs(UnmanagedType.Bool)]
            public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
    
            [StructLayout(LayoutKind.Sequential)]
            public struct RECT {
                public int Left;
                public int Top;
                public int Right;
                public int Bottom;
            }
        }
    "@
    
    function IsAppFullScreen {
        param (
            [Parameter(Mandatory = $true)]
            [string] $ProcessName
        )
    
        $processes = Get-Process | Where-Object { $_.MainWindowTitle -ne "" }
    
        foreach ($process in $processes) {
            $handle = $process.MainWindowHandle
            $rect = New-Object Win32.RECT
    
            if (-not [Win32]::GetWindowRect($handle, [ref]$rect)) {
                continue
            }
    
            $width = $rect.Right - $rect.Left
            $height = $rect.Bottom - $rect.Top
    
            $screenWidth = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds.Width
            $screenHeight = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds.Height
    
            if ($width -eq $screenWidth -and $height -eq $screenHeight) {
                return $true
            }
        }
    
        return $false
    }
    
    # Check if any app is fullscreen
    $isAnyAppFullScreen = $false
    $isAnyAppFullScreen = IsAppFullScreen
    
    # Return true if any app is fullscreen, false otherwise
    $isAnyAppFullScreen
    

    and NodeJs

    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-WmiObject Win32_DesktopMonitor).ScreenWidth
                    $screenHeight = (Get-WmiObject Win32_DesktopMonitor).ScreenHeight
                    
                    # 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');
            });
        });
    }
    
    // Usage
    isFullscreenAppRunning().then(result => {
        console.log('Is any app running in fullscreen?', result);
    }).catch(error => {
        console.error('Error:', error);
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search