native_window_views.cc 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363
  1. // Copyright (c) 2014 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_views.h"
  5. #if defined(OS_WIN)
  6. #include <wrl/client.h>
  7. #endif
  8. #include <memory>
  9. #include <tuple>
  10. #include <utility>
  11. #include <vector>
  12. #include "atom/browser/api/atom_api_web_contents.h"
  13. #include "atom/browser/native_browser_view_views.h"
  14. #include "atom/browser/ui/inspectable_web_contents.h"
  15. #include "atom/browser/ui/inspectable_web_contents_view.h"
  16. #include "atom/browser/ui/views/root_view.h"
  17. #include "atom/browser/web_contents_preferences.h"
  18. #include "atom/browser/web_view_manager.h"
  19. #include "atom/browser/window_list.h"
  20. #include "atom/common/atom_constants.h"
  21. #include "atom/common/draggable_region.h"
  22. #include "atom/common/native_mate_converters/image_converter.h"
  23. #include "atom/common/options_switches.h"
  24. #include "base/numerics/ranges.h"
  25. #include "base/strings/utf_string_conversions.h"
  26. #include "content/public/browser/browser_thread.h"
  27. #include "native_mate/dictionary.h"
  28. #include "ui/aura/window_tree_host.h"
  29. #include "ui/base/hit_test.h"
  30. #include "ui/gfx/image/image.h"
  31. #include "ui/gfx/native_widget_types.h"
  32. #include "ui/views/background.h"
  33. #include "ui/views/controls/webview/unhandled_keyboard_event_handler.h"
  34. #include "ui/views/controls/webview/webview.h"
  35. #include "ui/views/widget/native_widget_private.h"
  36. #include "ui/views/widget/widget.h"
  37. #include "ui/views/window/client_view.h"
  38. #include "ui/wm/core/shadow_types.h"
  39. #include "ui/wm/core/window_util.h"
  40. #if defined(USE_X11)
  41. #include "atom/browser/browser.h"
  42. #include "atom/browser/ui/views/frameless_view.h"
  43. #include "atom/browser/ui/views/global_menu_bar_x11.h"
  44. #include "atom/browser/ui/views/native_frame_view.h"
  45. #include "atom/browser/ui/x/event_disabler.h"
  46. #include "atom/browser/ui/x/window_state_watcher.h"
  47. #include "atom/browser/ui/x/x_window_utils.h"
  48. #include "base/strings/string_util.h"
  49. #include "chrome/browser/ui/libgtkui/unity_service.h"
  50. #include "ui/base/x/x11_util.h"
  51. #include "ui/gfx/x/x11_types.h"
  52. #include "ui/views/widget/desktop_aura/desktop_window_tree_host_x11.h"
  53. #include "ui/views/window/native_frame_view.h"
  54. #elif defined(OS_WIN)
  55. #include "atom/browser/ui/views/win_frame_view.h"
  56. #include "atom/browser/ui/win/atom_desktop_native_widget_aura.h"
  57. #include "skia/ext/skia_utils_win.h"
  58. #include "ui/base/win/shell.h"
  59. #include "ui/display/screen.h"
  60. #include "ui/display/win/screen_win.h"
  61. #include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h"
  62. #endif
  63. namespace atom {
  64. namespace {
  65. #if defined(OS_WIN)
  66. void FlipWindowStyle(HWND handle, bool on, DWORD flag) {
  67. DWORD style = ::GetWindowLong(handle, GWL_STYLE);
  68. if (on)
  69. style |= flag;
  70. else
  71. style &= ~flag;
  72. ::SetWindowLong(handle, GWL_STYLE, style);
  73. }
  74. // Similar to the ones in display::win::ScreenWin, but with rounded values
  75. // These help to avoid problems that arise from unresizable windows where the
  76. // original ceil()-ed values can cause calculation errors, since converting
  77. // both ways goes through a ceil() call. Related issue: #15816
  78. gfx::Rect ScreenToDIPRect(HWND hwnd, const gfx::Rect& pixel_bounds) {
  79. float scale_factor = display::win::ScreenWin::GetScaleFactorForHWND(hwnd);
  80. gfx::Rect dip_rect = ScaleToRoundedRect(pixel_bounds, 1.0f / scale_factor);
  81. dip_rect.set_origin(
  82. display::win::ScreenWin::ScreenToDIPRect(hwnd, pixel_bounds).origin());
  83. return dip_rect;
  84. }
  85. gfx::Rect DIPToScreenRect(HWND hwnd, const gfx::Rect& pixel_bounds) {
  86. float scale_factor = display::win::ScreenWin::GetScaleFactorForHWND(hwnd);
  87. gfx::Rect screen_rect = ScaleToRoundedRect(pixel_bounds, scale_factor);
  88. screen_rect.set_origin(
  89. display::win::ScreenWin::DIPToScreenRect(hwnd, pixel_bounds).origin());
  90. return screen_rect;
  91. }
  92. #endif
  93. class NativeWindowClientView : public views::ClientView {
  94. public:
  95. NativeWindowClientView(views::Widget* widget,
  96. views::View* root_view,
  97. NativeWindowViews* window)
  98. : views::ClientView(widget, root_view), window_(window) {}
  99. ~NativeWindowClientView() override = default;
  100. bool CanClose() override {
  101. window_->NotifyWindowCloseButtonClicked();
  102. return false;
  103. }
  104. private:
  105. NativeWindowViews* window_;
  106. DISALLOW_COPY_AND_ASSIGN(NativeWindowClientView);
  107. };
  108. } // namespace
  109. NativeWindowViews::NativeWindowViews(const mate::Dictionary& options,
  110. NativeWindow* parent)
  111. : NativeWindow(options, parent),
  112. root_view_(new RootView(this)),
  113. keyboard_event_handler_(new views::UnhandledKeyboardEventHandler) {
  114. options.Get(options::kTitle, &title_);
  115. bool menu_bar_autohide;
  116. if (options.Get(options::kAutoHideMenuBar, &menu_bar_autohide))
  117. root_view_->SetAutoHideMenuBar(menu_bar_autohide);
  118. #if defined(OS_WIN)
  119. // On Windows we rely on the CanResize() to indicate whether window can be
  120. // resized, and it should be set before window is created.
  121. options.Get(options::kResizable, &resizable_);
  122. options.Get(options::kMinimizable, &minimizable_);
  123. options.Get(options::kMaximizable, &maximizable_);
  124. // Transparent window must not have thick frame.
  125. options.Get("thickFrame", &thick_frame_);
  126. if (transparent())
  127. thick_frame_ = false;
  128. #endif
  129. if (enable_larger_than_screen())
  130. // We need to set a default maximum window size here otherwise Windows
  131. // will not allow us to resize the window larger than scree.
  132. // Setting directly to INT_MAX somehow doesn't work, so we just devide
  133. // by 10, which should still be large enough.
  134. SetContentSizeConstraints(extensions::SizeConstraints(
  135. gfx::Size(), gfx::Size(INT_MAX / 10, INT_MAX / 10)));
  136. int width = 800, height = 600;
  137. options.Get(options::kWidth, &width);
  138. options.Get(options::kHeight, &height);
  139. gfx::Rect bounds(0, 0, width, height);
  140. widget_size_ = bounds.size();
  141. widget()->AddObserver(this);
  142. views::Widget::InitParams params;
  143. params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
  144. params.bounds = bounds;
  145. params.delegate = this;
  146. params.type = views::Widget::InitParams::TYPE_WINDOW;
  147. params.remove_standard_frame = !has_frame();
  148. if (transparent())
  149. params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
  150. // The given window is most likely not rectangular since it uses
  151. // transparency and has no standard frame, don't show a shadow for it.
  152. if (transparent() && !has_frame())
  153. params.shadow_type = views::Widget::InitParams::SHADOW_TYPE_NONE;
  154. bool focusable;
  155. if (options.Get(options::kFocusable, &focusable) && !focusable)
  156. params.activatable = views::Widget::InitParams::ACTIVATABLE_NO;
  157. #if defined(OS_WIN)
  158. if (parent)
  159. params.parent = parent->GetNativeWindow();
  160. params.native_widget = new AtomDesktopNativeWidgetAura(this);
  161. #elif defined(USE_X11)
  162. std::string name = Browser::Get()->GetName();
  163. // Set WM_WINDOW_ROLE.
  164. params.wm_role_name = "browser-window";
  165. // Set WM_CLASS.
  166. params.wm_class_name = base::ToLowerASCII(name);
  167. params.wm_class_class = name;
  168. #endif
  169. widget()->Init(params);
  170. bool fullscreen = false;
  171. options.Get(options::kFullscreen, &fullscreen);
  172. std::string window_type;
  173. options.Get(options::kType, &window_type);
  174. #if defined(USE_X11)
  175. // Start monitoring window states.
  176. window_state_watcher_.reset(new WindowStateWatcher(this));
  177. // Set _GTK_THEME_VARIANT to dark if we have "dark-theme" option set.
  178. bool use_dark_theme = false;
  179. if (options.Get(options::kDarkTheme, &use_dark_theme) && use_dark_theme) {
  180. XDisplay* xdisplay = gfx::GetXDisplay();
  181. XChangeProperty(xdisplay, GetAcceleratedWidget(),
  182. XInternAtom(xdisplay, "_GTK_THEME_VARIANT", x11::False),
  183. XInternAtom(xdisplay, "UTF8_STRING", x11::False), 8,
  184. PropModeReplace,
  185. reinterpret_cast<const unsigned char*>("dark"), 4);
  186. }
  187. // Before the window is mapped the SetWMSpecState can not work, so we have
  188. // to manually set the _NET_WM_STATE.
  189. std::vector<::Atom> state_atom_list;
  190. bool skip_taskbar = false;
  191. if (options.Get(options::kSkipTaskbar, &skip_taskbar) && skip_taskbar) {
  192. state_atom_list.push_back(GetAtom("_NET_WM_STATE_SKIP_TASKBAR"));
  193. }
  194. // Before the window is mapped, there is no SHOW_FULLSCREEN_STATE.
  195. if (fullscreen) {
  196. state_atom_list.push_back(GetAtom("_NET_WM_STATE_FULLSCREEN"));
  197. }
  198. if (parent) {
  199. SetParentWindow(parent);
  200. // Force using dialog type for child window.
  201. window_type = "dialog";
  202. // Modal window needs the _NET_WM_STATE_MODAL hint.
  203. if (is_modal())
  204. state_atom_list.push_back(GetAtom("_NET_WM_STATE_MODAL"));
  205. }
  206. if (!state_atom_list.empty())
  207. ui::SetAtomArrayProperty(GetAcceleratedWidget(), "_NET_WM_STATE", "ATOM",
  208. state_atom_list);
  209. // Set the _NET_WM_WINDOW_TYPE.
  210. if (!window_type.empty())
  211. SetWindowType(GetAcceleratedWidget(), window_type);
  212. #endif
  213. #if defined(OS_WIN)
  214. if (!has_frame()) {
  215. // Set Window style so that we get a minimize and maximize animation when
  216. // frameless.
  217. DWORD frame_style = WS_CAPTION | WS_OVERLAPPED;
  218. if (resizable_)
  219. frame_style |= WS_THICKFRAME;
  220. if (minimizable_)
  221. frame_style |= WS_MINIMIZEBOX;
  222. if (maximizable_)
  223. frame_style |= WS_MAXIMIZEBOX;
  224. // We should not show a frame for transparent window.
  225. if (!thick_frame_)
  226. frame_style &= ~(WS_THICKFRAME | WS_CAPTION);
  227. ::SetWindowLong(GetAcceleratedWidget(), GWL_STYLE, frame_style);
  228. }
  229. LONG ex_style = ::GetWindowLong(GetAcceleratedWidget(), GWL_EXSTYLE);
  230. if (window_type == "toolbar")
  231. ex_style |= WS_EX_TOOLWINDOW;
  232. ::SetWindowLong(GetAcceleratedWidget(), GWL_EXSTYLE, ex_style);
  233. #endif
  234. if (has_frame()) {
  235. // TODO(zcbenz): This was used to force using native frame on Windows 2003,
  236. // we should check whether setting it in InitParams can work.
  237. widget()->set_frame_type(views::Widget::FrameType::FRAME_TYPE_FORCE_NATIVE);
  238. widget()->FrameTypeChanged();
  239. #if defined(OS_WIN)
  240. // thickFrame also works for normal window.
  241. if (!thick_frame_)
  242. FlipWindowStyle(GetAcceleratedWidget(), false, WS_THICKFRAME);
  243. #endif
  244. }
  245. // Default content view.
  246. SetContentView(new views::View());
  247. gfx::Size size = bounds.size();
  248. if (has_frame() &&
  249. options.Get(options::kUseContentSize, &use_content_size_) &&
  250. use_content_size_)
  251. size = ContentBoundsToWindowBounds(gfx::Rect(size)).size();
  252. widget()->CenterWindow(size);
  253. #if defined(OS_WIN)
  254. // Save initial window state.
  255. if (fullscreen)
  256. last_window_state_ = ui::SHOW_STATE_FULLSCREEN;
  257. else
  258. last_window_state_ = ui::SHOW_STATE_NORMAL;
  259. #endif
  260. #if defined(OS_LINUX)
  261. // Listen to move events.
  262. aura::Window* window = GetNativeWindow();
  263. if (window)
  264. window->AddPreTargetHandler(this);
  265. #endif
  266. }
  267. NativeWindowViews::~NativeWindowViews() {
  268. widget()->RemoveObserver(this);
  269. #if defined(OS_WIN)
  270. // Disable mouse forwarding to relinquish resources, should any be held.
  271. SetForwardMouseMessages(false);
  272. #endif
  273. #if defined(OS_LINUX)
  274. aura::Window* window = GetNativeWindow();
  275. if (window)
  276. window->RemovePreTargetHandler(this);
  277. #endif
  278. }
  279. void NativeWindowViews::SetContentView(views::View* view) {
  280. if (content_view()) {
  281. root_view_->RemoveChildView(content_view());
  282. }
  283. set_content_view(view);
  284. focused_view_ = view;
  285. root_view_->AddChildView(content_view());
  286. root_view_->Layout();
  287. }
  288. void NativeWindowViews::Close() {
  289. if (!IsClosable()) {
  290. WindowList::WindowCloseCancelled(this);
  291. return;
  292. }
  293. widget()->Close();
  294. }
  295. void NativeWindowViews::CloseImmediately() {
  296. widget()->CloseNow();
  297. }
  298. void NativeWindowViews::Focus(bool focus) {
  299. // For hidden window focus() should do nothing.
  300. if (!IsVisible())
  301. return;
  302. if (focus) {
  303. widget()->Activate();
  304. } else {
  305. widget()->Deactivate();
  306. }
  307. }
  308. bool NativeWindowViews::IsFocused() {
  309. return widget()->IsActive();
  310. }
  311. void NativeWindowViews::Show() {
  312. if (is_modal() && NativeWindow::parent() &&
  313. !widget()->native_widget_private()->IsVisible())
  314. static_cast<NativeWindowViews*>(parent())->IncrementChildModals();
  315. widget()->native_widget_private()->Show(GetRestoredState(), gfx::Rect());
  316. // explicitly focus the window
  317. widget()->Activate();
  318. NotifyWindowShow();
  319. #if defined(USE_X11)
  320. if (global_menu_bar_)
  321. global_menu_bar_->OnWindowMapped();
  322. #endif
  323. }
  324. void NativeWindowViews::ShowInactive() {
  325. widget()->ShowInactive();
  326. NotifyWindowShow();
  327. #if defined(USE_X11)
  328. if (global_menu_bar_)
  329. global_menu_bar_->OnWindowMapped();
  330. #endif
  331. }
  332. void NativeWindowViews::Hide() {
  333. if (is_modal() && NativeWindow::parent())
  334. static_cast<NativeWindowViews*>(parent())->DecrementChildModals();
  335. widget()->Hide();
  336. NotifyWindowHide();
  337. #if defined(USE_X11)
  338. if (global_menu_bar_)
  339. global_menu_bar_->OnWindowUnmapped();
  340. #endif
  341. }
  342. bool NativeWindowViews::IsVisible() {
  343. return widget()->IsVisible();
  344. }
  345. bool NativeWindowViews::IsEnabled() {
  346. #if defined(OS_WIN)
  347. return ::IsWindowEnabled(GetAcceleratedWidget());
  348. #elif defined(USE_X11)
  349. return !event_disabler_.get();
  350. #endif
  351. }
  352. void NativeWindowViews::IncrementChildModals() {
  353. num_modal_children_++;
  354. SetEnabledInternal(ShouldBeEnabled());
  355. }
  356. void NativeWindowViews::DecrementChildModals() {
  357. if (num_modal_children_ > 0) {
  358. num_modal_children_--;
  359. }
  360. SetEnabledInternal(ShouldBeEnabled());
  361. }
  362. void NativeWindowViews::SetEnabled(bool enable) {
  363. if (enable != is_enabled_) {
  364. is_enabled_ = enable;
  365. SetEnabledInternal(ShouldBeEnabled());
  366. }
  367. }
  368. bool NativeWindowViews::ShouldBeEnabled() {
  369. return is_enabled_ && (num_modal_children_ == 0);
  370. }
  371. void NativeWindowViews::SetEnabledInternal(bool enable) {
  372. if (enable && IsEnabled()) {
  373. return;
  374. } else if (!enable && !IsEnabled()) {
  375. return;
  376. }
  377. #if defined(OS_WIN)
  378. ::EnableWindow(GetAcceleratedWidget(), enable);
  379. #elif defined(USE_X11)
  380. views::DesktopWindowTreeHostX11* tree_host =
  381. views::DesktopWindowTreeHostX11::GetHostForXID(GetAcceleratedWidget());
  382. if (enable) {
  383. tree_host->RemoveEventRewriter(event_disabler_.get());
  384. event_disabler_.reset();
  385. } else {
  386. event_disabler_.reset(new EventDisabler);
  387. tree_host->AddEventRewriter(event_disabler_.get());
  388. }
  389. #endif
  390. }
  391. #if defined(USE_X11)
  392. void NativeWindowViews::Maximize() {
  393. if (IsVisible())
  394. widget()->Maximize();
  395. else
  396. widget()->native_widget_private()->Show(ui::SHOW_STATE_MAXIMIZED,
  397. gfx::Rect());
  398. }
  399. #endif
  400. void NativeWindowViews::Unmaximize() {
  401. #if defined(OS_WIN)
  402. if (!(::GetWindowLong(GetAcceleratedWidget(), GWL_STYLE) & WS_THICKFRAME)) {
  403. SetBounds(restore_bounds_, false);
  404. return;
  405. }
  406. #endif
  407. widget()->Restore();
  408. }
  409. bool NativeWindowViews::IsMaximized() {
  410. return widget()->IsMaximized();
  411. }
  412. void NativeWindowViews::Minimize() {
  413. if (IsVisible())
  414. widget()->Minimize();
  415. else
  416. widget()->native_widget_private()->Show(ui::SHOW_STATE_MINIMIZED,
  417. gfx::Rect());
  418. }
  419. void NativeWindowViews::Restore() {
  420. widget()->Restore();
  421. }
  422. bool NativeWindowViews::IsMinimized() {
  423. return widget()->IsMinimized();
  424. }
  425. void NativeWindowViews::SetFullScreen(bool fullscreen) {
  426. if (!IsFullScreenable())
  427. return;
  428. #if defined(OS_WIN)
  429. // There is no native fullscreen state on Windows.
  430. bool leaving_fullscreen = IsFullscreen() && !fullscreen;
  431. if (fullscreen) {
  432. last_window_state_ = ui::SHOW_STATE_FULLSCREEN;
  433. NotifyWindowEnterFullScreen();
  434. } else {
  435. last_window_state_ = ui::SHOW_STATE_NORMAL;
  436. NotifyWindowLeaveFullScreen();
  437. }
  438. // For window without WS_THICKFRAME style, we can not call SetFullscreen().
  439. // This path will be used for transparent windows as well.
  440. if (!thick_frame_) {
  441. if (fullscreen) {
  442. restore_bounds_ = GetBounds();
  443. auto display =
  444. display::Screen::GetScreen()->GetDisplayNearestPoint(GetPosition());
  445. SetBounds(display.bounds(), false);
  446. } else {
  447. SetBounds(restore_bounds_, false);
  448. }
  449. return;
  450. }
  451. // We set the new value after notifying, so we can handle the size event
  452. // correctly.
  453. widget()->SetFullscreen(fullscreen);
  454. // If restoring from fullscreen and the window isn't visible, force visible,
  455. // else a non-responsive window shell could be rendered.
  456. // (this situation may arise when app starts with fullscreen: true)
  457. // Note: the following must be after "widget()->SetFullscreen(fullscreen);"
  458. if (leaving_fullscreen && !IsVisible())
  459. FlipWindowStyle(GetAcceleratedWidget(), true, WS_VISIBLE);
  460. #else
  461. if (IsVisible())
  462. widget()->SetFullscreen(fullscreen);
  463. else if (fullscreen)
  464. widget()->native_widget_private()->Show(ui::SHOW_STATE_FULLSCREEN,
  465. gfx::Rect());
  466. // Auto-hide menubar when in fullscreen.
  467. if (fullscreen)
  468. SetMenuBarVisibility(false);
  469. else
  470. SetMenuBarVisibility(!IsMenuBarAutoHide());
  471. #endif
  472. }
  473. bool NativeWindowViews::IsFullscreen() const {
  474. return widget()->IsFullscreen();
  475. }
  476. void NativeWindowViews::SetBounds(const gfx::Rect& bounds, bool animate) {
  477. #if defined(OS_WIN) || defined(USE_X11)
  478. // On Linux and Windows the minimum and maximum size should be updated with
  479. // window size when window is not resizable.
  480. if (!resizable_) {
  481. SetMaximumSize(bounds.size());
  482. SetMinimumSize(bounds.size());
  483. }
  484. #endif
  485. widget()->SetBounds(bounds);
  486. }
  487. gfx::Rect NativeWindowViews::GetBounds() {
  488. #if defined(OS_WIN)
  489. if (IsMinimized())
  490. return widget()->GetRestoredBounds();
  491. #endif
  492. return widget()->GetWindowBoundsInScreen();
  493. }
  494. gfx::Rect NativeWindowViews::GetContentBounds() {
  495. return content_view() ? content_view()->GetBoundsInScreen() : gfx::Rect();
  496. }
  497. gfx::Size NativeWindowViews::GetContentSize() {
  498. #if defined(OS_WIN)
  499. if (IsMinimized())
  500. return NativeWindow::GetContentSize();
  501. #endif
  502. return content_view() ? content_view()->size() : gfx::Size();
  503. }
  504. gfx::Rect NativeWindowViews::GetNormalBounds() {
  505. return widget()->GetRestoredBounds();
  506. }
  507. void NativeWindowViews::SetContentSizeConstraints(
  508. const extensions::SizeConstraints& size_constraints) {
  509. NativeWindow::SetContentSizeConstraints(size_constraints);
  510. #if defined(OS_WIN)
  511. // Changing size constraints would force adding the WS_THICKFRAME style, so
  512. // do nothing if thickFrame is false.
  513. if (!thick_frame_)
  514. return;
  515. #endif
  516. // widget_delegate() is only available after Init() is called, we make use of
  517. // this to determine whether native widget has initialized.
  518. if (widget() && widget()->widget_delegate())
  519. widget()->OnSizeConstraintsChanged();
  520. if (resizable_)
  521. old_size_constraints_ = size_constraints;
  522. }
  523. void NativeWindowViews::SetResizable(bool resizable) {
  524. if (resizable != resizable_) {
  525. // On Linux there is no "resizable" property of a window, we have to set
  526. // both the minimum and maximum size to the window size to achieve it.
  527. if (resizable) {
  528. SetContentSizeConstraints(old_size_constraints_);
  529. SetMaximizable(maximizable_);
  530. } else {
  531. old_size_constraints_ = GetContentSizeConstraints();
  532. resizable_ = false;
  533. gfx::Size content_size = GetContentSize();
  534. SetContentSizeConstraints(
  535. extensions::SizeConstraints(content_size, content_size));
  536. }
  537. }
  538. #if defined(OS_WIN)
  539. if (has_frame() && thick_frame_)
  540. FlipWindowStyle(GetAcceleratedWidget(), resizable, WS_THICKFRAME);
  541. #endif
  542. resizable_ = resizable;
  543. }
  544. void NativeWindowViews::MoveTop() {
  545. // TODO(julien.isorce): fix chromium in order to use existing
  546. // widget()->StackAtTop().
  547. #if defined(OS_WIN)
  548. gfx::Point pos = GetPosition();
  549. gfx::Size size = GetSize();
  550. ::SetWindowPos(GetAcceleratedWidget(), HWND_TOP, pos.x(), pos.y(),
  551. size.width(), size.height(),
  552. SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
  553. #elif defined(USE_X11)
  554. atom::MoveWindowToForeground(GetAcceleratedWidget());
  555. #endif
  556. }
  557. bool NativeWindowViews::IsResizable() {
  558. #if defined(OS_WIN)
  559. if (has_frame())
  560. return ::GetWindowLong(GetAcceleratedWidget(), GWL_STYLE) & WS_THICKFRAME;
  561. #endif
  562. return CanResize();
  563. }
  564. void NativeWindowViews::SetMovable(bool movable) {
  565. movable_ = movable;
  566. }
  567. bool NativeWindowViews::IsMovable() {
  568. #if defined(OS_WIN)
  569. return movable_;
  570. #else
  571. return true; // Not implemented on Linux.
  572. #endif
  573. }
  574. void NativeWindowViews::SetMinimizable(bool minimizable) {
  575. #if defined(OS_WIN)
  576. FlipWindowStyle(GetAcceleratedWidget(), minimizable, WS_MINIMIZEBOX);
  577. #endif
  578. minimizable_ = minimizable;
  579. }
  580. bool NativeWindowViews::IsMinimizable() {
  581. #if defined(OS_WIN)
  582. return ::GetWindowLong(GetAcceleratedWidget(), GWL_STYLE) & WS_MINIMIZEBOX;
  583. #else
  584. return true; // Not implemented on Linux.
  585. #endif
  586. }
  587. void NativeWindowViews::SetMaximizable(bool maximizable) {
  588. #if defined(OS_WIN)
  589. FlipWindowStyle(GetAcceleratedWidget(), maximizable, WS_MAXIMIZEBOX);
  590. #endif
  591. maximizable_ = maximizable;
  592. }
  593. bool NativeWindowViews::IsMaximizable() {
  594. #if defined(OS_WIN)
  595. return ::GetWindowLong(GetAcceleratedWidget(), GWL_STYLE) & WS_MAXIMIZEBOX;
  596. #else
  597. return true; // Not implemented on Linux.
  598. #endif
  599. }
  600. void NativeWindowViews::SetExcludedFromShownWindowsMenu(bool excluded) {}
  601. bool NativeWindowViews::IsExcludedFromShownWindowsMenu() {
  602. // return false on unsupported platforms
  603. return false;
  604. }
  605. void NativeWindowViews::SetFullScreenable(bool fullscreenable) {
  606. fullscreenable_ = fullscreenable;
  607. }
  608. bool NativeWindowViews::IsFullScreenable() {
  609. return fullscreenable_;
  610. }
  611. void NativeWindowViews::SetClosable(bool closable) {
  612. #if defined(OS_WIN)
  613. HMENU menu = GetSystemMenu(GetAcceleratedWidget(), false);
  614. if (closable) {
  615. EnableMenuItem(menu, SC_CLOSE, MF_BYCOMMAND | MF_ENABLED);
  616. } else {
  617. EnableMenuItem(menu, SC_CLOSE, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
  618. }
  619. #endif
  620. }
  621. bool NativeWindowViews::IsClosable() {
  622. #if defined(OS_WIN)
  623. HMENU menu = GetSystemMenu(GetAcceleratedWidget(), false);
  624. MENUITEMINFO info;
  625. memset(&info, 0, sizeof(info));
  626. info.cbSize = sizeof(info);
  627. info.fMask = MIIM_STATE;
  628. if (!GetMenuItemInfo(menu, SC_CLOSE, false, &info)) {
  629. return false;
  630. }
  631. return !(info.fState & MFS_DISABLED);
  632. #elif defined(USE_X11)
  633. return true;
  634. #endif
  635. }
  636. void NativeWindowViews::SetAlwaysOnTop(bool top,
  637. const std::string& level,
  638. int relativeLevel,
  639. std::string* error) {
  640. bool level_changed = top != widget()->IsAlwaysOnTop();
  641. widget()->SetAlwaysOnTop(top);
  642. // This must be notified at the very end or IsAlwaysOnTop
  643. // will not yet have been updated to reflect the new status
  644. if (level_changed)
  645. NativeWindow::NotifyWindowAlwaysOnTopChanged();
  646. }
  647. bool NativeWindowViews::IsAlwaysOnTop() {
  648. return widget()->IsAlwaysOnTop();
  649. }
  650. void NativeWindowViews::Center() {
  651. widget()->CenterWindow(GetSize());
  652. }
  653. void NativeWindowViews::Invalidate() {
  654. widget()->SchedulePaintInRect(gfx::Rect(GetBounds().size()));
  655. }
  656. void NativeWindowViews::SetTitle(const std::string& title) {
  657. title_ = title;
  658. widget()->UpdateWindowTitle();
  659. }
  660. std::string NativeWindowViews::GetTitle() {
  661. return title_;
  662. }
  663. void NativeWindowViews::FlashFrame(bool flash) {
  664. #if defined(OS_WIN)
  665. // The Chromium's implementation has a bug stopping flash.
  666. if (!flash) {
  667. FLASHWINFO fwi;
  668. fwi.cbSize = sizeof(fwi);
  669. fwi.hwnd = GetAcceleratedWidget();
  670. fwi.dwFlags = FLASHW_STOP;
  671. fwi.uCount = 0;
  672. FlashWindowEx(&fwi);
  673. return;
  674. }
  675. #endif
  676. widget()->FlashFrame(flash);
  677. }
  678. void NativeWindowViews::SetSkipTaskbar(bool skip) {
  679. #if defined(OS_WIN)
  680. Microsoft::WRL::ComPtr<ITaskbarList> taskbar;
  681. if (FAILED(::CoCreateInstance(CLSID_TaskbarList, nullptr,
  682. CLSCTX_INPROC_SERVER,
  683. IID_PPV_ARGS(&taskbar))) ||
  684. FAILED(taskbar->HrInit()))
  685. return;
  686. if (skip) {
  687. taskbar->DeleteTab(GetAcceleratedWidget());
  688. } else {
  689. taskbar->AddTab(GetAcceleratedWidget());
  690. taskbar_host_.RestoreThumbarButtons(GetAcceleratedWidget());
  691. }
  692. #elif defined(USE_X11)
  693. SetWMSpecState(GetAcceleratedWidget(), skip,
  694. GetAtom("_NET_WM_STATE_SKIP_TASKBAR"));
  695. #endif
  696. }
  697. void NativeWindowViews::SetSimpleFullScreen(bool simple_fullscreen) {
  698. SetFullScreen(simple_fullscreen);
  699. }
  700. bool NativeWindowViews::IsSimpleFullScreen() {
  701. return IsFullscreen();
  702. }
  703. void NativeWindowViews::SetKiosk(bool kiosk) {
  704. SetFullScreen(kiosk);
  705. }
  706. bool NativeWindowViews::IsKiosk() {
  707. return IsFullscreen();
  708. }
  709. void NativeWindowViews::SetBackgroundColor(SkColor background_color) {
  710. // web views' background color.
  711. root_view_->SetBackground(views::CreateSolidBackground(background_color));
  712. #if defined(OS_WIN)
  713. // Set the background color of native window.
  714. HBRUSH brush = CreateSolidBrush(skia::SkColorToCOLORREF(background_color));
  715. ULONG_PTR previous_brush =
  716. SetClassLongPtr(GetAcceleratedWidget(), GCLP_HBRBACKGROUND,
  717. reinterpret_cast<LONG_PTR>(brush));
  718. if (previous_brush)
  719. DeleteObject((HBRUSH)previous_brush);
  720. InvalidateRect(GetAcceleratedWidget(), NULL, 1);
  721. #endif
  722. }
  723. void NativeWindowViews::SetHasShadow(bool has_shadow) {
  724. wm::SetShadowElevation(GetNativeWindow(),
  725. has_shadow ? wm::kShadowElevationInactiveWindow
  726. : wm::kShadowElevationNone);
  727. }
  728. bool NativeWindowViews::HasShadow() {
  729. return GetNativeWindow()->GetProperty(wm::kShadowElevationKey) !=
  730. wm::kShadowElevationNone;
  731. }
  732. void NativeWindowViews::SetOpacity(const double opacity) {
  733. #if defined(OS_WIN)
  734. const double boundedOpacity = base::ClampToRange(opacity, 0.0, 1.0);
  735. HWND hwnd = GetAcceleratedWidget();
  736. if (!layered_) {
  737. LONG ex_style = ::GetWindowLong(hwnd, GWL_EXSTYLE);
  738. ex_style |= WS_EX_LAYERED;
  739. ::SetWindowLong(hwnd, GWL_EXSTYLE, ex_style);
  740. layered_ = true;
  741. }
  742. ::SetLayeredWindowAttributes(hwnd, 0, boundedOpacity * 255, LWA_ALPHA);
  743. opacity_ = boundedOpacity;
  744. #else
  745. opacity_ = 1.0; // setOpacity unsupported on Linux
  746. #endif
  747. }
  748. double NativeWindowViews::GetOpacity() {
  749. return opacity_;
  750. }
  751. void NativeWindowViews::SetIgnoreMouseEvents(bool ignore, bool forward) {
  752. #if defined(OS_WIN)
  753. LONG ex_style = ::GetWindowLong(GetAcceleratedWidget(), GWL_EXSTYLE);
  754. if (ignore)
  755. ex_style |= (WS_EX_TRANSPARENT | WS_EX_LAYERED);
  756. else
  757. ex_style &= ~(WS_EX_TRANSPARENT | WS_EX_LAYERED);
  758. if (layered_)
  759. ex_style |= WS_EX_LAYERED;
  760. ::SetWindowLong(GetAcceleratedWidget(), GWL_EXSTYLE, ex_style);
  761. // Forwarding is always disabled when not ignoring mouse messages.
  762. if (!ignore) {
  763. SetForwardMouseMessages(false);
  764. } else {
  765. SetForwardMouseMessages(forward);
  766. }
  767. #elif defined(USE_X11)
  768. if (ignore) {
  769. XRectangle r = {0, 0, 1, 1};
  770. XShapeCombineRectangles(gfx::GetXDisplay(), GetAcceleratedWidget(),
  771. ShapeInput, 0, 0, &r, 1, ShapeSet, YXBanded);
  772. } else {
  773. XShapeCombineMask(gfx::GetXDisplay(), GetAcceleratedWidget(), ShapeInput, 0,
  774. 0, x11::None, ShapeSet);
  775. }
  776. #endif
  777. }
  778. void NativeWindowViews::SetContentProtection(bool enable) {
  779. #if defined(OS_WIN)
  780. DWORD affinity = enable ? WDA_MONITOR : WDA_NONE;
  781. ::SetWindowDisplayAffinity(GetAcceleratedWidget(), affinity);
  782. #endif
  783. }
  784. void NativeWindowViews::SetFocusable(bool focusable) {
  785. #if defined(OS_WIN)
  786. LONG ex_style = ::GetWindowLong(GetAcceleratedWidget(), GWL_EXSTYLE);
  787. if (focusable)
  788. ex_style &= ~WS_EX_NOACTIVATE;
  789. else
  790. ex_style |= WS_EX_NOACTIVATE;
  791. ::SetWindowLong(GetAcceleratedWidget(), GWL_EXSTYLE, ex_style);
  792. SetSkipTaskbar(!focusable);
  793. Focus(false);
  794. #endif
  795. }
  796. void NativeWindowViews::SetMenu(AtomMenuModel* menu_model) {
  797. #if defined(USE_X11)
  798. if (menu_model == nullptr) {
  799. global_menu_bar_.reset();
  800. root_view_->UnregisterAcceleratorsWithFocusManager();
  801. return;
  802. }
  803. if (!global_menu_bar_ && ShouldUseGlobalMenuBar())
  804. global_menu_bar_.reset(new GlobalMenuBarX11(this));
  805. // Use global application menu bar when possible.
  806. if (global_menu_bar_ && global_menu_bar_->IsServerStarted()) {
  807. root_view_->RegisterAcceleratorsWithFocusManager(menu_model);
  808. global_menu_bar_->SetMenu(menu_model);
  809. return;
  810. }
  811. #endif
  812. // Should reset content size when setting menu.
  813. gfx::Size content_size = GetContentSize();
  814. bool should_reset_size = use_content_size_ && has_frame() &&
  815. !IsMenuBarAutoHide() &&
  816. ((!!menu_model) != root_view_->HasMenu());
  817. root_view_->SetMenu(menu_model);
  818. if (should_reset_size) {
  819. // Enlarge the size constraints for the menu.
  820. int menu_bar_height = root_view_->GetMenuBarHeight();
  821. extensions::SizeConstraints constraints = GetContentSizeConstraints();
  822. if (constraints.HasMinimumSize()) {
  823. gfx::Size min_size = constraints.GetMinimumSize();
  824. min_size.set_height(min_size.height() + menu_bar_height);
  825. constraints.set_minimum_size(min_size);
  826. }
  827. if (constraints.HasMaximumSize()) {
  828. gfx::Size max_size = constraints.GetMaximumSize();
  829. max_size.set_height(max_size.height() + menu_bar_height);
  830. constraints.set_maximum_size(max_size);
  831. }
  832. SetContentSizeConstraints(constraints);
  833. // Resize the window to make sure content size is not changed.
  834. SetContentSize(content_size);
  835. }
  836. }
  837. void NativeWindowViews::AddBrowserView(NativeBrowserView* view) {
  838. if (!content_view())
  839. return;
  840. if (!view) {
  841. return;
  842. }
  843. add_browser_view(view);
  844. content_view()->AddChildView(
  845. view->GetInspectableWebContentsView()->GetView());
  846. }
  847. void NativeWindowViews::RemoveBrowserView(NativeBrowserView* view) {
  848. if (!content_view())
  849. return;
  850. if (!view) {
  851. return;
  852. }
  853. content_view()->RemoveChildView(
  854. view->GetInspectableWebContentsView()->GetView());
  855. remove_browser_view(view);
  856. }
  857. void NativeWindowViews::SetParentWindow(NativeWindow* parent) {
  858. NativeWindow::SetParentWindow(parent);
  859. #if defined(USE_X11)
  860. XDisplay* xdisplay = gfx::GetXDisplay();
  861. XSetTransientForHint(
  862. xdisplay, GetAcceleratedWidget(),
  863. parent ? parent->GetAcceleratedWidget() : DefaultRootWindow(xdisplay));
  864. #elif defined(OS_WIN)
  865. // To set parentship between windows into Windows is better to play with the
  866. // owner instead of the parent, as Windows natively seems to do if a parent
  867. // is specified at window creation time.
  868. // For do this we must NOT use the ::SetParent function, instead we must use
  869. // the ::GetWindowLongPtr or ::SetWindowLongPtr functions with "nIndex" set
  870. // to "GWLP_HWNDPARENT" which actually means the window owner.
  871. HWND hwndParent = parent ? parent->GetAcceleratedWidget() : NULL;
  872. if (hwndParent ==
  873. (HWND)::GetWindowLongPtr(GetAcceleratedWidget(), GWLP_HWNDPARENT))
  874. return;
  875. ::SetWindowLongPtr(GetAcceleratedWidget(), GWLP_HWNDPARENT,
  876. (LONG_PTR)hwndParent);
  877. // Ensures the visibility
  878. if (IsVisible()) {
  879. WINDOWPLACEMENT wp;
  880. wp.length = sizeof(WINDOWPLACEMENT);
  881. ::GetWindowPlacement(GetAcceleratedWidget(), &wp);
  882. ::ShowWindow(GetAcceleratedWidget(), SW_HIDE);
  883. ::ShowWindow(GetAcceleratedWidget(), wp.showCmd);
  884. ::BringWindowToTop(GetAcceleratedWidget());
  885. }
  886. #endif
  887. }
  888. gfx::NativeView NativeWindowViews::GetNativeView() const {
  889. return widget()->GetNativeView();
  890. }
  891. gfx::NativeWindow NativeWindowViews::GetNativeWindow() const {
  892. return widget()->GetNativeWindow();
  893. }
  894. void NativeWindowViews::SetProgressBar(double progress,
  895. NativeWindow::ProgressState state) {
  896. #if defined(OS_WIN)
  897. taskbar_host_.SetProgressBar(GetAcceleratedWidget(), progress, state);
  898. #elif defined(USE_X11)
  899. if (unity::IsRunning()) {
  900. unity::SetProgressFraction(progress);
  901. }
  902. #endif
  903. }
  904. void NativeWindowViews::SetOverlayIcon(const gfx::Image& overlay,
  905. const std::string& description) {
  906. #if defined(OS_WIN)
  907. taskbar_host_.SetOverlayIcon(GetAcceleratedWidget(), overlay, description);
  908. #endif
  909. }
  910. void NativeWindowViews::SetAutoHideMenuBar(bool auto_hide) {
  911. root_view_->SetAutoHideMenuBar(auto_hide);
  912. }
  913. bool NativeWindowViews::IsMenuBarAutoHide() {
  914. return root_view_->IsMenuBarAutoHide();
  915. }
  916. void NativeWindowViews::SetMenuBarVisibility(bool visible) {
  917. root_view_->SetMenuBarVisibility(visible);
  918. }
  919. bool NativeWindowViews::IsMenuBarVisible() {
  920. return root_view_->IsMenuBarVisible();
  921. }
  922. void NativeWindowViews::SetVisibleOnAllWorkspaces(bool visible,
  923. bool visibleOnFullScreen) {
  924. widget()->SetVisibleOnAllWorkspaces(visible);
  925. }
  926. bool NativeWindowViews::IsVisibleOnAllWorkspaces() {
  927. #if defined(USE_X11)
  928. // Use the presence/absence of _NET_WM_STATE_STICKY in _NET_WM_STATE to
  929. // determine whether the current window is visible on all workspaces.
  930. XAtom sticky_atom = GetAtom("_NET_WM_STATE_STICKY");
  931. std::vector<XAtom> wm_states;
  932. ui::GetAtomArrayProperty(GetAcceleratedWidget(), "_NET_WM_STATE", &wm_states);
  933. return std::find(wm_states.begin(), wm_states.end(), sticky_atom) !=
  934. wm_states.end();
  935. #endif
  936. return false;
  937. }
  938. gfx::AcceleratedWidget NativeWindowViews::GetAcceleratedWidget() const {
  939. if (GetNativeWindow() && GetNativeWindow()->GetHost())
  940. return GetNativeWindow()->GetHost()->GetAcceleratedWidget();
  941. else
  942. return gfx::kNullAcceleratedWidget;
  943. }
  944. NativeWindowHandle NativeWindowViews::GetNativeWindowHandle() const {
  945. return GetAcceleratedWidget();
  946. }
  947. gfx::Rect NativeWindowViews::ContentBoundsToWindowBounds(
  948. const gfx::Rect& bounds) const {
  949. if (!has_frame())
  950. return bounds;
  951. gfx::Rect window_bounds(bounds);
  952. #if defined(OS_WIN)
  953. HWND hwnd = GetAcceleratedWidget();
  954. gfx::Rect dpi_bounds = DIPToScreenRect(hwnd, bounds);
  955. window_bounds = ScreenToDIPRect(
  956. hwnd,
  957. widget()->non_client_view()->GetWindowBoundsForClientBounds(dpi_bounds));
  958. #endif
  959. if (root_view_->HasMenu() && root_view_->IsMenuBarVisible()) {
  960. int menu_bar_height = root_view_->GetMenuBarHeight();
  961. window_bounds.set_y(window_bounds.y() - menu_bar_height);
  962. window_bounds.set_height(window_bounds.height() + menu_bar_height);
  963. }
  964. return window_bounds;
  965. }
  966. gfx::Rect NativeWindowViews::WindowBoundsToContentBounds(
  967. const gfx::Rect& bounds) const {
  968. if (!has_frame())
  969. return bounds;
  970. gfx::Rect content_bounds(bounds);
  971. #if defined(OS_WIN)
  972. HWND hwnd = GetAcceleratedWidget();
  973. content_bounds.set_size(DIPToScreenRect(hwnd, content_bounds).size());
  974. RECT rect;
  975. SetRectEmpty(&rect);
  976. DWORD style = ::GetWindowLong(hwnd, GWL_STYLE);
  977. DWORD ex_style = ::GetWindowLong(hwnd, GWL_EXSTYLE);
  978. AdjustWindowRectEx(&rect, style, FALSE, ex_style);
  979. content_bounds.set_width(content_bounds.width() - (rect.right - rect.left));
  980. content_bounds.set_height(content_bounds.height() - (rect.bottom - rect.top));
  981. content_bounds.set_size(ScreenToDIPRect(hwnd, content_bounds).size());
  982. #endif
  983. if (root_view_->HasMenu() && root_view_->IsMenuBarVisible()) {
  984. int menu_bar_height = root_view_->GetMenuBarHeight();
  985. content_bounds.set_y(content_bounds.y() + menu_bar_height);
  986. content_bounds.set_height(content_bounds.height() - menu_bar_height);
  987. }
  988. return content_bounds;
  989. }
  990. void NativeWindowViews::UpdateDraggableRegions(
  991. std::unique_ptr<SkRegion> region) {
  992. draggable_region_ = std::move(region);
  993. }
  994. #if defined(OS_WIN)
  995. void NativeWindowViews::SetIcon(HICON window_icon, HICON app_icon) {
  996. // We are responsible for storing the images.
  997. window_icon_ = base::win::ScopedHICON(CopyIcon(window_icon));
  998. app_icon_ = base::win::ScopedHICON(CopyIcon(app_icon));
  999. HWND hwnd = GetAcceleratedWidget();
  1000. SendMessage(hwnd, WM_SETICON, ICON_SMALL,
  1001. reinterpret_cast<LPARAM>(window_icon_.get()));
  1002. SendMessage(hwnd, WM_SETICON, ICON_BIG,
  1003. reinterpret_cast<LPARAM>(app_icon_.get()));
  1004. }
  1005. #elif defined(USE_X11)
  1006. void NativeWindowViews::SetIcon(const gfx::ImageSkia& icon) {
  1007. auto* tree_host = static_cast<views::DesktopWindowTreeHost*>(
  1008. views::DesktopWindowTreeHostX11::GetHostForXID(GetAcceleratedWidget()));
  1009. tree_host->SetWindowIcons(icon, icon);
  1010. }
  1011. #endif
  1012. void NativeWindowViews::OnWidgetActivationChanged(views::Widget* changed_widget,
  1013. bool active) {
  1014. if (changed_widget != widget())
  1015. return;
  1016. if (active)
  1017. NativeWindow::NotifyWindowFocus();
  1018. else
  1019. NativeWindow::NotifyWindowBlur();
  1020. // Hide menu bar when window is blured.
  1021. if (!active && IsMenuBarAutoHide() && IsMenuBarVisible())
  1022. SetMenuBarVisibility(false);
  1023. root_view_->ResetAltState();
  1024. }
  1025. void NativeWindowViews::OnWidgetBoundsChanged(views::Widget* changed_widget,
  1026. const gfx::Rect& bounds) {
  1027. if (changed_widget != widget())
  1028. return;
  1029. // Note: We intentionally use `GetBounds()` instead of `bounds` to properly
  1030. // handle minimized windows on Windows.
  1031. const auto new_bounds = GetBounds();
  1032. if (widget_size_ != new_bounds.size()) {
  1033. int width_delta = new_bounds.width() - widget_size_.width();
  1034. int height_delta = new_bounds.height() - widget_size_.height();
  1035. for (NativeBrowserView* item : browser_views()) {
  1036. NativeBrowserViewViews* native_view =
  1037. static_cast<NativeBrowserViewViews*>(item);
  1038. native_view->SetAutoResizeProportions(widget_size_);
  1039. native_view->AutoResize(new_bounds, width_delta, height_delta);
  1040. }
  1041. NotifyWindowResize();
  1042. widget_size_ = new_bounds.size();
  1043. }
  1044. }
  1045. void NativeWindowViews::OnWidgetDestroying(views::Widget* widget) {
  1046. #if defined(OS_LINUX)
  1047. aura::Window* window = GetNativeWindow();
  1048. if (window)
  1049. window->RemovePreTargetHandler(this);
  1050. #endif
  1051. }
  1052. void NativeWindowViews::DeleteDelegate() {
  1053. if (is_modal() && this->parent()) {
  1054. auto* parent = this->parent();
  1055. // Enable parent window after current window gets closed.
  1056. static_cast<NativeWindowViews*>(parent)->DecrementChildModals();
  1057. // Focus on parent window.
  1058. parent->Focus(true);
  1059. }
  1060. NotifyWindowClosed();
  1061. }
  1062. views::View* NativeWindowViews::GetInitiallyFocusedView() {
  1063. return focused_view_;
  1064. }
  1065. bool NativeWindowViews::CanResize() const {
  1066. return resizable_;
  1067. }
  1068. bool NativeWindowViews::CanMaximize() const {
  1069. return resizable_ && maximizable_;
  1070. }
  1071. bool NativeWindowViews::CanMinimize() const {
  1072. #if defined(OS_WIN)
  1073. return minimizable_;
  1074. #elif defined(USE_X11)
  1075. return true;
  1076. #endif
  1077. }
  1078. base::string16 NativeWindowViews::GetWindowTitle() const {
  1079. return base::UTF8ToUTF16(title_);
  1080. }
  1081. views::View* NativeWindowViews::GetContentsView() {
  1082. return root_view_.get();
  1083. }
  1084. bool NativeWindowViews::ShouldDescendIntoChildForEventHandling(
  1085. gfx::NativeView child,
  1086. const gfx::Point& location) {
  1087. // App window should claim mouse events that fall within the draggable region.
  1088. if (draggable_region() &&
  1089. draggable_region()->contains(location.x(), location.y()))
  1090. return false;
  1091. // And the events on border for dragging resizable frameless window.
  1092. if (!has_frame() && CanResize()) {
  1093. FramelessView* frame =
  1094. static_cast<FramelessView*>(widget()->non_client_view()->frame_view());
  1095. return frame->ResizingBorderHitTest(location) == HTNOWHERE;
  1096. }
  1097. return true;
  1098. }
  1099. views::ClientView* NativeWindowViews::CreateClientView(views::Widget* widget) {
  1100. return new NativeWindowClientView(widget, root_view_.get(), this);
  1101. }
  1102. views::NonClientFrameView* NativeWindowViews::CreateNonClientFrameView(
  1103. views::Widget* widget) {
  1104. #if defined(OS_WIN)
  1105. WinFrameView* frame_view = new WinFrameView;
  1106. frame_view->Init(this, widget);
  1107. return frame_view;
  1108. #else
  1109. if (has_frame()) {
  1110. return new NativeFrameView(this, widget);
  1111. } else {
  1112. FramelessView* frame_view = new FramelessView;
  1113. frame_view->Init(this, widget);
  1114. return frame_view;
  1115. }
  1116. #endif
  1117. }
  1118. void NativeWindowViews::OnWidgetMove() {
  1119. NotifyWindowMove();
  1120. }
  1121. void NativeWindowViews::HandleKeyboardEvent(
  1122. content::WebContents*,
  1123. const content::NativeWebKeyboardEvent& event) {
  1124. #if defined(OS_LINUX)
  1125. if (event.windows_key_code == ui::VKEY_BROWSER_BACK)
  1126. NotifyWindowExecuteAppCommand(kBrowserBackward);
  1127. else if (event.windows_key_code == ui::VKEY_BROWSER_FORWARD)
  1128. NotifyWindowExecuteAppCommand(kBrowserForward);
  1129. #endif
  1130. keyboard_event_handler_->HandleKeyboardEvent(event,
  1131. root_view_->GetFocusManager());
  1132. root_view_->HandleKeyEvent(event);
  1133. }
  1134. #if defined(OS_LINUX)
  1135. void NativeWindowViews::OnMouseEvent(ui::MouseEvent* event) {
  1136. if (event->type() != ui::ET_MOUSE_PRESSED)
  1137. return;
  1138. if (event->changed_button_flags() == ui::EF_BACK_MOUSE_BUTTON)
  1139. NotifyWindowExecuteAppCommand(kBrowserBackward);
  1140. else if (event->changed_button_flags() == ui::EF_FORWARD_MOUSE_BUTTON)
  1141. NotifyWindowExecuteAppCommand(kBrowserForward);
  1142. }
  1143. #endif
  1144. ui::WindowShowState NativeWindowViews::GetRestoredState() {
  1145. if (IsMaximized())
  1146. return ui::SHOW_STATE_MAXIMIZED;
  1147. if (IsFullscreen())
  1148. return ui::SHOW_STATE_FULLSCREEN;
  1149. return ui::SHOW_STATE_NORMAL;
  1150. }
  1151. // static
  1152. NativeWindow* NativeWindow::Create(const mate::Dictionary& options,
  1153. NativeWindow* parent) {
  1154. return new NativeWindowViews(options, parent);
  1155. }
  1156. } // namespace atom