native_window.cc 17 KB

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