native_window.cc 16 KB

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