1use std::cell::RefCell;
6
7use waft_embedder::{
8 Matrix4, Rect, SelectionRect, Size, SystemBars, SystemBarsBehavior, SystemMouseCursorKind,
9 SystemUiOverlayStyle, TextEditingValue, TextInputConfiguration, TextInputHost,
10 ViewFocusDirection, ViewFocusState, ViewHost,
11};
12
13#[derive(Default)]
18pub struct TestViewHost {
19 cursors: RefCell<Vec<(i64, SystemMouseCursorKind)>>,
20 focus_requests: RefCell<Vec<(ViewFocusState, ViewFocusDirection)>>,
21 text_input: RecordingTextInput,
22 framework_handles_back: RefCell<Vec<bool>>,
23 overlay_styles: RefCell<Vec<SystemUiOverlayStyle>>,
24 bars_shown: RefCell<Vec<BarsShown>>,
25}
26
27impl TestViewHost {
28 pub fn cursors(&self) -> Vec<(i64, SystemMouseCursorKind)> {
30 self.cursors.borrow().clone()
31 }
32
33 pub fn focus_requests(&self) -> Vec<(ViewFocusState, ViewFocusDirection)> {
35 self.focus_requests.borrow().clone()
36 }
37
38 pub fn text_input_calls(&self) -> Vec<TextInputCall> {
40 self.text_input.calls.borrow().clone()
41 }
42
43 pub fn framework_handles_back(&self) -> Vec<bool> {
46 self.framework_handles_back.borrow().clone()
47 }
48
49 pub fn overlay_styles(&self) -> Vec<SystemUiOverlayStyle> {
52 self.overlay_styles.borrow().clone()
53 }
54
55 pub fn bars_shown(&self) -> Vec<BarsShown> {
58 self.bars_shown.borrow().clone()
59 }
60}
61
62#[derive(Clone, Copy, Debug, PartialEq, Eq)]
64pub enum BarsShown {
65 Hidden(SystemBarsBehavior),
66 Shown,
67}
68
69impl ViewHost for TestViewHost {
70 fn text_input(&self) -> Option<&dyn TextInputHost> {
71 Some(&self.text_input)
72 }
73
74 fn activate_system_cursor(&self, device: i64, kind: SystemMouseCursorKind) {
75 self.cursors.borrow_mut().push((device, kind));
76 }
77
78 fn request_focus_change(&self, state: ViewFocusState, direction: ViewFocusDirection) {
79 self.focus_requests.borrow_mut().push((state, direction));
80 }
81
82 fn set_framework_handles_back(&self, handles: bool) {
83 self.framework_handles_back.borrow_mut().push(handles);
84 }
85
86 fn system_bars(&self) -> Option<&dyn SystemBars> {
87 Some(self)
88 }
89}
90
91impl SystemBars for TestViewHost {
92 fn set_overlay_style(&self, style: &SystemUiOverlayStyle) {
93 self.overlay_styles.borrow_mut().push(*style);
94 }
95
96 fn hide(&self, behavior: SystemBarsBehavior) {
97 self.bars_shown
98 .borrow_mut()
99 .push(BarsShown::Hidden(behavior));
100 }
101
102 fn show(&self) {
103 self.bars_shown.borrow_mut().push(BarsShown::Shown);
104 }
105}
106
107#[derive(Clone, Debug, PartialEq)]
109pub enum TextInputCall {
110 SetClient(TextInputConfiguration),
111 Show,
112 UpdateConfig(TextInputConfiguration),
113 ClearClient,
114 Hide,
115 SetEditingState(TextEditingValue),
116 SetComposingRect(Rect),
117 SetCaretRect(Rect),
118 SetClientGeometry(Size, Matrix4),
119 SetSelectionRects(Vec<SelectionRect>),
120}
121
122#[derive(Default)]
124struct RecordingTextInput {
125 calls: RefCell<Vec<TextInputCall>>,
126}
127
128impl RecordingTextInput {
129 fn record(&self, call: TextInputCall) {
130 self.calls.borrow_mut().push(call);
131 }
132}
133
134impl TextInputHost for RecordingTextInput {
135 fn set_client(&self, configuration: &TextInputConfiguration) {
136 self.record(TextInputCall::SetClient(configuration.clone()));
137 }
138
139 fn show(&self) {
140 self.record(TextInputCall::Show);
141 }
142
143 fn update_config(&self, configuration: &TextInputConfiguration) {
144 self.record(TextInputCall::UpdateConfig(configuration.clone()));
145 }
146
147 fn clear_client(&self) {
148 self.record(TextInputCall::ClearClient);
149 }
150
151 fn hide(&self) {
152 self.record(TextInputCall::Hide);
153 }
154
155 fn set_editing_state(&self, value: &TextEditingValue) {
156 self.record(TextInputCall::SetEditingState(value.clone()));
157 }
158
159 fn set_composing_rect(&self, rect: Rect) {
160 self.record(TextInputCall::SetComposingRect(rect));
161 }
162
163 fn set_caret_rect(&self, rect: Rect) {
164 self.record(TextInputCall::SetCaretRect(rect));
165 }
166
167 fn set_client_geometry(&self, size: Size, transform: &Matrix4) {
168 self.record(TextInputCall::SetClientGeometry(size, *transform));
169 }
170
171 fn set_selection_rects(&self, selection_rects: &[SelectionRect]) {
172 self.record(TextInputCall::SetSelectionRects(selection_rects.to_vec()));
173 }
174}