From 07176022022ac45ef4b51791588bf314509f40b9 Mon Sep 17 00:00:00 2001 From: George Atkinson Date: Sun, 20 Mar 2022 14:39:40 +0000 Subject: [PATCH] Rename on_create to on_build. Run fmt --- core/src/handle.rs | 4 +-- core/src/views/textbox.rs | 2 +- core/src/views/value_slider.rs | 64 ++++++++++++++-------------------- examples/value_slider.rs | 3 +- 4 files changed, 30 insertions(+), 43 deletions(-) diff --git a/core/src/handle.rs b/core/src/handle.rs index e79df9018..316415418 100644 --- a/core/src/handle.rs +++ b/core/src/handle.rs @@ -45,7 +45,7 @@ impl<'a, T> Handle<'a, T> { self } - pub fn on_create(self, callback: F) -> Self + pub fn on_build(self, callback: F) -> Self where F: Fn(&mut Context), { @@ -387,6 +387,4 @@ impl<'a, T> Handle<'a, T> { set_style!(border_radius_top_right, Units); set_style!(border_radius_bottom_left, Units); set_style!(border_radius_bottom_right, Units); - - } diff --git a/core/src/views/textbox.rs b/core/src/views/textbox.rs index 1bed012a6..94dede1fb 100644 --- a/core/src/views/textbox.rs +++ b/core/src/views/textbox.rs @@ -532,7 +532,7 @@ impl Model for TextboxData { TextEvent::SetOnSubmit(on_submit) => { self.on_submit = on_submit.clone(); } - + TextEvent::SetOnEditEnd(on_edit_end) => { self.on_edit_end = on_edit_end.clone(); } diff --git a/core/src/views/value_slider.rs b/core/src/views/value_slider.rs index e9ad6bed7..9ad0128c9 100644 --- a/core/src/views/value_slider.rs +++ b/core/src/views/value_slider.rs @@ -1,8 +1,8 @@ use std::marker::PhantomData; use crate::{ - Binding, Context, Element, Handle, Lens, LensExt, Model, MouseButton, - Units::*, View, WindowEvent, ZStack, Color, Label, Modifiers, Event, Textbox, Actions + Binding, Color, Context, Element, Event, Handle, Label, Lens, LensExt, Model, Modifiers, + MouseButton, Textbox, Units::*, View, WindowEvent, ZStack, }; use super::textbox::TextEvent; @@ -25,7 +25,7 @@ impl Model for VSDataInternal { self.edit = *flag; } - _=> {} + _ => {} } } } @@ -37,46 +37,40 @@ pub struct ValueSlider { on_changing: Option>, } -impl ValueSlider +impl ValueSlider where L: Lens, { pub fn new(cx: &mut Context, lens: L) -> Handle { - Self { - p: PhantomData::default(), - is_dragging: false, - on_changing: None, - }.build2(cx, |cx|{ + Self { p: PhantomData::default(), is_dragging: false, on_changing: None }.build2(cx, |cx| { + VSDataInternal { edit: false }.build(cx); - VSDataInternal { - edit: false, - }.build(cx); - - ZStack::new(cx, |cx|{ + ZStack::new(cx, |cx| { Element::new(cx) .height(Stretch(1.0)) .width(Stretch(1.0)) .background_color(Color::rgb(200, 200, 200)) - .bind(lens.clone(), move |handle, l|{ + .bind(lens.clone(), move |handle, l| { let val = *l.get(handle.cx); handle.width(Percentage(val * 100.0)); }); - Binding::new(cx, VSDataInternal::edit, move |cx, edit|{ + Binding::new(cx, VSDataInternal::edit, move |cx, edit| { if *edit.get(cx) { Textbox::new(cx, lens.clone().map(|val| format!("{:.2}", val))) - .on_create(|cx|{ + .on_build(|cx| { cx.emit(TextEvent::StartEdit); cx.emit(TextEvent::SelectAll); }) - .on_submit(|cx, txt|{ + .on_submit(|cx, txt| { if let Ok(val) = txt.parse::() { let val = val.clamp(0.0, 1.0); cx.emit(ValueSliderEvent::OnSubmit(val)); } }) - .on_edit_end(|cx|{ + .on_edit_end(|cx| { cx.emit(ValueSliderEvent::SetEdit(false)); }) + .background_color(Color::transparent()) .child_space(Stretch(1.0)) .height(Stretch(1.0)) .width(Stretch(1.0)); @@ -93,29 +87,26 @@ where } } -impl View for ValueSlider -where +impl View for ValueSlider +where L: Lens, { fn event(&mut self, cx: &mut Context, event: &mut Event) { - if let Some(value_slider_event) = event.message.downcast() { match value_slider_event { ValueSliderEvent::OnSubmit(val) => { - if let Some(callback) = &self.on_changing { (callback)(cx, *val); } } - _=> {} + _ => {} } } if let Some(window_event) = event.message.downcast() { match window_event { WindowEvent::MouseDown(button) if *button == MouseButton::Left => { - if cx.modifiers.contains(Modifiers::ALT) { cx.emit(ValueSliderEvent::SetEdit(true)); } else { @@ -123,16 +114,16 @@ where if !vs_data.edit { self.is_dragging = true; cx.capture(); - + let mut dx = (cx.mouse.left.pos_down.0 - cx.cache.get_posx(cx.current)) / cx.cache.get_width(cx.current); - + dx = dx.clamp(0.0, 1.0); - + if let Some(callback) = self.on_changing.take() { (callback)(cx, dx); - + self.on_changing = Some(callback); } } @@ -147,9 +138,8 @@ where WindowEvent::MouseMove(x, _) => { if self.is_dragging { - - let mut dx = (*x - cx.cache.get_posx(cx.current)) - / cx.cache.get_width(cx.current); + let mut dx = + (*x - cx.cache.get_posx(cx.current)) / cx.cache.get_width(cx.current); dx = dx.clamp(0.0, 1.0); @@ -159,17 +149,17 @@ where } } - _=> {} + _ => {} } } } } -impl<'a, L> Handle<'a, ValueSlider> -where +impl<'a, L> Handle<'a, ValueSlider> +where L: Lens, { - pub fn on_changing(self, callback: F) -> Self + pub fn on_changing(self, callback: F) -> Self where F: 'static + Fn(&mut Context, f32), { @@ -181,4 +171,4 @@ where self } -} \ No newline at end of file +} diff --git a/examples/value_slider.rs b/examples/value_slider.rs index 87bbbb3ff..875c98f59 100644 --- a/examples/value_slider.rs +++ b/examples/value_slider.rs @@ -22,11 +22,10 @@ impl Model for AppData { } fn main() { - let mut window_description = WindowDescription::new(); + let window_description = WindowDescription::new().with_title("Value Slider"); Application::new(window_description, |cx| { AppData { val: 0.5 }.build(cx); - ValueSlider::new(cx, AppData::val) .on_changing(|cx, val| cx.emit(AppEvent::SetValue(val))) .width(Pixels(300.0))