native_window.cc 26 KB

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