native_window.cc 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  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() {
  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() {
  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() {
  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() {
  275. return WindowBoundsToContentBounds(GetBounds());
  276. }
  277. bool NativeWindow::IsNormal() {
  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() {
  373. return sheet_offset_x_;
  374. }
  375. double NativeWindow::GetSheetOffsetY() {
  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() {
  383. return "";
  384. }
  385. void NativeWindow::SetDocumentEdited(bool edited) {}
  386. bool NativeWindow::IsDocumentEdited() {
  387. return false;
  388. }
  389. void NativeWindow::SetFocusable(bool focusable) {}
  390. bool NativeWindow::IsFocusable() {
  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. absl::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() {
  424. return false;
  425. }
  426. void NativeWindow::SetMenuBarVisibility(bool visible) {}
  427. bool NativeWindow::IsMenuBarVisible() {
  428. return true;
  429. }
  430. double NativeWindow::GetAspectRatio() {
  431. return aspect_ratio_;
  432. }
  433. gfx::Size NativeWindow::GetAspectRatioExtraSize() {
  434. return aspect_ratio_extraSize_;
  435. }
  436. void NativeWindow::SetAspectRatio(double aspect_ratio,
  437. const gfx::Size& extra_size) {
  438. aspect_ratio_ = aspect_ratio;
  439. aspect_ratio_extraSize_ = extra_size;
  440. }
  441. void NativeWindow::PreviewFile(const std::string& path,
  442. const std::string& display_name) {}
  443. void NativeWindow::CloseFilePreview() {}
  444. absl::optional<gfx::Rect> NativeWindow::GetWindowControlsOverlayRect() {
  445. return overlay_rect_;
  446. }
  447. void NativeWindow::SetWindowControlsOverlayRect(const gfx::Rect& overlay_rect) {
  448. overlay_rect_ = overlay_rect;
  449. }
  450. void NativeWindow::NotifyWindowRequestPreferredWidth(int* width) {
  451. for (NativeWindowObserver& observer : observers_)
  452. observer.RequestPreferredWidth(width);
  453. }
  454. void NativeWindow::NotifyWindowCloseButtonClicked() {
  455. // First ask the observers whether we want to close.
  456. bool prevent_default = false;
  457. for (NativeWindowObserver& observer : observers_)
  458. observer.WillCloseWindow(&prevent_default);
  459. if (prevent_default) {
  460. WindowList::WindowCloseCancelled(this);
  461. return;
  462. }
  463. // Then ask the observers how should we close the window.
  464. for (NativeWindowObserver& observer : observers_)
  465. observer.OnCloseButtonClicked(&prevent_default);
  466. if (prevent_default)
  467. return;
  468. CloseImmediately();
  469. }
  470. void NativeWindow::NotifyWindowClosed() {
  471. if (is_closed_)
  472. return;
  473. is_closed_ = true;
  474. for (NativeWindowObserver& observer : observers_)
  475. observer.OnWindowClosed();
  476. WindowList::RemoveWindow(this);
  477. }
  478. void NativeWindow::NotifyWindowEndSession() {
  479. for (NativeWindowObserver& observer : observers_)
  480. observer.OnWindowEndSession();
  481. }
  482. void NativeWindow::NotifyWindowBlur() {
  483. for (NativeWindowObserver& observer : observers_)
  484. observer.OnWindowBlur();
  485. }
  486. void NativeWindow::NotifyWindowFocus() {
  487. for (NativeWindowObserver& observer : observers_)
  488. observer.OnWindowFocus();
  489. }
  490. void NativeWindow::NotifyWindowIsKeyChanged(bool is_key) {
  491. for (NativeWindowObserver& observer : observers_)
  492. observer.OnWindowIsKeyChanged(is_key);
  493. }
  494. void NativeWindow::NotifyWindowShow() {
  495. for (NativeWindowObserver& observer : observers_)
  496. observer.OnWindowShow();
  497. }
  498. void NativeWindow::NotifyWindowHide() {
  499. for (NativeWindowObserver& observer : observers_)
  500. observer.OnWindowHide();
  501. }
  502. void NativeWindow::NotifyWindowMaximize() {
  503. for (NativeWindowObserver& observer : observers_)
  504. observer.OnWindowMaximize();
  505. }
  506. void NativeWindow::NotifyWindowUnmaximize() {
  507. for (NativeWindowObserver& observer : observers_)
  508. observer.OnWindowUnmaximize();
  509. }
  510. void NativeWindow::NotifyWindowMinimize() {
  511. for (NativeWindowObserver& observer : observers_)
  512. observer.OnWindowMinimize();
  513. }
  514. void NativeWindow::NotifyWindowRestore() {
  515. for (NativeWindowObserver& observer : observers_)
  516. observer.OnWindowRestore();
  517. }
  518. void NativeWindow::NotifyWindowWillResize(const gfx::Rect& new_bounds,
  519. const gfx::ResizeEdge& edge,
  520. bool* prevent_default) {
  521. for (NativeWindowObserver& observer : observers_)
  522. observer.OnWindowWillResize(new_bounds, edge, prevent_default);
  523. }
  524. void NativeWindow::NotifyWindowWillMove(const gfx::Rect& new_bounds,
  525. bool* prevent_default) {
  526. for (NativeWindowObserver& observer : observers_)
  527. observer.OnWindowWillMove(new_bounds, prevent_default);
  528. }
  529. void NativeWindow::NotifyWindowResize() {
  530. NotifyLayoutWindowControlsOverlay();
  531. for (NativeWindowObserver& observer : observers_)
  532. observer.OnWindowResize();
  533. }
  534. void NativeWindow::NotifyWindowResized() {
  535. for (NativeWindowObserver& observer : observers_)
  536. observer.OnWindowResized();
  537. }
  538. void NativeWindow::NotifyWindowMove() {
  539. for (NativeWindowObserver& observer : observers_)
  540. observer.OnWindowMove();
  541. }
  542. void NativeWindow::NotifyWindowMoved() {
  543. for (NativeWindowObserver& observer : observers_)
  544. observer.OnWindowMoved();
  545. }
  546. void NativeWindow::NotifyWindowEnterFullScreen() {
  547. NotifyLayoutWindowControlsOverlay();
  548. for (NativeWindowObserver& observer : observers_)
  549. observer.OnWindowEnterFullScreen();
  550. }
  551. void NativeWindow::NotifyWindowSwipe(const std::string& direction) {
  552. for (NativeWindowObserver& observer : observers_)
  553. observer.OnWindowSwipe(direction);
  554. }
  555. void NativeWindow::NotifyWindowRotateGesture(float rotation) {
  556. for (NativeWindowObserver& observer : observers_)
  557. observer.OnWindowRotateGesture(rotation);
  558. }
  559. void NativeWindow::NotifyWindowSheetBegin() {
  560. for (NativeWindowObserver& observer : observers_)
  561. observer.OnWindowSheetBegin();
  562. }
  563. void NativeWindow::NotifyWindowSheetEnd() {
  564. for (NativeWindowObserver& observer : observers_)
  565. observer.OnWindowSheetEnd();
  566. }
  567. void NativeWindow::NotifyWindowLeaveFullScreen() {
  568. NotifyLayoutWindowControlsOverlay();
  569. for (NativeWindowObserver& observer : observers_)
  570. observer.OnWindowLeaveFullScreen();
  571. }
  572. void NativeWindow::NotifyWindowEnterHtmlFullScreen() {
  573. for (NativeWindowObserver& observer : observers_)
  574. observer.OnWindowEnterHtmlFullScreen();
  575. }
  576. void NativeWindow::NotifyWindowLeaveHtmlFullScreen() {
  577. for (NativeWindowObserver& observer : observers_)
  578. observer.OnWindowLeaveHtmlFullScreen();
  579. }
  580. void NativeWindow::NotifyWindowAlwaysOnTopChanged() {
  581. for (NativeWindowObserver& observer : observers_)
  582. observer.OnWindowAlwaysOnTopChanged();
  583. }
  584. void NativeWindow::NotifyWindowExecuteAppCommand(const std::string& command) {
  585. for (NativeWindowObserver& observer : observers_)
  586. observer.OnExecuteAppCommand(command);
  587. }
  588. void NativeWindow::NotifyTouchBarItemInteraction(const std::string& item_id,
  589. base::Value::Dict details) {
  590. for (NativeWindowObserver& observer : observers_)
  591. observer.OnTouchBarItemResult(item_id, details);
  592. }
  593. void NativeWindow::NotifyNewWindowForTab() {
  594. for (NativeWindowObserver& observer : observers_)
  595. observer.OnNewWindowForTab();
  596. }
  597. void NativeWindow::NotifyWindowSystemContextMenu(int x,
  598. int y,
  599. bool* prevent_default) {
  600. for (NativeWindowObserver& observer : observers_)
  601. observer.OnSystemContextMenu(x, y, prevent_default);
  602. }
  603. void NativeWindow::NotifyLayoutWindowControlsOverlay() {
  604. auto bounding_rect = GetWindowControlsOverlayRect();
  605. if (bounding_rect.has_value()) {
  606. for (NativeWindowObserver& observer : observers_)
  607. observer.UpdateWindowControlsOverlay(bounding_rect.value());
  608. }
  609. }
  610. #if BUILDFLAG(IS_WIN)
  611. void NativeWindow::NotifyWindowMessage(UINT message,
  612. WPARAM w_param,
  613. LPARAM l_param) {
  614. for (NativeWindowObserver& observer : observers_)
  615. observer.OnWindowMessage(message, w_param, l_param);
  616. }
  617. #endif
  618. int NativeWindow::NonClientHitTest(const gfx::Point& point) {
  619. #if !BUILDFLAG(IS_MAC)
  620. // We need to ensure we account for resizing borders on Windows and Linux.
  621. if ((!has_frame() || has_client_frame()) && IsResizable()) {
  622. auto* frame =
  623. static_cast<FramelessView*>(widget()->non_client_view()->frame_view());
  624. int border_hit = frame->ResizingBorderHitTest(point);
  625. if (border_hit != HTNOWHERE)
  626. return border_hit;
  627. }
  628. #endif
  629. for (auto* provider : draggable_region_providers_) {
  630. int hit = provider->NonClientHitTest(point);
  631. if (hit != HTNOWHERE)
  632. return hit;
  633. }
  634. return HTNOWHERE;
  635. }
  636. void NativeWindow::AddDraggableRegionProvider(
  637. DraggableRegionProvider* provider) {
  638. if (!base::Contains(draggable_region_providers_, provider)) {
  639. draggable_region_providers_.push_back(provider);
  640. }
  641. }
  642. void NativeWindow::RemoveDraggableRegionProvider(
  643. DraggableRegionProvider* provider) {
  644. draggable_region_providers_.remove_if(
  645. [&provider](DraggableRegionProvider* p) { return p == provider; });
  646. }
  647. void NativeWindow::AddBackgroundThrottlingSource(
  648. BackgroundThrottlingSource* source) {
  649. auto result = background_throttling_sources_.insert(source);
  650. DCHECK(result.second) << "Added already stored BackgroundThrottlingSource.";
  651. UpdateBackgroundThrottlingState();
  652. }
  653. void NativeWindow::RemoveBackgroundThrottlingSource(
  654. BackgroundThrottlingSource* source) {
  655. auto result = background_throttling_sources_.erase(source);
  656. DCHECK(result == 1)
  657. << "Tried to remove non existing BackgroundThrottlingSource.";
  658. UpdateBackgroundThrottlingState();
  659. }
  660. void NativeWindow::UpdateBackgroundThrottlingState() {
  661. if (!GetWidget() || !GetWidget()->GetCompositor()) {
  662. return;
  663. }
  664. bool enable_background_throttling = true;
  665. for (const auto* background_throttling_source :
  666. background_throttling_sources_) {
  667. if (!background_throttling_source->GetBackgroundThrottling()) {
  668. enable_background_throttling = false;
  669. break;
  670. }
  671. }
  672. GetWidget()->GetCompositor()->SetBackgroundThrottling(
  673. enable_background_throttling);
  674. }
  675. views::Widget* NativeWindow::GetWidget() {
  676. return widget();
  677. }
  678. const views::Widget* NativeWindow::GetWidget() const {
  679. return widget();
  680. }
  681. std::u16string NativeWindow::GetAccessibleWindowTitle() const {
  682. if (accessible_title_.empty()) {
  683. return views::WidgetDelegate::GetAccessibleWindowTitle();
  684. }
  685. return accessible_title_;
  686. }
  687. void NativeWindow::SetAccessibleTitle(const std::string& title) {
  688. accessible_title_ = base::UTF8ToUTF16(title);
  689. }
  690. std::string NativeWindow::GetAccessibleTitle() {
  691. return base::UTF16ToUTF8(accessible_title_);
  692. }
  693. void NativeWindow::HandlePendingFullscreenTransitions() {
  694. if (pending_transitions_.empty()) {
  695. set_fullscreen_transition_type(FullScreenTransitionType::kNone);
  696. return;
  697. }
  698. bool next_transition = pending_transitions_.front();
  699. pending_transitions_.pop();
  700. SetFullScreen(next_transition);
  701. }
  702. // static
  703. int32_t NativeWindow::next_id_ = 0;
  704. bool NativeWindow::IsTranslucent() const {
  705. // Transparent windows are translucent
  706. if (transparent()) {
  707. return true;
  708. }
  709. #if BUILDFLAG(IS_MAC)
  710. // Windows with vibrancy set are translucent
  711. if (!vibrancy().empty()) {
  712. return true;
  713. }
  714. #endif
  715. #if BUILDFLAG(IS_WIN)
  716. // Windows with certain background materials may be translucent
  717. const std::string& bg_material = background_material();
  718. if (!bg_material.empty() && bg_material != "none") {
  719. return true;
  720. }
  721. #endif
  722. return false;
  723. }
  724. // static
  725. void NativeWindowRelay::CreateForWebContents(
  726. content::WebContents* web_contents,
  727. base::WeakPtr<NativeWindow> window) {
  728. DCHECK(web_contents);
  729. if (!web_contents->GetUserData(UserDataKey())) {
  730. web_contents->SetUserData(
  731. UserDataKey(),
  732. base::WrapUnique(new NativeWindowRelay(web_contents, window)));
  733. }
  734. }
  735. NativeWindowRelay::NativeWindowRelay(content::WebContents* web_contents,
  736. base::WeakPtr<NativeWindow> window)
  737. : content::WebContentsUserData<NativeWindowRelay>(*web_contents),
  738. native_window_(window) {}
  739. NativeWindowRelay::~NativeWindowRelay() = default;
  740. WEB_CONTENTS_USER_DATA_KEY_IMPL(NativeWindowRelay);
  741. } // namespace electron