Fix drawer position (#174)

This commit is contained in:
Jason Lee 2024-08-23 22:01:52 +08:00 committed by GitHub
parent 72b8290945
commit 5533a8b72c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 92 additions and 97 deletions

View file

@ -204,24 +204,29 @@ impl TabPanel {
let panel = self.panels.get(0).unwrap();
return h_flex()
.id("tab")
.justify_between()
.items_center()
.py_2()
.px_3()
.line_height(rems(1.0))
.child(panel.title(cx))
.child(self.render_menu_button(cx))
.on_drag(
DragPanel {
panel: panel.clone(),
tab_panel: view,
},
|drag, cx| {
cx.stop_propagation();
cx.new_view(|_| drag.clone())
},
.pr_3()
.child(
div()
.id("tab")
.py_2()
.px_3()
.min_w_16()
.child(panel.title(cx))
.on_drag(
DragPanel {
panel: panel.clone(),
tab_panel: view,
},
|drag, cx| {
cx.stop_propagation();
cx.new_view(|_| drag.clone())
},
),
)
.child(self.render_menu_button(cx))
.into_any_element();
}

View file

@ -150,7 +150,7 @@ impl RenderOnce for Drawer {
.shadow_xl()
.map(|this| {
// Set the size of the drawer.
if placement.is_vertical() {
if placement.is_horizontal() {
this.h_full().w(self.size)
} else {
this.w_full().h(self.size)

View file

@ -1,10 +1,9 @@
use std::rc::Rc;
use gpui::{
canvas, deferred, div, prelude::FluentBuilder, px, AnyElement, AnyView, Axis, Bounds,
DragMoveEvent, EntityId, InteractiveElement as _, IntoElement, MouseButton, ParentElement,
Pixels, Render, StatefulInteractiveElement, Styled, View, ViewContext, VisualContext as _,
WindowContext,
canvas, div, prelude::FluentBuilder, px, AnyElement, AnyView, Axis, Bounds, DragMoveEvent,
EntityId, InteractiveElement as _, IntoElement, MouseButton, ParentElement, Pixels, Render,
StatefulInteractiveElement, Styled, View, ViewContext, VisualContext as _, WindowContext,
};
use crate::{h_flex, theme::ActiveTheme, v_flex, AxisExt};
@ -125,89 +124,80 @@ impl ResizablePanelGroup {
fn render_resize_handle(&self, ix: usize, cx: &mut ViewContext<Self>) -> impl IntoElement {
let axis = self.axis;
let neg_offset = -HANDLE_PADDING;
let neg_offset = -HANDLE_PADDING + px(1.);
deferred(
div()
.id(("resizable-handle", ix))
.occlude()
.absolute()
.flex_shrink_0()
.when(self.axis.is_horizontal(), |this| {
this.cursor_col_resize()
.top_0()
.right(neg_offset)
.h_full()
.w(px(0.))
.px(HANDLE_PADDING)
})
.when(self.axis.is_vertical(), |this| {
this.cursor_row_resize()
.bottom(neg_offset)
.left_0()
.w_full()
.h(px(0.))
.py(HANDLE_PADDING)
})
.child(
div()
.bg(cx.theme().border)
.when(self.axis.is_horizontal(), |this| {
this.h_full().w(self.handle_size)
})
.when(self.axis.is_vertical(), |this| {
this.w_full().h(self.handle_size)
}),
)
.on_drag_move(cx.listener(move |view, e: &DragMoveEvent<DragPanel>, cx| {
match e.drag(cx) {
DragPanel((entity_id, ix, axis)) => {
if cx.entity_id() != *entity_id {
return;
}
let ix = *ix;
view.resizing_panel_ix = Some(ix);
let panel = view
.panels
.get(ix)
.expect("BUG: invalid panel index")
.read(cx);
view.sync_real_panel_sizes(cx);
match axis {
Axis::Horizontal => view.resize_panels(
ix,
e.event.position.x - panel.bounds.left(),
cx,
),
Axis::Vertical => {
view.resize_panels(
ix,
e.event.position.y - panel.bounds.top(),
cx,
);
}
}
}
}
}))
.on_mouse_up_out(
MouseButton::Left,
cx.listener(|view, _, _| {
if view.resizing_panel_ix.is_none() {
div()
.id(("resizable-handle", ix))
.occlude()
.absolute()
.flex_shrink_0()
.when(self.axis.is_horizontal(), |this| {
this.cursor_col_resize()
.top_0()
.right(neg_offset)
.h_full()
.w(px(1.))
.px(HANDLE_PADDING)
})
.when(self.axis.is_vertical(), |this| {
this.cursor_row_resize()
.bottom(neg_offset)
.left_0()
.w_full()
.h(px(1.))
.py(HANDLE_PADDING)
})
.child(
div()
.bg(cx.theme().border)
.when(self.axis.is_horizontal(), |this| {
this.h_full().w(self.handle_size)
})
.when(self.axis.is_vertical(), |this| {
this.w_full().h(self.handle_size)
}),
)
.on_drag_move(cx.listener(
move |view, e: &DragMoveEvent<DragPanel>, cx| match e.drag(cx) {
DragPanel((entity_id, ix, axis)) => {
if cx.entity_id() != *entity_id {
return;
}
view.resizing_panel_ix = None;
}),
)
.on_drag(DragPanel((cx.entity_id(), ix, axis)), |drag_panel, cx| {
cx.stop_propagation();
cx.new_view(|_| drag_panel.clone())
let ix = *ix;
view.resizing_panel_ix = Some(ix);
let panel = view
.panels
.get(ix)
.expect("BUG: invalid panel index")
.read(cx);
view.sync_real_panel_sizes(cx);
match axis {
Axis::Horizontal => {
view.resize_panels(ix, e.event.position.x - panel.bounds.left(), cx)
}
Axis::Vertical => {
view.resize_panels(ix, e.event.position.y - panel.bounds.top(), cx);
}
}
}
},
))
.on_mouse_up_out(
MouseButton::Left,
cx.listener(|view, _, _| {
if view.resizing_panel_ix.is_none() {
return;
}
view.resizing_panel_ix = None;
}),
)
.with_priority(0)
)
.on_drag(DragPanel((cx.entity_id(), ix, axis)), |drag_panel, cx| {
cx.stop_propagation();
cx.new_view(|_| drag_panel.clone())
})
}
fn sync_real_panel_sizes(&mut self, cx: &WindowContext) {