Skip to content

Commit

Permalink
dock: Fix toggle button check will stack overflow bug. (#312)
Browse files Browse the repository at this point in the history
Continue #297 #309

```
thread 'main' has overflowed its stack
fatal runtime error: stack overflow
[1]    45068 abort      cargo run
```
  • Loading branch information
huacnlee authored Oct 4, 2024
1 parent 3df7556 commit e1e66e8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
26 changes: 20 additions & 6 deletions crates/ui/src/dock/stack_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,40 +279,54 @@ impl StackPanel {
}

/// Check if the given panel is at the first top left in the stack.
pub(super) fn is_top_left_panel(&self, panel: View<TabPanel>, cx: &AppContext) -> bool {
pub(super) fn is_top_left_panel(
&self,
panel: View<TabPanel>,
check_parent: bool,
cx: &AppContext,
) -> bool {
let first_panel = self.panels.first();

if let Some(parent) = &self.parent {
return parent.read(cx).is_top_left_panel(panel, cx);
if check_parent {
return parent.read(cx).is_top_left_panel(panel, true, cx);
}
}

if let Some(view) = first_panel {
if let Ok(view) = view.view().downcast::<TabPanel>() {
return view.entity_id() == panel.entity_id();
} else if let Ok(view) = view.view().downcast::<Self>() {
return view.read(cx).is_top_left_panel(panel, cx);
return view.read(cx).is_top_left_panel(panel, false, cx);
}
}
false
}

/// Check if the given panel is at the first top right in the stack.
pub(super) fn is_top_right_panel(&self, panel: View<TabPanel>, cx: &AppContext) -> bool {
pub(super) fn is_top_right_panel(
&self,
panel: View<TabPanel>,
check_parent: bool,
cx: &AppContext,
) -> bool {
let first_panel = if self.axis.is_vertical() {
self.panels.first()
} else {
self.panels.last()
};

if let Some(parent) = &self.parent {
return parent.read(cx).is_top_right_panel(panel, cx);
if check_parent {
return parent.read(cx).is_top_right_panel(panel, true, cx);
}
}

if let Some(view) = first_panel {
if let Ok(view) = view.view().downcast::<TabPanel>() {
return view.entity_id() == panel.entity_id();
} else if let Ok(view) = view.view().downcast::<Self>() {
return view.read(cx).is_top_right_panel(panel, cx);
return view.read(cx).is_top_right_panel(panel, false, cx);
}
}

Expand Down
10 changes: 8 additions & 2 deletions crates/ui/src/dock/tab_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,10 @@ impl TabPanel {
}

if let Some(parent) = self.stack_panel.as_ref() {
if !parent.read(cx).is_top_left_panel(cx.view().clone(), cx) {
if !parent
.read(cx)
.is_top_left_panel(cx.view().clone(), true, cx)
{
return None;
}
}
Expand All @@ -381,7 +384,10 @@ impl TabPanel {
}

if let Some(parent) = self.stack_panel.as_ref() {
if !parent.read(cx).is_top_right_panel(cx.view().clone(), cx) {
if !parent
.read(cx)
.is_top_right_panel(cx.view().clone(), true, cx)
{
return None;
}
}
Expand Down

0 comments on commit e1e66e8

Please sign in to comment.