Skip to content

Commit f4d9e73

Browse files
authored
Merge pull request #74 from Leif-Rydenfalk/main
Synth example fix
2 parents 4bfe622 + 2dcfd6a commit f4d9e73

File tree

4 files changed

+532
-373
lines changed

4 files changed

+532
-373
lines changed

examples/calculator/src/calculator.rs

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ use rui::*;
99
/// Represents the state of the clear button functionality.
1010
/// Used to toggle between Clear (C) and All Clear (AC) modes.
1111
#[derive(PartialEq, Clone, Copy)]
12-
enum ClearState {
13-
/// Initial state - displays "C"
14-
Initial,
15-
/// State after clearing - displays "AC"
16-
JustCleared,
12+
enum ClearMode {
13+
/// Initial state - displays "C", only clears current input
14+
OnlyInput,
15+
/// State after clearing - displays "AC", resets all calculator state
16+
FullReset,
1717
}
1818

1919
/// Represents the basic arithmetic operations supported by the calculator.
@@ -362,7 +362,7 @@ impl Calculator {
362362
},
363363
Button::Special(action) => match action {
364364
SpecialOperator::Clear => {
365-
if cx[s].clear_state == ClearState::JustCleared {
365+
if cx[s].clear_mode == ClearMode::FullReset {
366366
"AC".to_string()
367367
} else {
368368
"C".to_string()
@@ -404,7 +404,7 @@ pub struct CalculatorState {
404404
/// The last operator used (for repeat calculations)
405405
last_operator: Option<Operator>,
406406
/// Current state of the clear button (C/AC)
407-
clear_state: ClearState,
407+
clear_mode: ClearMode,
408408
}
409409

410410
impl CalculatorState {
@@ -417,7 +417,7 @@ impl CalculatorState {
417417
has_error: false,
418418
is_result_displayed: false,
419419
last_operator: None,
420-
clear_state: ClearState::Initial,
420+
clear_mode: ClearMode::OnlyInput,
421421
}
422422
}
423423

@@ -435,13 +435,7 @@ impl CalculatorState {
435435
Some(Operator::Add) => first_operand + second_operand,
436436
Some(Operator::Subtract) => first_operand - second_operand,
437437
Some(Operator::Multiply) => first_operand * second_operand,
438-
Some(Operator::Divide) => {
439-
if second_operand == 0.0 {
440-
self.has_error = true;
441-
return; // Early return on division by zero
442-
}
443-
first_operand / second_operand
444-
}
438+
Some(Operator::Divide) => first_operand / second_operand,
445439
None => return, // Handle the case where no operator is set
446440
};
447441

@@ -511,7 +505,7 @@ impl CalculatorState {
511505
self.is_input_new = true;
512506
self.has_error = false;
513507
self.is_result_displayed = false;
514-
self.clear_state = ClearState::Initial;
508+
self.clear_mode = ClearMode::OnlyInput;
515509
}
516510

517511
/// Processes button presses and updates calculator state accordingly.
@@ -523,10 +517,10 @@ impl CalculatorState {
523517
| Button::Special(SpecialOperator::Percentage)
524518
| Button::Special(SpecialOperator::Equals)
525519
| Button::Special(SpecialOperator::Decimal) => {
526-
if self.clear_state == ClearState::JustCleared {
520+
if self.clear_mode == ClearMode::FullReset {
527521
self.reset();
528522
} else {
529-
self.clear_state = ClearState::Initial;
523+
self.clear_mode = ClearMode::OnlyInput;
530524
}
531525

532526
match button {
@@ -558,16 +552,20 @@ impl CalculatorState {
558552
}
559553
}
560554
Button::Special(SpecialOperator::Clear) => {
561-
if self.second_operand == "0"
562-
&& self.first_operand == "0"
563-
&& self.current_operator.is_none()
555+
if self.second_operand != "0"
556+
|| self.current_operator.is_some()
557+
|| self.is_result_displayed
564558
{
565-
self.reset();
566-
self.clear_state = ClearState::JustCleared;
567-
} else {
559+
// If there's any ongoing state, first clear the current input
568560
self.second_operand = "0".to_string();
561+
self.current_operator = None;
569562
self.is_input_new = true;
570-
self.clear_state = ClearState::Initial;
563+
self.is_result_displayed = false;
564+
self.clear_mode = ClearMode::FullReset;
565+
} else {
566+
// If already cleared, perform a full reset
567+
self.reset();
568+
self.clear_mode = ClearMode::OnlyInput;
571569
}
572570
}
573571
}

examples/custom_modifier_with_state.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ impl Default for MyState {
1616
trait MyMod: View + Sized {
1717
fn my_modifier(self) -> impl View {
1818
state(MyState::default, move |s, cx| {
19-
self.clone().offset(LocalOffset::new(0.0, cx[s].offset))
19+
self.clone()
20+
.offset(LocalOffset::new(0.0, cx[s].offset))
2021
.anim(move |cx, _| {
2122
cx[s].offset *= 0.9;
2223
})
@@ -33,4 +34,4 @@ fn my_text(name: &str) -> impl View {
3334

3435
fn main() {
3536
hstack((my_text("without"), my_text("with").my_modifier())).run()
36-
}
37+
}

examples/synth/src/main.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::sync::{Arc, Mutex};
55
mod midi_keyboard;
66
mod synth;
77

8-
use midi_keyboard::{MidiFrequency, MidiNote, MidiNoteId};
8+
use midi_keyboard::{MidiFrequency, MidiKeyboard, MidiNoteId};
99
use synth::{Oscillator, Synth};
1010

1111
use rui::*;
@@ -21,18 +21,32 @@ fn main() {
2121
});
2222

2323
// Create and configure the MIDI keyboard
24-
midi_keyboard::MidiKeyboard::new()
24+
MidiKeyboard::new()
2525
.num_keys(25)
26-
.on_note_begin(move |note: MidiNote| {
26+
.start_octave(4)
27+
.max_simultaneous_keys(5)
28+
.on_note_begin(move |event| {
29+
let note = event.note;
30+
2731
let mut synth = synth.lock().unwrap();
28-
let frequency: MidiFrequency = note.try_into().unwrap();
32+
33+
// Get the frequency of the note.
34+
let frequency: MidiFrequency = note.frequency();
35+
36+
// Create an audio source for the note.
2937
let audio_source = Oscillator::sine_wave(frequency).amplify(1.0);
30-
let source_id: MidiNoteId = note.try_into().unwrap();
38+
39+
// Get the note id (u8) if you need it. 0 is the lowest note. 127 is the highest note.
40+
let source_id: MidiNoteId = note.id();
41+
42+
// Send the audio source to the synth.
3143
synth.play_source(Box::new(audio_source), source_id);
3244
})
33-
.on_note_end(move |note: MidiNote| {
45+
.on_note_end(move |event| {
46+
let note = event.note;
47+
3448
let mut synth = synth_clone.lock().unwrap();
35-
let source_id: MidiNoteId = note.try_into().unwrap();
49+
let source_id: MidiNoteId = note.id();
3650
synth.release_source(source_id);
3751
})
3852
.show()

0 commit comments

Comments
 (0)