#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Focus {
Panel(u64),
Overlay(String),
}
impl Focus {
#[must_use]
pub const fn panel(viewport_id: u64) -> Self {
Self::Panel(viewport_id)
}
#[must_use]
pub fn overlay(overlay_id: impl Into<String>) -> Self {
Self::Overlay(overlay_id.into())
}
#[must_use]
pub const fn is_panel(&self) -> bool {
matches!(self, Self::Panel(_))
}
#[must_use]
pub const fn is_overlay(&self) -> bool {
matches!(self, Self::Overlay(_))
}
#[must_use]
pub const fn viewport_id(&self) -> Option<u64> {
match self {
Self::Panel(id) => Some(*id),
Self::Overlay(_) => None,
}
}
#[must_use]
pub fn overlay_id(&self) -> Option<&str> {
match self {
Self::Panel(_) => None,
Self::Overlay(id) => Some(id),
}
}
}
pub trait FocusManager {
fn current(&self) -> &Focus;
fn focus_panel(&mut self, viewport_id: u64);
fn focus_overlay(&mut self, overlay_id: &str);
fn return_to_panel(&mut self);
fn is_panel_focused(&self, viewport_id: u64) -> bool {
matches!(self.current(), Focus::Panel(id) if *id == viewport_id)
}
fn is_overlay_focused(&self, overlay_id: &str) -> bool {
matches!(self.current(), Focus::Overlay(id) if id == overlay_id)
}
fn has_overlay_focus(&self) -> bool {
self.current().is_overlay()
}
}
#[cfg(test)]
#[path = "focus_tests.rs"]
mod tests;