Wire up container styling to iced_native

This commit is contained in:
Héctor Ramón Jiménez 2021-10-18 15:36:32 +07:00
parent edea093350
commit d61cb58d92
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
17 changed files with 65 additions and 99 deletions

View file

@ -150,7 +150,7 @@ impl Application for GameOfLife {
Container::new(content)
.width(Length::Fill)
.height(Length::Fill)
.style(style::Container)
.style(&style::Container)
.into()
}
}

View file

@ -177,7 +177,11 @@ impl Application for Example {
let title_bar = pane_grid::TitleBar::new(title)
.controls(pane.controls.view(id, total_panes, pane.is_pinned))
.padding(10)
.style(style::TitleBar { is_focused });
.style(if is_focused {
&style::TitleBar::Focused
} else {
&style::TitleBar::Active
});
pane_grid::Content::new(pane.content.view(
id,
@ -185,7 +189,11 @@ impl Application for Example {
pane.is_pinned,
))
.title_bar(title_bar)
.style(style::Pane { is_focused })
.style(if is_focused {
&style::Pane::Focused
} else {
&style::Pane::Active
})
})
.width(Length::Fill)
.height(Length::Fill)
@ -387,14 +395,16 @@ mod style {
0xC4 as f32 / 255.0,
);
pub struct TitleBar {
pub is_focused: bool,
pub enum TitleBar {
Active,
Focused,
}
impl container::StyleSheet for TitleBar {
fn style(&self) -> container::Style {
let pane = Pane {
is_focused: self.is_focused,
let pane = match self {
Self::Active => Pane::Active,
Self::Focused => Pane::Focused,
}
.style();
@ -406,8 +416,9 @@ mod style {
}
}
pub struct Pane {
pub is_focused: bool,
pub enum Pane {
Active,
Focused,
}
impl container::StyleSheet for Pane {
@ -415,10 +426,9 @@ mod style {
container::Style {
background: Some(Background::Color(SURFACE)),
border_width: 2.0,
border_color: if self.is_focused {
Color::BLACK
} else {
Color::from_rgb(0.7, 0.7, 0.7)
border_color: match self {
Self::Active => Color::from_rgb(0.7, 0.7, 0.7),
Self::Focused => Color::BLACK,
},
..Default::default()
}

View file

@ -164,7 +164,7 @@ impl Sandbox for ScrollableDemo {
Container::new(scrollable)
.width(Length::Fill)
.height(Length::Fill)
.style(*theme),
.style(theme.clone().into()),
)
.push(ProgressBar::new(
0.0..=1.0,
@ -190,7 +190,7 @@ impl Sandbox for ScrollableDemo {
.height(Length::Fill)
.center_x()
.center_y()
.style(self.theme)
.style(self.theme.into())
.into()
}
}

View file

@ -16,11 +16,11 @@ impl Default for Theme {
}
}
impl From<Theme> for Box<dyn container::StyleSheet> {
impl From<Theme> for &'static dyn container::StyleSheet {
fn from(theme: Theme) -> Self {
match theme {
Theme::Light => Default::default(),
Theme::Dark => dark::Container.into(),
Theme::Dark => &dark::Container,
}
}
}

View file

@ -149,7 +149,7 @@ impl Sandbox for Styling {
.height(Length::Fill)
.center_x()
.center_y()
.style(self.theme)
.style(self.theme.into())
.into()
}
}
@ -176,11 +176,11 @@ mod style {
}
}
impl From<Theme> for Box<dyn container::StyleSheet> {
impl From<Theme> for &'static dyn container::StyleSheet {
fn from(theme: Theme) -> Self {
match theme {
Theme::Light => Default::default(),
Theme::Dark => dark::Container.into(),
Theme::Dark => &dark::Container,
}
}
}

View file

@ -115,7 +115,7 @@ fn tooltip<'a>(
)
.gap(5)
.padding(10)
.style(style::Tooltip)
.style(&style::Tooltip)
.into()
}

View file

@ -1,6 +1,5 @@
//! Decorate content and apply alignment.
use crate::container;
use crate::{Backend, Renderer};
use crate::Renderer;
pub use iced_style::container::{Style, StyleSheet};
@ -10,10 +9,3 @@ pub use iced_style::container::{Style, StyleSheet};
/// `Renderer`.
pub type Container<'a, Message, Backend> =
iced_native::Container<'a, Message, Renderer<Backend>>;
impl<B> iced_native::container::Renderer for Renderer<B>
where
B: Backend,
{
type Style = Box<dyn container::StyleSheet>;
}

View file

@ -1,5 +1,4 @@
//! Build and show dropdown menus.
use crate::container;
use crate::event::{self, Event};
use crate::layout;
use crate::mouse;
@ -396,7 +395,7 @@ where
/// able to use a [`Menu`] in your user interface.
///
/// [renderer]: crate::renderer
pub trait Renderer: container::Renderer + text::Renderer {
pub trait Renderer: text::Renderer {
/// The [`Menu`] style supported by this renderer.
type Style: Default + Clone;
}

View file

@ -1,6 +1,5 @@
use crate::button;
use crate::checkbox;
use crate::container;
use crate::pane_grid;
use crate::progress_bar;
use crate::radio;
@ -123,10 +122,6 @@ impl progress_bar::Renderer for Null {
const DEFAULT_HEIGHT: u16 = 30;
}
impl container::Renderer for Null {
type Style = ();
}
impl pane_grid::Renderer for Null {
type Style = ();
}

View file

@ -13,11 +13,13 @@ use crate::{
use std::u32;
pub use iced_style::container::{Style, StyleSheet};
/// An element decorating some content.
///
/// It is normally used for alignment purposes.
#[allow(missing_debug_implementations)]
pub struct Container<'a, Message, Renderer: self::Renderer> {
pub struct Container<'a, Message, Renderer> {
padding: Padding,
width: Length,
height: Length,
@ -25,13 +27,13 @@ pub struct Container<'a, Message, Renderer: self::Renderer> {
max_height: u32,
horizontal_alignment: alignment::Horizontal,
vertical_alignment: alignment::Vertical,
style: Renderer::Style,
style: &'a dyn StyleSheet,
content: Element<'a, Message, Renderer>,
}
impl<'a, Message, Renderer> Container<'a, Message, Renderer>
where
Renderer: self::Renderer,
Renderer: crate::Renderer,
{
/// Creates an empty [`Container`].
pub fn new<T>(content: T) -> Self
@ -46,7 +48,7 @@ where
max_height: u32::MAX,
horizontal_alignment: alignment::Horizontal::Left,
vertical_alignment: alignment::Vertical::Top,
style: Renderer::Style::default(),
style: Default::default(),
content: content.into(),
}
}
@ -106,8 +108,8 @@ where
}
/// Sets the style of the [`Container`].
pub fn style(mut self, style: impl Into<Renderer::Style>) -> Self {
self.style = style.into();
pub fn style(mut self, style: &'a dyn StyleSheet) -> Self {
self.style = style;
self
}
}
@ -115,7 +117,7 @@ where
impl<'a, Message, Renderer> Widget<Message, Renderer>
for Container<'a, Message, Renderer>
where
Renderer: self::Renderer,
Renderer: crate::Renderer,
{
fn width(&self) -> Length {
self.width
@ -211,21 +213,10 @@ where
}
}
/// The renderer of a [`Container`].
///
/// Your [renderer] will need to implement this trait before being
/// able to use a [`Container`] in your user interface.
///
/// [renderer]: crate::renderer
pub trait Renderer: crate::Renderer {
/// The style supported by this renderer.
type Style: Default;
}
impl<'a, Message, Renderer> From<Container<'a, Message, Renderer>>
for Element<'a, Message, Renderer>
where
Renderer: 'a + self::Renderer,
Renderer: 'a + crate::Renderer,
Message: 'a,
{
fn from(

View file

@ -27,7 +27,6 @@ pub use split::Split;
pub use state::State;
pub use title_bar::TitleBar;
use crate::container;
use crate::event::{self, Event};
use crate::layout;
use crate::mouse;
@ -318,7 +317,7 @@ pub struct ResizeEvent {
impl<'a, Message, Renderer> Widget<Message, Renderer>
for PaneGrid<'a, Message, Renderer>
where
Renderer: self::Renderer + container::Renderer,
Renderer: self::Renderer,
{
fn width(&self) -> Length {
self.width
@ -565,7 +564,7 @@ where
/// able to use a [`PaneGrid`] in your user interface.
///
/// [renderer]: crate::renderer
pub trait Renderer: crate::Renderer + container::Renderer + Sized {
pub trait Renderer: crate::Renderer + Sized {
/// The style supported by this renderer.
type Style: Default;
}

View file

@ -13,7 +13,7 @@ use crate::{Clipboard, Element, Hasher, Layout, Point, Rectangle, Size};
pub struct Content<'a, Message, Renderer: pane_grid::Renderer> {
title_bar: Option<TitleBar<'a, Message, Renderer>>,
body: Element<'a, Message, Renderer>,
style: <Renderer as container::Renderer>::Style,
style: &'a dyn container::StyleSheet,
}
impl<'a, Message, Renderer> Content<'a, Message, Renderer>
@ -39,11 +39,8 @@ where
}
/// Sets the style of the [`Content`].
pub fn style(
mut self,
style: impl Into<<Renderer as container::Renderer>::Style>,
) -> Self {
self.style = style.into();
pub fn style(mut self, style: &'a dyn container::StyleSheet) -> Self {
self.style = style;
self
}
}
@ -217,7 +214,7 @@ where
impl<'a, T, Message, Renderer> From<T> for Content<'a, Message, Renderer>
where
T: Into<Element<'a, Message, Renderer>>,
Renderer: pane_grid::Renderer + container::Renderer,
Renderer: pane_grid::Renderer,
{
fn from(element: T) -> Self {
Self::new(element)

View file

@ -17,7 +17,7 @@ pub struct TitleBar<'a, Message, Renderer: pane_grid::Renderer> {
controls: Option<Element<'a, Message, Renderer>>,
padding: Padding,
always_show_controls: bool,
style: <Renderer as container::Renderer>::Style,
style: &'a dyn container::StyleSheet,
}
impl<'a, Message, Renderer> TitleBar<'a, Message, Renderer>
@ -54,11 +54,8 @@ where
}
/// Sets the style of the [`TitleBar`].
pub fn style(
mut self,
style: impl Into<<Renderer as container::Renderer>::Style>,
) -> Self {
self.style = style.into();
pub fn style(mut self, style: &'a dyn container::StyleSheet) -> Self {
self.style = style;
self
}

View file

@ -122,10 +122,7 @@ impl<'a, Message, Renderer: crate::Renderer> Scrollable<'a, Message, Renderer> {
}
/// Sets the style of the [`Scrollable`] .
pub fn style<'b>(mut self, style_sheet: &'b dyn StyleSheet) -> Self
where
'b: 'a,
{
pub fn style(mut self, style_sheet: &'a dyn StyleSheet) -> Self {
self.style_sheet = style_sheet;
self
}

View file

@ -16,7 +16,7 @@ pub struct Tooltip<'a, Message, Renderer: self::Renderer> {
content: Element<'a, Message, Renderer>,
tooltip: Text<Renderer>,
position: Position,
style: <Renderer as container::Renderer>::Style,
style: &'a dyn container::StyleSheet,
gap: u16,
padding: u16,
}
@ -70,11 +70,11 @@ where
}
/// Sets the style of the [`Tooltip`].
pub fn style(
mut self,
style: impl Into<<Renderer as container::Renderer>::Style>,
) -> Self {
self.style = style.into();
pub fn style<'b>(mut self, style: &'b dyn container::StyleSheet) -> Self
where
'b: 'a,
{
self.style = style;
self
}
}
@ -160,9 +160,7 @@ where
///
/// [`Tooltip`]: struct.Tooltip.html
/// [renderer]: ../../renderer/index.html
pub trait Renderer:
crate::Renderer + text::Renderer + container::Renderer
{
pub trait Renderer: crate::Renderer + text::Renderer {
/// The default padding of a [`Tooltip`] drawn by this renderer.
const DEFAULT_PADDING: u16;
}

View file

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

View file

@ -19,7 +19,7 @@ pub struct Container<'a, Message> {
max_height: u32,
horizontal_alignment: alignment::Horizontal,
vertical_alignment: alignment::Vertical,
style_sheet: Box<dyn StyleSheet>,
style_sheet: &'a dyn StyleSheet,
content: Element<'a, Message>,
}
@ -89,8 +89,8 @@ impl<'a, Message> Container<'a, Message> {
}
/// Sets the style of the [`Container`].
pub fn style(mut self, style: impl Into<Box<dyn StyleSheet>>) -> Self {
self.style_sheet = style.into();
pub fn style(mut self, style: &'a dyn StyleSheet) -> Self {
self.style_sheet = style;
self
}
}