cursor follows focus to center of window

This commit is contained in:
may 2024-09-26 19:34:18 +02:00 committed by Victoria Brekenfeld
parent ea2215ec3f
commit a4d875e35e

View file

@ -10,7 +10,7 @@ use smithay::{
input::{pointer::MotionEvent, Seat}, input::{pointer::MotionEvent, Seat},
output::Output, output::Output,
reexports::wayland_server::Resource, reexports::wayland_server::Resource,
utils::{IsAlive, Serial, SERIAL_COUNTER}, utils::{IsAlive, Point, Serial, SERIAL_COUNTER},
wayland::{ wayland::{
seat::WaylandFocus, seat::WaylandFocus,
selection::{data_device::set_data_device_focus, primary_selection::set_primary_focus}, selection::{data_device::set_data_device_focus, primary_selection::set_primary_focus},
@ -231,21 +231,22 @@ fn update_focus_state(
if target.is_some() { if target.is_some() {
//need to borrow mutably for surface under //need to borrow mutably for surface under
let mut shell = state.common.shell.write().unwrap(); let mut shell = state.common.shell.write().unwrap();
// get the top left corner of the target element // get geometry of the target element
let geometry = shell.focused_geometry(target.unwrap()); let geometry = shell.focused_geometry(target.unwrap());
//to avoid the nested mutable borrow of state if let Some(geometry) = geometry {
if geometry.is_some() { // get the center of the target element
let top_left = geometry.unwrap().loc.to_f64(); let window_center = Point::from((geometry.size.w / 2, geometry.size.h / 2));
let new_pos = (geometry.loc + window_center).to_f64();
// create a pointer target from the target element // create a pointer target from the target element
let output = shell let output = shell
.outputs() .outputs()
.find(|output| output.geometry().to_f64().contains(top_left)) .find(|output| output.geometry().to_f64().contains(new_pos))
.cloned() .cloned()
.unwrap_or(seat.active_output()); .unwrap_or(seat.active_output());
let focus = shell let focus = shell
.surface_under(top_left, &output) .surface_under(new_pos, &output)
.map(|(focus, loc)| (focus, loc.as_logical())); .map(|(focus, loc)| (focus, loc.as_logical()));
//drop here to avoid multiple mutable borrows //drop here to avoid multiple mutable borrows
mem::drop(shell); mem::drop(shell);
@ -253,7 +254,7 @@ fn update_focus_state(
state, state,
focus, focus,
&MotionEvent { &MotionEvent {
location: top_left.as_logical(), location: new_pos.as_logical(),
serial: SERIAL_COUNTER.next_serial(), serial: SERIAL_COUNTER.next_serial(),
time: 0, time: 0,
}, },