native_window.cc 26 KB

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