| 1 | //! Compatibility policy facade over the adopted kit composer plan. |
| 2 | use crate::tui::app::ComposerDensity; |
| 3 | use codewhale_ratatui::{NativeComposerDensity, native_composer_height}; |
| 4 | fn density(value: ComposerDensity) -> NativeComposerDensity { |
| 5 | match value { |
| 6 | ComposerDensity::Compact => NativeComposerDensity::Compact, |
| 7 | ComposerDensity::Comfortable => NativeComposerDensity::Comfortable, |
| 8 | ComposerDensity::Spacious => NativeComposerDensity::Spacious, |
| 9 | } |
| 10 | } |
| 11 | pub fn desired_height( |
| 12 | rows: usize, |
| 13 | menus: usize, |
| 14 | available: u16, |
| 15 | value: ComposerDensity, |
| 16 | panel: bool, |
| 17 | ) -> u16 { |
| 18 | native_composer_height(rows, menus, available, density(value), panel) |
| 19 | } |
| 20 | |
| 21 | pub(super) fn paint_waterline( |
| 22 | area: ratatui::layout::Rect, |
| 23 | buf: &mut ratatui::buffer::Buffer, |
| 24 | theme: &codewhale_palette::UiTheme, |
| 25 | ) { |
| 26 | let area = area.intersection(buf.area); |
| 27 | if area.width < 4 || area.height < 2 { |
| 28 | return; |
| 29 | } |
| 30 | use codewhale_ratatui::{ |
| 31 | color::{blend, mix_oklab}, |
| 32 | ocean::ocean_volumetric_caustic, |
| 33 | }; |
| 34 | let body = theme.composer_bg; |
| 35 | let crest = mix_oklab(body, theme.accent_secondary, 0.71); |
| 36 | let trough = mix_oklab(body, theme.accent_primary, 0.32); |
| 37 | let surface = mix_oklab(body, theme.accent_secondary, 0.18); |
| 38 | for x in area.left()..area.right() { |
| 39 | let cell = &mut buf[(x, area.top())]; |
| 40 | if cell.fg == theme.border && matches!(cell.symbol(), "─" | "╭" | "╮") { |
| 41 | let light = ocean_volumetric_caustic( |
| 42 | std::time::Duration::ZERO, |
| 43 | x - area.left(), |
| 44 | 0, |
| 45 | area.width, |
| 46 | 1, |
| 47 | ); |
| 48 | cell.set_fg(blend(crest, trough, light)) |
| 49 | .set_bg(blend(surface, body, light)); |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | #[cfg(test)] |
| 55 | pub use codewhale_ratatui::native_composer_top_padding as top_padding; |
| 56 | |
| 57 | #[cfg(test)] |
| 58 | mod tests { |
| 59 | use super::*; |
| 60 | |
| 61 | #[test] |
| 62 | fn waterline_only_lights_the_native_neutral_outline() { |
| 63 | use codewhale_palette::UNDERWATER_UI_THEME as theme; |
| 64 | use ratatui::{buffer::Buffer, layout::Rect}; |
| 65 | let area = Rect::new(3, 4, 106, 4); |
| 66 | let mut buf = Buffer::empty(area); |
| 67 | for x in area.left()..area.right() { |
| 68 | buf[(x, area.y)] |
| 69 | .set_symbol("─") |
| 70 | .set_fg(theme.border) |
| 71 | .set_bg(theme.composer_bg); |
| 72 | } |
| 73 | buf[(7, 4)].set_fg(theme.warning); |
| 74 | buf[(8, 4)].set_symbol("S"); |
| 75 | buf[(9, 5)].set_symbol("─").set_fg(theme.border); |
| 76 | let before = buf.clone(); |
| 77 | paint_waterline(area, &mut buf, &theme); |
| 78 | for pos in [(7, 4), (8, 4), (9, 5)] { |
| 79 | assert_eq!(buf[pos], before[pos]); |
| 80 | } |
| 81 | assert_ne!(buf[(3, 4)].fg, before[(3, 4)].fg); |
| 82 | assert_ne!(buf[(3, 4)].fg, buf[(21, 4)].fg); |
| 83 | } |
| 84 | |
| 85 | #[test] |
| 86 | fn short_composer_respects_density_and_keeps_padding_below_the_caret() { |
| 87 | for (density, height) in [ |
| 88 | (ComposerDensity::Compact, 2), |
| 89 | (ComposerDensity::Comfortable, 3), |
| 90 | (ComposerDensity::Spacious, 4), |
| 91 | ] { |
| 92 | assert_eq!(desired_height(1, 0, 8, density, false), height); |
| 93 | } |
| 94 | assert_eq!( |
| 95 | top_padding(1, 2), |
| 96 | 0, |
| 97 | "one spare row belongs below the input" |
| 98 | ); |
| 99 | assert_eq!(top_padding(1, 3), 1); |
| 100 | } |
| 101 | |
| 102 | #[test] |
| 103 | fn compact_height_sheds_border_before_content() { |
| 104 | // Only two rows available: keep a border + one content row. |
| 105 | let height = desired_height(1, 0, 2, ComposerDensity::Comfortable, false); |
| 106 | assert_eq!(height, 2); |
| 107 | } |
| 108 | |
| 109 | #[test] |
| 110 | fn content_growth_expands_past_the_density_cap_up_to_available_room() { |
| 111 | // Six content rows + border fits under the Comfortable cap of 9. |
| 112 | let height = desired_height(6, 0, 12, ComposerDensity::Comfortable, false); |
| 113 | assert_eq!(height, 7, "typed content must grow the composer: {height}"); |
| 114 | |
| 115 | let comfortable = desired_height(20, 0, 30, ComposerDensity::Comfortable, false); |
| 116 | assert_eq!(comfortable, 21, "Comfortable grows past its cap of 9"); |
| 117 | let spacious = desired_height(20, 0, 30, ComposerDensity::Spacious, false); |
| 118 | assert_eq!(spacious, 21, "Spacious grows past its cap of 12"); |
| 119 | } |
| 120 | |
| 121 | #[test] |
| 122 | fn spacious_panel_reserves_input_padding_and_both_borders() { |
| 123 | let height = desired_height(1, 0, 12, ComposerDensity::Spacious, true); |
| 124 | assert_eq!(height, 5, "panel = 2 borders + 3 input rows, got {height}"); |
| 125 | } |
| 126 | } |
| 127 |