use uzor::layout::docking::{DockPanel, LeafId};
use uzor::Rect;
use crate::types::command::{DragOutChrome, DragOutPolicy};
use crate::types::ids::{Seconds, WindowId};
use crate::types::intent::DockIntent;
use crate::types::ops::{DockPointer, LayoutEffect};
use crate::types::window::{Point, SizePx, WindowCommand, WindowSpec};
use super::{dock, rects, Ctx, LayoutEffects, LayoutEngine, WindowLayout};
pub const MOVE_FRESH_S: f64 = 0.75;
pub const DWELL_S: f64 = 1.0;
pub const STALE_S: f64 = 1.25;
const MICRO_MIN: (f64, f64) = (320.0, 240.0);
const MICRO_CAP: f64 = 0.92;
const MICRO_MIN_INNER: SizePx = SizePx::new(240, 160);
const MICRO_CHROME_H: f64 = 32.0;
const GRAB_X: f64 = 60.0;
#[derive(Clone, Debug)]
pub(super) struct Payload<P> {
panels: Vec<P>,
active: usize,
size: (f64, f64),
}
impl<P: DockPanel> Payload<P> {
pub(super) fn new(panels: Vec<P>, active: usize, size: (f64, f64)) -> Self {
Self {
panels,
active,
size,
}
}
pub(super) fn into_parts(self) -> (Vec<P>, usize) {
(self.panels, self.active)
}
fn title(&self) -> String {
self.panels
.get(self.active)
.or_else(|| self.panels.first())
.map(|p| p.title().to_string())
.unwrap_or_default()
}
}
pub(super) struct Outbound<P> {
leaf: LeafId,
payload: Payload<P>,
pos: Point,
}
impl<P> Outbound<P> {
pub(super) fn new(leaf: LeafId, payload: Payload<P>, pos: Point) -> Self {
Self { leaf, payload, pos }
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
struct Dwell {
started: Seconds,
target: WindowId,
}
pub(super) struct DragOutSession<P> {
src: WindowId,
from: LeafId,
chrome: DragOutChrome,
started: Seconds,
held: Option<Payload<P>>,
micro: Option<WindowId>,
grab: Point,
last_moved: Option<Seconds>,
dwell: Option<Dwell>,
}
impl<P> DragOutSession<P> {
pub(super) fn holds_panel(&self) -> bool {
self.held.is_some()
}
pub(super) fn deadline(&self) -> Option<Seconds> {
self.dwell.map(|d| d.started.after(Seconds(DWELL_S)))
}
fn placed_in(&self) -> Option<WindowId> {
match (self.micro, &self.held) {
(Some(m), None) => Some(m),
_ => None,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct DragOutView {
src: WindowId,
from: LeafId,
chrome: DragOutChrome,
started: Seconds,
micro: Option<WindowId>,
placed: bool,
dwell: Option<(WindowId, Seconds)>,
}
#[allow(dead_code)]
impl DragOutView {
pub fn src(&self) -> WindowId {
self.src
}
pub fn leaf(&self) -> LeafId {
self.from
}
pub fn chrome(&self) -> DragOutChrome {
self.chrome
}
pub fn started(&self) -> Seconds {
self.started
}
pub fn micro(&self) -> Option<WindowId> {
self.micro
}
pub fn placed(&self) -> bool {
self.placed
}
pub fn dwell(&self) -> Option<(WindowId, Seconds)> {
self.dwell
}
}
pub(super) fn allowed<P: DockPanel>(w: &WindowLayout<P>, ctx: &Ctx<P>) -> bool {
matches!(ctx.policy.drag_out, DragOutPolicy::Enabled { .. })
&& w.caps.multi_window
&& w.micro.is_none()
&& w.outer.is_some()
&& !ctx.dragout_busy
}
fn project<P: DockPanel>(w: &WindowLayout<P>, client: Point) -> Option<Point> {
w.outer
.map(|o| Point::new(o.x + client.x * w.scale, o.y + client.y * w.scale))
}
pub(super) fn micro_move<P: DockPanel>(
win: WindowId,
w: &mut WindowLayout<P>,
grab: Point,
p: Point,
fx: &mut LayoutEffects,
) {
let Some(o) = w.outer else {
return;
};
let (dx, dy) = ((p.x - grab.x) * w.scale, (p.y - grab.y) * w.scale);
if dx.abs() < 0.5 && dy.abs() < 0.5 {
return;
}
let moved = Rect::new((o.x + dx).round(), (o.y + dy).round(), o.width, o.height);
w.outer = Some(moved);
fx.push(LayoutEffect::Window {
win,
cmd: WindowCommand::SetOuterRect {
position: (moved.x as i32, moved.y as i32),
size: SizePx::new(o.width.max(1.0) as u32, o.height.max(1.0) as u32),
},
});
}
impl<P: DockPanel> LayoutEngine<P> {
pub(super) fn dragout_obs(&self) -> Option<DragOutView> {
self.dragout.as_ref().map(|s| DragOutView {
src: s.src,
from: s.from,
chrome: s.chrome,
started: s.started,
micro: s.micro,
placed: s.placed_in().is_some(),
dwell: s.dwell.map(|d| (d.target, d.started)),
})
}
pub(super) fn dragout_pointer(&mut self, win: WindowId, event: DockPointer) {
if let (Some(s), DockPointer::Down { pos, .. }) = (&mut self.dragout, event) {
if s.micro == Some(win) {
s.grab = pos;
}
}
}
pub(super) fn dragout_moved(&mut self, win: WindowId, now: Seconds) {
if let Some(s) = &mut self.dragout {
if s.placed_in() == Some(win) {
s.last_moved = Some(now);
}
}
}
fn window_at(&self, screen: Point, exclude: WindowId) -> Option<WindowId> {
self.windows
.iter()
.filter(|(id, w)| **id != exclude && w.micro.is_none())
.find(|(_, w)| w.outer.is_some_and(|o| rects::contains(o, screen)))
.map(|(id, _)| *id)
}
fn dock_at_screen(
&mut self,
dst: WindowId,
screen: Point,
payload: Payload<P>,
fx: &mut LayoutEffects,
) -> Option<LeafId> {
let mut out = None;
self.edit(dst, fx, |w, _, _| {
let local = match w.outer {
Some(o) => Point::new((screen.x - o.x) / w.scale, (screen.y - o.y) / w.scale),
None => Point::new(f64::NAN, f64::NAN),
};
let leaf = dock::leaf_at(&w.dock, local);
out = dock::dock_payload(&mut w.dock, leaf, payload);
});
out
}
pub(super) fn dragout_start(
&mut self,
src: WindowId,
now: Seconds,
out: Outbound<P>,
fx: &mut LayoutEffects,
) {
let Outbound { leaf, payload, pos } = out;
let chrome = match self.policy.drag_out {
DragOutPolicy::Enabled { chrome } => Some(chrome),
DragOutPolicy::Disabled => None,
};
let screen = self.windows.get(&src).and_then(|w| project(w, pos));
let (Some(chrome), Some(screen)) = (chrome, screen) else {
self.dock_at_screen(src, Point::new(f64::NAN, f64::NAN), payload, fx);
return;
};
if let Some(dst) = self.window_at(screen, src) {
if let Some(to) = self.dock_at_screen(dst, screen, payload, fx) {
fx.push(LayoutEffect::Intent(DockIntent::PanelMoved {
from: (src, leaf),
to: (dst, to),
zone: uzor::layout::docking::DropZone::Center,
}));
}
return;
}
let Some(sw) = self.windows.get(&src) else {
return;
};
let (spec, grab) = micro_spec(sw, src, leaf, &payload, chrome, screen);
self.dragout = Some(DragOutSession {
src,
from: leaf,
chrome,
started: now,
held: Some(payload),
micro: None,
grab,
last_moved: None,
dwell: None,
});
fx.push(LayoutEffect::SpawnWindow { src, spec });
}
pub(super) fn dragout_adopt(&mut self, win: WindowId, fx: &mut LayoutEffects) {
let Some(s) = &mut self.dragout else {
return;
};
if s.micro.is_some() || s.held.is_none() {
return;
}
s.micro = Some(win);
fx.push(LayoutEffect::Intent(DockIntent::PanelDraggedOut {
from: (s.src, s.from),
to: win,
}));
self.dragout_place(win, fx);
}
pub(super) fn dragout_place(&mut self, win: WindowId, fx: &mut LayoutEffects) {
if !self.windows.contains_key(&win) {
return;
}
let Some(s) = &mut self.dragout else {
return;
};
if s.micro != Some(win) {
return;
}
let Some(payload) = s.held.take() else {
return;
};
let chrome = s.chrome;
self.edit(win, fx, |w, _, _| {
w.micro = Some(chrome);
if chrome == DragOutChrome::UzorChrome {
w.chrome_slot.visible = true;
w.chrome_slot.height = MICRO_CHROME_H as f32;
}
w.expand = None;
dock::dock_payload(&mut w.dock, None, payload);
});
}
pub(super) fn dragout_window_closed(&mut self, win: WindowId) {
if self.dragout.as_ref().is_some_and(|s| s.micro == Some(win)) {
self.dragout = None;
}
}
pub(super) fn dragout_tick(&mut self, now: Seconds, fx: &mut LayoutEffects) {
let Some(s) = &self.dragout else {
return;
};
let Some(m) = s.placed_in() else {
return;
};
let screen = self.windows.get(&m).and_then(|w| project(w, s.grab));
let target = screen.and_then(|p| self.window_at(p, m));
let since_move = s.last_moved.map(|t| now.get() - t.get());
let fresh = since_move.is_some_and(|d| d <= MOVE_FRESH_S);
let stale = since_move.is_none_or(|d| d > STALE_S);
let next = match (target, s.dwell) {
(None, _) => None,
(Some(target), None) if fresh => Some(Dwell {
started: now,
target,
}),
(Some(_), None) => None,
(Some(_), Some(_)) if stale => None,
(Some(target), Some(d)) if d.target != target => fresh.then_some(Dwell {
started: now,
target,
}),
(Some(target), Some(d)) if now.get() - d.started.get() >= DWELL_S => {
if let Some(screen) = screen {
self.dragout_complete(m, target, screen, fx);
}
return;
}
(Some(_), Some(d)) => Some(d),
};
if let Some(s) = &mut self.dragout {
s.dwell = next;
}
}
fn dragout_complete(
&mut self,
micro: WindowId,
dst: WindowId,
screen: Point,
fx: &mut LayoutEffects,
) {
let mut taken = None;
self.edit(micro, fx, |w, _, _| taken = dock::take_all(&mut w.dock));
if let Some((from, payload)) = taken {
if let Some(to) = self.dock_at_screen(dst, screen, payload, fx) {
fx.push(LayoutEffect::Intent(DockIntent::PanelMoved {
from: (micro, from),
to: (dst, to),
zone: uzor::layout::docking::DropZone::Center,
}));
}
}
fx.push(LayoutEffect::Window {
win: micro,
cmd: WindowCommand::SetVisible(false),
});
fx.push(LayoutEffect::Window {
win: micro,
cmd: WindowCommand::Close,
});
self.dragout = None;
}
}
fn micro_spec<P: DockPanel>(
sw: &WindowLayout<P>,
src: WindowId,
leaf: LeafId,
payload: &Payload<P>,
chrome: DragOutChrome,
screen: Point,
) -> (WindowSpec, Point) {
let pad_top = match chrome {
DragOutChrome::UzorChrome => MICRO_CHROME_H,
DragOutChrome::Os | DragOutChrome::None => 0.0,
};
let vp = sw.viewport;
let (pw, ph) = payload.size;
let w = pw.min(vp.width * MICRO_CAP).max(MICRO_MIN.0);
let h = (ph + pad_top).min(vp.height * MICRO_CAP).max(MICRO_MIN.1);
let header = sw.dock.tab_bar_height() as f64;
let grab = Point::new(
(w / 2.0).min(GRAB_X),
if pad_top > 0.0 {
pad_top / 2.0
} else {
header / 2.0
},
);
let scale = sw.scale;
let inner = SizePx::new((w * scale).round() as u32, (h * scale).round() as u32);
let mut spec = WindowSpec::new(
format!("drag-out-{}-{}", src.0, leaf.0),
payload.title(),
inner,
);
spec.min_inner_size = Some(MICRO_MIN_INNER);
spec.decorations = chrome == DragOutChrome::Os;
spec.position = Some((
(screen.x - grab.x * scale).round() as i32,
(screen.y - grab.y * scale).round() as i32,
));
(spec, grab)
}