Skip to content

Commit

Permalink
panel: Update Panel toolbar_buttons method to use ViewContext<Self>. (
Browse files Browse the repository at this point in the history
#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>>
```
  • Loading branch information
huacnlee authored Jan 9, 2025
1 parent bf82e3d commit de42cc4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 19 deletions.
6 changes: 3 additions & 3 deletions crates/story/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,8 @@ impl Panel for StoryContainer {
.menu("Info", Box::new(ShowPanelInfo))
}

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

fn dump(&self, _cx: &AppContext) -> PanelState {
Expand Down
10 changes: 5 additions & 5 deletions crates/ui/src/dock/panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
fn toolbar_buttons(&self, cx: &WindowContext) -> Vec<Button> {
vec![]
fn toolbar_buttons(&self, cx: &mut ViewContext<Self>) -> Option<Vec<Button>> {
None
}

/// Dump the panel, used to serialize the panel.
Expand All @@ -113,7 +113,7 @@ pub trait PanelView: 'static + Send + Sync {
fn set_active(&self, active: bool, cx: &mut WindowContext);
fn set_zoomed(&self, zoomed: bool, cx: &mut WindowContext);
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 focus_handle(&self, cx: &AppContext) -> FocusHandle;
fn dump(&self, cx: &AppContext) -> PanelState;
Expand Down Expand Up @@ -160,8 +160,8 @@ impl<T: Panel> PanelView for View<T> {
self.read(cx).popup_menu(menu, cx)
}

fn toolbar_buttons(&self, cx: &WindowContext) -> Vec<Button> {
self.read(cx).toolbar_buttons(cx)
fn toolbar_buttons(&self, cx: &mut WindowContext) -> Option<Vec<Button>> {
self.update(cx, |this, cx| this.toolbar_buttons(cx))
}

fn view(&self) -> AnyView {
Expand Down
17 changes: 6 additions & 11 deletions crates/ui/src/dock/tab_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,9 @@ impl Panel for TabPanel {
}
}

fn toolbar_buttons(&self, cx: &WindowContext) -> Vec<Button> {
if let Some(panel) = self.active_panel(cx) {
panel.toolbar_buttons(cx)
} else {
vec![]
}
fn toolbar_buttons(&self, cx: &mut ViewContext<Self>) -> Option<Vec<Button>> {
self.active_panel(cx)
.and_then(|panel| panel.toolbar_buttons(cx))
}

fn dump(&self, cx: &AppContext) -> PanelState {
Expand Down Expand Up @@ -390,11 +387,9 @@ impl TabPanel {
.gap_2()
.occlude()
.items_center()
.children(
self.toolbar_buttons(cx)
.into_iter()
.map(|btn| btn.xsmall().ghost()),
)
.when_some(self.toolbar_buttons(cx), |this, buttons| {
this.children(buttons.into_iter().map(|btn| btn.xsmall().ghost()))
})
.when(self.is_zoomed, |this| {
this.child(
Button::new("zoom")
Expand Down

0 comments on commit de42cc4

Please sign in to comment.