Skip to main content

hjkl_form/
host.rs

1//! Minimal `Host` impl for per-field editors.
2
3use hjkl_engine::{CursorShape, Host, Viewport};
4use std::time::Instant;
5
6/// Host adapter mounted on every text field's `Editor`. Mirrors
7/// [`hjkl_engine::DefaultHost`] but lives in this crate so consumers
8/// can reach it without the `default` feature gating.
9pub struct FormFieldHost {
10    last_cursor_shape: CursorShape,
11    started: Instant,
12    clipboard: Option<String>,
13    viewport: Viewport,
14}
15
16impl FormFieldHost {
17    /// Construct a field host with a one-row default viewport. The
18    /// renderer overwrites `width` / `height` per frame from the field's
19    /// body rect.
20    pub fn new() -> Self {
21        Self {
22            last_cursor_shape: CursorShape::Block,
23            started: Instant::now(),
24            clipboard: None,
25            viewport: Viewport {
26                top_row: 0,
27                top_col: 0,
28                width: 40,
29                height: 1,
30                ..Viewport::default()
31            },
32        }
33    }
34
35    /// Most recent cursor shape requested by the engine.
36    pub fn cursor_shape(&self) -> CursorShape {
37        self.last_cursor_shape
38    }
39}
40
41impl Default for FormFieldHost {
42    fn default() -> Self {
43        Self::new()
44    }
45}
46
47impl Host for FormFieldHost {
48    type Intent = ();
49
50    fn write_clipboard(&mut self, text: String) {
51        self.clipboard = Some(text);
52    }
53
54    fn read_clipboard(&mut self) -> Option<String> {
55        self.clipboard.clone()
56    }
57
58    fn now(&self) -> std::time::Duration {
59        self.started.elapsed()
60    }
61
62    fn prompt_search(&mut self) -> Option<String> {
63        None
64    }
65
66    fn emit_cursor_shape(&mut self, shape: CursorShape) {
67        self.last_cursor_shape = shape;
68    }
69
70    fn emit_intent(&mut self, _intent: Self::Intent) {}
71
72    fn viewport(&self) -> &Viewport {
73        &self.viewport
74    }
75
76    fn viewport_mut(&mut self) -> &mut Viewport {
77        &mut self.viewport
78    }
79}