shell: Unify element_for_(x11_|wl_)surface

This commit is contained in:
Victoria Brekenfeld 2024-03-21 12:44:40 +01:00 committed by Victoria Brekenfeld
parent dbe2a073ef
commit 6858238bd6
13 changed files with 56 additions and 105 deletions

View file

@ -711,7 +711,7 @@ impl Program for CosmicStackInternal {
if let Some(surface) = self.windows.lock().unwrap()[active].wl_surface() {
loop_handle.insert_idle(move |state| {
if let Some(mapped) =
state.common.shell.element_for_wl_surface(&surface).cloned()
state.common.shell.element_for_surface(&surface).cloned()
{
let position = if let Some((output, set)) =
state.common.shell.workspaces.sets.iter().find(|(_, set)| {
@ -757,7 +757,7 @@ impl Program for CosmicStackInternal {
if let Some(surface) = self.windows.lock().unwrap()[idx].wl_surface() {
loop_handle.insert_idle(move |state| {
if let Some(mapped) =
state.common.shell.element_for_wl_surface(&surface).cloned()
state.common.shell.element_for_surface(&surface).cloned()
{
if let Some(workspace) = state.common.shell.space_for_mut(&mapped) {
let Some(elem_geo) = workspace.element_geometry(&mapped) else {

View file

@ -73,6 +73,18 @@ impl From<X11Surface> for CosmicSurface {
}
}
impl PartialEq<WlSurface> for CosmicSurface {
fn eq(&self, other: &WlSurface) -> bool {
self.wl_surface().map_or(false, |s| &s == other)
}
}
impl PartialEq<X11Surface> for CosmicSurface {
fn eq(&self, other: &X11Surface) -> bool {
self.x11_surface().map_or(false, |s| s == other)
}
}
#[derive(Default)]
struct Minimized(AtomicBool);

View file

@ -289,7 +289,7 @@ impl Program for CosmicWindowInternal {
if let Some(surface) = self.window.wl_surface() {
loop_handle.insert_idle(move |state| {
if let Some(mapped) =
state.common.shell.element_for_wl_surface(&surface).cloned()
state.common.shell.element_for_surface(&surface).cloned()
{
state.common.shell.minimize_request(&mapped)
}
@ -300,7 +300,7 @@ impl Program for CosmicWindowInternal {
if let Some(surface) = self.window.wl_surface() {
loop_handle.insert_idle(move |state| {
if let Some(mapped) =
state.common.shell.element_for_wl_surface(&surface).cloned()
state.common.shell.element_for_surface(&surface).cloned()
{
let seat = state.common.last_active_seat().clone();
state.common.shell.maximize_toggle(&mapped, &seat)
@ -314,7 +314,7 @@ impl Program for CosmicWindowInternal {
if let Some(surface) = self.window.wl_surface() {
loop_handle.insert_idle(move |state| {
if let Some(mapped) =
state.common.shell.element_for_wl_surface(&surface).cloned()
state.common.shell.element_for_surface(&surface).cloned()
{
let position = if let Some((output, set)) =
state.common.shell.workspaces.sets.iter().find(|(_, set)| {

View file

@ -1467,7 +1467,10 @@ impl Shell {
}
}
pub fn element_for_surface(&self, surface: &CosmicSurface) -> Option<&CosmicMapped> {
pub fn element_for_surface<S>(&self, surface: &S) -> Option<&CosmicMapped>
where
CosmicSurface: PartialEq<S>,
{
self.workspaces.sets.values().find_map(|set| {
set.minimized_windows
.iter()
@ -1482,39 +1485,6 @@ impl Shell {
})
}
pub fn element_for_wl_surface(&self, surface: &WlSurface) -> Option<&CosmicMapped> {
self.workspaces.sets.values().find_map(|set| {
set.minimized_windows
.iter()
.map(|w| &w.window)
.chain(set.sticky_layer.mapped())
.find(|w| {
w.windows()
.any(|(s, _)| s.wl_surface().as_ref() == Some(surface))
})
.or_else(|| {
set.workspaces
.iter()
.find_map(|w| w.element_for_wl_surface(surface))
})
})
}
pub fn element_for_x11_surface(&self, surface: &X11Surface) -> Option<&CosmicMapped> {
self.workspaces.sets.values().find_map(|set| {
set.minimized_windows
.iter()
.map(|w| &w.window)
.chain(set.sticky_layer.mapped())
.find(|w| w.windows().any(|(s, _)| s.x11_surface() == Some(surface)))
.or_else(|| {
set.workspaces
.iter()
.find_map(|w| w.element_for_x11_surface(surface))
})
})
}
pub fn space_for(&self, mapped: &CosmicMapped) -> Option<&Workspace> {
self.workspaces.spaces().find(|workspace| {
workspace.mapped().any(|m| m == mapped)
@ -1819,7 +1789,7 @@ impl Shell {
let parent_is_sticky = if let Some(toplevel) = window.0.toplevel() {
if let Some(parent) = toplevel.parent() {
if let Some(elem) = state.common.shell.element_for_wl_surface(&parent) {
if let Some(elem) = state.common.shell.element_for_surface(&parent) {
state
.common
.shell
@ -2315,7 +2285,7 @@ impl Shell {
if let Some(start_data) =
check_grab_preconditions(&seat, surface, serial, ReleaseMode::NoMouseButtons)
{
if let Some(mapped) = state.common.shell.element_for_wl_surface(surface).cloned() {
if let Some(mapped) = state.common.shell.element_for_surface(surface).cloned() {
let (_, relative_loc) = mapped
.windows()
.find(|(w, _)| w.wl_surface().as_ref() == Some(surface))
@ -2434,9 +2404,7 @@ impl Shell {
let output = seat.active_output();
if let Some(mut start_data) = check_grab_preconditions(&seat, surface, serial, release) {
if let Some(mut old_mapped) =
state.common.shell.element_for_wl_surface(surface).cloned()
{
if let Some(mut old_mapped) = state.common.shell.element_for_surface(surface).cloned() {
if old_mapped.is_minimized() {
return;
}
@ -3063,7 +3031,7 @@ impl Shell {
if let Some(start_data) =
check_grab_preconditions(&seat, surface, serial, ReleaseMode::NoMouseButtons)
{
if let Some(mapped) = state.common.shell.element_for_wl_surface(surface).cloned() {
if let Some(mapped) = state.common.shell.element_for_surface(surface).cloned() {
if mapped.is_fullscreen(true) || mapped.is_maximized(true) {
return;
}

View file

@ -44,7 +44,6 @@ use smithay::{
seat::WaylandFocus,
xdg_activation::{XdgActivationState, XdgActivationToken},
},
xwayland::X11Surface,
};
use std::{
collections::{HashMap, HashSet, VecDeque},
@ -365,7 +364,7 @@ impl Workspace {
}
pub fn commit(&mut self, surface: &WlSurface) {
if let Some(mapped) = self.element_for_wl_surface(surface) {
if let Some(mapped) = self.element_for_surface(surface) {
mapped
.windows()
.find(|(w, _)| w.wl_surface().as_ref() == Some(surface))
@ -483,7 +482,10 @@ impl Workspace {
}
}
pub fn element_for_surface(&self, surface: &CosmicSurface) -> Option<&CosmicMapped> {
pub fn element_for_surface<S>(&self, surface: &S) -> Option<&CosmicMapped>
where
CosmicSurface: PartialEq<S>,
{
self.floating_layer
.mapped()
.chain(self.tiling_layer.mapped().map(|(w, _)| w))
@ -491,25 +493,6 @@ impl Workspace {
.find(|e| e.windows().any(|(w, _)| &w == surface))
}
pub fn element_for_wl_surface(&self, surface: &WlSurface) -> Option<&CosmicMapped> {
self.floating_layer
.mapped()
.chain(self.tiling_layer.mapped().map(|(w, _)| w))
.chain(self.minimized_windows.iter().map(|w| &w.window))
.find(|e| {
e.windows()
.any(|(w, _)| w.wl_surface().as_ref() == Some(surface))
})
}
pub fn element_for_x11_surface(&self, surface: &X11Surface) -> Option<&CosmicMapped> {
self.floating_layer
.mapped()
.chain(self.tiling_layer.mapped().map(|(w, _)| w))
.chain(self.minimized_windows.iter().map(|w| &w.window))
.find(|e| e.windows().any(|(w, _)| w.x11_surface() == Some(surface)))
}
pub fn element_under(
&mut self,
location: Point<f64, Global>,