summaryrefslogtreecommitdiffstats
path: root/virtual/src/widget/button.rs
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2022-02-09 19:42:15 +0700
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2022-02-09 19:42:15 +0700
commit8f0839e786f8d521f7319dd0e188d43284f526b7 (patch)
tree6d902f1f28f02842134c0a07139d7aa9dae5eb80 /virtual/src/widget/button.rs
parentadce9e04213803bd775538efddf6e7908d1c605e (diff)
downloadiced-8f0839e786f8d521f7319dd0e188d43284f526b7.tar.gz
iced-8f0839e786f8d521f7319dd0e188d43284f526b7.tar.bz2
iced-8f0839e786f8d521f7319dd0e188d43284f526b7.zip
Draft `iced_virtual` subcrate
The idea here is to expose a set of "virtual widgets" that can be used with a `Virtual` widget and its `virtual::State`. A virtual widget is a widget that does not contain any state, but instead is a "virtual" representation of the "real" widget. The real widgets are stored in the `virtual::State`. Every time a new virtual widget tree is created during `view`, it is compared to the previous one and "real" widgets are added / removed to the `virtual::State`. Effectively, this removes the need to keep track of local widget state in the application state and allows `view` to take an immutable reference to `self`. To summarize, using this crate should allow users to remove `State` structs in their application state. Eventually, the strategy used here may be adopted generally and, as a result, all of the widgets in `iced_native` would be replaced!
Diffstat (limited to 'virtual/src/widget/button.rs')
-rw-r--r--virtual/src/widget/button.rs64
1 files changed, 64 insertions, 0 deletions
diff --git a/virtual/src/widget/button.rs b/virtual/src/widget/button.rs
new file mode 100644
index 00000000..115be80b
--- /dev/null
+++ b/virtual/src/widget/button.rs
@@ -0,0 +1,64 @@
+use crate::element::{self, Element};
+use crate::Widget;
+
+pub struct Button<Message, Renderer> {
+ content: Element<Message, Renderer>,
+ on_press: Option<Message>,
+}
+
+impl<Message, Renderer> Button<Message, Renderer> {
+ pub fn new(
+ content: impl element::Descriptor<Message, Renderer> + 'static,
+ ) -> Self {
+ Button {
+ content: Element::new(content),
+ on_press: None,
+ }
+ }
+
+ pub fn on_press(mut self, on_press: Message) -> Self {
+ self.on_press = Some(on_press);
+ self
+ }
+}
+
+impl<Message, Renderer> element::Descriptor<Message, Renderer>
+ for Button<Message, Renderer>
+where
+ Message: 'static + Clone,
+ Renderer: 'static,
+{
+ fn tag(&self) -> std::any::TypeId {
+ std::any::TypeId::of::<Self>()
+ }
+
+ fn build(&self) -> Box<dyn Widget<Message, Renderer>> {
+ Box::new(State { is_pressed: false })
+ }
+
+ fn children(&self) -> &[Element<Message, Renderer>] {
+ std::slice::from_ref(&self.content)
+ }
+
+ fn clone(&self) -> Box<dyn element::Descriptor<Message, Renderer>> {
+ Box::new(Clone::clone(self))
+ }
+}
+
+impl<Message, Renderer> Clone for Button<Message, Renderer>
+where
+ Message: Clone,
+{
+ fn clone(&self) -> Self {
+ Self {
+ content: self.content.clone(),
+ on_press: self.on_press.clone(),
+ }
+ }
+}
+
+pub struct State {
+ is_pressed: bool,
+}
+
+impl<Message, Renderer> Widget<Message, Renderer> for State {}