Flutter Engine
flutter_window_controller.cc
Go to the documentation of this file.
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 
6 
7 #include <algorithm>
8 #include <iostream>
9 
10 namespace flutter {
11 
13  const std::string& icu_data_path)
14  : icu_data_path_(icu_data_path) {
15  init_succeeded_ = FlutterDesktopInit();
16 }
17 
19  if (controller_) {
20  FlutterDesktopDestroyWindow(controller_);
21  }
22  if (init_succeeded_) {
24  }
25 }
26 
28  const WindowProperties& window_properties,
29  const std::string& assets_path,
30  const std::vector<std::string>& arguments,
31  const std::string& aot_library_path) {
32  if (!init_succeeded_) {
33  std::cerr << "Could not create window; FlutterDesktopInit failed."
34  << std::endl;
35  return false;
36  }
37 
38  if (controller_) {
39  std::cerr << "Only one Flutter window can exist at a time." << std::endl;
40  return false;
41  }
42 
43  FlutterDesktopWindowProperties c_window_properties = {};
44  c_window_properties.title = window_properties.title.c_str();
45  c_window_properties.width = window_properties.width;
46  c_window_properties.height = window_properties.height;
47  c_window_properties.prevent_resize = window_properties.prevent_resize;
48 
49  FlutterDesktopEngineProperties c_engine_properties = {};
50  c_engine_properties.assets_path = assets_path.c_str();
51  c_engine_properties.aot_library_path = aot_library_path.c_str();
52  c_engine_properties.icu_data_path = icu_data_path_.c_str();
53  std::vector<const char*> engine_switches;
55  arguments.begin(), arguments.end(), std::back_inserter(engine_switches),
56  [](const std::string& arg) -> const char* { return arg.c_str(); });
57  if (engine_switches.size() > 0) {
58  c_engine_properties.switches = &engine_switches[0];
59  c_engine_properties.switches_count = engine_switches.size();
60  }
61 
62  controller_ =
63  FlutterDesktopCreateWindow(c_window_properties, c_engine_properties);
64  if (!controller_) {
65  std::cerr << "Failed to create window." << std::endl;
66  return false;
67  }
68  window_ =
69  std::make_unique<FlutterWindow>(FlutterDesktopGetWindow(controller_));
70  return true;
71 }
72 
74  if (controller_) {
75  FlutterDesktopDestroyWindow(controller_);
76  controller_ = nullptr;
77  window_ = nullptr;
78  }
79 }
80 
82  const std::string& plugin_name) {
83  if (!controller_) {
84  std::cerr << "Cannot get plugin registrar without a window; call "
85  "CreateWindow first."
86  << std::endl;
87  return nullptr;
88  }
90  plugin_name.c_str());
91 }
92 
94  std::chrono::milliseconds timeout) {
95  if (!controller_) {
96  std::cerr << "Cannot run event loop without a window window; call "
97  "CreateWindow first."
98  << std::endl;
99  return false;
100  }
101  uint32_t timeout_milliseconds;
102  if (timeout == std::chrono::milliseconds::max()) {
103  // The C API uses 0 to represent no timeout, so convert |max| to 0.
104  timeout_milliseconds = 0;
105  } else if (timeout.count() > UINT32_MAX) {
106  timeout_milliseconds = UINT32_MAX;
107  } else {
108  timeout_milliseconds = static_cast<uint32_t>(timeout.count());
109  }
110  bool still_running = FlutterDesktopRunWindowEventLoopWithTimeout(
111  controller_, timeout_milliseconds);
112  if (!still_running) {
113  DestroyWindow();
114  }
115  return still_running;
116 }
117 
119  while (RunEventLoopWithTimeout()) {
120  }
121 }
122 
123 } // namespace flutter
bool FlutterDesktopInit()
void FlutterDesktopTerminate()
bool RunEventLoopWithTimeout(std::chrono::milliseconds timeout=std::chrono::milliseconds::max())
FlutterDesktopEngineRef FlutterDesktopGetEngine(FlutterDesktopWindowControllerRef controller)
bool FlutterDesktopRunWindowEventLoopWithTimeout(FlutterDesktopWindowControllerRef controller, uint32_t millisecond_timeout)
void FlutterDesktopDestroyWindow(FlutterDesktopWindowControllerRef controller)
FlutterDesktopWindowRef FlutterDesktopGetWindow(FlutterDesktopWindowControllerRef controller)
FlutterDesktopPluginRegistrarRef GetRegistrarForPlugin(const std::string &plugin_name) override
bool CreateWindow(const WindowProperties &window_properties, const std::string &assets_path, const std::vector< std::string > &arguments, const std::string &aot_library_path="")
FlutterWindowController(const std::string &icu_data_path)
FlutterDesktopPluginRegistrarRef FlutterDesktopGetPluginRegistrar(FlutterDesktopEngineRef engine, const char *plugin_name)
FlutterDesktopWindowControllerRef FlutterDesktopCreateWindow(const FlutterDesktopWindowProperties &window_properties, const FlutterDesktopEngineProperties &engine_properties)