Draft first-class TitleBar in pane_grid

This commit is contained in:
Héctor Ramón Jiménez 2020-06-04 07:13:38 +02:00
parent 4c494c7244
commit a11bcf5af0
10 changed files with 190 additions and 56 deletions

View file

@ -0,0 +1,30 @@
use crate::pane_grid::Axis;
/// The arrangement of a [`PaneGrid`].
///
/// [`PaneGrid`]: struct.PaneGrid.html
#[derive(Debug, Clone)]
pub enum Configuration<T> {
/// A split of the available space.
Split {
/// The direction of the split.
axis: Axis,
/// The ratio of the split in [0.0, 1.0].
ratio: f32,
/// The left/top [`Content`] of the split.
///
/// [`Configuration`]: enum.Node.html
a: Box<Configuration<T>>,
/// The right/bottom [`Content`] of the split.
///
/// [`Configuration`]: enum.Node.html
b: Box<Configuration<T>>,
},
/// A [`Pane`].
///
/// [`Pane`]: struct.Pane.html
Pane(T),
}

View file

@ -1,30 +1,88 @@
use crate::pane_grid::Axis;
use crate::layout;
use crate::pane_grid::{self, TitleBar};
use crate::{Clipboard, Element, Event, Hasher, Layout, Point};
/// The content of a [`PaneGrid`].
/// The content of a [`Pane`].
///
/// [`PaneGrid`]: struct.PaneGrid.html
#[derive(Debug, Clone)]
pub enum Content<T> {
/// A split of the available space.
Split {
/// The direction of the split.
axis: Axis,
/// The ratio of the split in [0.0, 1.0].
ratio: f32,
/// The left/top [`Content`] of the split.
///
/// [`Content`]: enum.Node.html
a: Box<Content<T>>,
/// The right/bottom [`Content`] of the split.
///
/// [`Content`]: enum.Node.html
b: Box<Content<T>>,
},
/// A [`Pane`].
///
/// [`Pane`]: struct.Pane.html
Pane(T),
/// [`Pane`]: struct.Pane.html
pub struct Content<'a, Message, Renderer> {
title_bar: Option<TitleBar<'a, Message, Renderer>>,
body: Element<'a, Message, Renderer>,
}
impl<'a, Message, Renderer> Content<'a, Message, Renderer> {
pub fn new(body: impl Into<Element<'a, Message, Renderer>>) -> Self {
Self {
title_bar: None,
body: body.into(),
}
}
pub fn title_bar(
mut self,
title_bar: TitleBar<'a, Message, Renderer>,
) -> Self {
self.title_bar = Some(title_bar);
self
}
}
impl<'a, Message, Renderer> Content<'a, Message, Renderer>
where
Renderer: pane_grid::Renderer,
{
pub fn draw(
&self,
renderer: &mut Renderer,
defaults: &Renderer::Defaults,
layout: Layout<'_>,
cursor_position: Point,
) -> Renderer::Output {
renderer.draw_pane(
defaults,
self.title_bar.as_ref(),
&self.body,
layout,
cursor_position,
)
}
pub(crate) fn is_over_drag_target(
&self,
_layout: Layout<'_>,
_cursor_position: Point,
) -> bool {
false
}
pub(crate) fn layout(
&self,
renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node {
self.body.layout(renderer, limits)
}
pub(crate) fn on_event(
&mut self,
event: Event,
layout: Layout<'_>,
cursor_position: Point,
messages: &mut Vec<Message>,
renderer: &Renderer,
clipboard: Option<&dyn Clipboard>,
) {
self.body.on_event(
event,
layout,
cursor_position,
messages,
renderer,
clipboard,
)
}
pub(crate) fn hash_layout(&self, state: &mut Hasher) {
self.body.hash_layout(state);
}
}

View file

@ -1,6 +1,6 @@
use crate::{
keyboard,
pane_grid::{Axis, Content, Direction, Node, Pane, Split},
pane_grid::{Axis, Configuration, Direction, Node, Pane, Split},
Hasher, Point, Rectangle, Size,
};
@ -53,18 +53,21 @@ impl<T> State<T> {
/// [`State`]: struct.State.html
/// [`Pane`]: struct.Pane.html
pub fn new(first_pane_state: T) -> (Self, Pane) {
(Self::with_content(Content::Pane(first_pane_state)), Pane(0))
(
Self::with_configuration(Configuration::Pane(first_pane_state)),
Pane(0),
)
}
/// Creates a new [`State`] with the given [`Content`].
/// Creates a new [`State`] with the given [`Configuration`].
///
/// [`State`]: struct.State.html
/// [`Content`]: enum.Content.html
pub fn with_content(content: impl Into<Content<T>>) -> Self {
/// [`Configuration`]: enum.Configuration.html
pub fn with_configuration(config: impl Into<Configuration<T>>) -> Self {
let mut panes = HashMap::new();
let (layout, last_id) =
Self::distribute_content(&mut panes, content.into(), 0);
Self::distribute_content(&mut panes, config.into(), 0);
State {
panes,
@ -274,11 +277,11 @@ impl<T> State<T> {
fn distribute_content(
panes: &mut HashMap<Pane, T>,
content: Content<T>,
content: Configuration<T>,
next_id: usize,
) -> (Node, usize) {
match content {
Content::Split { axis, ratio, a, b } => {
Configuration::Split { axis, ratio, a, b } => {
let (a, next_id) = Self::distribute_content(panes, *a, next_id);
let (b, next_id) = Self::distribute_content(panes, *b, next_id);
@ -293,7 +296,7 @@ impl<T> State<T> {
next_id + 1,
)
}
Content::Pane(state) => {
Configuration::Pane(state) => {
let id = Pane(next_id);
let _ = panes.insert(id, state);

View file

@ -0,0 +1,6 @@
use crate::Element;
pub struct TitleBar<'a, Message, Renderer> {
title: String,
buttons: Option<Element<'a, Message, Renderer>>,
}