mod axis;
mod configuration;
mod content;
mod direction;
mod node;
mod pane;
mod split;
mod state;
mod title_bar;
pub use axis::Axis;
pub use configuration::Configuration;
pub use content::Content;
pub use direction::Direction;
pub use node::Node;
pub use pane::Pane;
pub use split::Split;
pub use state::{Focus, State};
pub use title_bar::TitleBar;
use crate::{
container, keyboard, layout, mouse, overlay, row, text, Clipboard, Element,
Event, Hasher, Layout, Length, Point, Rectangle, Size, Vector, Widget,
};
#[allow(missing_debug_implementations)]
pub struct PaneGrid<'a, Message, Renderer: self::Renderer> {
state: &'a mut state::Internal,
elements: Vec<(Pane, Content<'a, Message, Renderer>)>,
width: Length,
height: Length,
spacing: u16,
modifier_keys: keyboard::ModifiersState,
on_drag: Option<Box<dyn Fn(DragEvent) -> Message + 'a>>,
on_resize: Option<(u16, Box<dyn Fn(ResizeEvent) -> Message + 'a>)>,
on_key_press: Option<Box<dyn Fn(KeyPressEvent) -> Option<Message> + 'a>>,
}
impl<'a, Message, Renderer> PaneGrid<'a, Message, Renderer>
where
Renderer: self::Renderer,
{
pub fn new<T>(
state: &'a mut State<T>,
view: impl Fn(
Pane,
&'a mut T,
Option<Focus>,
) -> Content<'a, Message, Renderer>,
) -> Self {
let elements = {
let action = state.internal.action();
let current_focus = action.focus();
state
.panes
.iter_mut()
.map(move |(pane, pane_state)| {
let focus = match current_focus {
Some((focused_pane, focus))
if *pane == focused_pane =>
{
Some(focus)
}
_ => None,
};
(*pane, view(*pane, pane_state, focus))
})
.collect()
};
Self {
state: &mut state.internal,
elements,
width: Length::Fill,
height: Length::Fill,
spacing: 0,
modifier_keys: keyboard::ModifiersState {
control: true,
..Default::default()
},
on_drag: None,
on_resize: None,
on_key_press: None,
}
}
pub fn width(mut self, width: Length) -> Self {
self.width = width;
self
}
pub fn height(mut self, height: Length) -> Self {
self.height = height;
self
}
pub fn spacing(mut self, units: u16) -> Self {
self.spacing = units;
self
}
pub fn modifier_keys(
mut self,
modifier_keys: keyboard::ModifiersState,
) -> Self {
self.modifier_keys = modifier_keys;
self
}
pub fn on_drag<F>(mut self, f: F) -> Self
where
F: 'a + Fn(DragEvent) -> Message,
{
self.on_drag = Some(Box::new(f));
self
}
pub fn on_resize<F>(mut self, leeway: u16, f: F) -> Self
where
F: 'a + Fn(ResizeEvent) -> Message,
{
self.on_resize = Some((leeway, Box::new(f)));
self
}
pub fn on_key_press<F>(mut self, f: F) -> Self
where
F: 'a + Fn(KeyPressEvent) -> Option<Message>,
{
self.on_key_press = Some(Box::new(f));
self
}
}
impl<'a, Message, Renderer> PaneGrid<'a, Message, Renderer>
where
Renderer: self::Renderer,
{
fn click_pane(
&mut self,
layout: Layout<'_>,
cursor_position: Point,
messages: &mut Vec<Message>,
) {
let mut clicked_region =
self.elements.iter().zip(layout.children()).filter(
|(_, layout)| layout.bounds().contains(cursor_position),
);
if let Some(((pane, content), layout)) = clicked_region.next() {
match &self.on_drag {
Some(on_drag) => {
if content.can_be_picked_at(layout, cursor_position) {
let pane_position = layout.position();
let origin = cursor_position
- Vector::new(pane_position.x, pane_position.y);
self.state.pick_pane(pane, origin);
messages
.push(on_drag(DragEvent::Picked { pane: *pane }));
} else {
self.state.focus(pane);
}
}
None => {
self.state.focus(pane);
}
}
}
}
fn trigger_resize(
&mut self,
layout: Layout<'_>,
cursor_position: Point,
messages: &mut Vec<Message>,
) {
if let Some((_, on_resize)) = &self.on_resize {
if let Some((split, _)) = self.state.picked_split() {
let bounds = layout.bounds();
let splits = self.state.split_regions(
f32::from(self.spacing),
Size::new(bounds.width, bounds.height),
);
if let Some((axis, rectangle, _)) = splits.get(&split) {
let ratio = match axis {
Axis::Horizontal => {
let position =
cursor_position.y - bounds.y - rectangle.y;
(position / rectangle.height).max(0.1).min(0.9)
}
Axis::Vertical => {
let position =
cursor_position.x - bounds.x - rectangle.x;
(position / rectangle.width).max(0.1).min(0.9)
}
};
messages.push(on_resize(ResizeEvent { split, ratio }));
}
}
}
}
}
#[derive(Debug, Clone, Copy)]
pub enum DragEvent {
Picked {
pane: Pane,
},
Dropped {
pane: Pane,
target: Pane,
},
Canceled {
pane: Pane,
},
}
#[derive(Debug, Clone, Copy)]
pub struct ResizeEvent {
pub split: Split,
pub ratio: f32,
}
#[derive(Debug, Clone, Copy)]
pub struct KeyPressEvent {
pub key_code: keyboard::KeyCode,
pub modifiers: keyboard::ModifiersState,
}
impl<'a, Message, Renderer> Widget<Message, Renderer>
for PaneGrid<'a, Message, Renderer>
where
Renderer: self::Renderer + container::Renderer,
{
fn width(&self) -> Length {
self.width
}
fn height(&self) -> Length {
self.height
}
fn layout(
&self,
renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node {
let limits = limits.width(self.width).height(self.height);
let size = limits.resolve(Size::ZERO);
let regions = self.state.pane_regions(f32::from(self.spacing), size);
let children = self
.elements
.iter()
.filter_map(|(pane, element)| {
let region = regions.get(pane)?;
let size = Size::new(region.width, region.height);
let mut node =
element.layout(renderer, &layout::Limits::new(size, size));
node.move_to(Point::new(region.x, region.y));
Some(node)
})
.collect();
layout::Node::with_children(size, children)
}
fn on_event(
&mut self,
event: Event,
layout: Layout<'_>,
cursor_position: Point,
messages: &mut Vec<Message>,
renderer: &Renderer,
clipboard: Option<&dyn Clipboard>,
) {
match event {
Event::Mouse(mouse_event) => match mouse_event {
mouse::Event::ButtonPressed(mouse::Button::Left) => {
let bounds = layout.bounds();
if bounds.contains(cursor_position) {
match self.on_resize {
Some((leeway, _)) => {
let relative_cursor = Point::new(
cursor_position.x - bounds.x,
cursor_position.y - bounds.y,
);
let splits = self.state.split_regions(
f32::from(self.spacing),
Size::new(bounds.width, bounds.height),
);
let clicked_split = hovered_split(
splits.iter(),
f32::from(self.spacing + leeway),
relative_cursor,
);
if let Some((split, axis)) = clicked_split {
self.state.pick_split(&split, axis);
} else {
self.click_pane(
layout,
cursor_position,
messages,
);
}
}
None => {
self.click_pane(
layout,
cursor_position,
messages,
);
}
}
} else {
if cursor_position.x > 0.0 && cursor_position.y > 0.0 {
self.state.unfocus();
}
}
}
mouse::Event::ButtonReleased(mouse::Button::Left) => {
if let Some((pane, _)) = self.state.picked_pane() {
self.state.focus(&pane);
if let Some(on_drag) = &self.on_drag {
let mut dropped_region = self
.elements
.iter()
.zip(layout.children())
.filter(|(_, layout)| {
layout.bounds().contains(cursor_position)
});
let event = match dropped_region.next() {
Some(((target, _), _)) if pane != *target => {
DragEvent::Dropped {
pane,
target: *target,
}
}
_ => DragEvent::Canceled { pane },
};
messages.push(on_drag(event));
}
} else if self.state.picked_split().is_some() {
self.state.drop_split();
}
}
mouse::Event::CursorMoved { .. } => {
self.trigger_resize(layout, cursor_position, messages);
}
_ => {}
},
Event::Keyboard(keyboard_event) => {
match keyboard_event {
keyboard::Event::KeyPressed {
modifiers,
key_code,
} => {
if let Some(on_key_press) = &self.on_key_press {
if let Some(_) = self.state.active_pane() {
if modifiers.matches(self.modifier_keys) {
if let Some(message) =
on_key_press(KeyPressEvent {
key_code,
modifiers,
})
{
messages.push(message);
}
}
}
}
}
_ => {}
}
}
_ => {}
}
if self.state.picked_pane().is_none() {
{
self.elements.iter_mut().zip(layout.children()).for_each(
|((_, pane), layout)| {
pane.on_event(
event.clone(),
layout,
cursor_position,
messages,
renderer,
clipboard,
)
},
);
}
}
}
fn draw(
&self,
renderer: &mut Renderer,
defaults: &Renderer::Defaults,
layout: Layout<'_>,
cursor_position: Point,
) -> Renderer::Output {
let picked_split = self
.state
.picked_split()
.or_else(|| match self.on_resize {
Some((leeway, _)) => {
let bounds = layout.bounds();
let relative_cursor = Point::new(
cursor_position.x - bounds.x,
cursor_position.y - bounds.y,
);
let splits = self
.state
.split_regions(f32::from(self.spacing), bounds.size());
hovered_split(
splits.iter(),
f32::from(self.spacing + leeway),
relative_cursor,
)
}
None => None,
})
.map(|(_, axis)| axis);
self::Renderer::draw(
renderer,
defaults,
&self.elements,
self.state.picked_pane(),
picked_split,
layout,
cursor_position,
)
}
fn hash_layout(&self, state: &mut Hasher) {
use std::hash::Hash;
struct Marker;
std::any::TypeId::of::<Marker>().hash(state);
self.width.hash(state);
self.height.hash(state);
self.state.hash_layout(state);
for (_, element) in &self.elements {
element.hash_layout(state);
}
}
fn overlay(
&mut self,
layout: Layout<'_>,
) -> Option<overlay::Element<'_, Message, Renderer>> {
self.elements
.iter_mut()
.zip(layout.children())
.filter_map(|((_, pane), layout)| pane.overlay(layout))
.next()
}
}
pub trait Renderer:
crate::Renderer + container::Renderer + text::Renderer + Sized
{
fn draw<Message>(
&mut self,
defaults: &Self::Defaults,
content: &[(Pane, Content<'_, Message, Self>)],
dragging: Option<(Pane, Point)>,
resizing: Option<Axis>,
layout: Layout<'_>,
cursor_position: Point,
) -> Self::Output;
fn draw_pane<Message>(
&mut self,
defaults: &Self::Defaults,
bounds: Rectangle,
style: &Self::Style,
title_bar: Option<(&TitleBar<'_, Message, Self>, Layout<'_>)>,
body: (&Element<'_, Message, Self>, Layout<'_>),
cursor_position: Point,
) -> Self::Output;
fn draw_title_bar<Message>(
&mut self,
defaults: &Self::Defaults,
bounds: Rectangle,
style: &Self::Style,
title: &str,
title_size: u16,
title_font: Self::Font,
title_bounds: Rectangle,
controls: Option<(&Element<'_, Message, Self>, Layout<'_>)>,
cursor_position: Point,
) -> Self::Output;
}
impl<'a, Message, Renderer> From<PaneGrid<'a, Message, Renderer>>
for Element<'a, Message, Renderer>
where
Renderer: 'a + self::Renderer + row::Renderer,
Message: 'a,
{
fn from(
pane_grid: PaneGrid<'a, Message, Renderer>,
) -> Element<'a, Message, Renderer> {
Element::new(pane_grid)
}
}
fn hovered_split<'a>(
splits: impl Iterator<Item = (&'a Split, &'a (Axis, Rectangle, f32))>,
spacing: f32,
cursor_position: Point,
) -> Option<(Split, Axis)> {
splits
.filter_map(|(split, (axis, region, ratio))| {
let bounds =
axis.split_line_bounds(*region, *ratio, f32::from(spacing));
if bounds.contains(cursor_position) {
Some((*split, *axis))
} else {
None
}
})
.next()
}