use super::{DockPanel, LeafId, PanelRect, DropZone};
use serde::{Serialize, Deserialize};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct FloatingWindowId(pub u64);
impl std::fmt::Display for FloatingWindowId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "FloatingWindow({})", self.0)
}
}
#[derive(Clone, Debug)]
pub struct FloatingWindow<P: DockPanel> {
pub id: FloatingWindowId,
pub panels: Vec<P>,
pub active_tab: usize,
pub x: f32,
pub y: f32,
pub width: f32,
pub height: f32,
}
impl<P: DockPanel> FloatingWindow<P> {
pub fn new(
id: FloatingWindowId,
panels: Vec<P>,
active_tab: usize,
x: f32,
y: f32,
width: f32,
height: f32,
) -> Self {
Self {
id,
panels,
active_tab,
x,
y,
width,
height,
}
}
pub fn rect(&self) -> PanelRect {
PanelRect::new(self.x, self.y, self.width, self.height)
}
pub fn title(&self) -> &str {
self.panels
.get(self.active_tab)
.map(|p| p.title())
.unwrap_or("Floating Window")
}
pub fn contains(&self, x: f32, y: f32) -> bool {
x >= self.x && x <= self.x + self.width
&& y >= self.y && y <= self.y + self.height
}
pub fn active_panel(&self) -> Option<&P> {
self.panels.get(self.active_tab)
}
pub fn active_panel_mut(&mut self) -> Option<&mut P> {
self.panels.get_mut(self.active_tab)
}
pub fn tab_count(&self) -> usize {
self.panels.len()
}
}
#[derive(Clone, Debug)]
pub struct FloatingDragState {
pub window_id: FloatingWindowId,
pub offset_x: f32,
pub offset_y: f32,
pub dock_target: Option<(LeafId, DropZone, bool)>,
}
impl FloatingDragState {
pub fn new(window_id: FloatingWindowId, offset_x: f32, offset_y: f32) -> Self {
Self {
window_id,
offset_x,
offset_y,
dock_target: None,
}
}
pub fn has_dock_target(&self) -> bool {
self.dock_target.is_some()
}
pub fn dock_target(&self) -> Option<(LeafId, DropZone, bool)> {
self.dock_target
}
pub fn set_dock_target(&mut self, target: Option<(LeafId, DropZone, bool)>) {
self.dock_target = target;
}
}