Repository navigation
feat: Popover——锚在父节点上的浮动卡片,不抢焦点、放不下自动翻转 #48
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
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
2 changes: 1 addition & 1 deletion
2
compose/src/commonMain/kotlin/app/tinyui/generated/RuntimeBytecode.kt
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
53 changes: 53 additions & 0 deletions
53
compose/src/commonTest/kotlin/app/tinyui/components/PopoverOffsetTest.kt
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,53 @@ | ||
| package app.tinyui.components | ||
|
|
||
| import androidx.compose.ui.unit.IntOffset | ||
| import androidx.compose.ui.unit.IntRect | ||
| import androidx.compose.ui.unit.IntSize | ||
| import kotlin.test.Test | ||
| import kotlin.test.assertEquals | ||
|
|
||
| class PopoverOffsetTest { | ||
| private val window = IntSize(400, 800) | ||
| // a field across the middle of the screen, 16 px in from each side | ||
| private val field = IntRect(16, 300, 384, 356) | ||
| private val card = IntSize(368, 200) | ||
|
|
||
| @Test | ||
| fun abovePutsTheBottomEdgeAGapOverTheAnchor() = | ||
| assertEquals(IntOffset(16, 92), popoverOffset(field, window, card, above = true, gap = 8, rtl = false)) | ||
|
|
||
| @Test | ||
| fun belowPutsTheTopEdgeAGapUnderTheAnchor() = | ||
| assertEquals(IntOffset(16, 364), popoverOffset(field, window, card, above = false, gap = 8, rtl = false)) | ||
|
|
||
| @Test | ||
| fun flipsWhenThePreferredSideHasNoRoom() { | ||
| val nearTop = IntRect(16, 100, 384, 156) | ||
| assertEquals(IntOffset(16, 164), popoverOffset(nearTop, window, card, above = true, gap = 8, rtl = false)) | ||
| val nearBottom = IntRect(16, 600, 384, 656) | ||
| assertEquals(IntOffset(16, 392), popoverOffset(nearBottom, window, card, above = false, gap = 8, rtl = false)) | ||
| } | ||
|
|
||
| @Test | ||
| fun fitsNeitherSideTakesTheRoomierOneAndStaysInTheWindow() { | ||
| val tall = IntSize(368, 500) | ||
| // 300 above vs 444 below: below, pushed up so the bottom edge stays on screen | ||
| assertEquals(IntOffset(16, 300), popoverOffset(field, window, tall, above = true, gap = 8, rtl = false)) | ||
| } | ||
|
|
||
| @Test | ||
| fun startEdgesAlignAndNeverLeaveTheWindow() { | ||
| val narrow = IntSize(100, 50) | ||
| assertEquals(IntOffset(284, 242), popoverOffset(field, window, narrow, above = true, gap = 8, rtl = true)) | ||
| val pastRightEdge = IntRect(350, 300, 390, 340) | ||
| assertEquals(IntOffset(300, 242), popoverOffset(pastRightEdge, window, narrow, above = true, gap = 8, rtl = false)) | ||
| } | ||
|
|
||
| @Test | ||
| fun aFullWidthAnchorGetsAFullWidthCardFlushWithTheEdges() { | ||
| // Popup measures its content against the window, so the card is never wider than the window | ||
| val edgeToEdge = IntRect(0, 300, 400, 356) | ||
| assertEquals(IntOffset(0, 92), popoverOffset(edgeToEdge, window, IntSize(400, 200), above = true, gap = 8, rtl = false)) | ||
| assertEquals(IntOffset(0, 92), popoverOffset(edgeToEdge, window, IntSize(400, 200), above = true, gap = 8, rtl = true)) | ||
| } | ||
| } |
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
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.
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.
[建议]
matchAnchorWidth时popupContentSize包含Surface外侧的 8dp padding,宽度会比锚点多 16dp;但这里将外层窗口的 x 限制为非负。若锚点本身铺满窗口,外层 x 会被钳到 0,而实际Surface从 x=8 开始、仍保持锚点宽度,右侧会越过窗口 8dp 并被 Popup 裁切,违背两轴不出窗口。应在计算中按实际卡片边界处理阴影边距(例如允许外层为负边距,或在边缘缩减/调整阴影留白),并补充全宽锚点的测试。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.
已修(6c94794)。根因是卡片外那圈 8dp 阴影边距:当初以为阴影会被 Popup 窗口裁掉才加的,双端实测不留边距阴影也完整,于是直接去掉。现在 popup 内容就是卡片本身,全宽锚点时卡片与锚点同宽、x 为 0,不再越出窗口;补了
aFullWidthAnchorGetsAFullWidthCardFlushWithTheEdges(LTR / RTL)。