Rename `overlay::Content` trait to `Overlay`

The `Overlay` struct is now `overlay::Element`.
This commit is contained in:
Héctor Ramón Jiménez 2020-07-10 01:31:56 +02:00
parent ce8cb228ef
commit 1070b61f34
12 changed files with 186 additions and 184 deletions

View File

@ -1,5 +1,5 @@
use crate::{
layout, Clipboard, Color, Event, Hasher, Layout, Length, Overlay, Point,
layout, overlay, Clipboard, Color, Event, Hasher, Layout, Length, Point,
Widget,
};
use std::rc::Rc;
@ -276,7 +276,7 @@ where
pub fn overlay<'b>(
&'b mut self,
layout: Layout<'_>,
) -> Option<Overlay<'b, Message, Renderer>> {
) -> Option<overlay::Element<'b, Message, Renderer>> {
self.widget.overlay(layout)
}
}
@ -366,7 +366,7 @@ where
fn overlay(
&mut self,
layout: Layout<'_>,
) -> Option<Overlay<'_, B, Renderer>> {
) -> Option<overlay::Element<'_, B, Renderer>> {
let mapper = self.mapper.clone();
self.widget
@ -452,7 +452,7 @@ where
fn overlay(
&mut self,
layout: Layout<'_>,
) -> Option<Overlay<'_, Message, Renderer>> {
) -> Option<overlay::Element<'_, Message, Renderer>> {
self.element.overlay(layout)
}
}

View File

@ -1,101 +1,13 @@
mod content;
mod element;
pub mod menu;
pub use content::Content;
pub use element::Element;
pub use menu::Menu;
use crate::{layout, Clipboard, Event, Hasher, Layout, Point, Size, Vector};
use std::rc::Rc;
use crate::{layout, Clipboard, Event, Hasher, Layout, Point, Size};
#[allow(missing_debug_implementations)]
pub struct Overlay<'a, Message, Renderer> {
position: Point,
content: Box<dyn Content<Message, Renderer> + 'a>,
}
impl<'a, Message, Renderer> Overlay<'a, Message, Renderer>
where
Renderer: crate::Renderer,
{
pub fn new(
position: Point,
content: Box<dyn Content<Message, Renderer> + 'a>,
) -> Self {
Self { position, content }
}
pub fn translate(mut self, translation: Vector) -> Self {
self.position = self.position + translation;
self
}
pub fn map<B>(self, f: Rc<dyn Fn(Message) -> B>) -> Overlay<'a, B, Renderer>
where
Message: 'a,
Renderer: 'a,
B: 'static,
{
Overlay {
position: self.position,
content: Box::new(Map::new(self.content, f)),
}
}
pub fn layout(&self, renderer: &Renderer, bounds: Size) -> layout::Node {
self.content.layout(renderer, bounds, self.position)
}
pub fn draw(
&self,
renderer: &mut Renderer,
defaults: &Renderer::Defaults,
layout: Layout<'_>,
cursor_position: Point,
) -> Renderer::Output {
self.content
.draw(renderer, defaults, layout, cursor_position)
}
pub fn hash_layout(&self, state: &mut Hasher) {
self.content.hash_layout(state, self.position);
}
pub fn on_event(
&mut self,
event: Event,
layout: Layout<'_>,
cursor_position: Point,
messages: &mut Vec<Message>,
renderer: &Renderer,
clipboard: Option<&dyn Clipboard>,
) {
self.content.on_event(
event,
layout,
cursor_position,
messages,
renderer,
clipboard,
)
}
}
struct Map<'a, A, B, Renderer> {
content: Box<dyn Content<A, Renderer> + 'a>,
mapper: Rc<dyn Fn(A) -> B>,
}
impl<'a, A, B, Renderer> Map<'a, A, B, Renderer> {
pub fn new(
content: Box<dyn Content<A, Renderer> + 'a>,
mapper: Rc<dyn Fn(A) -> B + 'static>,
) -> Map<'a, A, B, Renderer> {
Map { content, mapper }
}
}
impl<'a, A, B, Renderer> Content<B, Renderer> for Map<'a, A, B, Renderer>
pub trait Overlay<Message, Renderer>
where
Renderer: crate::Renderer,
{
@ -104,34 +16,7 @@ where
renderer: &Renderer,
bounds: Size,
position: Point,
) -> layout::Node {
self.content.layout(renderer, bounds, position)
}
fn on_event(
&mut self,
event: Event,
layout: Layout<'_>,
cursor_position: Point,
messages: &mut Vec<B>,
renderer: &Renderer,
clipboard: Option<&dyn Clipboard>,
) {
let mut original_messages = Vec::new();
self.content.on_event(
event,
layout,
cursor_position,
&mut original_messages,
renderer,
clipboard,
);
original_messages
.drain(..)
.for_each(|message| messages.push((self.mapper)(message)));
}
) -> layout::Node;
fn draw(
&self,
@ -139,12 +24,18 @@ where
defaults: &Renderer::Defaults,
layout: Layout<'_>,
cursor_position: Point,
) -> Renderer::Output {
self.content
.draw(renderer, defaults, layout, cursor_position)
}
) -> Renderer::Output;
fn hash_layout(&self, state: &mut Hasher, position: Point) {
self.content.hash_layout(state, position);
fn hash_layout(&self, state: &mut Hasher, position: Point);
fn on_event(
&mut self,
_event: Event,
_layout: Layout<'_>,
_cursor_position: Point,
_messages: &mut Vec<Message>,
_renderer: &Renderer,
_clipboard: Option<&dyn Clipboard>,
) {
}
}

