tenseleyflow/rcal / 4d1c7ff

Browse files

Refine create modal field styling

Authored by espadonne
SHA
4d1c7ff3368583f10ed4deccda4105d9889f8714
Parents
a5ebd87
Tree
a0ed618

1 changed file

StatusFile+-
M src/tui.rs 77 20
src/tui.rsmodified
@@ -12,7 +12,7 @@ use crate::{
1212
     agenda::{
1313
         AgendaSource, DayAgenda, DayMinute, EmptyAgendaSource, Event, EventTiming, TimedAgendaEvent,
1414
     },
15
-    app::{AppState, CreateEventForm, ViewMode},
15
+    app::{AppState, CreateEventForm, CreateEventFormRowKind, ViewMode},
1616
     calendar::{
1717
         CalendarCell, CalendarDate, CalendarMonth, CalendarWeek, DAYS_PER_WEEK, MONTH_GRID_WEEKS,
1818
     },
@@ -478,6 +478,8 @@ struct CreateModalStyles {
478478
     title: Style,
479479
     label: Style,
480480
     value: Style,
481
+    checkbox: Style,
482
+    checkbox_mark: Style,
481483
     error: Style,
482484
     footer: Style,
483485
 }
@@ -491,8 +493,10 @@ impl CreateModalStyles {
491493
                 .fg(Color::Cyan)
492494
                 .bg(Color::Black)
493495
                 .add_modifier(Modifier::BOLD),
494
-            label: Style::new().fg(Color::Gray).bg(Color::Black),
495
-            value: Style::new().fg(Color::White).bg(Color::Black),
496
+            label: Style::new().fg(Color::White).bg(Color::Black),
497
+            value: Style::new().fg(Color::Gray).bg(Color::Black),
498
+            checkbox: Style::new().fg(Color::Yellow).bg(Color::Black),
499
+            checkbox_mark: Style::new().fg(Color::White).bg(Color::Black),
496500
             error: Style::new()
497501
                 .fg(Color::Red)
498502
                 .bg(Color::Black)
@@ -835,7 +839,14 @@ fn render_create_event_modal(
835839
         let value_x = label_x.saturating_add(label_width).saturating_add(1);
836840
         if value_x < content.right() {
837841
             let value_width = content.right() - value_x;
838
-            write_padded_left(buf, y, value_x, value_width, &row.value, styles.value);
842
+            match row.kind {
843
+                CreateEventFormRowKind::Text => {
844
+                    write_padded_left(buf, y, value_x, value_width, &row.value, styles.value);
845
+                }
846
+                CreateEventFormRowKind::Toggle => {
847
+                    write_toggle_value(buf, y, value_x, value_width, &row.value, styles);
848
+                }
849
+            }
839850
         }
840851
     }
841852
 
@@ -1609,6 +1620,29 @@ fn write_padded_left(buf: &mut Buffer, y: u16, x: u16, width: u16, text: &str, s
16091620
     buf.set_stringn(x, y, text, usize::from(width), style);
16101621
 }
16111622
 
1623
+fn write_toggle_value(
1624
+    buf: &mut Buffer,
1625
+    y: u16,
1626
+    x: u16,
1627
+    width: u16,
1628
+    value: &str,
1629
+    styles: CreateModalStyles,
1630
+) {
1631
+    write_padded_left(buf, y, x, width, value, styles.value);
1632
+    if width == 0 || !value.starts_with('[') {
1633
+        return;
1634
+    }
1635
+
1636
+    set_cell(buf, x, y, "[", styles.checkbox);
1637
+    if width > 1 {
1638
+        let mark = value.get(1..2).unwrap_or(" ");
1639
+        set_cell(buf, x.saturating_add(1), y, mark, styles.checkbox_mark);
1640
+    }
1641
+    if width > 2 && value.get(2..3) == Some("]") {
1642
+        set_cell(buf, x.saturating_add(2), y, "]", styles.checkbox);
1643
+    }
1644
+}
1645
+
16121646
 const fn inset_rect(area: Rect) -> Rect {
16131647
     Rect::new(
16141648
         area.x.saturating_add(1),
@@ -1652,6 +1686,7 @@ fn distribute<const N: usize>(total: u16) -> [u16; N] {
16521686
 #[cfg(test)]
16531687
 mod tests {
16541688
     use super::*;
1689
+    use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
16551690
     use ratatui::style::Modifier;
16561691
     use time::{Month, Time};
16571692
 
@@ -1675,6 +1710,10 @@ mod tests {
16751710
         CalendarDate::from_ymd(year, month, day).expect("valid test date")
16761711
     }
16771712
 
1713
+    fn key(code: KeyCode) -> KeyEvent {
1714
+        KeyEvent::new(code, KeyModifiers::empty())
1715
+    }
1716
+
16781717
     fn render_test_buffer(month: &CalendarMonth, width: u16, height: u16) -> Buffer {
16791718
         let area = Rect::new(0, 0, width, height);
16801719
         let mut buffer = Buffer::empty(area);
@@ -1728,16 +1767,24 @@ mod tests {
17281767
         }
17291768
     }
17301769
 
1770
+    fn assert_styled_cell(buffer: &Buffer, x: u16, y: u16, symbol: &str, fg: Color) {
1771
+        let cell = buffer.cell((x, y)).expect("text cell exists");
1772
+        assert_eq!(cell.symbol(), symbol);
1773
+        assert_eq!(cell.fg, fg);
1774
+        assert_eq!(cell.bg, Color::Black);
1775
+        assert!(!cell.modifier.contains(Modifier::DIM));
1776
+        assert!(!cell.modifier.contains(Modifier::BOLD));
1777
+    }
1778
+
17311779
     fn assert_styled_text(buffer: &Buffer, x: u16, y: u16, text: &str, fg: Color) {
17321780
         for (offset, character) in text.chars().enumerate() {
1733
-            let cell = buffer
1734
-                .cell((x + u16::try_from(offset).expect("offset fits"), y))
1735
-                .expect("text cell exists");
1736
-            assert_eq!(cell.symbol(), character.to_string());
1737
-            assert_eq!(cell.fg, fg);
1738
-            assert_eq!(cell.bg, Color::Black);
1739
-            assert!(!cell.modifier.contains(Modifier::DIM));
1740
-            assert!(!cell.modifier.contains(Modifier::BOLD));
1781
+            assert_styled_cell(
1782
+                buffer,
1783
+                x + u16::try_from(offset).expect("offset fits"),
1784
+                y,
1785
+                &character.to_string(),
1786
+                fg,
1787
+            );
17411788
         }
17421789
     }
17431790
 
@@ -1955,10 +2002,13 @@ mod tests {
19552002
     }
19562003
 
19572004
     #[test]
1958
-    fn create_modal_uses_gray_labels_and_white_values() {
2005
+    fn create_modal_uses_bright_labels_gray_values_and_checkbox_styles() {
19592006
         let selected = date(2026, Month::April, 23);
19602007
         let mut app = AppState::new(selected);
19612008
         app.apply(AppAction::OpenCreate);
2009
+        let _ = app.handle_create_key(key(KeyCode::Char('A')));
2010
+        let _ = app.handle_create_key(key(KeyCode::Tab));
2011
+        let _ = app.handle_create_key(key(KeyCode::Enter));
19622012
 
19632013
         let area = Rect::new(0, 0, 84, 26);
19642014
         let buffer = render_app_buffer(&app, area.width, area.height);
@@ -1969,13 +2019,20 @@ mod tests {
19692019
         let label_width = 12.min(content.width.saturating_sub(1));
19702020
         let value_x = label_x.saturating_add(label_width).saturating_add(1);
19712021
 
1972
-        assert_styled_text(&buffer, content.x, row_y, ">", Color::Gray);
1973
-        assert_styled_text(&buffer, label_x, row_y, "Title", Color::Gray);
1974
-        assert_styled_text(&buffer, label_x, row_y + 2, "Start date", Color::Gray);
1975
-        assert_styled_text(&buffer, value_x, row_y + 1, "[ ]", Color::White);
1976
-        assert_styled_text(&buffer, value_x, row_y + 2, "2026-04-23", Color::White);
1977
-        assert_styled_text(&buffer, value_x, row_y + 3, "09:00", Color::White);
1978
-        assert_styled_text(&buffer, value_x, row_y + 8, "[ ] 5m", Color::White);
2022
+        assert_styled_text(&buffer, content.x, row_y + 1, ">", Color::White);
2023
+        assert_styled_text(&buffer, label_x, row_y, "Title", Color::White);
2024
+        assert_styled_text(&buffer, label_x, row_y + 1, "All day", Color::White);
2025
+        assert_styled_text(&buffer, label_x, row_y + 2, "Start date", Color::White);
2026
+        assert_styled_text(&buffer, value_x, row_y, "A", Color::Gray);
2027
+        assert_styled_text(&buffer, value_x, row_y + 2, "2026-04-23", Color::Gray);
2028
+        assert_styled_text(&buffer, value_x, row_y + 3, "09:00", Color::Gray);
2029
+        assert_styled_cell(&buffer, value_x, row_y + 1, "[", Color::Yellow);
2030
+        assert_styled_cell(&buffer, value_x + 1, row_y + 1, "x", Color::White);
2031
+        assert_styled_cell(&buffer, value_x + 2, row_y + 1, "]", Color::Yellow);
2032
+        assert_styled_cell(&buffer, value_x, row_y + 8, "[", Color::Yellow);
2033
+        assert_styled_cell(&buffer, value_x + 1, row_y + 8, " ", Color::White);
2034
+        assert_styled_cell(&buffer, value_x + 2, row_y + 8, "]", Color::Yellow);
2035
+        assert_styled_text(&buffer, value_x + 4, row_y + 8, "5m", Color::Gray);
19792036
     }
19802037
 
19812038
     #[test]