native_window_views.cc 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738
  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 "shell/browser/native_window_views.h"
  5. #if BUILDFLAG(IS_WIN)
  6. #include <wrl/client.h>
  7. #endif
  8. #include <memory>
  9. #include <utility>
  10. #include <vector>
  11. #include "base/cxx17_backports.h"
  12. #include "base/stl_util.h"
  13. #include "base/strings/utf_string_conversions.h"
  14. #include "content/public/browser/browser_thread.h"
  15. #include "content/public/browser/desktop_media_id.h"
  16. #include "shell/browser/api/electron_api_web_contents.h"
  17. #include "shell/browser/native_browser_view_views.h"
  18. #include "shell/browser/ui/drag_util.h"
  19. #include "shell/browser/ui/inspectable_web_contents.h"
  20. #include "shell/browser/ui/inspectable_web_contents_view.h"
  21. #include "shell/browser/ui/views/root_view.h"
  22. #include "shell/browser/web_contents_preferences.h"
  23. #include "shell/browser/web_view_manager.h"
  24. #include "shell/browser/window_list.h"
  25. #include "shell/common/electron_constants.h"
  26. #include "shell/common/gin_converters/image_converter.h"
  27. #include "shell/common/gin_helper/dictionary.h"
  28. #include "shell/common/options_switches.h"
  29. #include "ui/aura/window_tree_host.h"
  30. #include "ui/base/hit_test.h"
  31. #include "ui/gfx/image/image.h"
  32. #include "ui/gfx/native_widget_types.h"
  33. #include "ui/views/background.h"
  34. #include "ui/views/controls/webview/unhandled_keyboard_event_handler.h"
  35. #include "ui/views/controls/webview/webview.h"
  36. #include "ui/views/widget/native_widget_private.h"
  37. #include "ui/views/widget/widget.h"
  38. #include "ui/views/window/client_view.h"
  39. #include "ui/wm/core/shadow_types.h"
  40. #include "ui/wm/core/window_util.h"
  41. #if BUILDFLAG(IS_LINUX)
  42. #include "base/strings/string_util.h"
  43. #include "shell/browser/browser.h"
  44. #include "shell/browser/linux/unity_service.h"
  45. #include "shell/browser/ui/electron_desktop_window_tree_host_linux.h"
  46. #include "shell/browser/ui/views/client_frame_view_linux.h"
  47. #include "shell/browser/ui/views/frameless_view.h"
  48. #include "shell/browser/ui/views/native_frame_view.h"
  49. #include "shell/common/platform_util.h"
  50. #include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h"
  51. #include "ui/views/window/native_frame_view.h"
  52. #if defined(USE_OZONE)
  53. #include "shell/browser/ui/views/global_menu_bar_x11.h"
  54. #include "shell/browser/ui/x/event_disabler.h"
  55. #include "shell/browser/ui/x/x_window_utils.h"
  56. #include "ui/base/x/x11_util.h"
  57. #include "ui/gfx/x/shape.h"
  58. #include "ui/gfx/x/x11_atom_cache.h"
  59. #include "ui/gfx/x/xproto.h"
  60. #include "ui/gfx/x/xproto_util.h"
  61. #include "ui/ozone/public/ozone_platform.h"
  62. #endif
  63. #elif BUILDFLAG(IS_WIN)
  64. #include "base/win/win_util.h"
  65. #include "content/public/common/color_parser.h"
  66. #include "shell/browser/ui/views/win_frame_view.h"
  67. #include "shell/browser/ui/win/electron_desktop_native_widget_aura.h"
  68. #include "skia/ext/skia_utils_win.h"
  69. #include "ui/base/win/shell.h"
  70. #include "ui/display/screen.h"
  71. #include "ui/display/win/screen_win.h"
  72. #include "ui/gfx/color_utils.h"
  73. #endif
  74. namespace electron {
  75. #if BUILDFLAG(IS_WIN)
  76. // Similar to the ones in display::win::ScreenWin, but with rounded values
  77. // These help to avoid problems that arise from unresizable windows where the
  78. // original ceil()-ed values can cause calculation errors, since converting
  79. // both ways goes through a ceil() call. Related issue: #15816
  80. gfx::Rect ScreenToDIPRect(HWND hwnd, const gfx::Rect& pixel_bounds) {
  81. float scale_factor = display::win::ScreenWin::GetScaleFactorForHWND(hwnd);
  82. gfx::Rect dip_rect = ScaleToRoundedRect(pixel_bounds, 1.0f / scale_factor);
  83. dip_rect.set_origin(
  84. display::win::ScreenWin::ScreenToDIPRect(hwnd, pixel_bounds).origin());
  85. return dip_rect;
  86. }
  87. #endif
  88. namespace {
  89. #if BUILDFLAG(IS_WIN)
  90. const LPCWSTR kUniqueTaskBarClassName = L"Shell_TrayWnd";
  91. void FlipWindowStyle(HWND handle, bool on, DWORD flag) {
  92. DWORD style = ::GetWindowLong(handle, GWL_STYLE);
  93. if (on)
  94. style |= flag;
  95. else
  96. style &= ~flag;
  97. ::SetWindowLong(handle, GWL_STYLE, style);
  98. // Window's frame styles are cached so we need to call SetWindowPos
  99. // with the SWP_FRAMECHANGED flag to update cache properly.
  100. ::SetWindowPos(handle, 0, 0, 0, 0, 0, // ignored
  101. SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
  102. SWP_NOACTIVATE | SWP_NOOWNERZORDER);
  103. }
  104. gfx::Rect DIPToScreenRect(HWND hwnd, const gfx::Rect& pixel_bounds) {
  105. float scale_factor = display::win::ScreenWin::GetScaleFactorForHWND(hwnd);
  106. gfx::Rect screen_rect = ScaleToRoundedRect(pixel_bounds, scale_factor);
  107. screen_rect.set_origin(
  108. display::win::ScreenWin::DIPToScreenRect(hwnd, pixel_bounds).origin());
  109. return screen_rect;
  110. }
  111. #endif
  112. #if defined(USE_OZONE)
  113. bool CreateGlobalMenuBar() {
  114. return ui::OzonePlatform::GetInstance()
  115. ->GetPlatformProperties()
  116. .supports_global_application_menus;
  117. }
  118. #endif
  119. #if defined(USE_OZONE_PLATFORM_X11)
  120. bool IsX11() {
  121. return ui::OzonePlatform::GetInstance()
  122. ->GetPlatformProperties()
  123. .electron_can_call_x11;
  124. }
  125. #endif
  126. class NativeWindowClientView : public views::ClientView {
  127. public:
  128. NativeWindowClientView(views::Widget* widget,
  129. views::View* root_view,
  130. NativeWindowViews* window)
  131. : views::ClientView(widget, root_view), window_(window) {}
  132. ~NativeWindowClientView() override = default;
  133. // disable copy
  134. NativeWindowClientView(const NativeWindowClientView&) = delete;
  135. NativeWindowClientView& operator=(const NativeWindowClientView&) = delete;
  136. views::CloseRequestResult OnWindowCloseRequested() override {
  137. window_->NotifyWindowCloseButtonClicked();
  138. return views::CloseRequestResult::kCannotClose;
  139. }
  140. private:
  141. NativeWindowViews* window_;
  142. };
  143. } // namespace
  144. NativeWindowViews::NativeWindowViews(const gin_helper::Dictionary& options,
  145. NativeWindow* parent)
  146. : NativeWindow(options, parent),
  147. root_view_(std::make_unique<RootView>(this)),
  148. keyboard_event_handler_(
  149. std::make_unique<views::UnhandledKeyboardEventHandler>()) {
  150. options.Get(options::kTitle, &title_);
  151. bool menu_bar_autohide;
  152. if (options.Get(options::kAutoHideMenuBar, &menu_bar_autohide))
  153. root_view_->SetAutoHideMenuBar(menu_bar_autohide);
  154. #if BUILDFLAG(IS_WIN)
  155. // On Windows we rely on the CanResize() to indicate whether window can be
  156. // resized, and it should be set before window is created.
  157. options.Get(options::kResizable, &resizable_);
  158. options.Get(options::kMinimizable, &minimizable_);
  159. options.Get(options::kMaximizable, &maximizable_);
  160. // Transparent window must not have thick frame.
  161. options.Get("thickFrame", &thick_frame_);
  162. if (transparent())
  163. thick_frame_ = false;
  164. overlay_button_color_ = color_utils::GetSysSkColor(COLOR_BTNFACE);
  165. overlay_symbol_color_ = color_utils::GetSysSkColor(COLOR_BTNTEXT);
  166. v8::Local<v8::Value> titlebar_overlay;
  167. if (options.Get(options::ktitleBarOverlay, &titlebar_overlay) &&
  168. titlebar_overlay->IsObject()) {
  169. gin_helper::Dictionary titlebar_overlay_obj =
  170. gin::Dictionary::CreateEmpty(options.isolate());
  171. options.Get(options::ktitleBarOverlay, &titlebar_overlay_obj);
  172. std::string overlay_color_string;
  173. if (titlebar_overlay_obj.Get(options::kOverlayButtonColor,
  174. &overlay_color_string)) {
  175. bool success = content::ParseCssColorString(overlay_color_string,
  176. &overlay_button_color_);
  177. DCHECK(success);
  178. }
  179. std::string overlay_symbol_color_string;
  180. if (titlebar_overlay_obj.Get(options::kOverlaySymbolColor,
  181. &overlay_symbol_color_string)) {
  182. bool success = content::ParseCssColorString(overlay_symbol_color_string,
  183. &overlay_symbol_color_);
  184. DCHECK(success);
  185. }
  186. }
  187. if (title_bar_style_ != TitleBarStyle::kNormal)
  188. set_has_frame(false);
  189. #endif
  190. if (enable_larger_than_screen())
  191. // We need to set a default maximum window size here otherwise Windows
  192. // will not allow us to resize the window larger than scree.
  193. // Setting directly to INT_MAX somehow doesn't work, so we just divide
  194. // by 10, which should still be large enough.
  195. SetContentSizeConstraints(extensions::SizeConstraints(
  196. gfx::Size(), gfx::Size(INT_MAX / 10, INT_MAX / 10)));
  197. int width = 800, height = 600;
  198. options.Get(options::kWidth, &width);
  199. options.Get(options::kHeight, &height);
  200. gfx::Rect bounds(0, 0, width, height);
  201. widget_size_ = bounds.size();
  202. widget()->AddObserver(this);
  203. views::Widget::InitParams params;
  204. params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
  205. params.bounds = bounds;
  206. params.delegate = this;
  207. params.type = views::Widget::InitParams::TYPE_WINDOW;
  208. params.remove_standard_frame = !has_frame() || has_client_frame();
  209. // If a client frame, we need to draw our own shadows.
  210. if (transparent() || has_client_frame())
  211. params.opacity = views::Widget::InitParams::WindowOpacity::kTranslucent;
  212. // The given window is most likely not rectangular since it uses
  213. // transparency and has no standard frame, don't show a shadow for it.
  214. if (transparent() && !has_frame())
  215. params.shadow_type = views::Widget::InitParams::ShadowType::kNone;
  216. bool focusable;
  217. if (options.Get(options::kFocusable, &focusable) && !focusable)
  218. params.activatable = views::Widget::InitParams::Activatable::kNo;
  219. #if BUILDFLAG(IS_WIN)
  220. if (parent)
  221. params.parent = parent->GetNativeWindow();
  222. params.native_widget = new ElectronDesktopNativeWidgetAura(this);
  223. #elif BUILDFLAG(IS_LINUX)
  224. std::string name = Browser::Get()->GetName();
  225. // Set WM_WINDOW_ROLE.
  226. params.wm_role_name = "browser-window";
  227. // Set WM_CLASS.
  228. params.wm_class_name = base::ToLowerASCII(name);
  229. params.wm_class_class = name;
  230. // Set Wayland application ID.
  231. params.wayland_app_id = platform_util::GetXdgAppId();
  232. auto* native_widget = new views::DesktopNativeWidgetAura(widget());
  233. params.native_widget = native_widget;
  234. params.desktop_window_tree_host =
  235. new ElectronDesktopWindowTreeHostLinux(this, native_widget);
  236. #endif
  237. // Ref https://github.com/electron/electron/issues/30760
  238. // Set the can_resize param before initializing the widget.
  239. // When resizable_ is true, this causes the WS_THICKFRAME style
  240. // to be passed into CreateWindowEx and SetWindowLong calls in
  241. // WindowImpl::Init and HwndMessageHandler::SizeConstraintsChanged
  242. // respectively. As a result, the Windows 7 frame doesn't show,
  243. // but it isn't clear why this is the case.
  244. // When resizable_ is false, WS_THICKFRAME is not passed into the
  245. // SetWindowLong call, so the Windows 7 frame still shows.
  246. // One workaround would be to call set_can_resize(true) here,
  247. // and then move the SetCanResize(resizable_) call after the
  248. // SetWindowLong call around line 365, but that's a much larger change.
  249. set_can_resize(true);
  250. widget()->Init(std::move(params));
  251. // When the workaround above is not needed anymore, only this
  252. // call should be necessary.
  253. // With the workaround in place, this call doesn't do anything.
  254. SetCanResize(resizable_);
  255. bool fullscreen = false;
  256. options.Get(options::kFullscreen, &fullscreen);
  257. std::string window_type;
  258. options.Get(options::kType, &window_type);
  259. #if BUILDFLAG(IS_LINUX)
  260. // Set _GTK_THEME_VARIANT to dark if we have "dark-theme" option set.
  261. bool use_dark_theme = false;
  262. if (options.Get(options::kDarkTheme, &use_dark_theme) && use_dark_theme) {
  263. SetGTKDarkThemeEnabled(use_dark_theme);
  264. }
  265. if (parent)
  266. SetParentWindow(parent);
  267. #endif
  268. #if defined(USE_OZONE_PLATFORM_X11)
  269. if (IsX11()) {
  270. // Before the window is mapped the SetWMSpecState can not work, so we have
  271. // to manually set the _NET_WM_STATE.
  272. std::vector<x11::Atom> state_atom_list;
  273. // Before the window is mapped, there is no SHOW_FULLSCREEN_STATE.
  274. if (fullscreen) {
  275. state_atom_list.push_back(x11::GetAtom("_NET_WM_STATE_FULLSCREEN"));
  276. }
  277. if (parent) {
  278. // Force using dialog type for child window.
  279. window_type = "dialog";
  280. // Modal window needs the _NET_WM_STATE_MODAL hint.
  281. if (is_modal())
  282. state_atom_list.push_back(x11::GetAtom("_NET_WM_STATE_MODAL"));
  283. }
  284. if (!state_atom_list.empty())
  285. SetArrayProperty(static_cast<x11::Window>(GetAcceleratedWidget()),
  286. x11::GetAtom("_NET_WM_STATE"), x11::Atom::ATOM,
  287. state_atom_list);
  288. // Set the _NET_WM_WINDOW_TYPE.
  289. if (!window_type.empty())
  290. SetWindowType(static_cast<x11::Window>(GetAcceleratedWidget()),
  291. window_type);
  292. }
  293. #endif
  294. #if BUILDFLAG(IS_WIN)
  295. if (!has_frame()) {
  296. // Set Window style so that we get a minimize and maximize animation when
  297. // frameless.
  298. DWORD frame_style = WS_CAPTION | WS_OVERLAPPED;
  299. if (resizable_)
  300. frame_style |= WS_THICKFRAME;
  301. if (minimizable_)
  302. frame_style |= WS_MINIMIZEBOX;
  303. if (maximizable_)
  304. frame_style |= WS_MAXIMIZEBOX;
  305. // We should not show a frame for transparent window.
  306. if (!thick_frame_)
  307. frame_style &= ~(WS_THICKFRAME | WS_CAPTION);
  308. ::SetWindowLong(GetAcceleratedWidget(), GWL_STYLE, frame_style);
  309. }
  310. LONG ex_style = ::GetWindowLong(GetAcceleratedWidget(), GWL_EXSTYLE);
  311. if (window_type == "toolbar")
  312. ex_style |= WS_EX_TOOLWINDOW;
  313. ::SetWindowLong(GetAcceleratedWidget(), GWL_EXSTYLE, ex_style);
  314. #endif
  315. if (has_frame() && !has_client_frame()) {
  316. // TODO(zcbenz): This was used to force using native frame on Windows 2003,
  317. // we should check whether setting it in InitParams can work.
  318. widget()->set_frame_type(views::Widget::FrameType::kForceNative);
  319. widget()->FrameTypeChanged();
  320. #if BUILDFLAG(IS_WIN)
  321. // thickFrame also works for normal window.
  322. if (!thick_frame_)
  323. FlipWindowStyle(GetAcceleratedWidget(), false, WS_THICKFRAME);
  324. #endif
  325. }
  326. // Default content view.
  327. SetContentView(new views::View());
  328. gfx::Size size = bounds.size();
  329. if (has_frame() &&
  330. options.Get(options::kUseContentSize, &use_content_size_) &&
  331. use_content_size_)
  332. size = ContentBoundsToWindowBounds(gfx::Rect(size)).size();
  333. widget()->CenterWindow(size);
  334. #if BUILDFLAG(IS_WIN)
  335. // Save initial window state.
  336. if (fullscreen)
  337. last_window_state_ = ui::SHOW_STATE_FULLSCREEN;
  338. else
  339. last_window_state_ = ui::SHOW_STATE_NORMAL;
  340. #endif
  341. // Listen to mouse events.
  342. aura::Window* window = GetNativeWindow();
  343. if (window)
  344. window->AddPreTargetHandler(this);
  345. #if BUILDFLAG(IS_LINUX)
  346. // On linux after the widget is initialized we might have to force set the
  347. // bounds if the bounds are smaller than the current display
  348. SetBounds(gfx::Rect(GetPosition(), bounds.size()), false);
  349. #endif
  350. SetOwnedByWidget(false);
  351. RegisterDeleteDelegateCallback(base::BindOnce(
  352. [](NativeWindowViews* window) {
  353. if (window->is_modal() && window->parent()) {
  354. auto* parent = window->parent();
  355. // Enable parent window after current window gets closed.
  356. static_cast<NativeWindowViews*>(parent)->DecrementChildModals();
  357. // Focus on parent window.
  358. parent->Focus(true);
  359. }
  360. window->NotifyWindowClosed();
  361. },
  362. this));
  363. }
  364. NativeWindowViews::~NativeWindowViews() {
  365. widget()->RemoveObserver(this);
  366. #if BUILDFLAG(IS_WIN)
  367. // Disable mouse forwarding to relinquish resources, should any be held.
  368. SetForwardMouseMessages(false);
  369. #endif
  370. aura::Window* window = GetNativeWindow();
  371. if (window)
  372. window->RemovePreTargetHandler(this);
  373. }
  374. void NativeWindowViews::SetGTKDarkThemeEnabled(bool use_dark_theme) {
  375. #if defined(USE_OZONE_PLATFORM_X11)
  376. if (IsX11()) {
  377. const std::string color = use_dark_theme ? "dark" : "light";
  378. x11::SetStringProperty(static_cast<x11::Window>(GetAcceleratedWidget()),
  379. x11::GetAtom("_GTK_THEME_VARIANT"),
  380. x11::GetAtom("UTF8_STRING"), color);
  381. }
  382. #endif
  383. }
  384. void NativeWindowViews::SetContentView(views::View* view) {
  385. if (content_view()) {
  386. root_view_->RemoveChildView(content_view());
  387. }
  388. set_content_view(view);
  389. focused_view_ = view;
  390. root_view_->AddChildView(content_view());
  391. root_view_->Layout();
  392. }
  393. void NativeWindowViews::Close() {
  394. if (!IsClosable()) {
  395. WindowList::WindowCloseCancelled(this);
  396. return;
  397. }
  398. widget()->Close();
  399. }
  400. void NativeWindowViews::CloseImmediately() {
  401. widget()->CloseNow();
  402. }
  403. void NativeWindowViews::Focus(bool focus) {
  404. // For hidden window focus() should do nothing.
  405. if (!IsVisible())
  406. return;
  407. if (focus) {
  408. widget()->Activate();
  409. } else {
  410. widget()->Deactivate();
  411. }
  412. }
  413. bool NativeWindowViews::IsFocused() {
  414. return widget()->IsActive();
  415. }
  416. void NativeWindowViews::Show() {
  417. if (is_modal() && NativeWindow::parent() &&
  418. !widget()->native_widget_private()->IsVisible())
  419. static_cast<NativeWindowViews*>(parent())->IncrementChildModals();
  420. widget()->native_widget_private()->Show(GetRestoredState(), gfx::Rect());
  421. // explicitly focus the window
  422. widget()->Activate();
  423. NotifyWindowShow();
  424. #if defined(USE_OZONE)
  425. if (global_menu_bar_)
  426. global_menu_bar_->OnWindowMapped();
  427. #endif
  428. #if defined(USE_OZONE_PLATFORM_X11)
  429. // On X11, setting Z order before showing the window doesn't take effect,
  430. // so we have to call it again.
  431. if (IsX11())
  432. widget()->SetZOrderLevel(widget()->GetZOrderLevel());
  433. #endif
  434. }
  435. void NativeWindowViews::ShowInactive() {
  436. widget()->ShowInactive();
  437. NotifyWindowShow();
  438. #if defined(USE_OZONE)
  439. if (global_menu_bar_)
  440. global_menu_bar_->OnWindowMapped();
  441. #endif
  442. }
  443. void NativeWindowViews::Hide() {
  444. if (is_modal() && NativeWindow::parent())
  445. static_cast<NativeWindowViews*>(parent())->DecrementChildModals();
  446. widget()->Hide();
  447. NotifyWindowHide();
  448. #if defined(USE_OZONE)
  449. if (global_menu_bar_)
  450. global_menu_bar_->OnWindowUnmapped();
  451. #endif
  452. #if BUILDFLAG(IS_WIN)
  453. // When the window is removed from the taskbar via win.hide(),
  454. // the thumbnail buttons need to be set up again.
  455. // Ensure that when the window is hidden,
  456. // the taskbar host is notified that it should re-add them.
  457. taskbar_host_.SetThumbarButtonsAdded(false);
  458. #endif
  459. }
  460. bool NativeWindowViews::IsVisible() {
  461. return widget()->IsVisible();
  462. }
  463. bool NativeWindowViews::IsEnabled() {
  464. #if BUILDFLAG(IS_WIN)
  465. return ::IsWindowEnabled(GetAcceleratedWidget());
  466. #elif BUILDFLAG(IS_LINUX)
  467. #if defined(USE_OZONE_PLATFORM_X11)
  468. if (IsX11())
  469. return !event_disabler_.get();
  470. #endif
  471. NOTIMPLEMENTED();
  472. return true;
  473. #endif
  474. }
  475. void NativeWindowViews::IncrementChildModals() {
  476. num_modal_children_++;
  477. SetEnabledInternal(ShouldBeEnabled());
  478. }
  479. void NativeWindowViews::DecrementChildModals() {
  480. if (num_modal_children_ > 0) {
  481. num_modal_children_--;
  482. }
  483. SetEnabledInternal(ShouldBeEnabled());
  484. }
  485. void NativeWindowViews::SetEnabled(bool enable) {
  486. if (enable != is_enabled_) {
  487. is_enabled_ = enable;
  488. SetEnabledInternal(ShouldBeEnabled());
  489. }
  490. }
  491. bool NativeWindowViews::ShouldBeEnabled() {
  492. return is_enabled_ && (num_modal_children_ == 0);
  493. }
  494. void NativeWindowViews::SetEnabledInternal(bool enable) {
  495. if (enable && IsEnabled()) {
  496. return;
  497. } else if (!enable && !IsEnabled()) {
  498. return;
  499. }
  500. #if BUILDFLAG(IS_WIN)
  501. ::EnableWindow(GetAcceleratedWidget(), enable);
  502. #elif defined(USE_OZONE_PLATFORM_X11)
  503. if (IsX11()) {
  504. views::DesktopWindowTreeHostPlatform* tree_host =
  505. views::DesktopWindowTreeHostLinux::GetHostForWidget(
  506. GetAcceleratedWidget());
  507. if (enable) {
  508. tree_host->RemoveEventRewriter(event_disabler_.get());
  509. event_disabler_.reset();
  510. } else {
  511. event_disabler_ = std::make_unique<EventDisabler>();
  512. tree_host->AddEventRewriter(event_disabler_.get());
  513. }
  514. }
  515. #endif
  516. }
  517. #if BUILDFLAG(IS_LINUX)
  518. void NativeWindowViews::Maximize() {
  519. if (IsVisible()) {
  520. widget()->Maximize();
  521. } else {
  522. widget()->native_widget_private()->Show(ui::SHOW_STATE_MAXIMIZED,
  523. gfx::Rect());
  524. NotifyWindowShow();
  525. }
  526. }
  527. #endif
  528. void NativeWindowViews::Unmaximize() {
  529. if (IsMaximized()) {
  530. #if BUILDFLAG(IS_WIN)
  531. if (transparent()) {
  532. SetBounds(restore_bounds_, false);
  533. NotifyWindowUnmaximize();
  534. return;
  535. }
  536. #endif
  537. widget()->Restore();
  538. }
  539. }
  540. bool NativeWindowViews::IsMaximized() {
  541. if (widget()->IsMaximized()) {
  542. return true;
  543. } else {
  544. #if BUILDFLAG(IS_WIN)
  545. if (transparent()) {
  546. // Compare the size of the window with the size of the display
  547. auto display = display::Screen::GetScreen()->GetDisplayNearestWindow(
  548. GetNativeWindow());
  549. // Maximized if the window is the same dimensions and placement as the
  550. // display
  551. return GetBounds() == display.work_area();
  552. }
  553. #endif
  554. return false;
  555. }
  556. }
  557. void NativeWindowViews::Minimize() {
  558. if (IsVisible())
  559. widget()->Minimize();
  560. else
  561. widget()->native_widget_private()->Show(ui::SHOW_STATE_MINIMIZED,
  562. gfx::Rect());
  563. }
  564. void NativeWindowViews::Restore() {
  565. widget()->Restore();
  566. }
  567. bool NativeWindowViews::IsMinimized() {
  568. return widget()->IsMinimized();
  569. }
  570. void NativeWindowViews::SetFullScreen(bool fullscreen) {
  571. if (!IsFullScreenable())
  572. return;
  573. #if BUILDFLAG(IS_WIN)
  574. // There is no native fullscreen state on Windows.
  575. bool leaving_fullscreen = IsFullscreen() && !fullscreen;
  576. if (fullscreen) {
  577. last_window_state_ = ui::SHOW_STATE_FULLSCREEN;
  578. NotifyWindowEnterFullScreen();
  579. } else {
  580. last_window_state_ = ui::SHOW_STATE_NORMAL;
  581. NotifyWindowLeaveFullScreen();
  582. }
  583. // For window without WS_THICKFRAME style, we can not call SetFullscreen().
  584. // This path will be used for transparent windows as well.
  585. if (!thick_frame_) {
  586. if (fullscreen) {
  587. restore_bounds_ = GetBounds();
  588. auto display =
  589. display::Screen::GetScreen()->GetDisplayNearestPoint(GetPosition());
  590. SetBounds(display.bounds(), false);
  591. } else {
  592. SetBounds(restore_bounds_, false);
  593. }
  594. return;
  595. }
  596. // We set the new value after notifying, so we can handle the size event
  597. // correctly.
  598. widget()->SetFullscreen(fullscreen);
  599. // If restoring from fullscreen and the window isn't visible, force visible,
  600. // else a non-responsive window shell could be rendered.
  601. // (this situation may arise when app starts with fullscreen: true)
  602. // Note: the following must be after "widget()->SetFullscreen(fullscreen);"
  603. if (leaving_fullscreen && !IsVisible())
  604. FlipWindowStyle(GetAcceleratedWidget(), true, WS_VISIBLE);
  605. #else
  606. if (IsVisible())
  607. widget()->SetFullscreen(fullscreen);
  608. else if (fullscreen)
  609. widget()->native_widget_private()->Show(ui::SHOW_STATE_FULLSCREEN,
  610. gfx::Rect());
  611. // Auto-hide menubar when in fullscreen.
  612. if (fullscreen)
  613. SetMenuBarVisibility(false);
  614. else
  615. SetMenuBarVisibility(!IsMenuBarAutoHide());
  616. #endif
  617. }
  618. bool NativeWindowViews::IsFullscreen() const {
  619. return widget()->IsFullscreen();
  620. }
  621. void NativeWindowViews::SetBounds(const gfx::Rect& bounds, bool animate) {
  622. #if BUILDFLAG(IS_WIN)
  623. if (is_moving_ || is_resizing_) {
  624. pending_bounds_change_ = bounds;
  625. }
  626. #endif
  627. #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX)
  628. // On Linux and Windows the minimum and maximum size should be updated with
  629. // window size when window is not resizable.
  630. if (!resizable_) {
  631. SetMaximumSize(bounds.size());
  632. SetMinimumSize(bounds.size());
  633. }
  634. #endif
  635. widget()->SetBounds(bounds);
  636. }
  637. gfx::Rect NativeWindowViews::GetBounds() {
  638. #if BUILDFLAG(IS_WIN)
  639. if (IsMinimized())
  640. return widget()->GetRestoredBounds();
  641. #endif
  642. return widget()->GetWindowBoundsInScreen();
  643. }
  644. gfx::Rect NativeWindowViews::GetContentBounds() {
  645. return content_view() ? content_view()->GetBoundsInScreen() : gfx::Rect();
  646. }
  647. gfx::Size NativeWindowViews::GetContentSize() {
  648. #if BUILDFLAG(IS_WIN)
  649. if (IsMinimized())
  650. return NativeWindow::GetContentSize();
  651. #endif
  652. return content_view() ? content_view()->size() : gfx::Size();
  653. }
  654. gfx::Rect NativeWindowViews::GetNormalBounds() {
  655. return widget()->GetRestoredBounds();
  656. }
  657. void NativeWindowViews::SetContentSizeConstraints(
  658. const extensions::SizeConstraints& size_constraints) {
  659. NativeWindow::SetContentSizeConstraints(size_constraints);
  660. #if BUILDFLAG(IS_WIN)
  661. // Changing size constraints would force adding the WS_THICKFRAME style, so
  662. // do nothing if thickFrame is false.
  663. if (!thick_frame_)
  664. return;
  665. #endif
  666. // widget_delegate() is only available after Init() is called, we make use of
  667. // this to determine whether native widget has initialized.
  668. if (widget() && widget()->widget_delegate())
  669. widget()->OnSizeConstraintsChanged();
  670. if (resizable_)
  671. old_size_constraints_ = size_constraints;
  672. }
  673. void NativeWindowViews::SetResizable(bool resizable) {
  674. if (resizable != resizable_) {
  675. // On Linux there is no "resizable" property of a window, we have to set
  676. // both the minimum and maximum size to the window size to achieve it.
  677. if (resizable) {
  678. SetContentSizeConstraints(old_size_constraints_);
  679. SetMaximizable(maximizable_);
  680. } else {
  681. old_size_constraints_ = GetContentSizeConstraints();
  682. resizable_ = false;
  683. gfx::Size content_size = GetContentSize();
  684. SetContentSizeConstraints(
  685. extensions::SizeConstraints(content_size, content_size));
  686. }
  687. }
  688. #if BUILDFLAG(IS_WIN)
  689. if (has_frame() && thick_frame_)
  690. FlipWindowStyle(GetAcceleratedWidget(), resizable, WS_THICKFRAME);
  691. #endif
  692. resizable_ = resizable;
  693. SetCanResize(resizable_);
  694. }
  695. bool NativeWindowViews::MoveAbove(const std::string& sourceId) {
  696. const content::DesktopMediaID id = content::DesktopMediaID::Parse(sourceId);
  697. if (id.type != content::DesktopMediaID::TYPE_WINDOW)
  698. return false;
  699. #if BUILDFLAG(IS_WIN)
  700. const HWND otherWindow = reinterpret_cast<HWND>(id.id);
  701. if (!::IsWindow(otherWindow))
  702. return false;
  703. ::SetWindowPos(GetAcceleratedWidget(), GetWindow(otherWindow, GW_HWNDPREV), 0,
  704. 0, 0, 0,
  705. SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
  706. #elif defined(USE_OZONE_PLATFORM_X11)
  707. if (IsX11()) {
  708. if (!IsWindowValid(static_cast<x11::Window>(id.id)))
  709. return false;
  710. electron::MoveWindowAbove(static_cast<x11::Window>(GetAcceleratedWidget()),
  711. static_cast<x11::Window>(id.id));
  712. }
  713. #endif
  714. return true;
  715. }
  716. void NativeWindowViews::MoveTop() {
  717. // TODO(julien.isorce): fix chromium in order to use existing
  718. // widget()->StackAtTop().
  719. #if BUILDFLAG(IS_WIN)
  720. gfx::Point pos = GetPosition();
  721. gfx::Size size = GetSize();
  722. ::SetWindowPos(GetAcceleratedWidget(), HWND_TOP, pos.x(), pos.y(),
  723. size.width(), size.height(),
  724. SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
  725. #elif defined(USE_OZONE_PLATFORM_X11)
  726. if (IsX11())
  727. electron::MoveWindowToForeground(
  728. static_cast<x11::Window>(GetAcceleratedWidget()));
  729. #endif
  730. }
  731. bool NativeWindowViews::IsResizable() {
  732. #if BUILDFLAG(IS_WIN)
  733. if (has_frame())
  734. return ::GetWindowLong(GetAcceleratedWidget(), GWL_STYLE) & WS_THICKFRAME;
  735. #endif
  736. return resizable_;
  737. }
  738. void NativeWindowViews::SetAspectRatio(double aspect_ratio,
  739. const gfx::Size& extra_size) {
  740. NativeWindow::SetAspectRatio(aspect_ratio, extra_size);
  741. gfx::SizeF aspect(aspect_ratio, 1.0);
  742. // Scale up because SetAspectRatio() truncates aspect value to int
  743. aspect.Scale(100);
  744. widget()->SetAspectRatio(aspect);
  745. }
  746. void NativeWindowViews::SetMovable(bool movable) {
  747. movable_ = movable;
  748. }
  749. bool NativeWindowViews::IsMovable() {
  750. #if BUILDFLAG(IS_WIN)
  751. return movable_;
  752. #else
  753. return true; // Not implemented on Linux.
  754. #endif
  755. }
  756. void NativeWindowViews::SetMinimizable(bool minimizable) {
  757. #if BUILDFLAG(IS_WIN)
  758. FlipWindowStyle(GetAcceleratedWidget(), minimizable, WS_MINIMIZEBOX);
  759. if (IsWindowControlsOverlayEnabled()) {
  760. auto* frame_view =
  761. static_cast<WinFrameView*>(widget()->non_client_view()->frame_view());
  762. frame_view->caption_button_container()->UpdateButtons();
  763. }
  764. #endif
  765. minimizable_ = minimizable;
  766. }
  767. bool NativeWindowViews::IsMinimizable() {
  768. #if BUILDFLAG(IS_WIN)
  769. return ::GetWindowLong(GetAcceleratedWidget(), GWL_STYLE) & WS_MINIMIZEBOX;
  770. #else
  771. return true; // Not implemented on Linux.
  772. #endif
  773. }
  774. void NativeWindowViews::SetMaximizable(bool maximizable) {
  775. #if BUILDFLAG(IS_WIN)
  776. FlipWindowStyle(GetAcceleratedWidget(), maximizable, WS_MAXIMIZEBOX);
  777. if (IsWindowControlsOverlayEnabled()) {
  778. auto* frame_view =
  779. static_cast<WinFrameView*>(widget()->non_client_view()->frame_view());
  780. frame_view->caption_button_container()->UpdateButtons();
  781. }
  782. #endif
  783. maximizable_ = maximizable;
  784. }
  785. bool NativeWindowViews::IsMaximizable() {
  786. #if BUILDFLAG(IS_WIN)
  787. return ::GetWindowLong(GetAcceleratedWidget(), GWL_STYLE) & WS_MAXIMIZEBOX;
  788. #else
  789. return true; // Not implemented on Linux.
  790. #endif
  791. }
  792. void NativeWindowViews::SetExcludedFromShownWindowsMenu(bool excluded) {}
  793. bool NativeWindowViews::IsExcludedFromShownWindowsMenu() {
  794. // return false on unsupported platforms
  795. return false;
  796. }
  797. void NativeWindowViews::SetFullScreenable(bool fullscreenable) {
  798. fullscreenable_ = fullscreenable;
  799. }
  800. bool NativeWindowViews::IsFullScreenable() {
  801. return fullscreenable_;
  802. }
  803. void NativeWindowViews::SetClosable(bool closable) {
  804. #if BUILDFLAG(IS_WIN)
  805. HMENU menu = GetSystemMenu(GetAcceleratedWidget(), false);
  806. if (closable) {
  807. EnableMenuItem(menu, SC_CLOSE, MF_BYCOMMAND | MF_ENABLED);
  808. } else {
  809. EnableMenuItem(menu, SC_CLOSE, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
  810. }
  811. if (IsWindowControlsOverlayEnabled()) {
  812. auto* frame_view =
  813. static_cast<WinFrameView*>(widget()->non_client_view()->frame_view());
  814. frame_view->caption_button_container()->UpdateButtons();
  815. }
  816. #endif
  817. }
  818. bool NativeWindowViews::IsClosable() {
  819. #if BUILDFLAG(IS_WIN)
  820. HMENU menu = GetSystemMenu(GetAcceleratedWidget(), false);
  821. MENUITEMINFO info;
  822. memset(&info, 0, sizeof(info));
  823. info.cbSize = sizeof(info);
  824. info.fMask = MIIM_STATE;
  825. if (!GetMenuItemInfo(menu, SC_CLOSE, false, &info)) {
  826. return false;
  827. }
  828. return !(info.fState & MFS_DISABLED);
  829. #elif BUILDFLAG(IS_LINUX)
  830. return true;
  831. #endif
  832. }
  833. void NativeWindowViews::SetAlwaysOnTop(ui::ZOrderLevel z_order,
  834. const std::string& level,
  835. int relativeLevel) {
  836. bool level_changed = z_order != widget()->GetZOrderLevel();
  837. widget()->SetZOrderLevel(z_order);
  838. #if BUILDFLAG(IS_WIN)
  839. // Reset the placement flag.
  840. behind_task_bar_ = false;
  841. if (z_order != ui::ZOrderLevel::kNormal) {
  842. // On macOS the window is placed behind the Dock for the following levels.
  843. // Re-use the same names on Windows to make it easier for the user.
  844. static const std::vector<std::string> levels = {
  845. "floating", "torn-off-menu", "modal-panel", "main-menu", "status"};
  846. behind_task_bar_ = base::Contains(levels, level);
  847. }
  848. #endif
  849. MoveBehindTaskBarIfNeeded();
  850. // This must be notified at the very end or IsAlwaysOnTop
  851. // will not yet have been updated to reflect the new status
  852. if (level_changed)
  853. NativeWindow::NotifyWindowAlwaysOnTopChanged();
  854. }
  855. ui::ZOrderLevel NativeWindowViews::GetZOrderLevel() {
  856. return widget()->GetZOrderLevel();
  857. }
  858. void NativeWindowViews::Center() {
  859. widget()->CenterWindow(GetSize());
  860. }
  861. void NativeWindowViews::Invalidate() {
  862. widget()->SchedulePaintInRect(gfx::Rect(GetBounds().size()));
  863. }
  864. void NativeWindowViews::SetTitle(const std::string& title) {
  865. title_ = title;
  866. widget()->UpdateWindowTitle();
  867. }
  868. std::string NativeWindowViews::GetTitle() {
  869. return title_;
  870. }
  871. void NativeWindowViews::FlashFrame(bool flash) {
  872. #if BUILDFLAG(IS_WIN)
  873. // The Chromium's implementation has a bug stopping flash.
  874. if (!flash) {
  875. FLASHWINFO fwi;
  876. fwi.cbSize = sizeof(fwi);
  877. fwi.hwnd = GetAcceleratedWidget();
  878. fwi.dwFlags = FLASHW_STOP;
  879. fwi.uCount = 0;
  880. FlashWindowEx(&fwi);
  881. return;
  882. }
  883. #endif
  884. widget()->FlashFrame(flash);
  885. }
  886. void NativeWindowViews::SetSkipTaskbar(bool skip) {
  887. #if BUILDFLAG(IS_WIN)
  888. Microsoft::WRL::ComPtr<ITaskbarList> taskbar;
  889. if (FAILED(::CoCreateInstance(CLSID_TaskbarList, nullptr,
  890. CLSCTX_INPROC_SERVER,
  891. IID_PPV_ARGS(&taskbar))) ||
  892. FAILED(taskbar->HrInit()))
  893. return;
  894. if (skip) {
  895. taskbar->DeleteTab(GetAcceleratedWidget());
  896. } else {
  897. taskbar->AddTab(GetAcceleratedWidget());
  898. taskbar_host_.RestoreThumbarButtons(GetAcceleratedWidget());
  899. }
  900. #endif
  901. }
  902. void NativeWindowViews::SetSimpleFullScreen(bool simple_fullscreen) {
  903. SetFullScreen(simple_fullscreen);
  904. }
  905. bool NativeWindowViews::IsSimpleFullScreen() {
  906. return IsFullscreen();
  907. }
  908. void NativeWindowViews::SetKiosk(bool kiosk) {
  909. SetFullScreen(kiosk);
  910. }
  911. bool NativeWindowViews::IsKiosk() {
  912. return IsFullscreen();
  913. }
  914. bool NativeWindowViews::IsTabletMode() const {
  915. #if BUILDFLAG(IS_WIN)
  916. return base::win::IsWindows10OrGreaterTabletMode(GetAcceleratedWidget());
  917. #else
  918. return false;
  919. #endif
  920. }
  921. SkColor NativeWindowViews::GetBackgroundColor() {
  922. auto* background = root_view_->background();
  923. if (!background)
  924. return SK_ColorTRANSPARENT;
  925. return background->get_color();
  926. }
  927. void NativeWindowViews::SetBackgroundColor(SkColor background_color) {
  928. // web views' background color.
  929. root_view_->SetBackground(views::CreateSolidBackground(background_color));
  930. #if BUILDFLAG(IS_WIN)
  931. // Set the background color of native window.
  932. HBRUSH brush = CreateSolidBrush(skia::SkColorToCOLORREF(background_color));
  933. ULONG_PTR previous_brush =
  934. SetClassLongPtr(GetAcceleratedWidget(), GCLP_HBRBACKGROUND,
  935. reinterpret_cast<LONG_PTR>(brush));
  936. if (previous_brush)
  937. DeleteObject((HBRUSH)previous_brush);
  938. InvalidateRect(GetAcceleratedWidget(), NULL, 1);
  939. #endif
  940. }
  941. void NativeWindowViews::SetHasShadow(bool has_shadow) {
  942. wm::SetShadowElevation(GetNativeWindow(),
  943. has_shadow ? wm::kShadowElevationInactiveWindow
  944. : wm::kShadowElevationNone);
  945. }
  946. bool NativeWindowViews::HasShadow() {
  947. return GetNativeWindow()->GetProperty(wm::kShadowElevationKey) !=
  948. wm::kShadowElevationNone;
  949. }
  950. void NativeWindowViews::SetOpacity(const double opacity) {
  951. #if BUILDFLAG(IS_WIN)
  952. const double boundedOpacity = base::clamp(opacity, 0.0, 1.0);
  953. HWND hwnd = GetAcceleratedWidget();
  954. if (!layered_) {
  955. LONG ex_style = ::GetWindowLong(hwnd, GWL_EXSTYLE);
  956. ex_style |= WS_EX_LAYERED;
  957. ::SetWindowLong(hwnd, GWL_EXSTYLE, ex_style);
  958. layered_ = true;
  959. }
  960. ::SetLayeredWindowAttributes(hwnd, 0, boundedOpacity * 255, LWA_ALPHA);
  961. opacity_ = boundedOpacity;
  962. #else
  963. opacity_ = 1.0; // setOpacity unsupported on Linux
  964. #endif
  965. }
  966. double NativeWindowViews::GetOpacity() {
  967. return opacity_;
  968. }
  969. void NativeWindowViews::SetIgnoreMouseEvents(bool ignore, bool forward) {
  970. #if BUILDFLAG(IS_WIN)
  971. LONG ex_style = ::GetWindowLong(GetAcceleratedWidget(), GWL_EXSTYLE);
  972. if (ignore)
  973. ex_style |= (WS_EX_TRANSPARENT | WS_EX_LAYERED);
  974. else
  975. ex_style &= ~(WS_EX_TRANSPARENT | WS_EX_LAYERED);
  976. if (layered_)
  977. ex_style |= WS_EX_LAYERED;
  978. ::SetWindowLong(GetAcceleratedWidget(), GWL_EXSTYLE, ex_style);
  979. // Forwarding is always disabled when not ignoring mouse messages.
  980. if (!ignore) {
  981. SetForwardMouseMessages(false);
  982. } else {
  983. SetForwardMouseMessages(forward);
  984. }
  985. #elif defined(USE_OZONE_PLATFORM_X11)
  986. if (IsX11()) {
  987. auto* connection = x11::Connection::Get();
  988. if (ignore) {
  989. x11::Rectangle r{0, 0, 1, 1};
  990. connection->shape().Rectangles({
  991. .operation = x11::Shape::So::Set,
  992. .destination_kind = x11::Shape::Sk::Input,
  993. .ordering = x11::ClipOrdering::YXBanded,
  994. .destination_window =
  995. static_cast<x11::Window>(GetAcceleratedWidget()),
  996. .rectangles = {r},
  997. });
  998. } else {
  999. connection->shape().Mask({
  1000. .operation = x11::Shape::So::Set,
  1001. .destination_kind = x11::Shape::Sk::Input,
  1002. .destination_window =
  1003. static_cast<x11::Window>(GetAcceleratedWidget()),
  1004. .source_bitmap = x11::Pixmap::None,
  1005. });
  1006. }
  1007. }
  1008. #endif
  1009. }
  1010. void NativeWindowViews::SetContentProtection(bool enable) {
  1011. #if BUILDFLAG(IS_WIN)
  1012. HWND hwnd = GetAcceleratedWidget();
  1013. DWORD affinity = enable ? WDA_EXCLUDEFROMCAPTURE : WDA_NONE;
  1014. ::SetWindowDisplayAffinity(hwnd, affinity);
  1015. if (!layered_) {
  1016. // Workaround to prevent black window on screen capture after hiding and
  1017. // showing the BrowserWindow.
  1018. LONG ex_style = ::GetWindowLong(hwnd, GWL_EXSTYLE);
  1019. ex_style |= WS_EX_LAYERED;
  1020. ::SetWindowLong(hwnd, GWL_EXSTYLE, ex_style);
  1021. layered_ = true;
  1022. }
  1023. #endif
  1024. }
  1025. void NativeWindowViews::SetFocusable(bool focusable) {
  1026. widget()->widget_delegate()->SetCanActivate(focusable);
  1027. #if BUILDFLAG(IS_WIN)
  1028. LONG ex_style = ::GetWindowLong(GetAcceleratedWidget(), GWL_EXSTYLE);
  1029. if (focusable)
  1030. ex_style &= ~WS_EX_NOACTIVATE;
  1031. else
  1032. ex_style |= WS_EX_NOACTIVATE;
  1033. ::SetWindowLong(GetAcceleratedWidget(), GWL_EXSTYLE, ex_style);
  1034. SetSkipTaskbar(!focusable);
  1035. Focus(false);
  1036. #endif
  1037. }
  1038. bool NativeWindowViews::IsFocusable() {
  1039. bool can_activate = widget()->widget_delegate()->CanActivate();
  1040. #if BUILDFLAG(IS_WIN)
  1041. LONG ex_style = ::GetWindowLong(GetAcceleratedWidget(), GWL_EXSTYLE);
  1042. bool no_activate = ex_style & WS_EX_NOACTIVATE;
  1043. return !no_activate && can_activate;
  1044. #else
  1045. return can_activate;
  1046. #endif
  1047. }
  1048. void NativeWindowViews::SetMenu(ElectronMenuModel* menu_model) {
  1049. #if defined(USE_OZONE)
  1050. // Remove global menu bar.
  1051. if (global_menu_bar_ && menu_model == nullptr) {
  1052. global_menu_bar_.reset();
  1053. root_view_->UnregisterAcceleratorsWithFocusManager();
  1054. return;
  1055. }
  1056. // Use global application menu bar when possible.
  1057. if (CreateGlobalMenuBar() && ShouldUseGlobalMenuBar()) {
  1058. if (!global_menu_bar_)
  1059. global_menu_bar_ = std::make_unique<GlobalMenuBarX11>(this);
  1060. if (global_menu_bar_->IsServerStarted()) {
  1061. root_view_->RegisterAcceleratorsWithFocusManager(menu_model);
  1062. global_menu_bar_->SetMenu(menu_model);
  1063. return;
  1064. }
  1065. }
  1066. #endif
  1067. // Should reset content size when setting menu.
  1068. gfx::Size content_size = GetContentSize();
  1069. bool should_reset_size = use_content_size_ && has_frame() &&
  1070. !IsMenuBarAutoHide() &&
  1071. ((!!menu_model) != root_view_->HasMenu());
  1072. root_view_->SetMenu(menu_model);
  1073. if (should_reset_size) {
  1074. // Enlarge the size constraints for the menu.
  1075. int menu_bar_height = root_view_->GetMenuBarHeight();
  1076. extensions::SizeConstraints constraints = GetContentSizeConstraints();
  1077. if (constraints.HasMinimumSize()) {
  1078. gfx::Size min_size = constraints.GetMinimumSize();
  1079. min_size.set_height(min_size.height() + menu_bar_height);
  1080. constraints.set_minimum_size(min_size);
  1081. }
  1082. if (constraints.HasMaximumSize()) {
  1083. gfx::Size max_size = constraints.GetMaximumSize();
  1084. max_size.set_height(max_size.height() + menu_bar_height);
  1085. constraints.set_maximum_size(max_size);
  1086. }
  1087. SetContentSizeConstraints(constraints);
  1088. // Resize the window to make sure content size is not changed.
  1089. SetContentSize(content_size);
  1090. }
  1091. }
  1092. void NativeWindowViews::AddBrowserView(NativeBrowserView* view) {
  1093. if (!content_view())
  1094. return;
  1095. if (!view) {
  1096. return;
  1097. }
  1098. add_browser_view(view);
  1099. if (view->GetInspectableWebContentsView())
  1100. content_view()->AddChildView(
  1101. view->GetInspectableWebContentsView()->GetView());
  1102. }
  1103. void NativeWindowViews::RemoveBrowserView(NativeBrowserView* view) {
  1104. if (!content_view())
  1105. return;
  1106. if (!view) {
  1107. return;
  1108. }
  1109. if (view->GetInspectableWebContentsView())
  1110. content_view()->RemoveChildView(
  1111. view->GetInspectableWebContentsView()->GetView());
  1112. remove_browser_view(view);
  1113. }
  1114. void NativeWindowViews::SetTopBrowserView(NativeBrowserView* view) {
  1115. if (!content_view())
  1116. return;
  1117. if (!view) {
  1118. return;
  1119. }
  1120. remove_browser_view(view);
  1121. add_browser_view(view);
  1122. if (view->GetInspectableWebContentsView())
  1123. content_view()->ReorderChildView(
  1124. view->GetInspectableWebContentsView()->GetView(), -1);
  1125. }
  1126. void NativeWindowViews::SetParentWindow(NativeWindow* parent) {
  1127. NativeWindow::SetParentWindow(parent);
  1128. #if defined(USE_OZONE_PLATFORM_X11)
  1129. if (IsX11())
  1130. x11::SetProperty(
  1131. static_cast<x11::Window>(GetAcceleratedWidget()),
  1132. x11::Atom::WM_TRANSIENT_FOR, x11::Atom::WINDOW,
  1133. parent ? static_cast<x11::Window>(parent->GetAcceleratedWidget())
  1134. : ui::GetX11RootWindow());
  1135. #elif BUILDFLAG(IS_WIN)
  1136. // To set parentship between windows into Windows is better to play with the
  1137. // owner instead of the parent, as Windows natively seems to do if a parent
  1138. // is specified at window creation time.
  1139. // For do this we must NOT use the ::SetParent function, instead we must use
  1140. // the ::GetWindowLongPtr or ::SetWindowLongPtr functions with "nIndex" set
  1141. // to "GWLP_HWNDPARENT" which actually means the window owner.
  1142. HWND hwndParent = parent ? parent->GetAcceleratedWidget() : NULL;
  1143. if (hwndParent ==
  1144. (HWND)::GetWindowLongPtr(GetAcceleratedWidget(), GWLP_HWNDPARENT))
  1145. return;
  1146. ::SetWindowLongPtr(GetAcceleratedWidget(), GWLP_HWNDPARENT,
  1147. (LONG_PTR)hwndParent);
  1148. // Ensures the visibility
  1149. if (IsVisible()) {
  1150. WINDOWPLACEMENT wp;
  1151. wp.length = sizeof(WINDOWPLACEMENT);
  1152. ::GetWindowPlacement(GetAcceleratedWidget(), &wp);
  1153. ::ShowWindow(GetAcceleratedWidget(), SW_HIDE);
  1154. ::ShowWindow(GetAcceleratedWidget(), wp.showCmd);
  1155. ::BringWindowToTop(GetAcceleratedWidget());
  1156. }
  1157. #endif
  1158. }
  1159. gfx::NativeView NativeWindowViews::GetNativeView() const {
  1160. return widget()->GetNativeView();
  1161. }
  1162. gfx::NativeWindow NativeWindowViews::GetNativeWindow() const {
  1163. return widget()->GetNativeWindow();
  1164. }
  1165. void NativeWindowViews::SetProgressBar(double progress,
  1166. NativeWindow::ProgressState state) {
  1167. #if BUILDFLAG(IS_WIN)
  1168. taskbar_host_.SetProgressBar(GetAcceleratedWidget(), progress, state);
  1169. #elif BUILDFLAG(IS_LINUX)
  1170. if (unity::IsRunning()) {
  1171. unity::SetProgressFraction(progress);
  1172. }
  1173. #endif
  1174. }
  1175. void NativeWindowViews::SetOverlayIcon(const gfx::Image& overlay,
  1176. const std::string& description) {
  1177. #if BUILDFLAG(IS_WIN)
  1178. SkBitmap overlay_bitmap = overlay.AsBitmap();
  1179. taskbar_host_.SetOverlayIcon(GetAcceleratedWidget(), overlay_bitmap,
  1180. description);
  1181. #endif
  1182. }
  1183. void NativeWindowViews::SetAutoHideMenuBar(bool auto_hide) {
  1184. root_view_->SetAutoHideMenuBar(auto_hide);
  1185. }
  1186. bool NativeWindowViews::IsMenuBarAutoHide() {
  1187. return root_view_->IsMenuBarAutoHide();
  1188. }
  1189. void NativeWindowViews::SetMenuBarVisibility(bool visible) {
  1190. root_view_->SetMenuBarVisibility(visible);
  1191. }
  1192. bool NativeWindowViews::IsMenuBarVisible() {
  1193. return root_view_->IsMenuBarVisible();
  1194. }
  1195. void NativeWindowViews::SetVisibleOnAllWorkspaces(
  1196. bool visible,
  1197. bool visibleOnFullScreen,
  1198. bool skipTransformProcessType) {
  1199. widget()->SetVisibleOnAllWorkspaces(visible);
  1200. }
  1201. bool NativeWindowViews::IsVisibleOnAllWorkspaces() {
  1202. #if defined(USE_OZONE_PLATFORM_X11)
  1203. if (IsX11()) {
  1204. // Use the presence/absence of _NET_WM_STATE_STICKY in _NET_WM_STATE to
  1205. // determine whether the current window is visible on all workspaces.
  1206. x11::Atom sticky_atom = x11::GetAtom("_NET_WM_STATE_STICKY");
  1207. std::vector<x11::Atom> wm_states;
  1208. GetArrayProperty(static_cast<x11::Window>(GetAcceleratedWidget()),
  1209. x11::GetAtom("_NET_WM_STATE"), &wm_states);
  1210. return std::find(wm_states.begin(), wm_states.end(), sticky_atom) !=
  1211. wm_states.end();
  1212. }
  1213. #endif
  1214. return false;
  1215. }
  1216. content::DesktopMediaID NativeWindowViews::GetDesktopMediaID() const {
  1217. const gfx::AcceleratedWidget accelerated_widget = GetAcceleratedWidget();
  1218. content::DesktopMediaID::Id window_handle = content::DesktopMediaID::kNullId;
  1219. content::DesktopMediaID::Id aura_id = content::DesktopMediaID::kNullId;
  1220. #if BUILDFLAG(IS_WIN)
  1221. window_handle =
  1222. reinterpret_cast<content::DesktopMediaID::Id>(accelerated_widget);
  1223. #elif BUILDFLAG(IS_LINUX)
  1224. window_handle = static_cast<uint32_t>(accelerated_widget);
  1225. #endif
  1226. aura::WindowTreeHost* const host =
  1227. aura::WindowTreeHost::GetForAcceleratedWidget(accelerated_widget);
  1228. aura::Window* const aura_window = host ? host->window() : nullptr;
  1229. if (aura_window) {
  1230. aura_id = content::DesktopMediaID::RegisterNativeWindow(
  1231. content::DesktopMediaID::TYPE_WINDOW, aura_window)
  1232. .window_id;
  1233. }
  1234. // No constructor to pass the aura_id. Make sure to not use the other
  1235. // constructor that has a third parameter, it is for yet another purpose.
  1236. content::DesktopMediaID result = content::DesktopMediaID(
  1237. content::DesktopMediaID::TYPE_WINDOW, window_handle);
  1238. // Confusing but this is how content::DesktopMediaID is designed. The id
  1239. // property is the window handle whereas the window_id property is an id
  1240. // given by a map containing all aura instances.
  1241. result.window_id = aura_id;
  1242. return result;
  1243. }
  1244. gfx::AcceleratedWidget NativeWindowViews::GetAcceleratedWidget() const {
  1245. if (GetNativeWindow() && GetNativeWindow()->GetHost())
  1246. return GetNativeWindow()->GetHost()->GetAcceleratedWidget();
  1247. else
  1248. return gfx::kNullAcceleratedWidget;
  1249. }
  1250. NativeWindowHandle NativeWindowViews::GetNativeWindowHandle() const {
  1251. return GetAcceleratedWidget();
  1252. }
  1253. gfx::Rect NativeWindowViews::ContentBoundsToWindowBounds(
  1254. const gfx::Rect& bounds) const {
  1255. if (!has_frame())
  1256. return bounds;
  1257. gfx::Rect window_bounds(bounds);
  1258. #if BUILDFLAG(IS_WIN)
  1259. if (widget()->non_client_view()) {
  1260. HWND hwnd = GetAcceleratedWidget();
  1261. gfx::Rect dpi_bounds = DIPToScreenRect(hwnd, bounds);
  1262. window_bounds = ScreenToDIPRect(
  1263. hwnd, widget()->non_client_view()->GetWindowBoundsForClientBounds(
  1264. dpi_bounds));
  1265. }
  1266. #endif
  1267. if (root_view_->HasMenu() && root_view_->IsMenuBarVisible()) {
  1268. int menu_bar_height = root_view_->GetMenuBarHeight();
  1269. window_bounds.set_y(window_bounds.y() - menu_bar_height);
  1270. window_bounds.set_height(window_bounds.height() + menu_bar_height);
  1271. }
  1272. return window_bounds;
  1273. }
  1274. gfx::Rect NativeWindowViews::WindowBoundsToContentBounds(
  1275. const gfx::Rect& bounds) const {
  1276. if (!has_frame())
  1277. return bounds;
  1278. gfx::Rect content_bounds(bounds);
  1279. #if BUILDFLAG(IS_WIN)
  1280. HWND hwnd = GetAcceleratedWidget();
  1281. content_bounds.set_size(DIPToScreenRect(hwnd, content_bounds).size());
  1282. RECT rect;
  1283. SetRectEmpty(&rect);
  1284. DWORD style = ::GetWindowLong(hwnd, GWL_STYLE);
  1285. DWORD ex_style = ::GetWindowLong(hwnd, GWL_EXSTYLE);
  1286. AdjustWindowRectEx(&rect, style, FALSE, ex_style);
  1287. content_bounds.set_width(content_bounds.width() - (rect.right - rect.left));
  1288. content_bounds.set_height(content_bounds.height() - (rect.bottom - rect.top));
  1289. content_bounds.set_size(ScreenToDIPRect(hwnd, content_bounds).size());
  1290. #endif
  1291. if (root_view_->HasMenu() && root_view_->IsMenuBarVisible()) {
  1292. int menu_bar_height = root_view_->GetMenuBarHeight();
  1293. content_bounds.set_y(content_bounds.y() + menu_bar_height);
  1294. content_bounds.set_height(content_bounds.height() - menu_bar_height);
  1295. }
  1296. return content_bounds;
  1297. }
  1298. void NativeWindowViews::UpdateDraggableRegions(
  1299. const std::vector<mojom::DraggableRegionPtr>& regions) {
  1300. draggable_region_ = DraggableRegionsToSkRegion(regions);
  1301. }
  1302. #if BUILDFLAG(IS_WIN)
  1303. void NativeWindowViews::SetIcon(HICON window_icon, HICON app_icon) {
  1304. // We are responsible for storing the images.
  1305. window_icon_ = base::win::ScopedHICON(CopyIcon(window_icon));
  1306. app_icon_ = base::win::ScopedHICON(CopyIcon(app_icon));
  1307. HWND hwnd = GetAcceleratedWidget();
  1308. SendMessage(hwnd, WM_SETICON, ICON_SMALL,
  1309. reinterpret_cast<LPARAM>(window_icon_.get()));
  1310. SendMessage(hwnd, WM_SETICON, ICON_BIG,
  1311. reinterpret_cast<LPARAM>(app_icon_.get()));
  1312. }
  1313. #elif BUILDFLAG(IS_LINUX)
  1314. void NativeWindowViews::SetIcon(const gfx::ImageSkia& icon) {
  1315. auto* tree_host = views::DesktopWindowTreeHostLinux::GetHostForWidget(
  1316. GetAcceleratedWidget());
  1317. tree_host->SetWindowIcons(icon, {});
  1318. }
  1319. #endif
  1320. void NativeWindowViews::OnWidgetActivationChanged(views::Widget* changed_widget,
  1321. bool active) {
  1322. if (changed_widget != widget())
  1323. return;
  1324. if (active) {
  1325. MoveBehindTaskBarIfNeeded();
  1326. NativeWindow::NotifyWindowFocus();
  1327. } else {
  1328. NativeWindow::NotifyWindowBlur();
  1329. }
  1330. // Hide menu bar when window is blurred.
  1331. if (!active && IsMenuBarAutoHide() && IsMenuBarVisible())
  1332. SetMenuBarVisibility(false);
  1333. root_view_->ResetAltState();
  1334. }
  1335. void NativeWindowViews::OnWidgetBoundsChanged(views::Widget* changed_widget,
  1336. const gfx::Rect& bounds) {
  1337. if (changed_widget != widget())
  1338. return;
  1339. // Note: We intentionally use `GetBounds()` instead of `bounds` to properly
  1340. // handle minimized windows on Windows.
  1341. const auto new_bounds = GetBounds();
  1342. if (widget_size_ != new_bounds.size()) {
  1343. int width_delta = new_bounds.width() - widget_size_.width();
  1344. int height_delta = new_bounds.height() - widget_size_.height();
  1345. for (NativeBrowserView* item : browser_views()) {
  1346. auto* native_view = static_cast<NativeBrowserViewViews*>(item);
  1347. native_view->SetAutoResizeProportions(widget_size_);
  1348. native_view->AutoResize(new_bounds, width_delta, height_delta);
  1349. }
  1350. NotifyWindowResize();
  1351. widget_size_ = new_bounds.size();
  1352. }
  1353. }
  1354. void NativeWindowViews::OnWidgetDestroying(views::Widget* widget) {
  1355. aura::Window* window = GetNativeWindow();
  1356. if (window)
  1357. window->RemovePreTargetHandler(this);
  1358. }
  1359. void NativeWindowViews::OnWidgetDestroyed(views::Widget* changed_widget) {
  1360. widget_destroyed_ = true;
  1361. }
  1362. views::View* NativeWindowViews::GetInitiallyFocusedView() {
  1363. return focused_view_;
  1364. }
  1365. bool NativeWindowViews::CanMaximize() const {
  1366. return resizable_ && maximizable_;
  1367. }
  1368. bool NativeWindowViews::CanMinimize() const {
  1369. #if BUILDFLAG(IS_WIN)
  1370. return minimizable_;
  1371. #elif BUILDFLAG(IS_LINUX)
  1372. return true;
  1373. #endif
  1374. }
  1375. std::u16string NativeWindowViews::GetWindowTitle() const {
  1376. return base::UTF8ToUTF16(title_);
  1377. }
  1378. views::View* NativeWindowViews::GetContentsView() {
  1379. return root_view_.get();
  1380. }
  1381. bool NativeWindowViews::ShouldDescendIntoChildForEventHandling(
  1382. gfx::NativeView child,
  1383. const gfx::Point& location) {
  1384. // App window should claim mouse events that fall within any BrowserViews'
  1385. // draggable region.
  1386. for (auto* view : browser_views()) {
  1387. auto* native_view = static_cast<NativeBrowserViewViews*>(view);
  1388. auto* view_draggable_region = native_view->draggable_region();
  1389. if (view_draggable_region &&
  1390. view_draggable_region->contains(location.x(), location.y()))
  1391. return false;
  1392. }
  1393. // App window should claim mouse events that fall within the draggable region.
  1394. if (draggable_region() &&
  1395. draggable_region()->contains(location.x(), location.y()))
  1396. return false;
  1397. // And the events on border for dragging resizable frameless window.
  1398. if ((!has_frame() || has_client_frame()) && resizable_) {
  1399. auto* frame =
  1400. static_cast<FramelessView*>(widget()->non_client_view()->frame_view());
  1401. return frame->ResizingBorderHitTest(location) == HTNOWHERE;
  1402. }
  1403. return true;
  1404. }
  1405. views::ClientView* NativeWindowViews::CreateClientView(views::Widget* widget) {
  1406. return new NativeWindowClientView(widget, root_view_.get(), this);
  1407. }
  1408. std::unique_ptr<views::NonClientFrameView>
  1409. NativeWindowViews::CreateNonClientFrameView(views::Widget* widget) {
  1410. #if BUILDFLAG(IS_WIN)
  1411. auto frame_view = std::make_unique<WinFrameView>();
  1412. frame_view->Init(this, widget);
  1413. return frame_view;
  1414. #else
  1415. if (has_frame() && !has_client_frame()) {
  1416. return std::make_unique<NativeFrameView>(this, widget);
  1417. } else {
  1418. auto frame_view = has_frame() && has_client_frame()
  1419. ? std::make_unique<ClientFrameViewLinux>()
  1420. : std::make_unique<FramelessView>();
  1421. frame_view->Init(this, widget);
  1422. return frame_view;
  1423. }
  1424. #endif
  1425. }
  1426. void NativeWindowViews::OnWidgetMove() {
  1427. NotifyWindowMove();
  1428. }
  1429. void NativeWindowViews::HandleKeyboardEvent(
  1430. content::WebContents*,
  1431. const content::NativeWebKeyboardEvent& event) {
  1432. if (widget_destroyed_)
  1433. return;
  1434. #if BUILDFLAG(IS_LINUX)
  1435. if (event.windows_key_code == ui::VKEY_BROWSER_BACK)
  1436. NotifyWindowExecuteAppCommand(kBrowserBackward);
  1437. else if (event.windows_key_code == ui::VKEY_BROWSER_FORWARD)
  1438. NotifyWindowExecuteAppCommand(kBrowserForward);
  1439. #endif
  1440. keyboard_event_handler_->HandleKeyboardEvent(event,
  1441. root_view_->GetFocusManager());
  1442. root_view_->HandleKeyEvent(event);
  1443. }
  1444. void NativeWindowViews::OnMouseEvent(ui::MouseEvent* event) {
  1445. if (event->type() != ui::ET_MOUSE_PRESSED)
  1446. return;
  1447. // Alt+Click should not toggle menu bar.
  1448. root_view_->ResetAltState();
  1449. #if BUILDFLAG(IS_LINUX)
  1450. if (event->changed_button_flags() == ui::EF_BACK_MOUSE_BUTTON)
  1451. NotifyWindowExecuteAppCommand(kBrowserBackward);
  1452. else if (event->changed_button_flags() == ui::EF_FORWARD_MOUSE_BUTTON)
  1453. NotifyWindowExecuteAppCommand(kBrowserForward);
  1454. #endif
  1455. }
  1456. ui::WindowShowState NativeWindowViews::GetRestoredState() {
  1457. if (IsMaximized()) {
  1458. #if BUILDFLAG(IS_WIN)
  1459. // Only restore Maximized state when window is NOT transparent style
  1460. if (!transparent()) {
  1461. return ui::SHOW_STATE_MAXIMIZED;
  1462. }
  1463. #else
  1464. return ui::SHOW_STATE_MAXIMIZED;
  1465. #endif
  1466. }
  1467. if (IsFullscreen())
  1468. return ui::SHOW_STATE_FULLSCREEN;
  1469. return ui::SHOW_STATE_NORMAL;
  1470. }
  1471. void NativeWindowViews::MoveBehindTaskBarIfNeeded() {
  1472. #if BUILDFLAG(IS_WIN)
  1473. if (behind_task_bar_) {
  1474. const HWND task_bar_hwnd = ::FindWindow(kUniqueTaskBarClassName, nullptr);
  1475. ::SetWindowPos(GetAcceleratedWidget(), task_bar_hwnd, 0, 0, 0, 0,
  1476. SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
  1477. }
  1478. #endif
  1479. // TODO(julien.isorce): Implement X11 case.
  1480. }
  1481. // static
  1482. NativeWindow* NativeWindow::Create(const gin_helper::Dictionary& options,
  1483. NativeWindow* parent) {
  1484. return new NativeWindowViews(options, parent);
  1485. }
  1486. } // namespace electron