1
0
Fork 0

Replace Binding with two display properties

This commit is contained in:
Robbert van der Helm 2022-03-20 01:12:10 +01:00
parent d8f8d545dd
commit 4f9a83860b
2 changed files with 105 additions and 114 deletions

8
Cargo.lock generated
View file

@ -3605,7 +3605,7 @@ checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]] [[package]]
name = "vizia" name = "vizia"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/robbert-vdh/vizia.git?branch=feature/baseview-modifiers#dd14c83c4f70b19d00ae397dcc902cadd5464907" source = "git+https://github.com/robbert-vdh/vizia.git?branch=feature/baseview-modifiers#8247518a4d74d741ad27334fc48ba961f43134ff"
dependencies = [ dependencies = [
"vizia_baseview", "vizia_baseview",
"vizia_core", "vizia_core",
@ -3614,7 +3614,7 @@ dependencies = [
[[package]] [[package]]
name = "vizia_baseview" name = "vizia_baseview"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/robbert-vdh/vizia.git?branch=feature/baseview-modifiers#dd14c83c4f70b19d00ae397dcc902cadd5464907" source = "git+https://github.com/robbert-vdh/vizia.git?branch=feature/baseview-modifiers#8247518a4d74d741ad27334fc48ba961f43134ff"
dependencies = [ dependencies = [
"baseview", "baseview",
"femtovg", "femtovg",
@ -3626,7 +3626,7 @@ dependencies = [
[[package]] [[package]]
name = "vizia_core" name = "vizia_core"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/robbert-vdh/vizia.git?branch=feature/baseview-modifiers#dd14c83c4f70b19d00ae397dcc902cadd5464907" source = "git+https://github.com/robbert-vdh/vizia.git?branch=feature/baseview-modifiers#8247518a4d74d741ad27334fc48ba961f43134ff"
dependencies = [ dependencies = [
"bitflags", "bitflags",
"copypasta", "copypasta",
@ -3649,7 +3649,7 @@ dependencies = [
[[package]] [[package]]
name = "vizia_derive" name = "vizia_derive"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/robbert-vdh/vizia.git?branch=feature/baseview-modifiers#dd14c83c4f70b19d00ae397dcc902cadd5464907" source = "git+https://github.com/robbert-vdh/vizia.git?branch=feature/baseview-modifiers#8247518a4d74d741ad27334fc48ba961f43134ff"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",

View file

@ -146,34 +146,30 @@ impl ParamSlider {
&& step_count.is_none() && step_count.is_none()
&& (0.45..=0.55).contains(&default_value); && (0.45..=0.55).contains(&default_value);
Binding::new(
cx,
ParamSliderInternal::text_input_active,
move |cx, text_input_active| {
let param_display_value_lens = let param_display_value_lens =
params.map(move |params| params_to_param(params).to_string()); params.map(move |params| params_to_param(params).to_string());
let param_preview_display_value_lens = |normalized_value| { let param_preview_display_value_lens = |normalized_value| {
params.map(move |params| { params.map(move |params| {
params_to_param(params) params_to_param(params).normalized_value_to_string(normalized_value, true)
.normalized_value_to_string(normalized_value, true)
}) })
}; };
let normalized_param_value_lens = let normalized_param_value_lens =
params.map(move |params| params_to_param(params).normalized_value()); params.map(move |params| params_to_param(params).normalized_value());
// Only draw the text input widget when it gets focussed. Otherwise, overlay the // Only draw the text input widget when it gets focussed. Otherwise, overlay the
// label with the slider. // label with the slider. We need to always include the text input widget in the
if *text_input_active.get(cx) { // tree because using a `Binding` here would cause the text box to get recreated
Textbox::new(cx, param_display_value_lens) // whenever `ParamSliderInternal::text_input_active` changes.
Textbox::new(cx, param_display_value_lens.clone())
.class("value-entry") .class("value-entry")
.on_submit(|cx, string| { .on_submit(|cx, string| cx.emit(ParamSliderEvent::TextInput(string)))
cx.emit(ParamSliderEvent::TextInput(string))
})
.on_focus_out(|cx| cx.emit(ParamSliderEvent::CancelTextInput)) .on_focus_out(|cx| cx.emit(ParamSliderEvent::CancelTextInput))
.display(ParamSliderInternal::text_input_active)
.child_space(Stretch(1.0)) .child_space(Stretch(1.0))
.height(Stretch(1.0)) .height(Stretch(1.0))
.width(Stretch(1.0)); .width(Stretch(1.0));
} else {
// This only gets shown when the text box isn't active
ZStack::new(cx, move |cx| { ZStack::new(cx, move |cx| {
// The filled bar portion. This can be visualized in a couple different // The filled bar portion. This can be visualized in a couple different
// ways depending on the current style property. See // ways depending on the current style property. See
@ -184,9 +180,7 @@ impl ParamSlider {
.bind(normalized_param_value_lens, move |handle, value| { .bind(normalized_param_value_lens, move |handle, value| {
let current_value = *value.get(handle.cx); let current_value = *value.get(handle.cx);
let (start_t, delta) = match style { let (start_t, delta) = match style {
ParamSliderStyle::Centered ParamSliderStyle::Centered if draw_fill_from_default => {
if draw_fill_from_default =>
{
let delta = (default_value - current_value).abs(); let delta = (default_value - current_value).abs();
( (
default_value.min(current_value), default_value.min(current_value),
@ -195,17 +189,16 @@ impl ParamSlider {
if delta >= 1e-3 { delta } else { 0.0 }, if delta >= 1e-3 { delta } else { 0.0 },
) )
} }
ParamSliderStyle::Centered ParamSliderStyle::Centered | ParamSliderStyle::FromLeft => {
| ParamSliderStyle::FromLeft => (0.0, current_value), (0.0, current_value)
}
ParamSliderStyle::CurrentStep ParamSliderStyle::CurrentStep
| ParamSliderStyle::CurrentStepLabeled => { | ParamSliderStyle::CurrentStepLabeled => {
let previous_step = unsafe { let previous_step = unsafe {
param_ptr param_ptr.previous_normalized_step(current_value)
.previous_normalized_step(current_value)
};
let next_step = unsafe {
param_ptr.next_normalized_step(current_value)
}; };
let next_step =
unsafe { param_ptr.next_normalized_step(current_value) };
( (
(previous_step + current_value) / 2.0, (previous_step + current_value) / 2.0,
(next_step + current_value) / 2.0, (next_step + current_value) / 2.0,
@ -223,19 +216,19 @@ impl ParamSlider {
// Either display the current value, or display all values over the // Either display the current value, or display all values over the
// parameter's steps // parameter's steps
// TODO: Do the same thing as in the iced widget where we draw the
// text overlapping the fill area slightly differently. We can
// set the cip region directly in vizia.
match (style, step_count) { match (style, step_count) {
(ParamSliderStyle::CurrentStepLabeled, Some(step_count)) => { (ParamSliderStyle::CurrentStepLabeled, Some(step_count)) => {
HStack::new(cx, |cx| { HStack::new(cx, |cx| {
// There are step_count + 1 possible values for a // There are step_count + 1 possible values for a
// discrete parameter // discrete parameter
for value in 0..step_count + 1 { for value in 0..step_count + 1 {
let normalized_value = let normalized_value = value as f32 / step_count as f32;
value as f32 / step_count as f32;
Label::new( Label::new(
cx, cx,
param_preview_display_value_lens( param_preview_display_value_lens(normalized_value),
normalized_value,
),
) )
.class("value") .class("value")
.class("value--multiple") .class("value--multiple")
@ -258,10 +251,8 @@ impl ParamSlider {
} }
}; };
}) })
.display(ParamSliderInternal::text_input_active.map(|active| !active))
.hoverable(false); .hoverable(false);
}
},
);
}); });
}) })
} }