native_window.cc 18 KB

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