From a4f2ebbe6acbee74d82c0d98c69f0d2388423bbb Mon Sep 17 00:00:00 2001 From: KonTy <9524513+KonTy@users.noreply.github.com> Date: Wed, 10 Jan 2024 12:58:06 -0800 Subject: [PATCH] Highly anticipated fix for small screens (#1358) * Increase performance during loading. Add a clear button to the search box. Add link and description attributes to the applications JSON. Use the link for linking to the app website. Use the description as a tooltip for each app. Add a clickable link to the website for each application (this took a long time; don't kick me if I got some wrong). Pressing Escape now clears the filter box. Pressing Alt-P prints your PID. * Fix for services that are being stopped * Compile winutil * Adding new Get-LocalizedYesNo based on choice.exe which is faster and more reliable, thank you @dtm-r for implementing it and testing it on English, German, Dutch, French, Italian, Spansich and Russian. Incredible work by @dtm-r, all cridit and props go to him. See this thread for details https://github.com/ChrisTitusTech/winutil/issues/1324 * Added error-checking logic for mounting ISOs and also created a wiki page that explains some of the errors. * Highly anticipated fix for small screen computers --------- Co-authored-by: KonTy --- scripts/main.ps1 | 69 ++++++++++++++++++++++++++---------------------- winutil.ps1 | 69 ++++++++++++++++++++++++++---------------------- 2 files changed, 76 insertions(+), 62 deletions(-) diff --git a/scripts/main.ps1 b/scripts/main.ps1 index 4c4ceed1e6..5a91b2dfce 100644 --- a/scripts/main.ps1 +++ b/scripts/main.ps1 @@ -280,27 +280,18 @@ $sync["Form"].Add_MouseDoubleClick({ } }) +$sync["Form"].Add_ContentRendered({ -# setting window icon to make it look more professional -$sync["Form"].Add_Loaded({ - - $downloadUrl = "https://christitus.com/images/logo-full.png" - $destinationPath = Join-Path $env:TEMP "cttlogo.png" - - # Check if the file already exists - if (-not (Test-Path $destinationPath)) { - # File does not exist, download it - $wc = New-Object System.Net.WebClient - $wc.DownloadFile($downloadUrl, $destinationPath) - Write-Host "File downloaded to: $destinationPath" - } else { - Write-Output "File already exists at: $destinationPath" + foreach ($proc in (Get-Process | Where-Object { $_.MainWindowTitle -and $_.MainWindowTitle -like "*tit*" })) { + if ($proc.Id -ne [System.IntPtr]::Zero) { + Write-Debug "MainWindowHandle: $($proc.Id) $($proc.MainWindowTitle) $($proc.MainWindowHandle)" + $windowHandle = $proc.MainWindowHandle + } } - $sync["Form"].Icon = $destinationPath - Try { - [Void][Window] - } Catch { + try { + [void][Window] + } catch { Add-Type @" using System; using System.Runtime.InteropServices; @@ -312,8 +303,7 @@ $sync["Form"].Add_Loaded({ [return: MarshalAs(UnmanagedType.Bool)] public static extern bool MoveWindow(IntPtr handle, int x, int y, int width, int height, bool redraw); [DllImport("user32.dll")] - [return: MarshalAs(UnmanagedType.Bool)] - public static extern bool ShowWindow(IntPtr handle, int state); + public static extern int GetSystemMetrics(int nIndex); } public struct RECT { public int Left; // x position of upper-left corner @@ -323,20 +313,37 @@ $sync["Form"].Add_Loaded({ } "@ } - - $processId = [System.Diagnostics.Process]::GetCurrentProcess().Id - $windowHandle = (Get-Process -Id $processId).MainWindowHandle + $rect = New-Object RECT - [Void][Window]::GetWindowRect($windowHandle,[ref]$rect) - - # only snap upper edge don't move left to right, in case people have multimon setup - $x = $rect.Left - $y = 0 + [void][Window]::GetWindowRect($windowHandle, [ref]$rect) $width = $rect.Right - $rect.Left $height = $rect.Bottom - $rect.Top - - # Move the window to that position... - [Void][Window]::MoveWindow($windowHandle, $x, $y, $width, $height, $True) + + Write-Debug "UpperLeft:$($rect.Left),$($rect.Top) LowerBottom:$($rect.Right),$($rect.Bottom). Width:$($width) Height:$($height)" + + # Load the Windows Forms assembly + Add-Type -AssemblyName System.Windows.Forms + $primaryScreen = [System.Windows.Forms.Screen]::PrimaryScreen + # Check if the primary screen is found + if ($primaryScreen) { + # Extract screen width and height for the primary monitor + $screenWidth = $primaryScreen.Bounds.Width + $screenHeight = $primaryScreen.Bounds.Height + + # Print the screen size + Write-Debug "Primary Monitor Width: $screenWidth pixels" + Write-Debug "Primary Monitor Height: $screenHeight pixels" + + # Compare with the primary monitor size + if ($width -gt $screenWidth -or $height -gt $screenHeight) { + Write-Debug "The specified width and/or height is greater than the primary monitor size." + [void][Window]::MoveWindow($windowHandle, 0, 0, $screenWidth, $screenHeight, $True) + } else { + Write-Debug "The specified width and height are within the primary monitor size limits." + } + } else { + Write-Debug "Unable to retrieve information about the primary monitor." + } Invoke-WPFTab "WPFTab1BT" $sync["Form"].Focus() diff --git a/winutil.ps1 b/winutil.ps1 index 0ceb6fa8ba..bfdd684752 100644 --- a/winutil.ps1 +++ b/winutil.ps1 @@ -9808,27 +9808,18 @@ $sync["Form"].Add_MouseDoubleClick({ } }) +$sync["Form"].Add_ContentRendered({ -# setting window icon to make it look more professional -$sync["Form"].Add_Loaded({ - - $downloadUrl = "https://christitus.com/images/logo-full.png" - $destinationPath = Join-Path $env:TEMP "cttlogo.png" - - # Check if the file already exists - if (-not (Test-Path $destinationPath)) { - # File does not exist, download it - $wc = New-Object System.Net.WebClient - $wc.DownloadFile($downloadUrl, $destinationPath) - Write-Host "File downloaded to: $destinationPath" - } else { - Write-Output "File already exists at: $destinationPath" + foreach ($proc in (Get-Process | Where-Object { $_.MainWindowTitle -and $_.MainWindowTitle -like "*tit*" })) { + if ($proc.Id -ne [System.IntPtr]::Zero) { + Write-Debug "MainWindowHandle: $($proc.Id) $($proc.MainWindowTitle) $($proc.MainWindowHandle)" + $windowHandle = $proc.MainWindowHandle + } } - $sync["Form"].Icon = $destinationPath - Try { - [Void][Window] - } Catch { + try { + [void][Window] + } catch { Add-Type @" using System; using System.Runtime.InteropServices; @@ -9840,8 +9831,7 @@ $sync["Form"].Add_Loaded({ [return: MarshalAs(UnmanagedType.Bool)] public static extern bool MoveWindow(IntPtr handle, int x, int y, int width, int height, bool redraw); [DllImport("user32.dll")] - [return: MarshalAs(UnmanagedType.Bool)] - public static extern bool ShowWindow(IntPtr handle, int state); + public static extern int GetSystemMetrics(int nIndex); } public struct RECT { public int Left; // x position of upper-left corner @@ -9851,20 +9841,37 @@ $sync["Form"].Add_Loaded({ } "@ } - - $processId = [System.Diagnostics.Process]::GetCurrentProcess().Id - $windowHandle = (Get-Process -Id $processId).MainWindowHandle + $rect = New-Object RECT - [Void][Window]::GetWindowRect($windowHandle,[ref]$rect) - - # only snap upper edge don't move left to right, in case people have multimon setup - $x = $rect.Left - $y = 0 + [void][Window]::GetWindowRect($windowHandle, [ref]$rect) $width = $rect.Right - $rect.Left $height = $rect.Bottom - $rect.Top - - # Move the window to that position... - [Void][Window]::MoveWindow($windowHandle, $x, $y, $width, $height, $True) + + Write-Debug "UpperLeft:$($rect.Left),$($rect.Top) LowerBottom:$($rect.Right),$($rect.Bottom). Width:$($width) Height:$($height)" + + # Load the Windows Forms assembly + Add-Type -AssemblyName System.Windows.Forms + $primaryScreen = [System.Windows.Forms.Screen]::PrimaryScreen + # Check if the primary screen is found + if ($primaryScreen) { + # Extract screen width and height for the primary monitor + $screenWidth = $primaryScreen.Bounds.Width + $screenHeight = $primaryScreen.Bounds.Height + + # Print the screen size + Write-Debug "Primary Monitor Width: $screenWidth pixels" + Write-Debug "Primary Monitor Height: $screenHeight pixels" + + # Compare with the primary monitor size + if ($width -gt $screenWidth -or $height -gt $screenHeight) { + Write-Debug "The specified width and/or height is greater than the primary monitor size." + [void][Window]::MoveWindow($windowHandle, 0, 0, $screenWidth, $screenHeight, $True) + } else { + Write-Debug "The specified width and height are within the primary monitor size limits." + } + } else { + Write-Debug "Unable to retrieve information about the primary monitor." + } Invoke-WPFTab "WPFTab1BT" $sync["Form"].Focus()