native_window_views.cc 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364
  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. widget()->widget_delegate()->SetCanActivate(focusable);
  786. #if defined(OS_WIN)
  787. LONG ex_style = ::GetWindowLong(GetAcceleratedWidget(), GWL_EXSTYLE);
  788. if (focusable)
  789. ex_style &= ~WS_EX_NOACTIVATE;
  790. else
  791. ex_style |= WS_EX_NOACTIVATE;
  792. ::SetWindowLong(GetAcceleratedWidget(), GWL_EXSTYLE, ex_style);
  793. SetSkipTaskbar(!focusable);
  794. Focus(false);
  795. #endif
  796. }
  797. void NativeWindowViews::SetMenu(AtomMenuModel* menu_model) {
  798. #if defined(USE_X11)
  799. if (menu_model == nullptr) {
  800. global_menu_bar_.reset();
  801. root_view_->UnregisterAcceleratorsWithFocusManager();
  802. return;
  803. }
  804. if (!global_menu_bar_ && ShouldUseGlobalMenuBar())
  805. global_menu_bar_.reset(new GlobalMenuBarX11(this));
  806. // Use global application menu bar when possible.
  807. if (global_menu_bar_ && global_menu_bar_->IsServerStarted()) {
  808. root_view_->RegisterAcceleratorsWithFocusManager(menu_model);
  809. global_menu_bar_->SetMenu(menu_model);
  810. return;
  811. }
  812. #endif
  813. // Should reset content size when setting menu.
  814. gfx::Size content_size = GetContentSize();
  815. bool should_reset_size = use_content_size_ && has_frame() &&
  816. !IsMenuBarAutoHide() &&
  817. ((!!menu_model) != root_view_->HasMenu());
  818. root_view_->SetMenu(menu_model);
  819. if (should_reset_size) {
  820. // Enlarge the size constraints for the menu.
  821. int menu_bar_height = root_view_->GetMenuBarHeight();
  822. extensions::SizeConstraints constraints = GetContentSizeConstraints();
  823. if (constraints.HasMinimumSize()) {
  824. gfx::Size min_size = constraints.GetMinimumSize();
  825. min_size.set_height(min_size.height() + menu_bar_height);
  826. constraints.set_minimum_size(min_size);
  827. }
  828. if (constraints.HasMaximumSize()) {
  829. gfx::Size max_size = constraints.GetMaximumSize();
  830. max_size.set_height(max_size.height() + menu_bar_height);
  831. constraints.set_maximum_size(max_size);
  832. }
  833. SetContentSizeConstraints(constraints);
  834. // Resize the window to make sure content size is not changed.
  835. SetContentSize(content_size);
  836. }
  837. }
  838. void NativeWindowViews::AddBrowserView(NativeBrowserView* view) {
  839. if (!content_view())
  840. return;
  841. if (!view) {
  842. return;
  843. }
  844. add_browser_view(view);
  845. content_view()->AddChildView(
  846. view->GetInspectableWebContentsView()->GetView());
  847. }
  848. void NativeWindowViews::RemoveBrowserView(NativeBrowserView* view) {
  849. if (!content_view())
  850. return;
  851. if (!view) {
  852. return;
  853. }
  854. content_view()->RemoveChildView(
  855. view->GetInspectableWebContentsView()->GetView());
  856. remove_browser_view(view);
  857. }
  858. void NativeWindowViews::SetParentWindow(NativeWindow* parent) {
  859. NativeWindow::SetParentWindow(parent);
  860. #if defined(USE_X11)
  861. XDisplay* xdisplay = gfx::GetXDisplay();
  862. XSetTransientForHint(
  863. xdisplay, GetAcceleratedWidget(),
  864. parent ? parent->GetAcceleratedWidget() : DefaultRootWindow(xdisplay));
  865. #elif defined(OS_WIN)
  866. // To set parentship between windows into Windows is better to play with the
  867. // owner instead of the parent, as Windows natively seems to do if a parent
  868. // is specified at window creation time.
  869. // For do this we must NOT use the ::SetParent function, instead we must use
  870. // the ::GetWindowLongPtr or ::SetWindowLongPtr functions with "nIndex" set
  871. // to "GWLP_HWNDPARENT" which actually means the window owner.
  872. HWND hwndParent = parent ? parent->GetAcceleratedWidget() : NULL;
  873. if (hwndParent ==
  874. (HWND)::GetWindowLongPtr(GetAcceleratedWidget(), GWLP_HWNDPARENT))
  875. return;
  876. ::SetWindowLongPtr(GetAcceleratedWidget(), GWLP_HWNDPARENT,
  877. (LONG_PTR)hwndParent);
  878. // Ensures the visibility
  879. if (IsVisible()) {
  880. WINDOWPLACEMENT wp;
  881. wp.length = sizeof(WINDOWPLACEMENT);
  882. ::GetWindowPlacement(GetAcceleratedWidget(), &wp);
  883. ::ShowWindow(GetAcceleratedWidget(), SW_HIDE);
  884. ::ShowWindow(GetAcceleratedWidget(), wp.showCmd);
  885. ::BringWindowToTop(GetAcceleratedWidget());
  886. }
  887. #endif
  888. }
  889. gfx::NativeView NativeWindowViews::GetNativeView() const {
  890. return widget()->GetNativeView();
  891. }
  892. gfx::NativeWindow NativeWindowViews::GetNativeWindow() const {
  893. return widget()->GetNativeWindow();
  894. }
  895. void NativeWindowViews::SetProgressBar(double progress,
  896. NativeWindow::ProgressState state) {
  897. #if defined(OS_WIN)
  898. taskbar_host_.SetProgressBar(GetAcceleratedWidget(), progress, state);
  899. #elif defined(USE_X11)
  900. if (unity::IsRunning()) {
  901. unity::SetProgressFraction(progress);
  902. }
  903. #endif
  904. }
  905. void NativeWindowViews::SetOverlayIcon(const gfx::Image& overlay,
  906. const std::string& description) {
  907. #if defined(OS_WIN)
  908. taskbar_host_.SetOverlayIcon(GetAcceleratedWidget(), overlay, description);
  909. #endif
  910. }
  911. void NativeWindowViews::SetAutoHideMenuBar(bool auto_hide) {
  912. root_view_->SetAutoHideMenuBar(auto_hide);
  913. }
  914. bool NativeWindowViews::IsMenuBarAutoHide() {
  915. return root_view_->IsMenuBarAutoHide();
  916. }
  917. void NativeWindowViews::SetMenuBarVisibility(bool visible) {
  918. root_view_->SetMenuBarVisibility(visible);
  919. }
  920. bool NativeWindowViews::IsMenuBarVisible() {
  921. return root_view_->IsMenuBarVisible();
  922. }
  923. void NativeWindowViews::SetVisibleOnAllWorkspaces(bool visible,
  924. bool visibleOnFullScreen) {
  925. widget()->SetVisibleOnAllWorkspaces(visible);
  926. }
  927. bool NativeWindowViews::IsVisibleOnAllWorkspaces() {
  928. #if defined(USE_X11)
  929. // Use the presence/absence of _NET_WM_STATE_STICKY in _NET_WM_STATE to
  930. // determine whether the current window is visible on all workspaces.
  931. XAtom sticky_atom = GetAtom("_NET_WM_STATE_STICKY");
  932. std::vector<XAtom> wm_states;
  933. ui::GetAtomArrayProperty(GetAcceleratedWidget(), "_NET_WM_STATE", &wm_states);
  934. return std::find(wm_states.begin(), wm_states.end(), sticky_atom) !=
  935. wm_states.end();
  936. #endif
  937. return false;
  938. }
  939. gfx::AcceleratedWidget NativeWindowViews::GetAcceleratedWidget() const {
  940. if (GetNativeWindow() && GetNativeWindow()->GetHost())
  941. return GetNativeWindow()->GetHost()->GetAcceleratedWidget();
  942. else
  943. return gfx::kNullAcceleratedWidget;
  944. }
  945. NativeWindowHandle NativeWindowViews::GetNativeWindowHandle() const {
  946. return GetAcceleratedWidget();
  947. }
  948. gfx::Rect NativeWindowViews::ContentBoundsToWindowBounds(
  949. const gfx::Rect& bounds) const {
  950. if (!has_frame())
  951. return bounds;
  952. gfx::Rect window_bounds(bounds);
  953. #if defined(OS_WIN)
  954. HWND hwnd = GetAcceleratedWidget();
  955. gfx::Rect dpi_bounds = DIPToScreenRect(hwnd, bounds);
  956. window_bounds = ScreenToDIPRect(
  957. hwnd,
  958. widget()->non_client_view()->GetWindowBoundsForClientBounds(dpi_bounds));
  959. #endif
  960. if (root_view_->HasMenu() && root_view_->IsMenuBarVisible()) {
  961. int menu_bar_height = root_view_->GetMenuBarHeight();
  962. window_bounds.set_y(window_bounds.y() - menu_bar_height);
  963. window_bounds.set_height(window_bounds.height() + menu_bar_height);
  964. }
  965. return window_bounds;
  966. }
  967. gfx::Rect NativeWindowViews::WindowBoundsToContentBounds(
  968. const gfx::Rect& bounds) const {
  969. if (!has_frame())
  970. return bounds;
  971. gfx::Rect content_bounds(bounds);
  972. #if defined(OS_WIN)
  973. HWND hwnd = GetAcceleratedWidget();
  974. content_bounds.set_size(DIPToScreenRect(hwnd, content_bounds).size());
  975. RECT rect;
  976. SetRectEmpty(&rect);
  977. DWORD style = ::GetWindowLong(hwnd, GWL_STYLE);
  978. DWORD ex_style = ::GetWindowLong(hwnd, GWL_EXSTYLE);
  979. AdjustWindowRectEx(&rect, style, FALSE, ex_style);
  980. content_bounds.set_width(content_bounds.width() - (rect.right - rect.left));
  981. content_bounds.set_height(content_bounds.height() - (rect.bottom - rect.top));
  982. content_bounds.set_size(ScreenToDIPRect(hwnd, content_bounds).size());
  983. #endif
  984. if (root_view_->HasMenu() && root_view_->IsMenuBarVisible()) {
  985. int menu_bar_height = root_view_->GetMenuBarHeight();
  986. content_bounds.set_y(content_bounds.y() + menu_bar_height);
  987. content_bounds.set_height(content_bounds.height() - menu_bar_height);
  988. }
  989. return content_bounds;
  990. }
  991. void NativeWindowViews::UpdateDraggableRegions(
  992. std::unique_ptr<SkRegion> region) {
  993. draggable_region_ = std::move(region);
  994. }
  995. #if defined(OS_WIN)
  996. void NativeWindowViews::SetIcon(HICON window_icon, HICON app_icon) {
  997. // We are responsible for storing the images.
  998. window_icon_ = base::win::ScopedHICON(CopyIcon(window_icon));
  999. app_icon_ = base::win::ScopedHICON(CopyIcon(app_icon));
  1000. HWND hwnd = GetAcceleratedWidget();
  1001. SendMessage(hwnd, WM_SETICON, ICON_SMALL,
  1002. reinterpret_cast<LPARAM>(window_icon_.get()));
  1003. SendMessage(hwnd, WM_SETICON, ICON_BIG,
  1004. reinterpret_cast<LPARAM>(app_icon_.get()));
  1005. }
  1006. #elif defined(USE_X11)
  1007. void NativeWindowViews::SetIcon(const gfx::ImageSkia& icon) {
  1008. auto* tree_host = static_cast<views::DesktopWindowTreeHost*>(
  1009. views::DesktopWindowTreeHostX11::GetHostForXID(GetAcceleratedWidget()));
  1010. tree_host->SetWindowIcons(icon, icon);
  1011. }
  1012. #endif
  1013. void NativeWindowViews::OnWidgetActivationChanged(views::Widget* changed_widget,
  1014. bool active) {
  1015. if (changed_widget != widget())
  1016. return;
  1017. if (active)
  1018. NativeWindow::NotifyWindowFocus();
  1019. else
  1020. NativeWindow::NotifyWindowBlur();
  1021. // Hide menu bar when window is blured.
  1022. if (!active && IsMenuBarAutoHide() && IsMenuBarVisible())
  1023. SetMenuBarVisibility(false);
  1024. root_view_->ResetAltState();
  1025. }
  1026. void NativeWindowViews::OnWidgetBoundsChanged(views::Widget* changed_widget,
  1027. const gfx::Rect& bounds) {
  1028. if (changed_widget != widget())
  1029. return;
  1030. // Note: We intentionally use `GetBounds()` instead of `bounds` to properly
  1031. // handle minimized windows on Windows.
  1032. const auto new_bounds = GetBounds();
  1033. if (widget_size_ != new_bounds.size()) {
  1034. int width_delta = new_bounds.width() - widget_size_.width();
  1035. int height_delta = new_bounds.height() - widget_size_.height();
  1036. for (NativeBrowserView* item : browser_views()) {
  1037. NativeBrowserViewViews* native_view =
  1038. static_cast<NativeBrowserViewViews*>(item);
  1039. native_view->SetAutoResizeProportions(widget_size_);
  1040. native_view->AutoResize(new_bounds, width_delta, height_delta);
  1041. }
  1042. NotifyWindowResize();
  1043. widget_size_ = new_bounds.size();
  1044. }
  1045. }
  1046. void NativeWindowViews::OnWidgetDestroying(views::Widget* widget) {
  1047. #if defined(OS_LINUX)
  1048. aura::Window* window = GetNativeWindow();
  1049. if (window)
  1050. window->RemovePreTargetHandler(this);
  1051. #endif
  1052. }
  1053. void NativeWindowViews::DeleteDelegate() {
  1054. if (is_modal() && this->parent()) {
  1055. auto* parent = this->parent();
  1056. // Enable parent window after current window gets closed.
  1057. static_cast<NativeWindowViews*>(parent)->DecrementChildModals();
  1058. // Focus on parent window.
  1059. parent->Focus(true);
  1060. }
  1061. NotifyWindowClosed();
  1062. }
  1063. views::View* NativeWindowViews::GetInitiallyFocusedView() {
  1064. return focused_view_;
  1065. }
  1066. bool NativeWindowViews::CanResize() const {
  1067. return resizable_;
  1068. }
  1069. bool NativeWindowViews::CanMaximize() const {
  1070. return resizable_ && maximizable_;
  1071. }
  1072. bool NativeWindowViews::CanMinimize() const {
  1073. #if defined(OS_WIN)
  1074. return minimizable_;
  1075. #elif defined(USE_X11)
  1076. return true;
  1077. #endif
  1078. }
  1079. base::string16 NativeWindowViews::GetWindowTitle() const {
  1080. return base::UTF8ToUTF16(title_);
  1081. }
  1082. views::View* NativeWindowViews::GetContentsView() {
  1083. return root_view_.get();
  1084. }
  1085. bool NativeWindowViews::ShouldDescendIntoChildForEventHandling(
  1086. gfx::NativeView child,
  1087. const gfx::Point& location) {
  1088. // App window should claim mouse events that fall within the draggable region.
  1089. if (draggable_region() &&
  1090. draggable_region()->contains(location.x(), location.y()))
  1091. return false;
  1092. // And the events on border for dragging resizable frameless window.
  1093. if (!has_frame() && CanResize()) {
  1094. FramelessView* frame =
  1095. static_cast<FramelessView*>(widget()->non_client_view()->frame_view());
  1096. return frame->ResizingBorderHitTest(location) == HTNOWHERE;
  1097. }
  1098. return true;
  1099. }
  1100. views::ClientView* NativeWindowViews::CreateClientView(views::Widget* widget) {
  1101. return new NativeWindowClientView(widget, root_view_.get(), this);
  1102. }
  1103. views::NonClientFrameView* NativeWindowViews::CreateNonClientFrameView(
  1104. views::Widget* widget) {
  1105. #if defined(OS_WIN)
  1106. WinFrameView* frame_view = new WinFrameView;
  1107. frame_view->Init(this, widget);
  1108. return frame_view;
  1109. #else
  1110. if (has_frame()) {
  1111. return new NativeFrameView(this, widget);
  1112. } else {
  1113. FramelessView* frame_view = new FramelessView;
  1114. frame_view->Init(this, widget);
  1115. return frame_view;
  1116. }
  1117. #endif
  1118. }
  1119. void NativeWindowViews::OnWidgetMove() {
  1120. NotifyWindowMove();
  1121. }
  1122. void NativeWindowViews::HandleKeyboardEvent(
  1123. content::WebContents*,
  1124. const content::NativeWebKeyboardEvent& event) {
  1125. #if defined(OS_LINUX)
  1126. if (event.windows_key_code == ui::VKEY_BROWSER_BACK)
  1127. NotifyWindowExecuteAppCommand(kBrowserBackward);
  1128. else if (event.windows_key_code == ui::VKEY_BROWSER_FORWARD)
  1129. NotifyWindowExecuteAppCommand(kBrowserForward);
  1130. #endif
  1131. keyboard_event_handler_->HandleKeyboardEvent(event,
  1132. root_view_->GetFocusManager());
  1133. root_view_->HandleKeyEvent(event);
  1134. }
  1135. #if defined(OS_LINUX)
  1136. void NativeWindowViews::OnMouseEvent(ui::MouseEvent* event) {
  1137. if (event->type() != ui::ET_MOUSE_PRESSED)
  1138. return;
  1139. if (event->changed_button_flags() == ui::EF_BACK_MOUSE_BUTTON)
  1140. NotifyWindowExecuteAppCommand(kBrowserBackward);
  1141. else if (event->changed_button_flags() == ui::EF_FORWARD_MOUSE_BUTTON)
  1142. NotifyWindowExecuteAppCommand(kBrowserForward);
  1143. }
  1144. #endif
  1145. ui::WindowShowState NativeWindowViews::GetRestoredState() {
  1146. if (IsMaximized())
  1147. return ui::SHOW_STATE_MAXIMIZED;
  1148. if (IsFullscreen())
  1149. return ui::SHOW_STATE_FULLSCREEN;
  1150. return ui::SHOW_STATE_NORMAL;
  1151. }
  1152. // static
  1153. NativeWindow* NativeWindow::Create(const mate::Dictionary& options,
  1154. NativeWindow* parent) {
  1155. return new NativeWindowViews(options, parent);
  1156. }
  1157. } // namespace atom