Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP - Automate readme screenshots #18

Draft
wants to merge 35 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions .github/workflows/screenshots.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
name: Generate Screenshots

on:
pull_request:
push:
tags:
- "v*"

jobs:
screenshot:
runs-on: windows-latest

steps:
- uses: actions/checkout@v4

- name: Set up Python 3.x
uses: actions/setup-python@v5
with:
python-version: ">=3.7 <3.12"
cache: "pip"

- name: Install dependencies
run: |
pip install .
shell: powershell

- name: Increase resolution
run: |
Set-DisplayResolution -Width 1920 -Height 1080 -Force
shell: powershell

- name: Hide taskbar
run: |
$location = @{Path = 'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\StuckRects3'; Name = 'Settings'}
$value = Get-ItemPropertyValue @location
$value[8] = 123
Set-ItemProperty @location $value
Stop-Process -Name Explorer
shell: powershell

# Debug capture the entire screen
- uses: OrbitalOwen/desktop-screenshot-action@0.1
with:
file-name: "screenshot-debug0.jpg"

# - name: Upload screenshots
# uses: actions/upload-artifact@v4
# with:
# name: screenshots
# path: screenshot-*.png

# - name: Run app and take screenshot (Light mode)
# run: |
# .\screenshot.ps1 light
# shell: powershell

- name: Run app
run: |
Start-Process -NoNewWindow -FilePath "python" -ArgumentList "torf_gui/gui.py"
shell: powershell

- name: Wait for app to start
run: |
Start-Sleep -s 5
shell: powershell

# Debug capture the entire screen
- uses: OrbitalOwen/desktop-screenshot-action@0.1
with:
file-name: "screenshot-debug1.jpg"

# - name: Upload screenshots
# uses: actions/upload-artifact@v4
# with:
# name: screenshots
# path: screenshot-*.png

- name: Set dark mode
run: |
$darkModeReg = "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize"
Set-ItemProperty -Path $darkModeReg -Name "AppsUseLightTheme" -Value 0
shell: powershell

# - name: Run app and take screenshot (Dark mode)
# run: |
# .\screenshot.ps1 dark
# shell: powershell

- name: Run app
run: |
Start-Process -NoNewWindow -FilePath "python" -ArgumentList "torf_gui/gui.py"
shell: powershell

- name: Wait for app to start
run: |
Start-Sleep -s 5
shell: powershell

# Debug capture the entire screen
- uses: OrbitalOwen/desktop-screenshot-action@0.1
with:
file-name: "screenshot-debug2.jpg"

- name: list directory
run: |
Get-ChildItem
shell: powershell

- name: Upload screenshots
uses: actions/upload-artifact@v4
with:
name: screenshots
path: screenshot-*.*
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,7 @@ ENV/

# pytest
.pytest_cache/

screenshot-light.png
screenshot-dark.png
screenshot.png
96 changes: 96 additions & 0 deletions screenshot.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
param (
[Parameter(Mandatory = $true)]
[ValidateSet("light", "dark")]
[string]$theme
)

# Debug print the theme
Write-Host "Theme: $theme"

$version = (Get-Content torf_gui/version.py | Select-String -Pattern "__version__").ToString().Split(" ")[2].Trim('"')

# Print the version

Write-Host "Version: $version"

# Start the Python script
Start-Process python "torf_gui/gui.py"

Write-Host "Waiting for the process to start..."

# Wait for the new process to start
Start-Sleep -Seconds 5

# Get the new Python process
$newProcess = Get-Process -Name python | Where-Object { $_.Id -ne $originalProcess.Id }

# Debug print running processes
# Write-Host "Running processes:"
# Get-Process | ForEach-Object { Write-Host $_.Name }

# Debug print the process ID
Write-Host "Process ID: $($newProcess.Id)"

# Capture the window screenshot
Add-Type -TypeDefinition @"
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential)]
public struct RECT {
public int Left;
public int Top;
public int Right;
public int Bottom;
}

public class ScreenCapture {
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll")]
public static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect);

[DllImport("user32.dll")]
public static extern bool ClientToScreen(IntPtr hWnd, ref Point lpPoint);

public Image CaptureWindow(string windowTitle) {
IntPtr handle = FindWindow(null, windowTitle);
if (handle == IntPtr.Zero) {
throw new ArgumentException(String.Format("Window with title '{0}' not found.", windowTitle));
}

RECT rect;
GetClientRect(handle, out rect);
var topLeft = new Point(rect.Left, rect.Top);
var bottomRight = new Point(rect.Right, rect.Bottom);
ClientToScreen(handle, ref topLeft);
ClientToScreen(handle, ref bottomRight);
return CaptureArea(new Rectangle(topLeft.X, topLeft.Y, bottomRight.X - topLeft.X, bottomRight.Y - topLeft.Y));
}

public Image CaptureArea(Rectangle area) {
var bitmap = new Bitmap(area.Width, area.Height);
using (var graphics = Graphics.FromImage(bitmap)) {
graphics.CopyFromScreen(area.Left, area.Top, 0, 0, area.Size);
}
return bitmap;
}
}
"@ -ReferencedAssemblies System.Drawing, System.Drawing.Common, System.Drawing.Primitives, System.Windows.Forms

# Capture the window screenshot

Write-Host "Capturing the window screenshot..."

$screenCapture = New-Object ScreenCapture
Write-Host "Test 1"
$bitmap = $screenCapture.CaptureWindow("torf-gui $version")
Write-Host "Test 2"
$bitmap.Save("screenshot-$theme.png", [System.Drawing.Imaging.ImageFormat]::Png)
Write-Host "Test 3"

# Close the Python script
$newProcess.Kill()
Loading