Styled and refined behavior for split resizing

This commit is contained in:
Mikayla Maki 2023-07-13 11:28:21 -07:00
parent 26b9be628e
commit d5f7ad08fa
No known key found for this signature in database
2 changed files with 86 additions and 179 deletions

View file

@ -1182,8 +1182,10 @@ impl EditorElement {
}); });
scene.push_mouse_region( scene.push_mouse_region(
MouseRegion::new::<ScrollbarMouseHandlers>(cx.view_id(), cx.view_id(), track_bounds) MouseRegion::new::<ScrollbarMouseHandlers>(cx.view_id(), cx.view_id(), track_bounds)
.on_move(move |_, editor: &mut Editor, cx| { .on_move(move |event, editor: &mut Editor, cx| {
if event.pressed_button.is_none() {
editor.scroll_manager.show_scrollbar(cx); editor.scroll_manager.show_scrollbar(cx);
}
}) })
.on_down(MouseButton::Left, { .on_down(MouseButton::Left, {
let row_range = row_range.clone(); let row_range = row_range.clone();

View file

@ -1,6 +1,8 @@
use std::{cell::RefCell, rc::Rc, sync::Arc}; use std::{cell::RefCell, rc::Rc, sync::Arc};
use crate::{AppState, FollowerStatesByLeader, Pane, Workspace}; use crate::{
pane_group::element::PaneAxisElement, AppState, FollowerStatesByLeader, Pane, Workspace,
};
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use call::{ActiveCall, ParticipantLocation}; use call::{ActiveCall, ParticipantLocation};
use gpui::{ use gpui::{
@ -13,8 +15,6 @@ use project::Project;
use serde::Deserialize; use serde::Deserialize;
use theme::Theme; use theme::Theme;
use self::adjustable_group::{AdjustableGroupElement, AdjustableGroupItem};
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub struct PaneGroup { pub struct PaneGroup {
pub(crate) root: Member, pub(crate) root: Member,
@ -122,11 +122,7 @@ impl Member {
Down | Right => vec![Member::Pane(old_pane), Member::Pane(new_pane)], Down | Right => vec![Member::Pane(old_pane), Member::Pane(new_pane)],
}; };
Member::Axis(PaneAxis { Member::Axis(PaneAxis::new(axis, members))
axis,
members,
ratios: Default::default(),
})
} }
fn contains(&self, needle: &ViewHandle<Pane>) -> bool { fn contains(&self, needle: &ViewHandle<Pane>) -> bool {
@ -308,16 +304,16 @@ impl Member {
pub(crate) struct PaneAxis { pub(crate) struct PaneAxis {
pub axis: Axis, pub axis: Axis,
pub members: Vec<Member>, pub members: Vec<Member>,
ratios: Rc<RefCell<Vec<f32>>>, flexes: Rc<RefCell<Vec<f32>>>,
} }
impl PaneAxis { impl PaneAxis {
pub fn new(axis: Axis, members: Vec<Member>) -> Self { pub fn new(axis: Axis, members: Vec<Member>) -> Self {
let ratios = Rc::new(RefCell::new(vec![1.; members.len()])); let flexes = Rc::new(RefCell::new(vec![1.; members.len()]));
Self { Self {
axis, axis,
members, members,
ratios, flexes,
} }
} }
@ -342,6 +338,7 @@ impl PaneAxis {
} }
self.members.insert(idx, Member::Pane(new_pane.clone())); self.members.insert(idx, Member::Pane(new_pane.clone()));
*self.flexes.borrow_mut() = vec![1.; self.members.len()];
} else { } else {
*member = *member =
Member::new_axis(old_pane.clone(), new_pane.clone(), direction); Member::new_axis(old_pane.clone(), new_pane.clone(), direction);
@ -381,6 +378,7 @@ impl PaneAxis {
if found_pane { if found_pane {
if let Some(idx) = remove_member { if let Some(idx) = remove_member {
self.members.remove(idx); self.members.remove(idx);
*self.flexes.borrow_mut() = vec![1.; self.members.len()];
} }
if self.members.len() == 1 { if self.members.len() == 1 {
@ -405,23 +403,17 @@ impl PaneAxis {
app_state: &Arc<AppState>, app_state: &Arc<AppState>,
cx: &mut ViewContext<Workspace>, cx: &mut ViewContext<Workspace>,
) -> AnyElement<Workspace> { ) -> AnyElement<Workspace> {
let ratios = self.ratios.clone(); debug_assert!(self.members.len() == self.flexes.borrow().len());
let mut flex_container =
AdjustableGroupElement::new(self.axis, 2., basis, move |new_flexes, _, cx| {
let mut borrow = ratios.borrow_mut();
for (ix, flex) in new_flexes {
if let Some(el) = borrow.get_mut(ix) {
*el = flex;
}
}
cx.notify(); // TODO: SImplify further by just passing in the flexes pointer directly, no need to generify!
}); let mut flex_container = PaneAxisElement::new(self.axis, basis, self.flexes.clone());
let ratios_borrow = self.ratios.borrow(); let mut members = self
let next_basis = basis + self.members.len(); .members
let mut members = self.members.iter().zip(ratios_borrow.iter()).peekable(); .iter()
while let Some((member, flex)) = members.next() { .enumerate()
.peekable();
while let Some((ix, member)) = members.next() {
let last = members.peek().is_none(); let last = members.peek().is_none();
// TODO: Restore this // TODO: Restore this
@ -431,7 +423,7 @@ impl PaneAxis {
let mut member = member.render( let mut member = member.render(
project, project,
next_basis, (basis + ix) * 10,
theme, theme,
follower_state, follower_state,
active_call, active_call,
@ -440,6 +432,7 @@ impl PaneAxis {
app_state, app_state,
cx, cx,
); );
if !last { if !last {
let mut border = theme.workspace.pane_divider; let mut border = theme.workspace.pane_divider;
border.left = false; border.left = false;
@ -455,8 +448,7 @@ impl PaneAxis {
member = member.contained().with_border(border).into_any(); member = member.contained().with_border(border).into_any();
} }
flex_container = flex_container = flex_container.with_child(member.into_any());
flex_container.with_child(AdjustableGroupItem::new(member, *flex).into_any());
} }
flex_container.into_any() flex_container.into_any()
@ -516,48 +508,34 @@ impl SplitDirection {
} }
} }
mod adjustable_group { // TODO: PaneAxis element here
mod element {
use std::{any::Any, ops::Range, rc::Rc}; use std::{cell::RefCell, ops::Range, rc::Rc};
use gpui::{ use gpui::{
color::Color,
geometry::{ geometry::{
rect::RectF, rect::RectF,
vector::{vec2f, Vector2F}, vector::{vec2f, Vector2F},
}, },
json::{self, ToJson}, json::{self, ToJson},
platform::{CursorStyle, MouseButton}, platform::{CursorStyle, MouseButton},
AnyElement, Axis, CursorRegion, Element, EventContext, LayoutContext, MouseRegion, Quad, AnyElement, Axis, CursorRegion, Element, LayoutContext, MouseRegion, RectFExt,
RectFExt, SceneBuilder, SizeConstraint, Vector2FExt, View, ViewContext, SceneBuilder, SizeConstraint, Vector2FExt, View, ViewContext,
}; };
use serde_json::Value;
use smallvec::SmallVec;
struct AdjustableFlexData { pub struct PaneAxisElement<V: View> {
flex: f32,
}
pub struct AdjustableGroupElement<V: View> {
axis: Axis, axis: Axis,
handle_size: f32,
basis: usize, basis: usize,
callback: Rc<dyn Fn(SmallVec<[(usize, f32); 2]>, &mut V, &mut EventContext<V>)>, flexes: Rc<RefCell<Vec<f32>>>,
children: Vec<AnyElement<V>>, children: Vec<AnyElement<V>>,
} }
impl<V: View> AdjustableGroupElement<V> { impl<V: View> PaneAxisElement<V> {
pub fn new( pub fn new(axis: Axis, basis: usize, flexes: Rc<RefCell<Vec<f32>>>) -> Self {
axis: Axis,
handle_size: f32,
basis: usize,
callback: impl Fn(SmallVec<[(usize, f32); 2]>, &mut V, &mut EventContext<V>) + 'static,
) -> Self {
Self { Self {
axis, axis,
handle_size,
basis, basis,
callback: Rc::new(callback), flexes,
children: Default::default(), children: Default::default(),
} }
} }
@ -571,19 +549,17 @@ mod adjustable_group {
view: &mut V, view: &mut V,
cx: &mut LayoutContext<V>, cx: &mut LayoutContext<V>,
) { ) {
let flexes = self.flexes.borrow();
let cross_axis = self.axis.invert(); let cross_axis = self.axis.invert();
let last_ix = self.children.len() - 1;
for (ix, child) in self.children.iter_mut().enumerate() { for (ix, child) in self.children.iter_mut().enumerate() {
let flex = child.metadata::<AdjustableFlexData>().unwrap().flex; let flex = flexes[ix];
let handle_size = if ix == last_ix { 0. } else { self.handle_size };
let child_size = if *remaining_flex == 0.0 { let child_size = if *remaining_flex == 0.0 {
*remaining_space *remaining_space
} else { } else {
let space_per_flex = *remaining_space / *remaining_flex; let space_per_flex = *remaining_space / *remaining_flex;
space_per_flex * flex space_per_flex * flex
} - handle_size; };
let child_constraint = match self.axis { let child_constraint = match self.axis {
Axis::Horizontal => SizeConstraint::new( Axis::Horizontal => SizeConstraint::new(
@ -596,20 +572,20 @@ mod adjustable_group {
), ),
}; };
let child_size = child.layout(child_constraint, view, cx); let child_size = child.layout(child_constraint, view, cx);
*remaining_space -= child_size.along(self.axis) + handle_size; *remaining_space -= child_size.along(self.axis);
*remaining_flex -= flex; *remaining_flex -= flex;
*cross_axis_max = cross_axis_max.max(child_size.along(cross_axis)); *cross_axis_max = cross_axis_max.max(child_size.along(cross_axis));
} }
} }
} }
impl<V: View> Extend<AnyElement<V>> for AdjustableGroupElement<V> { impl<V: View> Extend<AnyElement<V>> for PaneAxisElement<V> {
fn extend<T: IntoIterator<Item = AnyElement<V>>>(&mut self, children: T) { fn extend<T: IntoIterator<Item = AnyElement<V>>>(&mut self, children: T) {
self.children.extend(children); self.children.extend(children);
} }
} }
impl<V: View> Element<V> for AdjustableGroupElement<V> { impl<V: View> Element<V> for PaneAxisElement<V> {
type LayoutState = f32; type LayoutState = f32;
type PaintState = (); type PaintState = ();
@ -619,14 +595,11 @@ mod adjustable_group {
view: &mut V, view: &mut V,
cx: &mut LayoutContext<V>, cx: &mut LayoutContext<V>,
) -> (Vector2F, Self::LayoutState) { ) -> (Vector2F, Self::LayoutState) {
debug_assert!(self.children.len() == self.flexes.borrow().len());
let mut remaining_flex = 0.; let mut remaining_flex = 0.;
let mut cross_axis_max: f32 = 0.0; let mut cross_axis_max: f32 = 0.0;
for child in &mut self.children { for flex in self.flexes.borrow().iter() {
let metadata = child.metadata::<AdjustableFlexData>();
let flex = metadata
.map(|metadata| metadata.flex)
.expect("All children of an adjustable flex must be AdjustableFlexItems");
remaining_flex += flex; remaining_flex += flex;
} }
@ -695,48 +668,61 @@ mod adjustable_group {
Axis::Vertical => child_origin += vec2f(0.0, child.size().y()), Axis::Vertical => child_origin += vec2f(0.0, child.size().y()),
} }
const HANDLE_HITBOX_SIZE: f32 = 4.0;
if let Some((next_ix, next_child)) = children_iter.peek() { if let Some((next_ix, next_child)) = children_iter.peek() {
let bounds = match self.axis { scene.push_stacking_context(None, None);
let handle_origin = match self.axis {
Axis::Horizontal => child_origin - vec2f(HANDLE_HITBOX_SIZE / 2., 0.0),
Axis::Vertical => child_origin - vec2f(0.0, HANDLE_HITBOX_SIZE / 2.),
};
let handle_bounds = match self.axis {
Axis::Horizontal => RectF::new( Axis::Horizontal => RectF::new(
child_origin, handle_origin,
vec2f(self.handle_size, visible_bounds.height()), vec2f(HANDLE_HITBOX_SIZE, visible_bounds.height()),
), ),
Axis::Vertical => RectF::new( Axis::Vertical => RectF::new(
child_origin, handle_origin,
vec2f(visible_bounds.width(), self.handle_size), vec2f(visible_bounds.width(), HANDLE_HITBOX_SIZE),
), ),
}; };
scene.push_quad(Quad { // use gpui::color::Color,
bounds, // scene.push_quad(Quad {
background: Some(Color::red()), // bounds: handle_bounds,
..Default::default() // background: Some(Color::red()),
}); // ..Default::default()
// });
let style = match self.axis { let style = match self.axis {
Axis::Horizontal => CursorStyle::ResizeLeftRight, Axis::Horizontal => CursorStyle::ResizeLeftRight,
Axis::Vertical => CursorStyle::ResizeUpDown, Axis::Vertical => CursorStyle::ResizeUpDown,
}; };
scene.push_cursor_region(CursorRegion { bounds, style }); scene.push_cursor_region(CursorRegion {
bounds: handle_bounds,
style,
});
let callback = self.callback.clone();
let axis = self.axis; let axis = self.axis;
let child_size = child.size(); let child_size = child.size();
let next_child_size = next_child.size(); let next_child_size = next_child.size();
let mut drag_bounds = visible_bounds.clone(); let drag_bounds = visible_bounds.clone();
// Unsure why this should be needed.... let flexes = self.flexes.clone();
drag_bounds.set_origin_y(0.); let current_flex = flexes.borrow()[ix];
let current_flex = child.metadata::<AdjustableFlexData>().unwrap().flex;
let next_flex = next_child.metadata::<AdjustableFlexData>().unwrap().flex;
let next_ix = *next_ix; let next_ix = *next_ix;
let next_flex = flexes.borrow()[next_ix];
const HORIZONTAL_MIN_SIZE: f32 = 80.; const HORIZONTAL_MIN_SIZE: f32 = 80.;
const VERTICAL_MIN_SIZE: f32 = 100.; const VERTICAL_MIN_SIZE: f32 = 100.;
enum ResizeHandle {} enum ResizeHandle {}
let mut mouse_region = let mut mouse_region = MouseRegion::new::<ResizeHandle>(
MouseRegion::new::<ResizeHandle>(cx.view_id(), self.basis + ix, bounds); cx.view_id(),
self.basis + ix,
handle_bounds,
);
mouse_region = mouse_region =
mouse_region.on_drag(MouseButton::Left, move |drag, v: &mut V, cx| { mouse_region.on_drag(MouseButton::Left, move |drag, _: &mut V, cx| {
let min_size = match axis { let min_size = match axis {
Axis::Horizontal => HORIZONTAL_MIN_SIZE, Axis::Horizontal => HORIZONTAL_MIN_SIZE,
Axis::Vertical => VERTICAL_MIN_SIZE, Axis::Vertical => VERTICAL_MIN_SIZE,
@ -748,15 +734,15 @@ mod adjustable_group {
return; return;
} }
let flex_position = drag.position - drag_bounds.origin(); let mut current_target_size = (drag.position - child_start).along(axis);
let mut current_target_size = (flex_position - child_start).along(axis);
let proposed_current_pixel_change = let proposed_current_pixel_change =
current_target_size - child_size.along(axis); current_target_size - child_size.along(axis);
if proposed_current_pixel_change < 0. { if proposed_current_pixel_change < 0. {
current_target_size = current_target_size.max(min_size); current_target_size = current_target_size.max(min_size);
} else if proposed_current_pixel_change > 0. { } else if proposed_current_pixel_change > 0. {
// TODO: cascade this size change down, collect into a vec // TODO: cascade this size change down, collect all changes into a vec
let next_target_size = (next_child_size.along(axis) let next_target_size = (next_child_size.along(axis)
- proposed_current_pixel_change) - proposed_current_pixel_change)
.max(min_size); .max(min_size);
@ -768,25 +754,18 @@ mod adjustable_group {
let current_pixel_change = current_target_size - child_size.along(axis); let current_pixel_change = current_target_size - child_size.along(axis);
let flex_change = current_pixel_change / drag_bounds.length_along(axis); let flex_change = current_pixel_change / drag_bounds.length_along(axis);
let current_target_flex = current_flex + flex_change; let current_target_flex = current_flex + flex_change;
let next_target_flex = next_flex - flex_change; let next_target_flex = next_flex - flex_change;
callback( let mut borrow = flexes.borrow_mut();
smallvec::smallvec![ *borrow.get_mut(ix).unwrap() = current_target_flex;
(ix, current_target_flex), *borrow.get_mut(next_ix).unwrap() = next_target_flex;
(next_ix, next_target_flex),
], cx.notify();
v,
cx,
)
}); });
scene.push_mouse_region(mouse_region); scene.push_mouse_region(mouse_region);
match self.axis { scene.pop_stacking_context();
Axis::Horizontal => child_origin += vec2f(self.handle_size, 0.0),
Axis::Vertical => child_origin += vec2f(0.0, self.handle_size),
}
} }
} }
@ -819,85 +798,11 @@ mod adjustable_group {
cx: &ViewContext<V>, cx: &ViewContext<V>,
) -> json::Value { ) -> json::Value {
serde_json::json!({ serde_json::json!({
"type": "Flex", "type": "PaneAxis",
"bounds": bounds.to_json(), "bounds": bounds.to_json(),
"axis": self.axis.to_json(), "axis": self.axis.to_json(),
"children": self.children.iter().map(|child| child.debug(view, cx)).collect::<Vec<json::Value>>() "children": self.children.iter().map(|child| child.debug(view, cx)).collect::<Vec<json::Value>>()
}) })
} }
} }
pub struct AdjustableGroupItem<V: View> {
metadata: AdjustableFlexData,
child: AnyElement<V>,
}
impl<V: View> AdjustableGroupItem<V> {
pub fn new(child: impl Element<V>, flex: f32) -> Self {
Self {
metadata: AdjustableFlexData { flex },
child: child.into_any(),
}
}
}
impl<V: View> Element<V> for AdjustableGroupItem<V> {
type LayoutState = ();
type PaintState = ();
fn layout(
&mut self,
constraint: SizeConstraint,
view: &mut V,
cx: &mut LayoutContext<V>,
) -> (Vector2F, Self::LayoutState) {
let size = self.child.layout(constraint, view, cx);
(size, ())
}
fn paint(
&mut self,
scene: &mut SceneBuilder,
bounds: RectF,
visible_bounds: RectF,
_: &mut Self::LayoutState,
view: &mut V,
cx: &mut ViewContext<V>,
) -> Self::PaintState {
self.child
.paint(scene, bounds.origin(), visible_bounds, view, cx)
}
fn rect_for_text_range(
&self,
range_utf16: Range<usize>,
_: RectF,
_: RectF,
_: &Self::LayoutState,
_: &Self::PaintState,
view: &V,
cx: &ViewContext<V>,
) -> Option<RectF> {
self.child.rect_for_text_range(range_utf16, view, cx)
}
fn metadata(&self) -> Option<&dyn Any> {
Some(&self.metadata)
}
fn debug(
&self,
_: RectF,
_: &Self::LayoutState,
_: &Self::PaintState,
view: &V,
cx: &ViewContext<V>,
) -> Value {
serde_json::json!({
"type": "Flexible",
"flex": self.metadata.flex,
"child": self.child.debug(view, cx)
})
}
}
} }