Extract pointer_pos_for_window_cast()
Browse files at this commit0fb6c5706bbbed78ef4abee846f5e896d7e14ee5Parent: 79aaa4c1 changed file +61 -50
- src/niri.rs +61 -50
src/niri.rsmodified+61 -50
| @@ -3940,6 +3940,66 @@ impl Niri { | |||
| 3940 | } | 3940 | } |
| 3941 | } | 3941 | } |
| 3942 | 3942 | ||
| 3943 | + /// Checks if the pointer should be included on a window cast or screenshot. | ||
| 3944 | + /// | ||
| 3945 | + /// Returns `(cursor_global_pos, win_pos)` if the pointer should be included, or `None` | ||
| 3946 | + /// otherwise. | ||
| 3947 | + pub fn pointer_pos_for_window_cast( | ||
| 3948 | + &self, | ||
| 3949 | + mapped: &Mapped, | ||
| 3950 | + ) -> Option<(Point<f64, Logical>, Point<f64, Logical>)> { | ||
| 3951 | + // Tablet cursor. | ||
| 3952 | + if let Some(tablet_pos) = self.tablet_cursor_location { | ||
| 3953 | + let contents = self.contents_under(tablet_pos); | ||
| 3954 | + if let Some((w, HitType::Input { win_pos })) = contents.window { | ||
| 3955 | + if w == mapped.window { | ||
| 3956 | + // Tablet tools don't currently expose current focus, and don't currently | ||
| 3957 | + // have grabs. When those are implemented, this branch should be adjusted | ||
| 3958 | + // to look more similar to the branch below. | ||
| 3959 | + return Some((tablet_pos, win_pos)); | ||
| 3960 | + } | ||
| 3961 | + } | ||
| 3962 | + } | ||
| 3963 | + // Regular cursor. | ||
| 3964 | + else if let Some((w, HitType::Input { win_pos })) = &self.pointer_contents.window { | ||
| 3965 | + if w == &mapped.window { | ||
| 3966 | + // Grabs can modify the pointer focus, making it different from | ||
| 3967 | + // pointer_contents. Notably, gestures like Mod+MMB will remove the pointer | ||
| 3968 | + // focus, and ClickGrab will keep pointer focus on the clicked window even | ||
| 3969 | + // while it's moving over a different window. | ||
| 3970 | + // | ||
| 3971 | + // So, double-check that current_focus() (after grabs) also matches the pointer | ||
| 3972 | + // contents. | ||
| 3973 | + let pointer = self.seat.get_pointer().unwrap(); | ||
| 3974 | + | ||
| 3975 | + // The DnD grab is a bit special because it has its own focus (data device) | ||
| 3976 | + // while the pointer focus is cleared. That focus is not currently exposed from | ||
| 3977 | + // Smithay, and showing DnD icons on window screenshots seems useful, so let's | ||
| 3978 | + // just allow it during DnD grabs. | ||
| 3979 | + let is_dnd_grab = pointer | ||
| 3980 | + .with_grab(|_, grab| State::is_dnd_grab(grab.as_any())) | ||
| 3981 | + .unwrap_or(false); | ||
| 3982 | + | ||
| 3983 | + let current_focus_matches = is_dnd_grab | ||
| 3984 | + || pointer | ||
| 3985 | + .current_focus() | ||
| 3986 | + .map(|focused| self.find_root_shell_surface(&focused)) | ||
| 3987 | + .is_some_and(|focused| mapped.is_wl_surface(&focused)); | ||
| 3988 | + if current_focus_matches { | ||
| 3989 | + // We don't check for pointer visibility because it can only be Visible or | ||
| 3990 | + // Hidden, and never Disabled (then it wouldn't have focus). Even when the | ||
| 3991 | + // pointer is Hidden, we want to render it, since the user explicitly | ||
| 3992 | + // requested show_pointer = true, and otherwise there's no easy way to | ||
| 3993 | + // screenshot a window with pointer with hide-when-typing because pressing | ||
| 3994 | + // the screenshot bind will hide the pointer. | ||
| 3995 | + return Some((pointer.current_location(), *win_pos)); | ||
| 3996 | + } | ||
| 3997 | + } | ||
| 3998 | + } | ||
| 3999 | + | ||
| 4000 | + None | ||
| 4001 | + } | ||
| 4002 | + | ||
| 3943 | pub fn refresh_pointer_outputs(&mut self) { | 4003 | pub fn refresh_pointer_outputs(&mut self) { |
| 3944 | if !self.pointer_visibility.is_visible() { | 4004 | if !self.pointer_visibility.is_visible() { |
| 3945 | return; | 4005 | return; |
| @@ -5684,7 +5744,7 @@ impl Niri { | |||
| 5684 | 5744 | ||
| 5685 | // Add pointer if requested and it's over this window. | 5745 | // Add pointer if requested and it's over this window. |
| 5686 | if show_pointer { | 5746 | if show_pointer { |
| 5687 | - let mut render = |win_pos: Point<f64, Logical>| { | 5747 | + if let Some((_, win_pos)) = self.pointer_pos_for_window_cast(mapped) { |
| 5688 | // Pointer elements are at output-local physical coords. | 5748 | // Pointer elements are at output-local physical coords. |
| 5689 | // Relocate by -win_pos to make them window-relative. | 5749 | // Relocate by -win_pos to make them window-relative. |
| 5690 | let pos = win_pos.to_physical_precise_round(scale).upscale(-1); | 5750 | let pos = win_pos.to_physical_precise_round(scale).upscale(-1); |
| @@ -5692,55 +5752,6 @@ impl Niri { | |||
| 5692 | let elem = RelocateRenderElement::from_element(elem, pos, Relocate::Relative); | 5752 | let elem = RelocateRenderElement::from_element(elem, pos, Relocate::Relative); |
| 5693 | elements.push(elem.into()); | 5753 | elements.push(elem.into()); |
| 5694 | }); | 5754 | }); |
| 5695 | - }; | ||
| 5696 | - | ||
| 5697 | - // Tablet cursor. | ||
| 5698 | - if let Some(tablet_pos) = self.tablet_cursor_location { | ||
| 5699 | - let contents = self.contents_under(tablet_pos); | ||
| 5700 | - if let Some((w, HitType::Input { win_pos })) = contents.window { | ||
| 5701 | - if w == mapped.window { | ||
| 5702 | - // Tablet tools don't currently expose current focus, and don't currently | ||
| 5703 | - // have grabs. When those are implemented, this branch should be adjusted | ||
| 5704 | - // to look more similar to the branch below. | ||
| 5705 | - render(win_pos); | ||
| 5706 | - } | ||
| 5707 | - } | ||
| 5708 | - } | ||
| 5709 | - // Regular cursor. | ||
| 5710 | - else if let Some((w, HitType::Input { win_pos })) = &self.pointer_contents.window { | ||
| 5711 | - if w == &mapped.window { | ||
| 5712 | - // Grabs can modify the pointer focus, making it different from | ||
| 5713 | - // pointer_contents. Notably, gestures like Mod+MMB will remove the pointer | ||
| 5714 | - // focus, and ClickGrab will keep pointer focus on the clicked window even | ||
| 5715 | - // while it's moving over a different window. | ||
| 5716 | - // | ||
| 5717 | - // So, double-check that current_focus() (after grabs) also matches the pointer | ||
| 5718 | - // contents. | ||
| 5719 | - let pointer = self.seat.get_pointer().unwrap(); | ||
| 5720 | - | ||
| 5721 | - // The DnD grab is a bit special because it has its own focus (data device) | ||
| 5722 | - // while the pointer focus is cleared. That focus is not currently exposed from | ||
| 5723 | - // Smithay, and showing DnD icons on window screenshots seems useful, so let's | ||
| 5724 | - // just allow it during DnD grabs. | ||
| 5725 | - let is_dnd_grab = pointer | ||
| 5726 | - .with_grab(|_, grab| State::is_dnd_grab(grab.as_any())) | ||
| 5727 | - .unwrap_or(false); | ||
| 5728 | - | ||
| 5729 | - let current_focus_matches = is_dnd_grab | ||
| 5730 | - || pointer | ||
| 5731 | - .current_focus() | ||
| 5732 | - .map(|focused| self.find_root_shell_surface(&focused)) | ||
| 5733 | - .is_some_and(|focused| mapped.is_wl_surface(&focused)); | ||
| 5734 | - if current_focus_matches { | ||
| 5735 | - // We don't check for pointer visibility because it can only be Visible or | ||
| 5736 | - // Hidden, and never Disabled (then it wouldn't have focus). Even when the | ||
| 5737 | - // pointer is Hidden, we want to render it, since the user explicitly | ||
| 5738 | - // requested show_pointer = true, and otherwise there's no easy way to | ||
| 5739 | - // screenshot a window with pointer with hide-when-typing because pressing | ||
| 5740 | - // the screenshot bind will hide the pointer. | ||
| 5741 | - render(*win_pos); | ||
| 5742 | - } | ||
| 5743 | - } | ||
| 5744 | } | 5755 | } |
| 5745 | } | 5756 | } |
| 5746 | let pointer_count = elements.len(); | 5757 | let pointer_count = elements.len(); |