2023-06-25 17:18:50 -05:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
2023-06-25 17:49:06 -05:00
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
2023-06-25 17:18:50 -05:00
|
|
|
|
2023-06-24 17:39:40 -05:00
|
|
|
use smithay::{desktop::Window, output::Output, utils::IsAlive};
|
2023-06-17 21:02:58 -05:00
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct FocusState {
|
|
|
|
focus_stack: Vec<Window>,
|
2023-06-24 17:39:40 -05:00
|
|
|
pub focused_output: Option<Output>,
|
2023-06-17 21:02:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FocusState {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Default::default()
|
|
|
|
}
|
|
|
|
|
2023-07-04 15:20:41 -05:00
|
|
|
// TODO: how does this work with unmapped windows?
|
2023-07-01 19:06:37 -05:00
|
|
|
/// Get the currently focused window. If there is none, the previous focus is returned.
|
2023-06-17 21:02:58 -05:00
|
|
|
pub fn current_focus(&mut self) -> Option<Window> {
|
|
|
|
while let Some(window) = self.focus_stack.last() {
|
|
|
|
if window.alive() {
|
|
|
|
return Some(window.clone());
|
|
|
|
}
|
|
|
|
self.focus_stack.pop();
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2023-06-30 21:34:07 -05:00
|
|
|
/// Set the currently focused window.
|
2023-06-17 21:02:58 -05:00
|
|
|
pub fn set_focus(&mut self, window: Window) {
|
|
|
|
self.focus_stack.retain(|win| win != &window);
|
|
|
|
self.focus_stack.push(window);
|
|
|
|
}
|
|
|
|
}
|