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(¬e_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(¬e_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(¬e_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(¬e_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 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
433 if is_piano {
434 if self.note_count == NOTES_PER_OCTAVE as u8 {
435 draw_octave(
436 renderer,
437 bounds,
438 &state.pressed_notes,
439 self.octave,
440 &self.midnam_note_names,
441 )
442 } else {
443 draw_partial_octave(
444 renderer,
445 bounds,
446 &state.pressed_notes,
447 self.octave,
448 &self.midnam_note_names,
449 self.note_count,
450 )
451 }
452 } else {
453 draw_chromatic_rows(
454 renderer,
455 bounds,
456 &state.pressed_notes,
457 self.octave,
458 &self.midnam_note_names,
459 self.note_count,
460 )
461 }
462 }
463}
464
465pub fn row_height(zoom_y: f32) -> f32 {
466 ((WHITE_KEY_HEIGHT * WHITE_KEYS_PER_OCTAVE as f32 / NOTES_PER_OCTAVE as f32) * zoom_y).max(1.0)
467}
468
469#[cfg(test)]
470mod tests {
471 use super::*;
472 use iced::widget::canvas::Program;
473 use iced::{Point, Rectangle, Size, event, mouse};
474
475 #[derive(Debug, Clone, PartialEq, Eq)]
476 enum TestMessage {
477 Pressed(u8),
478 Released(u8),
479 }
480
481 fn action_message(action: CanvasAction<TestMessage>) -> (Option<TestMessage>, event::Status) {
482 let (message, _redraw, status) = action.into_inner();
483 (message, status)
484 }
485
486 #[test]
487 fn octave_keyboard_update_publishes_pressed_and_released_notes() {
488 let keyboard = OctaveKeyboard::new(
489 4,
490 HashMap::new(),
491 TestMessage::Pressed,
492 TestMessage::Released,
493 );
494 let bounds = Rectangle::new(Point::ORIGIN, Size::new(20.0, 70.0));
495 let press_cursor = mouse::Cursor::Available(Point::new(15.0, 65.0));
496 let release_cursor = mouse::Cursor::Available(Point::new(15.0, 65.0));
497 let mut state = OctaveKeyboardState::default();
498
499 let press = keyboard
500 .update(
501 &mut state,
502 &Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)),
503 bounds,
504 press_cursor,
505 )
506 .expect("press action");
507 let (message, status) = action_message(press);
508 assert_eq!(message, Some(TestMessage::Pressed(48)));
509 assert_eq!(status, event::Status::Captured);
510 assert_eq!(state.active_note_class, Some(0));
511 assert!(state.pressed_notes.contains(&0));
512
513 let release = keyboard
514 .update(
515 &mut state,
516 &Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)),
517 bounds,
518 release_cursor,
519 )
520 .expect("release action");
521 let (message, status) = action_message(release);
522 assert_eq!(message, Some(TestMessage::Released(48)));
523 assert_eq!(status, event::Status::Ignored);
524 assert!(state.pressed_notes.is_empty());
525 }
526
527 #[test]
528 fn partial_octave_keyboard_maps_top_note_to_midi_127() {
529 let keyboard = OctaveKeyboard::new(
530 10,
531 HashMap::new(),
532 TestMessage::Pressed,
533 TestMessage::Released,
534 );
535 let bounds = Rectangle::new(Point::ORIGIN, Size::new(20.0, 80.0));
536 let cursor = mouse::Cursor::Available(Point::new(15.0, 5.0));
537 let mut state = OctaveKeyboardState::default();
538
539 let press = keyboard
540 .update(
541 &mut state,
542 &Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)),
543 bounds,
544 cursor,
545 )
546 .expect("press action");
547 let (message, status) = action_message(press);
548
549 assert_eq!(message, Some(TestMessage::Pressed(127)));
550 assert_eq!(status, event::Status::Captured);
551 assert_eq!(state.active_note_class, Some(7));
552 }
553}