pinnacle/src/focus.rs

382 lines
11 KiB
Rust
Raw Normal View History

2023-08-01 11:06:35 -05:00
// SPDX-License-Identifier: GPL-3.0-or-later
2023-06-25 17:18:50 -05:00
use smithay::{
2023-08-11 18:48:51 -05:00
desktop::{LayerSurface, PopupKind, Space},
input::{
keyboard::KeyboardTarget,
pointer::{MotionEvent, PointerTarget},
Seat,
},
output::Output,
reexports::wayland_server::{protocol::wl_surface::WlSurface, Resource},
2023-09-12 09:38:34 -05:00
utils::{IsAlive, SERIAL_COUNTER},
wayland::seat::WaylandFocus,
};
2023-07-24 18:59:05 -05:00
2023-09-09 22:47:59 -05:00
use crate::{
state::{State, WithState},
window::WindowElement,
};
2023-06-17 21:02:58 -05:00
#[derive(Default)]
pub struct FocusState {
2023-08-11 18:48:51 -05:00
pub focus_stack: Vec<WindowElement>,
2023-06-24 17:39:40 -05:00
pub focused_output: Option<Output>,
2023-06-17 21:02:58 -05:00
}
2023-09-09 22:47:59 -05:00
impl State {
/// Get the currently focused window on `output`, if any.
2023-09-15 02:50:42 -05:00
pub fn focused_window(&mut self, output: &Output) -> Option<WindowElement> {
2023-09-09 22:47:59 -05:00
self.focus_state.focus_stack.retain(|win| win.alive());
let mut windows = self.focus_state.focus_stack.iter().rev().filter(|win| {
let win_tags = win.with_state(|state| state.tags.clone());
let output_tags =
output.with_state(|state| state.focused_tags().cloned().collect::<Vec<_>>());
win_tags
.iter()
.any(|win_tag| output_tags.iter().any(|op_tag| win_tag == op_tag))
});
windows.next().cloned()
}
2023-09-12 09:38:34 -05:00
/// Update the focus. This will raise the current focus and activate it,
/// as well as setting the keyboard focus to it.
pub fn update_focus(&mut self, output: &Output) {
2023-09-15 02:50:42 -05:00
let current_focus = self.focused_window(output);
2023-09-12 09:38:34 -05:00
if let Some(win) = &current_focus {
self.space.raise_element(win, true);
2023-09-15 02:50:42 -05:00
if let WindowElement::Wayland(w) = win {
w.toplevel().send_configure();
}
2023-09-12 09:38:34 -05:00
}
self.seat.get_keyboard().expect("no keyboard").set_focus(
self,
current_focus.map(|win| win.into()),
SERIAL_COUNTER.next_serial(),
);
// TODO: if there already is a visible focused window, don't do anything
2023-09-12 09:38:34 -05:00
}
2023-09-09 22:47:59 -05:00
}
2023-06-17 21:02:58 -05:00
impl FocusState {
pub fn new() -> Self {
Default::default()
}
2023-06-30 21:34:07 -05:00
/// Set the currently focused window.
2023-07-24 18:59:05 -05:00
pub fn set_focus(&mut self, window: WindowElement) {
2023-06-17 21:02:58 -05:00
self.focus_stack.retain(|win| win != &window);
self.focus_stack.push(window);
}
2023-08-11 18:48:51 -05:00
/// Fix focus layering for all windows in the `focus_stack`.
///
2023-09-09 22:47:59 -05:00
/// This will call `space.raise_element` on all windows from back
/// to front to correct their z locations.
2023-08-11 18:48:51 -05:00
pub fn fix_up_focus(&self, space: &mut Space<WindowElement>) {
for win in self.focus_stack.iter() {
2023-09-02 20:26:11 -05:00
space.raise_element(win, false);
2023-08-11 18:48:51 -05:00
}
}
2023-06-17 21:02:58 -05:00
}
#[derive(Debug, Clone, PartialEq)]
pub enum FocusTarget {
Window(WindowElement),
Popup(PopupKind),
2023-08-04 18:48:10 -05:00
LayerSurface(LayerSurface),
}
impl IsAlive for FocusTarget {
fn alive(&self) -> bool {
match self {
FocusTarget::Window(window) => window.alive(),
FocusTarget::Popup(popup) => popup.alive(),
2023-08-04 18:48:10 -05:00
FocusTarget::LayerSurface(surf) => surf.alive(),
}
}
}
2023-07-28 15:16:25 -05:00
impl TryFrom<FocusTarget> for WlSurface {
type Error = ();
fn try_from(value: FocusTarget) -> Result<Self, Self::Error> {
value.wl_surface().ok_or(())
}
}
2023-08-28 22:53:24 -05:00
impl PointerTarget<State> for FocusTarget {
fn enter(&self, seat: &Seat<State>, data: &mut State, event: &MotionEvent) {
match self {
FocusTarget::Window(window) => PointerTarget::enter(window, seat, data, event),
FocusTarget::Popup(popup) => {
PointerTarget::enter(popup.wl_surface(), seat, data, event);
}
2023-08-04 18:48:10 -05:00
FocusTarget::LayerSurface(surf) => PointerTarget::enter(surf, seat, data, event),
}
}
2023-08-28 22:53:24 -05:00
fn motion(&self, seat: &Seat<State>, data: &mut State, event: &MotionEvent) {
match self {
FocusTarget::Window(window) => PointerTarget::motion(window, seat, data, event),
FocusTarget::Popup(popup) => {
PointerTarget::motion(popup.wl_surface(), seat, data, event);
}
2023-08-04 18:48:10 -05:00
FocusTarget::LayerSurface(surf) => PointerTarget::motion(surf, seat, data, event),
}
}
fn relative_motion(
&self,
2023-08-28 22:53:24 -05:00
seat: &Seat<State>,
data: &mut State,
event: &smithay::input::pointer::RelativeMotionEvent,
) {
match self {
FocusTarget::Window(window) => {
PointerTarget::relative_motion(window, seat, data, event);
}
FocusTarget::Popup(popup) => {
PointerTarget::relative_motion(popup.wl_surface(), seat, data, event);
}
2023-08-04 18:48:10 -05:00
FocusTarget::LayerSurface(surf) => {
PointerTarget::relative_motion(surf, seat, data, event);
}
}
}
fn button(
&self,
2023-08-28 22:53:24 -05:00
seat: &Seat<State>,
data: &mut State,
event: &smithay::input::pointer::ButtonEvent,
) {
match self {
FocusTarget::Window(window) => PointerTarget::button(window, seat, data, event),
FocusTarget::Popup(popup) => {
PointerTarget::button(popup.wl_surface(), seat, data, event);
}
2023-08-04 18:48:10 -05:00
FocusTarget::LayerSurface(surf) => PointerTarget::button(surf, seat, data, event),
}
}
fn axis(
&self,
2023-08-28 22:53:24 -05:00
seat: &Seat<State>,
data: &mut State,
frame: smithay::input::pointer::AxisFrame,
) {
match self {
FocusTarget::Window(window) => PointerTarget::axis(window, seat, data, frame),
FocusTarget::Popup(popup) => PointerTarget::axis(popup.wl_surface(), seat, data, frame),
2023-08-04 18:48:10 -05:00
FocusTarget::LayerSurface(surf) => PointerTarget::axis(surf, seat, data, frame),
}
}
fn leave(
&self,
2023-08-28 22:53:24 -05:00
seat: &Seat<State>,
data: &mut State,
serial: smithay::utils::Serial,
time: u32,
) {
match self {
FocusTarget::Window(window) => PointerTarget::leave(window, seat, data, serial, time),
FocusTarget::Popup(popup) => {
PointerTarget::leave(popup.wl_surface(), seat, data, serial, time);
}
2023-08-04 18:48:10 -05:00
FocusTarget::LayerSurface(surf) => PointerTarget::leave(surf, seat, data, serial, time),
}
}
2023-09-08 20:46:01 -05:00
fn gesture_swipe_begin(
&self,
_seat: &Seat<State>,
_data: &mut State,
_event: &smithay::input::pointer::GestureSwipeBeginEvent,
) {
todo!()
}
fn gesture_swipe_update(
&self,
_seat: &Seat<State>,
_data: &mut State,
_event: &smithay::input::pointer::GestureSwipeUpdateEvent,
) {
todo!()
}
fn gesture_swipe_end(
&self,
_seat: &Seat<State>,
_data: &mut State,
_event: &smithay::input::pointer::GestureSwipeEndEvent,
) {
todo!()
}
fn gesture_pinch_begin(
&self,
_seat: &Seat<State>,
_data: &mut State,
_event: &smithay::input::pointer::GesturePinchBeginEvent,
) {
todo!()
}
fn gesture_pinch_update(
&self,
_seat: &Seat<State>,
_data: &mut State,
_event: &smithay::input::pointer::GesturePinchUpdateEvent,
) {
todo!()
}
fn gesture_pinch_end(
&self,
_seat: &Seat<State>,
_data: &mut State,
_event: &smithay::input::pointer::GesturePinchEndEvent,
) {
todo!()
}
fn gesture_hold_begin(
&self,
_seat: &Seat<State>,
_data: &mut State,
_event: &smithay::input::pointer::GestureHoldBeginEvent,
) {
todo!()
}
fn gesture_hold_end(
&self,
_seat: &Seat<State>,
_data: &mut State,
_event: &smithay::input::pointer::GestureHoldEndEvent,
) {
todo!()
}
}
2023-08-28 22:53:24 -05:00
impl KeyboardTarget<State> for FocusTarget {
fn enter(
&self,
2023-08-28 22:53:24 -05:00
seat: &Seat<State>,
data: &mut State,
keys: Vec<smithay::input::keyboard::KeysymHandle<'_>>,
serial: smithay::utils::Serial,
) {
match self {
FocusTarget::Window(window) => KeyboardTarget::enter(window, seat, data, keys, serial),
FocusTarget::Popup(popup) => {
KeyboardTarget::enter(popup.wl_surface(), seat, data, keys, serial);
}
2023-08-04 18:48:10 -05:00
FocusTarget::LayerSurface(surf) => {
KeyboardTarget::enter(surf, seat, data, keys, serial);
}
}
}
2023-08-28 22:53:24 -05:00
fn leave(&self, seat: &Seat<State>, data: &mut State, serial: smithay::utils::Serial) {
match self {
FocusTarget::Window(window) => KeyboardTarget::leave(window, seat, data, serial),
FocusTarget::Popup(popup) => {
KeyboardTarget::leave(popup.wl_surface(), seat, data, serial);
}
2023-08-04 18:48:10 -05:00
FocusTarget::LayerSurface(surf) => KeyboardTarget::leave(surf, seat, data, serial),
}
}
fn key(
&self,
2023-08-28 22:53:24 -05:00
seat: &Seat<State>,
data: &mut State,
key: smithay::input::keyboard::KeysymHandle<'_>,
state: smithay::backend::input::KeyState,
serial: smithay::utils::Serial,
time: u32,
) {
match self {
FocusTarget::Window(window) => {
KeyboardTarget::key(window, seat, data, key, state, serial, time);
}
FocusTarget::Popup(popup) => {
KeyboardTarget::key(popup.wl_surface(), seat, data, key, state, serial, time);
}
2023-08-04 18:48:10 -05:00
FocusTarget::LayerSurface(surf) => {
KeyboardTarget::key(surf, seat, data, key, state, serial, time);
}
}
}
fn modifiers(
&self,
2023-08-28 22:53:24 -05:00
seat: &Seat<State>,
data: &mut State,
modifiers: smithay::input::keyboard::ModifiersState,
serial: smithay::utils::Serial,
) {
match self {
FocusTarget::Window(window) => {
KeyboardTarget::modifiers(window, seat, data, modifiers, serial);
}
FocusTarget::Popup(popup) => {
KeyboardTarget::modifiers(popup.wl_surface(), seat, data, modifiers, serial);
}
2023-08-04 18:48:10 -05:00
FocusTarget::LayerSurface(surf) => {
KeyboardTarget::modifiers(surf, seat, data, modifiers, serial);
}
}
}
}
impl WaylandFocus for FocusTarget {
fn wl_surface(&self) -> Option<WlSurface> {
match self {
FocusTarget::Window(window) => window.wl_surface(),
FocusTarget::Popup(popup) => Some(popup.wl_surface().clone()),
2023-08-04 18:48:10 -05:00
FocusTarget::LayerSurface(surf) => Some(surf.wl_surface().clone()),
}
}
fn same_client_as(
&self,
object_id: &smithay::reexports::wayland_server::backend::ObjectId,
) -> bool {
match self {
FocusTarget::Window(WindowElement::Wayland(window)) => window.same_client_as(object_id),
FocusTarget::Window(WindowElement::X11(surface)) => surface.same_client_as(object_id),
FocusTarget::Popup(popup) => popup.wl_surface().id().same_client_as(object_id),
2023-08-04 18:48:10 -05:00
FocusTarget::LayerSurface(surf) => surf.wl_surface().id().same_client_as(object_id),
}
}
}
impl From<WindowElement> for FocusTarget {
fn from(value: WindowElement) -> Self {
FocusTarget::Window(value)
}
}
impl From<PopupKind> for FocusTarget {
fn from(value: PopupKind) -> Self {
FocusTarget::Popup(value)
}
}
2023-08-04 18:48:10 -05:00
impl From<LayerSurface> for FocusTarget {
fn from(value: LayerSurface) -> Self {
FocusTarget::LayerSurface(value)
}
}