window_size_plugin.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // Copyright 2019 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "include/window_size/window_size_plugin.h"
  15. #include <Windows.h>
  16. #include <flutter/flutter_view.h>
  17. #include <flutter/method_channel.h>
  18. #include <flutter/plugin_registrar_windows.h>
  19. #include <flutter/standard_method_codec.h>
  20. #include <flutter_windows.h>
  21. #include <codecvt>
  22. #include <memory>
  23. #include <optional>
  24. #include <sstream>
  25. namespace {
  26. using flutter::EncodableList;
  27. using flutter::EncodableMap;
  28. using flutter::EncodableValue;
  29. // See window_size_channel.dart for documentation.
  30. const char kChannelName[] = "flutter/windowsize";
  31. const char kGetScreenListMethod[] = "getScreenList";
  32. const char kGetWindowInfoMethod[] = "getWindowInfo";
  33. const char kSetWindowFrameMethod[] = "setWindowFrame";
  34. const char kSetWindowMinimumSize[] = "setWindowMinimumSize";
  35. const char kSetWindowMaximumSize[] = "setWindowMaximumSize";
  36. const char kSetWindowTitleMethod[] = "setWindowTitle";
  37. const char ksetWindowVisibilityMethod[] = "setWindowVisibility";
  38. const char kFrameKey[] = "frame";
  39. const char kVisibleFrameKey[] = "visibleFrame";
  40. const char kScaleFactorKey[] = "scaleFactor";
  41. const char kScreenKey[] = "screen";
  42. const double kBaseDpi = 96.0;
  43. // Returns a POINT corresponding to channel representation of a size.
  44. POINT GetPointForPlatformChannelRepresentationSize(const EncodableList &size) {
  45. POINT point = {};
  46. point.x = static_cast<LONG>(std::get<double>(size[0]));
  47. point.y = static_cast<LONG>(std::get<double>(size[1]));
  48. return point;
  49. }
  50. // Returns the serializable form of |frame| expected by the platform channel.
  51. EncodableValue GetPlatformChannelRepresentationForRect(const RECT &rect) {
  52. return EncodableValue(EncodableList{
  53. EncodableValue(static_cast<double>(rect.left)),
  54. EncodableValue(static_cast<double>(rect.top)),
  55. EncodableValue(static_cast<double>(rect.right) -
  56. static_cast<double>(rect.left)),
  57. EncodableValue(static_cast<double>(rect.bottom) -
  58. static_cast<double>(rect.top)),
  59. });
  60. }
  61. // Extracts information from monitor |monitor| and returns the
  62. // serializable form expected by the platform channel.
  63. EncodableValue GetPlatformChannelRepresentationForMonitor(HMONITOR monitor) {
  64. if (!monitor) {
  65. return EncodableValue();
  66. }
  67. MONITORINFO info;
  68. info.cbSize = sizeof(MONITORINFO);
  69. ::GetMonitorInfo(monitor, &info);
  70. UINT dpi = FlutterDesktopGetDpiForMonitor(monitor);
  71. double scale_factor = dpi / kBaseDpi;
  72. return EncodableValue(EncodableMap{
  73. {EncodableValue(kFrameKey),
  74. GetPlatformChannelRepresentationForRect(info.rcMonitor)},
  75. {EncodableValue(kVisibleFrameKey),
  76. GetPlatformChannelRepresentationForRect(info.rcWork)},
  77. {EncodableValue(kScaleFactorKey), EncodableValue(scale_factor)},
  78. });
  79. }
  80. BOOL CALLBACK MonitorRepresentationEnumProc(HMONITOR monitor, HDC hdc,
  81. LPRECT clip, LPARAM list_ref) {
  82. EncodableValue *monitors = reinterpret_cast<EncodableValue *>(list_ref);
  83. std::get<EncodableList>(*monitors).push_back(
  84. GetPlatformChannelRepresentationForMonitor(monitor));
  85. return TRUE;
  86. }
  87. // Extracts information from |window| and returns the serializable form expected
  88. // by the platform channel.
  89. EncodableValue GetPlatformChannelRepresentationForWindow(HWND window) {
  90. if (!window) {
  91. return EncodableValue();
  92. }
  93. RECT frame;
  94. ::GetWindowRect(window, &frame);
  95. HMONITOR window_monitor =
  96. ::MonitorFromWindow(window, MONITOR_DEFAULTTOPRIMARY);
  97. double scale_factor = FlutterDesktopGetDpiForHWND(window) / kBaseDpi;
  98. return EncodableValue(EncodableMap{
  99. {EncodableValue(kFrameKey),
  100. GetPlatformChannelRepresentationForRect(frame)},
  101. {EncodableValue(kScreenKey),
  102. GetPlatformChannelRepresentationForMonitor(window_monitor)},
  103. {EncodableValue(kScaleFactorKey), EncodableValue(scale_factor)},
  104. });
  105. }
  106. HWND GetRootWindow(flutter::FlutterView *view) {
  107. return ::GetAncestor(view->GetNativeWindow(), GA_ROOT);
  108. }
  109. class WindowSizePlugin : public flutter::Plugin {
  110. public:
  111. static void RegisterWithRegistrar(flutter::PluginRegistrarWindows *registrar);
  112. // Creates a plugin that communicates on the given channel.
  113. WindowSizePlugin(flutter::PluginRegistrarWindows *registrar);
  114. virtual ~WindowSizePlugin();
  115. private:
  116. // Called when a method is called on the plugin channel;
  117. void HandleMethodCall(const flutter::MethodCall<> &method_call,
  118. std::unique_ptr<flutter::MethodResult<>> result);
  119. // Called for top-level WindowProc delegation.
  120. std::optional<LRESULT> HandleWindowProc(HWND hwnd, UINT message,
  121. WPARAM wparam, LPARAM lparam);
  122. // The registrar for this plugin, for accessing the window.
  123. flutter::PluginRegistrarWindows *registrar_;
  124. // The ID of the WindowProc delegate registration.
  125. int window_proc_id_ = -1;
  126. // The minimum size set by the platform channel.
  127. POINT min_size_ = {0, 0};
  128. // The maximum size set by the platform channel.
  129. POINT max_size_ = {-1, -1};
  130. };
  131. // static
  132. void WindowSizePlugin::RegisterWithRegistrar(
  133. flutter::PluginRegistrarWindows *registrar) {
  134. auto channel = std::make_unique<flutter::MethodChannel<>>(
  135. registrar->messenger(), kChannelName,
  136. &flutter::StandardMethodCodec::GetInstance());
  137. auto plugin = std::make_unique<WindowSizePlugin>(registrar);
  138. channel->SetMethodCallHandler(
  139. [plugin_pointer = plugin.get()](const auto &call, auto result) {
  140. plugin_pointer->HandleMethodCall(call, std::move(result));
  141. });
  142. registrar->AddPlugin(std::move(plugin));
  143. }
  144. WindowSizePlugin::WindowSizePlugin(flutter::PluginRegistrarWindows *registrar)
  145. : registrar_(registrar) {
  146. window_proc_id_ = registrar_->RegisterTopLevelWindowProcDelegate(
  147. [this](HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) {
  148. return HandleWindowProc(hwnd, message, wparam, lparam);
  149. });
  150. }
  151. WindowSizePlugin::~WindowSizePlugin() {
  152. registrar_->UnregisterTopLevelWindowProcDelegate(window_proc_id_);
  153. }
  154. void WindowSizePlugin::HandleMethodCall(
  155. const flutter::MethodCall<> &method_call,
  156. std::unique_ptr<flutter::MethodResult<>> result) {
  157. if (method_call.method_name().compare(kGetScreenListMethod) == 0) {
  158. EncodableValue screens(std::in_place_type<EncodableList>);
  159. ::EnumDisplayMonitors(nullptr, nullptr, MonitorRepresentationEnumProc,
  160. reinterpret_cast<LPARAM>(&screens));
  161. result->Success(screens);
  162. } else if (method_call.method_name().compare(kGetWindowInfoMethod) == 0) {
  163. result->Success(GetPlatformChannelRepresentationForWindow(
  164. GetRootWindow(registrar_->GetView())));
  165. } else if (method_call.method_name().compare(kSetWindowFrameMethod) == 0) {
  166. const auto *frame_list =
  167. std::get_if<EncodableList>(method_call.arguments());
  168. if (!frame_list || frame_list->size() != 4) {
  169. result->Error("Bad arguments", "Expected 4-element list");
  170. return;
  171. }
  172. // Frame validity (e.g., non-zero size) is assumed to be checked on the Dart
  173. // side of the call.
  174. int x = static_cast<int>(std::get<double>((*frame_list)[0]));
  175. int y = static_cast<int>(std::get<double>((*frame_list)[1]));
  176. int width = static_cast<int>(std::get<double>((*frame_list)[2]));
  177. int height = static_cast<int>(std::get<double>((*frame_list)[3]));
  178. ::SetWindowPos(GetRootWindow(registrar_->GetView()), nullptr, x, y, width,
  179. height, SWP_NOACTIVATE | SWP_NOOWNERZORDER);
  180. result->Success();
  181. } else if (method_call.method_name().compare(kSetWindowMinimumSize) == 0) {
  182. const auto *size = std::get_if<EncodableList>(method_call.arguments());
  183. if (!size || size->size() != 2) {
  184. result->Error("Bad arguments", "Expected 2-element list");
  185. return;
  186. }
  187. min_size_ = GetPointForPlatformChannelRepresentationSize(*size);
  188. result->Success();
  189. } else if (method_call.method_name().compare(kSetWindowMaximumSize) == 0) {
  190. const auto *size = std::get_if<EncodableList>(method_call.arguments());
  191. if (!size || size->size() != 2) {
  192. result->Error("Bad arguments", "Expected 2-element list");
  193. return;
  194. }
  195. max_size_ = GetPointForPlatformChannelRepresentationSize(*size);
  196. result->Success();
  197. } else if (method_call.method_name().compare(kSetWindowTitleMethod) == 0) {
  198. const auto *title = std::get_if<std::string>(method_call.arguments());
  199. if (!title) {
  200. result->Error("Bad arguments", "Expected string");
  201. return;
  202. }
  203. std::wstring wstr =
  204. std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t>{}
  205. .from_bytes(*title);
  206. ::SetWindowText(GetRootWindow(registrar_->GetView()), wstr.c_str());
  207. result->Success();
  208. } else if (method_call.method_name().compare(ksetWindowVisibilityMethod) ==
  209. 0) {
  210. const bool *visible = std::get_if<bool>(method_call.arguments());
  211. if (visible == nullptr) {
  212. result->Error("Bad arguments", "Expected bool");
  213. return;
  214. }
  215. ::ShowWindow(GetRootWindow(registrar_->GetView()),
  216. *visible ? SW_SHOW : SW_HIDE);
  217. result->Success();
  218. } else {
  219. result->NotImplemented();
  220. }
  221. }
  222. std::optional<LRESULT> WindowSizePlugin::HandleWindowProc(HWND hwnd,
  223. UINT message,
  224. WPARAM wparam,
  225. LPARAM lparam) {
  226. std::optional<LRESULT> result;
  227. switch (message) {
  228. case WM_GETMINMAXINFO:
  229. MINMAXINFO *info = reinterpret_cast<MINMAXINFO *>(lparam);
  230. // For the special "unconstrained" values, leave the defaults.
  231. if (min_size_.x != 0) info->ptMinTrackSize.x = min_size_.x;
  232. if (min_size_.y != 0) info->ptMinTrackSize.y = min_size_.y;
  233. if (max_size_.x != -1) info->ptMaxTrackSize.x = max_size_.x;
  234. if (max_size_.y != -1) info->ptMaxTrackSize.y = max_size_.y;
  235. result = 0;
  236. break;
  237. }
  238. return result;
  239. }
  240. } // namespace
  241. void WindowSizePluginRegisterWithRegistrar(
  242. FlutterDesktopPluginRegistrarRef registrar) {
  243. WindowSizePlugin::RegisterWithRegistrar(
  244. flutter::PluginRegistrarManager::GetInstance()
  245. ->GetRegistrar<flutter::PluginRegistrarWindows>(registrar));
  246. }