Remove Message: Clone
bound in some web widgets
This commit is contained in:
parent
b5cd9923f2
commit
361be7f6b7
@ -8,11 +8,18 @@ use std::rc::Rc;
|
|||||||
///
|
///
|
||||||
/// [`Application`]: trait.Application.html
|
/// [`Application`]: trait.Application.html
|
||||||
#[allow(missing_debug_implementations)]
|
#[allow(missing_debug_implementations)]
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct Bus<Message> {
|
pub struct Bus<Message> {
|
||||||
publish: Rc<Box<dyn Fn(Message, &mut dyn dodrio::RootRender)>>,
|
publish: Rc<Box<dyn Fn(Message, &mut dyn dodrio::RootRender)>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<Message> Clone for Bus<Message> {
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
Self {
|
||||||
|
publish: Rc::clone(&self.publish),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<Message> Bus<Message>
|
impl<Message> Bus<Message>
|
||||||
where
|
where
|
||||||
Message: 'static,
|
Message: 'static,
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
use crate::{style, Bus, Color, Element, Widget};
|
use crate::{style, Bus, Color, Element, Widget};
|
||||||
|
|
||||||
use dodrio::bumpalo;
|
use dodrio::bumpalo;
|
||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
/// A box that can be checked.
|
/// A box that can be checked.
|
||||||
///
|
///
|
||||||
@ -22,7 +23,7 @@ use dodrio::bumpalo;
|
|||||||
#[allow(missing_debug_implementations)]
|
#[allow(missing_debug_implementations)]
|
||||||
pub struct Checkbox<Message> {
|
pub struct Checkbox<Message> {
|
||||||
is_checked: bool,
|
is_checked: bool,
|
||||||
on_toggle: Box<dyn Fn(bool) -> Message>,
|
on_toggle: Rc<dyn Fn(bool) -> Message>,
|
||||||
label: String,
|
label: String,
|
||||||
label_color: Option<Color>,
|
label_color: Option<Color>,
|
||||||
}
|
}
|
||||||
@ -44,7 +45,7 @@ impl<Message> Checkbox<Message> {
|
|||||||
{
|
{
|
||||||
Checkbox {
|
Checkbox {
|
||||||
is_checked,
|
is_checked,
|
||||||
on_toggle: Box::new(f),
|
on_toggle: Rc::new(f),
|
||||||
label: String::from(label),
|
label: String::from(label),
|
||||||
label_color: None,
|
label_color: None,
|
||||||
}
|
}
|
||||||
@ -61,7 +62,7 @@ impl<Message> Checkbox<Message> {
|
|||||||
|
|
||||||
impl<Message> Widget<Message> for Checkbox<Message>
|
impl<Message> Widget<Message> for Checkbox<Message>
|
||||||
where
|
where
|
||||||
Message: 'static + Clone,
|
Message: 'static,
|
||||||
{
|
{
|
||||||
fn node<'b>(
|
fn node<'b>(
|
||||||
&self,
|
&self,
|
||||||
@ -74,7 +75,8 @@ where
|
|||||||
let checkbox_label = bumpalo::format!(in bump, "{}", self.label);
|
let checkbox_label = bumpalo::format!(in bump, "{}", self.label);
|
||||||
|
|
||||||
let event_bus = bus.clone();
|
let event_bus = bus.clone();
|
||||||
let msg = (self.on_toggle)(!self.is_checked);
|
let on_toggle = self.on_toggle.clone();
|
||||||
|
let is_checked = self.is_checked;
|
||||||
|
|
||||||
// TODO: Complete styling
|
// TODO: Complete styling
|
||||||
label(bump)
|
label(bump)
|
||||||
@ -83,7 +85,8 @@ where
|
|||||||
.attr("type", "checkbox")
|
.attr("type", "checkbox")
|
||||||
.bool_attr("checked", self.is_checked)
|
.bool_attr("checked", self.is_checked)
|
||||||
.on("click", move |root, vdom, _event| {
|
.on("click", move |root, vdom, _event| {
|
||||||
event_bus.publish(msg.clone(), root);
|
let msg = on_toggle(!is_checked);
|
||||||
|
event_bus.publish(msg, root);
|
||||||
|
|
||||||
vdom.schedule_render();
|
vdom.schedule_render();
|
||||||
})
|
})
|
||||||
@ -96,7 +99,7 @@ where
|
|||||||
|
|
||||||
impl<'a, Message> From<Checkbox<Message>> for Element<'a, Message>
|
impl<'a, Message> From<Checkbox<Message>> for Element<'a, Message>
|
||||||
where
|
where
|
||||||
Message: 'static + Clone,
|
Message: 'static,
|
||||||
{
|
{
|
||||||
fn from(checkbox: Checkbox<Message>) -> Element<'a, Message> {
|
fn from(checkbox: Checkbox<Message>) -> Element<'a, Message> {
|
||||||
Element::new(checkbox)
|
Element::new(checkbox)
|
||||||
|
@ -134,7 +134,7 @@ where
|
|||||||
|
|
||||||
impl<'a, Message> From<Scrollable<'a, Message>> for Element<'a, Message>
|
impl<'a, Message> From<Scrollable<'a, Message>> for Element<'a, Message>
|
||||||
where
|
where
|
||||||
Message: 'static + Clone,
|
Message: 'static,
|
||||||
{
|
{
|
||||||
fn from(scrollable: Scrollable<'a, Message>) -> Element<'a, Message> {
|
fn from(scrollable: Scrollable<'a, Message>) -> Element<'a, Message> {
|
||||||
Element::new(scrollable)
|
Element::new(scrollable)
|
||||||
|
@ -82,7 +82,7 @@ impl<'a, Message> Slider<'a, Message> {
|
|||||||
|
|
||||||
impl<'a, Message> Widget<Message> for Slider<'a, Message>
|
impl<'a, Message> Widget<Message> for Slider<'a, Message>
|
||||||
where
|
where
|
||||||
Message: 'static + Clone,
|
Message: 'static,
|
||||||
{
|
{
|
||||||
fn node<'b>(
|
fn node<'b>(
|
||||||
&self,
|
&self,
|
||||||
@ -130,7 +130,7 @@ where
|
|||||||
|
|
||||||
impl<'a, Message> From<Slider<'a, Message>> for Element<'a, Message>
|
impl<'a, Message> From<Slider<'a, Message>> for Element<'a, Message>
|
||||||
where
|
where
|
||||||
Message: 'static + Clone,
|
Message: 'static,
|
||||||
{
|
{
|
||||||
fn from(slider: Slider<'a, Message>) -> Element<'a, Message> {
|
fn from(slider: Slider<'a, Message>) -> Element<'a, Message> {
|
||||||
Element::new(slider)
|
Element::new(slider)
|
||||||
|
Loading…
Reference in New Issue
Block a user