Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #145 +/- ##
==========================================
+ Coverage 89.74% 90.05% +0.31%
==========================================
Files 31 32 +1
Lines 5139 5181 +42
Branches 5139 5181 +42
==========================================
+ Hits 4612 4666 +54
+ Misses 427 414 -13
- Partials 100 101 +1
🚀 New features to boost your workflow:
|
|
Thanks for the PR! I think there is a potential pitfall here: On the first pass, the returned rect will be (0,0,0,0), and additional passes aren't triggered automatically, so I think there are a few ways users might unexpectedly not get any actual values out of this. Knowing the dimensions on the first pass is impossible, and automatically triggering additional passes when the rect changes might introduce another pitfall – an easy way to create infinite/busy rendering loops depending on how the rect is used. A step forward might be to emphasize in the documentation that this hook provides the rect for the previous pass, if there was one. However, there's still a shortcoming of the hook: let r: Rect = hooks.use_component_rect();
hooks.use_terminal_events(move |event| {
// If you use `r` here, it might not be the correct rectangle for the most recent render!
// For example, if only one render pass has been done, `r` will be (0, 0, 0, 0).
});To allow closures to reliably get the value for the latest render pass, a reference type, maybe leveraging the new UseRef API, might be better. So maybe this looks like... let r: Ref<Rect> = hooks.use_component_rect();
hooks.use_terminal_events(move |event| {
// `r.get()` is always the up-to-date value here!
}); |
|
I think I got it ? I'm not sure I used the new |
|
Yep, this looks like it'll do the trick! I'm going to leave this open for a bit though and see if any better ideas for approaching this problem come to me. Of course in the meantime, anyone reading this can copy/paste the hook into your own codebase if needed. |
|
Since it returns the position / size from the render of the previous frame, would it not be better to be explicit about that and return |
That's a good idea. I rewrote it to return |
What It Does
Add a new
UseComponentRectwithhooks.use_component_rect(). Works similar touse_terminal_size, but for the current component: returns component's rect from the previous rendering orNonefor the first rendering.Related Issues