waft_embedder/client.rs
1//! Flutter counterpart: `dart:ui` `hooks.dart`.
2//!
3//! Dart's hooks are isolate-global functions the engine calls. Here they are
4//! methods on the embedder client so a host crate need not name `App`.
5
6use std::time::Duration;
7
8use crate::{
9 AppExitReply, AppExitResponse, AppLifecycleState, ClipboardEvent, DragReport, EditCommand,
10 Frame, KeyData, MenuId, NotificationActivation, OpenRequest, PointerReport,
11 ServiceExtensionCall, ServiceExtensionError, TextEditingValue, TextInputAction, TrayIconEvent,
12 TrayIconId, ViewFocusEvent, ViewId,
13};
14
15/// Complete frames and typed view lifecycle notifications.
16///
17/// The platform updates its view registry before sending a lifecycle method,
18/// so the client can immediately query the affected view.
19pub trait EmbedderClient {
20 /// A screen refreshed at `frame.elapsed`: `views` are the views on it, in sight, that asked
21 /// for a frame ([`Platform::request_frame`](crate::Platform::request_frame)) before the
22 /// refresh began; one that asks during it is in the next.
23 ///
24 /// The host never calls it from inside another call into the client.
25 fn frame(&mut self, views: &[ViewId], frame: Frame);
26
27 /// A view arrived after launch: opened by the app, or given by the system. The views that
28 /// exist at launch are in `Platform::views` when the app starts, and none of them is
29 /// reported here.
30 fn view_added(&mut self, id: ViewId);
31
32 /// Flutter's window metrics event: something of `id`'s metrics changed, the screen it is on
33 /// included.
34 fn view_metrics_changed(&mut self, id: ViewId);
35
36 /// A view is gone; the app keeps running.
37 fn view_removed(&mut self, id: ViewId);
38
39 /// Flutter `FlutterEngineNotifyDisplayUpdate`: a screen was added, removed or changed, and
40 /// `Platform::screens` already answers the new list. A view that is on another screen now
41 /// is reported through `view_metrics_changed`. The default drops it.
42 fn screens_changed(&mut self) {}
43
44 /// Flutter `PlatformDispatcher.onViewFocusChange`.
45 fn view_focus_changed(&mut self, event: ViewFocusEvent) {
46 let _ = event;
47 }
48
49 /// What pointing devices did, as the system reported it, in the order it happened: the
50 /// embedder API's `FlutterEngineSendPointerEvent`. The client adds what the framework also
51 /// needs ([`PointerDataPacketConverter`](crate::PointerDataPacketConverter)) and hands the
52 /// result to `PlatformDispatcher.onPointerDataPacket`.
53 fn pointer_reports(&mut self, reports: Vec<PointerReport>);
54
55 /// What is dragged from outside the application entered, moved over, was dropped on or
56 /// left a view: the dropped report is the one its items are read in. No dart:ui hook; the
57 /// default drops it.
58 fn drag_report(&mut self, report: DragReport) {
59 let _ = report;
60 }
61
62 /// The user copied, cut or pasted in `view` through the platform's own command: the
63 /// browser's, or a desktop's Edit menu, which a key reaches only once the framework has left
64 /// it unhandled. No dart:ui hook; the default drops it.
65 fn clipboard_event(&mut self, view: ViewId, event: ClipboardEvent) {
66 let _ = (view, event);
67 }
68
69 /// The user chose an edit command for `view` from the system's own Edit menu, which a key
70 /// reaches only once the framework has left it unhandled: undo, redo, select all or delete.
71 /// No dart:ui hook; the default drops it.
72 fn edit_command(&mut self, view: ViewId, command: EditCommand) {
73 let _ = (view, command);
74 }
75
76 /// The user chose the item `id` of the app's menu bar (`MenuBar::set_menus`). Flutter's
77 /// `Menu.selectedCallback` on the `flutter/menu` channel. The default drops it.
78 fn menu_selected(&mut self, id: MenuId) {
79 let _ = id;
80 }
81
82 /// The user chose the item `id` of the Dock icon's menu (`DockMenu::set_menu`). No dart:ui
83 /// hook; the default drops it.
84 fn dock_menu_selected(&mut self, id: MenuId) {
85 let _ = id;
86 }
87
88 /// Something happened to the tray icon `icon`, as its system reports it. No dart:ui hook; the
89 /// default drops it.
90 fn tray_icon_event(&mut self, icon: TrayIconId, event: TrayIconEvent) {
91 let _ = (icon, event);
92 }
93
94 /// The system asks the app to open files and links: from the file manager, another app or
95 /// the command line, at launch or while it runs. A launch's request comes once the client
96 /// exists. No dart:ui hook: Flutter's embedders hand these to their plugins. The default drops
97 /// it.
98 fn open_requested(&mut self, request: OpenRequest) {
99 let _ = request;
100 }
101
102 /// The user clicked a notification the app posted, or one of its actions, or replied: while
103 /// the app runs, or to start it, when the activation comes once the client exists. No dart:ui
104 /// hook: Flutter's apps hear these through plugins. The default drops it.
105 fn notification_activated(&mut self, activation: NotificationActivation) {
106 let _ = activation;
107 }
108
109 /// Flutter `PlatformDispatcher.onKeyData`.
110 ///
111 /// Returns whether the framework handled the event; a host that shares the
112 /// keyboard with other native components should not propagate a handled one.
113 fn key_data(&mut self, data: KeyData) -> bool;
114
115 /// The user asked to go back in `view`: Android's back button or back gesture, in the
116 /// activity whose window the view is, and whatever a later host maps onto the same idea.
117 ///
118 /// Answers whether the application went back. `false` means there was nothing left to
119 /// go back to in the view, and the host should then do what the platform does without the
120 /// application, which on Android is the system's own Back for the activity.
121 ///
122 /// Flutter sends this as `popRoute` on `SystemChannels.navigation`, and has to ask the
123 /// framework in advance whether it handles back at all, because the answer cannot
124 /// cross its channel in time. This call answers on the spot instead; a host that cannot ask
125 /// on the spot hears it in advance through
126 /// [`ViewHost::set_framework_handles_back`](crate::ViewHost::set_framework_handles_back).
127 fn pop_route(&mut self, view: ViewId) -> bool {
128 let _ = view;
129 false
130 }
131
132 /// The user asked to close `view`: its window's close button, or the system's Close
133 /// command. The host closes nothing itself; the application closes the window, through
134 /// [`HostWindow::close`](crate::HostWindow::close), or keeps it open, such as until its user
135 /// has chosen whether to save. Nothing waits for an answer.
136 ///
137 /// Flutter's engine reports this to the window's controller, whose delegate's
138 /// `onWindowCloseRequested` closes it by default. The default here keeps the view open.
139 fn view_close_requested(&mut self, view: ViewId) {
140 let _ = view;
141 }
142
143 /// The window of `view` was minimized or restored, became occluded or shown, went full screen
144 /// or left it, or moved, as it does when maximized or returned, and
145 /// its [`HostWindow`](crate::HostWindow) already answers how it is now: as Flutter's window
146 /// controllers notify their listeners at the platform's word. A resize is reported as a
147 /// metrics change alone (`view_metrics_changed`). The default drops it.
148 fn window_changed(&mut self, view: ViewId) {
149 let _ = view;
150 }
151
152 /// A request for a view ([`crate::Platform::request_view`]) got no view: the host could not
153 /// make its window. The default drops it.
154 fn view_request_failed(&mut self, request: crate::RequestId, error: crate::ViewError) {
155 let _ = (request, error);
156 }
157
158 /// The system asked the application to quit, and the application may refuse: the user
159 /// chose Quit, or is logging out. Every request comes here, a second one while the first is
160 /// still being decided included, so that the application decides each; `reply` takes its
161 /// answer, and the host quits on [`AppExitResponse::Exit`].
162 ///
163 /// The answer may take as long as the application needs, such as asking its user whether
164 /// to save first, and is given inside a call into this client: this one, or a later one.
165 ///
166 /// Flutter sends this as `System.requestAppExit` on `SystemChannels.platform` and replies
167 /// once `ServicesBinding.handleRequestAppExit`'s `Future` has completed. The default
168 /// answers `Exit` at once, as `ServicesBinding`'s own answer does.
169 fn request_app_exit(&mut self, reply: AppExitReply) {
170 reply.answer(AppExitResponse::Exit);
171 }
172
173 /// The application entered `state`: its views were shown, hidden, focused or unfocused, or
174 /// the system put it in the background or brought it back. The host sends the state it is
175 /// in, and the framework fills in the states between, as Flutter's `ServicesBinding` does.
176 ///
177 /// Flutter sends this as the state's name on `SystemChannels.lifecycle`. The default drops
178 /// it.
179 fn app_lifecycle_state_changed(&mut self, state: AppLifecycleState) {
180 let _ = state;
181 }
182
183 /// Flutter `PlatformDispatcher.onPlatformBrightnessChanged`: the host's light or dark
184 /// preference changed, and `Platform::platform_brightness` already answers the new one.
185 fn platform_brightness_changed(&mut self);
186
187 /// Flutter `PlatformDispatcher.onLocaleChanged`: the host's locale list changed, and
188 /// `Platform::locales` already answers the new one.
189 fn locales_changed(&mut self);
190
191 /// Flutter `PlatformDispatcher.onSystemFontsChanged`: the host registered faces the
192 /// framework did not have when text last laid out.
193 fn system_fonts_changed(&mut self) {}
194
195 /// Another app changed what is on the clipboard, or the host cleared a copy of this app's that
196 /// expired, as far as the host can tell: at the moments its system says so, or lets it check.
197 /// The app's own writes are not reported, as it made them, but where the system tells them
198 /// apart from no other change, as a browser does not. No dart:ui hook; the default drops it.
199 fn clipboard_changed(&mut self) {}
200
201 /// The wake asked for through [`Dispatcher::wake_at`](crate::Dispatcher::wake_at) or
202 /// [`Dispatcher::wake_now`](crate::Dispatcher::wake_now).
203 ///
204 /// `elapsed` is the platform clock, the same one `Frame::elapsed` reports. Dart's
205 /// event loop runs due `Timer`s itself; here the client advances its own clock.
206 fn wake(&mut self, elapsed: Duration);
207
208 /// Flutter `TextInputClient.updateEditingValue`. The default drops it.
209 fn text_input_editing_value(&mut self, view: ViewId, value: TextEditingValue) {
210 let _ = (view, value);
211 }
212
213 /// Flutter `TextInputClient.performAction`. The default drops it.
214 fn text_input_action(&mut self, view: ViewId, action: TextInputAction) {
215 let _ = (view, action);
216 }
217
218 /// Flutter `TextInputClient.connectionClosed`. The default drops it.
219 fn text_input_closed(&mut self, view: ViewId) {
220 let _ = view;
221 }
222
223 /// Flutter `SystemContextMenuClient.handleSystemHide`. The default drops it.
224 fn system_context_menu_hidden(&mut self) {}
225
226 /// Flutter `SystemContextMenuClient.handleCustomContextMenuAction`. The default drops it.
227 fn custom_context_menu_action(&mut self, callback_id: &str) {
228 let _ = callback_id;
229 }
230
231 /// A client asked for the service extension `call` names: the framework answers it through
232 /// `call.reply`, which may be later, as its task ends. A host carries the call from its
233 /// transport and the answer back; the default answers that no extension of that name is
234 /// served.
235 fn service_extension_call(&mut self, call: ServiceExtensionCall) {
236 let ServiceExtensionCall { name, reply, .. } = call;
237 reply.answer(Err(ServiceExtensionError::Unknown {
238 name,
239 registered: Vec::new(),
240 }));
241 }
242}