Implement Widget::draw for PaneGrid

This commit is contained in:
Héctor Ramón Jiménez 2021-10-25 15:37:28 +07:00
parent 1afbc98544
commit 41394b4e90
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
6 changed files with 218 additions and 154 deletions

View file

@ -7,8 +7,7 @@
//! drag and drop, and hotkey support. //! drag and drop, and hotkey support.
//! //!
//! [`pane_grid` example]: https://github.com/hecrj/iced/tree/0.3/examples/pane_grid //! [`pane_grid` example]: https://github.com/hecrj/iced/tree/0.3/examples/pane_grid
use crate::{Backend, Renderer}; use crate::Renderer;
use iced_native::pane_grid;
pub use iced_native::pane_grid::{ pub use iced_native::pane_grid::{
Axis, Configuration, Content, Direction, DragEvent, Node, Pane, Axis, Configuration, Content, Direction, DragEvent, Node, Pane,
@ -25,10 +24,3 @@ pub use iced_style::pane_grid::{Line, StyleSheet};
/// This is an alias of an `iced_native` pane grid with an `iced_wgpu::Renderer`. /// This is an alias of an `iced_native` pane grid with an `iced_wgpu::Renderer`.
pub type PaneGrid<'a, Message, Backend> = pub type PaneGrid<'a, Message, Backend> =
iced_native::PaneGrid<'a, Message, Renderer<Backend>>; iced_native::PaneGrid<'a, Message, Renderer<Backend>>;
impl<B> pane_grid::Renderer for Renderer<B>
where
B: Backend,
{
type Style = Box<dyn StyleSheet>;
}

View file

@ -74,10 +74,6 @@ impl progress_bar::Renderer for Null {
const DEFAULT_HEIGHT: u16 = 30; const DEFAULT_HEIGHT: u16 = 30;
} }
impl pane_grid::Renderer for Null {
type Style = ();
}
impl toggler::Renderer for Null { impl toggler::Renderer for Null {
type Style = (); type Style = ();

View file

@ -34,10 +34,12 @@ use crate::overlay;
use crate::renderer; use crate::renderer;
use crate::touch; use crate::touch;
use crate::{ use crate::{
Clipboard, Element, Hasher, Layout, Length, Point, Rectangle, Size, Vector, Clipboard, Color, Element, Hasher, Layout, Length, Point, Rectangle, Size,
Widget, Vector, Widget,
}; };
pub use iced_style::pane_grid::{Line, StyleSheet};
/// A collection of panes distributed using either vertical or horizontal splits /// A collection of panes distributed using either vertical or horizontal splits
/// to completely fill the space available. /// to completely fill the space available.
/// ///
@ -88,7 +90,7 @@ use crate::{
/// .on_resize(10, Message::PaneResized); /// .on_resize(10, Message::PaneResized);
/// ``` /// ```
#[allow(missing_debug_implementations)] #[allow(missing_debug_implementations)]
pub struct PaneGrid<'a, Message, Renderer: self::Renderer> { pub struct PaneGrid<'a, Message, Renderer> {
state: &'a mut state::Internal, state: &'a mut state::Internal,
elements: Vec<(Pane, Content<'a, Message, Renderer>)>, elements: Vec<(Pane, Content<'a, Message, Renderer>)>,
width: Length, width: Length,
@ -97,12 +99,12 @@ pub struct PaneGrid<'a, Message, Renderer: self::Renderer> {
on_click: Option<Box<dyn Fn(Pane) -> Message + 'a>>, on_click: Option<Box<dyn Fn(Pane) -> Message + 'a>>,
on_drag: Option<Box<dyn Fn(DragEvent) -> Message + 'a>>, on_drag: Option<Box<dyn Fn(DragEvent) -> Message + 'a>>,
on_resize: Option<(u16, Box<dyn Fn(ResizeEvent) -> Message + 'a>)>, on_resize: Option<(u16, Box<dyn Fn(ResizeEvent) -> Message + 'a>)>,
style: <Renderer as self::Renderer>::Style, style_sheet: &'a dyn StyleSheet,
} }
impl<'a, Message, Renderer> PaneGrid<'a, Message, Renderer> impl<'a, Message, Renderer> PaneGrid<'a, Message, Renderer>
where where
Renderer: self::Renderer, Renderer: crate::Renderer,
{ {
/// Creates a [`PaneGrid`] with the given [`State`] and view function. /// Creates a [`PaneGrid`] with the given [`State`] and view function.
/// ///
@ -129,7 +131,7 @@ where
on_click: None, on_click: None,
on_drag: None, on_drag: None,
on_resize: None, on_resize: None,
style: Default::default(), style_sheet: Default::default(),
} }
} }
@ -189,18 +191,15 @@ where
} }
/// Sets the style of the [`PaneGrid`]. /// Sets the style of the [`PaneGrid`].
pub fn style( pub fn style(mut self, style: &'a dyn StyleSheet) -> Self {
mut self, self.style_sheet = style;
style: impl Into<<Renderer as self::Renderer>::Style>,
) -> Self {
self.style = style.into();
self self
} }
} }
impl<'a, Message, Renderer> PaneGrid<'a, Message, Renderer> impl<'a, Message, Renderer> PaneGrid<'a, Message, Renderer>
where where
Renderer: self::Renderer, Renderer: crate::Renderer,
{ {
fn click_pane( fn click_pane(
&mut self, &mut self,
@ -317,7 +316,7 @@ pub struct ResizeEvent {
impl<'a, Message, Renderer> Widget<Message, Renderer> impl<'a, Message, Renderer> Widget<Message, Renderer>
for PaneGrid<'a, Message, Renderer> for PaneGrid<'a, Message, Renderer>
where where
Renderer: self::Renderer, Renderer: crate::Renderer,
{ {
fn width(&self) -> Length { fn width(&self) -> Length {
self.width self.width
@ -480,55 +479,136 @@ where
cursor_position: Point, cursor_position: Point,
viewport: &Rectangle, viewport: &Rectangle,
) { ) {
// let picked_split = self let picked_pane = self.state.picked_pane();
// .state
// .picked_split()
// .and_then(|(split, axis)| {
// let bounds = layout.bounds();
// let splits = self let picked_split = self
// .state .state
// .split_regions(f32::from(self.spacing), bounds.size()); .picked_split()
.and_then(|(split, axis)| {
let bounds = layout.bounds();
// let (_axis, region, ratio) = splits.get(&split)?; let splits = self
.state
.split_regions(f32::from(self.spacing), bounds.size());
// let region = axis.split_line_bounds( let (_axis, region, ratio) = splits.get(&split)?;
// *region,
// *ratio,
// f32::from(self.spacing),
// );
// Some((axis, region + Vector::new(bounds.x, bounds.y), true)) let region = axis.split_line_bounds(
// }) *region,
// .or_else(|| match self.on_resize { *ratio,
// Some((leeway, _)) => { f32::from(self.spacing),
// let bounds = layout.bounds(); );
// let relative_cursor = Point::new( Some((axis, region + Vector::new(bounds.x, bounds.y), true))
// cursor_position.x - bounds.x, })
// cursor_position.y - bounds.y, .or_else(|| match self.on_resize {
// ); Some((leeway, _)) => {
let bounds = layout.bounds();
// let splits = self let relative_cursor = Point::new(
// .state cursor_position.x - bounds.x,
// .split_regions(f32::from(self.spacing), bounds.size()); cursor_position.y - bounds.y,
);
// let (_split, axis, region) = hovered_split( let splits = self
// splits.iter(), .state
// f32::from(self.spacing + leeway), .split_regions(f32::from(self.spacing), bounds.size());
// relative_cursor,
// )?;
// Some(( let (_split, axis, region) = hovered_split(
// axis, splits.iter(),
// region + Vector::new(bounds.x, bounds.y), f32::from(self.spacing + leeway),
// false, relative_cursor,
// )) )?;
// }
// None => None,
// });
// TODO Some((
axis,
region + Vector::new(bounds.x, bounds.y),
false,
))
}
None => None,
});
let pane_cursor_position = if picked_pane.is_some() {
// TODO: Remove once cursor availability is encoded in the type
// system
Point::new(-1.0, -1.0)
} else {
cursor_position
};
for ((id, pane), layout) in self.elements.iter().zip(layout.children())
{
match picked_pane {
Some((dragging, origin)) if *id == dragging => {
let bounds = layout.bounds();
renderer.with_layer(
Rectangle {
x: cursor_position.x - origin.x,
y: cursor_position.y - origin.y,
width: bounds.width + 0.5,
height: bounds.height + 0.5,
},
Vector::new(0, 0),
|renderer| {
pane.draw(
renderer,
style,
layout,
pane_cursor_position,
viewport,
);
},
);
}
_ => {
pane.draw(
renderer,
style,
layout,
pane_cursor_position,
viewport,
);
}
}
}
if let Some((axis, split_region, is_picked)) = picked_split {
let highlight = if is_picked {
self.style_sheet.picked_split()
} else {
self.style_sheet.hovered_split()
};
if let Some(highlight) = highlight {
renderer.fill_rectangle(renderer::Quad {
bounds: match axis {
Axis::Horizontal => Rectangle {
x: split_region.x,
y: (split_region.y
+ (split_region.height - highlight.width)
/ 2.0)
.round(),
width: split_region.width,
height: highlight.width,
},
Axis::Vertical => Rectangle {
x: (split_region.x
+ (split_region.width - highlight.width) / 2.0)
.round(),
y: split_region.y,
width: highlight.width,
height: split_region.height,
},
},
background: highlight.color.into(),
border_radius: 0.0,
border_width: 0.0,
border_color: Color::TRANSPARENT,
});
}
}
} }
fn hash_layout(&self, state: &mut Hasher) { fn hash_layout(&self, state: &mut Hasher) {
@ -558,21 +638,10 @@ where
} }
} }
/// The renderer of a [`PaneGrid`].
///
/// Your [renderer] will need to implement this trait before being
/// able to use a [`PaneGrid`] in your user interface.
///
/// [renderer]: crate::renderer
pub trait Renderer: crate::Renderer + Sized {
/// The style supported by this renderer.
type Style: Default;
}
impl<'a, Message, Renderer> From<PaneGrid<'a, Message, Renderer>> impl<'a, Message, Renderer> From<PaneGrid<'a, Message, Renderer>>
for Element<'a, Message, Renderer> for Element<'a, Message, Renderer>
where where
Renderer: 'a + self::Renderer, Renderer: 'a + crate::Renderer,
Message: 'a, Message: 'a,
{ {
fn from( fn from(

View file

@ -2,7 +2,7 @@ use crate::container;
use crate::event::{self, Event}; use crate::event::{self, Event};
use crate::layout; use crate::layout;
use crate::overlay; use crate::overlay;
use crate::pane_grid::{self, TitleBar}; use crate::pane_grid::TitleBar;
use crate::renderer; use crate::renderer;
use crate::{Clipboard, Element, Hasher, Layout, Point, Rectangle, Size}; use crate::{Clipboard, Element, Hasher, Layout, Point, Rectangle, Size};
@ -10,22 +10,22 @@ use crate::{Clipboard, Element, Hasher, Layout, Point, Rectangle, Size};
/// ///
/// [`Pane`]: crate::widget::pane_grid::Pane /// [`Pane`]: crate::widget::pane_grid::Pane
#[allow(missing_debug_implementations)] #[allow(missing_debug_implementations)]
pub struct Content<'a, Message, Renderer: pane_grid::Renderer> { pub struct Content<'a, Message, Renderer> {
title_bar: Option<TitleBar<'a, Message, Renderer>>, title_bar: Option<TitleBar<'a, Message, Renderer>>,
body: Element<'a, Message, Renderer>, body: Element<'a, Message, Renderer>,
style: &'a dyn container::StyleSheet, style_sheet: &'a dyn container::StyleSheet,
} }
impl<'a, Message, Renderer> Content<'a, Message, Renderer> impl<'a, Message, Renderer> Content<'a, Message, Renderer>
where where
Renderer: pane_grid::Renderer, Renderer: crate::Renderer,
{ {
/// Creates a new [`Content`] with the provided body. /// Creates a new [`Content`] with the provided body.
pub fn new(body: impl Into<Element<'a, Message, Renderer>>) -> Self { pub fn new(body: impl Into<Element<'a, Message, Renderer>>) -> Self {
Self { Self {
title_bar: None, title_bar: None,
body: body.into(), body: body.into(),
style: Default::default(), style_sheet: Default::default(),
} }
} }
@ -39,15 +39,15 @@ where
} }
/// Sets the style of the [`Content`]. /// Sets the style of the [`Content`].
pub fn style(mut self, style: &'a dyn container::StyleSheet) -> Self { pub fn style(mut self, style_sheet: &'a dyn container::StyleSheet) -> Self {
self.style = style; self.style_sheet = style_sheet;
self self
} }
} }
impl<'a, Message, Renderer> Content<'a, Message, Renderer> impl<'a, Message, Renderer> Content<'a, Message, Renderer>
where where
Renderer: pane_grid::Renderer, Renderer: crate::Renderer,
{ {
/// Draws the [`Content`] with the provided [`Renderer`] and [`Layout`]. /// Draws the [`Content`] with the provided [`Renderer`] and [`Layout`].
/// ///
@ -60,32 +60,41 @@ where
cursor_position: Point, cursor_position: Point,
viewport: &Rectangle, viewport: &Rectangle,
) { ) {
// TODO let bounds = layout.bounds();
// if let Some(title_bar) = &self.title_bar {
// let mut children = layout.children();
// let title_bar_layout = children.next().unwrap();
// let body_layout = children.next().unwrap();
// renderer.draw_pane( {
// defaults, let style = self.style_sheet.style();
// layout.bounds(),
// &self.style, container::draw_background(renderer, &style, bounds);
// Some((title_bar, title_bar_layout)), }
// (&self.body, body_layout),
// cursor_position, if let Some(title_bar) = &self.title_bar {
// viewport, let mut children = layout.children();
// ) let title_bar_layout = children.next().unwrap();
// } else { let body_layout = children.next().unwrap();
// renderer.draw_pane(
// defaults, let show_controls = bounds.contains(cursor_position);
// layout.bounds(),
// &self.style, title_bar.draw(
// None, renderer,
// (&self.body, layout), style,
// cursor_position, title_bar_layout,
// viewport, cursor_position,
// ) viewport,
// } show_controls,
);
self.body.draw(
renderer,
style,
body_layout,
cursor_position,
viewport,
);
} else {
self.body
.draw(renderer, style, layout, cursor_position, viewport);
}
} }
/// Returns whether the [`Content`] with the given [`Layout`] can be picked /// Returns whether the [`Content`] with the given [`Layout`] can be picked
@ -214,7 +223,7 @@ where
impl<'a, T, Message, Renderer> From<T> for Content<'a, Message, Renderer> impl<'a, T, Message, Renderer> From<T> for Content<'a, Message, Renderer>
where where
T: Into<Element<'a, Message, Renderer>>, T: Into<Element<'a, Message, Renderer>>,
Renderer: pane_grid::Renderer, Renderer: crate::Renderer,
{ {
fn from(element: T) -> Self { fn from(element: T) -> Self {
Self::new(element) Self::new(element)

View file

@ -2,7 +2,6 @@ use crate::container;
use crate::event::{self, Event}; use crate::event::{self, Event};
use crate::layout; use crate::layout;
use crate::overlay; use crate::overlay;
use crate::pane_grid;
use crate::renderer; use crate::renderer;
use crate::{ use crate::{
Clipboard, Element, Hasher, Layout, Padding, Point, Rectangle, Size, Clipboard, Element, Hasher, Layout, Padding, Point, Rectangle, Size,
@ -12,17 +11,17 @@ use crate::{
/// ///
/// [`Pane`]: crate::widget::pane_grid::Pane /// [`Pane`]: crate::widget::pane_grid::Pane
#[allow(missing_debug_implementations)] #[allow(missing_debug_implementations)]
pub struct TitleBar<'a, Message, Renderer: pane_grid::Renderer> { pub struct TitleBar<'a, Message, Renderer> {
content: Element<'a, Message, Renderer>, content: Element<'a, Message, Renderer>,
controls: Option<Element<'a, Message, Renderer>>, controls: Option<Element<'a, Message, Renderer>>,
padding: Padding, padding: Padding,
always_show_controls: bool, always_show_controls: bool,
style: &'a dyn container::StyleSheet, style_sheet: &'a dyn container::StyleSheet,
} }
impl<'a, Message, Renderer> TitleBar<'a, Message, Renderer> impl<'a, Message, Renderer> TitleBar<'a, Message, Renderer>
where where
Renderer: pane_grid::Renderer, Renderer: crate::Renderer,
{ {
/// Creates a new [`TitleBar`] with the given content. /// Creates a new [`TitleBar`] with the given content.
pub fn new<E>(content: E) -> Self pub fn new<E>(content: E) -> Self
@ -34,7 +33,7 @@ where
controls: None, controls: None,
padding: Padding::ZERO, padding: Padding::ZERO,
always_show_controls: false, always_show_controls: false,
style: Default::default(), style_sheet: Default::default(),
} }
} }
@ -55,7 +54,7 @@ where
/// Sets the style of the [`TitleBar`]. /// Sets the style of the [`TitleBar`].
pub fn style(mut self, style: &'a dyn container::StyleSheet) -> Self { pub fn style(mut self, style: &'a dyn container::StyleSheet) -> Self {
self.style = style; self.style_sheet = style;
self self
} }
@ -75,7 +74,7 @@ where
impl<'a, Message, Renderer> TitleBar<'a, Message, Renderer> impl<'a, Message, Renderer> TitleBar<'a, Message, Renderer>
where where
Renderer: pane_grid::Renderer, Renderer: crate::Renderer,
{ {
/// Draws the [`TitleBar`] with the provided [`Renderer`] and [`Layout`]. /// Draws the [`TitleBar`] with the provided [`Renderer`] and [`Layout`].
/// ///
@ -83,39 +82,47 @@ where
pub fn draw( pub fn draw(
&self, &self,
renderer: &mut Renderer, renderer: &mut Renderer,
style: &renderer::Style, inherited_style: &renderer::Style,
layout: Layout<'_>, layout: Layout<'_>,
cursor_position: Point, cursor_position: Point,
viewport: &Rectangle, viewport: &Rectangle,
show_controls: bool, show_controls: bool,
) { ) {
// let mut children = layout.children(); let bounds = layout.bounds();
// let padded = children.next().unwrap(); let style = self.style_sheet.style();
let inherited_style = renderer::Style {
text_color: style.text_color.unwrap_or(inherited_style.text_color),
};
// let mut children = padded.children(); container::draw_background(renderer, &style, bounds);
// let title_layout = children.next().unwrap();
// let controls = if let Some(controls) = &self.controls { let mut children = layout.children();
// let controls_layout = children.next().unwrap(); let padded = children.next().unwrap();
// if show_controls || self.always_show_controls { let mut children = padded.children();
// Some((controls, controls_layout)) let title_layout = children.next().unwrap();
// } else {
// None
// }
// } else {
// None
// };
// renderer.draw_title_bar( self.content.draw(
// defaults, renderer,
// layout.bounds(), &inherited_style,
// &self.style, title_layout,
// (&self.content, title_layout), cursor_position,
// controls, viewport,
// cursor_position, );
// viewport,
// ) if let Some(controls) = &self.controls {
let controls_layout = children.next().unwrap();
if show_controls || self.always_show_controls {
controls.draw(
renderer,
&inherited_style,
controls_layout,
cursor_position,
viewport,
);
}
}
} }
/// Returns whether the mouse cursor is over the pick area of the /// Returns whether the mouse cursor is over the pick area of the

View file

@ -35,17 +35,8 @@ impl StyleSheet for Default {
} }
} }
impl std::default::Default for Box<dyn StyleSheet> { impl std::default::Default for &'static dyn StyleSheet {
fn default() -> Self { fn default() -> Self {
Box::new(Default) &Default
}
}
impl<T> From<T> for Box<dyn StyleSheet>
where
T: 'static + StyleSheet,
{
fn from(style: T) -> Self {
Box::new(style)
} }
} }