forge
Explore
Log in

Don't you dare go Hollow

Implement warp-mouse-to-focus

Browse files at this commit

741bee461cbdaf0236eaa55dc86dc84b01613128FluxTapeParent: 0c57815
niri-config/src/lib.rsmodified+5 -0
@@ -69,6 +69,8 @@ pub struct Input {
6969 pub touch: Touch,
7070 #[knuffel(child)]
7171 pub disable_power_key_handling: bool,
72+ #[knuffel(child)]
73+ pub warp_mouse_to_focus: bool,
7274 }
7375
7476 #[derive(knuffel::Decode, Debug, Default, PartialEq, Eq)]
@@ -1592,6 +1594,8 @@ mod tests {
15921594 }
15931595
15941596 disable-power-key-handling
1597+
1598+ warp-mouse-to-focus
15951599 }
15961600
15971601 output "eDP-1" {
@@ -1731,6 +1735,7 @@ mod tests {
17311735 map_to_output: Some("eDP-1".to_owned()),
17321736 },
17331737 disable_power_key_handling: true,
1738+ warp_mouse_to_focus: true,
17341739 },
17351740 outputs: vec![Output {
17361741 off: false,
resources/default-config.kdlmodified+3 -0
@@ -67,6 +67,9 @@ input {
6767 // Uncomment this if you would like to configure the power button elsewhere
6868 // (i.e. logind.conf).
6969 // disable-power-key-handling
70+
71+ // Uncomment this to make the mouse warp to the center of newly focused windows.
72+ // warp-mouse-to-focus
7073 }
7174
7275 // You can configure outputs by their name, which you can find
src/input.rsmodified+78 -16
@@ -411,139 +411,166 @@ impl State {
411411 }
412412 Action::MoveColumnLeft => {
413413 self.niri.layout.move_left();
414+ self.maybe_warp_cursor_to_focus();
414415 // FIXME: granular
415416 self.niri.queue_redraw_all();
416417 }
417418 Action::MoveColumnRight => {
418419 self.niri.layout.move_right();
420+ self.maybe_warp_cursor_to_focus();
419421 // FIXME: granular
420422 self.niri.queue_redraw_all();
421423 }
422424 Action::MoveColumnToFirst => {
423425 self.niri.layout.move_column_to_first();
426+ self.maybe_warp_cursor_to_focus();
424427 // FIXME: granular
425428 self.niri.queue_redraw_all();
426429 }
427430 Action::MoveColumnToLast => {
428431 self.niri.layout.move_column_to_last();
432+ self.maybe_warp_cursor_to_focus();
429433 // FIXME: granular
430434 self.niri.queue_redraw_all();
431435 }
432436 Action::MoveWindowDown => {
433437 self.niri.layout.move_down();
438+ self.maybe_warp_cursor_to_focus();
434439 // FIXME: granular
435440 self.niri.queue_redraw_all();
436441 }
437442 Action::MoveWindowUp => {
438443 self.niri.layout.move_up();
444+ self.maybe_warp_cursor_to_focus();
439445 // FIXME: granular
440446 self.niri.queue_redraw_all();
441447 }
442448 Action::MoveWindowDownOrToWorkspaceDown => {
443449 self.niri.layout.move_down_or_to_workspace_down();
450+ self.maybe_warp_cursor_to_focus();
444451 // FIXME: granular
445452 self.niri.queue_redraw_all();
446453 }
447454 Action::MoveWindowUpOrToWorkspaceUp => {
448455 self.niri.layout.move_up_or_to_workspace_up();
456+ self.maybe_warp_cursor_to_focus();
449457 // FIXME: granular
450458 self.niri.queue_redraw_all();
451459 }
452460 Action::ConsumeOrExpelWindowLeft => {
453461 self.niri.layout.consume_or_expel_window_left();
462+ self.maybe_warp_cursor_to_focus();
454463 // FIXME: granular
455464 self.niri.queue_redraw_all();
456465 }
457466 Action::ConsumeOrExpelWindowRight => {
458467 self.niri.layout.consume_or_expel_window_right();
468+ self.maybe_warp_cursor_to_focus();
459469 // FIXME: granular
460470 self.niri.queue_redraw_all();
461471 }
462472 Action::FocusColumnLeft => {
463473 self.niri.layout.focus_left();
474+ self.maybe_warp_cursor_to_focus();
464475 // FIXME: granular
465476 self.niri.queue_redraw_all();
466477 }
467478 Action::FocusColumnRight => {
468479 self.niri.layout.focus_right();
480+ self.maybe_warp_cursor_to_focus();
469481 // FIXME: granular
470482 self.niri.queue_redraw_all();
471483 }
472484 Action::FocusColumnFirst => {
473485 self.niri.layout.focus_column_first();
486+ self.maybe_warp_cursor_to_focus();
474487 // FIXME: granular
475488 self.niri.queue_redraw_all();
476489 }
477490 Action::FocusColumnLast => {
478491 self.niri.layout.focus_column_last();
492+ self.maybe_warp_cursor_to_focus();
479493 // FIXME: granular
480494 self.niri.queue_redraw_all();
481495 }
482496 Action::FocusWindowDown => {
483497 self.niri.layout.focus_down();
498+ self.maybe_warp_cursor_to_focus();
484499 // FIXME: granular
485500 self.niri.queue_redraw_all();
486501 }
487502 Action::FocusWindowUp => {
488503 self.niri.layout.focus_up();
504+ self.maybe_warp_cursor_to_focus();
489505 // FIXME: granular
490506 self.niri.queue_redraw_all();
491507 }
492508 Action::FocusWindowOrWorkspaceDown => {
493509 self.niri.layout.focus_window_or_workspace_down();
510+ self.maybe_warp_cursor_to_focus();
494511 // FIXME: granular
495512 self.niri.queue_redraw_all();
496513 }
497514 Action::FocusWindowOrWorkspaceUp => {
498515 self.niri.layout.focus_window_or_workspace_up();
516+ self.maybe_warp_cursor_to_focus();
499517 // FIXME: granular
500518 self.niri.queue_redraw_all();
501519 }
502520 Action::MoveWindowToWorkspaceDown => {
503521 self.niri.layout.move_to_workspace_down();
522+ self.maybe_warp_cursor_to_focus();
504523 // FIXME: granular
505524 self.niri.queue_redraw_all();
506525 }
507526 Action::MoveWindowToWorkspaceUp => {
508527 self.niri.layout.move_to_workspace_up();
528+ self.maybe_warp_cursor_to_focus();
509529 // FIXME: granular
510530 self.niri.queue_redraw_all();
511531 }
512532 Action::MoveWindowToWorkspace(idx) => {
513533 let idx = idx.saturating_sub(1) as usize;
514534 self.niri.layout.move_to_workspace(idx);
535+ self.maybe_warp_cursor_to_focus();
515536 // FIXME: granular
516537 self.niri.queue_redraw_all();
517538 }
518539 Action::MoveColumnToWorkspaceDown => {
519540 self.niri.layout.move_column_to_workspace_down();
541+ self.maybe_warp_cursor_to_focus();
520542 // FIXME: granular
521543 self.niri.queue_redraw_all();
522544 }
523545 Action::MoveColumnToWorkspaceUp => {
524546 self.niri.layout.move_column_to_workspace_up();
547+ self.maybe_warp_cursor_to_focus();
525548 // FIXME: granular
526549 self.niri.queue_redraw_all();
527550 }
528551 Action::MoveColumnToWorkspace(idx) => {
529552 let idx = idx.saturating_sub(1) as usize;
530553 self.niri.layout.move_column_to_workspace(idx);
554+ self.maybe_warp_cursor_to_focus();
531555 // FIXME: granular
532556 self.niri.queue_redraw_all();
533557 }
534558 Action::FocusWorkspaceDown => {
535559 self.niri.layout.switch_workspace_down();
560+ self.maybe_warp_cursor_to_focus();
536561 // FIXME: granular
537562 self.niri.queue_redraw_all();
538563 }
539564 Action::FocusWorkspaceUp => {
540565 self.niri.layout.switch_workspace_up();
566+ self.maybe_warp_cursor_to_focus();
541567 // FIXME: granular
542568 self.niri.queue_redraw_all();
543569 }
544570 Action::FocusWorkspace(idx) => {
545571 let idx = idx.saturating_sub(1) as usize;
546572 self.niri.layout.switch_workspace(idx);
573+ self.maybe_warp_cursor_to_focus();
547574 // FIXME: granular
548575 self.niri.queue_redraw_all();
549576 }
@@ -559,11 +586,14 @@ impl State {
559586 }
560587 Action::ConsumeWindowIntoColumn => {
561588 self.niri.layout.consume_into_column();
589+ // This does not cause immediate focus or window size change, so warping mouse to
590+ // focus won't do anything here.
562591 // FIXME: granular
563592 self.niri.queue_redraw_all();
564593 }
565594 Action::ExpelWindowFromColumn => {
566595 self.niri.layout.expel_from_column();
596+ self.maybe_warp_cursor_to_focus();
567597 // FIXME: granular
568598 self.niri.queue_redraw_all();
569599 }
@@ -581,81 +611,105 @@ impl State {
581611 Action::FocusMonitorLeft => {
582612 if let Some(output) = self.niri.output_left() {
583613 self.niri.layout.focus_output(&output);
584- self.move_cursor_to_output(&output);
614+ if !self.maybe_warp_cursor_to_focus_centered() {
615+ self.move_cursor_to_output(&output);
616+ }
585617 }
586618 }
587619 Action::FocusMonitorRight => {
588620 if let Some(output) = self.niri.output_right() {
589621 self.niri.layout.focus_output(&output);
590- self.move_cursor_to_output(&output);
622+ if !self.maybe_warp_cursor_to_focus_centered() {
623+ self.move_cursor_to_output(&output);
624+ }
591625 }
592626 }
593627 Action::FocusMonitorDown => {
594628 if let Some(output) = self.niri.output_down() {
595629 self.niri.layout.focus_output(&output);
596- self.move_cursor_to_output(&output);
630+ if !self.maybe_warp_cursor_to_focus_centered() {
631+ self.move_cursor_to_output(&output);
632+ }
597633 }
598634 }
599635 Action::FocusMonitorUp => {
600636 if let Some(output) = self.niri.output_up() {
601637 self.niri.layout.focus_output(&output);
602- self.move_cursor_to_output(&output);
638+ if !self.maybe_warp_cursor_to_focus_centered() {
639+ self.move_cursor_to_output(&output);
640+ }
603641 }
604642 }
605643 Action::MoveWindowToMonitorLeft => {
606644 if let Some(output) = self.niri.output_left() {
607645 self.niri.layout.move_to_output(&output);
608646 self.niri.layout.focus_output(&output);
609- self.move_cursor_to_output(&output);
647+ if !self.maybe_warp_cursor_to_focus_centered() {
648+ self.move_cursor_to_output(&output);
649+ }
610650 }
611651 }
612652 Action::MoveWindowToMonitorRight => {
613653 if let Some(output) = self.niri.output_right() {
614654 self.niri.layout.move_to_output(&output);
615655 self.niri.layout.focus_output(&output);
616- self.move_cursor_to_output(&output);
656+ if !self.maybe_warp_cursor_to_focus_centered() {
657+ self.move_cursor_to_output(&output);
658+ }
617659 }
618660 }
619661 Action::MoveWindowToMonitorDown => {
620662 if let Some(output) = self.niri.output_down() {
621663 self.niri.layout.move_to_output(&output);
622664 self.niri.layout.focus_output(&output);
623- self.move_cursor_to_output(&output);
665+ if !self.maybe_warp_cursor_to_focus_centered() {
666+ self.move_cursor_to_output(&output);
667+ }
624668 }
625669 }
626670 Action::MoveWindowToMonitorUp => {
627671 if let Some(output) = self.niri.output_up() {
628672 self.niri.layout.move_to_output(&output);
629673 self.niri.layout.focus_output(&output);
630- self.move_cursor_to_output(&output);
674+ if !self.maybe_warp_cursor_to_focus_centered() {
675+ self.move_cursor_to_output(&output);
676+ }
631677 }
632678 }
633679 Action::MoveColumnToMonitorLeft => {
634680 if let Some(output) = self.niri.output_left() {
635681 self.niri.layout.move_column_to_output(&output);
636682 self.niri.layout.focus_output(&output);
637- self.move_cursor_to_output(&output);
683+ if !self.maybe_warp_cursor_to_focus_centered() {
684+ self.move_cursor_to_output(&output);
685+ }
638686 }
639687 }
640688 Action::MoveColumnToMonitorRight => {
641689 if let Some(output) = self.niri.output_right() {
642690 self.niri.layout.move_column_to_output(&output);
643691 self.niri.layout.focus_output(&output);
644- self.move_cursor_to_output(&output);
692+ if !self.maybe_warp_cursor_to_focus_centered() {
693+ self.move_cursor_to_output(&output);
694+ }
645695 }
646696 }
647697 Action::MoveColumnToMonitorDown => {
648698 if let Some(output) = self.niri.output_down() {
649699 self.niri.layout.move_column_to_output(&output);
650700 self.niri.layout.focus_output(&output);
651- self.move_cursor_to_output(&output);
701+ if !self.maybe_warp_cursor_to_focus_centered() {
702+ self.move_cursor_to_output(&output);
703+ }
652704 }
653705 }
654706 Action::MoveColumnToMonitorUp => {
655707 if let Some(output) = self.niri.output_up() {
656708 self.niri.layout.move_column_to_output(&output);
657709 self.niri.layout.focus_output(&output);
658- self.move_cursor_to_output(&output);
710+ if !self.maybe_warp_cursor_to_focus_centered() {
711+ self.move_cursor_to_output(&output);
712+ }
659713 }
660714 }
661715 Action::SetColumnWidth(change) => {
@@ -672,25 +726,33 @@ impl State {
672726 Action::MoveWorkspaceToMonitorLeft => {
673727 if let Some(output) = self.niri.output_left() {
674728 self.niri.layout.move_workspace_to_output(&output);
675- self.move_cursor_to_output(&output);
729+ if !self.maybe_warp_cursor_to_focus_centered() {
730+ self.move_cursor_to_output(&output);
731+ }
676732 }
677733 }
678734 Action::MoveWorkspaceToMonitorRight => {
679735 if let Some(output) = self.niri.output_right() {
680736 self.niri.layout.move_workspace_to_output(&output);
681- self.move_cursor_to_output(&output);
737+ if !self.maybe_warp_cursor_to_focus_centered() {
738+ self.move_cursor_to_output(&output);
739+ }
682740 }
683741 }
684742 Action::MoveWorkspaceToMonitorDown => {
685743 if let Some(output) = self.niri.output_down() {
686744 self.niri.layout.move_workspace_to_output(&output);
687- self.move_cursor_to_output(&output);
745+ if !self.maybe_warp_cursor_to_focus_centered() {
746+ self.move_cursor_to_output(&output);
747+ }
688748 }
689749 }
690750 Action::MoveWorkspaceToMonitorUp => {
691751 if let Some(output) = self.niri.output_up() {
692752 self.niri.layout.move_workspace_to_output(&output);
693- self.move_cursor_to_output(&output);
753+ if !self.maybe_warp_cursor_to_focus_centered() {
754+ self.move_cursor_to_output(&output);
755+ }
694756 }
695757 }
696758 }
src/layout/monitor.rsmodified+8 -0
@@ -583,6 +583,14 @@ impl<W: LayoutElement> Monitor<W> {
583583 self.clean_up_workspaces();
584584 }
585585
586+ /// Returns the geometry of the active tile relative to and clamped to the output.
587+ ///
588+ /// During animations, assumes the final view position.
589+ pub fn active_tile_visual_rectangle(&self) -> Option<Rectangle<i32, Logical>> {
590+ // FIXME: switch gesture.
591+ self.active_workspace_ref().active_tile_visual_rectangle()
592+ }
593+
586594 pub fn window_under(
587595 &self,
588596 pos_within_output: Point<f64, Logical>,
src/layout/workspace.rsmodified+38 -0
@@ -181,6 +181,15 @@ impl OutputId {
181181 }
182182 }
183183
184+impl ViewOffsetAdjustment {
185+ pub fn target_view_offset(&self) -> f64 {
186+ match self {
187+ ViewOffsetAdjustment::Animation(anim) => anim.to(),
188+ ViewOffsetAdjustment::Gesture(gesture) => gesture.current_view_offset,
189+ }
190+ }
191+}
192+
184193 impl ColumnWidth {
185194 fn resolve(self, options: &Options, view_width: i32) -> i32 {
186195 match self {
@@ -1124,6 +1133,31 @@ impl<W: LayoutElement> Workspace<W> {
11241133 first.chain(rest)
11251134 }
11261135
1136+ fn active_column_ref(&self) -> Option<&Column<W>> {
1137+ if self.columns.is_empty() {
1138+ return None;
1139+ }
1140+ Some(&self.columns[self.active_column_idx])
1141+ }
1142+
1143+ /// Returns the geometry of the active tile relative to and clamped to the view.
1144+ ///
1145+ /// During animations, assumes the final view position.
1146+ pub fn active_tile_visual_rectangle(&self) -> Option<Rectangle<i32, Logical>> {
1147+ let col = self.active_column_ref()?;
1148+ let view_pos = self
1149+ .view_offset_adj
1150+ .as_ref()
1151+ .map_or(self.view_offset, |adj| adj.target_view_offset() as i32);
1152+
1153+ let tile_pos = Point::from((-view_pos, col.tile_y(col.active_tile_idx)));
1154+ let tile_size = col.active_tile_ref().tile_size();
1155+ let tile_rect = Rectangle::from_loc_and_size(tile_pos, tile_size);
1156+
1157+ let view = Rectangle::from_loc_and_size((0, 0), self.view_size);
1158+ view.intersection(tile_rect)
1159+ }
1160+
11271161 pub fn window_under(
11281162 &self,
11291163 pos: Point<f64, Logical>,
@@ -2010,6 +2044,10 @@ impl<W: LayoutElement> Column<W> {
20102044 pos
20112045 })
20122046 }
2047+
2048+ fn active_tile_ref(&self) -> &Tile<W> {
2049+ &self.tiles[self.active_tile_idx]
2050+ }
20132051 }
20142052
20152053 fn compute_new_view_offset(
src/niri.rsmodified+78 -2
@@ -112,7 +112,7 @@ use crate::ui::hotkey_overlay::HotkeyOverlay;
112112 use crate::ui::screenshot_ui::{ScreenshotUi, ScreenshotUiRenderElement};
113113 use crate::utils::spawning::CHILD_ENV;
114114 use crate::utils::{
115- center, get_monotonic_time, make_screenshot_path, output_size, write_png_rgba8,
115+ center, center_f64, get_monotonic_time, make_screenshot_path, output_size, write_png_rgba8,
116116 };
117117 use crate::window::Unmapped;
118118 use crate::{animation, niri_render_elements};
@@ -197,7 +197,6 @@ pub struct Niri {
197197 // popup grabs are active (which means the real keyboard focus is on a popup descending from
198198 // this toplevel surface).
199199 pub keyboard_focus: Option<WlSurface>,
200-
201200 pub idle_inhibiting_surfaces: HashSet<WlSurface>,
202201 pub is_fdo_idle_inhibited: Arc<AtomicBool>,
203202
@@ -314,6 +313,11 @@ struct SurfaceFrameThrottlingState {
314313 last_sent_at: RefCell<Option<(Output, u32)>>,
315314 }
316315
316+pub enum CenterCoords {
317+ Seperately,
318+ Both,
319+}
320+
317321 #[derive(Default)]
318322 pub struct WindowOffscreenId(pub RefCell<Option<Id>>);
319323
@@ -401,6 +405,78 @@ impl State {
401405 self.niri.queue_redraw_all();
402406 }
403407
408+ /// Moves cursor within the specified rectangle, only adjusting coordinates if needed.
409+ fn move_cursor_to_rect(&mut self, rect: Rectangle<f64, Logical>, mode: CenterCoords) -> bool {
410+ let pointer = &self.niri.seat.get_pointer().unwrap();
411+ let cur_loc = pointer.current_location();
412+ let x_in_bound = cur_loc.x >= rect.loc.x && cur_loc.x <= rect.loc.x + rect.size.w;
413+ let y_in_bound = cur_loc.y >= rect.loc.y && cur_loc.y <= rect.loc.y + rect.size.h;
414+
415+ let p = match mode {
416+ CenterCoords::Seperately => {
417+ if x_in_bound && y_in_bound {
418+ return false;
419+ } else if y_in_bound {
420+ // adjust x
421+ Point::from((rect.loc.x + rect.size.w / 2.0, cur_loc.y))
422+ } else if x_in_bound {
423+ // adjust y
424+ Point::from((cur_loc.x, rect.loc.y + rect.size.h / 2.0))
425+ } else {
426+ // adjust x and y
427+ center_f64(rect)
428+ }
429+ }
430+ CenterCoords::Both => {
431+ if x_in_bound && y_in_bound {
432+ return false;
433+ } else {
434+ // adjust x and y
435+ center_f64(rect)
436+ }
437+ }
438+ };
439+
440+ self.move_cursor(p);
441+ true
442+ }
443+
444+ pub fn move_cursor_to_focused_tile(&mut self, mode: CenterCoords) -> bool {
445+ let Some(output) = self.niri.layout.active_output() else {
446+ return false;
447+ };
448+ let output = output.clone();
449+ let monitor = self.niri.layout.monitor_for_output(&output).unwrap();
450+
451+ let mut rv = false;
452+ let rect = monitor.active_tile_visual_rectangle();
453+
454+ if let Some(rect) = rect {
455+ let output_geo = self.niri.global_space.output_geometry(&output).unwrap();
456+ let mut rect = rect;
457+ rect.loc += output_geo.loc;
458+ rv = self.move_cursor_to_rect(rect.to_f64(), mode);
459+ }
460+
461+ rv
462+ }
463+
464+ pub fn maybe_warp_cursor_to_focus(&mut self) -> bool {
465+ if !self.niri.config.borrow().input.warp_mouse_to_focus {
466+ return false;
467+ }
468+
469+ self.move_cursor_to_focused_tile(CenterCoords::Seperately)
470+ }
471+
472+ pub fn maybe_warp_cursor_to_focus_centered(&mut self) -> bool {
473+ if !self.niri.config.borrow().input.warp_mouse_to_focus {
474+ return false;
475+ }
476+
477+ self.move_cursor_to_focused_tile(CenterCoords::Both)
478+ }
479+
404480 pub fn refresh_pointer_focus(&mut self) {
405481 let _span = tracy_client::span!("Niri::refresh_pointer_focus");
406482
src/utils/mod.rsmodified+4 -0
@@ -40,6 +40,10 @@ pub fn center(rect: Rectangle<i32, Logical>) -> Point<i32, Logical> {
4040 rect.loc + rect.size.downscale(2).to_point()
4141 }
4242
43+pub fn center_f64(rect: Rectangle<f64, Logical>) -> Point<f64, Logical> {
44+ rect.loc + rect.size.downscale(2.0).to_point()
45+}
46+
4347 pub fn output_size(output: &Output) -> Size<i32, Logical> {
4448 let output_scale = output.current_scale().integer_scale();
4549 let output_transform = output.current_transform();