native_window.cc 26 KB

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