use std::fmt;
use std::sync::{Arc, Mutex};
pub use bus_contract;
use bus_contract::{Bus, Point, PointDecl, PointKind, declare_all};
pub use crate::floats::ClosePolicy;
use crate::{DockSpace, Split};
#[derive(Debug)]
pub struct DockDrop;
impl Point for DockDrop {
const ID: &'static str = "stack.dock.drop";
const KIND: PointKind = PointKind::Request;
type Input = DropAsk;
type Value = ();
}
#[derive(Debug)]
pub struct LayoutChanged;
impl Point for LayoutChanged {
const ID: &'static str = "stack.layout.changed";
const KIND: PointKind = PointKind::Event;
type Input = LayoutChange;
type Value = ();
}
#[derive(Debug)]
pub struct WindowDetached;
impl Point for WindowDetached {
const ID: &'static str = "stack.window.detached";
const KIND: PointKind = PointKind::Event;
type Input = DetachedInfo;
type Value = ();
}
#[derive(Debug)]
pub struct WindowClose;
impl Point for WindowClose {
const ID: &'static str = "stack.window.close";
const KIND: PointKind = PointKind::Request;
type Input = CloseAsk;
type Value = ClosePolicy;
}
#[derive(Debug)]
pub struct WindowClosed;
impl Point for WindowClosed {
const ID: &'static str = "stack.window.closed";
const KIND: PointKind = PointKind::Event;
type Input = ClosedInfo;
type Value = ();
}
#[derive(Debug)]
pub struct WindowRepaired;
impl Point for WindowRepaired {
const ID: &'static str = "stack.window.repaired";
const KIND: PointKind = PointKind::Event;
type Input = RepairedInfo;
type Value = ();
}
pub const POINTS: &[PointDecl] = &[
PointDecl::of::<DockDrop>(
"A dragged tab or group is about to be dropped; a refusal vetoes it.",
)
.fail_closed(),
PointDecl::of::<LayoutChanged>("The layout or the docks changed."),
PointDecl::of::<WindowDetached>("A float opened from a tab or a group moved out of a dock."),
PointDecl::of::<WindowClose>("A float is closing: what becomes of its tabs."),
PointDecl::of::<WindowClosed>("A float closed."),
PointDecl::of::<WindowRepaired>("A float was brought back onto a screen, or docked back."),
];
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum DropTarget {
NewWindow,
EmptyDock,
Append,
Insert(usize),
Split(Split),
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct DropAsk {
pub tabs: Vec<String>,
pub group: bool,
pub from: Option<String>,
pub to: Option<String>,
pub from_float: bool,
pub to_float: bool,
pub target: DropTarget,
pub zone: Option<String>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Change {
Resized,
Reset,
Folded,
Unfolded,
AutoFolded,
AutoUnfolded,
StripClicked,
OpenedOver,
ClosedOver,
FlyoutClosed,
FlyoutResized,
TabMoved,
Detached,
FloatMoved,
FloatClosed,
FloatRepaired,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct LayoutChange {
pub change: Change,
pub zone: Option<String>,
pub dock: Option<String>,
pub float: Option<String>,
}
impl LayoutChange {
pub fn of(event: &crate::layout::LayoutEvent) -> Self {
use crate::layout::LayoutEvent;
let (change, zone) = match event {
LayoutEvent::Resized { zone, .. } => (Change::Resized, zone.0.clone()),
LayoutEvent::Reset { zone } => (Change::Reset, zone.0.clone()),
LayoutEvent::Folded { zone } => (Change::Folded, zone.0.clone()),
LayoutEvent::Unfolded { zone } => (Change::Unfolded, zone.0.clone()),
LayoutEvent::FlyoutClosed { key } => (Change::FlyoutClosed, key.clone()),
LayoutEvent::FlyoutResized { key, .. } => (Change::FlyoutResized, key.clone()),
LayoutEvent::AutoFolded { zone } => (Change::AutoFolded, zone.0.clone()),
LayoutEvent::AutoUnfolded { zone } => (Change::AutoUnfolded, zone.0.clone()),
LayoutEvent::StripClicked { zone, .. } => (Change::StripClicked, zone.0.clone()),
LayoutEvent::OpenedOver { zone } => (Change::OpenedOver, zone.0.clone()),
LayoutEvent::ClosedOver { zone } => (Change::ClosedOver, zone.0.clone()),
};
let mut change = Self::new(change);
change.zone = Some(zone);
change
}
pub fn new(change: Change) -> Self {
Self {
change,
zone: None,
dock: None,
float: None,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct DetachedInfo {
pub key: String,
pub dock: String,
pub tabs: Vec<String>,
pub native: bool,
pub screen: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct CloseAsk {
pub key: String,
pub dock: String,
pub tabs: Vec<String>,
pub origins: Vec<Option<String>>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct ClosedInfo {
pub key: String,
pub dock: String,
pub policy: Option<ClosePolicy>,
pub moved: Vec<(String, String)>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct RepairedInfo {
pub key: String,
pub from: Option<String>,
pub to: Option<String>,
}
pub(crate) struct SpaceBus<Tab> {
pub(crate) bus: Arc<dyn Bus>,
pub(crate) key: fn(&Tab) -> String,
verdicts: Verdicts,
}
type Verdicts = Arc<Mutex<Vec<(DropAsk, Option<String>)>>>;
impl<Tab> Clone for SpaceBus<Tab> {
fn clone(&self) -> Self {
Self {
bus: self.bus.clone(),
key: self.key,
verdicts: self.verdicts.clone(),
}
}
}
impl<Tab> fmt::Debug for SpaceBus<Tab> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SpaceBus").finish_non_exhaustive()
}
}
impl<Tab> SpaceBus<Tab> {
pub(crate) fn drop_refusal(&self, ask: DropAsk, fresh: bool) -> Option<String> {
if !fresh
&& let Ok(verdicts) = self.verdicts.lock()
&& let Some((_, verdict)) = verdicts.iter().find(|(asked, _)| *asked == ask)
{
return verdict.clone();
}
let out = self.bus.alter::<DockDrop>(ask.clone(), ());
let verdict = out.refused.map(|refusal| refusal.reason);
if let Ok(mut verdicts) = self.verdicts.lock() {
verdicts.retain(|(asked, _)| *asked != ask);
verdicts.push((ask, verdict.clone()));
}
verdict
}
pub(crate) fn forget_verdicts(&self) {
if let Ok(mut verdicts) = self.verdicts.lock() {
verdicts.clear();
}
}
pub(crate) fn close_policy(&self, ask: CloseAsk, policy: ClosePolicy) -> ClosePolicy {
let out = self.bus.alter::<WindowClose>(ask, policy);
if out.failures.is_empty() && out.refused.is_none() {
out.value
} else {
ClosePolicy::default()
}
}
pub(crate) fn emit<P: Point<Value = ()>>(&self, input: P::Input) {
let _ = self.bus.alter::<P>(input, ());
}
}
impl<Tab> DockSpace<Tab> {
#[must_use]
pub fn with_bus(mut self, bus: Arc<dyn Bus>, key: fn(&Tab) -> String) -> Self {
self.set_bus(bus, key);
self
}
pub fn set_bus(&mut self, bus: Arc<dyn Bus>, key: fn(&Tab) -> String) {
let _ = declare_all(&*bus, POINTS);
self.bus = Some(SpaceBus {
bus,
key,
verdicts: Arc::default(),
});
}
pub fn bus(&self) -> Option<&Arc<dyn Bus>> {
self.bus.as_ref().map(|bus| &bus.bus)
}
pub(crate) fn emit_change(&self, change: LayoutChange) {
if let Some(bus) = &self.bus {
bus.emit::<LayoutChanged>(change);
}
}
}