Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow resize in both direcitons on Meta+Right click #860

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 91 additions & 77 deletions src/shell/layout/tiling/grabs/resize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
shell::{
focus::target::PointerFocusTarget,
grabs::{GrabStartData, ReleaseMode},
layout::Orientation,
layout::{tiling::ResizeTarget, Orientation},
},
utils::prelude::*,
};
Expand Down Expand Up @@ -83,6 +83,11 @@ 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 request = match orientation {
// NOTE: vertical split means we are resizing horizontially
Orientation::Horizontal => ResizeTarget::new_vertical(node, left_up_idx),
Orientation::Vertical => ResizeTarget::new_horizonital(node, left_up_idx),
};
pointer.set_grab(
state,
ResizeForkGrab::new(
Expand All @@ -92,9 +97,7 @@ impl PointerTarget<State> for ResizeForkTarget {
location,
}),
location.as_global(),
node,
left_up_idx,
orientation,
request,
output,
ReleaseMode::NoMouseButtons,
),
Expand Down Expand Up @@ -137,6 +140,11 @@ 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 request = match orientation {
// NOTE: vertical split means we are resizing horizontially
Orientation::Horizontal => ResizeTarget::new_vertical(node, left_up_idx),
Orientation::Vertical => ResizeTarget::new_horizonital(node, left_up_idx),
};
touch.set_grab(
state,
ResizeForkGrab::new(
Expand All @@ -146,9 +154,7 @@ impl TouchTarget<State> for ResizeForkTarget {
location,
}),
location.as_global(),
node,
left_up_idx,
orientation,
request,
output,
ReleaseMode::NoMouseButtons,
),
Expand Down Expand Up @@ -183,34 +189,30 @@ pub struct ResizeForkGrab {
start_data: GrabStartData,
last_loc: Point<f64, Global>,
old_tree: Option<Tree<Data>>,
accumulated_delta: f64,
node: NodeId,
accumulated_delta_left: f64,
accumulated_delta_up: f64,
target: ResizeTarget,
output: WeakOutput,
left_up_idx: usize,
orientation: Orientation,
release: ReleaseMode,
}

impl ResizeForkGrab {
pub fn new(
start_data: GrabStartData,
pointer_loc: Point<f64, Global>,
node: NodeId,
idx: usize,
orientation: Orientation,
request: ResizeTarget,
output: WeakOutput,
release: ReleaseMode,
) -> ResizeForkGrab {
ResizeForkGrab {
start_data,
last_loc: pointer_loc,
old_tree: None,
accumulated_delta: 0.0,
node,
output,
left_up_idx: idx,
orientation,
accumulated_delta_left: 0.0,
accumulated_delta_up: 0.0,
target: request,
release,
output,
}
}
}
Expand Down Expand Up @@ -253,7 +255,8 @@ impl ResizeForkGrab {

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

// 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;
};

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

let old_size = sizes[self.left_up_idx];
sizes[self.left_up_idx] = (old_size
+ self.accumulated_delta.round() as i32)
.max(if self.orientation == Orientation::Vertical {
360
} else {
240
});
let diff = old_size - sizes[self.left_up_idx];
let next_size = sizes[self.left_up_idx + 1] + diff;
sizes[self.left_up_idx + 1] =
next_size.max(if self.orientation == Orientation::Vertical {
360
} else {
240
});
let next_diff = next_size - sizes[self.left_up_idx + 1];
sizes[self.left_up_idx] += next_diff;
}
_ => unreachable!(),
}
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.target.left_node_idx {
perform_fork_grab_resize(tree, left_node, *left_idx, self.accumulated_delta_left);
}
if let Some((up_node, up_idx)) = &self.target.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 @@ -557,3 +516,58 @@ impl TouchGrab<State> for ResizeForkGrab {

fn unset(&mut self, _data: &mut State) {}
}

fn perform_fork_grab_resize(
tree: &mut Tree<Data>,
node: &NodeId,
left_up_idx: usize,
delta: f64,
) -> bool {
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;
}
Loading