Skip to content

Commit

Permalink
Add toggle_dock feature
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee committed Sep 30, 2024
1 parent aa0e248 commit caa673a
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 8 deletions.
1 change: 1 addition & 0 deletions assets/icons/panel-bottom.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions assets/icons/panel-left.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions assets/icons/panel-right.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
58 changes: 57 additions & 1 deletion crates/app/src/story_workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use ui::{
h_flex,
popup_menu::PopupMenuExt,
theme::{ActiveTheme, Colorize as _, Theme},
ContextModal, IconName, Root, Sizable,
ContextModal, IconName, Root, Selectable, Sizable,
};
use workspace::TitleBar;

Expand Down Expand Up @@ -274,6 +274,61 @@ impl StoryWorkspace {
Ok(window)
})
}

fn render_panel_buttons(&self, cx: &mut ViewContext<Self>) -> impl IntoElement {
let left_open = self
.dock_area
.read(cx)
.is_dock_open(ui::dock::DockPlacement::Left, cx);
let bottom_open = self
.dock_area
.read(cx)
.is_dock_open(ui::dock::DockPlacement::Bottom, cx);
let right_open = self
.dock_area
.read(cx)
.is_dock_open(ui::dock::DockPlacement::Right, cx);

h_flex()
.mr_2()
.gap_1()
.child(
Button::new("panel-left")
.icon(IconName::PanelLeft)
.small()
.ghost()
.selected(left_open)
.on_click(cx.listener(move |this, _: &ClickEvent, cx| {
this.dock_area.update(cx, |dock_area, cx| {
dock_area.toggle_dock(ui::dock::DockPlacement::Left, cx);
})
})),
)
.child(
Button::new("panel-bottom")
.icon(IconName::PanelBottom)
.small()
.ghost()
.selected(bottom_open)
.on_click(cx.listener(move |this, _: &ClickEvent, cx| {
this.dock_area.update(cx, |dock_area, cx| {
dock_area.toggle_dock(ui::dock::DockPlacement::Bottom, cx);
})
})),
)
.child(
Button::new("panel-right")
.icon(IconName::PanelRight)
.small()
.ghost()
.selected(right_open)
.on_click(cx.listener(move |this, _: &ClickEvent, cx| {
this.dock_area.update(cx, |dock_area, cx| {
dock_area.toggle_dock(ui::dock::DockPlacement::Right, cx);
})
})),
)
}
}

pub fn open_new(
Expand Down Expand Up @@ -324,6 +379,7 @@ impl Render for StoryWorkspace {
.justify_end()
.px_2()
.gap_2()
.child(self.render_panel_buttons(cx))
.child(self.theme_color_picker.clone())
.child(
Button::new("theme-mode")
Expand Down
16 changes: 9 additions & 7 deletions crates/ui/src/dock/dock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,6 @@ impl Dock {
Self::new(dock_area, panels, DockPlacement::Right, cx)
}

/// Set the Dock to be open.
pub fn open(mut self) -> Self {
self.open = true;
self
}

/// Set the Dock to be resizeable, default: true
pub fn resizeable(mut self, resizeable: bool) -> Self {
self.resizeable = resizeable;
Expand All @@ -122,6 +116,15 @@ impl Dock {
&self.panels
}

pub fn is_open(&self) -> bool {
self.open
}

pub fn toggle_open(&mut self, cx: &mut ViewContext<Self>) {
self.open = !self.open;
cx.notify();
}

/// Returns the active panel index.
pub fn active_ix(&self) -> usize {
self.active_ix
Expand Down Expand Up @@ -177,7 +180,6 @@ impl Dock {
.occlude()
.absolute()
.flex_shrink_0()
.bg(gpui::transparent_white())
.when(self.placement.is_left(), |this| {
// FIXME: Improve this to let the scroll bar have px(HANDLE_PADDING)
this.cursor_col_resize()
Expand Down
20 changes: 20 additions & 0 deletions crates/ui/src/dock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,26 @@ impl DockArea {
});
}

pub fn is_dock_open(&self, placement: DockPlacement, cx: &AppContext) -> bool {
match placement {
DockPlacement::Left => self.left_dock.read(cx).is_open(),
DockPlacement::Bottom => self.bottom_dock.read(cx).is_open(),
DockPlacement::Right => self.right_dock.read(cx).is_open(),
}
}

pub fn toggle_dock(&self, placement: DockPlacement, cx: &mut ViewContext<Self>) {
let dock = match placement {
DockPlacement::Left => &self.left_dock,
DockPlacement::Bottom => &self.bottom_dock,
DockPlacement::Right => &self.right_dock,
};

dock.update(cx, |view, cx| {
view.toggle_open(cx);
})
}

/// Dump the dock panels layout to DockItemState.
///
/// See also `DockItemState::to_item` for the load DockItem from DockItemState.
Expand Down
6 changes: 6 additions & 0 deletions crates/ui/src/icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ pub enum IconName {
Minus,
Moon,
Palette,
PanelBottom,
PanelLeft,
PanelRight,
Plus,
Search,
SortAscending,
Expand Down Expand Up @@ -105,6 +108,9 @@ impl IconName {
IconName::ThumbsDown => "icons/thumbs-down.svg",
IconName::ThumbsUp => "icons/thumbs-up.svg",
IconName::TriangleAlert => "icons/triangle-alert.svg",
IconName::PanelLeft => "icons/panel-left.svg",
IconName::PanelBottom => "icons/panel-bottom.svg",
IconName::PanelRight => "icons/panel-right.svg",
}
.into()
}
Expand Down

0 comments on commit caa673a

Please sign in to comment.