• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef FLUTTER_SHELL_PLATFORM_FUCHSIA_PLATFORM_VIEW_H_
6 #define FLUTTER_SHELL_PLATFORM_FUCHSIA_PLATFORM_VIEW_H_
7 
8 #include <map>
9 #include <set>
10 
11 #include <fuchsia/modular/cpp/fidl.h>
12 #include <fuchsia/ui/gfx/cpp/fidl.h>
13 #include <fuchsia/ui/input/cpp/fidl.h>
14 #include <fuchsia/ui/scenic/cpp/fidl.h>
15 #include <lib/fit/function.h>
16 
17 #include "flutter/fml/macros.h"
18 #include "flutter/lib/ui/window/viewport_metrics.h"
19 #include "flutter/shell/common/platform_view.h"
20 #include "lib/fidl/cpp/binding.h"
21 #include "lib/ui/scenic/cpp/id.h"
22 
23 #include "surface.h"
24 
25 namespace flutter_runner {
26 
27 using OnMetricsUpdate = fit::function<void(const fuchsia::ui::gfx::Metrics&)>;
28 using OnSizeChangeHint =
29     fit::function<void(float width_change_factor, float height_change_factor)>;
30 
31 // The per engine component residing on the platform thread is responsible for
32 // all platform specific integrations.
33 //
34 // The PlatformView implements SessionListener and gets Session events but it
35 // does *not* actually own the Session itself; that is owned by the Compositor
36 // thread.
37 class PlatformView final : public flutter::PlatformView,
38                            private fuchsia::ui::scenic::SessionListener,
39                            public fuchsia::ui::input::InputMethodEditorClient {
40  public:
41   PlatformView(PlatformView::Delegate& delegate,
42                std::string debug_label,
43                flutter::TaskRunners task_runners,
44                fidl::InterfaceHandle<fuchsia::sys::ServiceProvider>
45                    parent_environment_service_provider,
46                fidl::InterfaceRequest<fuchsia::ui::scenic::SessionListener>
47                    session_listener_request,
48                fit::closure on_session_listener_error_callback,
49                OnMetricsUpdate session_metrics_did_change_callback,
50                OnSizeChangeHint session_size_change_hint_callback,
51                zx_handle_t vsync_event_handle);
52   PlatformView(PlatformView::Delegate& delegate,
53                std::string debug_label,
54                flutter::TaskRunners task_runners,
55                fidl::InterfaceHandle<fuchsia::sys::ServiceProvider>
56                    parent_environment_service_provider,
57                zx_handle_t vsync_event_handle);
58 
59   ~PlatformView();
60 
61   void UpdateViewportMetrics(const fuchsia::ui::gfx::Metrics& metrics);
62 
63  private:
64   const std::string debug_label_;
65 
66   fidl::Binding<fuchsia::ui::scenic::SessionListener> session_listener_binding_;
67   fit::closure session_listener_error_callback_;
68   OnMetricsUpdate metrics_changed_callback_;
69   OnSizeChangeHint size_change_hint_callback_;
70 
71   int current_text_input_client_ = 0;
72   fidl::Binding<fuchsia::ui::input::InputMethodEditorClient> ime_client_;
73   fuchsia::ui::input::InputMethodEditorPtr ime_;
74   fuchsia::ui::input::ImeServicePtr text_sync_service_;
75 
76   fuchsia::sys::ServiceProviderPtr parent_environment_service_provider_;
77   fuchsia::modular::ClipboardPtr clipboard_;
78   std::unique_ptr<Surface> surface_;
79   flutter::LogicalMetrics metrics_;
80   fuchsia::ui::gfx::Metrics scenic_metrics_;
81   // last_text_state_ is the last state of the text input as reported by the IME
82   // or initialized by Flutter. We set it to null if Flutter doesn't want any
83   // input, since then there is no text input state at all.
84   std::unique_ptr<fuchsia::ui::input::TextInputState> last_text_state_;
85 
86   std::set<int> down_pointers_;
87   std::map<
88       std::string /* channel */,
89       fit::function<void(
90           fml::RefPtr<flutter::PlatformMessage> /* message */)> /* handler */>
91       platform_message_handlers_;
92   zx_handle_t vsync_event_handle_ = 0;
93 
94   void RegisterPlatformMessageHandlers();
95 
96   void FlushViewportMetrics();
97 
98   // Called when the view's properties have changed.
99   void OnPropertiesChanged(
100       const fuchsia::ui::gfx::ViewProperties& view_properties);
101 
102   // |fuchsia::ui::input::InputMethodEditorClient|
103   void DidUpdateState(
104       fuchsia::ui::input::TextInputState state,
105       std::unique_ptr<fuchsia::ui::input::InputEvent> event) override;
106 
107   // |fuchsia::ui::input::InputMethodEditorClient|
108   void OnAction(fuchsia::ui::input::InputMethodAction action) override;
109 
110   // |fuchsia::ui::scenic::SessionListener|
111   void OnScenicError(std::string error) override;
112   void OnScenicEvent(std::vector<fuchsia::ui::scenic::Event> events) override;
113 
114   void OnChildViewConnected(scenic::ResourceId view_holder_id);
115   void OnChildViewDisconnected(scenic::ResourceId view_holder_id);
116   void OnChildViewStateChanged(scenic::ResourceId view_holder_id, bool state);
117 
118   bool OnHandlePointerEvent(const fuchsia::ui::input::PointerEvent& pointer);
119 
120   bool OnHandleKeyboardEvent(const fuchsia::ui::input::KeyboardEvent& keyboard);
121 
122   bool OnHandleFocusEvent(const fuchsia::ui::input::FocusEvent& focus);
123 
124   // Gets a new input method editor from the input connection. Run when both
125   // Scenic has focus and Flutter has requested input with setClient.
126   void ActivateIme();
127 
128   // Detaches the input method editor connection, ending the edit session and
129   // closing the onscreen keyboard. Call when input is no longer desired, either
130   // because Scenic says we lost focus or when Flutter no longer has a text
131   // field focused.
132   void DeactivateIme();
133 
134   // |flutter::PlatformView|
135   std::unique_ptr<flutter::VsyncWaiter> CreateVSyncWaiter() override;
136 
137   // |flutter::PlatformView|
138   std::unique_ptr<flutter::Surface> CreateRenderingSurface() override;
139 
140   // |flutter::PlatformView|
141   void HandlePlatformMessage(
142       fml::RefPtr<flutter::PlatformMessage> message) override;
143 
144   // |flutter::PlatformView|
145   void UpdateSemantics(
146       flutter::SemanticsNodeUpdates update,
147       flutter::CustomAccessibilityActionUpdates actions) override;
148 
149   // Channel handler for kAccessibilityChannel. This is currently not
150   // being used, but it is necessary to handle accessibility messages
151   // that are sent by Flutter when semantics is enabled.
152   void HandleAccessibilityChannelPlatformMessage(
153       fml::RefPtr<flutter::PlatformMessage> message);
154 
155   // Channel handler for kFlutterPlatformChannel
156   void HandleFlutterPlatformChannelPlatformMessage(
157       fml::RefPtr<flutter::PlatformMessage> message);
158 
159   // Channel handler for kTextInputChannel
160   void HandleFlutterTextInputChannelPlatformMessage(
161       fml::RefPtr<flutter::PlatformMessage> message);
162 
163   FML_DISALLOW_COPY_AND_ASSIGN(PlatformView);
164 };
165 
166 }  // namespace flutter_runner
167 
168 #endif  // FLUTTER_SHELL_PLATFORM_FUCHSIA_PLATFORM_VIEW_H_
169