cosmic-comp/src/shell/focus/mod.rs

343 lines
12 KiB
Rust
Raw Normal View History

2022-07-04 16:00:29 +02:00
use crate::{
2022-09-28 12:01:29 +02:00
shell::{element::CosmicMapped, Shell, Workspace},
2022-07-04 16:00:29 +02:00
state::Common,
utils::prelude::*,
wayland::handlers::xdg_shell::PopupGrabData,
xwayland::XWaylandState,
};
use indexmap::IndexSet;
use smithay::{
2022-09-28 12:01:29 +02:00
desktop::{layer_map_for_output, PopupUngrabStrategy},
2022-08-31 13:01:23 +02:00
input::Seat,
utils::{IsAlive, Serial, SERIAL_COUNTER},
wayland::seat::WaylandFocus,
};
2022-09-28 12:01:29 +02:00
use std::cell::RefCell;
use tracing::{debug, trace};
2022-09-28 12:01:29 +02:00
use self::target::{KeyboardFocusTarget, WindowGroup};
use super::{layout::floating::FloatingLayout, CosmicSurface};
pub mod target;
#[derive(Debug, serde::Deserialize, Clone, Copy, PartialEq, Eq)]
pub enum FocusDirection {
Left,
Right,
Up,
Down,
In,
Out,
}
2022-09-28 12:01:29 +02:00
pub struct FocusStack<'a>(pub(super) Option<&'a IndexSet<CosmicMapped>>);
pub struct FocusStackMut<'a>(pub(super) &'a mut IndexSet<CosmicMapped>);
impl<'a> FocusStack<'a> {
2022-09-28 12:01:29 +02:00
pub fn last(&self) -> Option<&CosmicMapped> {
self.0
.as_ref()
.and_then(|set| set.iter().rev().find(|w| w.alive()))
}
2022-09-28 12:01:29 +02:00
pub fn iter(&self) -> impl Iterator<Item = &'_ CosmicMapped> {
self.0
.iter()
.flat_map(|set| set.iter().rev().filter(|w| w.alive()))
}
}
impl<'a> FocusStackMut<'a> {
2022-09-28 12:01:29 +02:00
pub fn append(&mut self, window: &CosmicMapped) {
self.0.retain(|w| w.alive());
self.0.shift_remove(window);
self.0.insert(window.clone());
}
2022-09-28 12:01:29 +02:00
pub fn last(&self) -> Option<&CosmicMapped> {
self.0.iter().rev().find(|w| w.alive())
}
2022-09-28 12:01:29 +02:00
pub fn iter(&self) -> impl Iterator<Item = &'_ CosmicMapped> {
self.0.iter().rev().filter(|w| w.alive())
}
}
2022-09-28 12:01:29 +02:00
impl Workspace {}
2022-09-28 12:01:29 +02:00
pub struct ActiveFocus(RefCell<Option<KeyboardFocusTarget>>);
impl ActiveFocus {
2022-09-28 12:01:29 +02:00
fn set(seat: &Seat<State>, target: Option<KeyboardFocusTarget>) {
if !seat
.user_data()
2022-09-28 12:01:29 +02:00
.insert_if_missing(|| ActiveFocus(RefCell::new(target.clone())))
{
*seat
.user_data()
.get::<ActiveFocus>()
.unwrap()
.0
2022-09-28 12:01:29 +02:00
.borrow_mut() = target;
}
}
2022-09-28 12:01:29 +02:00
fn get(seat: &Seat<State>) -> Option<KeyboardFocusTarget> {
seat.user_data()
.get::<ActiveFocus>()
.and_then(|a| a.0.borrow().clone())
}
}
impl Shell {
pub fn set_focus<'a>(
2022-08-31 13:01:23 +02:00
state: &mut State,
2022-09-28 12:01:29 +02:00
target: Option<&KeyboardFocusTarget>,
active_seat: &Seat<State>,
serial: Option<Serial>,
) {
// update FocusStack and notify layouts about new focus (if any window)
let element = match target {
Some(KeyboardFocusTarget::Element(mapped)) => Some(mapped.clone()),
2023-10-25 19:41:30 +02:00
Some(KeyboardFocusTarget::Fullscreen(window)) => {
state.common.shell.element_for_surface(window).cloned()
}
_ => None,
};
if let Some(mapped) = element {
if let Some(workspace) = state.common.shell.space_for_mut(&mapped) {
2022-09-28 12:01:29 +02:00
let mut focus_stack = workspace.focus_stack.get_mut(active_seat);
if Some(&mapped) != focus_stack.last() {
trace!(?mapped, "Focusing window.");
focus_stack.append(&mapped);
2022-09-28 12:01:29 +02:00
// also remove popup grabs, if we are switching focus
if let Some(mut popup_grab) = active_seat
.user_data()
.get::<PopupGrabData>()
.and_then(|x| x.take())
{
if !popup_grab.has_ended() {
popup_grab.ungrab(PopupUngrabStrategy::All);
}
}
}
}
}
// update keyboard focus
if let Some(keyboard) = active_seat.get_keyboard() {
2022-09-28 12:01:29 +02:00
ActiveFocus::set(active_seat, target.cloned());
keyboard.set_focus(
2022-08-31 13:01:23 +02:00
state,
2022-09-28 12:01:29 +02:00
target.cloned(),
serial.unwrap_or_else(|| SERIAL_COUNTER.next_serial()),
);
}
}
fn update_active<'a, 'b>(
&mut self,
seats: impl Iterator<Item = &'a Seat<State>>,
2023-03-07 20:28:41 +01:00
mut xwm: Option<&'b mut XWaylandState>,
) {
// update activate status
let focused_windows = seats
.flat_map(|seat| {
if matches!(
seat.get_keyboard().unwrap().current_focus(),
Some(KeyboardFocusTarget::Group(_))
) {
return None;
}
2023-10-25 19:40:26 +02:00
Some(self.outputs().flat_map(|o| {
2022-09-28 12:01:29 +02:00
let space = self.active_space(o);
let stack = space.focus_stack.get(seat);
stack.last().cloned()
}))
})
.flatten()
.collect::<Vec<_>>();
2023-10-25 19:40:26 +02:00
for output in self.outputs().cloned().collect::<Vec<_>>().into_iter() {
// TODO: Add self.workspaces.active_workspaces()
let workspace = self.workspaces.active_mut(&output);
for focused in focused_windows.iter() {
2023-05-26 12:47:39 +02:00
if let CosmicSurface::X11(window) = focused.active_window() {
if let Some(xwm) = xwm.as_mut().and_then(|state| state.xwm.as_mut()) {
let _ = xwm.raise_window(&window);
}
2023-05-26 12:47:39 +02:00
}
raise_with_children(&mut workspace.floating_layer, focused);
}
2022-09-28 12:01:29 +02:00
for window in workspace.mapped() {
window.set_activated(focused_windows.contains(&window));
window.configure();
}
}
}
}
fn raise_with_children(floating_layer: &mut FloatingLayout, focused: &CosmicMapped) {
if floating_layer.mapped().any(|m| m == focused) {
floating_layer.space.raise_element(focused, true);
for element in floating_layer
.space
.elements()
.filter(|elem| {
let parent = match elem.active_window() {
CosmicSurface::Wayland(w) => w.toplevel().parent(),
_ => None,
};
parent == focused.active_window().wl_surface()
})
.cloned()
.collect::<Vec<_>>()
.into_iter()
{
raise_with_children(floating_layer, &element);
}
}
}
impl Common {
pub fn set_focus(
2022-08-31 13:01:23 +02:00
state: &mut State,
2022-09-28 12:01:29 +02:00
target: Option<&KeyboardFocusTarget>,
active_seat: &Seat<State>,
serial: Option<Serial>,
) {
2022-09-28 12:01:29 +02:00
Shell::set_focus(state, target, active_seat, serial);
let seats = state.common.seats().cloned().collect::<Vec<_>>();
state
.common
.shell
2023-03-07 20:28:41 +01:00
.update_active(seats.iter(), state.common.xwayland_state.as_mut());
}
2022-08-31 13:01:23 +02:00
pub fn refresh_focus(state: &mut State) {
2022-09-28 12:01:29 +02:00
let seats = state.common.seats().cloned().collect::<Vec<_>>();
2022-08-31 13:01:23 +02:00
for seat in seats {
2022-09-28 12:01:29 +02:00
let output = seat.active_output();
2023-10-25 19:40:26 +02:00
if !state.common.shell.outputs().any(|o| o == &output) {
seat.set_active_output(&state.common.shell.outputs().next().unwrap());
continue;
}
2022-08-31 13:01:23 +02:00
let last_known_focus = ActiveFocus::get(&seat);
2022-09-28 12:01:29 +02:00
if let Some(target) = last_known_focus {
if target.alive() {
match target {
KeyboardFocusTarget::Element(mapped) => {
let workspace = state.common.shell.active_space(&output);
let focus_stack = workspace.focus_stack.get(&seat);
2023-02-13 20:28:16 +01:00
if focus_stack.last().map(|m| m == &mapped).unwrap_or(false)
2023-10-25 19:41:30 +02:00
&& workspace.get_fullscreen().is_none()
2023-02-13 20:28:16 +01:00
{
continue; // Focus is valid
} else {
trace!("Wrong Window, focus fixup");
}
}
2022-09-28 12:01:29 +02:00
KeyboardFocusTarget::LayerSurface(layer) => {
if layer_map_for_output(&output).layers().any(|l| l == &layer) {
continue; // Focus is valid
}
}
2023-10-25 19:40:26 +02:00
KeyboardFocusTarget::Group(WindowGroup { node, .. }) => {
if state
.common
.shell
.workspaces
.active(&output)
.1
.tiling_layer
.has_node(&node)
{
2022-09-28 12:01:29 +02:00
continue; // Focus is valid,
}
}
KeyboardFocusTarget::Fullscreen(window) => {
2023-04-01 20:35:58 +04:00
let workspace = state.common.shell.active_space(&output);
let focus_stack = workspace.focus_stack.get(&seat);
if focus_stack
.last()
2023-10-25 19:41:30 +02:00
.map(|m| m.has_active_window(&window))
2023-04-01 20:35:58 +04:00
.unwrap_or(false)
2023-10-25 19:41:30 +02:00
&& workspace.get_fullscreen().is_some()
2023-04-01 20:35:58 +04:00
{
continue; // Focus is valid
} else {
trace!("Wrong Window, focus fixup");
}
}
KeyboardFocusTarget::Popup(_) => {
continue; // Focus is valid
}
2022-09-28 12:01:29 +02:00
};
} else {
trace!("Surface dead, focus fixup");
}
2022-09-27 13:46:25 +02:00
} else {
let workspace = state.common.shell.active_space(&output);
let focus_stack = workspace.focus_stack.get(&seat);
if focus_stack.last().is_none() {
continue; // Focus is valid
} else {
trace!("No previous window, focus fixup");
}
}
// fixup focus
{
// also remove popup grabs, if we are switching focus
if let Some(mut popup_grab) = seat
.user_data()
.get::<PopupGrabData>()
.and_then(|x| x.take())
{
if !popup_grab.has_ended() {
2022-08-31 13:01:23 +02:00
popup_grab.ungrab(PopupUngrabStrategy::All);
}
}
// update keyboard focus
2022-09-28 12:01:29 +02:00
let target = state
2022-08-31 13:01:23 +02:00
.common
.shell
.active_space(&output)
2023-10-25 19:41:30 +02:00
.get_fullscreen()
2022-09-28 12:01:29 +02:00
.cloned()
2023-02-13 20:28:16 +01:00
.map(KeyboardFocusTarget::Fullscreen)
.or_else(|| {
state
.common
.shell
.active_space(&output)
.focus_stack
.get(&seat)
.last()
.cloned()
.map(KeyboardFocusTarget::from)
});
if let Some(keyboard) = seat.get_keyboard() {
debug!("Restoring focus to {:?}", target.as_ref());
2022-09-28 12:01:29 +02:00
keyboard.set_focus(state, target.clone(), SERIAL_COUNTER.next_serial());
ActiveFocus::set(&seat, target);
}
}
}
2022-09-28 12:01:29 +02:00
let seats = state.common.seats().cloned().collect::<Vec<_>>();
state
.common
.shell
2023-03-07 20:28:41 +01:00
.update_active(seats.iter(), state.common.xwayland_state.as_mut())
}
}