Skip to main content

maolan_widgets/
piano.rs

1use crate::midi::{MIDI_NOTE_COUNT, NOTES_PER_OCTAVE, WHITE_KEY_HEIGHT, WHITE_KEYS_PER_OCTAVE};
2use iced::{
3    Background, Color, Event, Point, Rectangle, Renderer, Size, Theme, gradient, mouse,
4    widget::canvas::{self, Action as CanvasAction, Frame, Geometry, Path, Program},
5};
6use std::collections::{HashMap, HashSet};
7
8pub fn is_black_key(pitch: u8) -> bool {
9    matches!(pitch % 12, 1 | 3 | 6 | 8 | 10)
10}
11
12pub fn note_color(velocity: u8, channel: u8) -> Color {
13    let t = (velocity as f32 / 127.0).clamp(0.0, 1.0);
14    let c = (channel as f32 / 15.0).clamp(0.0, 1.0);
15    Color {
16        r: 0.25 + 0.45 * t,
17        g: 0.35 + 0.4 * (1.0 - c),
18        b: 0.65 + 0.3 * c,
19        a: 0.9,
20    }
21}
22
23pub fn brighten(color: Color, amount: f32) -> Color {
24    Color {
25        r: (color.r + amount).min(1.0),
26        g: (color.g + amount).min(1.0),
27        b: (color.b + amount).min(1.0),
28        a: color.a,
29    }
30}
31
32pub fn darken(color: Color, amount: f32) -> Color {
33    Color {
34        r: (color.r - amount).max(0.0),
35        g: (color.g - amount).max(0.0),
36        b: (color.b - amount).max(0.0),
37        a: color.a,
38    }
39}
40
41pub fn note_two_edge_gradient(base: Color) -> Background {
42    let edge = brighten(base, 0.08);
43    let middle = darken(base, 0.08);
44    Background::Gradient(
45        gradient::Linear::new(0.0)
46            .add_stop(0.0, edge)
47            .add_stop(0.5, middle)
48            .add_stop(1.0, edge)
49            .into(),
50    )
51}
52
53pub fn octave_note_count(octave: u8) -> u8 {
54    let start = usize::from(octave) * NOTES_PER_OCTAVE;
55    if start >= MIDI_NOTE_COUNT {
56        0
57    } else {
58        (MIDI_NOTE_COUNT - start).min(NOTES_PER_OCTAVE) as u8
59    }
60}
61
62fn draw_chromatic_rows(
63    renderer: &Renderer,
64    bounds: Rectangle,
65    pressed_notes: &HashSet<u8>,
66    octave: u8,
67    midnam_note_names: &HashMap<u8, String>,
68    note_count: u8,
69) -> Vec<canvas::Geometry> {
70    let mut frame = Frame::new(renderer, bounds.size());
71    let note_height = bounds.height / f32::from(note_count.max(1));
72
73    for i in 0..note_count {
74        let note_in_octave = note_count - 1 - i;
75        let midi_note = octave * 12 + note_in_octave;
76        let is_pressed = pressed_notes.contains(&note_in_octave);
77        let y_pos = f32::from(i) * note_height;
78
79        let rect = Path::rectangle(
80            Point::new(0.0, y_pos),
81            Size::new(bounds.width, note_height - 1.0),
82        );
83        let is_black = is_black_key(note_in_octave);
84
85        frame.fill(
86            &rect,
87            if is_pressed {
88                Color::from_rgb(0.2, 0.6, 0.9)
89            } else if is_black {
90                Color::from_rgb(0.18, 0.18, 0.2)
91            } else {
92                Color::from_rgb(0.92, 0.92, 0.94)
93            },
94        );
95        frame.stroke(
96            &rect,
97            canvas::Stroke::default()
98                .with_width(1.0)
99                .with_color(Color::from_rgb(0.25, 0.25, 0.28)),
100        );
101
102        if let Some(note_name) = midnam_note_names.get(&midi_note) {
103            use iced::widget::canvas::Text;
104            frame.fill_text(Text {
105                content: note_name.clone(),
106                position: Point::new(4.0, y_pos + note_height * 0.5 - 6.0),
107                color: if is_black { Color::WHITE } else { Color::BLACK },
108                size: 11.0.into(),
109                ..Text::default()
110            });
111        }
112    }
113
114    vec![frame.into_geometry()]
115}
116
117fn draw_partial_octave(
118    renderer: &Renderer,
119    bounds: Rectangle,
120    pressed_notes: &HashSet<u8>,
121    octave: u8,
122    midnam_note_names: &HashMap<u8, String>,
123    note_count: u8,
124) -> Vec<canvas::Geometry> {
125    let mut frame = Frame::new(renderer, bounds.size());
126    let white_note_ids = [0_u8, 2, 4, 5, 7];
127    let black_key_offsets = [1_u8, 2, 4];
128    let black_note_ids = [1_u8, 3, 6];
129    let white_key_height = bounds.height / white_note_ids.len() as f32;
130    let black_key_height = white_key_height * 0.6;
131    let black_key_width = bounds.width * 0.6;
132
133    for (i, note_id) in white_note_ids.iter().enumerate() {
134        let midi_note = octave * 12 + *note_id;
135        let is_pressed = pressed_notes.contains(note_id);
136        let y_pos = bounds.height - ((i + 1) as f32 * white_key_height);
137        let rect = Path::rectangle(
138            Point::new(0.0, y_pos),
139            Size::new(bounds.width, white_key_height - 1.0),
140        );
141        frame.fill(
142            &rect,
143            if is_pressed {
144                Color::from_rgb(0.0, 0.5, 1.0)
145            } else {
146                Color::WHITE
147            },
148        );
149        frame.stroke(&rect, canvas::Stroke::default().with_width(1.0));
150        if let Some(note_name) = midnam_note_names.get(&midi_note) {
151            use iced::widget::canvas::Text;
152            frame.fill_text(Text {
153                content: note_name.clone(),
154                position: Point::new(bounds.width - 25.0, y_pos + white_key_height * 0.5 - 6.0),
155                color: Color::BLACK,
156                size: 10.0.into(),
157                ..Text::default()
158            });
159        }
160    }
161
162    for (idx, offset) in black_key_offsets.iter().enumerate() {
163        let note_id = black_note_ids[idx];
164        if note_id >= note_count {
165            continue;
166        }
167        let is_pressed = pressed_notes.contains(&note_id);
168        let y_pos_black =
169            bounds.height - (f32::from(*offset) * white_key_height) - (black_key_height * 0.5);
170        let rect = Path::rectangle(
171            Point::new(0.0, y_pos_black),
172            Size::new(black_key_width, black_key_height),
173        );
174        frame.fill(
175            &rect,
176            if is_pressed {
177                Color::from_rgb(0.0, 0.4, 0.8)
178            } else {
179                Color::BLACK
180            },
181        );
182    }
183
184    vec![frame.into_geometry()]
185}
186
187pub fn draw_octave(
188    renderer: &Renderer,
189    bounds: Rectangle,
190    pressed_notes: &HashSet<u8>,
191    octave: u8,
192    midnam_note_names: &HashMap<u8, String>,
193) -> Vec<canvas::Geometry> {
194    let mut frame = Frame::new(renderer, bounds.size());
195    let white_key_height = bounds.height / 7.0;
196
197    for i in 0..7 {
198        let note_id = match i {
199            0 => 0,
200            1 => 2,
201            2 => 4,
202            3 => 5,
203            4 => 7,
204            5 => 9,
205            6 => 11,
206            _ => 0,
207        };
208        let midi_note = octave * 12 + note_id;
209        let is_pressed = pressed_notes.contains(&note_id);
210        let y_pos = bounds.height - ((i + 1) as f32 * white_key_height);
211        let rect = Path::rectangle(
212            Point::new(0.0, y_pos),
213            Size::new(bounds.width, white_key_height - 1.0),
214        );
215
216        frame.fill(
217            &rect,
218            if is_pressed {
219                Color::from_rgb(0.0, 0.5, 1.0)
220            } else {
221                Color::WHITE
222            },
223        );
224        frame.stroke(&rect, canvas::Stroke::default().with_width(1.0));
225
226        if let Some(note_name) = midnam_note_names.get(&midi_note) {
227            use iced::widget::canvas::Text;
228            frame.fill_text(Text {
229                content: note_name.clone(),
230                position: Point::new(bounds.width - 25.0, y_pos + white_key_height * 0.5 - 6.0),
231                color: Color::BLACK,
232                size: 10.0.into(),
233                ..Text::default()
234            });
235        }
236    }
237
238    let black_key_offsets = [1, 2, 4, 5, 6];
239    let black_note_ids = [1, 3, 6, 8, 10];
240    let black_key_width = bounds.width * 0.6;
241    let black_key_height = white_key_height * 0.6;
242
243    for (idx, offset) in black_key_offsets.iter().enumerate() {
244        let note_id = black_note_ids[idx];
245        let is_pressed = pressed_notes.contains(&note_id);
246        let y_pos_black =
247            bounds.height - (*offset as f32 * white_key_height) - (black_key_height * 0.5);
248        let rect = Path::rectangle(
249            Point::new(0.0, y_pos_black),
250            Size::new(black_key_width, black_key_height),
251        );
252
253        frame.fill(
254            &rect,
255            if is_pressed {
256                Color::from_rgb(0.0, 0.4, 0.8)
257            } else {
258                Color::BLACK
259            },
260        );
261    }
262
263    vec![frame.into_geometry()]
264}
265
266#[derive(Debug, Clone)]
267pub struct OctaveKeyboard<Message, Press, Release>
268where
269    Press: Fn(u8) -> Message + Clone,
270    Release: Fn(u8) -> Message + Clone,
271{
272    pub octave: u8,
273    pub note_count: u8,
274    pub midnam_note_names: HashMap<u8, String>,
275    /// Cached names count for widget identity
276    on_press: Press,
277    on_release: Release,
278}
279
280impl<Message, Press, Release> OctaveKeyboard<Message, Press, Release>
281where
282    Press: Fn(u8) -> Message + Clone,
283    Release: Fn(u8) -> Message + Clone,
284{
285    pub fn new(
286        octave: u8,
287        midnam_note_names: HashMap<u8, String>,
288        on_press: Press,
289        on_release: Release,
290    ) -> Self {
291        Self {
292            octave,
293            note_count: octave_note_count(octave),
294            midnam_note_names,
295            on_press,
296            on_release,
297        }
298    }
299
300    fn note_class_at(&self, cursor: Point, bounds: Rectangle) -> Option<u8> {
301        if self.note_count == 0 {
302            return None;
303        }
304        if self.note_count < NOTES_PER_OCTAVE as u8 {
305            let white_note_ids = [0_u8, 2, 4, 5, 7];
306            let black_key_offsets = [1_u8, 2, 4];
307            let black_note_ids = [1_u8, 3, 6];
308            let white_key_height = bounds.height / white_note_ids.len() as f32;
309            let black_key_width = bounds.width * 0.6;
310            let black_key_height = white_key_height * 0.6;
311
312            if cursor.x <= black_key_width {
313                for (idx, offset) in black_key_offsets.iter().enumerate() {
314                    let note_id = black_note_ids[idx];
315                    if note_id >= self.note_count {
316                        continue;
317                    }
318                    let y_pos_black = bounds.height
319                        - (f32::from(*offset) * white_key_height)
320                        - (black_key_height * 0.5);
321                    if cursor.y >= y_pos_black && cursor.y <= y_pos_black + black_key_height {
322                        return Some(note_id);
323                    }
324                }
325            }
326
327            for (i, note_id) in white_note_ids.iter().enumerate() {
328                let y_pos = bounds.height - ((i + 1) as f32 * white_key_height);
329                if cursor.y >= y_pos && cursor.y <= y_pos + white_key_height {
330                    return Some(*note_id);
331                }
332            }
333            return None;
334        }
335        let white_key_height = bounds.height / 7.0;
336        let black_key_offsets = [1, 2, 4, 5, 6];
337        let black_note_ids = [1, 3, 6, 8, 10];
338        let black_key_width = bounds.width * 0.6;
339        let black_key_height = white_key_height * 0.6;
340
341        if cursor.x <= black_key_width {
342            for (idx, offset) in black_key_offsets.iter().enumerate() {
343                let y_pos_black =
344                    bounds.height - (*offset as f32 * white_key_height) - (black_key_height * 0.5);
345                if cursor.y >= y_pos_black && cursor.y <= y_pos_black + black_key_height {
346                    return Some(black_note_ids[idx]);
347                }
348            }
349        }
350
351        for i in 0..7 {
352            let note_id = match i {
353                0 => 0,
354                1 => 2,
355                2 => 4,
356                3 => 5,
357                4 => 7,
358                5 => 9,
359                6 => 11,
360                _ => 0,
361            };
362            let y_pos = bounds.height - ((i + 1) as f32 * white_key_height);
363            if cursor.y >= y_pos && cursor.y <= y_pos + white_key_height {
364                return Some(note_id);
365            }
366        }
367        None
368    }
369
370    fn midi_note(&self, note_class: u8) -> u8 {
371        (usize::from(self.octave) * 12 + usize::from(note_class)) as u8
372    }
373}
374
375#[derive(Default, Debug)]
376pub struct OctaveKeyboardState {
377    pub pressed_notes: HashSet<u8>,
378    pub active_note_class: Option<u8>,
379}
380
381impl<Message, Press, Release> Program<Message> for OctaveKeyboard<Message, Press, Release>
382where
383    Message: 'static,
384    Press: Fn(u8) -> Message + Clone,
385    Release: Fn(u8) -> Message + Clone,
386{
387    type State = OctaveKeyboardState;
388
389    fn update(
390        &self,
391        state: &mut Self::State,
392        event: &Event,
393        bounds: Rectangle,
394        cursor: mouse::Cursor,
395    ) -> Option<CanvasAction<Message>> {
396        match event {
397            Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) => {
398                if let Some(position) = cursor.position_in(bounds)
399                    && let Some(note_class) = self.note_class_at(position, bounds)
400                {
401                    state.active_note_class = Some(note_class);
402                    state.pressed_notes.clear();
403                    state.pressed_notes.insert(note_class);
404                    return Some(
405                        CanvasAction::publish((self.on_press.clone())(self.midi_note(note_class)))
406                            .and_capture(),
407                    );
408                }
409            }
410            Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)) => {
411                if let Some(note_class) = state.active_note_class.take() {
412                    state.pressed_notes.clear();
413                    return Some(CanvasAction::publish((self.on_release.clone())(
414                        self.midi_note(note_class),
415                    )));
416                }
417            }
418            _ => {}
419        }
420        None
421    }
422
423    fn draw(
424        &self,
425        state: &Self::State,
426        renderer: &Renderer,
427        _theme: &Theme,
428        bounds: Rectangle,
429        _cursor: mouse::Cursor,
430    ) -> Vec<Geometry> {
431        let is_piano = self.midnam_note_names.is_empty();
432        eprintln!(
433            "[piano] octave={} names_count={} is_piano={} sample_names={:?}",
434            self.octave,
435            self.midnam_note_names.len(),
436            is_piano,
437            self.midnam_note_names.iter().take(3).collect::<Vec<_>>()
438        );
439
440        if is_piano {
441            if self.note_count == NOTES_PER_OCTAVE as u8 {
442                draw_octave(
443                    renderer,
444                    bounds,
445                    &state.pressed_notes,
446                    self.octave,
447                    &self.midnam_note_names,
448                )
449            } else {
450                draw_partial_octave(
451                    renderer,
452                    bounds,
453                    &state.pressed_notes,
454                    self.octave,
455                    &self.midnam_note_names,
456                    self.note_count,
457                )
458            }
459        } else {
460            draw_chromatic_rows(
461                renderer,
462                bounds,
463                &state.pressed_notes,
464                self.octave,
465                &self.midnam_note_names,
466                self.note_count,
467            )
468        }
469    }
470}
471
472pub fn row_height(zoom_y: f32) -> f32 {
473    ((WHITE_KEY_HEIGHT * WHITE_KEYS_PER_OCTAVE as f32 / NOTES_PER_OCTAVE as f32) * zoom_y).max(1.0)
474}
475
476#[cfg(test)]
477mod tests {
478    use super::*;
479    use iced::widget::canvas::Program;
480    use iced::{Point, Rectangle, Size, event, mouse};
481
482    #[derive(Debug, Clone, PartialEq, Eq)]
483    enum TestMessage {
484        Pressed(u8),
485        Released(u8),
486    }
487
488    fn action_message(action: CanvasAction<TestMessage>) -> (Option<TestMessage>, event::Status) {
489        let (message, _redraw, status) = action.into_inner();
490        (message, status)
491    }
492
493    #[test]
494    fn octave_keyboard_update_publishes_pressed_and_released_notes() {
495        let keyboard = OctaveKeyboard::new(
496            4,
497            HashMap::new(),
498            TestMessage::Pressed,
499            TestMessage::Released,
500        );
501        let bounds = Rectangle::new(Point::ORIGIN, Size::new(20.0, 70.0));
502        let press_cursor = mouse::Cursor::Available(Point::new(15.0, 65.0));
503        let release_cursor = mouse::Cursor::Available(Point::new(15.0, 65.0));
504        let mut state = OctaveKeyboardState::default();
505
506        let press = keyboard
507            .update(
508                &mut state,
509                &Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)),
510                bounds,
511                press_cursor,
512            )
513            .expect("press action");
514        let (message, status) = action_message(press);
515        assert_eq!(message, Some(TestMessage::Pressed(48)));
516        assert_eq!(status, event::Status::Captured);
517        assert_eq!(state.active_note_class, Some(0));
518        assert!(state.pressed_notes.contains(&0));
519
520        let release = keyboard
521            .update(
522                &mut state,
523                &Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)),
524                bounds,
525                release_cursor,
526            )
527            .expect("release action");
528        let (message, status) = action_message(release);
529        assert_eq!(message, Some(TestMessage::Released(48)));
530        assert_eq!(status, event::Status::Ignored);
531        assert!(state.pressed_notes.is_empty());
532    }
533
534    #[test]
535    fn partial_octave_keyboard_maps_top_note_to_midi_127() {
536        let keyboard = OctaveKeyboard::new(
537            10,
538            HashMap::new(),
539            TestMessage::Pressed,
540            TestMessage::Released,
541        );
542        let bounds = Rectangle::new(Point::ORIGIN, Size::new(20.0, 80.0));
543        let cursor = mouse::Cursor::Available(Point::new(15.0, 5.0));
544        let mut state = OctaveKeyboardState::default();
545
546        let press = keyboard
547            .update(
548                &mut state,
549                &Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)),
550                bounds,
551                cursor,
552            )
553            .expect("press action");
554        let (message, status) = action_message(press);
555
556        assert_eq!(message, Some(TestMessage::Pressed(127)));
557        assert_eq!(status, event::Status::Captured);
558        assert_eq!(state.active_note_class, Some(7));
559    }
560}