panel: Update Panel toolbar_buttons method to use ViewContext<Self>. (#536)

## Break changes

The `toolbar_buttons` trait method in Panel has been changed:

```diff
- fn toolbar_buttons(&self, cx: &WindowContext) -> Vec<Button>
+ fn toolbar_buttons(&self, cx: &mut ViewContext<Self>) -> Option<Vec<Button>>
```
This commit is contained in:
Jason Lee 2025-01-09 16:49:35 +08:00 committed by GitHub
parent bf82e3db13
commit de42cc46a7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 14 additions and 19 deletions

View file

@ -348,8 +348,8 @@ impl Panel for StoryContainer {
.menu("Info", Box::new(ShowPanelInfo)) .menu("Info", Box::new(ShowPanelInfo))
} }
fn toolbar_buttons(&self, _cx: &WindowContext) -> Vec<Button> { fn toolbar_buttons(&self, _cx: &mut ViewContext<Self>) -> Option<Vec<Button>> {
vec![ Some(vec![
Button::new("info").icon(IconName::Info).on_click(|_, cx| { Button::new("info").icon(IconName::Info).on_click(|_, cx| {
cx.push_notification("You have clicked info button"); cx.push_notification("You have clicked info button");
}), }),
@ -358,7 +358,7 @@ impl Panel for StoryContainer {
.on_click(|_, cx| { .on_click(|_, cx| {
cx.push_notification("You have clicked search button"); cx.push_notification("You have clicked search button");
}), }),
] ])
} }
fn dump(&self, _cx: &AppContext) -> PanelState { fn dump(&self, _cx: &AppContext) -> PanelState {

View file

@ -91,8 +91,8 @@ pub trait Panel: EventEmitter<PanelEvent> + FocusableView {
} }
/// The addition toolbar buttons of the panel used to show in the right of the title bar, default is `None`. /// The addition toolbar buttons of the panel used to show in the right of the title bar, default is `None`.
fn toolbar_buttons(&self, cx: &WindowContext) -> Vec<Button> { fn toolbar_buttons(&self, cx: &mut ViewContext<Self>) -> Option<Vec<Button>> {
vec![] None
} }
/// Dump the panel, used to serialize the panel. /// Dump the panel, used to serialize the panel.
@ -113,7 +113,7 @@ pub trait PanelView: 'static + Send + Sync {
fn set_active(&self, active: bool, cx: &mut WindowContext); fn set_active(&self, active: bool, cx: &mut WindowContext);
fn set_zoomed(&self, zoomed: bool, cx: &mut WindowContext); fn set_zoomed(&self, zoomed: bool, cx: &mut WindowContext);
fn popup_menu(&self, menu: PopupMenu, cx: &WindowContext) -> PopupMenu; fn popup_menu(&self, menu: PopupMenu, cx: &WindowContext) -> PopupMenu;
fn toolbar_buttons(&self, cx: &WindowContext) -> Vec<Button>; fn toolbar_buttons(&self, cx: &mut WindowContext) -> Option<Vec<Button>>;
fn view(&self) -> AnyView; fn view(&self) -> AnyView;
fn focus_handle(&self, cx: &AppContext) -> FocusHandle; fn focus_handle(&self, cx: &AppContext) -> FocusHandle;
fn dump(&self, cx: &AppContext) -> PanelState; fn dump(&self, cx: &AppContext) -> PanelState;
@ -160,8 +160,8 @@ impl<T: Panel> PanelView for View<T> {
self.read(cx).popup_menu(menu, cx) self.read(cx).popup_menu(menu, cx)
} }
fn toolbar_buttons(&self, cx: &WindowContext) -> Vec<Button> { fn toolbar_buttons(&self, cx: &mut WindowContext) -> Option<Vec<Button>> {
self.read(cx).toolbar_buttons(cx) self.update(cx, |this, cx| this.toolbar_buttons(cx))
} }
fn view(&self) -> AnyView { fn view(&self) -> AnyView {

View file

@ -122,12 +122,9 @@ impl Panel for TabPanel {
} }
} }
fn toolbar_buttons(&self, cx: &WindowContext) -> Vec<Button> { fn toolbar_buttons(&self, cx: &mut ViewContext<Self>) -> Option<Vec<Button>> {
if let Some(panel) = self.active_panel(cx) { self.active_panel(cx)
panel.toolbar_buttons(cx) .and_then(|panel| panel.toolbar_buttons(cx))
} else {
vec![]
}
} }
fn dump(&self, cx: &AppContext) -> PanelState { fn dump(&self, cx: &AppContext) -> PanelState {
@ -390,11 +387,9 @@ impl TabPanel {
.gap_2() .gap_2()
.occlude() .occlude()
.items_center() .items_center()
.children( .when_some(self.toolbar_buttons(cx), |this, buttons| {
self.toolbar_buttons(cx) this.children(buttons.into_iter().map(|btn| btn.xsmall().ghost()))
.into_iter() })
.map(|btn| btn.xsmall().ghost()),
)
.when(self.is_zoomed, |this| { .when(self.is_zoomed, |this| {
this.child( this.child(
Button::new("zoom") Button::new("zoom")