native_window.cc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  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/memory/ptr_util.h"
  9. #include "base/strings/utf_string_conversions.h"
  10. #include "base/values.h"
  11. #include "content/public/browser/web_contents_user_data.h"
  12. #include "shell/browser/browser.h"
  13. #include "shell/browser/native_window_features.h"
  14. #include "shell/browser/window_list.h"
  15. #include "shell/common/color_util.h"
  16. #include "shell/common/gin_helper/dictionary.h"
  17. #include "shell/common/gin_helper/persistent_dictionary.h"
  18. #include "shell/common/options_switches.h"
  19. #include "ui/views/widget/widget.h"
  20. #if BUILDFLAG(IS_WIN)
  21. #include "ui/base/win/shell.h"
  22. #include "ui/display/win/screen_win.h"
  23. #endif
  24. #if defined(USE_OZONE)
  25. #include "ui/base/ui_base_features.h"
  26. #include "ui/ozone/public/ozone_platform.h"
  27. #endif
  28. namespace gin {
  29. template <>
  30. struct Converter<electron::NativeWindow::TitleBarStyle> {
  31. static bool FromV8(v8::Isolate* isolate,
  32. v8::Handle<v8::Value> val,
  33. electron::NativeWindow::TitleBarStyle* out) {
  34. using TitleBarStyle = electron::NativeWindow::TitleBarStyle;
  35. std::string title_bar_style;
  36. if (!ConvertFromV8(isolate, val, &title_bar_style))
  37. return false;
  38. if (title_bar_style == "hidden") {
  39. *out = TitleBarStyle::kHidden;
  40. #if BUILDFLAG(IS_MAC)
  41. } else if (title_bar_style == "hiddenInset") {
  42. *out = TitleBarStyle::kHiddenInset;
  43. } else if (title_bar_style == "customButtonsOnHover") {
  44. *out = TitleBarStyle::kCustomButtonsOnHover;
  45. #endif
  46. } else {
  47. return false;
  48. }
  49. return true;
  50. }
  51. };
  52. } // namespace gin
  53. namespace electron {
  54. namespace {
  55. #if BUILDFLAG(IS_WIN)
  56. gfx::Size GetExpandedWindowSize(const NativeWindow* window, gfx::Size size) {
  57. if (!window->transparent() || !ui::win::IsAeroGlassEnabled())
  58. return size;
  59. gfx::Size min_size = display::win::ScreenWin::ScreenToDIPSize(
  60. window->GetAcceleratedWidget(), gfx::Size(64, 64));
  61. // Some AMD drivers can't display windows that are less than 64x64 pixels,
  62. // so expand them to be at least that size. http://crbug.com/286609
  63. gfx::Size expanded(std::max(size.width(), min_size.width()),
  64. std::max(size.height(), min_size.height()));
  65. return expanded;
  66. }
  67. #endif
  68. } // namespace
  69. NativeWindow::NativeWindow(const gin_helper::Dictionary& options,
  70. NativeWindow* parent)
  71. : widget_(std::make_unique<views::Widget>()), parent_(parent) {
  72. ++next_id_;
  73. options.Get(options::kFrame, &has_frame_);
  74. options.Get(options::kTransparent, &transparent_);
  75. options.Get(options::kEnableLargerThanScreen, &enable_larger_than_screen_);
  76. options.Get(options::kTitleBarStyle, &title_bar_style_);
  77. v8::Local<v8::Value> titlebar_overlay;
  78. if (options.Get(options::ktitleBarOverlay, &titlebar_overlay)) {
  79. if (titlebar_overlay->IsBoolean()) {
  80. options.Get(options::ktitleBarOverlay, &titlebar_overlay_);
  81. } else if (titlebar_overlay->IsObject()) {
  82. titlebar_overlay_ = true;
  83. gin_helper::Dictionary titlebar_overlay_dict =
  84. gin::Dictionary::CreateEmpty(options.isolate());
  85. options.Get(options::ktitleBarOverlay, &titlebar_overlay_dict);
  86. int height;
  87. if (titlebar_overlay_dict.Get(options::kOverlayHeight, &height))
  88. titlebar_overlay_height_ = height;
  89. #if !(BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC))
  90. DCHECK(false);
  91. #endif
  92. }
  93. }
  94. if (parent)
  95. options.Get("modal", &is_modal_);
  96. #if defined(USE_OZONE)
  97. // Ozone X11 likes to prefer custom frames, but we don't need them unless
  98. // on Wayland.
  99. if (base::FeatureList::IsEnabled(features::kWaylandWindowDecorations) &&
  100. !ui::OzonePlatform::GetInstance()
  101. ->GetPlatformRuntimeProperties()
  102. .supports_server_side_window_decorations) {
  103. has_client_frame_ = true;
  104. }
  105. #endif
  106. WindowList::AddWindow(this);
  107. }
  108. NativeWindow::~NativeWindow() {
  109. // It's possible that the windows gets destroyed before it's closed, in that
  110. // case we need to ensure the Widget delegate gets destroyed and
  111. // OnWindowClosed message is still notified.
  112. if (widget_->widget_delegate())
  113. widget_->OnNativeWidgetDestroyed();
  114. NotifyWindowClosed();
  115. }
  116. void NativeWindow::InitFromOptions(const gin_helper::Dictionary& options) {
  117. // Setup window from options.
  118. int x = -1, y = -1;
  119. bool center;
  120. if (options.Get(options::kX, &x) && options.Get(options::kY, &y)) {
  121. SetPosition(gfx::Point(x, y));
  122. #if BUILDFLAG(IS_WIN)
  123. // FIXME(felixrieseberg): Dirty, dirty workaround for
  124. // https://github.com/electron/electron/issues/10862
  125. // Somehow, we need to call `SetBounds` twice to get
  126. // usable results. The root cause is still unknown.
  127. SetPosition(gfx::Point(x, y));
  128. #endif
  129. } else if (options.Get(options::kCenter, &center) && center) {
  130. Center();
  131. }
  132. bool use_content_size = false;
  133. options.Get(options::kUseContentSize, &use_content_size);
  134. // On Linux and Window we may already have maximum size defined.
  135. extensions::SizeConstraints size_constraints(
  136. use_content_size ? GetContentSizeConstraints() : GetSizeConstraints());
  137. int min_width = size_constraints.GetMinimumSize().width();
  138. int min_height = size_constraints.GetMinimumSize().height();
  139. options.Get(options::kMinWidth, &min_width);
  140. options.Get(options::kMinHeight, &min_height);
  141. size_constraints.set_minimum_size(gfx::Size(min_width, min_height));
  142. gfx::Size max_size = size_constraints.GetMaximumSize();
  143. int max_width = max_size.width() > 0 ? max_size.width() : INT_MAX;
  144. int max_height = max_size.height() > 0 ? max_size.height() : INT_MAX;
  145. bool have_max_width = options.Get(options::kMaxWidth, &max_width);
  146. if (have_max_width && max_width <= 0)
  147. max_width = INT_MAX;
  148. bool have_max_height = options.Get(options::kMaxHeight, &max_height);
  149. if (have_max_height && max_height <= 0)
  150. max_height = INT_MAX;
  151. // By default the window has a default maximum size that prevents it
  152. // from being resized larger than the screen, so we should only set this
  153. // if the user has passed in values.
  154. if (have_max_height || have_max_width || !max_size.IsEmpty())
  155. size_constraints.set_maximum_size(gfx::Size(max_width, max_height));
  156. if (use_content_size) {
  157. SetContentSizeConstraints(size_constraints);
  158. } else {
  159. SetSizeConstraints(size_constraints);
  160. }
  161. #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX)
  162. bool resizable;
  163. if (options.Get(options::kResizable, &resizable)) {
  164. SetResizable(resizable);
  165. }
  166. bool closable;
  167. if (options.Get(options::kClosable, &closable)) {
  168. SetClosable(closable);
  169. }
  170. #endif
  171. bool movable;
  172. if (options.Get(options::kMovable, &movable)) {
  173. SetMovable(movable);
  174. }
  175. bool has_shadow;
  176. if (options.Get(options::kHasShadow, &has_shadow)) {
  177. SetHasShadow(has_shadow);
  178. }
  179. double opacity;
  180. if (options.Get(options::kOpacity, &opacity)) {
  181. SetOpacity(opacity);
  182. }
  183. bool top;
  184. if (options.Get(options::kAlwaysOnTop, &top) && top) {
  185. SetAlwaysOnTop(ui::ZOrderLevel::kFloatingWindow);
  186. }
  187. bool fullscreenable = true;
  188. bool fullscreen = false;
  189. if (options.Get(options::kFullscreen, &fullscreen) && !fullscreen) {
  190. // Disable fullscreen button if 'fullscreen' is specified to false.
  191. #if BUILDFLAG(IS_MAC)
  192. fullscreenable = false;
  193. #endif
  194. }
  195. // Overridden by 'fullscreenable'.
  196. options.Get(options::kFullScreenable, &fullscreenable);
  197. SetFullScreenable(fullscreenable);
  198. if (fullscreen) {
  199. SetFullScreen(true);
  200. }
  201. bool skip;
  202. if (options.Get(options::kSkipTaskbar, &skip)) {
  203. SetSkipTaskbar(skip);
  204. }
  205. bool kiosk;
  206. if (options.Get(options::kKiosk, &kiosk) && kiosk) {
  207. SetKiosk(kiosk);
  208. }
  209. #if BUILDFLAG(IS_MAC)
  210. std::string type;
  211. if (options.Get(options::kVibrancyType, &type)) {
  212. SetVibrancy(type);
  213. }
  214. #endif
  215. std::string color;
  216. if (options.Get(options::kBackgroundColor, &color)) {
  217. SetBackgroundColor(ParseCSSColor(color));
  218. } else if (!transparent()) {
  219. // For normal window, use white as default background.
  220. SetBackgroundColor(SK_ColorWHITE);
  221. }
  222. std::string title(Browser::Get()->GetName());
  223. options.Get(options::kTitle, &title);
  224. SetTitle(title);
  225. // Then show it.
  226. bool show = true;
  227. options.Get(options::kShow, &show);
  228. if (show)
  229. Show();
  230. }
  231. bool NativeWindow::IsClosed() const {
  232. return is_closed_;
  233. }
  234. void NativeWindow::SetSize(const gfx::Size& size, bool animate) {
  235. SetBounds(gfx::Rect(GetPosition(), size), animate);
  236. }
  237. gfx::Size NativeWindow::GetSize() {
  238. return GetBounds().size();
  239. }
  240. void NativeWindow::SetPosition(const gfx::Point& position, bool animate) {
  241. SetBounds(gfx::Rect(position, GetSize()), animate);
  242. }
  243. gfx::Point NativeWindow::GetPosition() {
  244. return GetBounds().origin();
  245. }
  246. void NativeWindow::SetContentSize(const gfx::Size& size, bool animate) {
  247. SetSize(ContentBoundsToWindowBounds(gfx::Rect(size)).size(), animate);
  248. }
  249. gfx::Size NativeWindow::GetContentSize() {
  250. return GetContentBounds().size();
  251. }
  252. void NativeWindow::SetContentBounds(const gfx::Rect& bounds, bool animate) {
  253. SetBounds(ContentBoundsToWindowBounds(bounds), animate);
  254. }
  255. gfx::Rect NativeWindow::GetContentBounds() {
  256. return WindowBoundsToContentBounds(GetBounds());
  257. }
  258. bool NativeWindow::IsNormal() {
  259. return !IsMinimized() && !IsMaximized() && !IsFullscreen();
  260. }
  261. void NativeWindow::SetSizeConstraints(
  262. const extensions::SizeConstraints& window_constraints) {
  263. extensions::SizeConstraints content_constraints(GetContentSizeConstraints());
  264. if (window_constraints.HasMaximumSize()) {
  265. gfx::Rect max_bounds = WindowBoundsToContentBounds(
  266. gfx::Rect(window_constraints.GetMaximumSize()));
  267. content_constraints.set_maximum_size(max_bounds.size());
  268. }
  269. if (window_constraints.HasMinimumSize()) {
  270. gfx::Rect min_bounds = WindowBoundsToContentBounds(
  271. gfx::Rect(window_constraints.GetMinimumSize()));
  272. content_constraints.set_minimum_size(min_bounds.size());
  273. }
  274. SetContentSizeConstraints(content_constraints);
  275. }
  276. extensions::SizeConstraints NativeWindow::GetSizeConstraints() const {
  277. extensions::SizeConstraints content_constraints = GetContentSizeConstraints();
  278. extensions::SizeConstraints window_constraints;
  279. if (content_constraints.HasMaximumSize()) {
  280. gfx::Rect max_bounds = ContentBoundsToWindowBounds(
  281. gfx::Rect(content_constraints.GetMaximumSize()));
  282. window_constraints.set_maximum_size(max_bounds.size());
  283. }
  284. if (content_constraints.HasMinimumSize()) {
  285. gfx::Rect min_bounds = ContentBoundsToWindowBounds(
  286. gfx::Rect(content_constraints.GetMinimumSize()));
  287. window_constraints.set_minimum_size(min_bounds.size());
  288. }
  289. return window_constraints;
  290. }
  291. void NativeWindow::SetContentSizeConstraints(
  292. const extensions::SizeConstraints& size_constraints) {
  293. size_constraints_ = size_constraints;
  294. }
  295. extensions::SizeConstraints NativeWindow::GetContentSizeConstraints() const {
  296. return size_constraints_;
  297. }
  298. void NativeWindow::SetMinimumSize(const gfx::Size& size) {
  299. extensions::SizeConstraints size_constraints;
  300. size_constraints.set_minimum_size(size);
  301. SetSizeConstraints(size_constraints);
  302. }
  303. gfx::Size NativeWindow::GetMinimumSize() const {
  304. return GetSizeConstraints().GetMinimumSize();
  305. }
  306. void NativeWindow::SetMaximumSize(const gfx::Size& size) {
  307. extensions::SizeConstraints size_constraints;
  308. size_constraints.set_maximum_size(size);
  309. SetSizeConstraints(size_constraints);
  310. }
  311. gfx::Size NativeWindow::GetMaximumSize() const {
  312. return GetSizeConstraints().GetMaximumSize();
  313. }
  314. gfx::Size NativeWindow::GetContentMinimumSize() const {
  315. return GetContentSizeConstraints().GetMinimumSize();
  316. }
  317. gfx::Size NativeWindow::GetContentMaximumSize() const {
  318. gfx::Size maximum_size = GetContentSizeConstraints().GetMaximumSize();
  319. #if BUILDFLAG(IS_WIN)
  320. return GetContentSizeConstraints().HasMaximumSize()
  321. ? GetExpandedWindowSize(this, maximum_size)
  322. : maximum_size;
  323. #else
  324. return maximum_size;
  325. #endif
  326. }
  327. void NativeWindow::SetSheetOffset(const double offsetX, const double offsetY) {
  328. sheet_offset_x_ = offsetX;
  329. sheet_offset_y_ = offsetY;
  330. }
  331. double NativeWindow::GetSheetOffsetX() {
  332. return sheet_offset_x_;
  333. }
  334. double NativeWindow::GetSheetOffsetY() {
  335. return sheet_offset_y_;
  336. }
  337. bool NativeWindow::IsTabletMode() const {
  338. return false;
  339. }
  340. void NativeWindow::SetRepresentedFilename(const std::string& filename) {}
  341. std::string NativeWindow::GetRepresentedFilename() {
  342. return "";
  343. }
  344. void NativeWindow::SetDocumentEdited(bool edited) {}
  345. bool NativeWindow::IsDocumentEdited() {
  346. return false;
  347. }
  348. void NativeWindow::SetFocusable(bool focusable) {}
  349. bool NativeWindow::IsFocusable() {
  350. return false;
  351. }
  352. void NativeWindow::SetMenu(ElectronMenuModel* menu) {}
  353. void NativeWindow::SetParentWindow(NativeWindow* parent) {
  354. parent_ = parent;
  355. }
  356. void NativeWindow::SetAutoHideCursor(bool auto_hide) {}
  357. void NativeWindow::SelectPreviousTab() {}
  358. void NativeWindow::SelectNextTab() {}
  359. void NativeWindow::MergeAllWindows() {}
  360. void NativeWindow::MoveTabToNewWindow() {}
  361. void NativeWindow::ToggleTabBar() {}
  362. bool NativeWindow::AddTabbedWindow(NativeWindow* window) {
  363. return true; // for non-Mac platforms
  364. }
  365. void NativeWindow::SetVibrancy(const std::string& type) {}
  366. void NativeWindow::SetTouchBar(
  367. std::vector<gin_helper::PersistentDictionary> items) {}
  368. void NativeWindow::RefreshTouchBarItem(const std::string& item_id) {}
  369. void NativeWindow::SetEscapeTouchBarItem(
  370. gin_helper::PersistentDictionary item) {}
  371. void NativeWindow::SetAutoHideMenuBar(bool auto_hide) {}
  372. bool NativeWindow::IsMenuBarAutoHide() {
  373. return false;
  374. }
  375. void NativeWindow::SetMenuBarVisibility(bool visible) {}
  376. bool NativeWindow::IsMenuBarVisible() {
  377. return true;
  378. }
  379. double NativeWindow::GetAspectRatio() {
  380. return aspect_ratio_;
  381. }
  382. gfx::Size NativeWindow::GetAspectRatioExtraSize() {
  383. return aspect_ratio_extraSize_;
  384. }
  385. void NativeWindow::SetAspectRatio(double aspect_ratio,
  386. const gfx::Size& extra_size) {
  387. aspect_ratio_ = aspect_ratio;
  388. aspect_ratio_extraSize_ = extra_size;
  389. }
  390. void NativeWindow::PreviewFile(const std::string& path,
  391. const std::string& display_name) {}
  392. void NativeWindow::CloseFilePreview() {}
  393. gfx::Rect NativeWindow::GetWindowControlsOverlayRect() {
  394. return overlay_rect_;
  395. }
  396. void NativeWindow::SetWindowControlsOverlayRect(const gfx::Rect& overlay_rect) {
  397. overlay_rect_ = overlay_rect;
  398. }
  399. void NativeWindow::NotifyWindowRequestPreferredWidth(int* width) {
  400. for (NativeWindowObserver& observer : observers_)
  401. observer.RequestPreferredWidth(width);
  402. }
  403. void NativeWindow::NotifyWindowCloseButtonClicked() {
  404. // First ask the observers whether we want to close.
  405. bool prevent_default = false;
  406. for (NativeWindowObserver& observer : observers_)
  407. observer.WillCloseWindow(&prevent_default);
  408. if (prevent_default) {
  409. WindowList::WindowCloseCancelled(this);
  410. return;
  411. }
  412. // Then ask the observers how should we close the window.
  413. for (NativeWindowObserver& observer : observers_)
  414. observer.OnCloseButtonClicked(&prevent_default);
  415. if (prevent_default)
  416. return;
  417. CloseImmediately();
  418. }
  419. void NativeWindow::NotifyWindowClosed() {
  420. if (is_closed_)
  421. return;
  422. is_closed_ = true;
  423. for (NativeWindowObserver& observer : observers_)
  424. observer.OnWindowClosed();
  425. WindowList::RemoveWindow(this);
  426. }
  427. void NativeWindow::NotifyWindowEndSession() {
  428. for (NativeWindowObserver& observer : observers_)
  429. observer.OnWindowEndSession();
  430. }
  431. void NativeWindow::NotifyWindowBlur() {
  432. for (NativeWindowObserver& observer : observers_)
  433. observer.OnWindowBlur();
  434. }
  435. void NativeWindow::NotifyWindowFocus() {
  436. for (NativeWindowObserver& observer : observers_)
  437. observer.OnWindowFocus();
  438. }
  439. void NativeWindow::NotifyWindowIsKeyChanged(bool is_key) {
  440. for (NativeWindowObserver& observer : observers_)
  441. observer.OnWindowIsKeyChanged(is_key);
  442. }
  443. void NativeWindow::NotifyWindowShow() {
  444. for (NativeWindowObserver& observer : observers_)
  445. observer.OnWindowShow();
  446. }
  447. void NativeWindow::NotifyWindowHide() {
  448. for (NativeWindowObserver& observer : observers_)
  449. observer.OnWindowHide();
  450. }
  451. void NativeWindow::NotifyWindowMaximize() {
  452. for (NativeWindowObserver& observer : observers_)
  453. observer.OnWindowMaximize();
  454. }
  455. void NativeWindow::NotifyWindowUnmaximize() {
  456. for (NativeWindowObserver& observer : observers_)
  457. observer.OnWindowUnmaximize();
  458. }
  459. void NativeWindow::NotifyWindowMinimize() {
  460. for (NativeWindowObserver& observer : observers_)
  461. observer.OnWindowMinimize();
  462. }
  463. void NativeWindow::NotifyWindowRestore() {
  464. for (NativeWindowObserver& observer : observers_)
  465. observer.OnWindowRestore();
  466. }
  467. void NativeWindow::NotifyWindowWillResize(const gfx::Rect& new_bounds,
  468. const gfx::ResizeEdge& edge,
  469. bool* prevent_default) {
  470. for (NativeWindowObserver& observer : observers_)
  471. observer.OnWindowWillResize(new_bounds, edge, prevent_default);
  472. }
  473. void NativeWindow::NotifyWindowWillMove(const gfx::Rect& new_bounds,
  474. bool* prevent_default) {
  475. for (NativeWindowObserver& observer : observers_)
  476. observer.OnWindowWillMove(new_bounds, prevent_default);
  477. }
  478. void NativeWindow::NotifyWindowResize() {
  479. NotifyLayoutWindowControlsOverlay();
  480. for (NativeWindowObserver& observer : observers_)
  481. observer.OnWindowResize();
  482. }
  483. void NativeWindow::NotifyWindowResized() {
  484. for (NativeWindowObserver& observer : observers_)
  485. observer.OnWindowResized();
  486. }
  487. void NativeWindow::NotifyWindowMove() {
  488. for (NativeWindowObserver& observer : observers_)
  489. observer.OnWindowMove();
  490. }
  491. void NativeWindow::NotifyWindowMoved() {
  492. for (NativeWindowObserver& observer : observers_)
  493. observer.OnWindowMoved();
  494. }
  495. void NativeWindow::NotifyWindowEnterFullScreen() {
  496. for (NativeWindowObserver& observer : observers_)
  497. observer.OnWindowEnterFullScreen();
  498. }
  499. void NativeWindow::NotifyWindowSwipe(const std::string& direction) {
  500. for (NativeWindowObserver& observer : observers_)
  501. observer.OnWindowSwipe(direction);
  502. }
  503. void NativeWindow::NotifyWindowRotateGesture(float rotation) {
  504. for (NativeWindowObserver& observer : observers_)
  505. observer.OnWindowRotateGesture(rotation);
  506. }
  507. void NativeWindow::NotifyWindowSheetBegin() {
  508. for (NativeWindowObserver& observer : observers_)
  509. observer.OnWindowSheetBegin();
  510. }
  511. void NativeWindow::NotifyWindowSheetEnd() {
  512. for (NativeWindowObserver& observer : observers_)
  513. observer.OnWindowSheetEnd();
  514. }
  515. void NativeWindow::NotifyWindowLeaveFullScreen() {
  516. for (NativeWindowObserver& observer : observers_)
  517. observer.OnWindowLeaveFullScreen();
  518. }
  519. void NativeWindow::NotifyWindowEnterHtmlFullScreen() {
  520. for (NativeWindowObserver& observer : observers_)
  521. observer.OnWindowEnterHtmlFullScreen();
  522. }
  523. void NativeWindow::NotifyWindowLeaveHtmlFullScreen() {
  524. for (NativeWindowObserver& observer : observers_)
  525. observer.OnWindowLeaveHtmlFullScreen();
  526. }
  527. void NativeWindow::NotifyWindowAlwaysOnTopChanged() {
  528. for (NativeWindowObserver& observer : observers_)
  529. observer.OnWindowAlwaysOnTopChanged();
  530. }
  531. void NativeWindow::NotifyWindowExecuteAppCommand(const std::string& command) {
  532. for (NativeWindowObserver& observer : observers_)
  533. observer.OnExecuteAppCommand(command);
  534. }
  535. void NativeWindow::NotifyTouchBarItemInteraction(const std::string& item_id,
  536. base::Value::Dict details) {
  537. for (NativeWindowObserver& observer : observers_)
  538. observer.OnTouchBarItemResult(item_id, details);
  539. }
  540. void NativeWindow::NotifyNewWindowForTab() {
  541. for (NativeWindowObserver& observer : observers_)
  542. observer.OnNewWindowForTab();
  543. }
  544. void NativeWindow::NotifyWindowSystemContextMenu(int x,
  545. int y,
  546. bool* prevent_default) {
  547. for (NativeWindowObserver& observer : observers_)
  548. observer.OnSystemContextMenu(x, y, prevent_default);
  549. }
  550. void NativeWindow::NotifyLayoutWindowControlsOverlay() {
  551. gfx::Rect bounding_rect = GetWindowControlsOverlayRect();
  552. if (!bounding_rect.IsEmpty()) {
  553. for (NativeWindowObserver& observer : observers_)
  554. observer.UpdateWindowControlsOverlay(bounding_rect);
  555. }
  556. }
  557. #if BUILDFLAG(IS_WIN)
  558. void NativeWindow::NotifyWindowMessage(UINT message,
  559. WPARAM w_param,
  560. LPARAM l_param) {
  561. for (NativeWindowObserver& observer : observers_)
  562. observer.OnWindowMessage(message, w_param, l_param);
  563. }
  564. #endif
  565. views::Widget* NativeWindow::GetWidget() {
  566. return widget();
  567. }
  568. const views::Widget* NativeWindow::GetWidget() const {
  569. return widget();
  570. }
  571. std::u16string NativeWindow::GetAccessibleWindowTitle() const {
  572. if (accessible_title_.empty()) {
  573. return views::WidgetDelegate::GetAccessibleWindowTitle();
  574. }
  575. return accessible_title_;
  576. }
  577. void NativeWindow::SetAccessibleTitle(const std::string& title) {
  578. accessible_title_ = base::UTF8ToUTF16(title);
  579. }
  580. std::string NativeWindow::GetAccessibleTitle() {
  581. return base::UTF16ToUTF8(accessible_title_);
  582. }
  583. void NativeWindow::HandlePendingFullscreenTransitions() {
  584. if (pending_transitions_.empty()) {
  585. set_fullscreen_transition_type(FullScreenTransitionType::NONE);
  586. return;
  587. }
  588. bool next_transition = pending_transitions_.front();
  589. pending_transitions_.pop();
  590. SetFullScreen(next_transition);
  591. }
  592. // static
  593. int32_t NativeWindow::next_id_ = 0;
  594. // static
  595. void NativeWindowRelay::CreateForWebContents(
  596. content::WebContents* web_contents,
  597. base::WeakPtr<NativeWindow> window) {
  598. DCHECK(web_contents);
  599. if (!web_contents->GetUserData(UserDataKey())) {
  600. web_contents->SetUserData(
  601. UserDataKey(),
  602. base::WrapUnique(new NativeWindowRelay(web_contents, window)));
  603. }
  604. }
  605. NativeWindowRelay::NativeWindowRelay(content::WebContents* web_contents,
  606. base::WeakPtr<NativeWindow> window)
  607. : content::WebContentsUserData<NativeWindowRelay>(*web_contents),
  608. native_window_(window) {}
  609. NativeWindowRelay::~NativeWindowRelay() = default;
  610. WEB_CONTENTS_USER_DATA_KEY_IMPL(NativeWindowRelay);
  611. } // namespace electron