native_window_views.cc 53 KB

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