Skip to content

Commit 931fb22

Browse files
committed
Define a with_toplevel_state, generic over pending bool
Reduces a bit of duplication.
1 parent e55d16b commit 931fb22

File tree

1 file changed

+31
-70
lines changed

1 file changed

+31
-70
lines changed

src/shell/element/surface.rs

Lines changed: 31 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -214,18 +214,9 @@ impl CosmicSurface {
214214

215215
pub fn is_activated(&self, pending: bool) -> bool {
216216
match self.0.underlying_surface() {
217-
WindowSurface::Wayland(toplevel) => {
218-
if pending {
219-
toplevel.with_pending_state(|pending| {
220-
pending.states.contains(ToplevelState::Activated)
221-
})
222-
} else {
223-
toplevel
224-
.current_state()
225-
.states
226-
.contains(ToplevelState::Activated)
227-
}
228-
}
217+
WindowSurface::Wayland(toplevel) => with_toplevel_state(toplevel, pending, |state| {
218+
state.states.contains(ToplevelState::Activated)
219+
}),
229220
WindowSurface::X11(surface) => surface.is_activated(),
230221
}
231222
}
@@ -255,18 +246,11 @@ impl CosmicSurface {
255246
.and_then(|data| data.lock().unwrap().mode.map(|m| m != KdeMode::Server))
256247
});
257248

258-
let xdg_state = if pending {
259-
toplevel.with_pending_state(|pending| {
260-
pending
261-
.decoration_mode
262-
.map(|mode| mode == DecorationMode::ClientSide)
263-
})
264-
} else {
265-
toplevel
266-
.current_state()
249+
let xdg_state = with_toplevel_state(toplevel, pending, |state| {
250+
state
267251
.decoration_mode
268252
.map(|mode| mode == DecorationMode::ClientSide)
269-
};
253+
});
270254

271255
kde_state.or(xdg_state).unwrap_or(true)
272256
}
@@ -313,18 +297,9 @@ impl CosmicSurface {
313297
pub fn is_resizing(&self, pending: bool) -> Option<bool> {
314298
match self.0.underlying_surface() {
315299
WindowSurface::Wayland(toplevel) => {
316-
if pending {
317-
Some(toplevel.with_pending_state(|pending| {
318-
pending.states.contains(ToplevelState::Resizing)
319-
}))
320-
} else {
321-
Some(
322-
toplevel
323-
.current_state()
324-
.states
325-
.contains(ToplevelState::Resizing),
326-
)
327-
}
300+
Some(with_toplevel_state(toplevel, pending, |state| {
301+
state.states.contains(ToplevelState::Resizing)
302+
}))
328303
}
329304
WindowSurface::X11(_surface) => None,
330305
}
@@ -346,18 +321,9 @@ impl CosmicSurface {
346321
pub fn is_tiled(&self, pending: bool) -> Option<bool> {
347322
match self.0.underlying_surface() {
348323
WindowSurface::Wayland(toplevel) => {
349-
if pending {
350-
Some(toplevel.with_pending_state(|pending| {
351-
pending.states.contains(ToplevelState::TiledLeft)
352-
}))
353-
} else {
354-
Some(
355-
toplevel
356-
.current_state()
357-
.states
358-
.contains(ToplevelState::TiledLeft),
359-
)
360-
}
324+
Some(with_toplevel_state(toplevel, pending, |state| {
325+
state.states.contains(ToplevelState::TiledLeft)
326+
}))
361327
}
362328
WindowSurface::X11(_surface) => None,
363329
}
@@ -384,18 +350,9 @@ impl CosmicSurface {
384350

385351
pub fn is_fullscreen(&self, pending: bool) -> bool {
386352
match self.0.underlying_surface() {
387-
WindowSurface::Wayland(toplevel) => {
388-
if pending {
389-
toplevel.with_pending_state(|pending| {
390-
pending.states.contains(ToplevelState::Fullscreen)
391-
})
392-
} else {
393-
toplevel
394-
.current_state()
395-
.states
396-
.contains(ToplevelState::Fullscreen)
397-
}
398-
}
353+
WindowSurface::Wayland(toplevel) => with_toplevel_state(toplevel, pending, |state| {
354+
state.states.contains(ToplevelState::Fullscreen)
355+
}),
399356
WindowSurface::X11(surface) => surface.is_fullscreen(),
400357
}
401358
}
@@ -417,18 +374,9 @@ impl CosmicSurface {
417374

418375
pub fn is_maximized(&self, pending: bool) -> bool {
419376
match self.0.underlying_surface() {
420-
WindowSurface::Wayland(toplevel) => {
421-
if pending {
422-
toplevel.with_pending_state(|pending| {
423-
pending.states.contains(ToplevelState::Maximized)
424-
})
425-
} else {
426-
toplevel
427-
.current_state()
428-
.states
429-
.contains(ToplevelState::Maximized)
430-
}
431-
}
377+
WindowSurface::Wayland(toplevel) => with_toplevel_state(toplevel, pending, |state| {
378+
state.states.contains(ToplevelState::Maximized)
379+
}),
432380
WindowSurface::X11(surface) => surface.is_maximized(),
433381
}
434382
}
@@ -978,3 +926,16 @@ where
978926
self.0.render_elements(renderer, location, scale, alpha)
979927
}
980928
}
929+
930+
fn with_toplevel_state<T, F: FnOnce(&smithay::wayland::shell::xdg::ToplevelState) -> T>(
931+
toplevel: &ToplevelSurface,
932+
pending: bool,
933+
cb: F,
934+
) -> T {
935+
if pending {
936+
toplevel.with_pending_state(|pending| cb(pending))
937+
} else {
938+
let current = toplevel.current_state();
939+
cb(&current)
940+
}
941+
}

0 commit comments

Comments
 (0)