@@ -21,6 +21,53 @@ pub struct CalculatorUI { |
| 21 | 21 | gc: u32, |
| 22 | 22 | } |
| 23 | 23 | |
| 24 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 25 | +pub enum CalcButtonAction { |
| 26 | + InsertText(&'static str), |
| 27 | + Command(&'static str), |
| 28 | + Backspace, |
| 29 | + Delete, |
| 30 | + MoveLeft, |
| 31 | + MoveRight, |
| 32 | + Tab, |
| 33 | + Clear, |
| 34 | + Evaluate, |
| 35 | + ToggleExtended, |
| 36 | +} |
| 37 | + |
| 38 | +#[derive(Debug, Clone, Copy)] |
| 39 | +enum CalcButtonRole { |
| 40 | + Numeric, |
| 41 | + Operator, |
| 42 | + Function, |
| 43 | + Command, |
| 44 | + Control, |
| 45 | + Evaluate, |
| 46 | +} |
| 47 | + |
| 48 | +#[derive(Debug, Clone, Copy)] |
| 49 | +struct CalcButtonSpec { |
| 50 | + label: &'static str, |
| 51 | + action: CalcButtonAction, |
| 52 | + role: CalcButtonRole, |
| 53 | +} |
| 54 | + |
| 55 | +#[derive(Debug, Clone, Copy)] |
| 56 | +struct CalcButtonRender { |
| 57 | + rect: Rect, |
| 58 | + label: &'static str, |
| 59 | + action: CalcButtonAction, |
| 60 | + role: CalcButtonRole, |
| 61 | +} |
| 62 | + |
| 63 | +#[derive(Debug, Clone)] |
| 64 | +struct CalcButtonLayout { |
| 65 | + panel_rect: Rect, |
| 66 | + toggle_rect: Option<Rect>, |
| 67 | + buttons: Vec<CalcButtonRender>, |
| 68 | + hidden_optional_rows: usize, |
| 69 | +} |
| 70 | + |
| 24 | 71 | impl CalculatorUI { |
| 25 | 72 | const HELP_BUTTON_SIZE: u32 = 28; |
| 26 | 73 | |
@@ -71,6 +118,7 @@ impl CalculatorUI { |
| 71 | 118 | graph: &Graph2D, |
| 72 | 119 | graph3d: &Graph3D, |
| 73 | 120 | show_help_modal: bool, |
| 121 | + calc_buttons_extended: bool, |
| 74 | 122 | ) -> Result<()> { |
| 75 | 123 | let size = self.renderer.size(); |
| 76 | 124 | |
@@ -89,14 +137,27 @@ impl CalculatorUI { |
| 89 | 137 | // Mode indicator |
| 90 | 138 | self.draw_mode_indicator(mode)?; |
| 91 | 139 | |
| 140 | + let input_height: i32 = if math_input.is_some() { 88 } else { 50 }; |
| 141 | + let input_y = size.height as i32 - input_height - 10; |
| 142 | + let button_layout = if math_input.is_some() { |
| 143 | + self.calculator_button_layout(input_y, calc_buttons_extended) |
| 144 | + } else { |
| 145 | + None |
| 146 | + }; |
| 147 | + |
| 92 | 148 | // History area |
| 93 | 149 | let history_start_y = 40; |
| 94 | | - let input_height: i32 = if math_input.is_some() { 88 } else { 50 }; |
| 95 | | - let history_end_y = size.height as i32 - input_height - 20; |
| 150 | + let history_end_y = button_layout |
| 151 | + .as_ref() |
| 152 | + .map(|layout| layout.panel_rect.y - 10) |
| 153 | + .unwrap_or(size.height as i32 - input_height - 20); |
| 96 | 154 | self.draw_history(history, history_start_y, history_end_y)?; |
| 97 | 155 | |
| 156 | + if let Some(layout) = button_layout.as_ref() { |
| 157 | + self.draw_calculator_buttons(layout, calc_buttons_extended)?; |
| 158 | + } |
| 159 | + |
| 98 | 160 | // Input area |
| 99 | | - let input_y = size.height as i32 - input_height - 10; |
| 100 | 161 | if let Some(math_input) = math_input { |
| 101 | 162 | self.draw_math_input(math_input, cursor_visible, input_y, input_height as u32)?; |
| 102 | 163 | } else { |
@@ -130,6 +191,30 @@ impl CalculatorUI { |
| 130 | 191 | .contains_point(Point::new(x as i32, y as i32)) |
| 131 | 192 | } |
| 132 | 193 | |
| 194 | + pub fn calculator_button_action_at( |
| 195 | + &self, |
| 196 | + x: f64, |
| 197 | + y: f64, |
| 198 | + calc_buttons_extended: bool, |
| 199 | + ) -> Option<CalcButtonAction> { |
| 200 | + let size = self.renderer.size(); |
| 201 | + let input_y = size.height as i32 - 88 - 10; |
| 202 | + let layout = self.calculator_button_layout(input_y, calc_buttons_extended)?; |
| 203 | + let point = Point::new(x as i32, y as i32); |
| 204 | + |
| 205 | + if let Some(toggle) = layout.toggle_rect { |
| 206 | + if toggle.contains_point(point) { |
| 207 | + return Some(CalcButtonAction::ToggleExtended); |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + layout |
| 212 | + .buttons |
| 213 | + .iter() |
| 214 | + .find(|button| button.rect.contains_point(point)) |
| 215 | + .map(|button| button.action) |
| 216 | + } |
| 217 | + |
| 133 | 218 | fn render_graph_mode( |
| 134 | 219 | &mut self, |
| 135 | 220 | input: &str, |
@@ -335,6 +420,517 @@ impl CalculatorUI { |
| 335 | 420 | Ok(()) |
| 336 | 421 | } |
| 337 | 422 | |
| 423 | + fn calc_core_rows() -> Vec<Vec<CalcButtonSpec>> { |
| 424 | + vec![ |
| 425 | + vec![ |
| 426 | + CalcButtonSpec { |
| 427 | + label: "7", |
| 428 | + action: CalcButtonAction::InsertText("7"), |
| 429 | + role: CalcButtonRole::Numeric, |
| 430 | + }, |
| 431 | + CalcButtonSpec { |
| 432 | + label: "8", |
| 433 | + action: CalcButtonAction::InsertText("8"), |
| 434 | + role: CalcButtonRole::Numeric, |
| 435 | + }, |
| 436 | + CalcButtonSpec { |
| 437 | + label: "9", |
| 438 | + action: CalcButtonAction::InsertText("9"), |
| 439 | + role: CalcButtonRole::Numeric, |
| 440 | + }, |
| 441 | + CalcButtonSpec { |
| 442 | + label: "/", |
| 443 | + action: CalcButtonAction::InsertText("/"), |
| 444 | + role: CalcButtonRole::Operator, |
| 445 | + }, |
| 446 | + CalcButtonSpec { |
| 447 | + label: "(", |
| 448 | + action: CalcButtonAction::InsertText("("), |
| 449 | + role: CalcButtonRole::Operator, |
| 450 | + }, |
| 451 | + CalcButtonSpec { |
| 452 | + label: ")", |
| 453 | + action: CalcButtonAction::InsertText(")"), |
| 454 | + role: CalcButtonRole::Operator, |
| 455 | + }, |
| 456 | + ], |
| 457 | + vec![ |
| 458 | + CalcButtonSpec { |
| 459 | + label: "4", |
| 460 | + action: CalcButtonAction::InsertText("4"), |
| 461 | + role: CalcButtonRole::Numeric, |
| 462 | + }, |
| 463 | + CalcButtonSpec { |
| 464 | + label: "5", |
| 465 | + action: CalcButtonAction::InsertText("5"), |
| 466 | + role: CalcButtonRole::Numeric, |
| 467 | + }, |
| 468 | + CalcButtonSpec { |
| 469 | + label: "6", |
| 470 | + action: CalcButtonAction::InsertText("6"), |
| 471 | + role: CalcButtonRole::Numeric, |
| 472 | + }, |
| 473 | + CalcButtonSpec { |
| 474 | + label: "*", |
| 475 | + action: CalcButtonAction::InsertText("*"), |
| 476 | + role: CalcButtonRole::Operator, |
| 477 | + }, |
| 478 | + CalcButtonSpec { |
| 479 | + label: "x", |
| 480 | + action: CalcButtonAction::InsertText("x"), |
| 481 | + role: CalcButtonRole::Function, |
| 482 | + }, |
| 483 | + CalcButtonSpec { |
| 484 | + label: "y", |
| 485 | + action: CalcButtonAction::InsertText("y"), |
| 486 | + role: CalcButtonRole::Function, |
| 487 | + }, |
| 488 | + ], |
| 489 | + vec![ |
| 490 | + CalcButtonSpec { |
| 491 | + label: "1", |
| 492 | + action: CalcButtonAction::InsertText("1"), |
| 493 | + role: CalcButtonRole::Numeric, |
| 494 | + }, |
| 495 | + CalcButtonSpec { |
| 496 | + label: "2", |
| 497 | + action: CalcButtonAction::InsertText("2"), |
| 498 | + role: CalcButtonRole::Numeric, |
| 499 | + }, |
| 500 | + CalcButtonSpec { |
| 501 | + label: "3", |
| 502 | + action: CalcButtonAction::InsertText("3"), |
| 503 | + role: CalcButtonRole::Numeric, |
| 504 | + }, |
| 505 | + CalcButtonSpec { |
| 506 | + label: "-", |
| 507 | + action: CalcButtonAction::InsertText("-"), |
| 508 | + role: CalcButtonRole::Operator, |
| 509 | + }, |
| 510 | + CalcButtonSpec { |
| 511 | + label: "^", |
| 512 | + action: CalcButtonAction::InsertText("^"), |
| 513 | + role: CalcButtonRole::Operator, |
| 514 | + }, |
| 515 | + CalcButtonSpec { |
| 516 | + label: "!", |
| 517 | + action: CalcButtonAction::InsertText("!"), |
| 518 | + role: CalcButtonRole::Operator, |
| 519 | + }, |
| 520 | + ], |
| 521 | + vec![ |
| 522 | + CalcButtonSpec { |
| 523 | + label: "0", |
| 524 | + action: CalcButtonAction::InsertText("0"), |
| 525 | + role: CalcButtonRole::Numeric, |
| 526 | + }, |
| 527 | + CalcButtonSpec { |
| 528 | + label: ".", |
| 529 | + action: CalcButtonAction::InsertText("."), |
| 530 | + role: CalcButtonRole::Numeric, |
| 531 | + }, |
| 532 | + CalcButtonSpec { |
| 533 | + label: ",", |
| 534 | + action: CalcButtonAction::InsertText(","), |
| 535 | + role: CalcButtonRole::Operator, |
| 536 | + }, |
| 537 | + CalcButtonSpec { |
| 538 | + label: "+", |
| 539 | + action: CalcButtonAction::InsertText("+"), |
| 540 | + role: CalcButtonRole::Operator, |
| 541 | + }, |
| 542 | + CalcButtonSpec { |
| 543 | + label: "π", |
| 544 | + action: CalcButtonAction::InsertText("pi"), |
| 545 | + role: CalcButtonRole::Function, |
| 546 | + }, |
| 547 | + CalcButtonSpec { |
| 548 | + label: "=", |
| 549 | + action: CalcButtonAction::Evaluate, |
| 550 | + role: CalcButtonRole::Evaluate, |
| 551 | + }, |
| 552 | + ], |
| 553 | + ] |
| 554 | + } |
| 555 | + |
| 556 | + fn calc_scientific_rows() -> Vec<Vec<CalcButtonSpec>> { |
| 557 | + vec![ |
| 558 | + vec![ |
| 559 | + CalcButtonSpec { |
| 560 | + label: "sin(", |
| 561 | + action: CalcButtonAction::InsertText("sin("), |
| 562 | + role: CalcButtonRole::Function, |
| 563 | + }, |
| 564 | + CalcButtonSpec { |
| 565 | + label: "cos(", |
| 566 | + action: CalcButtonAction::InsertText("cos("), |
| 567 | + role: CalcButtonRole::Function, |
| 568 | + }, |
| 569 | + CalcButtonSpec { |
| 570 | + label: "tan(", |
| 571 | + action: CalcButtonAction::InsertText("tan("), |
| 572 | + role: CalcButtonRole::Function, |
| 573 | + }, |
| 574 | + CalcButtonSpec { |
| 575 | + label: "ln(", |
| 576 | + action: CalcButtonAction::InsertText("ln("), |
| 577 | + role: CalcButtonRole::Function, |
| 578 | + }, |
| 579 | + CalcButtonSpec { |
| 580 | + label: "log(", |
| 581 | + action: CalcButtonAction::InsertText("log("), |
| 582 | + role: CalcButtonRole::Function, |
| 583 | + }, |
| 584 | + CalcButtonSpec { |
| 585 | + label: "√", |
| 586 | + action: CalcButtonAction::InsertText("sqrt("), |
| 587 | + role: CalcButtonRole::Function, |
| 588 | + }, |
| 589 | + ], |
| 590 | + vec![ |
| 591 | + CalcButtonSpec { |
| 592 | + label: "sin⁻¹", |
| 593 | + action: CalcButtonAction::InsertText("asin("), |
| 594 | + role: CalcButtonRole::Function, |
| 595 | + }, |
| 596 | + CalcButtonSpec { |
| 597 | + label: "cos⁻¹", |
| 598 | + action: CalcButtonAction::InsertText("acos("), |
| 599 | + role: CalcButtonRole::Function, |
| 600 | + }, |
| 601 | + CalcButtonSpec { |
| 602 | + label: "tan⁻¹", |
| 603 | + action: CalcButtonAction::InsertText("atan("), |
| 604 | + role: CalcButtonRole::Function, |
| 605 | + }, |
| 606 | + CalcButtonSpec { |
| 607 | + label: "eˣ", |
| 608 | + action: CalcButtonAction::InsertText("exp("), |
| 609 | + role: CalcButtonRole::Function, |
| 610 | + }, |
| 611 | + CalcButtonSpec { |
| 612 | + label: "abs(", |
| 613 | + action: CalcButtonAction::InsertText("abs("), |
| 614 | + role: CalcButtonRole::Function, |
| 615 | + }, |
| 616 | + CalcButtonSpec { |
| 617 | + label: "gamma(", |
| 618 | + action: CalcButtonAction::InsertText("gamma("), |
| 619 | + role: CalcButtonRole::Function, |
| 620 | + }, |
| 621 | + ], |
| 622 | + ] |
| 623 | + } |
| 624 | + |
| 625 | + fn calc_extended_rows() -> Vec<Vec<CalcButtonSpec>> { |
| 626 | + vec![ |
| 627 | + vec![ |
| 628 | + CalcButtonSpec { |
| 629 | + label: "a⁄b", |
| 630 | + action: CalcButtonAction::Command("frac"), |
| 631 | + role: CalcButtonRole::Command, |
| 632 | + }, |
| 633 | + CalcButtonSpec { |
| 634 | + label: "∑", |
| 635 | + action: CalcButtonAction::Command("sum"), |
| 636 | + role: CalcButtonRole::Command, |
| 637 | + }, |
| 638 | + CalcButtonSpec { |
| 639 | + label: "∏", |
| 640 | + action: CalcButtonAction::Command("prod"), |
| 641 | + role: CalcButtonRole::Command, |
| 642 | + }, |
| 643 | + CalcButtonSpec { |
| 644 | + label: "∫", |
| 645 | + action: CalcButtonAction::Command("int"), |
| 646 | + role: CalcButtonRole::Command, |
| 647 | + }, |
| 648 | + CalcButtonSpec { |
| 649 | + label: "∫ᵇₐ", |
| 650 | + action: CalcButtonAction::Command("dint"), |
| 651 | + role: CalcButtonRole::Command, |
| 652 | + }, |
| 653 | + CalcButtonSpec { |
| 654 | + label: "d/dx", |
| 655 | + action: CalcButtonAction::Command("diff"), |
| 656 | + role: CalcButtonRole::Command, |
| 657 | + }, |
| 658 | + ], |
| 659 | + vec![ |
| 660 | + CalcButtonSpec { |
| 661 | + label: "limₓ→a", |
| 662 | + action: CalcButtonAction::Command("lim"), |
| 663 | + role: CalcButtonRole::Command, |
| 664 | + }, |
| 665 | + CalcButtonSpec { |
| 666 | + label: "x=?", |
| 667 | + action: CalcButtonAction::Command("solve"), |
| 668 | + role: CalcButtonRole::Command, |
| 669 | + }, |
| 670 | + CalcButtonSpec { |
| 671 | + label: "ⁿ√", |
| 672 | + action: CalcButtonAction::Command("nthroot"), |
| 673 | + role: CalcButtonRole::Command, |
| 674 | + }, |
| 675 | + CalcButtonSpec { |
| 676 | + label: "▦", |
| 677 | + action: CalcButtonAction::Command("matrix"), |
| 678 | + role: CalcButtonRole::Command, |
| 679 | + }, |
| 680 | + CalcButtonSpec { |
| 681 | + label: "↓min", |
| 682 | + action: CalcButtonAction::InsertText("min("), |
| 683 | + role: CalcButtonRole::Function, |
| 684 | + }, |
| 685 | + CalcButtonSpec { |
| 686 | + label: "↑max", |
| 687 | + action: CalcButtonAction::InsertText("max("), |
| 688 | + role: CalcButtonRole::Function, |
| 689 | + }, |
| 690 | + ], |
| 691 | + ] |
| 692 | + } |
| 693 | + |
| 694 | + fn calc_control_row() -> Vec<CalcButtonSpec> { |
| 695 | + vec![ |
| 696 | + CalcButtonSpec { |
| 697 | + label: "Bksp", |
| 698 | + action: CalcButtonAction::Backspace, |
| 699 | + role: CalcButtonRole::Control, |
| 700 | + }, |
| 701 | + CalcButtonSpec { |
| 702 | + label: "Del", |
| 703 | + action: CalcButtonAction::Delete, |
| 704 | + role: CalcButtonRole::Control, |
| 705 | + }, |
| 706 | + CalcButtonSpec { |
| 707 | + label: "Clr", |
| 708 | + action: CalcButtonAction::Clear, |
| 709 | + role: CalcButtonRole::Control, |
| 710 | + }, |
| 711 | + CalcButtonSpec { |
| 712 | + label: "<", |
| 713 | + action: CalcButtonAction::MoveLeft, |
| 714 | + role: CalcButtonRole::Control, |
| 715 | + }, |
| 716 | + CalcButtonSpec { |
| 717 | + label: ">", |
| 718 | + action: CalcButtonAction::MoveRight, |
| 719 | + role: CalcButtonRole::Control, |
| 720 | + }, |
| 721 | + CalcButtonSpec { |
| 722 | + label: "Tab", |
| 723 | + action: CalcButtonAction::Tab, |
| 724 | + role: CalcButtonRole::Control, |
| 725 | + }, |
| 726 | + ] |
| 727 | + } |
| 728 | + |
| 729 | + fn calculator_button_layout(&self, input_y: i32, extended: bool) -> Option<CalcButtonLayout> { |
| 730 | + let size = self.renderer.size(); |
| 731 | + let min_history_height = 90i32; |
| 732 | + let top_reserved = 46i32; |
| 733 | + let max_panel_height = input_y - top_reserved - min_history_height - 8; |
| 734 | + if max_panel_height < 132 { |
| 735 | + return None; |
| 736 | + } |
| 737 | + |
| 738 | + let cols = 6i32; |
| 739 | + let gap = 8i32; |
| 740 | + let panel_side_padding = 12i32; |
| 741 | + let panel_margin = 12i32; |
| 742 | + let panel_max_width = size.width as i32 - panel_margin * 2; |
| 743 | + if panel_max_width < 320 { |
| 744 | + return None; |
| 745 | + } |
| 746 | + |
| 747 | + let mut button_w = (panel_max_width - panel_side_padding * 2 - (cols - 1) * gap) / cols; |
| 748 | + button_w = button_w.clamp(44, 92); |
| 749 | + let button_h = ((button_w as f64) * 0.58).round() as i32; |
| 750 | + let button_h = button_h.clamp(30, 42); |
| 751 | + let inner_width = cols * button_w + (cols - 1) * gap; |
| 752 | + let panel_width = inner_width + panel_side_padding * 2; |
| 753 | + let panel_x = (size.width as i32 - panel_width) / 2; |
| 754 | + |
| 755 | + let mut rows = Vec::new(); |
| 756 | + rows.push(Self::calc_control_row()); |
| 757 | + rows.extend(Self::calc_core_rows()); |
| 758 | + |
| 759 | + let mut optional_rows = Self::calc_scientific_rows(); |
| 760 | + if extended { |
| 761 | + optional_rows.extend(Self::calc_extended_rows()); |
| 762 | + } |
| 763 | + |
| 764 | + let toolbar_h = 34i32; |
| 765 | + let row_gap = gap; |
| 766 | + let panel_vertical_padding = 12i32; |
| 767 | + let max_rows_fit = ((max_panel_height - toolbar_h - panel_vertical_padding * 2 + row_gap) |
| 768 | + / (button_h + row_gap)) |
| 769 | + .max(0) as usize; |
| 770 | + let required_rows = rows.len(); |
| 771 | + if max_rows_fit < required_rows { |
| 772 | + return None; |
| 773 | + } |
| 774 | + |
| 775 | + let extra_fit = max_rows_fit - required_rows; |
| 776 | + let optional_visible = optional_rows.len().min(extra_fit); |
| 777 | + let hidden_optional_rows = optional_rows.len().saturating_sub(optional_visible); |
| 778 | + rows.extend(optional_rows.into_iter().take(optional_visible)); |
| 779 | + |
| 780 | + let row_count = rows.len() as i32; |
| 781 | + let panel_height = panel_vertical_padding * 2 |
| 782 | + + toolbar_h |
| 783 | + + row_count * button_h |
| 784 | + + (row_count - 1).max(0) * row_gap; |
| 785 | + let panel_bottom = input_y - 10; |
| 786 | + let panel_y = panel_bottom - panel_height; |
| 787 | + |
| 788 | + if panel_y < top_reserved + min_history_height { |
| 789 | + return None; |
| 790 | + } |
| 791 | + |
| 792 | + let panel_rect = Rect::new(panel_x, panel_y, panel_width as u32, panel_height as u32); |
| 793 | + let toggle_rect = if panel_width >= 340 { |
| 794 | + Some(Rect::new( |
| 795 | + panel_x + (panel_width - 190) / 2, |
| 796 | + panel_y + 7, |
| 797 | + 190, |
| 798 | + 22, |
| 799 | + )) |
| 800 | + } else { |
| 801 | + None |
| 802 | + }; |
| 803 | + |
| 804 | + let mut buttons = Vec::new(); |
| 805 | + let grid_top = panel_y + panel_vertical_padding + toolbar_h; |
| 806 | + for (row_idx, row) in rows.iter().enumerate() { |
| 807 | + let cols_this_row = row.len() as i32; |
| 808 | + if cols_this_row == 0 { |
| 809 | + continue; |
| 810 | + } |
| 811 | + let row_width = cols_this_row * button_w + (cols_this_row - 1) * gap; |
| 812 | + let row_x = panel_x + (panel_width - row_width) / 2; |
| 813 | + let y = grid_top + row_idx as i32 * (button_h + row_gap); |
| 814 | + |
| 815 | + for (col_idx, spec) in row.iter().enumerate() { |
| 816 | + let x = row_x + col_idx as i32 * (button_w + gap); |
| 817 | + buttons.push(CalcButtonRender { |
| 818 | + rect: Rect::new(x, y, button_w as u32, button_h as u32), |
| 819 | + label: spec.label, |
| 820 | + action: spec.action, |
| 821 | + role: spec.role, |
| 822 | + }); |
| 823 | + } |
| 824 | + } |
| 825 | + |
| 826 | + Some(CalcButtonLayout { |
| 827 | + panel_rect, |
| 828 | + toggle_rect, |
| 829 | + buttons, |
| 830 | + hidden_optional_rows, |
| 831 | + }) |
| 832 | + } |
| 833 | + |
| 834 | + fn draw_calculator_buttons( |
| 835 | + &mut self, |
| 836 | + layout: &CalcButtonLayout, |
| 837 | + calc_buttons_extended: bool, |
| 838 | + ) -> Result<()> { |
| 839 | + self.renderer.fill_rounded_rect( |
| 840 | + layout.panel_rect, |
| 841 | + 10.0, |
| 842 | + self.theme.background.lighten(0.03).with_alpha(0.96), |
| 843 | + )?; |
| 844 | + self.renderer.stroke_rounded_rect( |
| 845 | + layout.panel_rect, |
| 846 | + 10.0, |
| 847 | + self.theme.border.with_alpha(0.75), |
| 848 | + 1.0, |
| 849 | + )?; |
| 850 | + |
| 851 | + if let Some(toggle_rect) = layout.toggle_rect { |
| 852 | + let toggle_bg = if calc_buttons_extended { |
| 853 | + self.theme.selection_background.with_alpha(0.95) |
| 854 | + } else { |
| 855 | + self.theme.item_hover_background.with_alpha(0.95) |
| 856 | + }; |
| 857 | + self.renderer |
| 858 | + .fill_rounded_rect(toggle_rect, 7.0, toggle_bg)?; |
| 859 | + self.renderer.stroke_rounded_rect( |
| 860 | + toggle_rect, |
| 861 | + 7.0, |
| 862 | + self.theme.border.with_alpha(0.85), |
| 863 | + 1.0, |
| 864 | + )?; |
| 865 | + |
| 866 | + let toggle_label = if calc_buttons_extended { |
| 867 | + "Mode: Extended" |
| 868 | + } else { |
| 869 | + "Mode: Scientific" |
| 870 | + }; |
| 871 | + let toggle_style = TextStyle::new() |
| 872 | + .font_family(&self.theme.font_family) |
| 873 | + .font_size(11.0) |
| 874 | + .color(self.theme.selection_foreground); |
| 875 | + let text_size = self.renderer.measure_text(toggle_label, &toggle_style)?; |
| 876 | + self.renderer.text( |
| 877 | + toggle_label, |
| 878 | + toggle_rect.x as f64 + (toggle_rect.width as f64 - text_size.width as f64) * 0.5, |
| 879 | + toggle_rect.y as f64 + (toggle_rect.height as f64 - text_size.height as f64) * 0.5, |
| 880 | + &toggle_style, |
| 881 | + )?; |
| 882 | + } |
| 883 | + |
| 884 | + if layout.hidden_optional_rows > 0 { |
| 885 | + let hint = format!( |
| 886 | + "{} function row(s) hidden by size", |
| 887 | + layout.hidden_optional_rows |
| 888 | + ); |
| 889 | + let hint_style = TextStyle::new() |
| 890 | + .font_family(&self.theme.font_family) |
| 891 | + .font_size(9.5) |
| 892 | + .color(self.theme.foreground.with_alpha(0.62)); |
| 893 | + self.renderer.text( |
| 894 | + &hint, |
| 895 | + (layout.panel_rect.x + 10) as f64, |
| 896 | + (layout.panel_rect.y + 10) as f64, |
| 897 | + &hint_style, |
| 898 | + )?; |
| 899 | + } |
| 900 | + |
| 901 | + for button in &layout.buttons { |
| 902 | + let bg = match button.role { |
| 903 | + CalcButtonRole::Numeric => self.theme.input_background.lighten(0.08), |
| 904 | + CalcButtonRole::Operator => self.theme.selection_background.with_alpha(0.45), |
| 905 | + CalcButtonRole::Function => self.theme.item_hover_background.with_alpha(0.88), |
| 906 | + CalcButtonRole::Command => Color::rgb(0.22, 0.34, 0.52).with_alpha(0.92), |
| 907 | + CalcButtonRole::Control => self.theme.background.lighten(0.09), |
| 908 | + CalcButtonRole::Evaluate => self.theme.selection_background.with_alpha(0.92), |
| 909 | + }; |
| 910 | + self.renderer.fill_rounded_rect(button.rect, 7.0, bg)?; |
| 911 | + self.renderer.stroke_rounded_rect( |
| 912 | + button.rect, |
| 913 | + 7.0, |
| 914 | + self.theme.border.with_alpha(0.7), |
| 915 | + 1.0, |
| 916 | + )?; |
| 917 | + |
| 918 | + let label_style = TextStyle::new() |
| 919 | + .font_family(&self.theme.font_family) |
| 920 | + .font_size(11.0) |
| 921 | + .color(self.theme.foreground.with_alpha(0.96)); |
| 922 | + let text_size = self.renderer.measure_text(button.label, &label_style)?; |
| 923 | + self.renderer.text( |
| 924 | + button.label, |
| 925 | + button.rect.x as f64 + (button.rect.width as f64 - text_size.width as f64) * 0.5, |
| 926 | + button.rect.y as f64 + (button.rect.height as f64 - text_size.height as f64) * 0.5, |
| 927 | + &label_style, |
| 928 | + )?; |
| 929 | + } |
| 930 | + |
| 931 | + Ok(()) |
| 932 | + } |
| 933 | + |
| 338 | 934 | fn help_button_rect(&self) -> Rect { |
| 339 | 935 | let size = self.renderer.size(); |
| 340 | 936 | let margin = 10i32; |
@@ -670,6 +1266,7 @@ impl CalculatorUI { |
| 670 | 1266 | math_renderer.fg_color = self.theme.foreground.with_alpha(0.85); |
| 671 | 1267 | math_renderer.slot_bg_color = self.theme.input_background.lighten(0.15); |
| 672 | 1268 | math_renderer.slot_focus_color = self.theme.selection_background; |
| 1269 | + math_renderer.cursor_color = self.theme.input_cursor; |
| 673 | 1270 | math_renderer.render(&prompt, padding as f64, baseline); |
| 674 | 1271 | math_renderer.render( |
| 675 | 1272 | mathbox, |
@@ -714,6 +1311,7 @@ impl CalculatorUI { |
| 714 | 1311 | math_renderer.fg_color = self.theme.selection_foreground; |
| 715 | 1312 | math_renderer.slot_bg_color = self.theme.input_background.lighten(0.15); |
| 716 | 1313 | math_renderer.slot_focus_color = self.theme.selection_background; |
| 1314 | + math_renderer.cursor_color = self.theme.input_cursor; |
| 717 | 1315 | math_renderer.render(&equals, equals_x, baseline); |
| 718 | 1316 | math_renderer.render( |
| 719 | 1317 | mathbox, |
@@ -865,6 +1463,7 @@ impl CalculatorUI { |
| 865 | 1463 | math_renderer.fg_color = self.theme.foreground; |
| 866 | 1464 | math_renderer.slot_bg_color = self.theme.input_background.lighten(0.15); |
| 867 | 1465 | math_renderer.slot_focus_color = self.theme.selection_background; |
| 1466 | + math_renderer.cursor_color = self.theme.input_cursor; |
| 868 | 1467 | math_renderer.render_with_cursor( |
| 869 | 1468 | math_input.mathbox(), |
| 870 | 1469 | (padding + 10) as f64, |