Skip to content

Commit

Permalink
review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
russelltg committed Sep 24, 2024
1 parent feee063 commit e6c766a
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 160 deletions.
207 changes: 87 additions & 120 deletions src/shell/layout/tiling/grabs/resize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ impl PointerTarget<State> for ResizeForkTarget {
data.common.event_loop_handle.insert_idle(move |state| {
let pointer = seat.get_pointer().unwrap();
let location = pointer.current_location();
let (left_node_idx, right_node_idx) = match orientation {
Orientation::Horizontal => (Some((node, left_up_idx)), None),
Orientation::Vertical => (None, Some((node, left_up_idx))),
};
pointer.set_grab(
state,
ResizeForkGrab::new(
Expand All @@ -92,10 +96,8 @@ impl PointerTarget<State> for ResizeForkTarget {
location,
}),
location.as_global(),
node,
left_up_idx,
None,
orientation,
left_node_idx,
right_node_idx,
output,
ReleaseMode::NoMouseButtons,
),
Expand Down Expand Up @@ -138,6 +140,10 @@ impl TouchTarget<State> for ResizeForkTarget {
let location = event.location;
data.common.event_loop_handle.insert_idle(move |state| {
let touch = seat.get_touch().unwrap();
let (left_node_idx, right_node_idx) = match orientation {
Orientation::Horizontal => (Some((node, left_up_idx)), None),
Orientation::Vertical => (None, Some((node, left_up_idx))),
};
touch.set_grab(
state,
ResizeForkGrab::new(
Expand All @@ -147,10 +153,8 @@ impl TouchTarget<State> for ResizeForkTarget {
location,
}),
location.as_global(),
node,
left_up_idx,
None, // only resizing in one dimension when dragging resize targets
orientation,
left_node_idx,
right_node_idx,
output,
ReleaseMode::NoMouseButtons,
),
Expand Down Expand Up @@ -185,39 +189,33 @@ pub struct ResizeForkGrab {
start_data: GrabStartData,
last_loc: Point<f64, Global>,
old_tree: Option<Tree<Data>>,
accumulated_delta: f64,
accumulated_delta_parent: f64,
node: NodeId,
accumulated_delta_left: f64,
accumulated_delta_up: f64,
left_node_idx: Option<(NodeId, usize)>,
up_node_idx: Option<(NodeId, usize)>,
output: WeakOutput,
left_up_idx: usize,
parent_left_up_idx: Option<usize>,
orientation: Orientation,
release: ReleaseMode,
}

impl ResizeForkGrab {
pub fn new(
start_data: GrabStartData,
pointer_loc: Point<f64, Global>,
node: NodeId,
left_up_idx: usize,
parent_left_up_idx: Option<usize>,
orientation: Orientation,
left_node_idx: Option<(NodeId, usize)>,
up_node_idx: Option<(NodeId, usize)>,
output: WeakOutput,
release: ReleaseMode,
) -> ResizeForkGrab {
ResizeForkGrab {
start_data,
last_loc: pointer_loc,
old_tree: None,
accumulated_delta: 0.0,
accumulated_delta_parent: 0.0,
node,
output,
left_up_idx,
parent_left_up_idx,
orientation,
accumulated_delta_left: 0.0,
accumulated_delta_up: 0.0,
left_node_idx,
up_node_idx,
release,
output,
}
}
}
Expand Down Expand Up @@ -260,8 +258,8 @@ impl ResizeForkGrab {

if !equal {
*old_tree = tree.copy_clone();
self.accumulated_delta = 0.0;
self.accumulated_delta_parent = 0.0;
self.accumulated_delta_left = 0.0;
self.accumulated_delta_up = 0.0;
} else {
*tree = old_tree.copy_clone();
}
Expand All @@ -270,73 +268,22 @@ impl ResizeForkGrab {
*x = Some(tree.copy_clone());
}
};
if tree.get(&self.node).is_ok() {
match self.orientation {
Orientation::Horizontal => {
self.accumulated_delta += delta.y.round();
self.accumulated_delta_parent += delta.x.round();
}
Orientation::Vertical => {
self.accumulated_delta += delta.x.round();
self.accumulated_delta_parent += delta.y.round();
}
}

// check that we are still alive
let mut iter = tree
.children_ids(&self.node)
.unwrap()
.skip(self.left_up_idx);
let first_elem = iter.next();
let second_elem = iter.next();
if first_elem.is_none() || second_elem.is_none() {
return true;
};

let node = tree.get_mut(&self.node).unwrap();
let parent = node.parent().cloned();

let child_orientation = match node.data_mut() {
Data::Group {
sizes, orientation, ..
} => {
if !perform_fork_grab_resize(
&mut sizes[..],
self.left_up_idx,
*orientation,
self.accumulated_delta,
) {
return false;
}

*orientation
}
_ => unreachable!(),
};

if let Some(parent_left_up_idx) = self.parent_left_up_idx {
if let Some(Data::Group {
orientation, sizes, ..
}) = parent.map(|p| tree.get_mut(&p).unwrap().data_mut())
{
if *orientation == child_orientation {
return false; // definitely want it to be the other direction, strange situation if not...
}
perform_fork_grab_resize(
&mut sizes[..],
parent_left_up_idx,
*orientation,
self.accumulated_delta_parent,
);
}
}
self.accumulated_delta_left += delta.x.round();
self.accumulated_delta_up += delta.y.round();

self.last_loc = location.as_global();
let blocker = TilingLayout::update_positions(&output, tree, gaps);
tiling_layer.pending_blockers.extend(blocker);
} else {
return true;
if let Some((left_node, left_idx)) = &self.left_node_idx {
perform_fork_grab_resize(tree, left_node, *left_idx, self.accumulated_delta_left);
}
if let Some((up_node, up_idx)) = &self.up_node_idx {
perform_fork_grab_resize(tree, up_node, *up_idx, self.accumulated_delta_up);
}

self.last_loc = location.as_global();
let blocker = TilingLayout::update_positions(&output, tree, gaps);
tiling_layer.pending_blockers.extend(blocker);
} else {
return true;
}
false
}
Expand Down Expand Up @@ -574,36 +521,56 @@ impl TouchGrab<State> for ResizeForkGrab {
}

fn perform_fork_grab_resize(
sizes: &mut [i32],
tree: &mut Tree<Data>,
node: &NodeId,
left_up_idx: usize,
orientation: Orientation,
delta: f64,
) -> bool {
if sizes[left_up_idx] + sizes[left_up_idx + 1]
< match orientation {
Orientation::Vertical => 720,
Orientation::Horizontal => 480,
}
{
return false;
};

let old_size = sizes[left_up_idx];
sizes[left_up_idx] =
(old_size + delta.round() as i32).max(if orientation == Orientation::Vertical {
360
} else {
240
});
let diff = old_size - sizes[left_up_idx];
let next_size = sizes[left_up_idx + 1] + diff;
sizes[left_up_idx + 1] = next_size.max(if orientation == Orientation::Vertical {
360
} else {
240
});
let next_diff = next_size - sizes[left_up_idx + 1];
sizes[left_up_idx] += next_diff;

true
if tree.get(&node).is_ok() {
// check that we are still alive
let mut iter = tree.children_ids(node).unwrap().skip(left_up_idx);
let first_elem = iter.next();
let second_elem = iter.next();
if first_elem.is_none() || second_elem.is_none() {
return true;
};

let node = tree.get_mut(node).unwrap();

match node.data_mut() {
Data::Group {
sizes, orientation, ..
} => {
if sizes[left_up_idx] + sizes[left_up_idx + 1]
< match orientation {
Orientation::Vertical => 720,
Orientation::Horizontal => 480,
}
{
return false;
};

let old_size = sizes[left_up_idx];
sizes[left_up_idx] = (old_size + delta.round() as i32).max(
if *orientation == Orientation::Vertical {
360
} else {
240
},
);
let diff = old_size - sizes[left_up_idx];
let next_size = sizes[left_up_idx + 1] + diff;
sizes[left_up_idx + 1] = next_size.max(if *orientation == Orientation::Vertical {
360
} else {
240
});
let next_diff = next_size - sizes[left_up_idx + 1];
sizes[left_up_idx] += next_diff;
}
_ => unreachable!(),
};
}

return true;
}
79 changes: 51 additions & 28 deletions src/shell/layout/tiling/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2463,51 +2463,74 @@ impl TilingLayout {
edges
}

// Returns (left_node_idx, up_node_idx)
pub fn resize_request(
&self,
mut node_id: NodeId,
window_node_id: NodeId,
edge: ResizeEdge,
) -> Option<(NodeId, usize, Option<usize>, Orientation)> {
) -> (Option<(NodeId, usize)>, Option<(NodeId, usize)>) {
let tree = self.tree();
// need to find two NodeIds and associated index
// that indicate the groups and then the index that needs to be resized inside each one
// the one that's closer to the root is the "upper" one, and the one closer to the hovered window is the "lower" one

while let Some(group_id) = tree.get(&node_id).unwrap().parent().cloned() {
let orientation = tree.get(&group_id).unwrap().data().orientation();
let node_idx = tree
.children_ids(&group_id)
let mut try_lower_node_id = window_node_id;
while let Some(try_lower_group_id) = tree.get(&try_lower_node_id).unwrap().parent().cloned()
{
let orientation = tree.get(&try_lower_group_id).unwrap().data().orientation();
let lower_node_idx_in_group = tree
.children_ids(&try_lower_group_id)
.unwrap()
.position(|id| id == &node_id)
.position(|id| id == &try_lower_node_id)
.unwrap();
let total = tree.children_ids(&group_id).unwrap().count();
let lower_total = tree.children_ids(&try_lower_group_id).unwrap().count();

if let Some(left_up_idx) =
resize_edge_to_left_up_idx(orientation, node_idx, total, edge)
resize_edge_to_left_up_idx(orientation, lower_node_idx_in_group, lower_total, edge)
{
let parent_left_up_idx =
if let Some(group_parent) = tree.get(&group_id).unwrap().parent() {
let group_idx = tree
.children_ids(&group_parent)
let lower_node_idx = Some((try_lower_group_id.clone(), left_up_idx)); // found a lower group id!

// need to find closest parent that
// 1. is the opposite orientation as the lower group we found
// 2. can be resized in the direction we want
let mut try_upper_node_id = try_lower_group_id;
let mut parent_left_up_node_idx = None;
while let Some(try_upper_group_id) =
tree.get(&try_upper_node_id).unwrap().parent().cloned()
{
// ensure meets condition (1)
if tree.get(&try_upper_group_id).unwrap().data().orientation() != orientation {
let upper_node_idx_in_group = tree
.children_ids(&try_upper_group_id)
.unwrap()
.position(|id| id == &group_id)
.position(|id| id == &try_upper_node_id)
.unwrap();
let group_parent_orientation =
tree.get(&group_parent).unwrap().data().orientation();
let group_parent_total = tree.children_ids(&group_parent).unwrap().count();
resize_edge_to_left_up_idx(
group_parent_orientation,
group_idx,
group_parent_total,
let upper_total = tree.children_ids(&try_upper_group_id).unwrap().count();

// ensure meets condition (2)
if let Some(idx) = resize_edge_to_left_up_idx(
!orientation,
upper_node_idx_in_group,
upper_total,
edge,
)
} else {
None
};
) {
parent_left_up_node_idx = Some((try_upper_group_id.clone(), idx)); // found an upper group id!
break;
}
}
try_upper_node_id = try_upper_group_id;
}

return Some((group_id, left_up_idx, parent_left_up_idx, orientation));
return match orientation {
Orientation::Horizontal => (parent_left_up_node_idx, lower_node_idx),
Orientation::Vertical => (lower_node_idx, parent_left_up_node_idx),
};
}

node_id = group_id;
try_lower_node_id = try_lower_group_id;
}

None
(None, None)
}

pub fn resize(
Expand Down
Loading

0 comments on commit e6c766a

Please sign in to comment.