Flutter Engine
The Flutter Engine
windows_lifecycle_manager.h
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
5#ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_WINDOWS_LIFECYCLE_MANAGER_H_
6#define FLUTTER_SHELL_PLATFORM_WINDOWS_WINDOWS_LIFECYCLE_MANAGER_H_
7
8#include <Windows.h>
9
10#include <cstdint>
11#include <map>
12#include <mutex>
13#include <optional>
14#include <set>
15
16#include "flutter/shell/platform/common/app_lifecycle_state.h"
17
18namespace flutter {
19
20class FlutterWindowsEngine;
21
22/// An event representing a change in window state that may update the
23// application lifecycle state.
24enum class WindowStateEvent {
25 kShow,
26 kHide,
27 kFocus,
29};
30
31/// A manager for lifecycle events of the top-level windows.
32///
33/// WndProc is called for window messages of the top-level Flutter window.
34/// ExternalWindowMessage is called for non-flutter top-level window messages.
35/// OnWindowStateEvent is called when the visibility or focus state of a window
36/// is changed, including the FlutterView window.
38 public:
41
42 // Called when the engine is notified it should quit, e.g. by an application
43 // call to `exitApplication`. When window is std::nullopt, this quits the
44 // application. Otherwise, it holds the HWND of the window that initiated the
45 // request, and exit_code is unused.
46 virtual void Quit(std::optional<HWND> window,
47 std::optional<WPARAM> wparam,
48 std::optional<LPARAM> lparam,
49 UINT exit_code);
50
51 // Intercept top level window WM_CLOSE message and listen to events that may
52 // update the application lifecycle.
53 bool WindowProc(HWND hwnd, UINT msg, WPARAM w, LPARAM l, LRESULT* result);
54
55 // Signal to start sending lifecycle state update messages.
56 virtual void BeginProcessingLifecycle();
57
58 // Signal to start consuming WM_CLOSE messages.
59 virtual void BeginProcessingExit();
60
61 // Update the app lifecycle state in response to a change in window state.
62 // When the app lifecycle state actually changes, this sends a platform
63 // message to the framework notifying it of the state change.
65
66 // Respond to a change in window state. Transitions as follows:
67 // When the only visible window is hidden, transition from resumed or
68 // inactive to hidden.
69 // When the only focused window is unfocused, transition from resumed to
70 // inactive.
71 // When a window is focused, transition from inactive to resumed.
72 // When a window is shown, transition from hidden to inactive.
73 virtual void OnWindowStateEvent(HWND hwnd, WindowStateEvent event);
74
76
77 // Called by the engine when a non-Flutter window receives an event that may
78 // alter the lifecycle state. The logic for external windows must differ from
79 // that used for FlutterWindow instances, because:
80 // - FlutterWindow does not receive WM_SHOW messages,
81 // - When FlutterWindow receives WM_SIZE messages, wparam stores no meaningful
82 // information, whereas it usually indicates the action which changed the
83 // window size.
84 // When this returns a result, the message has been consumed and should not be
85 // processed further. Currently, it will always return nullopt.
86 std::optional<LRESULT> ExternalWindowMessage(HWND hwnd,
88 WPARAM wparam,
89 LPARAM lparam);
90
91 protected:
92 // Check the number of top-level windows associated with this process, and
93 // return true only if there are 1 or fewer.
94 virtual bool IsLastWindowOfProcess();
95
96 virtual void DispatchMessage(HWND window,
97 UINT msg,
98 WPARAM wparam,
99 LPARAM lparam);
100
101 private:
102 // Pass top-level window close notifications to the application lifecycle
103 // logic. If the last window of the process receives WM_CLOSE and a listener
104 // is registered for WidgetsBindingObserver.didRequestAppExit, the message is
105 // sent to the framework to query whether the application should be allowed
106 // to quit.
107 bool HandleCloseMessage(HWND hwnd, WPARAM wparam, LPARAM lparam);
108
109 FlutterWindowsEngine* engine_;
110
111 std::map<std::tuple<HWND, WPARAM, LPARAM>, int> sent_close_messages_;
112
113 bool process_lifecycle_ = false;
114 bool process_exit_ = false;
115
116 std::set<HWND> visible_windows_;
117
118 std::set<HWND> focused_windows_;
119
120 std::mutex state_update_lock_;
121
123};
124
125} // namespace flutter
126
127#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_WINDOWS_LIFECYCLE_MANAGER_H_
virtual void OnWindowStateEvent(HWND hwnd, WindowStateEvent event)
std::optional< LRESULT > ExternalWindowMessage(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
WindowsLifecycleManager(FlutterWindowsEngine *engine)
bool WindowProc(HWND hwnd, UINT msg, WPARAM w, LPARAM l, LRESULT *result)
virtual void Quit(std::optional< HWND > window, std::optional< WPARAM > wparam, std::optional< LPARAM > lparam, UINT exit_code)
virtual void DispatchMessage(HWND window, UINT msg, WPARAM wparam, LPARAM lparam)
virtual void SetLifecycleState(AppLifecycleState state)
GLFWwindow * window
Definition: main.cc:45
FlutterEngine engine
Definition: main.cc:68
AtkStateType state
FlKeyEvent * event
GAsyncResult * result
Win32Message message
WindowStateEvent
An event representing a change in window state that may update the.
SkScalar w
LONG_PTR LRESULT
Definition: windows_types.h:61
unsigned int UINT
Definition: windows_types.h:32
LONG_PTR LPARAM
Definition: windows_types.h:60
UINT_PTR WPARAM
Definition: windows_types.h:59