native_window.cc 21 KB

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