Skip to content

Commit

Permalink
Allow dragging tabs (#3616)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxbrunsfeld authored Dec 13, 2023
2 parents 2e00da5 + 6362221 commit e09b07d
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 587 deletions.
6 changes: 5 additions & 1 deletion crates/collab_ui2/src/chat_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,11 @@ impl ChatPanel {
),
),
)
.child(div().grow().child(self.render_active_channel_messages(cx)))
.child(
div()
.flex_grow()
.child(self.render_active_channel_messages(cx)),
)
.child(
div()
.z_index(1)
Expand Down
38 changes: 28 additions & 10 deletions crates/gpui2/src/elements/div.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub struct GroupStyle {
pub style: Box<StyleRefinement>,
}

pub trait InteractiveElement: Sized + Element {
pub trait InteractiveElement: Sized {
fn interactivity(&mut self) -> &mut Interactivity;

fn group(mut self, group: impl Into<SharedString>) -> Self {
Expand Down Expand Up @@ -61,6 +61,10 @@ pub trait InteractiveElement: Sized + Element {
}

fn hover(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
debug_assert!(
self.interactivity().hover_style.is_none(),
"hover style already set"
);
self.interactivity().hover_style = Some(Box::new(f(StyleRefinement::default())));
self
}
Expand Down Expand Up @@ -436,7 +440,6 @@ pub trait StatefulInteractiveElement: InteractiveElement {
"calling tooltip more than once on the same element is not supported"
);
self.interactivity().tooltip_builder = Some(Rc::new(build_tooltip));

self
}
}
Expand Down Expand Up @@ -1013,6 +1016,10 @@ impl Interactivity {

let overflow = style.overflow;
if overflow.x == Overflow::Scroll || overflow.y == Overflow::Scroll {
if let Some(scroll_handle) = &self.scroll_handle {
scroll_handle.0.borrow_mut().overflow = overflow;
}

let scroll_offset = element_state
.scroll_offset
.get_or_insert_with(Rc::default)
Expand Down Expand Up @@ -1314,16 +1321,16 @@ where

impl<E> IntoElement for Focusable<E>
where
E: Element,
E: IntoElement,
{
type Element = E;
type Element = E::Element;

fn element_id(&self) -> Option<ElementId> {
self.element.element_id()
}

fn into_element(self) -> Self::Element {
self.element
self.element.into_element()
}
}

Expand Down Expand Up @@ -1417,6 +1424,7 @@ struct ScrollHandleState {
bounds: Bounds<Pixels>,
child_bounds: Vec<Bounds<Pixels>>,
requested_scroll_top: Option<(usize, Pixels)>,
overflow: Point<Overflow>,
}

#[derive(Clone)]
Expand Down Expand Up @@ -1462,12 +1470,22 @@ impl ScrollHandle {
return;
};

let scroll_offset = state.offset.borrow().y;
let mut scroll_offset = state.offset.borrow_mut();

if bounds.top() + scroll_offset < state.bounds.top() {
state.offset.borrow_mut().y = state.bounds.top() - bounds.top();
} else if bounds.bottom() + scroll_offset > state.bounds.bottom() {
state.offset.borrow_mut().y = state.bounds.bottom() - bounds.bottom();
if state.overflow.y == Overflow::Scroll {
if bounds.top() + scroll_offset.y < state.bounds.top() {
scroll_offset.y = state.bounds.top() - bounds.top();
} else if bounds.bottom() + scroll_offset.y > state.bounds.bottom() {
scroll_offset.y = state.bounds.bottom() - bounds.bottom();
}
}

if state.overflow.x == Overflow::Scroll {
if bounds.left() + scroll_offset.x < state.bounds.left() {
scroll_offset.x = state.bounds.left() - bounds.left();
} else if bounds.right() + scroll_offset.x > state.bounds.right() {
scroll_offset.x = state.bounds.right() - bounds.right();
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/gpui2/src/styled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ pub trait Styled: Sized {

/// Sets the element to allow a flex item to grow to fill any available space.
/// [Docs](https://tailwindcss.com/docs/flex-grow)
fn grow(mut self) -> Self {
fn flex_grow(mut self) -> Self {
self.style().flex_grow = Some(1.);
self
}
Expand Down
2 changes: 1 addition & 1 deletion crates/picker2/src/picker2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ impl<D: PickerDelegate> Render for Picker<D> {
.when(self.delegate.match_count() > 0, |el| {
el.child(
v_stack()
.grow()
.flex_grow()
.child(
uniform_list(
cx.view().clone(),
Expand Down
2 changes: 1 addition & 1 deletion crates/storybook2/src/story_selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl ComponentStory {
Self::Scroll => ScrollStory::view(cx).into(),
Self::Text => TextStory::view(cx).into(),
Self::Tab => cx.build_view(|_| ui::TabStory).into(),
Self::TabBar => cx.build_view(|cx| ui::TabBarStory::new(cx)).into(),
Self::TabBar => cx.build_view(|_| ui::TabBarStory).into(),
Self::ViewportUnits => cx.build_view(|_| crate::stories::ViewportUnitsStory).into(),
Self::ZIndex => cx.build_view(|_| ZIndexStory).into(),
Self::Picker => PickerStory::new(cx).into(),
Expand Down
8 changes: 0 additions & 8 deletions crates/ui2/src/components/list/list_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 3 additions & 13 deletions crates/ui2/src/components/stories/tab_bar.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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),
Expand Down
52 changes: 15 additions & 37 deletions crates/ui2/src/components/tab.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use std::cmp::Ordering;
use std::rc::Rc;

use gpui::{AnyElement, AnyView, ClickEvent, IntoElement, MouseButton};
use smallvec::SmallVec;

use crate::prelude::*;
use gpui::{AnyElement, IntoElement, Stateful};
use smallvec::SmallVec;
use std::cmp::Ordering;

/// The position of a [`Tab`] within a list of tabs.
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
Expand All @@ -29,12 +26,10 @@ pub enum TabCloseSide {

#[derive(IntoElement)]
pub struct Tab {
id: ElementId,
div: Stateful<Div>,
selected: bool,
position: TabPosition,
close_side: TabCloseSide,
on_click: Option<Rc<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>>,
tooltip: Option<Box<dyn Fn(&mut WindowContext) -> AnyView + 'static>>,
start_slot: Option<AnyElement>,
end_slot: Option<AnyElement>,
children: SmallVec<[AnyElement; 2]>,
Expand All @@ -43,12 +38,10 @@ pub struct Tab {
impl Tab {
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
id: id.into(),
div: div().id(id),
selected: false,
position: TabPosition::First,
close_side: TabCloseSide::End,
on_click: None,
tooltip: None,
start_slot: None,
end_slot: None,
children: SmallVec::new(),
Expand All @@ -65,16 +58,6 @@ impl Tab {
self
}

pub fn on_click(mut self, handler: impl Fn(&ClickEvent, &mut WindowContext) + 'static) -> Self {
self.on_click = Some(Rc::new(handler));
self
}

pub fn tooltip(mut self, tooltip: impl Fn(&mut WindowContext) -> AnyView + 'static) -> Self {
self.tooltip = Some(Box::new(tooltip));
self
}

pub fn start_slot<E: IntoElement>(mut self, element: impl Into<Option<E>>) -> Self {
self.start_slot = element.into().map(IntoElement::into_any_element);
self
Expand All @@ -86,6 +69,14 @@ impl Tab {
}
}

impl InteractiveElement for Tab {
fn interactivity(&mut self) -> &mut gpui::Interactivity {
self.div.interactivity()
}
}

impl StatefulInteractiveElement for Tab {}

impl Selectable for Tab {
fn selected(mut self, selected: bool) -> Self {
self.selected = selected;
Expand All @@ -100,7 +91,7 @@ impl ParentElement for Tab {
}

impl RenderOnce for Tab {
type Rendered = Div;
type Rendered = Stateful<Div>;

fn render(self, cx: &mut WindowContext) -> Self::Rendered {
const HEIGHT_IN_REMS: f32 = 30. / 16.;
Expand All @@ -120,7 +111,7 @@ impl RenderOnce for Tab {
),
};

div()
self.div
.h(rems(HEIGHT_IN_REMS))
.bg(tab_bg)
.border_color(cx.theme().colors().border)
Expand All @@ -146,26 +137,13 @@ impl RenderOnce for Tab {
.child(
h_stack()
.group("")
.id(self.id)
.relative()
.h_full()
.px_5()
.gap_1()
.text_color(text_color)
// .hover(|style| style.bg(tab_hover_bg))
// .active(|style| style.bg(tab_active_bg))
.when_some(self.on_click, |tab, on_click| {
tab.cursor_pointer().on_click(move |event, cx| {
// HACK: GPUI currently fires `on_click` with any mouse button,
// but we only care about the left button.
if event.down.button == MouseButton::Left {
(on_click)(event, cx)
}
})
})
.when_some(self.tooltip, |tab, tooltip| {
tab.tooltip(move |cx| tooltip(cx))
})
.child(
h_stack()
.w_3()
Expand Down
20 changes: 14 additions & 6 deletions crates/ui2/src/components/tab_bar.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
use gpui::{AnyElement, FocusHandle, Focusable, Stateful};
use gpui::{AnyElement, ScrollHandle, Stateful};
use smallvec::SmallVec;

use crate::prelude::*;

#[derive(IntoElement)]
pub struct TabBar {
id: ElementId,
focus_handle: FocusHandle,
start_children: SmallVec<[AnyElement; 2]>,
children: SmallVec<[AnyElement; 2]>,
end_children: SmallVec<[AnyElement; 2]>,
scroll_handle: Option<ScrollHandle>,
}

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,
start_children: SmallVec::new(),
children: SmallVec::new(),
end_children: SmallVec::new(),
scroll_handle: None,
}
}

pub fn track_scroll(mut self, scroll_handle: ScrollHandle) -> Self {
self.scroll_handle = Some(scroll_handle);
self
}

pub fn start_children_mut(&mut self) -> &mut SmallVec<[AnyElement; 2]> {
&mut self.start_children
}
Expand Down Expand Up @@ -84,15 +89,14 @@ impl ParentElement 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)
.group("tab_bar")
.track_focus(&self.focus_handle)
.flex()
.flex_none()
.w_full()
Expand Down Expand Up @@ -128,7 +132,11 @@ impl RenderOnce for TabBar {
h_stack()
.id("tabs")
.z_index(2)
.flex_grow()
.overflow_x_scroll()
.when_some(self.scroll_handle, |cx, scroll_handle| {
cx.track_scroll(&scroll_handle)
})
.children(self.children),
),
)
Expand Down
Loading

0 comments on commit e09b07d

Please sign in to comment.