resizable: Fix the bug for parent groups when initializing Panels with sizes Some and None (#356)

Optimization of https://github.com/longbridgeapp/gpui-component/pull/329

The issue with the initial width of the Panel has been optimised to
ensure that the left side of a Panel combination like [Some(px(510.)),
None] is initially rendered with a width of 510px.

---------

Co-authored-by: Jason Lee <huacnlee@gmail.com>
This commit is contained in:
xda 2024-10-17 18:39:00 +09:00 committed by GitHub
parent 8eff75d357
commit 913775c632
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -351,7 +351,8 @@ impl FluentBuilder for ResizablePanel {}
impl Render for ResizablePanel {
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
let view = cx.view().clone();
let total_size = self.group.as_ref().map(|group| group.read(cx).total_size());
div()
.flex()
.flex_grow()
@ -366,9 +367,16 @@ impl Render for ResizablePanel {
this.when(self.size.is_none(), |this| this.flex_shrink_0())
.flex_basis(size)
})
.when_some(self.size_ratio, |this, size_ratio| {
this.flex_basis(relative(size_ratio))
})
.map(
|this| match (self.size_ratio, self.size, total_size) {
(Some(size_ratio), _, _) => this.flex_basis(relative(size_ratio)),
(None, Some(size), Some(total_size)) => {
this.flex_basis(relative(size / total_size))
}
(None, Some(size), None) => this.flex_basis(size),
_ => this,
},
)
.child({
canvas(
move |bounds, cx| view.update(cx, |r, cx| r.update_size(bounds, cx)),