native_window.cc 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. // Copyright (c) 2013 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "shell/browser/native_window.h"
  5. #include <algorithm>
  6. #include <string>
  7. #include <vector>
  8. #include "base/containers/contains.h"
  9. #include "base/memory/ptr_util.h"
  10. #include "base/strings/utf_string_conversions.h"
  11. #include "base/values.h"
  12. #include "content/public/browser/web_contents_user_data.h"
  13. #include "include/core/SkColor.h"
  14. #include "shell/browser/background_throttling_source.h"
  15. #include "shell/browser/browser.h"
  16. #include "shell/browser/draggable_region_provider.h"
  17. #include "shell/browser/native_window_features.h"
  18. #include "shell/browser/ui/drag_util.h"
  19. #include "shell/browser/window_list.h"
  20. #include "shell/common/color_util.h"
  21. #include "shell/common/gin_helper/dictionary.h"
  22. #include "shell/common/gin_helper/persistent_dictionary.h"
  23. #include "shell/common/options_switches.h"
  24. #include "ui/base/hit_test.h"
  25. #include "ui/compositor/compositor.h"
  26. #include "ui/views/widget/widget.h"
  27. #if !BUILDFLAG(IS_MAC)
  28. #include "shell/browser/ui/views/frameless_view.h"
  29. #endif
  30. #if BUILDFLAG(IS_WIN)
  31. #include "ui/display/win/screen_win.h"
  32. #endif
  33. #if defined(USE_OZONE)
  34. #include "ui/base/ui_base_features.h"
  35. #include "ui/ozone/public/ozone_platform.h"
  36. #endif
  37. namespace gin {
  38. template <>
  39. struct Converter<electron::NativeWindow::TitleBarStyle> {
  40. static bool FromV8(v8::Isolate* isolate,
  41. v8::Local<v8::Value> val,
  42. electron::NativeWindow::TitleBarStyle* out) {
  43. using TitleBarStyle = electron::NativeWindow::TitleBarStyle;
  44. std::string title_bar_style;
  45. if (!ConvertFromV8(isolate, val, &title_bar_style))
  46. return false;
  47. if (title_bar_style == "hidden") {
  48. *out = TitleBarStyle::kHidden;
  49. #if BUILDFLAG(IS_MAC)
  50. } else if (title_bar_style == "hiddenInset") {
  51. *out = TitleBarStyle::kHiddenInset;
  52. } else if (title_bar_style == "customButtonsOnHover") {
  53. *out = TitleBarStyle::kCustomButtonsOnHover;
  54. #endif
  55. } else {
  56. return false;
  57. }
  58. return true;
  59. }
  60. };
  61. } // namespace gin
  62. namespace electron {
  63. namespace {
  64. #if BUILDFLAG(IS_WIN)
  65. gfx::Size GetExpandedWindowSize(const NativeWindow* window, gfx::Size size) {
  66. if (!window->transparent())
  67. return size;
  68. gfx::Size min_size = display::win::ScreenWin::ScreenToDIPSize(
  69. window->GetAcceleratedWidget(), gfx::Size(64, 64));
  70. // Some AMD drivers can't display windows that are less than 64x64 pixels,
  71. // so expand them to be at least that size. http://crbug.com/286609
  72. gfx::Size expanded(std::max(size.width(), min_size.width()),
  73. std::max(size.height(), min_size.height()));
  74. return expanded;
  75. }
  76. #endif
  77. } // namespace
  78. const char kElectronNativeWindowKey[] = "__ELECTRON_NATIVE_WINDOW__";
  79. NativeWindow::NativeWindow(const gin_helper::Dictionary& options,
  80. NativeWindow* parent)
  81. : widget_(std::make_unique<views::Widget>()), parent_(parent) {
  82. ++next_id_;
  83. options.Get(options::kFrame, &has_frame_);
  84. options.Get(options::kTransparent, &transparent_);
  85. options.Get(options::kEnableLargerThanScreen, &enable_larger_than_screen_);
  86. options.Get(options::kTitleBarStyle, &title_bar_style_);
  87. #if BUILDFLAG(IS_WIN)
  88. options.Get(options::kBackgroundMaterial, &background_material_);
  89. #elif BUILDFLAG(IS_MAC)
  90. options.Get(options::kVibrancyType, &vibrancy_);
  91. #endif
  92. v8::Local<v8::Value> titlebar_overlay;
  93. if (options.Get(options::ktitleBarOverlay, &titlebar_overlay)) {
  94. if (titlebar_overlay->IsBoolean()) {
  95. options.Get(options::ktitleBarOverlay, &titlebar_overlay_);
  96. } else if (titlebar_overlay->IsObject()) {
  97. titlebar_overlay_ = true;
  98. auto titlebar_overlay_dict =
  99. gin_helper::Dictionary::CreateEmpty(options.isolate());
  100. options.Get(options::ktitleBarOverlay, &titlebar_overlay_dict);
  101. int height;
  102. if (titlebar_overlay_dict.Get(options::kOverlayHeight, &height))
  103. titlebar_overlay_height_ = height;
  104. }
  105. }
  106. if (parent)
  107. options.Get("modal", &is_modal_);
  108. #if defined(USE_OZONE)
  109. // Ozone X11 likes to prefer custom frames, but we don't need them unless
  110. // on Wayland.
  111. if (base::FeatureList::IsEnabled(features::kWaylandWindowDecorations) &&
  112. !ui::OzonePlatform::GetInstance()
  113. ->GetPlatformRuntimeProperties()
  114. .supports_server_side_window_decorations) {
  115. has_client_frame_ = true;
  116. }
  117. #endif
  118. WindowList::AddWindow(this);
  119. }
  120. NativeWindow::~NativeWindow() {
  121. // It's possible that the windows gets destroyed before it's closed, in that
  122. // case we need to ensure the Widget delegate gets destroyed and
  123. // OnWindowClosed message is still notified.
  124. if (widget_->widget_delegate())
  125. widget_->OnNativeWidgetDestroyed();
  126. NotifyWindowClosed();
  127. }
  128. void NativeWindow::InitFromOptions(const gin_helper::Dictionary& options) {
  129. // Setup window from options.
  130. int x = -1, y = -1;
  131. bool center;
  132. if (options.Get(options::kX, &x) && options.Get(options::kY, &y)) {
  133. SetPosition(gfx::Point(x, y));
  134. #if BUILDFLAG(IS_WIN)
  135. // FIXME(felixrieseberg): Dirty, dirty workaround for
  136. // https://github.com/electron/electron/issues/10862
  137. // Somehow, we need to call `SetBounds` twice to get
  138. // usable results. The root cause is still unknown.
  139. SetPosition(gfx::Point(x, y));
  140. #endif
  141. } else if (options.Get(options::kCenter, &center) && center) {
  142. Center();
  143. }
  144. bool use_content_size = false;
  145. options.Get(options::kUseContentSize, &use_content_size);
  146. // On Linux and Window we may already have maximum size defined.
  147. extensions::SizeConstraints size_constraints(
  148. use_content_size ? GetContentSizeConstraints() : GetSizeConstraints());
  149. int min_width = size_constraints.GetMinimumSize().width();
  150. int min_height = size_constraints.GetMinimumSize().height();
  151. options.Get(options::kMinWidth, &min_width);
  152. options.Get(options::kMinHeight, &min_height);
  153. size_constraints.set_minimum_size(gfx::Size(min_width, min_height));
  154. gfx::Size max_size = size_constraints.GetMaximumSize();
  155. int max_width = max_size.width() > 0 ? max_size.width() : INT_MAX;
  156. int max_height = max_size.height() > 0 ? max_size.height() : INT_MAX;
  157. bool have_max_width = options.Get(options::kMaxWidth, &max_width);
  158. if (have_max_width && max_width <= 0)
  159. max_width = INT_MAX;
  160. bool have_max_height = options.Get(options::kMaxHeight, &max_height);
  161. if (have_max_height && max_height <= 0)
  162. max_height = INT_MAX;
  163. // By default the window has a default maximum size that prevents it
  164. // from being resized larger than the screen, so we should only set this
  165. // if the user has passed in values.
  166. if (have_max_height || have_max_width || !max_size.IsEmpty())
  167. size_constraints.set_maximum_size(gfx::Size(max_width, max_height));
  168. if (use_content_size) {
  169. SetContentSizeConstraints(size_constraints);
  170. } else {
  171. SetSizeConstraints(size_constraints);
  172. }
  173. #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX)
  174. bool closable;
  175. if (options.Get(options::kClosable, &closable)) {
  176. SetClosable(closable);
  177. }
  178. #endif
  179. bool movable;
  180. if (options.Get(options::kMovable, &movable)) {
  181. SetMovable(movable);
  182. }
  183. bool has_shadow;
  184. if (options.Get(options::kHasShadow, &has_shadow)) {
  185. SetHasShadow(has_shadow);
  186. }
  187. double opacity;
  188. if (options.Get(options::kOpacity, &opacity)) {
  189. SetOpacity(opacity);
  190. }
  191. bool top;
  192. if (options.Get(options::kAlwaysOnTop, &top) && top) {
  193. SetAlwaysOnTop(ui::ZOrderLevel::kFloatingWindow);
  194. }
  195. bool fullscreenable = true;
  196. bool fullscreen = false;
  197. if (options.Get(options::kFullscreen, &fullscreen) && !fullscreen) {
  198. // Disable fullscreen button if 'fullscreen' is specified to false.
  199. #if BUILDFLAG(IS_MAC)
  200. fullscreenable = false;
  201. #endif
  202. }
  203. options.Get(options::kFullScreenable, &fullscreenable);
  204. SetFullScreenable(fullscreenable);
  205. if (fullscreen)
  206. SetFullScreen(true);
  207. bool resizable;
  208. if (options.Get(options::kResizable, &resizable)) {
  209. SetResizable(resizable);
  210. }
  211. bool skip;
  212. if (options.Get(options::kSkipTaskbar, &skip)) {
  213. SetSkipTaskbar(skip);
  214. }
  215. bool kiosk;
  216. if (options.Get(options::kKiosk, &kiosk) && kiosk) {
  217. SetKiosk(kiosk);
  218. }
  219. #if BUILDFLAG(IS_MAC)
  220. std::string type;
  221. if (options.Get(options::kVibrancyType, &type)) {
  222. SetVibrancy(type);
  223. }
  224. #elif BUILDFLAG(IS_WIN)
  225. std::string material;
  226. if (options.Get(options::kBackgroundMaterial, &material)) {
  227. SetBackgroundMaterial(material);
  228. }
  229. #endif
  230. SkColor background_color = SK_ColorWHITE;
  231. if (std::string color; options.Get(options::kBackgroundColor, &color)) {
  232. background_color = ParseCSSColor(color);
  233. } else if (IsTranslucent()) {
  234. background_color = SK_ColorTRANSPARENT;
  235. }
  236. SetBackgroundColor(background_color);
  237. std::string title(Browser::Get()->GetName());
  238. options.Get(options::kTitle, &title);
  239. SetTitle(title);
  240. // Then show it.
  241. bool show = true;
  242. options.Get(options::kShow, &show);
  243. if (show)
  244. Show();
  245. }
  246. bool NativeWindow::IsClosed() const {
  247. return is_closed_;
  248. }
  249. void NativeWindow::SetSize(const gfx::Size& size, bool animate) {
  250. SetBounds(gfx::Rect(GetPosition(), size), animate);
  251. }
  252. gfx::Size NativeWindow::GetSize() const {
  253. return GetBounds().size();
  254. }
  255. void NativeWindow::SetPosition(const gfx::Point& position, bool animate) {
  256. SetBounds(gfx::Rect(position, GetSize()), animate);
  257. }
  258. gfx::Point NativeWindow::GetPosition() const {
  259. return GetBounds().origin();
  260. }
  261. void NativeWindow::SetContentSize(const gfx::Size& size, bool animate) {
  262. SetSize(ContentBoundsToWindowBounds(gfx::Rect(size)).size(), animate);
  263. }
  264. gfx::Size NativeWindow::GetContentSize() const {
  265. return GetContentBounds().size();
  266. }
  267. void NativeWindow::SetContentBounds(const gfx::Rect& bounds, bool animate) {
  268. SetBounds(ContentBoundsToWindowBounds(bounds), animate);
  269. }
  270. gfx::Rect NativeWindow::GetContentBounds() const {
  271. return WindowBoundsToContentBounds(GetBounds());
  272. }
  273. bool NativeWindow::IsNormal() const {
  274. return !IsMinimized() && !IsMaximized() && !IsFullscreen();
  275. }
  276. void NativeWindow::SetSizeConstraints(
  277. const extensions::SizeConstraints& window_constraints) {
  278. size_constraints_ = window_constraints;
  279. content_size_constraints_.reset();
  280. }
  281. extensions::SizeConstraints NativeWindow::GetSizeConstraints() const {
  282. if (size_constraints_)
  283. return *size_constraints_;
  284. if (!content_size_constraints_)
  285. return extensions::SizeConstraints();
  286. // Convert content size constraints to window size constraints.
  287. extensions::SizeConstraints constraints;
  288. if (content_size_constraints_->HasMaximumSize()) {
  289. gfx::Rect max_bounds = ContentBoundsToWindowBounds(
  290. gfx::Rect(content_size_constraints_->GetMaximumSize()));
  291. constraints.set_maximum_size(max_bounds.size());
  292. }
  293. if (content_size_constraints_->HasMinimumSize()) {
  294. gfx::Rect min_bounds = ContentBoundsToWindowBounds(
  295. gfx::Rect(content_size_constraints_->GetMinimumSize()));
  296. constraints.set_minimum_size(min_bounds.size());
  297. }
  298. return constraints;
  299. }
  300. void NativeWindow::SetContentSizeConstraints(
  301. const extensions::SizeConstraints& size_constraints) {
  302. content_size_constraints_ = size_constraints;
  303. size_constraints_.reset();
  304. }
  305. // Windows/Linux:
  306. // The return value of GetContentSizeConstraints will be passed to Chromium
  307. // to set min/max sizes of window. Note that we are returning content size
  308. // instead of window size because that is what Chromium expects, see the
  309. // comment of |WidgetSizeIsClientSize| in Chromium's codebase to learn more.
  310. //
  311. // macOS:
  312. // The min/max sizes are set directly by calling NSWindow's methods.
  313. extensions::SizeConstraints NativeWindow::GetContentSizeConstraints() const {
  314. if (content_size_constraints_)
  315. return *content_size_constraints_;
  316. if (!size_constraints_)
  317. return extensions::SizeConstraints();
  318. // Convert window size constraints to content size constraints.
  319. // Note that we are not caching the results, because Chromium reccalculates
  320. // window frame size everytime when min/max sizes are passed, and we must
  321. // do the same otherwise the resulting size with frame included will be wrong.
  322. extensions::SizeConstraints constraints;
  323. if (size_constraints_->HasMaximumSize()) {
  324. gfx::Rect max_bounds = WindowBoundsToContentBounds(
  325. gfx::Rect(size_constraints_->GetMaximumSize()));
  326. constraints.set_maximum_size(max_bounds.size());
  327. }
  328. if (size_constraints_->HasMinimumSize()) {
  329. gfx::Rect min_bounds = WindowBoundsToContentBounds(
  330. gfx::Rect(size_constraints_->GetMinimumSize()));
  331. constraints.set_minimum_size(min_bounds.size());
  332. }
  333. return constraints;
  334. }
  335. void NativeWindow::SetMinimumSize(const gfx::Size& size) {
  336. extensions::SizeConstraints size_constraints = GetSizeConstraints();
  337. size_constraints.set_minimum_size(size);
  338. SetSizeConstraints(size_constraints);
  339. }
  340. gfx::Size NativeWindow::GetMinimumSize() const {
  341. return GetSizeConstraints().GetMinimumSize();
  342. }
  343. void NativeWindow::SetMaximumSize(const gfx::Size& size) {
  344. extensions::SizeConstraints size_constraints = GetSizeConstraints();
  345. size_constraints.set_maximum_size(size);
  346. SetSizeConstraints(size_constraints);
  347. }
  348. gfx::Size NativeWindow::GetMaximumSize() const {
  349. return GetSizeConstraints().GetMaximumSize();
  350. }
  351. gfx::Size NativeWindow::GetContentMinimumSize() const {
  352. return GetContentSizeConstraints().GetMinimumSize();
  353. }
  354. gfx::Size NativeWindow::GetContentMaximumSize() const {
  355. gfx::Size maximum_size = GetContentSizeConstraints().GetMaximumSize();
  356. #if BUILDFLAG(IS_WIN)
  357. return GetContentSizeConstraints().HasMaximumSize()
  358. ? GetExpandedWindowSize(this, maximum_size)
  359. : maximum_size;
  360. #else
  361. return maximum_size;
  362. #endif
  363. }
  364. void NativeWindow::SetSheetOffset(const double offsetX, const double offsetY) {
  365. sheet_offset_x_ = offsetX;
  366. sheet_offset_y_ = offsetY;
  367. }
  368. double NativeWindow::GetSheetOffsetX() const {
  369. return sheet_offset_x_;
  370. }
  371. double NativeWindow::GetSheetOffsetY() const {
  372. return sheet_offset_y_;
  373. }
  374. bool NativeWindow::IsTabletMode() const {
  375. return false;
  376. }
  377. void NativeWindow::SetRepresentedFilename(const std::string& filename) {}
  378. std::string NativeWindow::GetRepresentedFilename() const {
  379. return "";
  380. }
  381. void NativeWindow::SetDocumentEdited(bool edited) {}
  382. bool NativeWindow::IsDocumentEdited() const {
  383. return false;
  384. }
  385. void NativeWindow::SetFocusable(bool focusable) {}
  386. bool NativeWindow::IsFocusable() const {
  387. return false;
  388. }
  389. void NativeWindow::SetMenu(ElectronMenuModel* menu) {}
  390. void NativeWindow::SetParentWindow(NativeWindow* parent) {
  391. parent_ = parent;
  392. }
  393. void NativeWindow::InvalidateShadow() {}
  394. void NativeWindow::SetAutoHideCursor(bool auto_hide) {}
  395. void NativeWindow::SelectPreviousTab() {}
  396. void NativeWindow::SelectNextTab() {}
  397. void NativeWindow::ShowAllTabs() {}
  398. void NativeWindow::MergeAllWindows() {}
  399. void NativeWindow::MoveTabToNewWindow() {}
  400. void NativeWindow::ToggleTabBar() {}
  401. bool NativeWindow::AddTabbedWindow(NativeWindow* window) {
  402. return true; // for non-Mac platforms
  403. }
  404. std::optional<std::string> NativeWindow::GetTabbingIdentifier() const {
  405. return ""; // for non-Mac platforms
  406. }
  407. void NativeWindow::SetVibrancy(const std::string& type) {
  408. vibrancy_ = type;
  409. }
  410. void NativeWindow::SetBackgroundMaterial(const std::string& type) {
  411. background_material_ = type;
  412. }
  413. void NativeWindow::SetTouchBar(
  414. std::vector<gin_helper::PersistentDictionary> items) {}
  415. void NativeWindow::RefreshTouchBarItem(const std::string& item_id) {}
  416. void NativeWindow::SetEscapeTouchBarItem(
  417. gin_helper::PersistentDictionary item) {}
  418. void NativeWindow::SetAutoHideMenuBar(bool auto_hide) {}
  419. bool NativeWindow::IsMenuBarAutoHide() const {
  420. return false;
  421. }
  422. void NativeWindow::SetMenuBarVisibility(bool visible) {}
  423. bool NativeWindow::IsMenuBarVisible() const {
  424. return true;
  425. }
  426. void NativeWindow::SetAspectRatio(double aspect_ratio,
  427. const gfx::Size& extra_size) {
  428. aspect_ratio_ = aspect_ratio;
  429. aspect_ratio_extraSize_ = extra_size;
  430. }
  431. void NativeWindow::PreviewFile(const std::string& path,
  432. const std::string& display_name) {}
  433. void NativeWindow::CloseFilePreview() {}
  434. std::optional<gfx::Rect> NativeWindow::GetWindowControlsOverlayRect() {
  435. return overlay_rect_;
  436. }
  437. void NativeWindow::SetWindowControlsOverlayRect(const gfx::Rect& overlay_rect) {
  438. overlay_rect_ = overlay_rect;
  439. }
  440. void NativeWindow::NotifyWindowRequestPreferredWidth(int* width) {
  441. for (NativeWindowObserver& observer : observers_)
  442. observer.RequestPreferredWidth(width);
  443. }
  444. void NativeWindow::NotifyWindowCloseButtonClicked() {
  445. // First ask the observers whether we want to close.
  446. bool prevent_default = false;
  447. for (NativeWindowObserver& observer : observers_)
  448. observer.WillCloseWindow(&prevent_default);
  449. if (prevent_default) {
  450. WindowList::WindowCloseCancelled(this);
  451. return;
  452. }
  453. // Then ask the observers how should we close the window.
  454. for (NativeWindowObserver& observer : observers_)
  455. observer.OnCloseButtonClicked(&prevent_default);
  456. if (prevent_default)
  457. return;
  458. CloseImmediately();
  459. }
  460. void NativeWindow::NotifyWindowClosed() {
  461. if (is_closed_)
  462. return;
  463. is_closed_ = true;
  464. for (NativeWindowObserver& observer : observers_)
  465. observer.OnWindowClosed();
  466. WindowList::RemoveWindow(this);
  467. }
  468. void NativeWindow::NotifyWindowEndSession() {
  469. for (NativeWindowObserver& observer : observers_)
  470. observer.OnWindowEndSession();
  471. }
  472. void NativeWindow::NotifyWindowBlur() {
  473. for (NativeWindowObserver& observer : observers_)
  474. observer.OnWindowBlur();
  475. }
  476. void NativeWindow::NotifyWindowFocus() {
  477. for (NativeWindowObserver& observer : observers_)
  478. observer.OnWindowFocus();
  479. }
  480. void NativeWindow::NotifyWindowIsKeyChanged(bool is_key) {
  481. for (NativeWindowObserver& observer : observers_)
  482. observer.OnWindowIsKeyChanged(is_key);
  483. }
  484. void NativeWindow::NotifyWindowShow() {
  485. for (NativeWindowObserver& observer : observers_)
  486. observer.OnWindowShow();
  487. }
  488. void NativeWindow::NotifyWindowHide() {
  489. for (NativeWindowObserver& observer : observers_)
  490. observer.OnWindowHide();
  491. }
  492. void NativeWindow::NotifyWindowMaximize() {
  493. for (NativeWindowObserver& observer : observers_)
  494. observer.OnWindowMaximize();
  495. }
  496. void NativeWindow::NotifyWindowUnmaximize() {
  497. for (NativeWindowObserver& observer : observers_)
  498. observer.OnWindowUnmaximize();
  499. }
  500. void NativeWindow::NotifyWindowMinimize() {
  501. for (NativeWindowObserver& observer : observers_)
  502. observer.OnWindowMinimize();
  503. }
  504. void NativeWindow::NotifyWindowRestore() {
  505. for (NativeWindowObserver& observer : observers_)
  506. observer.OnWindowRestore();
  507. }
  508. void NativeWindow::NotifyWindowWillResize(const gfx::Rect& new_bounds,
  509. const gfx::ResizeEdge& edge,
  510. bool* prevent_default) {
  511. for (NativeWindowObserver& observer : observers_)
  512. observer.OnWindowWillResize(new_bounds, edge, prevent_default);
  513. }
  514. void NativeWindow::NotifyWindowWillMove(const gfx::Rect& new_bounds,
  515. bool* prevent_default) {
  516. for (NativeWindowObserver& observer : observers_)
  517. observer.OnWindowWillMove(new_bounds, prevent_default);
  518. }
  519. void NativeWindow::NotifyWindowResize() {
  520. NotifyLayoutWindowControlsOverlay();
  521. for (NativeWindowObserver& observer : observers_)
  522. observer.OnWindowResize();
  523. }
  524. void NativeWindow::NotifyWindowResized() {
  525. for (NativeWindowObserver& observer : observers_)
  526. observer.OnWindowResized();
  527. }
  528. void NativeWindow::NotifyWindowMove() {
  529. for (NativeWindowObserver& observer : observers_)
  530. observer.OnWindowMove();
  531. }
  532. void NativeWindow::NotifyWindowMoved() {
  533. for (NativeWindowObserver& observer : observers_)
  534. observer.OnWindowMoved();
  535. }
  536. void NativeWindow::NotifyWindowEnterFullScreen() {
  537. NotifyLayoutWindowControlsOverlay();
  538. for (NativeWindowObserver& observer : observers_)
  539. observer.OnWindowEnterFullScreen();
  540. }
  541. void NativeWindow::NotifyWindowSwipe(const std::string& direction) {
  542. for (NativeWindowObserver& observer : observers_)
  543. observer.OnWindowSwipe(direction);
  544. }
  545. void NativeWindow::NotifyWindowRotateGesture(float rotation) {
  546. for (NativeWindowObserver& observer : observers_)
  547. observer.OnWindowRotateGesture(rotation);
  548. }
  549. void NativeWindow::NotifyWindowSheetBegin() {
  550. for (NativeWindowObserver& observer : observers_)
  551. observer.OnWindowSheetBegin();
  552. }
  553. void NativeWindow::NotifyWindowSheetEnd() {
  554. for (NativeWindowObserver& observer : observers_)
  555. observer.OnWindowSheetEnd();
  556. }
  557. void NativeWindow::NotifyWindowLeaveFullScreen() {
  558. NotifyLayoutWindowControlsOverlay();
  559. for (NativeWindowObserver& observer : observers_)
  560. observer.OnWindowLeaveFullScreen();
  561. }
  562. void NativeWindow::NotifyWindowEnterHtmlFullScreen() {
  563. for (NativeWindowObserver& observer : observers_)
  564. observer.OnWindowEnterHtmlFullScreen();
  565. }
  566. void NativeWindow::NotifyWindowLeaveHtmlFullScreen() {
  567. for (NativeWindowObserver& observer : observers_)
  568. observer.OnWindowLeaveHtmlFullScreen();
  569. }
  570. void NativeWindow::NotifyWindowAlwaysOnTopChanged() {
  571. for (NativeWindowObserver& observer : observers_)
  572. observer.OnWindowAlwaysOnTopChanged();
  573. }
  574. void NativeWindow::NotifyWindowExecuteAppCommand(const std::string& command) {
  575. for (NativeWindowObserver& observer : observers_)
  576. observer.OnExecuteAppCommand(command);
  577. }
  578. void NativeWindow::NotifyTouchBarItemInteraction(const std::string& item_id,
  579. base::Value::Dict details) {
  580. for (NativeWindowObserver& observer : observers_)
  581. observer.OnTouchBarItemResult(item_id, details);
  582. }
  583. void NativeWindow::NotifyNewWindowForTab() {
  584. for (NativeWindowObserver& observer : observers_)
  585. observer.OnNewWindowForTab();
  586. }
  587. void NativeWindow::NotifyWindowSystemContextMenu(int x,
  588. int y,
  589. bool* prevent_default) {
  590. for (NativeWindowObserver& observer : observers_)
  591. observer.OnSystemContextMenu(x, y, prevent_default);
  592. }
  593. void NativeWindow::NotifyLayoutWindowControlsOverlay() {
  594. auto bounding_rect = GetWindowControlsOverlayRect();
  595. if (bounding_rect.has_value()) {
  596. for (NativeWindowObserver& observer : observers_)
  597. observer.UpdateWindowControlsOverlay(bounding_rect.value());
  598. }
  599. }
  600. #if BUILDFLAG(IS_WIN)
  601. void NativeWindow::NotifyWindowMessage(UINT message,
  602. WPARAM w_param,
  603. LPARAM l_param) {
  604. for (NativeWindowObserver& observer : observers_)
  605. observer.OnWindowMessage(message, w_param, l_param);
  606. }
  607. #endif
  608. int NativeWindow::NonClientHitTest(const gfx::Point& point) {
  609. #if !BUILDFLAG(IS_MAC)
  610. // We need to ensure we account for resizing borders on Windows and Linux.
  611. if ((!has_frame() || has_client_frame()) && IsResizable()) {
  612. auto* frame =
  613. static_cast<FramelessView*>(widget()->non_client_view()->frame_view());
  614. int border_hit = frame->ResizingBorderHitTest(point);
  615. if (border_hit != HTNOWHERE)
  616. return border_hit;
  617. }
  618. #endif
  619. // This is to disable dragging in HTML5 full screen mode.
  620. // Details: https://github.com/electron/electron/issues/41002
  621. if (GetWidget()->IsFullscreen())
  622. return HTNOWHERE;
  623. for (auto* provider : draggable_region_providers_) {
  624. int hit = provider->NonClientHitTest(point);
  625. if (hit != HTNOWHERE)
  626. return hit;
  627. }
  628. return HTNOWHERE;
  629. }
  630. void NativeWindow::AddDraggableRegionProvider(
  631. DraggableRegionProvider* provider) {
  632. if (!base::Contains(draggable_region_providers_, provider)) {
  633. draggable_region_providers_.push_back(provider);
  634. }
  635. }
  636. void NativeWindow::RemoveDraggableRegionProvider(
  637. DraggableRegionProvider* provider) {
  638. draggable_region_providers_.remove_if(
  639. [&provider](DraggableRegionProvider* p) { return p == provider; });
  640. }
  641. void NativeWindow::AddBackgroundThrottlingSource(
  642. BackgroundThrottlingSource* source) {
  643. auto result = background_throttling_sources_.insert(source);
  644. DCHECK(result.second) << "Added already stored BackgroundThrottlingSource.";
  645. UpdateBackgroundThrottlingState();
  646. }
  647. void NativeWindow::RemoveBackgroundThrottlingSource(
  648. BackgroundThrottlingSource* source) {
  649. auto result = background_throttling_sources_.erase(source);
  650. DCHECK(result == 1)
  651. << "Tried to remove non existing BackgroundThrottlingSource.";
  652. UpdateBackgroundThrottlingState();
  653. }
  654. void NativeWindow::UpdateBackgroundThrottlingState() {
  655. if (!GetWidget() || !GetWidget()->GetCompositor()) {
  656. return;
  657. }
  658. bool enable_background_throttling = true;
  659. for (const auto* background_throttling_source :
  660. background_throttling_sources_) {
  661. if (!background_throttling_source->GetBackgroundThrottling()) {
  662. enable_background_throttling = false;
  663. break;
  664. }
  665. }
  666. GetWidget()->GetCompositor()->SetBackgroundThrottling(
  667. enable_background_throttling);
  668. }
  669. views::Widget* NativeWindow::GetWidget() {
  670. return widget();
  671. }
  672. const views::Widget* NativeWindow::GetWidget() const {
  673. return widget();
  674. }
  675. std::u16string NativeWindow::GetAccessibleWindowTitle() const {
  676. if (accessible_title_.empty()) {
  677. return views::WidgetDelegate::GetAccessibleWindowTitle();
  678. }
  679. return accessible_title_;
  680. }
  681. void NativeWindow::SetAccessibleTitle(const std::string& title) {
  682. accessible_title_ = base::UTF8ToUTF16(title);
  683. }
  684. std::string NativeWindow::GetAccessibleTitle() {
  685. return base::UTF16ToUTF8(accessible_title_);
  686. }
  687. void NativeWindow::HandlePendingFullscreenTransitions() {
  688. if (pending_transitions_.empty()) {
  689. set_fullscreen_transition_type(FullScreenTransitionType::kNone);
  690. return;
  691. }
  692. bool next_transition = pending_transitions_.front();
  693. pending_transitions_.pop();
  694. SetFullScreen(next_transition);
  695. }
  696. // static
  697. int32_t NativeWindow::next_id_ = 0;
  698. bool NativeWindow::IsTranslucent() const {
  699. // Transparent windows are translucent
  700. if (transparent()) {
  701. return true;
  702. }
  703. #if BUILDFLAG(IS_MAC)
  704. // Windows with vibrancy set are translucent
  705. if (!vibrancy().empty()) {
  706. return true;
  707. }
  708. #endif
  709. #if BUILDFLAG(IS_WIN)
  710. // Windows with certain background materials may be translucent
  711. const std::string& bg_material = background_material();
  712. if (!bg_material.empty() && bg_material != "none") {
  713. return true;
  714. }
  715. #endif
  716. return false;
  717. }
  718. // static
  719. void NativeWindowRelay::CreateForWebContents(
  720. content::WebContents* web_contents,
  721. base::WeakPtr<NativeWindow> window) {
  722. DCHECK(web_contents);
  723. if (!web_contents->GetUserData(UserDataKey())) {
  724. web_contents->SetUserData(
  725. UserDataKey(),
  726. base::WrapUnique(new NativeWindowRelay(web_contents, window)));
  727. }
  728. }
  729. NativeWindowRelay::NativeWindowRelay(content::WebContents* web_contents,
  730. base::WeakPtr<NativeWindow> window)
  731. : content::WebContentsUserData<NativeWindowRelay>(*web_contents),
  732. native_window_(window) {}
  733. NativeWindowRelay::~NativeWindowRelay() = default;
  734. WEB_CONTENTS_USER_DATA_KEY_IMPL(NativeWindowRelay);
  735. } // namespace electron