1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use crate::model::LayerVelocityMode;
use wasm_bindgen::JsCast;
use web_sys::{HtmlInputElement, InputEvent, MouseEvent};
use yew::{html, Callback, Component, Context, Html, Properties};
use yew_utils::components::drop_down::DropDown;
#[derive(Properties, PartialEq)]
pub struct TuningFormProps {
#[prop_or_default]
pub pitch_preference_init: f32,
#[prop_or_default]
pub layer_velocity_mode_init: LayerVelocityMode,
#[prop_or_default]
pub program_name_init: String,
#[prop_or_default]
pub on_pitch_preference_change: Callback<f32>,
#[prop_or_default]
pub on_layer_velocity_mode_change: Callback<LayerVelocityMode>,
#[prop_or_default]
pub on_program_name_change: Callback<String>,
#[prop_or_default]
pub on_save: Callback<Tuning>,
}
pub enum TuningFormMessages {
PitchPreferenceChange(f32),
LayerVelocityModeChange(LayerVelocityMode),
ProgramNameChanged(String),
Save,
}
#[derive(Default)]
pub struct TuningForm {
pub tuning: Tuning,
}
#[derive(Default, Clone)]
pub struct Tuning {
pub pitch_preference: f32,
pub layer_velocity_mode: LayerVelocityMode,
pub program_name: String,
}
impl Component for TuningForm {
type Message = TuningFormMessages;
type Properties = TuningFormProps;
fn create(ctx: &Context<Self>) -> Self {
Self {
tuning: Tuning {
pitch_preference: ctx.props().pitch_preference_init,
layer_velocity_mode: ctx.props().layer_velocity_mode_init,
program_name: ctx.props().program_name_init.clone(),
},
}
}
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
match msg {
TuningFormMessages::PitchPreferenceChange(pitch_preference) => {
self.tuning.pitch_preference = pitch_preference;
ctx.props()
.on_pitch_preference_change
.emit(pitch_preference);
false
}
TuningFormMessages::LayerVelocityModeChange(mode) => {
self.tuning.layer_velocity_mode = mode;
ctx.props().on_layer_velocity_mode_change.emit(mode);
true
}
TuningFormMessages::ProgramNameChanged(name) => {
self.tuning.program_name = name;
ctx.props()
.on_program_name_change
.emit(self.tuning.program_name.clone());
false
}
TuningFormMessages::Save => {
ctx.props().on_save.emit(self.tuning.clone());
false
}
}
}
fn view(&self, ctx: &Context<Self>) -> Html {
let layer_help_text = match self.tuning.layer_velocity_mode {
LayerVelocityMode::Spread => {
"Each layer will only be used for a range of the velocity."
}
LayerVelocityMode::Overlapping => "All the layers will play at the same time.",
};
html! {
<div class="box">
<div class="block">
<div class="field">
<label class="label">{"Pitch Preference"}</label>
<div class="control">
<input
id="pitch_preference"
type="range"
min=0
max=1
step=0.01
value={self.tuning.pitch_preference.to_string()}
oninput={TuningForm::on_pitch_preference_change(ctx)}
/>
</div>
</div>
<div class="field">
<label class="label">{"Layer Velocity Mode"}</label>
<div class="control">
<div class="select">
<DropDown<LayerVelocityMode>
initial={ctx.props().layer_velocity_mode_init}
options={vec![LayerVelocityMode::Overlapping, LayerVelocityMode::Spread]}
selection_changed={ctx.link().callback(TuningFormMessages::LayerVelocityModeChange)}
/>
</div>
</div>
<p class="help">{layer_help_text}</p>
</div>
</div>
<div class="block">
<div class="field has-addons">
<div class="control">
<input
class="input"
type="text"
placeholder="Program Name"
oninput={TuningForm::on_program_name_change(ctx)}
/>
</div>
<div class="control">
<button class="button is-link" onclick={ctx.link().callback(|_: MouseEvent| TuningFormMessages::Save)}>
{"Save"}
</button>
</div>
</div>
</div>
</div>
}
}
}
impl TuningForm {
fn on_pitch_preference_change(ctx: &Context<TuningForm>) -> Callback<InputEvent> {
ctx.link().batch_callback(|e: InputEvent| {
let input: HtmlInputElement = e
.target()
.and_then(|t| t.dyn_into::<HtmlInputElement>().ok())?;
Some(TuningFormMessages::PitchPreferenceChange(
input.value_as_number() as f32,
))
})
}
fn on_program_name_change(ctx: &Context<TuningForm>) -> Callback<InputEvent> {
ctx.link().batch_callback(|e: InputEvent| {
let input: HtmlInputElement = e
.target()
.and_then(|t| t.dyn_into::<HtmlInputElement>().ok())?;
Some(TuningFormMessages::ProgramNameChanged(input.value()))
})
}
}