| Flutter Windows Embedder |

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
18 namespace flutter {
19
20 class FlutterWindowsEngine;
21
22 /// An event representing a change in window state that may update the
23 // application lifecycle state.
24 enum class WindowStateEvent {
25 kShow,
26 kHide,
27 kFocus,
28 kUnfocus,
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.
37 class WindowsLifecycleManager {
38 public:
39 WindowsLifecycleManager(FlutterWindowsEngine* engine);
40 virtual ~WindowsLifecycleManager();
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.
64 virtual void SetLifecycleState(AppLifecycleState state);
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
75 AppLifecycleState GetLifecycleState() { return state_; }
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,
87 UINT message,
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
122 flutter::AppLifecycleState state_;
123 };
124
125 } // namespace flutter
126
127 #endif // FLUTTER_SHELL_PLATFORM_WINDOWS_WINDOWS_LIFECYCLE_MANAGER_H_
flutter::WindowsLifecycleManager::GetLifecycleState
AppLifecycleState GetLifecycleState()
Definition: windows_lifecycle_manager.h:75
flutter::WindowStateEvent
WindowStateEvent
An event representing a change in window state that may update the.
Definition: windows_lifecycle_manager.h:24
flutter::WindowStateEvent::kHide
@ kHide
flutter::FlutterWindowsEngine
Definition: flutter_windows_engine.h:89
flutter::WindowsLifecycleManager::BeginProcessingLifecycle
virtual void BeginProcessingLifecycle()
Definition: windows_lifecycle_manager.cc:187
flutter::WindowsLifecycleManager::~WindowsLifecycleManager
virtual ~WindowsLifecycleManager()
Definition: windows_lifecycle_manager.cc:19
flutter::WindowsLifecycleManager::DispatchMessage
virtual void DispatchMessage(HWND window, UINT msg, WPARAM wparam, LPARAM lparam)
Definition: windows_lifecycle_manager.cc:34
app_lifecycle_state.h
flutter::WindowsLifecycleManager::SetLifecycleState
virtual void SetLifecycleState(AppLifecycleState state)
Definition: windows_lifecycle_manager.cc:195
flutter::WindowStateEvent::kFocus
@ kFocus
flutter::WindowsLifecycleManager::ExternalWindowMessage
std::optional< LRESULT > ExternalWindowMessage(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
Definition: windows_lifecycle_manager.cc:256
flutter::WindowStateEvent::kShow
@ kShow
flutter::WindowsLifecycleManager::WindowsLifecycleManager
WindowsLifecycleManager(FlutterWindowsEngine *engine)
Definition: windows_lifecycle_manager.cc:16
flutter
Definition: accessibility_bridge_windows.cc:11
flutter::WindowsLifecycleManager::OnWindowStateEvent
virtual void OnWindowStateEvent(HWND hwnd, WindowStateEvent event)
Definition: windows_lifecycle_manager.cc:208
flutter::WindowsLifecycleManager::IsLastWindowOfProcess
virtual bool IsLastWindowOfProcess()
Definition: windows_lifecycle_manager.cc:164
flutter::WindowsLifecycleManager::Quit
virtual void Quit(std::optional< HWND > window, std::optional< WPARAM > wparam, std::optional< LPARAM > lparam, UINT exit_code)
Definition: windows_lifecycle_manager.cc:21
flutter::WindowsLifecycleManager::BeginProcessingExit
virtual void BeginProcessingExit()
Definition: windows_lifecycle_manager.cc:191
flutter::WindowStateEvent::kUnfocus
@ kUnfocus
message
Win32Message message
Definition: keyboard_unittests.cc:137
flutter::AppLifecycleState
AppLifecycleState
Definition: app_lifecycle_state.h:32
flutter::WindowsLifecycleManager
Definition: windows_lifecycle_manager.h:37
flutter::WindowsLifecycleManager::WindowProc
bool WindowProc(HWND hwnd, UINT msg, WPARAM w, LPARAM l, LRESULT *result)
Definition: windows_lifecycle_manager.cc:65