Wire up styling to Radio in iced_native
This commit is contained in:
parent
cc560aca18
commit
d39ad717ed
11 changed files with 97 additions and 135 deletions
|
|
@ -76,7 +76,7 @@ impl Sandbox for ScrollableDemo {
|
||||||
Some(*theme),
|
Some(*theme),
|
||||||
Message::ThemeChanged,
|
Message::ThemeChanged,
|
||||||
)
|
)
|
||||||
.style(*theme),
|
.style(theme.clone().into()),
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -25,11 +25,11 @@ impl From<Theme> for &'static dyn container::StyleSheet {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Theme> for Box<dyn radio::StyleSheet> {
|
impl From<Theme> for &'static dyn radio::StyleSheet {
|
||||||
fn from(theme: Theme) -> Self {
|
fn from(theme: Theme) -> Self {
|
||||||
match theme {
|
match theme {
|
||||||
Theme::Light => Default::default(),
|
Theme::Light => Default::default(),
|
||||||
Theme::Dark => dark::Radio.into(),
|
Theme::Dark => &dark::Radio,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -64,7 +64,7 @@ impl Sandbox for Styling {
|
||||||
Some(self.theme),
|
Some(self.theme),
|
||||||
Message::ThemeChanged,
|
Message::ThemeChanged,
|
||||||
)
|
)
|
||||||
.style(self.theme),
|
.style(self.theme.into()),
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
@ -185,11 +185,11 @@ mod style {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Theme> for Box<dyn radio::StyleSheet> {
|
impl From<Theme> for &'static dyn radio::StyleSheet {
|
||||||
fn from(theme: Theme) -> Self {
|
fn from(theme: Theme) -> Self {
|
||||||
match theme {
|
match theme {
|
||||||
Theme::Light => Default::default(),
|
Theme::Light => Default::default(),
|
||||||
Theme::Dark => dark::Radio.into(),
|
Theme::Dark => &dark::Radio,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,4 +7,4 @@ pub use iced_graphics::radio::{Style, StyleSheet};
|
||||||
///
|
///
|
||||||
/// This is an alias of an `iced_native` radio button with an
|
/// This is an alias of an `iced_native` radio button with an
|
||||||
/// `iced_wgpu::Renderer`.
|
/// `iced_wgpu::Renderer`.
|
||||||
pub type Radio<Message> = iced_native::Radio<Message, Renderer>;
|
pub type Radio<'a, Message> = iced_native::Radio<'a, Message, Renderer>;
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
//! Create choices using radio buttons.
|
//! Create choices using radio buttons.
|
||||||
use crate::{Backend, Renderer};
|
use crate::Renderer;
|
||||||
use iced_native::radio;
|
|
||||||
|
|
||||||
pub use iced_style::radio::{Style, StyleSheet};
|
pub use iced_style::radio::{Style, StyleSheet};
|
||||||
|
|
||||||
|
|
@ -8,15 +7,5 @@ pub use iced_style::radio::{Style, StyleSheet};
|
||||||
///
|
///
|
||||||
/// This is an alias of an `iced_native` radio button with an
|
/// This is an alias of an `iced_native` radio button with an
|
||||||
/// `iced_wgpu::Renderer`.
|
/// `iced_wgpu::Renderer`.
|
||||||
pub type Radio<Message, Backend> =
|
pub type Radio<'a, Message, Backend> =
|
||||||
iced_native::Radio<Message, Renderer<Backend>>;
|
iced_native::Radio<'a, Message, Renderer<Backend>>;
|
||||||
|
|
||||||
impl<B> radio::Renderer for Renderer<B>
|
|
||||||
where
|
|
||||||
B: Backend,
|
|
||||||
{
|
|
||||||
type Style = Box<dyn StyleSheet>;
|
|
||||||
|
|
||||||
const DEFAULT_SIZE: u16 = 28;
|
|
||||||
const DEFAULT_SPACING: u16 = 15;
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -66,13 +66,6 @@ impl renderer::Text for Null {
|
||||||
fn fill_text(&mut self, _text: renderer::text::Section<'_, Self::Font>) {}
|
fn fill_text(&mut self, _text: renderer::text::Section<'_, Self::Font>) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl radio::Renderer for Null {
|
|
||||||
type Style = ();
|
|
||||||
|
|
||||||
const DEFAULT_SIZE: u16 = 20;
|
|
||||||
const DEFAULT_SPACING: u16 = 15;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl checkbox::Renderer for Null {
|
impl checkbox::Renderer for Null {
|
||||||
type Style = ();
|
type Style = ();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,12 +11,14 @@ use crate::{
|
||||||
Rectangle, Row, Text, Widget,
|
Rectangle, Row, Text, Widget,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub use iced_style::radio::{Style, StyleSheet};
|
||||||
|
|
||||||
/// A circular button representing a choice.
|
/// A circular button representing a choice.
|
||||||
///
|
///
|
||||||
/// # Example
|
/// # Example
|
||||||
/// ```
|
/// ```
|
||||||
/// # type Radio<Message> =
|
/// # type Radio<'a, Message> =
|
||||||
/// # iced_native::Radio<Message, iced_native::renderer::Null>;
|
/// # iced_native::Radio<'a, Message, iced_native::renderer::Null>;
|
||||||
/// #
|
/// #
|
||||||
/// #[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
/// #[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
/// pub enum Choice {
|
/// pub enum Choice {
|
||||||
|
|
@ -38,7 +40,7 @@ use crate::{
|
||||||
///
|
///
|
||||||
/// 
|
/// 
|
||||||
#[allow(missing_debug_implementations)]
|
#[allow(missing_debug_implementations)]
|
||||||
pub struct Radio<Message, Renderer: self::Renderer + renderer::Text> {
|
pub struct Radio<'a, Message, Renderer: renderer::Text> {
|
||||||
is_selected: bool,
|
is_selected: bool,
|
||||||
on_click: Message,
|
on_click: Message,
|
||||||
label: String,
|
label: String,
|
||||||
|
|
@ -48,14 +50,19 @@ pub struct Radio<Message, Renderer: self::Renderer + renderer::Text> {
|
||||||
text_size: Option<u16>,
|
text_size: Option<u16>,
|
||||||
text_color: Option<Color>,
|
text_color: Option<Color>,
|
||||||
font: Renderer::Font,
|
font: Renderer::Font,
|
||||||
style: Renderer::Style,
|
style_sheet: &'a dyn StyleSheet,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Message, Renderer: self::Renderer + renderer::Text>
|
impl<'a, Message, Renderer: renderer::Text> Radio<'a, Message, Renderer>
|
||||||
Radio<Message, Renderer>
|
|
||||||
where
|
where
|
||||||
Message: Clone,
|
Message: Clone,
|
||||||
{
|
{
|
||||||
|
/// The default size of a [`Radio`] button.
|
||||||
|
pub const DEFAULT_SIZE: u16 = 28;
|
||||||
|
|
||||||
|
/// The default spacing of a [`Radio`] button.
|
||||||
|
pub const DEFAULT_SPACING: u16 = 15;
|
||||||
|
|
||||||
/// Creates a new [`Radio`] button.
|
/// Creates a new [`Radio`] button.
|
||||||
///
|
///
|
||||||
/// It expects:
|
/// It expects:
|
||||||
|
|
@ -79,12 +86,12 @@ where
|
||||||
on_click: f(value),
|
on_click: f(value),
|
||||||
label: label.into(),
|
label: label.into(),
|
||||||
width: Length::Shrink,
|
width: Length::Shrink,
|
||||||
size: <Renderer as self::Renderer>::DEFAULT_SIZE,
|
size: Self::DEFAULT_SIZE,
|
||||||
spacing: Renderer::DEFAULT_SPACING, //15
|
spacing: Self::DEFAULT_SPACING, //15
|
||||||
text_size: None,
|
text_size: None,
|
||||||
text_color: None,
|
text_color: None,
|
||||||
font: Default::default(),
|
font: Default::default(),
|
||||||
style: Renderer::Style::default(),
|
style_sheet: Default::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -125,16 +132,17 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the style of the [`Radio`] button.
|
/// Sets the style of the [`Radio`] button.
|
||||||
pub fn style(mut self, style: impl Into<Renderer::Style>) -> Self {
|
pub fn style(mut self, style_sheet: &'a dyn StyleSheet) -> Self {
|
||||||
self.style = style.into();
|
self.style_sheet = style_sheet;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Message, Renderer> Widget<Message, Renderer> for Radio<Message, Renderer>
|
impl<'a, Message, Renderer> Widget<Message, Renderer>
|
||||||
|
for Radio<'a, Message, Renderer>
|
||||||
where
|
where
|
||||||
Message: Clone,
|
Message: Clone,
|
||||||
Renderer: self::Renderer + renderer::Text,
|
Renderer: renderer::Text,
|
||||||
{
|
{
|
||||||
fn width(&self) -> Length {
|
fn width(&self) -> Length {
|
||||||
self.width
|
self.width
|
||||||
|
|
@ -198,36 +206,6 @@ where
|
||||||
cursor_position: Point,
|
cursor_position: Point,
|
||||||
_viewport: &Rectangle,
|
_viewport: &Rectangle,
|
||||||
) {
|
) {
|
||||||
// TODO
|
|
||||||
// let bounds = layout.bounds();
|
|
||||||
// let mut children = layout.children();
|
|
||||||
|
|
||||||
// let radio_layout = children.next().unwrap();
|
|
||||||
// let label_layout = children.next().unwrap();
|
|
||||||
// let radio_bounds = radio_layout.bounds();
|
|
||||||
|
|
||||||
// let label = text::Renderer::draw(
|
|
||||||
// renderer,
|
|
||||||
// defaults,
|
|
||||||
// label_layout.bounds(),
|
|
||||||
// &self.label,
|
|
||||||
// self.text_size.unwrap_or(renderer.default_size()),
|
|
||||||
// self.font,
|
|
||||||
// self.text_color,
|
|
||||||
// alignment::Horizontal::Left,
|
|
||||||
// alignment::Vertical::Center,
|
|
||||||
// );
|
|
||||||
|
|
||||||
// let is_mouse_over = bounds.contains(cursor_position);
|
|
||||||
|
|
||||||
// self::Renderer::draw(
|
|
||||||
// renderer,
|
|
||||||
// radio_bounds,
|
|
||||||
// self.is_selected,
|
|
||||||
// is_mouse_over,
|
|
||||||
// label,
|
|
||||||
// &self.style,
|
|
||||||
// )
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn hash_layout(&self, state: &mut Hasher) {
|
fn hash_layout(&self, state: &mut Hasher) {
|
||||||
|
|
@ -238,30 +216,15 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The renderer of a [`Radio`] button.
|
impl<'a, Message, Renderer> From<Radio<'a, Message, Renderer>>
|
||||||
///
|
|
||||||
/// Your [renderer] will need to implement this trait before being
|
|
||||||
/// able to use a [`Radio`] button in your user interface.
|
|
||||||
///
|
|
||||||
/// [renderer]: crate::renderer
|
|
||||||
pub trait Renderer: crate::Renderer {
|
|
||||||
/// The style supported by this renderer.
|
|
||||||
type Style: Default;
|
|
||||||
|
|
||||||
/// The default size of a [`Radio`] button.
|
|
||||||
const DEFAULT_SIZE: u16;
|
|
||||||
|
|
||||||
/// The default spacing of a [`Radio`] button.
|
|
||||||
const DEFAULT_SPACING: u16;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a, Message, Renderer> From<Radio<Message, Renderer>>
|
|
||||||
for Element<'a, Message, Renderer>
|
for Element<'a, Message, Renderer>
|
||||||
where
|
where
|
||||||
Message: 'a + Clone,
|
Message: 'a + Clone,
|
||||||
Renderer: 'a + self::Renderer + renderer::Text,
|
Renderer: 'a + renderer::Text,
|
||||||
{
|
{
|
||||||
fn from(radio: Radio<Message, Renderer>) -> Element<'a, Message, Renderer> {
|
fn from(
|
||||||
|
radio: Radio<'a, Message, Renderer>,
|
||||||
|
) -> Element<'a, Message, Renderer> {
|
||||||
Element::new(radio)
|
Element::new(radio)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -140,29 +140,17 @@ where
|
||||||
_cursor_position: Point,
|
_cursor_position: Point,
|
||||||
_viewport: &Rectangle,
|
_viewport: &Rectangle,
|
||||||
) {
|
) {
|
||||||
let bounds = layout.bounds();
|
draw(
|
||||||
|
renderer,
|
||||||
let x = match self.horizontal_alignment {
|
style,
|
||||||
alignment::Horizontal::Left => bounds.x,
|
layout,
|
||||||
alignment::Horizontal::Center => bounds.center_x(),
|
&self.content,
|
||||||
alignment::Horizontal::Right => bounds.x + bounds.width,
|
self.font,
|
||||||
};
|
self.size,
|
||||||
|
self.color,
|
||||||
let y = match self.vertical_alignment {
|
self.horizontal_alignment,
|
||||||
alignment::Vertical::Top => bounds.y,
|
self.vertical_alignment,
|
||||||
alignment::Vertical::Center => bounds.center_y(),
|
);
|
||||||
alignment::Vertical::Bottom => bounds.y + bounds.height,
|
|
||||||
};
|
|
||||||
|
|
||||||
renderer.fill_text(renderer::text::Section {
|
|
||||||
content: &self.content,
|
|
||||||
size: f32::from(self.size.unwrap_or(renderer.default_size())),
|
|
||||||
bounds: Rectangle { x, y, ..bounds },
|
|
||||||
color: self.color.unwrap_or(style.text_color),
|
|
||||||
font: self.font,
|
|
||||||
horizontal_alignment: self.horizontal_alignment,
|
|
||||||
vertical_alignment: self.vertical_alignment,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn hash_layout(&self, state: &mut Hasher) {
|
fn hash_layout(&self, state: &mut Hasher) {
|
||||||
|
|
@ -176,6 +164,44 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn draw<Renderer>(
|
||||||
|
renderer: &mut Renderer,
|
||||||
|
style: &renderer::Style,
|
||||||
|
layout: Layout<'_>,
|
||||||
|
content: &str,
|
||||||
|
font: Renderer::Font,
|
||||||
|
size: Option<u16>,
|
||||||
|
color: Option<Color>,
|
||||||
|
horizontal_alignment: alignment::Horizontal,
|
||||||
|
vertical_alignment: alignment::Vertical,
|
||||||
|
) where
|
||||||
|
Renderer: renderer::Text,
|
||||||
|
{
|
||||||
|
let bounds = layout.bounds();
|
||||||
|
|
||||||
|
let x = match horizontal_alignment {
|
||||||
|
alignment::Horizontal::Left => bounds.x,
|
||||||
|
alignment::Horizontal::Center => bounds.center_x(),
|
||||||
|
alignment::Horizontal::Right => bounds.x + bounds.width,
|
||||||
|
};
|
||||||
|
|
||||||
|
let y = match vertical_alignment {
|
||||||
|
alignment::Vertical::Top => bounds.y,
|
||||||
|
alignment::Vertical::Center => bounds.center_y(),
|
||||||
|
alignment::Vertical::Bottom => bounds.y + bounds.height,
|
||||||
|
};
|
||||||
|
|
||||||
|
renderer.fill_text(renderer::text::Section {
|
||||||
|
content,
|
||||||
|
size: f32::from(size.unwrap_or(renderer.default_size())),
|
||||||
|
bounds: Rectangle { x, y, ..bounds },
|
||||||
|
color: color.unwrap_or(style.text_color),
|
||||||
|
font,
|
||||||
|
horizontal_alignment,
|
||||||
|
vertical_alignment,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a, Message, Renderer> From<Text<Renderer>>
|
impl<'a, Message, Renderer> From<Text<Renderer>>
|
||||||
for Element<'a, Message, Renderer>
|
for Element<'a, Message, Renderer>
|
||||||
where
|
where
|
||||||
|
|
|
||||||
|
|
@ -37,17 +37,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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -31,17 +31,17 @@ use dodrio::bumpalo;
|
||||||
///
|
///
|
||||||
/// 
|
/// 
|
||||||
#[allow(missing_debug_implementations)]
|
#[allow(missing_debug_implementations)]
|
||||||
pub struct Radio<Message> {
|
pub struct Radio<'a, Message> {
|
||||||
is_selected: bool,
|
is_selected: bool,
|
||||||
on_click: Message,
|
on_click: Message,
|
||||||
label: String,
|
label: String,
|
||||||
id: Option<String>,
|
id: Option<String>,
|
||||||
name: Option<String>,
|
name: Option<String>,
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
style: Box<dyn StyleSheet>,
|
style_sheet: &'a dyn StyleSheet,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Message> Radio<Message> {
|
impl<'a, Message> Radio<'a, Message> {
|
||||||
/// Creates a new [`Radio`] button.
|
/// Creates a new [`Radio`] button.
|
||||||
///
|
///
|
||||||
/// It expects:
|
/// It expects:
|
||||||
|
|
@ -66,13 +66,13 @@ impl<Message> Radio<Message> {
|
||||||
label: label.into(),
|
label: label.into(),
|
||||||
id: None,
|
id: None,
|
||||||
name: None,
|
name: None,
|
||||||
style: Default::default(),
|
style_sheet: Default::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the style of the [`Radio`] button.
|
/// Sets the style of the [`Radio`] button.
|
||||||
pub fn style(mut self, style: impl Into<Box<dyn StyleSheet>>) -> Self {
|
pub fn style(mut self, style_sheet: &'a dyn StyleSheet) -> Self {
|
||||||
self.style = style.into();
|
self.style_sheet = style_sheet;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -89,7 +89,7 @@ impl<Message> Radio<Message> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Message> Widget<Message> for Radio<Message>
|
impl<'a, Message> Widget<Message> for Radio<'a, Message>
|
||||||
where
|
where
|
||||||
Message: 'static + Clone,
|
Message: 'static + Clone,
|
||||||
{
|
{
|
||||||
|
|
@ -142,11 +142,11 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, Message> From<Radio<Message>> for Element<'a, Message>
|
impl<'a, Message> From<Radio<'a, Message>> for Element<'a, Message>
|
||||||
where
|
where
|
||||||
Message: 'static + Clone,
|
Message: 'static + Clone,
|
||||||
{
|
{
|
||||||
fn from(radio: Radio<Message>) -> Element<'a, Message> {
|
fn from(radio: Radio<'a, Message>) -> Element<'a, Message> {
|
||||||
Element::new(radio)
|
Element::new(radio)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,4 +7,4 @@ pub use iced_graphics::radio::{Style, StyleSheet};
|
||||||
///
|
///
|
||||||
/// This is an alias of an `iced_native` radio button with an
|
/// This is an alias of an `iced_native` radio button with an
|
||||||
/// `iced_wgpu::Renderer`.
|
/// `iced_wgpu::Renderer`.
|
||||||
pub type Radio<Message> = iced_native::Radio<Message, Renderer>;
|
pub type Radio<'a, Message> = iced_native::Radio<'a, Message, Renderer>;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue