Implement warp-mouse-to-focus
Browse files at this commit741bee461cbdaf0236eaa55dc86dc84b01613128Parent: 0c578157 changed files +214 -18
- niri-config/src/lib.rs +5 -0
- resources/default-config.kdl +3 -0
- src/input.rs +78 -16
- src/layout/monitor.rs +8 -0
- src/layout/workspace.rs +38 -0
- src/niri.rs +78 -2
- src/utils/mod.rs +4 -0
niri-config/src/lib.rsmodified+5 -0
| @@ -69,6 +69,8 @@ pub struct Input { | ||
| 69 | 69 | pub touch: Touch, |
| 70 | 70 | #[knuffel(child)] |
| 71 | 71 | pub disable_power_key_handling: bool, |
| 72 | + #[knuffel(child)] | |
| 73 | + pub warp_mouse_to_focus: bool, | |
| 72 | 74 | } |
| 73 | 75 | |
| 74 | 76 | #[derive(knuffel::Decode, Debug, Default, PartialEq, Eq)] |
| @@ -1592,6 +1594,8 @@ mod tests { | ||
| 1592 | 1594 | } |
| 1593 | 1595 | |
| 1594 | 1596 | disable-power-key-handling |
| 1597 | + | |
| 1598 | + warp-mouse-to-focus | |
| 1595 | 1599 | } |
| 1596 | 1600 | |
| 1597 | 1601 | output "eDP-1" { |
| @@ -1731,6 +1735,7 @@ mod tests { | ||
| 1731 | 1735 | map_to_output: Some("eDP-1".to_owned()), |
| 1732 | 1736 | }, |
| 1733 | 1737 | disable_power_key_handling: true, |
| 1738 | + warp_mouse_to_focus: true, | |
| 1734 | 1739 | }, |
| 1735 | 1740 | outputs: vec![Output { |
| 1736 | 1741 | off: false, |
resources/default-config.kdlmodified+3 -0
| @@ -67,6 +67,9 @@ input { | ||
| 67 | 67 | // Uncomment this if you would like to configure the power button elsewhere |
| 68 | 68 | // (i.e. logind.conf). |
| 69 | 69 | // 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 | |
| 70 | 73 | } |
| 71 | 74 | |
| 72 | 75 | // You can configure outputs by their name, which you can find |
src/input.rsmodified+78 -16
| @@ -411,139 +411,166 @@ impl State { | ||
| 411 | 411 | } |
| 412 | 412 | Action::MoveColumnLeft => { |
| 413 | 413 | self.niri.layout.move_left(); |
| 414 | + self.maybe_warp_cursor_to_focus(); | |
| 414 | 415 | // FIXME: granular |
| 415 | 416 | self.niri.queue_redraw_all(); |
| 416 | 417 | } |
| 417 | 418 | Action::MoveColumnRight => { |
| 418 | 419 | self.niri.layout.move_right(); |
| 420 | + self.maybe_warp_cursor_to_focus(); | |
| 419 | 421 | // FIXME: granular |
| 420 | 422 | self.niri.queue_redraw_all(); |
| 421 | 423 | } |
| 422 | 424 | Action::MoveColumnToFirst => { |
| 423 | 425 | self.niri.layout.move_column_to_first(); |
| 426 | + self.maybe_warp_cursor_to_focus(); | |
| 424 | 427 | // FIXME: granular |
| 425 | 428 | self.niri.queue_redraw_all(); |
| 426 | 429 | } |
| 427 | 430 | Action::MoveColumnToLast => { |
| 428 | 431 | self.niri.layout.move_column_to_last(); |
| 432 | + self.maybe_warp_cursor_to_focus(); | |
| 429 | 433 | // FIXME: granular |
| 430 | 434 | self.niri.queue_redraw_all(); |
| 431 | 435 | } |
| 432 | 436 | Action::MoveWindowDown => { |
| 433 | 437 | self.niri.layout.move_down(); |
| 438 | + self.maybe_warp_cursor_to_focus(); | |
| 434 | 439 | // FIXME: granular |
| 435 | 440 | self.niri.queue_redraw_all(); |
| 436 | 441 | } |
| 437 | 442 | Action::MoveWindowUp => { |
| 438 | 443 | self.niri.layout.move_up(); |
| 444 | + self.maybe_warp_cursor_to_focus(); | |
| 439 | 445 | // FIXME: granular |
| 440 | 446 | self.niri.queue_redraw_all(); |
| 441 | 447 | } |
| 442 | 448 | Action::MoveWindowDownOrToWorkspaceDown => { |
| 443 | 449 | self.niri.layout.move_down_or_to_workspace_down(); |
| 450 | + self.maybe_warp_cursor_to_focus(); | |
| 444 | 451 | // FIXME: granular |
| 445 | 452 | self.niri.queue_redraw_all(); |
| 446 | 453 | } |
| 447 | 454 | Action::MoveWindowUpOrToWorkspaceUp => { |
| 448 | 455 | self.niri.layout.move_up_or_to_workspace_up(); |
| 456 | + self.maybe_warp_cursor_to_focus(); | |
| 449 | 457 | // FIXME: granular |
| 450 | 458 | self.niri.queue_redraw_all(); |
| 451 | 459 | } |
| 452 | 460 | Action::ConsumeOrExpelWindowLeft => { |
| 453 | 461 | self.niri.layout.consume_or_expel_window_left(); |
| 462 | + self.maybe_warp_cursor_to_focus(); | |
| 454 | 463 | // FIXME: granular |
| 455 | 464 | self.niri.queue_redraw_all(); |
| 456 | 465 | } |
| 457 | 466 | Action::ConsumeOrExpelWindowRight => { |
| 458 | 467 | self.niri.layout.consume_or_expel_window_right(); |
| 468 | + self.maybe_warp_cursor_to_focus(); | |
| 459 | 469 | // FIXME: granular |
| 460 | 470 | self.niri.queue_redraw_all(); |
| 461 | 471 | } |
| 462 | 472 | Action::FocusColumnLeft => { |
| 463 | 473 | self.niri.layout.focus_left(); |
| 474 | + self.maybe_warp_cursor_to_focus(); | |
| 464 | 475 | // FIXME: granular |
| 465 | 476 | self.niri.queue_redraw_all(); |
| 466 | 477 | } |
| 467 | 478 | Action::FocusColumnRight => { |
| 468 | 479 | self.niri.layout.focus_right(); |
| 480 | + self.maybe_warp_cursor_to_focus(); | |
| 469 | 481 | // FIXME: granular |
| 470 | 482 | self.niri.queue_redraw_all(); |
| 471 | 483 | } |
| 472 | 484 | Action::FocusColumnFirst => { |
| 473 | 485 | self.niri.layout.focus_column_first(); |
| 486 | + self.maybe_warp_cursor_to_focus(); | |
| 474 | 487 | // FIXME: granular |
| 475 | 488 | self.niri.queue_redraw_all(); |
| 476 | 489 | } |
| 477 | 490 | Action::FocusColumnLast => { |
| 478 | 491 | self.niri.layout.focus_column_last(); |
| 492 | + self.maybe_warp_cursor_to_focus(); | |
| 479 | 493 | // FIXME: granular |
| 480 | 494 | self.niri.queue_redraw_all(); |
| 481 | 495 | } |
| 482 | 496 | Action::FocusWindowDown => { |
| 483 | 497 | self.niri.layout.focus_down(); |
| 498 | + self.maybe_warp_cursor_to_focus(); | |
| 484 | 499 | // FIXME: granular |
| 485 | 500 | self.niri.queue_redraw_all(); |
| 486 | 501 | } |
| 487 | 502 | Action::FocusWindowUp => { |
| 488 | 503 | self.niri.layout.focus_up(); |
| 504 | + self.maybe_warp_cursor_to_focus(); | |
| 489 | 505 | // FIXME: granular |
| 490 | 506 | self.niri.queue_redraw_all(); |
| 491 | 507 | } |
| 492 | 508 | Action::FocusWindowOrWorkspaceDown => { |
| 493 | 509 | self.niri.layout.focus_window_or_workspace_down(); |
| 510 | + self.maybe_warp_cursor_to_focus(); | |
| 494 | 511 | // FIXME: granular |
| 495 | 512 | self.niri.queue_redraw_all(); |
| 496 | 513 | } |
| 497 | 514 | Action::FocusWindowOrWorkspaceUp => { |
| 498 | 515 | self.niri.layout.focus_window_or_workspace_up(); |
| 516 | + self.maybe_warp_cursor_to_focus(); | |
| 499 | 517 | // FIXME: granular |
| 500 | 518 | self.niri.queue_redraw_all(); |
| 501 | 519 | } |
| 502 | 520 | Action::MoveWindowToWorkspaceDown => { |
| 503 | 521 | self.niri.layout.move_to_workspace_down(); |
| 522 | + self.maybe_warp_cursor_to_focus(); | |
| 504 | 523 | // FIXME: granular |
| 505 | 524 | self.niri.queue_redraw_all(); |
| 506 | 525 | } |
| 507 | 526 | Action::MoveWindowToWorkspaceUp => { |
| 508 | 527 | self.niri.layout.move_to_workspace_up(); |
| 528 | + self.maybe_warp_cursor_to_focus(); | |
| 509 | 529 | // FIXME: granular |
| 510 | 530 | self.niri.queue_redraw_all(); |
| 511 | 531 | } |
| 512 | 532 | Action::MoveWindowToWorkspace(idx) => { |
| 513 | 533 | let idx = idx.saturating_sub(1) as usize; |
| 514 | 534 | self.niri.layout.move_to_workspace(idx); |
| 535 | + self.maybe_warp_cursor_to_focus(); | |
| 515 | 536 | // FIXME: granular |
| 516 | 537 | self.niri.queue_redraw_all(); |
| 517 | 538 | } |
| 518 | 539 | Action::MoveColumnToWorkspaceDown => { |
| 519 | 540 | self.niri.layout.move_column_to_workspace_down(); |
| 541 | + self.maybe_warp_cursor_to_focus(); | |
| 520 | 542 | // FIXME: granular |
| 521 | 543 | self.niri.queue_redraw_all(); |
| 522 | 544 | } |
| 523 | 545 | Action::MoveColumnToWorkspaceUp => { |
| 524 | 546 | self.niri.layout.move_column_to_workspace_up(); |
| 547 | + self.maybe_warp_cursor_to_focus(); | |
| 525 | 548 | // FIXME: granular |
| 526 | 549 | self.niri.queue_redraw_all(); |
| 527 | 550 | } |
| 528 | 551 | Action::MoveColumnToWorkspace(idx) => { |
| 529 | 552 | let idx = idx.saturating_sub(1) as usize; |
| 530 | 553 | self.niri.layout.move_column_to_workspace(idx); |
| 554 | + self.maybe_warp_cursor_to_focus(); | |
| 531 | 555 | // FIXME: granular |
| 532 | 556 | self.niri.queue_redraw_all(); |
| 533 | 557 | } |
| 534 | 558 | Action::FocusWorkspaceDown => { |
| 535 | 559 | self.niri.layout.switch_workspace_down(); |
| 560 | + self.maybe_warp_cursor_to_focus(); | |
| 536 | 561 | // FIXME: granular |
| 537 | 562 | self.niri.queue_redraw_all(); |
| 538 | 563 | } |
| 539 | 564 | Action::FocusWorkspaceUp => { |
| 540 | 565 | self.niri.layout.switch_workspace_up(); |
| 566 | + self.maybe_warp_cursor_to_focus(); | |
| 541 | 567 | // FIXME: granular |
| 542 | 568 | self.niri.queue_redraw_all(); |
| 543 | 569 | } |
| 544 | 570 | Action::FocusWorkspace(idx) => { |
| 545 | 571 | let idx = idx.saturating_sub(1) as usize; |
| 546 | 572 | self.niri.layout.switch_workspace(idx); |
| 573 | + self.maybe_warp_cursor_to_focus(); | |
| 547 | 574 | // FIXME: granular |
| 548 | 575 | self.niri.queue_redraw_all(); |
| 549 | 576 | } |
| @@ -559,11 +586,14 @@ impl State { | ||
| 559 | 586 | } |
| 560 | 587 | Action::ConsumeWindowIntoColumn => { |
| 561 | 588 | 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. | |
| 562 | 591 | // FIXME: granular |
| 563 | 592 | self.niri.queue_redraw_all(); |
| 564 | 593 | } |
| 565 | 594 | Action::ExpelWindowFromColumn => { |
| 566 | 595 | self.niri.layout.expel_from_column(); |
| 596 | + self.maybe_warp_cursor_to_focus(); | |
| 567 | 597 | // FIXME: granular |
| 568 | 598 | self.niri.queue_redraw_all(); |
| 569 | 599 | } |
| @@ -581,81 +611,105 @@ impl State { | ||
| 581 | 611 | Action::FocusMonitorLeft => { |
| 582 | 612 | if let Some(output) = self.niri.output_left() { |
| 583 | 613 | 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 | + } | |
| 585 | 617 | } |
| 586 | 618 | } |
| 587 | 619 | Action::FocusMonitorRight => { |
| 588 | 620 | if let Some(output) = self.niri.output_right() { |
| 589 | 621 | 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 | + } | |
| 591 | 625 | } |
| 592 | 626 | } |
| 593 | 627 | Action::FocusMonitorDown => { |
| 594 | 628 | if let Some(output) = self.niri.output_down() { |
| 595 | 629 | 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 | + } | |
| 597 | 633 | } |
| 598 | 634 | } |
| 599 | 635 | Action::FocusMonitorUp => { |
| 600 | 636 | if let Some(output) = self.niri.output_up() { |
| 601 | 637 | 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 | + } | |
| 603 | 641 | } |
| 604 | 642 | } |
| 605 | 643 | Action::MoveWindowToMonitorLeft => { |
| 606 | 644 | if let Some(output) = self.niri.output_left() { |
| 607 | 645 | self.niri.layout.move_to_output(&output); |
| 608 | 646 | 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 | + } | |
| 610 | 650 | } |
| 611 | 651 | } |
| 612 | 652 | Action::MoveWindowToMonitorRight => { |
| 613 | 653 | if let Some(output) = self.niri.output_right() { |
| 614 | 654 | self.niri.layout.move_to_output(&output); |
| 615 | 655 | 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 | + } | |
| 617 | 659 | } |
| 618 | 660 | } |
| 619 | 661 | Action::MoveWindowToMonitorDown => { |
| 620 | 662 | if let Some(output) = self.niri.output_down() { |
| 621 | 663 | self.niri.layout.move_to_output(&output); |
| 622 | 664 | 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 | + } | |
| 624 | 668 | } |
| 625 | 669 | } |
| 626 | 670 | Action::MoveWindowToMonitorUp => { |
| 627 | 671 | if let Some(output) = self.niri.output_up() { |
| 628 | 672 | self.niri.layout.move_to_output(&output); |
| 629 | 673 | 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 | + } | |
| 631 | 677 | } |
| 632 | 678 | } |
| 633 | 679 | Action::MoveColumnToMonitorLeft => { |
| 634 | 680 | if let Some(output) = self.niri.output_left() { |
| 635 | 681 | self.niri.layout.move_column_to_output(&output); |
| 636 | 682 | 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 | + } | |
| 638 | 686 | } |
| 639 | 687 | } |
| 640 | 688 | Action::MoveColumnToMonitorRight => { |
| 641 | 689 | if let Some(output) = self.niri.output_right() { |
| 642 | 690 | self.niri.layout.move_column_to_output(&output); |
| 643 | 691 | 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 | + } | |
| 645 | 695 | } |
| 646 | 696 | } |
| 647 | 697 | Action::MoveColumnToMonitorDown => { |
| 648 | 698 | if let Some(output) = self.niri.output_down() { |
| 649 | 699 | self.niri.layout.move_column_to_output(&output); |
| 650 | 700 | 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 | + } | |
| 652 | 704 | } |
| 653 | 705 | } |
| 654 | 706 | Action::MoveColumnToMonitorUp => { |
| 655 | 707 | if let Some(output) = self.niri.output_up() { |
| 656 | 708 | self.niri.layout.move_column_to_output(&output); |
| 657 | 709 | 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 | + } | |
| 659 | 713 | } |
| 660 | 714 | } |
| 661 | 715 | Action::SetColumnWidth(change) => { |
| @@ -672,25 +726,33 @@ impl State { | ||
| 672 | 726 | Action::MoveWorkspaceToMonitorLeft => { |
| 673 | 727 | if let Some(output) = self.niri.output_left() { |
| 674 | 728 | 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 | + } | |
| 676 | 732 | } |
| 677 | 733 | } |
| 678 | 734 | Action::MoveWorkspaceToMonitorRight => { |
| 679 | 735 | if let Some(output) = self.niri.output_right() { |
| 680 | 736 | 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 | + } | |
| 682 | 740 | } |
| 683 | 741 | } |
| 684 | 742 | Action::MoveWorkspaceToMonitorDown => { |
| 685 | 743 | if let Some(output) = self.niri.output_down() { |
| 686 | 744 | 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 | + } | |
| 688 | 748 | } |
| 689 | 749 | } |
| 690 | 750 | Action::MoveWorkspaceToMonitorUp => { |
| 691 | 751 | if let Some(output) = self.niri.output_up() { |
| 692 | 752 | 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 | + } | |
| 694 | 756 | } |
| 695 | 757 | } |
| 696 | 758 | } |
src/layout/monitor.rsmodified+8 -0
| @@ -583,6 +583,14 @@ impl<W: LayoutElement> Monitor<W> { | ||
| 583 | 583 | self.clean_up_workspaces(); |
| 584 | 584 | } |
| 585 | 585 | |
| 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 | + | |
| 586 | 594 | pub fn window_under( |
| 587 | 595 | &self, |
| 588 | 596 | pos_within_output: Point<f64, Logical>, |
src/layout/workspace.rsmodified+38 -0
| @@ -181,6 +181,15 @@ impl OutputId { | ||
| 181 | 181 | } |
| 182 | 182 | } |
| 183 | 183 | |
| 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 | + | |
| 184 | 193 | impl ColumnWidth { |
| 185 | 194 | fn resolve(self, options: &Options, view_width: i32) -> i32 { |
| 186 | 195 | match self { |
| @@ -1124,6 +1133,31 @@ impl<W: LayoutElement> Workspace<W> { | ||
| 1124 | 1133 | first.chain(rest) |
| 1125 | 1134 | } |
| 1126 | 1135 | |
| 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 | + | |
| 1127 | 1161 | pub fn window_under( |
| 1128 | 1162 | &self, |
| 1129 | 1163 | pos: Point<f64, Logical>, |
| @@ -2010,6 +2044,10 @@ impl<W: LayoutElement> Column<W> { | ||
| 2010 | 2044 | pos |
| 2011 | 2045 | }) |
| 2012 | 2046 | } |
| 2047 | + | |
| 2048 | + fn active_tile_ref(&self) -> &Tile<W> { | |
| 2049 | + &self.tiles[self.active_tile_idx] | |
| 2050 | + } | |
| 2013 | 2051 | } |
| 2014 | 2052 | |
| 2015 | 2053 | fn compute_new_view_offset( |
src/niri.rsmodified+78 -2
| @@ -112,7 +112,7 @@ use crate::ui::hotkey_overlay::HotkeyOverlay; | ||
| 112 | 112 | use crate::ui::screenshot_ui::{ScreenshotUi, ScreenshotUiRenderElement}; |
| 113 | 113 | use crate::utils::spawning::CHILD_ENV; |
| 114 | 114 | 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, | |
| 116 | 116 | }; |
| 117 | 117 | use crate::window::Unmapped; |
| 118 | 118 | use crate::{animation, niri_render_elements}; |
| @@ -197,7 +197,6 @@ pub struct Niri { | ||
| 197 | 197 | // popup grabs are active (which means the real keyboard focus is on a popup descending from |
| 198 | 198 | // this toplevel surface). |
| 199 | 199 | pub keyboard_focus: Option<WlSurface>, |
| 200 | - | |
| 201 | 200 | pub idle_inhibiting_surfaces: HashSet<WlSurface>, |
| 202 | 201 | pub is_fdo_idle_inhibited: Arc<AtomicBool>, |
| 203 | 202 | |
| @@ -314,6 +313,11 @@ struct SurfaceFrameThrottlingState { | ||
| 314 | 313 | last_sent_at: RefCell<Option<(Output, u32)>>, |
| 315 | 314 | } |
| 316 | 315 | |
| 316 | +pub enum CenterCoords { | |
| 317 | + Seperately, | |
| 318 | + Both, | |
| 319 | +} | |
| 320 | + | |
| 317 | 321 | #[derive(Default)] |
| 318 | 322 | pub struct WindowOffscreenId(pub RefCell<Option<Id>>); |
| 319 | 323 | |
| @@ -401,6 +405,78 @@ impl State { | ||
| 401 | 405 | self.niri.queue_redraw_all(); |
| 402 | 406 | } |
| 403 | 407 | |
| 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 | + | |
| 404 | 480 | pub fn refresh_pointer_focus(&mut self) { |
| 405 | 481 | let _span = tracy_client::span!("Niri::refresh_pointer_focus"); |
| 406 | 482 |
src/utils/mod.rsmodified+4 -0
| @@ -40,6 +40,10 @@ pub fn center(rect: Rectangle<i32, Logical>) -> Point<i32, Logical> { | ||
| 40 | 40 | rect.loc + rect.size.downscale(2).to_point() |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | +pub fn center_f64(rect: Rectangle<f64, Logical>) -> Point<f64, Logical> { | |
| 44 | + rect.loc + rect.size.downscale(2.0).to_point() | |
| 45 | +} | |
| 46 | + | |
| 43 | 47 | pub fn output_size(output: &Output) -> Size<i32, Logical> { |
| 44 | 48 | let output_scale = output.current_scale().integer_scale(); |
| 45 | 49 | let output_transform = output.current_transform(); |