View File

@ -1,34 +0,0 @@
use crate::{layout, Clipboard, Event, Hasher, Layout, Point, Size};
pub trait Content<Message, Renderer>
where
Renderer: crate::Renderer,
{
fn layout(
&self,
renderer: &Renderer,
bounds: Size,
position: Point,
) -> layout::Node;
fn draw(
&self,
renderer: &mut Renderer,
defaults: &Renderer::Defaults,
layout: Layout<'_>,
cursor_position: Point,
) -> Renderer::Output;
fn hash_layout(&self, state: &mut Hasher, position: Point);
fn on_event(
&mut self,
_event: Event,
_layout: Layout<'_>,
_cursor_position: Point,
_messages: &mut Vec<Message>,
_renderer: &Renderer,
_clipboard: Option<&dyn Clipboard>,
) {
}
}

View File

@ -0,0 +1,145 @@
pub use crate::Overlay;
use crate::{layout, Clipboard, Event, Hasher, Layout, Point, Size, Vector};
use std::rc::Rc;
#[allow(missing_debug_implementations)]
pub struct Element<'a, Message, Renderer> {
position: Point,
overlay: Box<dyn Overlay<Message, Renderer> + 'a>,
}
impl<'a, Message, Renderer> Element<'a, Message, Renderer>
where
Renderer: crate::Renderer,
{
pub fn new(
position: Point,
overlay: Box<dyn Overlay<Message, Renderer> + 'a>,
) -> Self {
Self { position, overlay }
}
pub fn translate(mut self, translation: Vector) -> Self {
self.position = self.position + translation;
self
}
pub fn map<B>(self, f: Rc<dyn Fn(Message) -> B>) -> Element<'a, B, Renderer>
where
Message: 'a,
Renderer: 'a,
B: 'static,
{
Element {
position: self.position,
overlay: Box::new(Map::new(self.overlay, f)),
}
}
pub fn layout(&self, renderer: &Renderer, bounds: Size) -> layout::Node {
self.overlay.layout(renderer, bounds, self.position)
}
pub fn draw(
&self,
renderer: &mut Renderer,
defaults: &Renderer::Defaults,
layout: Layout<'_>,
cursor_position: Point,
) -> Renderer::Output {
self.overlay
.draw(renderer, defaults, layout, cursor_position)
}
pub fn hash_layout(&self, state: &mut Hasher) {
self.overlay.hash_layout(state, self.position);
}
pub fn on_event(
&mut self,
event: Event,
layout: Layout<'_>,
cursor_position: Point,
messages: &mut Vec<Message>,
renderer: &Renderer,
clipboard: Option<&dyn Clipboard>,
) {
self.overlay.on_event(
event,
layout,
cursor_position,
messages,
renderer,
clipboard,
)
}
}
struct Map<'a, A, B, Renderer> {
content: Box<dyn Overlay<A, Renderer> + 'a>,
mapper: Rc<dyn Fn(A) -> B>,
}
impl<'a, A, B, Renderer> Map<'a, A, B, Renderer> {
pub fn new(
content: Box<dyn Overlay<A, Renderer> + 'a>,
mapper: Rc<dyn Fn(A) -> B + 'static>,
) -> Map<'a, A, B, Renderer> {
Map { content, mapper }
}
}
impl<'a, A, B, Renderer> Overlay<B, Renderer> for Map<'a, A, B, Renderer>
where
Renderer: crate::Renderer,
{
fn layout(
&self,
renderer: &Renderer,
bounds: Size,
position: Point,
) -> layout::Node {
self.content.layout(renderer, bounds, position)
}
fn on_event(
&mut self,
event: Event,
layout: Layout<'_>,
cursor_position: Point,
messages: &mut Vec<B>,
renderer: &Renderer,
clipboard: Option<&dyn Clipboard>,
) {
let mut original_messages = Vec::new();
self.content.on_event(
event,
layout,
cursor_position,
&mut original_messages,
renderer,
clipboard,
);
original_messages
.drain(..)
.for_each(|message| messages.push((self.mapper)(message)));
}
fn draw(
&self,
renderer: &mut Renderer,
defaults: &Renderer::Defaults,
layout: Layout<'_>,
cursor_position: Point,
) -> Renderer::Output {
self.content
.draw(renderer, defaults, layout, cursor_position)
}
fn hash_layout(&self, state: &mut Hasher, position: Point) {
self.content.hash_layout(state, position);
}
}

View File

