native_window.cc 26 KB

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