dockview_dioxus 0.1.9

A tiling/docking layout for Dioxus — panes split, resize, tab, float, and maximize, with the arrangement saved to JSON and restored on reload.
Documentation
//! Host-supplied configuration for [`PackedArea`](crate::render::packed::PackedArea).
//! Today just keybinds; passed once as a prop. Binds match the **produced character**
//! (`KeyboardEvent.key()`), so they follow the user's keyboard layout instead of a hardcoded
//! QWERTY physical position.

/// A single chord: the character the key produces, plus the non-shift modifiers held. Shift is
/// already baked into `key` (`"u"` vs `"U"`), so it isn't a separate flag.
#[derive(Clone, Copy, PartialEq)]
pub struct Keybind {
	/// Matched verbatim against `KeyboardEvent.key()` — e.g. `"u"`, `"U"`, `"Delete"`, `"f"`.
	pub key: &'static str,
	pub alt: bool,
	pub ctrl: bool,
}

impl Keybind {
	#[cfg(target_arch = "wasm32")]
	pub(crate) fn matches(&self, key: &str, alt: bool, ctrl: bool) -> bool {
		self.key == key && self.alt == alt && self.ctrl == ctrl
	}
}

/// Chords acting on the layout / the focused pane. Defaults: `u` / `U` for the undo tree,
/// `Backspace` to close the focused pane, `f` to toggle maximize on it, `?` for the keybind hint.
/// They never fire while an editable field is focused (see the listener), so bare letters don't
/// hijack typing.
#[derive(Clone, Copy, PartialEq)]
pub struct Keybinds {
	pub undo: Keybind = Keybind { key: "u", alt: false, ctrl: false },
	pub redo: Keybind = Keybind { key: "U", alt: false, ctrl: false },
	pub close: Keybind = Keybind { key: "Backspace", alt: false, ctrl: false },
	pub maximize: Keybind = Keybind { key: "f", alt: false, ctrl: false },
	pub help: Keybind = Keybind { key: "?", alt: false, ctrl: false },
}

impl Default for Keybinds {
	fn default() -> Self {
		Self { .. }
	}
}

#[derive(Clone, Default, PartialEq)]
pub struct Config {
	pub keybinds: Keybinds,
	/// Host-registered chords, each running arbitrary code over the live layout. Built-ins win on
	/// collision (the listener tries them first); the closure gets the same [`PackedApi`] `on_ready`
	/// hands out, so it can `save()` the current layout. A bare `Vec` is the whole API.
	pub actions: Vec<(Keybind, dioxus::prelude::Callback<crate::render::packed::PackedApi>)>,
	/// Desktop (`Xl`) column count: how many grid steps span the container's width on a wide screen.
	/// Smaller [`Breakpoint`]s scale this down so the *physical* step stays ~constant and tiles reflow
	/// instead of shrinking (see [`Breakpoint::scale_cols`]). The rendered horizontal step is
	/// `container_width / cols`, so within a band the layout still stretches to fill. A finer grid
	/// (more steps) gives smaller resize/placement increments.
	pub steps: u32 = 64,
	/// Row count — the vertical twin of [`steps`](Self::steps), but *not* scaled per [`Breakpoint`]:
	/// a narrow band is usually a taller device, so the `container_height / rows` vertical step
	/// already tracks the screen without help. Dividing by a *fixed* row count, not the used rows,
	/// keeps the whitespace-below look. The default ≈ a square step on a 16∶9 container (`64 × 9/16`).
	pub rows: u32 = 36,
}

/// Responsive width bands — Bootstrap's xs/sm/md/lg/xl boundaries (CSS px). The grid's column and
/// row counts are derived per band so the *physical* step size stays ~constant across devices: a
/// phone gets fewer steps than a desktop, so the same tiles reflow and stack down instead of
/// shrinking to illegibility. The count is fixed within a band (the grid still stretches to fill),
/// so a layout has one stable signature per band — persist one layout per `Breakpoint`, keyed by
/// its [`Display`](std::fmt::Display) name (`xs`/`sm`/`md`/`lg`/`xl`).
#[derive(Clone, Copy, Debug, Default, serde::Deserialize, Eq, Hash, PartialEq, serde::Serialize)]
pub enum Breakpoint {
	Xs,
	Sm,
	Md,
	Lg,
	#[default]
	Xl,
}

impl Breakpoint {
	/// Classify a container width (CSS px) into its band (Bootstrap's boundaries).
	pub(crate) fn of(width: f64) -> Self {
		match width {
			w if w < 576.0 => Self::Xs,
			w if w < 768.0 => Self::Sm,
			w if w < 992.0 => Self::Md,
			w if w < 1200.0 => Self::Lg,
			_ => Self::Xl,
		}
	}

	/// Design width the band scales against — its upper edge (the next band's [`of`](Self::of)
	/// threshold), with `Xl` (open-ended) capped at 1600.
	const fn design(self) -> f64 {
		match self {
			Self::Xs => 576.0,
			Self::Sm => 768.0,
			Self::Md => 992.0,
			Self::Lg => 1200.0,
			Self::Xl => 1600.0,
		}
	}

	/// Scale a desktop-tuned ([`Config`]) column count down to this band (≥ 1). `base · design /
	/// design(Xl)` holds the horizontal step's physical px ~constant — scaling the count by the
	/// band's width gives the same step size, hence the reflow rather than a shrink. Only *columns*
	/// scale: rows stay put because a narrow band is usually a taller device, so its height (and the
	/// `height / rows` vertical step) already tracks the screen on its own.
	pub(crate) fn scale_cols(self, base: u32) -> u32 {
		((base as f64 * self.design() / Self::Xl.design()).round() as u32).max(1)
	}
}

impl std::fmt::Display for Breakpoint {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		f.write_str(match self {
			Self::Xs => "xs",
			Self::Sm => "sm",
			Self::Md => "md",
			Self::Lg => "lg",
			Self::Xl => "xl",
		})
	}
}