@ -70,8 +70,8 @@ where
self,
position: Point,
target_height: f32,
) -> overlay::Overlay<'a, Message, Renderer> {
overlay::Overlay::new(
) -> overlay::Element<'a, Message, Renderer> {
overlay::Element::new(
position,
Box::new(Overlay::new(self, target_height)),
)
@ -149,7 +149,7 @@ where
}
}
impl<'a, Message, Renderer> overlay::Content<Message, Renderer>
impl<'a, Message, Renderer> crate::Overlay<Message, Renderer>
for Overlay<'a, Message, Renderer>
where
Renderer: self::Renderer,

View File

@ -1,4 +1,4 @@
use crate::{layout, Clipboard, Element, Event, Layout, Overlay, Point, Size};
use crate::{layout, overlay, Clipboard, Element, Event, Layout, Point, Size};
use std::hash::Hasher;
@ -398,7 +398,7 @@ where
fn overlay_layer(
cache: Option<Layer>,
bounds: Size,
overlay: &mut Overlay<'_, Message, Renderer>,
overlay: &mut overlay::Element<'_, Message, Renderer>,
renderer: &Renderer,
) -> Layer {
let new_hash = {

View File

@ -70,7 +70,7 @@ pub use text::Text;
#[doc(no_inline)]
pub use text_input::TextInput;
use crate::{layout, Clipboard, Event, Hasher, Layout, Length, Overlay, Point};
use crate::{layout, overlay, Clipboard, Event, Hasher, Layout, Length, Point};
/// A component that displays information and allows interaction.
///
@ -182,7 +182,7 @@ where
fn overlay<'b>(
&'b mut self,
_layout: Layout<'_>,
) -> Option<Overlay<'b, Message, Renderer>> {
) -> Option<overlay::Element<'b, Message, Renderer>> {
None
}
}

View File

@ -2,7 +2,7 @@
use std::hash::Hash;
use crate::{
layout, Align, Clipboard, Element, Event, Hasher, Layout, Length, Overlay,
layout, overlay, Align, Clipboard, Element, Event, Hasher, Layout, Length,
Point, Widget,
};
@ -208,7 +208,7 @@ where
fn overlay(
&mut self,
layout: Layout<'_>,
) -> Option<Overlay<'_, Message, Renderer>> {
) -> Option<overlay::Element<'_, Message, Renderer>> {
self.children
.iter_mut()
.zip(layout.children())

View File

@ -1,8 +1,8 @@
use crate::{
layout, mouse,
layout, mouse, overlay,
overlay::menu::{self, Menu},
scrollable, text, Clipboard, Element, Event, Hasher, Layout, Length,
Overlay, Point, Rectangle, Size, Widget,
scrollable, text, Clipboard, Element, Event, Hasher, Layout, Length, Point,
Rectangle, Size, Widget,
};
use std::borrow::Cow;
@ -225,7 +225,7 @@ where
fn overlay(
&mut self,
layout: Layout<'_>,
) -> Option<Overlay<'_, Message, Renderer>> {
) -> Option<overlay::Element<'_, Message, Renderer>> {
if self.menu.is_open() {
let bounds = layout.bounds();

View File

@ -2,7 +2,7 @@
use std::hash::Hash;
use crate::{
layout, Align, Clipboard, Element, Event, Hasher, Layout, Length, Overlay,
layout, overlay, Align, Clipboard, Element, Event, Hasher, Layout, Length,
Point, Rectangle, Widget,
};
@ -218,7 +218,7 @@ where
fn overlay(
&mut self,
layout: Layout<'_>,
) -> Option<Overlay<'_, Message, Renderer>> {
) -> Option<overlay::Element<'_, Message, Renderer>> {
self.content.overlay(layout.children().next().unwrap())
}
}

View File

@ -2,7 +2,7 @@
use std::hash::Hash;
use crate::{
layout, Align, Clipboard, Element, Event, Hasher, Layout, Length, Overlay,
layout, overlay, Align, Clipboard, Element, Event, Hasher, Layout, Length,
Point, Widget,
};
@ -210,7 +210,7 @@ where
fn overlay(
&mut self,
layout: Layout<'_>,
) -> Option<Overlay<'_, Message, Renderer>> {
) -> Option<overlay::Element<'_, Message, Renderer>> {
self.children
.iter_mut()
.zip(layout.children())

View File

@ -1,7 +1,7 @@
//! Navigate an endless amount of content with a scrollbar.
use crate::{
column, layout, mouse, Align, Clipboard, Column, Element, Event, Hasher,
Layout, Length, Overlay, Point, Rectangle, Size, Vector, Widget,
column, layout, mouse, overlay, Align, Clipboard, Column, Element, Event,
Hasher, Layout, Length, Point, Rectangle, Size, Vector, Widget,
};
use std::{f32, hash::Hash, u32};
@ -319,7 +319,7 @@ where
fn overlay(
&mut self,
layout: Layout<'_>,
) -> Option<Overlay<'_, Message, Renderer>> {
) -> Option<overlay::Element<'_, Message, Renderer>> {
let Self { content, state, .. } = self;
content