/
main.cpp
114 lines (99 loc) · 4.04 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <flutter/flutter_view_controller.h>
#include <windows.h>
#include <chrono>
#include <codecvt>
#include <iostream>
#include <string>
#include <vector>
#include "flutter/generated_plugin_registrant.h"
#include "win32_window.h"
#include "window_configuration.h"
namespace {
// Returns the path of the directory containing this executable, or an empty
// string if the directory cannot be found.
std::string GetExecutableDirectory() {
wchar_t buffer[MAX_PATH];
if (GetModuleFileName(nullptr, buffer, MAX_PATH) == 0) {
std::cerr << "Couldn't locate executable" << std::endl;
return "";
}
std::wstring_convert<std::codecvt_utf8<wchar_t>> wide_to_utf8;
std::string executable_path = wide_to_utf8.to_bytes(buffer);
size_t last_separator_position = executable_path.find_last_of('\\');
if (last_separator_position == std::string::npos) {
std::cerr << "Unabled to find parent directory of " << executable_path
<< std::endl;
return "";
}
return executable_path.substr(0, last_separator_position);
}
} // namespace
int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE prev, wchar_t *command_line,
int show_command) {
// Attach to console when present (e.g., 'flutter run') or create a
// new console when running with a debugger.
if (!::AttachConsole(ATTACH_PARENT_PROCESS) && ::IsDebuggerPresent()) {
::AllocConsole();
}
// Resources are located relative to the executable.
std::string base_directory = GetExecutableDirectory();
if (base_directory.empty()) {
base_directory = ".";
}
std::string data_directory = base_directory + "\\data";
std::string assets_path = data_directory + "\\flutter_assets";
std::string icu_data_path = data_directory + "\\icudtl.dat";
// Arguments for the Flutter Engine.
std::vector<std::string> arguments;
// Top-level window frame.
Win32Window::Point origin(kFlutterWindowOriginX, kFlutterWindowOriginY);
Win32Window::Size size(kFlutterWindowWidth, kFlutterWindowHeight);
flutter::FlutterViewController flutter_controller(
icu_data_path, size.width, size.height, assets_path, arguments);
RegisterPlugins(&flutter_controller);
// Create a top-level win32 window to host the Flutter view.
Win32Window window;
if (!window.CreateAndShow(kFlutterWindowTitle, origin, size)) {
return EXIT_FAILURE;
}
// Parent and resize Flutter view into top-level window.
window.SetChildContent(flutter_controller.GetNativeWindow());
// Run messageloop with a hook for flutter_controller to do work until
// the window is closed.
std::chrono::nanoseconds wait_duration(0);
// Run until the window is closed.
while (window.GetHandle() != nullptr) {
MsgWaitForMultipleObjects(0, NULL, FALSE,
static_cast<DWORD>(wait_duration.count() / 1000),
QS_ALLINPUT);
MSG message;
// All pending Windows messages must be processed; MsgWaitForMultipleObjects
// won't return again for items left in the queue after PeekMessage.
while (PeekMessage(&message, nullptr, 0, 0, PM_REMOVE)) {
if (message.message == WM_QUIT) {
window.Destroy();
break;
}
TranslateMessage(&message);
DispatchMessage(&message);
}
// Allow Flutter to process its messages.
// TODO: Consider interleaving processing on a per-message basis to avoid
// the possibility of one queue starving the other.
wait_duration = flutter_controller.ProcessMessages();
}
return EXIT_SUCCESS;
}