native_window.cc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  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 =
  84. gin::Dictionary::CreateEmpty(options.isolate());
  85. options.Get(options::ktitleBarOverlay, &titlebar_overlay);
  86. int height;
  87. if (titlebar_overlay.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. bool have_max_height = options.Get(options::kMaxHeight, &max_height);
  147. // By default the window has a default maximum size that prevents it
  148. // from being resized larger than the screen, so we should only set this
  149. // if th user has passed in values.
  150. if (have_max_height || have_max_width || !max_size.IsEmpty())
  151. size_constraints.set_maximum_size(gfx::Size(max_width, max_height));
  152. if (use_content_size) {
  153. SetContentSizeConstraints(size_constraints);
  154. } else {
  155. SetSizeConstraints(size_constraints);
  156. }
  157. #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX)
  158. bool resizable;
  159. if (options.Get(options::kResizable, &resizable)) {
  160. SetResizable(resizable);
  161. }
  162. bool closable;
  163. if (options.Get(options::kClosable, &closable)) {
  164. SetClosable(closable);
  165. }
  166. #endif
  167. bool movable;
  168. if (options.Get(options::kMovable, &movable)) {
  169. SetMovable(movable);
  170. }
  171. bool has_shadow;
  172. if (options.Get(options::kHasShadow, &has_shadow)) {
  173. SetHasShadow(has_shadow);
  174. }
  175. double opacity;
  176. if (options.Get(options::kOpacity, &opacity)) {
  177. SetOpacity(opacity);
  178. }
  179. bool top;
  180. if (options.Get(options::kAlwaysOnTop, &top) && top) {
  181. SetAlwaysOnTop(ui::ZOrderLevel::kFloatingWindow);
  182. }
  183. bool fullscreenable = true;
  184. bool fullscreen = false;
  185. if (options.Get(options::kFullscreen, &fullscreen) && !fullscreen) {
  186. // Disable fullscreen button if 'fullscreen' is specified to false.
  187. #if BUILDFLAG(IS_MAC)
  188. fullscreenable = false;
  189. #endif
  190. }
  191. // Overridden by 'fullscreenable'.
  192. options.Get(options::kFullScreenable, &fullscreenable);
  193. SetFullScreenable(fullscreenable);
  194. if (fullscreen) {
  195. SetFullScreen(true);
  196. }
  197. bool skip;
  198. if (options.Get(options::kSkipTaskbar, &skip)) {
  199. SetSkipTaskbar(skip);
  200. }
  201. bool kiosk;
  202. if (options.Get(options::kKiosk, &kiosk) && kiosk) {
  203. SetKiosk(kiosk);
  204. }
  205. #if BUILDFLAG(IS_MAC)
  206. std::string type;
  207. if (options.Get(options::kVibrancyType, &type)) {
  208. SetVibrancy(type);
  209. }
  210. #endif
  211. std::string color;
  212. if (options.Get(options::kBackgroundColor, &color)) {
  213. SetBackgroundColor(ParseCSSColor(color));
  214. } else if (!transparent()) {
  215. // For normal window, use white as default background.
  216. SetBackgroundColor(SK_ColorWHITE);
  217. }
  218. std::string title(Browser::Get()->GetName());
  219. options.Get(options::kTitle, &title);
  220. SetTitle(title);
  221. // Then show it.
  222. bool show = true;
  223. options.Get(options::kShow, &show);
  224. if (show)
  225. Show();
  226. }
  227. bool NativeWindow::IsClosed() const {
  228. return is_closed_;
  229. }
  230. void NativeWindow::SetSize(const gfx::Size& size, bool animate) {
  231. SetBounds(gfx::Rect(GetPosition(), size), animate);
  232. }
  233. gfx::Size NativeWindow::GetSize() {
  234. return GetBounds().size();
  235. }
  236. void NativeWindow::SetPosition(const gfx::Point& position, bool animate) {
  237. SetBounds(gfx::Rect(position, GetSize()), animate);
  238. }
  239. gfx::Point NativeWindow::GetPosition() {
  240. return GetBounds().origin();
  241. }
  242. void NativeWindow::SetContentSize(const gfx::Size& size, bool animate) {
  243. SetSize(ContentBoundsToWindowBounds(gfx::Rect(size)).size(), animate);
  244. }
  245. gfx::Size NativeWindow::GetContentSize() {
  246. return GetContentBounds().size();
  247. }
  248. void NativeWindow::SetContentBounds(const gfx::Rect& bounds, bool animate) {
  249. SetBounds(ContentBoundsToWindowBounds(bounds), animate);
  250. }
  251. gfx::Rect NativeWindow::GetContentBounds() {
  252. return WindowBoundsToContentBounds(GetBounds());
  253. }
  254. bool NativeWindow::IsNormal() {
  255. return !IsMinimized() && !IsMaximized() && !IsFullscreen();
  256. }
  257. void NativeWindow::SetSizeConstraints(
  258. const extensions::SizeConstraints& window_constraints) {
  259. extensions::SizeConstraints content_constraints(GetContentSizeConstraints());
  260. if (window_constraints.HasMaximumSize()) {
  261. gfx::Rect max_bounds = WindowBoundsToContentBounds(
  262. gfx::Rect(window_constraints.GetMaximumSize()));
  263. content_constraints.set_maximum_size(max_bounds.size());
  264. }
  265. if (window_constraints.HasMinimumSize()) {
  266. gfx::Rect min_bounds = WindowBoundsToContentBounds(
  267. gfx::Rect(window_constraints.GetMinimumSize()));
  268. content_constraints.set_minimum_size(min_bounds.size());
  269. }
  270. SetContentSizeConstraints(content_constraints);
  271. }
  272. extensions::SizeConstraints NativeWindow::GetSizeConstraints() const {
  273. extensions::SizeConstraints content_constraints = GetContentSizeConstraints();
  274. extensions::SizeConstraints window_constraints;
  275. if (content_constraints.HasMaximumSize()) {
  276. gfx::Rect max_bounds = ContentBoundsToWindowBounds(
  277. gfx::Rect(content_constraints.GetMaximumSize()));
  278. window_constraints.set_maximum_size(max_bounds.size());
  279. }
  280. if (content_constraints.HasMinimumSize()) {
  281. gfx::Rect min_bounds = ContentBoundsToWindowBounds(
  282. gfx::Rect(content_constraints.GetMinimumSize()));
  283. window_constraints.set_minimum_size(min_bounds.size());
  284. }
  285. return window_constraints;
  286. }
  287. void NativeWindow::SetContentSizeConstraints(
  288. const extensions::SizeConstraints& size_constraints) {
  289. size_constraints_ = size_constraints;
  290. }
  291. extensions::SizeConstraints NativeWindow::GetContentSizeConstraints() const {
  292. return size_constraints_;
  293. }
  294. void NativeWindow::SetMinimumSize(const gfx::Size& size) {
  295. extensions::SizeConstraints size_constraints;
  296. size_constraints.set_minimum_size(size);
  297. SetSizeConstraints(size_constraints);
  298. }
  299. gfx::Size NativeWindow::GetMinimumSize() const {
  300. return GetSizeConstraints().GetMinimumSize();
  301. }
  302. void NativeWindow::SetMaximumSize(const gfx::Size& size) {
  303. extensions::SizeConstraints size_constraints;
  304. size_constraints.set_maximum_size(size);
  305. SetSizeConstraints(size_constraints);
  306. }
  307. gfx::Size NativeWindow::GetMaximumSize() const {
  308. return GetSizeConstraints().GetMaximumSize();
  309. }
  310. gfx::Size NativeWindow::GetContentMinimumSize() const {
  311. return GetContentSizeConstraints().GetMinimumSize();
  312. }
  313. gfx::Size NativeWindow::GetContentMaximumSize() const {
  314. gfx::Size maximum_size = GetContentSizeConstraints().GetMaximumSize();
  315. #if BUILDFLAG(IS_WIN)
  316. return GetContentSizeConstraints().HasMaximumSize()
  317. ? GetExpandedWindowSize(this, maximum_size)
  318. : maximum_size;
  319. #else
  320. return maximum_size;
  321. #endif
  322. }
  323. void NativeWindow::SetSheetOffset(const double offsetX, const double offsetY) {
  324. sheet_offset_x_ = offsetX;
  325. sheet_offset_y_ = offsetY;
  326. }
  327. double NativeWindow::GetSheetOffsetX() {
  328. return sheet_offset_x_;
  329. }
  330. double NativeWindow::GetSheetOffsetY() {
  331. return sheet_offset_y_;
  332. }
  333. bool NativeWindow::IsTabletMode() const {
  334. return false;
  335. }
  336. void NativeWindow::SetRepresentedFilename(const std::string& filename) {}
  337. std::string NativeWindow::GetRepresentedFilename() {
  338. return "";
  339. }
  340. void NativeWindow::SetDocumentEdited(bool edited) {}
  341. bool NativeWindow::IsDocumentEdited() {
  342. return false;
  343. }
  344. void NativeWindow::SetFocusable(bool focusable) {}
  345. bool NativeWindow::IsFocusable() {
  346. return false;
  347. }
  348. void NativeWindow::SetMenu(ElectronMenuModel* menu) {}
  349. void NativeWindow::SetParentWindow(NativeWindow* parent) {
  350. parent_ = parent;
  351. }
  352. void NativeWindow::SetAutoHideCursor(bool auto_hide) {}
  353. void NativeWindow::SelectPreviousTab() {}
  354. void NativeWindow::SelectNextTab() {}
  355. void NativeWindow::MergeAllWindows() {}
  356. void NativeWindow::MoveTabToNewWindow() {}
  357. void NativeWindow::ToggleTabBar() {}
  358. bool NativeWindow::AddTabbedWindow(NativeWindow* window) {
  359. return true; // for non-Mac platforms
  360. }
  361. void NativeWindow::SetVibrancy(const std::string& type) {}
  362. void NativeWindow::SetTouchBar(
  363. std::vector<gin_helper::PersistentDictionary> items) {}
  364. void NativeWindow::RefreshTouchBarItem(const std::string& item_id) {}
  365. void NativeWindow::SetEscapeTouchBarItem(
  366. gin_helper::PersistentDictionary item) {}
  367. void NativeWindow::SetAutoHideMenuBar(bool auto_hide) {}
  368. bool NativeWindow::IsMenuBarAutoHide() {
  369. return false;
  370. }
  371. void NativeWindow::SetMenuBarVisibility(bool visible) {}
  372. bool NativeWindow::IsMenuBarVisible() {
  373. return true;
  374. }
  375. double NativeWindow::GetAspectRatio() {
  376. return aspect_ratio_;
  377. }
  378. gfx::Size NativeWindow::GetAspectRatioExtraSize() {
  379. return aspect_ratio_extraSize_;
  380. }
  381. void NativeWindow::SetAspectRatio(double aspect_ratio,
  382. const gfx::Size& extra_size) {
  383. aspect_ratio_ = aspect_ratio;
  384. aspect_ratio_extraSize_ = extra_size;
  385. }
  386. void NativeWindow::PreviewFile(const std::string& path,
  387. const std::string& display_name) {}
  388. void NativeWindow::CloseFilePreview() {}
  389. gfx::Rect NativeWindow::GetWindowControlsOverlayRect() {
  390. return overlay_rect_;
  391. }
  392. void NativeWindow::SetWindowControlsOverlayRect(const gfx::Rect& overlay_rect) {
  393. overlay_rect_ = overlay_rect;
  394. }
  395. void NativeWindow::NotifyWindowRequestPreferredWidth(int* width) {
  396. for (NativeWindowObserver& observer : observers_)
  397. observer.RequestPreferredWidth(width);
  398. }
  399. void NativeWindow::NotifyWindowCloseButtonClicked() {
  400. // First ask the observers whether we want to close.
  401. bool prevent_default = false;
  402. for (NativeWindowObserver& observer : observers_)
  403. observer.WillCloseWindow(&prevent_default);
  404. if (prevent_default) {
  405. WindowList::WindowCloseCancelled(this);
  406. return;
  407. }
  408. // Then ask the observers how should we close the window.
  409. for (NativeWindowObserver& observer : observers_)
  410. observer.OnCloseButtonClicked(&prevent_default);
  411. if (prevent_default)
  412. return;
  413. CloseImmediately();
  414. }
  415. void NativeWindow::NotifyWindowClosed() {
  416. if (is_closed_)
  417. return;
  418. is_closed_ = true;
  419. for (NativeWindowObserver& observer : observers_)
  420. observer.OnWindowClosed();
  421. WindowList::RemoveWindow(this);
  422. }
  423. void NativeWindow::NotifyWindowEndSession() {
  424. for (NativeWindowObserver& observer : observers_)
  425. observer.OnWindowEndSession();
  426. }
  427. void NativeWindow::NotifyWindowBlur() {
  428. for (NativeWindowObserver& observer : observers_)
  429. observer.OnWindowBlur();
  430. }
  431. void NativeWindow::NotifyWindowFocus() {
  432. for (NativeWindowObserver& observer : observers_)
  433. observer.OnWindowFocus();
  434. }
  435. void NativeWindow::NotifyWindowIsKeyChanged(bool is_key) {
  436. for (NativeWindowObserver& observer : observers_)
  437. observer.OnWindowIsKeyChanged(is_key);
  438. }
  439. void NativeWindow::NotifyWindowShow() {
  440. for (NativeWindowObserver& observer : observers_)
  441. observer.OnWindowShow();
  442. }
  443. void NativeWindow::NotifyWindowHide() {
  444. for (NativeWindowObserver& observer : observers_)
  445. observer.OnWindowHide();
  446. }
  447. void NativeWindow::NotifyWindowMaximize() {
  448. for (NativeWindowObserver& observer : observers_)
  449. observer.OnWindowMaximize();
  450. }
  451. void NativeWindow::NotifyWindowUnmaximize() {
  452. for (NativeWindowObserver& observer : observers_)
  453. observer.OnWindowUnmaximize();
  454. }
  455. void NativeWindow::NotifyWindowMinimize() {
  456. for (NativeWindowObserver& observer : observers_)
  457. observer.OnWindowMinimize();
  458. }
  459. void NativeWindow::NotifyWindowRestore() {
  460. for (NativeWindowObserver& observer : observers_)
  461. observer.OnWindowRestore();
  462. }
  463. void NativeWindow::NotifyWindowWillResize(const gfx::Rect& new_bounds,
  464. const gfx::ResizeEdge& edge,
  465. bool* prevent_default) {
  466. for (NativeWindowObserver& observer : observers_)
  467. observer.OnWindowWillResize(new_bounds, edge, prevent_default);
  468. }
  469. void NativeWindow::NotifyWindowWillMove(const gfx::Rect& new_bounds,
  470. bool* prevent_default) {
  471. for (NativeWindowObserver& observer : observers_)
  472. observer.OnWindowWillMove(new_bounds, prevent_default);
  473. }
  474. void NativeWindow::NotifyWindowResize() {
  475. NotifyLayoutWindowControlsOverlay();
  476. for (NativeWindowObserver& observer : observers_)
  477. observer.OnWindowResize();
  478. }
  479. void NativeWindow::NotifyWindowResized() {
  480. for (NativeWindowObserver& observer : observers_)
  481. observer.OnWindowResized();
  482. }
  483. void NativeWindow::NotifyWindowMove() {
  484. for (NativeWindowObserver& observer : observers_)
  485. observer.OnWindowMove();
  486. }
  487. void NativeWindow::NotifyWindowMoved() {
  488. for (NativeWindowObserver& observer : observers_)
  489. observer.OnWindowMoved();
  490. }
  491. void NativeWindow::NotifyWindowEnterFullScreen() {
  492. for (NativeWindowObserver& observer : observers_)
  493. observer.OnWindowEnterFullScreen();
  494. }
  495. void NativeWindow::NotifyWindowScrollTouchBegin() {
  496. for (NativeWindowObserver& observer : observers_)
  497. observer.OnWindowScrollTouchBegin();
  498. }
  499. void NativeWindow::NotifyWindowScrollTouchEnd() {
  500. for (NativeWindowObserver& observer : observers_)
  501. observer.OnWindowScrollTouchEnd();
  502. }
  503. void NativeWindow::NotifyWindowSwipe(const std::string& direction) {
  504. for (NativeWindowObserver& observer : observers_)
  505. observer.OnWindowSwipe(direction);
  506. }
  507. void NativeWindow::NotifyWindowRotateGesture(float rotation) {
  508. for (NativeWindowObserver& observer : observers_)
  509. observer.OnWindowRotateGesture(rotation);
  510. }
  511. void NativeWindow::NotifyWindowSheetBegin() {
  512. for (NativeWindowObserver& observer : observers_)
  513. observer.OnWindowSheetBegin();
  514. }
  515. void NativeWindow::NotifyWindowSheetEnd() {
  516. for (NativeWindowObserver& observer : observers_)
  517. observer.OnWindowSheetEnd();
  518. }
  519. void NativeWindow::NotifyWindowLeaveFullScreen() {
  520. for (NativeWindowObserver& observer : observers_)
  521. observer.OnWindowLeaveFullScreen();
  522. }
  523. void NativeWindow::NotifyWindowEnterHtmlFullScreen() {
  524. for (NativeWindowObserver& observer : observers_)
  525. observer.OnWindowEnterHtmlFullScreen();
  526. }
  527. void NativeWindow::NotifyWindowLeaveHtmlFullScreen() {
  528. for (NativeWindowObserver& observer : observers_)
  529. observer.OnWindowLeaveHtmlFullScreen();
  530. }
  531. void NativeWindow::NotifyWindowAlwaysOnTopChanged() {
  532. for (NativeWindowObserver& observer : observers_)
  533. observer.OnWindowAlwaysOnTopChanged();
  534. }
  535. void NativeWindow::NotifyWindowExecuteAppCommand(const std::string& command) {
  536. for (NativeWindowObserver& observer : observers_)
  537. observer.OnExecuteAppCommand(command);
  538. }
  539. void NativeWindow::NotifyTouchBarItemInteraction(
  540. const std::string& item_id,
  541. const base::DictionaryValue& details) {
  542. for (NativeWindowObserver& observer : observers_)
  543. observer.OnTouchBarItemResult(item_id, details);
  544. }
  545. void NativeWindow::NotifyNewWindowForTab() {
  546. for (NativeWindowObserver& observer : observers_)
  547. observer.OnNewWindowForTab();
  548. }
  549. void NativeWindow::NotifyWindowSystemContextMenu(int x,
  550. int y,
  551. bool* prevent_default) {
  552. for (NativeWindowObserver& observer : observers_)
  553. observer.OnSystemContextMenu(x, y, prevent_default);
  554. }
  555. void NativeWindow::NotifyLayoutWindowControlsOverlay() {
  556. gfx::Rect bounding_rect = GetWindowControlsOverlayRect();
  557. if (!bounding_rect.IsEmpty()) {
  558. for (NativeWindowObserver& observer : observers_)
  559. observer.UpdateWindowControlsOverlay(bounding_rect);
  560. }
  561. }
  562. #if BUILDFLAG(IS_WIN)
  563. void NativeWindow::NotifyWindowMessage(UINT message,
  564. WPARAM w_param,
  565. LPARAM l_param) {
  566. for (NativeWindowObserver& observer : observers_)
  567. observer.OnWindowMessage(message, w_param, l_param);
  568. }
  569. #endif
  570. views::Widget* NativeWindow::GetWidget() {
  571. return widget();
  572. }
  573. const views::Widget* NativeWindow::GetWidget() const {
  574. return widget();
  575. }
  576. std::u16string NativeWindow::GetAccessibleWindowTitle() const {
  577. if (accessible_title_.empty()) {
  578. return views::WidgetDelegate::GetAccessibleWindowTitle();
  579. }
  580. return accessible_title_;
  581. }
  582. void NativeWindow::SetAccessibleTitle(const std::string& title) {
  583. accessible_title_ = base::UTF8ToUTF16(title);
  584. }
  585. std::string NativeWindow::GetAccessibleTitle() {
  586. return base::UTF16ToUTF8(accessible_title_);
  587. }
  588. void NativeWindow::HandlePendingFullscreenTransitions() {
  589. if (pending_transitions_.empty()) {
  590. set_fullscreen_transition_type(FullScreenTransitionType::NONE);
  591. return;
  592. }
  593. bool next_transition = pending_transitions_.front();
  594. pending_transitions_.pop();
  595. SetFullScreen(next_transition);
  596. }
  597. // static
  598. int32_t NativeWindow::next_id_ = 0;
  599. // static
  600. void NativeWindowRelay::CreateForWebContents(
  601. content::WebContents* web_contents,
  602. base::WeakPtr<NativeWindow> window) {
  603. DCHECK(web_contents);
  604. if (!web_contents->GetUserData(UserDataKey())) {
  605. web_contents->SetUserData(
  606. UserDataKey(),
  607. base::WrapUnique(new NativeWindowRelay(web_contents, window)));
  608. }
  609. }
  610. NativeWindowRelay::NativeWindowRelay(content::WebContents* web_contents,
  611. base::WeakPtr<NativeWindow> window)
  612. : content::WebContentsUserData<NativeWindowRelay>(*web_contents),
  613. native_window_(window) {}
  614. NativeWindowRelay::~NativeWindowRelay() = default;
  615. WEB_CONTENTS_USER_DATA_KEY_IMPL(NativeWindowRelay);
  616. } // namespace electron