mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-16 05:01:03 +03:00
fix(desktop): keep browser panel corner masks aligned (#52461)
Co-authored-by: LukeParkerDev <10430890+Hona@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
Unverified
parent
a2e3dd2884
commit
b5c43a4409
@@ -48,6 +48,18 @@ function EmbedView(props: EmbedProps & { input: Input; bridge: Bridge }) {
|
||||
canvas.width = canvas.height = 1
|
||||
const paint = canvas.getContext("2d", { willReadFrequently: true })
|
||||
|
||||
// Let the browser resolve colors, including custom themes using color formats
|
||||
// that Electron's color parser cannot read.
|
||||
const resolve = (color: string) => {
|
||||
if (!paint) return undefined
|
||||
paint.clearRect(0, 0, 1, 1)
|
||||
paint.fillStyle = color
|
||||
paint.fillRect(0, 0, 1, 1)
|
||||
const data = paint.getImageData(0, 0, 1, 1).data
|
||||
|
||||
return [data[0], data[1], data[2], data[3]] as const
|
||||
}
|
||||
|
||||
const hide = () => {
|
||||
if (placed) props.bridge.embed(placed)
|
||||
placed = undefined
|
||||
@@ -135,20 +147,16 @@ function EmbedView(props: EmbedProps & { input: Input; bridge: Bridge }) {
|
||||
props.background ??
|
||||
getComputedStyle(element.closest(".bg-v2-background-bg-deep") ?? document.documentElement).backgroundColor
|
||||
|
||||
const next = `${id}:${visible}:${left}:${top}:${right}:${bottom}:${color}:${window.devicePixelRatio}`
|
||||
const ring = props.radius ? cardRing(element) : undefined
|
||||
const viewport = { width: Math.round(window.innerWidth * zoom), height: Math.round(window.innerHeight * zoom) }
|
||||
|
||||
const next = `${id}:${visible}:${left}:${top}:${right}:${bottom}:${viewport.width}:${viewport.height}:${color}:${ring?.color}:${ring?.width}:${window.devicePixelRatio}`
|
||||
|
||||
if (next === layout) return
|
||||
layout = next
|
||||
|
||||
// Let the browser resolve the color, including custom themes using color formats
|
||||
// that Electron's color parser cannot read.
|
||||
if (paint) {
|
||||
paint.clearRect(0, 0, 1, 1)
|
||||
paint.fillStyle = color
|
||||
paint.fillRect(0, 0, 1, 1)
|
||||
}
|
||||
|
||||
const rgba = paint?.getImageData(0, 0, 1, 1).data
|
||||
const background = resolve(color)
|
||||
const border = ring && resolve(ring.color)
|
||||
|
||||
if (placed !== id) hide()
|
||||
placed = id
|
||||
@@ -156,10 +164,16 @@ function EmbedView(props: EmbedProps & { input: Input; bridge: Bridge }) {
|
||||
const box = {
|
||||
visible,
|
||||
bounds: { x: left, y: top, width: Math.max(0, right - left), height: Math.max(0, bottom - top) },
|
||||
viewport,
|
||||
radius: Math.round((props.radius ?? 0) * zoom),
|
||||
}
|
||||
|
||||
props.bridge.embed(id, rgba ? { ...box, background: [rgba[0], rgba[1], rgba[2], rgba[3]] as const } : box)
|
||||
const backed = background ? { ...box, background } : box
|
||||
|
||||
props.bridge.embed(
|
||||
id,
|
||||
ring && border && border[3] > 0 ? { ...backed, border: { color: border, width: ring.width * zoom } } : backed,
|
||||
)
|
||||
}
|
||||
|
||||
const tick = () => {
|
||||
@@ -241,3 +255,23 @@ function EmbedView(props: EmbedProps & { input: Input; bridge: Bridge }) {
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
// The hairline ring (a zero-offset, zero-blur, spread-only shadow) of the nearest card around the box.
|
||||
// The corner masks sit where it curves, so they redraw it.
|
||||
function cardRing(element: HTMLElement) {
|
||||
for (let node = element.parentElement; node; node = node.parentElement) {
|
||||
const shadow = getComputedStyle(node).boxShadow
|
||||
|
||||
if (shadow === "none") continue
|
||||
|
||||
return shadow
|
||||
.split(/,(?![^(]*\))/)
|
||||
.map((layer) => layer.trim().match(/^(.+?)\s+(-?[\d.]+)px\s+(-?[\d.]+)px\s+([\d.]+)px\s+([\d.]+)px$/))
|
||||
.flatMap((match) =>
|
||||
match && +match[2] === 0 && +match[3] === 0 && +match[4] === 0 && +match[5] > 0
|
||||
? [{ color: match[1], width: +match[5] }]
|
||||
: [],
|
||||
)
|
||||
.at(0)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,28 +20,45 @@ type Entry = {
|
||||
readonly listeners: Set<(visible: boolean) => void>
|
||||
}
|
||||
|
||||
// A box edge this many CSS px from the window edge follows it while the renderer's layout catches up.
|
||||
const WINDOW_GUTTER = 24
|
||||
|
||||
/**
|
||||
* The web pages main extensions hand to the host. Each stays hidden until its window's renderer lays
|
||||
* it out and the extension shows it; the renderer's bounds already include the window zoom.
|
||||
*/
|
||||
export function createEmbeds() {
|
||||
const entries = new Map<string, Entry>()
|
||||
const resizing = new WeakSet<BrowserWindow>()
|
||||
|
||||
const apply = (entry: Entry) => {
|
||||
if (entry.window.isDestroyed()) return
|
||||
const layout = entry.layout?.bounds
|
||||
const viewport = entry.layout?.viewport
|
||||
// The renderer measures a frame or more behind a window resize. Edges beside the window
|
||||
// edge follow it now, so the view and its corner masks do not trail inside the panel.
|
||||
// This assumes the box takes the whole change, as the side panel beside the fixed-width chat does.
|
||||
const [width = 0, height = 0] = entry.window.getContentSize()
|
||||
const zoom = entry.window.webContents.getZoomFactor()
|
||||
|
||||
// The renderer's viewport is whole CSS px, so it can be off by one CSS px plus rounding; that is not a resize.
|
||||
const follow = (inset: number, change: number) =>
|
||||
inset <= WINDOW_GUTTER * zoom && Math.abs(change) >= zoom + 1 ? change : 0
|
||||
|
||||
const dx = layout && viewport ? follow(viewport.width - layout.x - layout.width, width - viewport.width) : 0
|
||||
const dy = layout && viewport ? follow(viewport.height - layout.y - layout.height, height - viewport.height) : 0
|
||||
|
||||
// Renderer measurements are fractional; native views take whole DIPs.
|
||||
const bounds = layout && {
|
||||
x: Math.round(layout.x),
|
||||
y: Math.round(layout.y),
|
||||
width: Math.round(layout.width),
|
||||
height: Math.round(layout.height),
|
||||
width: Math.round(layout.width + dx),
|
||||
height: Math.round(layout.height + dy),
|
||||
}
|
||||
|
||||
const visible = !!entry.layout?.visible && entry.shown && !!bounds && bounds.width > 0 && bounds.height > 0
|
||||
const placed = !!entry.layout?.visible && !!bounds && bounds.width > 0 && bounds.height > 0
|
||||
|
||||
if (visible && bounds) {
|
||||
if (placed && bounds) {
|
||||
entry.view.setBounds(bounds)
|
||||
|
||||
const size = Math.min(
|
||||
@@ -51,28 +68,34 @@ export function createEmbeds() {
|
||||
)
|
||||
|
||||
const background = entry.layout?.background
|
||||
const border = entry.layout?.border
|
||||
const scale = screen.getDisplayMatching(entry.window.getBounds()).scaleFactor
|
||||
const key = background && size > 0 ? `${background}:${size}:${scale}` : ""
|
||||
|
||||
const key =
|
||||
background && size > 0 ? `${background}:${size}:${scale}:${border?.color ?? ""}:${border?.width ?? ""}` : ""
|
||||
|
||||
if (background && key && key !== entry.cornerKey)
|
||||
createCornerImages(background, size, scale).forEach((image, index) => entry.corners[index]?.setImage(image))
|
||||
createCornerImages(background, size, scale, border).forEach((image, index) =>
|
||||
entry.corners[index]?.setImage(image),
|
||||
)
|
||||
entry.cornerKey = key
|
||||
// Plain bounds apply in the same frame as the view's; animated ones land a frame later,
|
||||
// which exposes a square page corner whenever the panel moves.
|
||||
entry.corners.forEach((corner, index) =>
|
||||
// A composited layer is required above a WebContentsView; a zero-duration update creates it.
|
||||
corner.setBounds(
|
||||
{
|
||||
x: bounds.x + (index ? bounds.width - size : 0),
|
||||
y: bounds.y + bounds.height - size,
|
||||
width: size,
|
||||
height: size,
|
||||
},
|
||||
{ animate: { duration: 0 } },
|
||||
),
|
||||
corner.setBounds({
|
||||
x: bounds.x + (index ? bounds.width - size : 0),
|
||||
y: bounds.y + bounds.height - size,
|
||||
width: size,
|
||||
height: size,
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
// Keep the corner layers up while the box is laid out, even before the page shows.
|
||||
// Over the DOM card they match its own rounded corners.
|
||||
const visible = placed && entry.shown
|
||||
entry.corners.forEach((corner) => corner.setVisible(placed && !!entry.cornerKey))
|
||||
entry.view.setVisible(visible)
|
||||
entry.corners.forEach((corner) => corner.setVisible(visible && !!entry.cornerKey))
|
||||
|
||||
if (visible === entry.onscreen) return
|
||||
entry.onscreen = visible
|
||||
@@ -101,10 +124,19 @@ export function createEmbeds() {
|
||||
return {
|
||||
create(owner: Instance, view: WebContentsView, window: BrowserWindow): Embed {
|
||||
const id = randomUUID()
|
||||
|
||||
if (!resizing.has(window)) {
|
||||
resizing.add(window)
|
||||
window.on("resize", () => entries.forEach((entry) => entry.window === window && apply(entry)))
|
||||
}
|
||||
|
||||
const corners = [new ImageView(), new ImageView()]
|
||||
view.setVisible(false)
|
||||
window.contentView.addChildView(view)
|
||||
corners.forEach((corner) => {
|
||||
// Painting above a WebContentsView needs a composited layer; a zero blur creates one
|
||||
// without an animated bounds update.
|
||||
corner.setBackgroundBlur(0)
|
||||
corner.setVisible(false)
|
||||
window.contentView.addChildView(corner)
|
||||
})
|
||||
|
||||
@@ -1,9 +1,19 @@
|
||||
import { nativeImage } from "electron"
|
||||
|
||||
type Color = readonly [number, number, number, number]
|
||||
|
||||
// Native browser surfaces ignore a parent View's clip path. Cover only the
|
||||
// pixels outside the bottom arcs; never resize or style the page itself.
|
||||
export function createCornerImages(color: readonly [number, number, number, number], radius: number, scale: number) {
|
||||
// The card's ring sits just outside the arc, inside these masks, so draw it here.
|
||||
export function createCornerImages(
|
||||
color: Color,
|
||||
radius: number,
|
||||
scale: number,
|
||||
border?: { readonly color: Color; readonly width: number },
|
||||
) {
|
||||
const size = Math.max(1, Math.round(radius * scale))
|
||||
const ring = (border?.width ?? 0) * scale
|
||||
const coverage = (edge: number, distance: number) => Math.max(0, Math.min(1, edge - distance + 0.5))
|
||||
|
||||
return [false, true].map((right) => {
|
||||
const pixels = Buffer.alloc(size * size * 4)
|
||||
@@ -11,13 +21,19 @@ export function createCornerImages(color: readonly [number, number, number, numb
|
||||
for (let y = 0; y < size; y++) {
|
||||
for (let x = 0; x < size; x++) {
|
||||
const distance = Math.hypot(right ? x + 0.5 : size - x - 0.5, y + 0.5)
|
||||
const alpha = Math.round(color[3] * Math.max(0, Math.min(1, distance - size + 0.5)))
|
||||
const inside = coverage(size, distance)
|
||||
const base = ((1 - inside) * color[3]) / 255
|
||||
const line = border ? ((coverage(size + ring, distance) - inside) * border.color[3]) / 255 : 0
|
||||
|
||||
const channel = (index: number) =>
|
||||
Math.round((border ? border.color[index] : 0) * line + color[index] * base * (1 - line))
|
||||
|
||||
const offset = (y * size + x) * 4
|
||||
// NativeImage bitmaps use premultiplied BGRA on supported desktop platforms.
|
||||
pixels[offset] = Math.round((color[2] * alpha) / 255)
|
||||
pixels[offset + 1] = Math.round((color[1] * alpha) / 255)
|
||||
pixels[offset + 2] = Math.round((color[0] * alpha) / 255)
|
||||
pixels[offset + 3] = alpha
|
||||
pixels[offset] = channel(2)
|
||||
pixels[offset + 1] = channel(1)
|
||||
pixels[offset + 2] = channel(0)
|
||||
pixels[offset + 3] = Math.round(255 * (line + base * (1 - line)))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -38,16 +38,22 @@ export const ExtensionFailure = Schema.Struct({ code: ExtensionErrorCode, messag
|
||||
export type ExtensionFailure = typeof ExtensionFailure.Type
|
||||
|
||||
// Window DIPs from the renderer, zoom applied. Background is RGBA with every channel 0-255; corners
|
||||
// in that color mask the view's bottom edge to `radius`.
|
||||
// in that color mask the view's bottom edge to `radius`, redrawing the card's `border` ring along the arc.
|
||||
const channel = Schema.Finite.check(Schema.isBetween({ minimum: 0, maximum: 255 }))
|
||||
|
||||
const rgba = Schema.Tuple([channel, channel, channel, channel])
|
||||
|
||||
export const ExtensionLayout = Schema.Struct({
|
||||
visible: Schema.Boolean,
|
||||
bounds: Schema.optionalKey(
|
||||
Schema.Struct({ x: Schema.Finite, y: Schema.Finite, width: Schema.Finite, height: Schema.Finite }),
|
||||
),
|
||||
background: Schema.optionalKey(Schema.Tuple([channel, channel, channel, channel])),
|
||||
viewport: Schema.optionalKey(Schema.Struct({ width: Schema.Finite, height: Schema.Finite })),
|
||||
background: Schema.optionalKey(rgba),
|
||||
radius: Schema.optionalKey(Schema.Finite.check(Schema.isBetween({ minimum: 0, maximum: 100 }))),
|
||||
border: Schema.optionalKey(
|
||||
Schema.Struct({ color: rgba, width: Schema.Finite.check(Schema.isBetween({ minimum: 0, maximum: 10 })) }),
|
||||
),
|
||||
})
|
||||
|
||||
export const ExtensionEndpoint = Schema.Struct({
|
||||
|
||||
@@ -45,10 +45,24 @@ export interface BridgeLayout {
|
||||
/** Height. */
|
||||
readonly height: number
|
||||
}
|
||||
/** The window's content size the bounds were measured against. */
|
||||
readonly viewport?: {
|
||||
/** Content width in window pixels. */
|
||||
readonly width: number
|
||||
/** Content height in window pixels. */
|
||||
readonly height: number
|
||||
}
|
||||
/** RGBA of the backdrop the rounded corners show. */
|
||||
readonly background?: readonly [number, number, number, number]
|
||||
/** Radius of the bottom corners. */
|
||||
readonly radius?: number
|
||||
/** The rounded card's hairline ring, which the corner masks would otherwise paint over. */
|
||||
readonly border?: {
|
||||
/** RGBA of the ring. */
|
||||
readonly color: readonly [number, number, number, number]
|
||||
/** Ring width in window pixels. */
|
||||
readonly width: number
|
||||
}
|
||||
}
|
||||
|
||||
/** An extension as the extension manager lists it. */
|
||||
|
||||
Reference in New Issue
Block a user