Allow dragging and dropping tabs

Co-authored-by: Nathan <nathan@zed.dev>
Co-authored-by: Marshall <marshall@zed.dev>
This commit is contained in:
Max Brunsfeld 2023-12-12 15:02:31 -08:00
parent a4a501603e
commit a579713a45
9 changed files with 82 additions and 535 deletions

View file

@ -54,14 +54,6 @@ impl ListItem {
self
}
pub fn on_drag(
mut self,
handler: impl Fn(&MouseDownEvent, &mut WindowContext) + 'static,
) -> Self {
self.on_secondary_mouse_down = Some(Box::new(handler));
self
}
pub fn tooltip(mut self, tooltip: impl Fn(&mut WindowContext) -> AnyView + 'static) -> Self {
self.tooltip = Some(Box::new(tooltip));
self

View file

@ -1,19 +1,9 @@
use gpui::{Div, FocusHandle, Render};
use gpui::{Div, Render};
use story::Story;
use crate::{prelude::*, Tab, TabBar, TabPosition};
pub struct TabBarStory {
tab_bar_focus_handle: FocusHandle,
}
impl TabBarStory {
pub fn new(cx: &mut ViewContext<Self>) -> Self {
Self {
tab_bar_focus_handle: cx.focus_handle(),
}
}
}
pub struct TabBarStory;
impl Render for TabBarStory {
type Element = Div;
@ -48,7 +38,7 @@ impl Render for TabBarStory {
.child(Story::label("Default"))
.child(
h_stack().child(
TabBar::new("tab_bar_1", self.tab_bar_focus_handle.clone())
TabBar::new("tab_bar_1")
.start_child(
IconButton::new("navigate_backward", Icon::ArrowLeft)
.icon_size(IconSize::Small),

View file

@ -1,22 +1,20 @@
use gpui::{AnyElement, FocusHandle, Focusable, Stateful};
use gpui::{AnyElement, Stateful};
use smallvec::SmallVec;
use crate::prelude::*;
#[derive(IntoElement)]
pub struct TabBar {
id: ElementId,
focus_handle: FocusHandle,
div: Stateful<Div>,
start_children: SmallVec<[AnyElement; 2]>,
children: SmallVec<[AnyElement; 2]>,
end_children: SmallVec<[AnyElement; 2]>,
}
impl TabBar {
pub fn new(id: impl Into<ElementId>, focus_handle: FocusHandle) -> Self {
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
id: id.into(),
focus_handle,
div: div().id(id),
start_children: SmallVec::new(),
children: SmallVec::new(),
end_children: SmallVec::new(),
@ -83,16 +81,22 @@ impl ParentElement for TabBar {
}
}
impl InteractiveElement for TabBar {
fn interactivity(&mut self) -> &mut gpui::Interactivity {
self.div.interactivity()
}
}
impl StatefulInteractiveElement for TabBar {}
impl RenderOnce for TabBar {
type Rendered = Focusable<Stateful<Div>>;
type Rendered = Stateful<Div>;
fn render(self, cx: &mut WindowContext) -> Self::Rendered {
const HEIGHT_IN_REMS: f32 = 30. / 16.;
div()
.id(self.id)
self.div
.group("tab_bar")
.track_focus(&self.focus_handle)
.flex()
.flex_none()
.w_full()
@ -128,6 +132,7 @@ impl RenderOnce for TabBar {
h_stack()
.id("tabs")
.z_index(2)
.flex_grow()
.overflow_x_scroll()
.children(self.children),
),