native_window.cc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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 "atom/browser/native_window.h"
  5. #include <algorithm>
  6. #include <string>
  7. #include <utility>
  8. #include <vector>
  9. #include "atom/browser/browser.h"
  10. #include "atom/browser/window_list.h"
  11. #include "atom/common/color_util.h"
  12. #include "atom/common/options_switches.h"
  13. #include "native_mate/dictionary.h"
  14. #include "ui/views/widget/widget.h"
  15. #if defined(OS_WIN)
  16. #include "ui/base/win/shell.h"
  17. #include "ui/display/win/screen_win.h"
  18. #endif
  19. namespace atom {
  20. namespace {
  21. #if defined(OS_WIN)
  22. gfx::Size GetExpandedWindowSize(const NativeWindow* window, gfx::Size size) {
  23. if (!window->transparent() || !ui::win::IsAeroGlassEnabled())
  24. return size;
  25. gfx::Size min_size = display::win::ScreenWin::ScreenToDIPSize(
  26. window->GetAcceleratedWidget(), gfx::Size(64, 64));
  27. // Some AMD drivers can't display windows that are less than 64x64 pixels,
  28. // so expand them to be at least that size. http://crbug.com/286609
  29. gfx::Size expanded(std::max(size.width(), min_size.width()),
  30. std::max(size.height(), min_size.height()));
  31. return expanded;
  32. }
  33. #endif
  34. } // namespace
  35. NativeWindow::NativeWindow(const mate::Dictionary& options,
  36. NativeWindow* parent)
  37. : widget_(new views::Widget), parent_(parent), weak_factory_(this) {
  38. options.Get(options::kFrame, &has_frame_);
  39. options.Get(options::kTransparent, &transparent_);
  40. options.Get(options::kEnableLargerThanScreen, &enable_larger_than_screen_);
  41. if (parent)
  42. options.Get("modal", &is_modal_);
  43. WindowList::AddWindow(this);
  44. }
  45. NativeWindow::~NativeWindow() {
  46. // It's possible that the windows gets destroyed before it's closed, in that
  47. // case we need to ensure the Widget delegate gets destroyed and
  48. // OnWindowClosed message is still notified.
  49. if (widget_->widget_delegate())
  50. widget_->OnNativeWidgetDestroyed();
  51. NotifyWindowClosed();
  52. }
  53. void NativeWindow::InitFromOptions(const mate::Dictionary& options) {
  54. // Setup window from options.
  55. int x = -1, y = -1;
  56. bool center;
  57. if (options.Get(options::kX, &x) && options.Get(options::kY, &y)) {
  58. SetPosition(gfx::Point(x, y));
  59. #if defined(OS_WIN)
  60. // FIXME(felixrieseberg): Dirty, dirty workaround for
  61. // https://github.com/electron/electron/issues/10862
  62. // Somehow, we need to call `SetBounds` twice to get
  63. // usable results. The root cause is still unknown.
  64. SetPosition(gfx::Point(x, y));
  65. #endif
  66. } else if (options.Get(options::kCenter, &center) && center) {
  67. Center();
  68. }
  69. // On Linux and Window we may already have maximum size defined.
  70. extensions::SizeConstraints size_constraints(GetContentSizeConstraints());
  71. int min_height = 0, min_width = 0;
  72. if (options.Get(options::kMinHeight, &min_height) |
  73. options.Get(options::kMinWidth, &min_width)) {
  74. size_constraints.set_minimum_size(gfx::Size(min_width, min_height));
  75. }
  76. int max_height = INT_MAX, max_width = INT_MAX;
  77. if (options.Get(options::kMaxHeight, &max_height) |
  78. options.Get(options::kMaxWidth, &max_width)) {
  79. size_constraints.set_maximum_size(gfx::Size(max_width, max_height));
  80. }
  81. bool use_content_size = false;
  82. options.Get(options::kUseContentSize, &use_content_size);
  83. if (use_content_size) {
  84. SetContentSizeConstraints(size_constraints);
  85. } else {
  86. SetSizeConstraints(size_constraints);
  87. }
  88. #if defined(OS_WIN) || defined(USE_X11)
  89. bool resizable;
  90. if (options.Get(options::kResizable, &resizable)) {
  91. SetResizable(resizable);
  92. }
  93. bool closable;
  94. if (options.Get(options::kClosable, &closable)) {
  95. SetClosable(closable);
  96. }
  97. #endif
  98. bool movable;
  99. if (options.Get(options::kMovable, &movable)) {
  100. SetMovable(movable);
  101. }
  102. bool has_shadow;
  103. if (options.Get(options::kHasShadow, &has_shadow)) {
  104. SetHasShadow(has_shadow);
  105. }
  106. double opacity;
  107. if (options.Get(options::kOpacity, &opacity)) {
  108. SetOpacity(opacity);
  109. }
  110. bool top;
  111. if (options.Get(options::kAlwaysOnTop, &top) && top) {
  112. SetAlwaysOnTop(true);
  113. }
  114. bool fullscreenable = true;
  115. bool fullscreen = false;
  116. if (options.Get(options::kFullscreen, &fullscreen) && !fullscreen) {
  117. // Disable fullscreen button if 'fullscreen' is specified to false.
  118. #if defined(OS_MACOSX)
  119. fullscreenable = false;
  120. #endif
  121. }
  122. // Overriden by 'fullscreenable'.
  123. options.Get(options::kFullScreenable, &fullscreenable);
  124. SetFullScreenable(fullscreenable);
  125. if (fullscreen) {
  126. SetFullScreen(true);
  127. }
  128. bool skip;
  129. if (options.Get(options::kSkipTaskbar, &skip)) {
  130. SetSkipTaskbar(skip);
  131. }
  132. bool kiosk;
  133. if (options.Get(options::kKiosk, &kiosk) && kiosk) {
  134. SetKiosk(kiosk);
  135. }
  136. #if defined(OS_MACOSX)
  137. std::string type;
  138. if (options.Get(options::kVibrancyType, &type)) {
  139. SetVibrancy(type);
  140. }
  141. #endif
  142. std::string color;
  143. if (options.Get(options::kBackgroundColor, &color)) {
  144. SetBackgroundColor(ParseHexColor(color));
  145. } else if (!transparent()) {
  146. // For normal window, use white as default background.
  147. SetBackgroundColor(SK_ColorWHITE);
  148. }
  149. std::string title(Browser::Get()->GetName());
  150. options.Get(options::kTitle, &title);
  151. SetTitle(title);
  152. // Then show it.
  153. bool show = true;
  154. options.Get(options::kShow, &show);
  155. if (show)
  156. Show();
  157. }
  158. bool NativeWindow::IsClosed() const {
  159. return is_closed_;
  160. }
  161. void NativeWindow::SetSize(const gfx::Size& size, bool animate) {
  162. SetBounds(gfx::Rect(GetPosition(), size), animate);
  163. }
  164. gfx::Size NativeWindow::GetSize() {
  165. return GetBounds().size();
  166. }
  167. void NativeWindow::SetPosition(const gfx::Point& position, bool animate) {
  168. SetBounds(gfx::Rect(position, GetSize()), animate);
  169. }
  170. gfx::Point NativeWindow::GetPosition() {
  171. return GetBounds().origin();
  172. }
  173. void NativeWindow::SetContentSize(const gfx::Size& size, bool animate) {
  174. SetSize(ContentBoundsToWindowBounds(gfx::Rect(size)).size(), animate);
  175. }
  176. gfx::Size NativeWindow::GetContentSize() {
  177. return GetContentBounds().size();
  178. }
  179. void NativeWindow::SetContentBounds(const gfx::Rect& bounds, bool animate) {
  180. SetBounds(ContentBoundsToWindowBounds(bounds), animate);
  181. }
  182. gfx::Rect NativeWindow::GetContentBounds() {
  183. return WindowBoundsToContentBounds(GetBounds());
  184. }
  185. bool NativeWindow::IsNormal() {
  186. return !IsMinimized() && !IsMaximized() && !IsFullscreen();
  187. }
  188. void NativeWindow::SetSizeConstraints(
  189. const extensions::SizeConstraints& window_constraints) {
  190. extensions::SizeConstraints content_constraints(GetContentSizeConstraints());
  191. if (window_constraints.HasMaximumSize()) {
  192. gfx::Rect max_bounds = WindowBoundsToContentBounds(
  193. gfx::Rect(window_constraints.GetMaximumSize()));
  194. content_constraints.set_maximum_size(max_bounds.size());
  195. }
  196. if (window_constraints.HasMinimumSize()) {
  197. gfx::Rect min_bounds = WindowBoundsToContentBounds(
  198. gfx::Rect(window_constraints.GetMinimumSize()));
  199. content_constraints.set_minimum_size(min_bounds.size());
  200. }
  201. SetContentSizeConstraints(content_constraints);
  202. }
  203. extensions::SizeConstraints NativeWindow::GetSizeConstraints() const {
  204. extensions::SizeConstraints content_constraints = GetContentSizeConstraints();
  205. extensions::SizeConstraints window_constraints;
  206. if (content_constraints.HasMaximumSize()) {
  207. gfx::Rect max_bounds = ContentBoundsToWindowBounds(
  208. gfx::Rect(content_constraints.GetMaximumSize()));
  209. window_constraints.set_maximum_size(max_bounds.size());
  210. }
  211. if (content_constraints.HasMinimumSize()) {
  212. gfx::Rect min_bounds = ContentBoundsToWindowBounds(
  213. gfx::Rect(content_constraints.GetMinimumSize()));
  214. window_constraints.set_minimum_size(min_bounds.size());
  215. }
  216. return window_constraints;
  217. }
  218. void NativeWindow::SetContentSizeConstraints(
  219. const extensions::SizeConstraints& size_constraints) {
  220. size_constraints_ = size_constraints;
  221. }
  222. extensions::SizeConstraints NativeWindow::GetContentSizeConstraints() const {
  223. return size_constraints_;
  224. }
  225. void NativeWindow::SetMinimumSize(const gfx::Size& size) {
  226. extensions::SizeConstraints size_constraints;
  227. size_constraints.set_minimum_size(size);
  228. SetSizeConstraints(size_constraints);
  229. }
  230. gfx::Size NativeWindow::GetMinimumSize() const {
  231. return GetSizeConstraints().GetMinimumSize();
  232. }
  233. void NativeWindow::SetMaximumSize(const gfx::Size& size) {
  234. extensions::SizeConstraints size_constraints;
  235. size_constraints.set_maximum_size(size);
  236. SetSizeConstraints(size_constraints);
  237. }
  238. gfx::Size NativeWindow::GetMaximumSize() const {
  239. return GetSizeConstraints().GetMaximumSize();
  240. }
  241. gfx::Size NativeWindow::GetContentMinimumSize() const {
  242. return GetContentSizeConstraints().GetMinimumSize();
  243. }
  244. gfx::Size NativeWindow::GetContentMaximumSize() const {
  245. gfx::Size maximum_size = GetContentSizeConstraints().GetMaximumSize();
  246. #if defined(OS_WIN)
  247. return GetContentSizeConstraints().HasMaximumSize()
  248. ? GetExpandedWindowSize(this, maximum_size)
  249. : maximum_size;
  250. #else
  251. return maximum_size;
  252. #endif
  253. }
  254. void NativeWindow::SetSheetOffset(const double offsetX, const double offsetY) {
  255. sheet_offset_x_ = offsetX;
  256. sheet_offset_y_ = offsetY;
  257. }
  258. double NativeWindow::GetSheetOffsetX() {
  259. return sheet_offset_x_;
  260. }
  261. double NativeWindow::GetSheetOffsetY() {
  262. return sheet_offset_y_;
  263. }
  264. void NativeWindow::SetRepresentedFilename(const std::string& filename) {}
  265. std::string NativeWindow::GetRepresentedFilename() {
  266. return "";
  267. }
  268. void NativeWindow::SetDocumentEdited(bool edited) {}
  269. bool NativeWindow::IsDocumentEdited() {
  270. return false;
  271. }
  272. void NativeWindow::SetFocusable(bool focusable) {}
  273. void NativeWindow::SetMenu(AtomMenuModel* menu) {}
  274. void NativeWindow::SetParentWindow(NativeWindow* parent) {
  275. parent_ = parent;
  276. }
  277. void NativeWindow::SetAutoHideCursor(bool auto_hide) {}
  278. void NativeWindow::SelectPreviousTab() {}
  279. void NativeWindow::SelectNextTab() {}
  280. void NativeWindow::MergeAllWindows() {}
  281. void NativeWindow::MoveTabToNewWindow() {}
  282. void NativeWindow::ToggleTabBar() {}
  283. bool NativeWindow::AddTabbedWindow(NativeWindow* window) {
  284. return true; // for non-Mac platforms
  285. }
  286. void NativeWindow::SetVibrancy(const std::string& filename) {}
  287. void NativeWindow::SetTouchBar(
  288. const std::vector<mate::PersistentDictionary>& items) {}
  289. void NativeWindow::RefreshTouchBarItem(const std::string& item_id) {}
  290. void NativeWindow::SetEscapeTouchBarItem(
  291. const mate::PersistentDictionary& item) {}
  292. void NativeWindow::SetAutoHideMenuBar(bool auto_hide) {}
  293. bool NativeWindow::IsMenuBarAutoHide() {
  294. return false;
  295. }
  296. void NativeWindow::SetMenuBarVisibility(bool visible) {}
  297. bool NativeWindow::IsMenuBarVisible() {
  298. return true;
  299. }
  300. bool NativeWindow::SetWindowButtonVisibility(bool visible) {
  301. return false;
  302. }
  303. double NativeWindow::GetAspectRatio() {
  304. return aspect_ratio_;
  305. }
  306. gfx::Size NativeWindow::GetAspectRatioExtraSize() {
  307. return aspect_ratio_extraSize_;
  308. }
  309. void NativeWindow::SetAspectRatio(double aspect_ratio,
  310. const gfx::Size& extra_size) {
  311. aspect_ratio_ = aspect_ratio;
  312. aspect_ratio_extraSize_ = extra_size;
  313. }
  314. void NativeWindow::PreviewFile(const std::string& path,
  315. const std::string& display_name) {}
  316. void NativeWindow::CloseFilePreview() {}
  317. void NativeWindow::NotifyWindowRequestPreferredWith(int* width) {
  318. for (NativeWindowObserver& observer : observers_)
  319. observer.RequestPreferredWidth(width);
  320. }
  321. void NativeWindow::NotifyWindowCloseButtonClicked() {
  322. // First ask the observers whether we want to close.
  323. bool prevent_default = false;
  324. for (NativeWindowObserver& observer : observers_)
  325. observer.WillCloseWindow(&prevent_default);
  326. if (prevent_default) {
  327. WindowList::WindowCloseCancelled(this);
  328. return;
  329. }
  330. // Then ask the observers how should we close the window.
  331. for (NativeWindowObserver& observer : observers_)
  332. observer.OnCloseButtonClicked(&prevent_default);
  333. if (prevent_default)
  334. return;
  335. CloseImmediately();
  336. }
  337. void NativeWindow::NotifyWindowClosed() {
  338. if (is_closed_)
  339. return;
  340. WindowList::RemoveWindow(this);
  341. is_closed_ = true;
  342. for (NativeWindowObserver& observer : observers_)
  343. observer.OnWindowClosed();
  344. }
  345. void NativeWindow::NotifyWindowEndSession() {
  346. for (NativeWindowObserver& observer : observers_)
  347. observer.OnWindowEndSession();
  348. }
  349. void NativeWindow::NotifyWindowBlur() {
  350. for (NativeWindowObserver& observer : observers_)
  351. observer.OnWindowBlur();
  352. }
  353. void NativeWindow::NotifyWindowFocus() {
  354. for (NativeWindowObserver& observer : observers_)
  355. observer.OnWindowFocus();
  356. }
  357. void NativeWindow::NotifyWindowShow() {
  358. for (NativeWindowObserver& observer : observers_)
  359. observer.OnWindowShow();
  360. }
  361. void NativeWindow::NotifyWindowHide() {
  362. for (NativeWindowObserver& observer : observers_)
  363. observer.OnWindowHide();
  364. }
  365. void NativeWindow::NotifyWindowMaximize() {
  366. for (NativeWindowObserver& observer : observers_)
  367. observer.OnWindowMaximize();
  368. }
  369. void NativeWindow::NotifyWindowUnmaximize() {
  370. for (NativeWindowObserver& observer : observers_)
  371. observer.OnWindowUnmaximize();
  372. }
  373. void NativeWindow::NotifyWindowMinimize() {
  374. for (NativeWindowObserver& observer : observers_)
  375. observer.OnWindowMinimize();
  376. }
  377. void NativeWindow::NotifyWindowRestore() {
  378. for (NativeWindowObserver& observer : observers_)
  379. observer.OnWindowRestore();
  380. }
  381. void NativeWindow::NotifyWindowWillResize(const gfx::Rect& new_bounds,
  382. bool* prevent_default) {
  383. for (NativeWindowObserver& observer : observers_)
  384. observer.OnWindowWillResize(new_bounds, prevent_default);
  385. }
  386. void NativeWindow::NotifyWindowWillMove(const gfx::Rect& new_bounds,
  387. bool* prevent_default) {
  388. for (NativeWindowObserver& observer : observers_)
  389. observer.OnWindowWillMove(new_bounds, prevent_default);
  390. }
  391. void NativeWindow::NotifyWindowResize() {
  392. for (NativeWindowObserver& observer : observers_)
  393. observer.OnWindowResize();
  394. }
  395. void NativeWindow::NotifyWindowMove() {
  396. for (NativeWindowObserver& observer : observers_)
  397. observer.OnWindowMove();
  398. }
  399. void NativeWindow::NotifyWindowMoved() {
  400. for (NativeWindowObserver& observer : observers_)
  401. observer.OnWindowMoved();
  402. }
  403. void NativeWindow::NotifyWindowEnterFullScreen() {
  404. for (NativeWindowObserver& observer : observers_)
  405. observer.OnWindowEnterFullScreen();
  406. }
  407. void NativeWindow::NotifyWindowScrollTouchBegin() {
  408. for (NativeWindowObserver& observer : observers_)
  409. observer.OnWindowScrollTouchBegin();
  410. }
  411. void NativeWindow::NotifyWindowScrollTouchEnd() {
  412. for (NativeWindowObserver& observer : observers_)
  413. observer.OnWindowScrollTouchEnd();
  414. }
  415. void NativeWindow::NotifyWindowSwipe(const std::string& direction) {
  416. for (NativeWindowObserver& observer : observers_)
  417. observer.OnWindowSwipe(direction);
  418. }
  419. void NativeWindow::NotifyWindowSheetBegin() {
  420. for (NativeWindowObserver& observer : observers_)
  421. observer.OnWindowSheetBegin();
  422. }
  423. void NativeWindow::NotifyWindowSheetEnd() {
  424. for (NativeWindowObserver& observer : observers_)
  425. observer.OnWindowSheetEnd();
  426. }
  427. void NativeWindow::NotifyWindowLeaveFullScreen() {
  428. for (NativeWindowObserver& observer : observers_)
  429. observer.OnWindowLeaveFullScreen();
  430. }
  431. void NativeWindow::NotifyWindowEnterHtmlFullScreen() {
  432. for (NativeWindowObserver& observer : observers_)
  433. observer.OnWindowEnterHtmlFullScreen();
  434. }
  435. void NativeWindow::NotifyWindowLeaveHtmlFullScreen() {
  436. for (NativeWindowObserver& observer : observers_)
  437. observer.OnWindowLeaveHtmlFullScreen();
  438. }
  439. void NativeWindow::NotifyWindowAlwaysOnTopChanged() {
  440. for (NativeWindowObserver& observer : observers_)
  441. observer.OnWindowAlwaysOnTopChanged();
  442. }
  443. void NativeWindow::NotifyWindowExecuteWindowsCommand(
  444. const std::string& command) {
  445. for (NativeWindowObserver& observer : observers_)
  446. observer.OnExecuteWindowsCommand(command);
  447. }
  448. void NativeWindow::NotifyTouchBarItemInteraction(
  449. const std::string& item_id,
  450. const base::DictionaryValue& details) {
  451. for (NativeWindowObserver& observer : observers_)
  452. observer.OnTouchBarItemResult(item_id, details);
  453. }
  454. void NativeWindow::NotifyNewWindowForTab() {
  455. for (NativeWindowObserver& observer : observers_)
  456. observer.OnNewWindowForTab();
  457. }
  458. #if defined(OS_WIN)
  459. void NativeWindow::NotifyWindowMessage(UINT message,
  460. WPARAM w_param,
  461. LPARAM l_param) {
  462. for (NativeWindowObserver& observer : observers_)
  463. observer.OnWindowMessage(message, w_param, l_param);
  464. }
  465. #endif
  466. views::Widget* NativeWindow::GetWidget() {
  467. return widget();
  468. }
  469. const views::Widget* NativeWindow::GetWidget() const {
  470. return widget();
  471. }
  472. // static
  473. void NativeWindowRelay::CreateForWebContents(
  474. content::WebContents* web_contents,
  475. base::WeakPtr<NativeWindow> window) {
  476. DCHECK(web_contents);
  477. if (!web_contents->GetUserData(UserDataKey())) {
  478. web_contents->SetUserData(UserDataKey(),
  479. base::WrapUnique(new NativeWindowRelay(window)));
  480. }
  481. }
  482. NativeWindowRelay::NativeWindowRelay(base::WeakPtr<NativeWindow> window)
  483. : native_window_(window) {}
  484. NativeWindowRelay::~NativeWindowRelay() = default;
  485. } // namespace atom