university-app
71 строка · 2.1 Кб
1#include "flutter_window.h"
2
3#include <optional>
4
5#include "flutter/generated_plugin_registrant.h"
6
7FlutterWindow::FlutterWindow(const flutter::DartProject& project)
8: project_(project) {}
9
10FlutterWindow::~FlutterWindow() {}
11
12bool FlutterWindow::OnCreate() {
13if (!Win32Window::OnCreate()) {
14return false;
15}
16
17RECT frame = GetClientArea();
18
19// The size here must match the window dimensions to avoid unnecessary surface
20// creation / destruction in the startup path.
21flutter_controller_ = std::make_unique<flutter::FlutterViewController>(
22frame.right - frame.left, frame.bottom - frame.top, project_);
23// Ensure that basic setup of the controller was successful.
24if (!flutter_controller_->engine() || !flutter_controller_->view()) {
25return false;
26}
27RegisterPlugins(flutter_controller_->engine());
28SetChildContent(flutter_controller_->view()->GetNativeWindow());
29
30flutter_controller_->engine()->SetNextFrameCallback([&]() {
31this->Show();
32});
33
34// Flutter can complete the first frame before the "show window" callback is
35// registered. The following call ensures a frame is pending to ensure the
36// window is shown. It is a no-op if the first frame hasn't completed yet.
37flutter_controller_->ForceRedraw();
38
39return true;
40}
41
42void FlutterWindow::OnDestroy() {
43if (flutter_controller_) {
44flutter_controller_ = nullptr;
45}
46
47Win32Window::OnDestroy();
48}
49
50LRESULT
51FlutterWindow::MessageHandler(HWND hwnd, UINT const message,
52WPARAM const wparam,
53LPARAM const lparam) noexcept {
54// Give Flutter, including plugins, an opportunity to handle window messages.
55if (flutter_controller_) {
56std::optional<LRESULT> result =
57flutter_controller_->HandleTopLevelWindowProc(hwnd, message, wparam,
58lparam);
59if (result) {
60return *result;
61}
62}
63
64switch (message) {
65case WM_FONTCHANGE:
66flutter_controller_->engine()->ReloadSystemFonts();
67break;
68}
69
70return Win32Window::MessageHandler(hwnd, message, wparam, lparam);
71}
72