Repository navigation
fix(desktop): preserve screenshot detail in scaled previews #9916
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
Open
Gigioxx
wants to merge
4
commits into
pingdotgg:main
Choose a base branch
from
Gigioxx:t3code/fix-issue-9872
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+130
−12
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
17f755a
fix(desktop): preserve screenshot detail in scaled previews
Gigioxx 77cd3ea
chore(desktop): drop the one-off preview capture smoke script
Gigioxx c370d27
fix(desktop): merge main and allow scaled preview capture setup
Gigioxx 58bf6d3
chore(desktop): sync latest main
Gigioxx 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
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,81 @@ | ||
| import * as Effect from "effect/Effect"; | ||
| import * as Schema from "effect/Schema"; | ||
| import * as Semaphore from "effect/Semaphore"; | ||
|
|
||
| const encodeString = Schema.encodeSync(Schema.fromJsonString(Schema.String)); | ||
| let captureSequence = 0; | ||
| const captureLocks = new WeakMap<Electron.WebContents, Semaphore.Semaphore>(); | ||
|
|
||
| // Chromium rasterizes CSS-scaled guests at their displayed size, even for capturePage. | ||
| // Freeze only that preview while capturing unscaled pixels; never change its viewport. | ||
| export const capturePreviewPage = (tabId: string, wc: Electron.WebContents) => { | ||
| let lock = captureLocks.get(wc); | ||
| if (!lock) { | ||
| lock = Semaphore.makeUnsafe(1); | ||
| captureLocks.set(wc, lock); | ||
| } | ||
| const capture = Effect.tryPromise((_signal) => wc.capturePage()); | ||
| const unscaledCapture = capture.pipe(Effect.timeout("1 second")); | ||
| return lock | ||
| .withPermit( | ||
| Effect.gen(function* () { | ||
| const host = wc.hostWebContents; | ||
| if (!host || host.isDestroyed()) return yield* unscaledCapture; | ||
| const scaled = yield* Effect.tryPromise((_signal) => | ||
| host.executeJavaScript(`(() => { | ||
| const view = document.querySelector("webview[data-preview-tab=" + CSS.escape(${encodeString(tabId)}) + "]"); | ||
| return !!view && getComputedStyle(view).transform !== "none"; | ||
| })()`), | ||
| ); | ||
| if (scaled !== true) return yield* unscaledCapture; | ||
| const frozen = yield* Effect.tryPromise((_signal) => wc.capturePage()); | ||
| const token = `preview-capture-${++captureSequence}`; | ||
| return yield* Effect.tryPromise((_signal) => | ||
| host.executeJavaScript(`(async () => { | ||
| const view = document.querySelector("webview[data-preview-tab=" + CSS.escape(${encodeString(tabId)}) + "]"); | ||
| if (!view || view.getWebContentsId() !== ${wc.id}) throw new Error("Preview guest detached before capture"); | ||
| const frozen = document.createElement("img"); | ||
| frozen.src = ${encodeString(frozen.toDataURL())}; | ||
| frozen.alt = ""; | ||
| frozen.className = view.className; | ||
| frozen.style.cssText = view.style.cssText; | ||
| frozen.style.pointerEvents = "none"; | ||
| frozen.style.maxWidth = "none"; | ||
| const holder = document.createElement("span"); | ||
| holder.id = ${encodeString(token)}; | ||
| holder.style.display = "contents"; | ||
| const style = document.createElement("style"); | ||
| style.textContent = 'webview[data-preview-tab="' + CSS.escape(view.dataset.previewTab) + '"] { transform: none !important; opacity: 0 !important; pointer-events: none !important; }'; | ||
| holder.append(frozen); | ||
| view.after(holder); | ||
| await frozen.decode(); | ||
| if (holder.isConnected && view.isConnected) holder.append(style); | ||
| })()`), | ||
| ).pipe( | ||
| Effect.andThen( | ||
| Effect.tryPromise((_signal) => | ||
| host.executeJavaScript( | ||
| "new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)))", | ||
| ), | ||
| ), | ||
| ), | ||
| Effect.andThen(capture), | ||
| Effect.ensuring( | ||
| Effect.suspend(() => | ||
| host.isDestroyed() | ||
| ? Effect.void | ||
| : Effect.tryPromise((_signal) => | ||
| host.executeJavaScript( | ||
| `document.getElementById(${encodeString(token)})?.remove()`, | ||
| ), | ||
| ).pipe(Effect.timeout("1 second"), Effect.ignore), | ||
| ), | ||
| ), | ||
| ); | ||
| }), | ||
| ) | ||
| .pipe( | ||
| // Bound queueing and overlay setup as well as both scaled captures. | ||
| Effect.timeout("5 seconds"), | ||
| ); | ||
| }; | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: pingdotgg/t3code
Length of output: 22737
🏁 Script executed:
Repository: pingdotgg/t3code
Length of output: 24050
Retry overlay cleanup while the host remains alive.
If the live host's
executeJavaScriptcleanup call rejects or exceeds the one-second timeout,Effect.ignoresuppresses the failure. The holder and itsopacity: 0 !importantstyle can then remain, leaving the preview covered and the guest hidden.capturePageWithRetrycannot recover this case because the cleanup failure is not propagated.Suggested fix
📝 Committable suggestion
🤖 Prompt for AI Agents