• 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 #include "include/flutter/flutter_window_controller.h"
6 
7 #include <algorithm>
8 #include <iostream>
9 
10 namespace flutter {
11 
FlutterWindowController(const std::string & icu_data_path)12 FlutterWindowController::FlutterWindowController(
13     const std::string& icu_data_path)
14     : icu_data_path_(icu_data_path) {
15   init_succeeded_ = FlutterDesktopInit();
16 }
17 
~FlutterWindowController()18 FlutterWindowController::~FlutterWindowController() {
19   if (controller_) {
20     FlutterDesktopDestroyWindow(controller_);
21   }
22   if (init_succeeded_) {
23     FlutterDesktopTerminate();
24   }
25 }
26 
CreateWindow(int width,int height,const std::string & title,const std::string & assets_path,const std::vector<std::string> & arguments)27 bool FlutterWindowController::CreateWindow(
28     int width,
29     int height,
30     const std::string& title,
31     const std::string& assets_path,
32     const std::vector<std::string>& arguments) {
33   if (!init_succeeded_) {
34     std::cerr << "Could not create window; FlutterDesktopInit failed."
35               << std::endl;
36     return false;
37   }
38 
39   if (controller_) {
40     std::cerr << "Only one Flutter window can exist at a time." << std::endl;
41     return false;
42   }
43 
44   std::vector<const char*> engine_arguments;
45   std::transform(
46       arguments.begin(), arguments.end(), std::back_inserter(engine_arguments),
47       [](const std::string& arg) -> const char* { return arg.c_str(); });
48   size_t arg_count = engine_arguments.size();
49 
50   controller_ = FlutterDesktopCreateWindow(
51       width, height, title.c_str(), assets_path.c_str(), icu_data_path_.c_str(),
52       arg_count > 0 ? &engine_arguments[0] : nullptr, arg_count);
53   if (!controller_) {
54     std::cerr << "Failed to create window." << std::endl;
55     return false;
56   }
57   window_ =
58       std::make_unique<FlutterWindow>(FlutterDesktopGetWindow(controller_));
59   return true;
60 }
61 
GetRegistrarForPlugin(const std::string & plugin_name)62 FlutterDesktopPluginRegistrarRef FlutterWindowController::GetRegistrarForPlugin(
63     const std::string& plugin_name) {
64   if (!controller_) {
65     std::cerr << "Cannot get plugin registrar without a window; call "
66                  "CreateWindow first."
67               << std::endl;
68     return nullptr;
69   }
70   return FlutterDesktopGetPluginRegistrar(controller_, plugin_name.c_str());
71 }
72 
RunEventLoop()73 void FlutterWindowController::RunEventLoop() {
74   if (controller_) {
75     FlutterDesktopRunWindowLoop(controller_);
76   }
77   window_ = nullptr;
78   controller_ = nullptr;
79 }
80 
81 }  // namespace flutter
82