Repository navigation
fix: elements at fractional positions drawing on the wrong cells #168
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
dreyfus92
wants to merge
4
commits into
main
Choose a base branch
from
fix/fractional-cell-coverage
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.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
767b675
docs(spec): define cell coverage for fractional layouts
dreyfus92 27bb597
test: cover cell coverage for fractional layouts
dreyfus92 b318602
fix: exclude the bottom and right edges from pointer hit testing
dreyfus92 307b7d0
fix: round fractional coordinates to the nearest cell
dreyfus92 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@bomb.sh/tty": patch | ||
| --- | ||
|
|
||
| Fixes how elements at fractional positions map to cells. An element now draws on, and receives the pointer for, exactly the cells whose centers its bounding box covers. Elements sliding in past the top edge no longer pile onto row 0, and two elements that meet halfway through a cell no longer both receive the pointer there. | ||
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,16 @@ | ||
| Count only the top and left edges of a bounding box as inside it, so a | ||
| point on an edge two elements share hits one of them (renderer-spec 7.11). | ||
|
|
||
| diff --git a/clay.h b/clay.h | ||
| index 7c967bb..8a1dc56 100644 | ||
| --- a/clay.h | ||
| +++ b/clay.h | ||
| @@ -1776,7 +1776,7 @@ Clay__MeasureTextCacheItem *Clay__MeasureTextCached(Clay_String *text, Clay_Text | ||
| } | ||
|
|
||
| bool Clay__PointIsInsideRect(Clay_Vector2 point, Clay_BoundingBox rect) { | ||
| - return point.x >= rect.x && point.x <= rect.x + rect.width && point.y >= rect.y && point.y <= rect.y + rect.height; | ||
| + return point.x >= rect.x && point.x < rect.x + rect.width && point.y >= rect.y && point.y < rect.y + rect.height; | ||
| } | ||
|
|
||
| Clay_LayoutElementHashMapItem* Clay__AddHashMapItem(Clay_ElementId elementId, Clay_LayoutElement* layoutElement) { |
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,100 @@ | ||
| import { describe, expect, it } from "./suite.ts"; | ||
| import { createTerm } from "../term.ts"; | ||
| import { close, fixed, grow, type Op, open, text } from "../ops.ts"; | ||
| import { print } from "./print.ts"; | ||
|
|
||
| const decode = (b: Uint8Array) => new TextDecoder().decode(b); | ||
| const trim = (s: string) => s.split("\n").map((l) => l.trimEnd()).join("\n"); | ||
|
|
||
| const LABELS = ["zero", "one", "two-long", "three"]; | ||
|
|
||
| // a 4-row panel floating at y over a 12×4 grid, one label per row | ||
| function panel(y: number): Op[] { | ||
| return [ | ||
| open("root", { layout: { width: grow(), height: grow() } }), | ||
| open("panel", { | ||
| layout: { width: fixed(10), height: fixed(4), direction: "ttb" }, | ||
| floating: { x: 0, y, attachTo: "root" }, | ||
| }), | ||
| ...LABELS.flatMap((label, i) => [ | ||
| open(`row${i}`, { layout: { width: grow(), height: fixed(1) } }), | ||
| text(label), | ||
| close(), | ||
| ]), | ||
| close(), | ||
| close(), | ||
| ]; | ||
| } | ||
|
|
||
| async function draw(ops: Op[], width: number, height: number) { | ||
| let term = await createTerm({ width, height }); | ||
| return trim(print(decode(term.render(ops).output), width, height)); | ||
| } | ||
|
|
||
| async function over( | ||
| ops: Op[], | ||
| [width, height]: [number, number], | ||
| [x, y]: [number, number], | ||
| ids: string[], | ||
| ) { | ||
| let term = await createTerm({ width, height }); | ||
| return term.render(ops, { pointer: { x, y, down: false } }).events | ||
| .map((event) => event.id) | ||
| .filter((id) => ids.includes(id)); | ||
| } | ||
|
|
||
| describe("cell coverage", () => { | ||
| it("clips the rows of a panel that sit above the top edge", async () => { | ||
| expect(await draw(panel(-2.5), 12, 4)).toEqual( | ||
| ["three", "", "", ""].join("\n"), | ||
| ); | ||
| }); | ||
|
|
||
| it("draws an edge more than halfway into a cell from the next cell", async () => { | ||
| expect(await draw(panel(0.7), 12, 4)).toEqual( | ||
| ["", "zero", "one", "two-long"].join("\n"), | ||
| ); | ||
| }); | ||
|
|
||
| it("draws an edge exactly halfway into a cell from that cell", async () => { | ||
| expect(await draw(panel(0.5), 12, 4)).toEqual( | ||
| ["zero", "one", "two-long", "three"].join("\n"), | ||
| ); | ||
| }); | ||
|
|
||
| it("puts the pointer over the row drawn in each cell", async () => { | ||
| let rows = LABELS.map((_, i) => `row${i}`); | ||
| for (let step = 0; step <= 40; step++) { | ||
| let y = -3 + step / 10; | ||
| let drawn = (await draw(panel(y), 12, 4)).split("\n"); | ||
| for (let row = 0; row < 4; row++) { | ||
| let label = drawn[row].trim(); | ||
| let hit = (await over(panel(y), [12, 4], [1, row], rows)) | ||
| .map((id) => LABELS[rows.indexOf(id)]); | ||
| expect({ y, row, hit }).toEqual({ y, row, hit: label ? [label] : [] }); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| it("gives a cell on a half-cell boundary to one element", async () => { | ||
| let split: Op[] = [ | ||
| open("root", { | ||
| layout: { width: grow(), height: grow(), direction: "ltr" }, | ||
| }), | ||
| open("left", { layout: { width: grow(), height: grow() } }), | ||
| text("L"), | ||
| close(), | ||
| open("right", { layout: { width: grow(), height: grow() } }), | ||
| text("R"), | ||
| close(), | ||
| close(), | ||
| ]; | ||
| expect((await draw(split, 41, 1)).trim()).toEqual(`L${" ".repeat(19)}R`); | ||
| expect(await over(split, [41, 1], [19, 0], ["left", "right"])).toEqual([ | ||
| "left", | ||
| ]); | ||
| expect(await over(split, [41, 1], [20, 0], ["left", "right"])).toEqual([ | ||
| "right", | ||
| ]); | ||
| }); | ||
| }); |
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.
Changeset needs revision.
Changeset package scope does not match the affected packages confidently. Changeset is not sufficiently consumer-focused.
View the proposed replacement