Repository navigation
chore: Move Unity process focus helpers into common package #1484
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package unityprocess | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
| ) | ||
|
|
||
| func commandErrorWithStderr(err error, stderr string) error { | ||
| if err == nil { | ||
| return nil | ||
| } | ||
| trimmedStderr := strings.TrimSpace(stderr) | ||
| if trimmedStderr == "" { | ||
| return err | ||
| } | ||
| return fmt.Errorf("%w: %s", err, trimmedStderr) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package unityprocess | ||
|
|
||
| import ( | ||
| "errors" | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| // Verifies command stderr is included in the returned error message. | ||
| func TestCommandErrorWithStderrAppendsTrimmedStderr(t *testing.T) { | ||
| err := commandErrorWithStderr(errors.New("exit status 1"), " PowerShell throw text\r\n") | ||
|
|
||
| if err == nil || !strings.Contains(err.Error(), "PowerShell throw text") { | ||
| t.Fatalf("expected stderr in error, got %v", err) | ||
| } | ||
| } | ||
|
|
||
| // Verifies empty command stderr leaves the original error message intact. | ||
| func TestCommandErrorWithStderrKeepsOriginalErrorWithoutStderr(t *testing.T) { | ||
| err := errors.New("exit status 1") | ||
|
|
||
| actual := commandErrorWithStderr(err, " \r\n") | ||
|
|
||
| if actual != err { | ||
| t.Fatalf("expected original error, got %v", actual) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| //go:build darwin | ||
|
|
||
| package unityprocess | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os/exec" | ||
| "strconv" | ||
| "strings" | ||
| ) | ||
|
|
||
| func FocusUnityProcess(ctx context.Context, pid int) error { | ||
| return setFrontmostProcessMac(ctx, pid) | ||
| } | ||
|
|
||
| func FocusUnityProcessWithRestore(ctx context.Context, pid int) (RestoreFocusFunc, error) { | ||
| previousPID := readFrontmostProcessIDMac(ctx) | ||
| if err := setFrontmostProcessMac(ctx, pid); err != nil { | ||
| return nil, err | ||
| } | ||
| if previousPID <= 0 { | ||
| return nil, nil | ||
| } | ||
| return func(ctx context.Context) error { | ||
| return setFrontmostProcessMac(ctx, previousPID) | ||
| }, nil | ||
| } | ||
|
|
||
| func readFrontmostProcessIDMac(ctx context.Context) int { | ||
| output, err := exec.CommandContext(ctx, "osascript", "-e", `tell application "System Events" to get unix id of first process whose frontmost is true`).Output() | ||
| if err != nil { | ||
| return 0 | ||
| } | ||
| pid, err := strconv.Atoi(strings.TrimSpace(string(output))) | ||
| if err != nil { | ||
| return 0 | ||
| } | ||
| return pid | ||
| } | ||
|
|
||
| func setFrontmostProcessMac(ctx context.Context, pid int) error { | ||
| script := fmt.Sprintf(`tell application "System Events" to set frontmost of (first process whose unix id is %d) to true`, pid) | ||
| return exec.CommandContext(ctx, "osascript", "-e", script).Run() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| $ErrorActionPreference = 'Stop' | ||
| Add-Type -TypeDefinition @" | ||
| using System; | ||
| using System.Runtime.InteropServices; | ||
| public static class Win32Interop { | ||
| [DllImport("user32.dll")] public static extern bool SetForegroundWindow(IntPtr hWnd); | ||
| [DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow); | ||
| } | ||
| "@ | ||
| try { $process = Get-Process -Id {{PID}} -ErrorAction Stop } catch { throw 'Unity process was not found: {{PID}}' } | ||
| $handle = $process.MainWindowHandle | ||
| if ($handle -eq 0) { throw 'Unity process has no main window handle: {{PID}}' } | ||
| $shown = [Win32Interop]::ShowWindowAsync($handle, 9) | ||
| if (-not $shown) { throw 'Failed to show Unity window' } | ||
| $focused = [Win32Interop]::SetForegroundWindow($handle) | ||
| if (-not $focused) { | ||
| $shell = New-Object -ComObject WScript.Shell | ||
| $focused = $shell.AppActivate({{PID}}) | ||
| } | ||
| if (-not $focused) { throw 'Failed to focus Unity window' } |
23 changes: 23 additions & 0 deletions
23
cli/common/unityprocess/focus_unity_process_with_restore.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| $ErrorActionPreference = 'Stop' | ||
| Add-Type -TypeDefinition @" | ||
| using System; | ||
| using System.Runtime.InteropServices; | ||
| public static class Win32Interop { | ||
| [DllImport("user32.dll")] public static extern IntPtr GetForegroundWindow(); | ||
| [DllImport("user32.dll")] public static extern bool SetForegroundWindow(IntPtr hWnd); | ||
| [DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow); | ||
| } | ||
| "@ | ||
| $previous = [Win32Interop]::GetForegroundWindow() | ||
| try { $process = Get-Process -Id {{PID}} -ErrorAction Stop } catch { throw 'Unity process was not found: {{PID}}' } | ||
| $handle = $process.MainWindowHandle | ||
| if ($handle -eq 0) { throw 'Unity process has no main window handle: {{PID}}' } | ||
| $shown = [Win32Interop]::ShowWindowAsync($handle, 9) | ||
| if (-not $shown) { throw 'Failed to show Unity window' } | ||
| $focused = [Win32Interop]::SetForegroundWindow($handle) | ||
| if (-not $focused) { | ||
| $shell = New-Object -ComObject WScript.Shell | ||
| $focused = $shell.AppActivate({{PID}}) | ||
| } | ||
| if (-not $focused) { throw 'Failed to focus Unity window' } | ||
| Write-Output $previous.ToInt64() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| //go:build !darwin && !windows | ||
|
|
||
| package unityprocess | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "runtime" | ||
| ) | ||
|
|
||
| func FocusUnityProcess(context.Context, int) error { | ||
| return fmt.Errorf("focus-window is not supported on %s", runtime.GOOS) | ||
| } | ||
|
|
||
| func FocusUnityProcessWithRestore(context.Context, int) (RestoreFocusFunc, error) { | ||
| return nil, fmt.Errorf("focus-window is not supported on %s", runtime.GOOS) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| //go:build windows | ||
|
|
||
| package unityprocess | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "os/exec" | ||
| ) | ||
|
|
||
| func FocusUnityProcess(ctx context.Context, pid int) error { | ||
| script := buildFocusUnityProcessWindowsScript(pid) | ||
| stderr := bytes.Buffer{} | ||
| command := exec.CommandContext(ctx, windowsPowerShellCommand, "-NoProfile", "-Command", script) | ||
| command.Stderr = &stderr | ||
| if err := command.Run(); err != nil { | ||
| return commandErrorWithStderr(err, stderr.String()) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func FocusUnityProcessWithRestore(ctx context.Context, pid int) (RestoreFocusFunc, error) { | ||
| script := buildFocusUnityProcessWindowsWithRestoreScript(pid) | ||
| stderr := bytes.Buffer{} | ||
| command := exec.CommandContext(ctx, windowsPowerShellCommand, "-NoProfile", "-Command", script) | ||
| command.Stderr = &stderr | ||
| output, err := command.Output() | ||
| if err != nil { | ||
| return nil, commandErrorWithStderr(err, stderr.String()) | ||
| } | ||
| previousHandle := parseWindowsForegroundHandle(string(output)) | ||
| if previousHandle == 0 { | ||
| return nil, nil | ||
| } | ||
| return func(ctx context.Context) error { | ||
| return restoreWindowsForegroundWindow(ctx, previousHandle) | ||
| }, nil | ||
| } | ||
|
|
||
| func restoreWindowsForegroundWindow(ctx context.Context, handle int64) error { | ||
| script := buildRestoreWindowsForegroundWindowScript(handle) | ||
| stderr := bytes.Buffer{} | ||
| command := exec.CommandContext(ctx, windowsPowerShellCommand, "-NoProfile", "-Command", script) | ||
| command.Stderr = &stderr | ||
| if err := command.Run(); err != nil { | ||
| return commandErrorWithStderr(err, stderr.String()) | ||
| } | ||
| return nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.