electron_api_base_window.cc 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385
  1. // Copyright (c) 2018 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/api/electron_api_base_window.h"
  5. #include <algorithm>
  6. #include <string>
  7. #include <utility>
  8. #include <vector>
  9. #include "base/task/single_thread_task_runner.h"
  10. #include "content/public/common/color_parser.h"
  11. #include "electron/buildflags/buildflags.h"
  12. #include "gin/dictionary.h"
  13. #include "gin/handle.h"
  14. #include "shell/browser/api/electron_api_menu.h"
  15. #include "shell/browser/api/electron_api_view.h"
  16. #include "shell/browser/api/electron_api_web_contents.h"
  17. #include "shell/browser/javascript_environment.h"
  18. #include "shell/browser/native_window.h"
  19. #include "shell/common/color_util.h"
  20. #include "shell/common/gin_converters/callback_converter.h"
  21. #include "shell/common/gin_converters/file_path_converter.h"
  22. #include "shell/common/gin_converters/gfx_converter.h"
  23. #include "shell/common/gin_converters/image_converter.h"
  24. #include "shell/common/gin_converters/native_window_converter.h"
  25. #include "shell/common/gin_converters/optional_converter.h"
  26. #include "shell/common/gin_converters/value_converter.h"
  27. #include "shell/common/gin_helper/dictionary.h"
  28. #include "shell/common/gin_helper/object_template_builder.h"
  29. #include "shell/common/gin_helper/persistent_dictionary.h"
  30. #include "shell/common/node_includes.h"
  31. #include "shell/common/options_switches.h"
  32. #if defined(TOOLKIT_VIEWS)
  33. #include "shell/browser/native_window_views.h"
  34. #endif
  35. #if BUILDFLAG(IS_WIN)
  36. #include "shell/browser/ui/views/win_frame_view.h"
  37. #include "shell/browser/ui/win/taskbar_host.h"
  38. #include "ui/base/win/shell.h"
  39. #elif BUILDFLAG(IS_LINUX)
  40. #include "shell/browser/ui/views/opaque_frame_view.h"
  41. #endif
  42. #if BUILDFLAG(IS_WIN)
  43. namespace gin {
  44. template <>
  45. struct Converter<electron::TaskbarHost::ThumbarButton> {
  46. static bool FromV8(v8::Isolate* isolate,
  47. v8::Local<v8::Value> val,
  48. electron::TaskbarHost::ThumbarButton* out) {
  49. gin::Dictionary dict(isolate);
  50. if (!gin::ConvertFromV8(isolate, val, &dict))
  51. return false;
  52. dict.Get("click", &(out->clicked_callback));
  53. dict.Get("tooltip", &(out->tooltip));
  54. dict.Get("flags", &out->flags);
  55. return dict.Get("icon", &(out->icon));
  56. }
  57. };
  58. } // namespace gin
  59. #endif
  60. namespace electron::api {
  61. namespace {
  62. // Converts binary data to Buffer.
  63. v8::Local<v8::Value> ToBuffer(v8::Isolate* isolate, void* val, int size) {
  64. auto buffer = node::Buffer::Copy(isolate, static_cast<char*>(val), size);
  65. if (buffer.IsEmpty())
  66. return v8::Null(isolate);
  67. else
  68. return buffer.ToLocalChecked();
  69. }
  70. } // namespace
  71. BaseWindow::BaseWindow(v8::Isolate* isolate,
  72. const gin_helper::Dictionary& options) {
  73. // The parent window.
  74. gin::Handle<BaseWindow> parent;
  75. if (options.Get("parent", &parent) && !parent.IsEmpty())
  76. parent_window_.Reset(isolate, parent.ToV8());
  77. // Offscreen windows are always created frameless.
  78. gin_helper::Dictionary web_preferences;
  79. bool offscreen;
  80. if (options.Get(options::kWebPreferences, &web_preferences) &&
  81. web_preferences.Get(options::kOffscreen, &offscreen) && offscreen) {
  82. const_cast<gin_helper::Dictionary&>(options).Set(options::kFrame, false);
  83. }
  84. // Creates NativeWindow.
  85. window_ = NativeWindow::Create(
  86. options, parent.IsEmpty() ? nullptr : parent->window_.get());
  87. window_->AddObserver(this);
  88. SetContentView(View::Create(isolate));
  89. #if defined(TOOLKIT_VIEWS)
  90. v8::Local<v8::Value> icon;
  91. if (options.Get(options::kIcon, &icon)) {
  92. SetIconImpl(isolate, icon, NativeImage::OnConvertError::kWarn);
  93. }
  94. #endif
  95. }
  96. BaseWindow::BaseWindow(gin_helper::Arguments* args,
  97. const gin_helper::Dictionary& options)
  98. : BaseWindow(args->isolate(), options) {
  99. InitWithArgs(args);
  100. // Init window after everything has been setup.
  101. window()->InitFromOptions(options);
  102. }
  103. BaseWindow::~BaseWindow() {
  104. CloseImmediately();
  105. // Destroy the native window in next tick because the native code might be
  106. // iterating all windows.
  107. base::SingleThreadTaskRunner::GetCurrentDefault()->DeleteSoon(
  108. FROM_HERE, window_.release());
  109. // Remove global reference so the JS object can be garbage collected.
  110. self_ref_.Reset();
  111. }
  112. void BaseWindow::InitWith(v8::Isolate* isolate, v8::Local<v8::Object> wrapper) {
  113. AttachAsUserData(window_.get());
  114. gin_helper::TrackableObject<BaseWindow>::InitWith(isolate, wrapper);
  115. // We can only append this window to parent window's child windows after this
  116. // window's JS wrapper gets initialized.
  117. if (!parent_window_.IsEmpty()) {
  118. gin::Handle<BaseWindow> parent;
  119. gin::ConvertFromV8(isolate, GetParentWindow(), &parent);
  120. DCHECK(!parent.IsEmpty());
  121. parent->child_windows_.Set(isolate, weak_map_id(), wrapper);
  122. }
  123. // Reference this object in case it got garbage collected.
  124. self_ref_.Reset(isolate, wrapper);
  125. }
  126. void BaseWindow::WillCloseWindow(bool* prevent_default) {
  127. if (Emit("close")) {
  128. *prevent_default = true;
  129. }
  130. }
  131. void BaseWindow::OnWindowClosed() {
  132. // Invalidate weak ptrs before the Javascript object is destroyed,
  133. // there might be some delayed emit events which shouldn't be
  134. // triggered after this.
  135. weak_factory_.InvalidateWeakPtrs();
  136. RemoveFromWeakMap();
  137. window_->RemoveObserver(this);
  138. // We can not call Destroy here because we need to call Emit first, but we
  139. // also do not want any method to be used, so just mark as destroyed here.
  140. MarkDestroyed();
  141. Emit("closed");
  142. RemoveFromParentChildWindows();
  143. // Destroy the native class when window is closed.
  144. base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
  145. FROM_HERE, GetDestroyClosure());
  146. }
  147. void BaseWindow::OnWindowQueryEndSession(
  148. const std::vector<std::string>& reasons,
  149. bool* prevent_default) {
  150. v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
  151. v8::HandleScope handle_scope(isolate);
  152. gin::Handle<gin_helper::internal::Event> event =
  153. gin_helper::internal::Event::New(isolate);
  154. v8::Local<v8::Object> event_object = event.ToV8().As<v8::Object>();
  155. gin::Dictionary dict(isolate, event_object);
  156. dict.Set("reasons", reasons);
  157. EmitWithoutEvent("query-session-end", event);
  158. if (event->GetDefaultPrevented()) {
  159. *prevent_default = true;
  160. }
  161. }
  162. void BaseWindow::OnWindowEndSession(const std::vector<std::string>& reasons) {
  163. v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
  164. v8::HandleScope handle_scope(isolate);
  165. gin::Handle<gin_helper::internal::Event> event =
  166. gin_helper::internal::Event::New(isolate);
  167. v8::Local<v8::Object> event_object = event.ToV8().As<v8::Object>();
  168. gin::Dictionary dict(isolate, event_object);
  169. dict.Set("reasons", reasons);
  170. EmitWithoutEvent("session-end", event);
  171. }
  172. void BaseWindow::OnWindowBlur() {
  173. EmitEventSoon("blur");
  174. }
  175. void BaseWindow::OnWindowFocus() {
  176. EmitEventSoon("focus");
  177. }
  178. void BaseWindow::OnWindowShow() {
  179. Emit("show");
  180. }
  181. void BaseWindow::OnWindowHide() {
  182. Emit("hide");
  183. }
  184. void BaseWindow::OnWindowMaximize() {
  185. Emit("maximize");
  186. }
  187. void BaseWindow::OnWindowUnmaximize() {
  188. Emit("unmaximize");
  189. }
  190. void BaseWindow::OnWindowMinimize() {
  191. Emit("minimize");
  192. }
  193. void BaseWindow::OnWindowRestore() {
  194. Emit("restore");
  195. }
  196. void BaseWindow::OnWindowWillResize(const gfx::Rect& new_bounds,
  197. const gfx::ResizeEdge& edge,
  198. bool* prevent_default) {
  199. v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
  200. v8::HandleScope handle_scope(isolate);
  201. auto info = gin::Dictionary::CreateEmpty(isolate);
  202. info.Set("edge", edge);
  203. if (Emit("will-resize", new_bounds, info)) {
  204. *prevent_default = true;
  205. }
  206. }
  207. void BaseWindow::OnWindowResize() {
  208. Emit("resize");
  209. }
  210. void BaseWindow::OnWindowResized() {
  211. Emit("resized");
  212. }
  213. void BaseWindow::OnWindowWillMove(const gfx::Rect& new_bounds,
  214. bool* prevent_default) {
  215. if (Emit("will-move", new_bounds)) {
  216. *prevent_default = true;
  217. }
  218. }
  219. void BaseWindow::OnWindowMove() {
  220. Emit("move");
  221. }
  222. void BaseWindow::OnWindowMoved() {
  223. Emit("moved");
  224. }
  225. void BaseWindow::OnWindowEnterFullScreen() {
  226. Emit("enter-full-screen");
  227. }
  228. void BaseWindow::OnWindowLeaveFullScreen() {
  229. Emit("leave-full-screen");
  230. }
  231. void BaseWindow::OnWindowSwipe(const std::string& direction) {
  232. Emit("swipe", direction);
  233. }
  234. void BaseWindow::OnWindowRotateGesture(float rotation) {
  235. Emit("rotate-gesture", rotation);
  236. }
  237. void BaseWindow::OnWindowSheetBegin() {
  238. Emit("sheet-begin");
  239. }
  240. void BaseWindow::OnWindowSheetEnd() {
  241. Emit("sheet-end");
  242. }
  243. void BaseWindow::OnWindowEnterHtmlFullScreen() {
  244. Emit("enter-html-full-screen");
  245. }
  246. void BaseWindow::OnWindowLeaveHtmlFullScreen() {
  247. Emit("leave-html-full-screen");
  248. }
  249. void BaseWindow::OnWindowAlwaysOnTopChanged() {
  250. Emit("always-on-top-changed", IsAlwaysOnTop());
  251. }
  252. void BaseWindow::OnExecuteAppCommand(const std::string_view command_name) {
  253. Emit("app-command", command_name);
  254. }
  255. void BaseWindow::OnTouchBarItemResult(const std::string& item_id,
  256. const base::Value::Dict& details) {
  257. Emit("-touch-bar-interaction", item_id, details);
  258. }
  259. void BaseWindow::OnNewWindowForTab() {
  260. Emit("new-window-for-tab");
  261. }
  262. void BaseWindow::OnSystemContextMenu(int x, int y, bool* prevent_default) {
  263. if (Emit("system-context-menu", gfx::Point(x, y))) {
  264. *prevent_default = true;
  265. }
  266. }
  267. #if BUILDFLAG(IS_WIN)
  268. void BaseWindow::OnWindowMessage(UINT message, WPARAM w_param, LPARAM l_param) {
  269. if (IsWindowMessageHooked(message)) {
  270. v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
  271. v8::HandleScope scope(isolate);
  272. messages_callback_map_[message].Run(
  273. ToBuffer(isolate, static_cast<void*>(&w_param), sizeof(WPARAM)),
  274. ToBuffer(isolate, static_cast<void*>(&l_param), sizeof(LPARAM)));
  275. }
  276. }
  277. #endif
  278. void BaseWindow::SetContentView(gin::Handle<View> view) {
  279. content_view_.Reset(JavascriptEnvironment::GetIsolate(), view.ToV8());
  280. window_->SetContentView(view->view());
  281. }
  282. void BaseWindow::CloseImmediately() {
  283. if (!window_->IsClosed())
  284. window_->CloseImmediately();
  285. }
  286. void BaseWindow::Close() {
  287. window_->Close();
  288. }
  289. void BaseWindow::Focus() {
  290. window_->Focus(true);
  291. }
  292. void BaseWindow::Blur() {
  293. window_->Focus(false);
  294. }
  295. bool BaseWindow::IsFocused() const {
  296. return window_->IsFocused();
  297. }
  298. void BaseWindow::Show() {
  299. window_->Show();
  300. }
  301. void BaseWindow::ShowInactive() {
  302. // This method doesn't make sense for modal window.
  303. if (IsModal())
  304. return;
  305. window_->ShowInactive();
  306. }
  307. void BaseWindow::Hide() {
  308. window_->Hide();
  309. }
  310. bool BaseWindow::IsVisible() const {
  311. return window_->IsVisible();
  312. }
  313. bool BaseWindow::IsEnabled() const {
  314. return window_->IsEnabled();
  315. }
  316. void BaseWindow::SetEnabled(bool enable) {
  317. window_->SetEnabled(enable);
  318. }
  319. void BaseWindow::Maximize() {
  320. window_->Maximize();
  321. }
  322. void BaseWindow::Unmaximize() {
  323. window_->Unmaximize();
  324. }
  325. bool BaseWindow::IsMaximized() const {
  326. return window_->IsMaximized();
  327. }
  328. void BaseWindow::Minimize() {
  329. window_->Minimize();
  330. }
  331. void BaseWindow::Restore() {
  332. window_->Restore();
  333. }
  334. bool BaseWindow::IsMinimized() const {
  335. return window_->IsMinimized();
  336. }
  337. void BaseWindow::SetFullScreen(bool fullscreen) {
  338. window_->SetFullScreen(fullscreen);
  339. }
  340. bool BaseWindow::IsFullscreen() const {
  341. return window_->IsFullscreen();
  342. }
  343. void BaseWindow::SetBounds(const gfx::Rect& bounds,
  344. gin_helper::Arguments* args) {
  345. bool animate = false;
  346. args->GetNext(&animate);
  347. window_->SetBounds(bounds, animate);
  348. }
  349. gfx::Rect BaseWindow::GetBounds() const {
  350. return window_->GetBounds();
  351. }
  352. bool BaseWindow::IsNormal() const {
  353. return window_->IsNormal();
  354. }
  355. gfx::Rect BaseWindow::GetNormalBounds() const {
  356. return window_->GetNormalBounds();
  357. }
  358. void BaseWindow::SetContentBounds(const gfx::Rect& bounds,
  359. gin_helper::Arguments* args) {
  360. bool animate = false;
  361. args->GetNext(&animate);
  362. window_->SetContentBounds(bounds, animate);
  363. }
  364. gfx::Rect BaseWindow::GetContentBounds() const {
  365. return window_->GetContentBounds();
  366. }
  367. void BaseWindow::SetSize(int width, int height, gin_helper::Arguments* args) {
  368. bool animate = false;
  369. gfx::Size size = window_->GetMinimumSize();
  370. size.SetToMax(gfx::Size(width, height));
  371. args->GetNext(&animate);
  372. window_->SetSize(size, animate);
  373. }
  374. std::vector<int> BaseWindow::GetSize() const {
  375. std::vector<int> result(2);
  376. gfx::Size size = window_->GetSize();
  377. result[0] = size.width();
  378. result[1] = size.height();
  379. return result;
  380. }
  381. void BaseWindow::SetContentSize(int width,
  382. int height,
  383. gin_helper::Arguments* args) {
  384. bool animate = false;
  385. args->GetNext(&animate);
  386. window_->SetContentSize(gfx::Size(width, height), animate);
  387. }
  388. std::vector<int> BaseWindow::GetContentSize() const {
  389. std::vector<int> result(2);
  390. gfx::Size size = window_->GetContentSize();
  391. result[0] = size.width();
  392. result[1] = size.height();
  393. return result;
  394. }
  395. void BaseWindow::SetMinimumSize(int width, int height) {
  396. window_->SetMinimumSize(gfx::Size(width, height));
  397. }
  398. std::vector<int> BaseWindow::GetMinimumSize() const {
  399. std::vector<int> result(2);
  400. gfx::Size size = window_->GetMinimumSize();
  401. result[0] = size.width();
  402. result[1] = size.height();
  403. return result;
  404. }
  405. void BaseWindow::SetMaximumSize(int width, int height) {
  406. window_->SetMaximumSize(gfx::Size(width, height));
  407. }
  408. std::vector<int> BaseWindow::GetMaximumSize() const {
  409. std::vector<int> result(2);
  410. gfx::Size size = window_->GetMaximumSize();
  411. result[0] = size.width();
  412. result[1] = size.height();
  413. return result;
  414. }
  415. void BaseWindow::SetSheetOffset(double offsetY, gin_helper::Arguments* args) {
  416. double offsetX = 0.0;
  417. args->GetNext(&offsetX);
  418. window_->SetSheetOffset(offsetX, offsetY);
  419. }
  420. void BaseWindow::SetResizable(bool resizable) {
  421. window_->SetResizable(resizable);
  422. }
  423. bool BaseWindow::IsResizable() const {
  424. return window_->IsResizable();
  425. }
  426. void BaseWindow::SetMovable(bool movable) {
  427. window_->SetMovable(movable);
  428. }
  429. bool BaseWindow::IsMovable() const {
  430. return window_->IsMovable();
  431. }
  432. void BaseWindow::SetMinimizable(bool minimizable) {
  433. window_->SetMinimizable(minimizable);
  434. }
  435. bool BaseWindow::IsMinimizable() const {
  436. return window_->IsMinimizable();
  437. }
  438. void BaseWindow::SetMaximizable(bool maximizable) {
  439. window_->SetMaximizable(maximizable);
  440. }
  441. bool BaseWindow::IsMaximizable() const {
  442. return window_->IsMaximizable();
  443. }
  444. void BaseWindow::SetFullScreenable(bool fullscreenable) {
  445. window_->SetFullScreenable(fullscreenable);
  446. }
  447. bool BaseWindow::IsFullScreenable() const {
  448. return window_->IsFullScreenable();
  449. }
  450. void BaseWindow::SetClosable(bool closable) {
  451. window_->SetClosable(closable);
  452. }
  453. bool BaseWindow::IsClosable() const {
  454. return window_->IsClosable();
  455. }
  456. void BaseWindow::SetAlwaysOnTop(bool top, gin_helper::Arguments* args) {
  457. std::string level = "floating";
  458. int relative_level = 0;
  459. args->GetNext(&level);
  460. args->GetNext(&relative_level);
  461. ui::ZOrderLevel z_order =
  462. top ? ui::ZOrderLevel::kFloatingWindow : ui::ZOrderLevel::kNormal;
  463. window_->SetAlwaysOnTop(z_order, level, relative_level);
  464. }
  465. bool BaseWindow::IsAlwaysOnTop() const {
  466. return window_->GetZOrderLevel() != ui::ZOrderLevel::kNormal;
  467. }
  468. void BaseWindow::Center() {
  469. window_->Center();
  470. }
  471. void BaseWindow::SetPosition(int x, int y, gin_helper::Arguments* args) {
  472. bool animate = false;
  473. args->GetNext(&animate);
  474. window_->SetPosition(gfx::Point(x, y), animate);
  475. }
  476. std::vector<int> BaseWindow::GetPosition() const {
  477. std::vector<int> result(2);
  478. gfx::Point pos = window_->GetPosition();
  479. result[0] = pos.x();
  480. result[1] = pos.y();
  481. return result;
  482. }
  483. void BaseWindow::MoveAbove(const std::string& sourceId,
  484. gin_helper::Arguments* args) {
  485. if (!window_->MoveAbove(sourceId))
  486. args->ThrowError("Invalid media source id");
  487. }
  488. void BaseWindow::MoveTop() {
  489. window_->MoveTop();
  490. }
  491. void BaseWindow::SetTitle(const std::string& title) {
  492. window_->SetTitle(title);
  493. }
  494. std::string BaseWindow::GetTitle() const {
  495. return window_->GetTitle();
  496. }
  497. void BaseWindow::SetAccessibleTitle(const std::string& title) {
  498. window_->SetAccessibleTitle(title);
  499. }
  500. std::string BaseWindow::GetAccessibleTitle() const {
  501. return window_->GetAccessibleTitle();
  502. }
  503. void BaseWindow::FlashFrame(bool flash) {
  504. window_->FlashFrame(flash);
  505. }
  506. void BaseWindow::SetSkipTaskbar(bool skip) {
  507. window_->SetSkipTaskbar(skip);
  508. }
  509. void BaseWindow::SetExcludedFromShownWindowsMenu(bool excluded) {
  510. window_->SetExcludedFromShownWindowsMenu(excluded);
  511. }
  512. bool BaseWindow::IsExcludedFromShownWindowsMenu() const {
  513. return window_->IsExcludedFromShownWindowsMenu();
  514. }
  515. void BaseWindow::SetSimpleFullScreen(bool simple_fullscreen) {
  516. window_->SetSimpleFullScreen(simple_fullscreen);
  517. }
  518. bool BaseWindow::IsSimpleFullScreen() const {
  519. return window_->IsSimpleFullScreen();
  520. }
  521. void BaseWindow::SetKiosk(bool kiosk) {
  522. window_->SetKiosk(kiosk);
  523. }
  524. bool BaseWindow::IsKiosk() const {
  525. return window_->IsKiosk();
  526. }
  527. bool BaseWindow::IsTabletMode() const {
  528. return window_->IsTabletMode();
  529. }
  530. void BaseWindow::SetBackgroundColor(const std::string& color_name) {
  531. SkColor color = ParseCSSColor(color_name);
  532. window_->SetBackgroundColor(color);
  533. }
  534. std::string BaseWindow::GetBackgroundColor() const {
  535. return ToRGBHex(window_->GetBackgroundColor());
  536. }
  537. void BaseWindow::InvalidateShadow() {
  538. window_->InvalidateShadow();
  539. }
  540. void BaseWindow::SetHasShadow(bool has_shadow) {
  541. window_->SetHasShadow(has_shadow);
  542. }
  543. bool BaseWindow::HasShadow() const {
  544. return window_->HasShadow();
  545. }
  546. void BaseWindow::SetOpacity(const double opacity) {
  547. window_->SetOpacity(opacity);
  548. }
  549. double BaseWindow::GetOpacity() const {
  550. return window_->GetOpacity();
  551. }
  552. void BaseWindow::SetShape(const std::vector<gfx::Rect>& rects) {
  553. window_->widget()->SetShape(std::make_unique<std::vector<gfx::Rect>>(rects));
  554. }
  555. void BaseWindow::SetRepresentedFilename(const std::string& filename) {
  556. window_->SetRepresentedFilename(filename);
  557. }
  558. std::string BaseWindow::GetRepresentedFilename() const {
  559. return window_->GetRepresentedFilename();
  560. }
  561. void BaseWindow::SetDocumentEdited(bool edited) {
  562. window_->SetDocumentEdited(edited);
  563. }
  564. bool BaseWindow::IsDocumentEdited() const {
  565. return window_->IsDocumentEdited();
  566. }
  567. void BaseWindow::SetIgnoreMouseEvents(bool ignore,
  568. gin_helper::Arguments* args) {
  569. gin_helper::Dictionary options;
  570. bool forward = false;
  571. args->GetNext(&options) && options.Get("forward", &forward);
  572. return window_->SetIgnoreMouseEvents(ignore, forward);
  573. }
  574. void BaseWindow::SetContentProtection(bool enable) {
  575. return window_->SetContentProtection(enable);
  576. }
  577. bool BaseWindow::IsContentProtected() const {
  578. return window_->IsContentProtected();
  579. }
  580. void BaseWindow::SetFocusable(bool focusable) {
  581. return window_->SetFocusable(focusable);
  582. }
  583. bool BaseWindow::IsFocusable() const {
  584. return window_->IsFocusable();
  585. }
  586. void BaseWindow::SetMenu(v8::Isolate* isolate, v8::Local<v8::Value> value) {
  587. auto context = isolate->GetCurrentContext();
  588. gin::Handle<Menu> menu;
  589. v8::Local<v8::Object> object;
  590. if (value->IsObject() && value->ToObject(context).ToLocal(&object) &&
  591. gin::ConvertFromV8(isolate, value, &menu) && !menu.IsEmpty()) {
  592. // We only want to update the menu if the menu has a non-zero item count,
  593. // or we risk crashes.
  594. if (menu->model()->GetItemCount() == 0) {
  595. RemoveMenu();
  596. } else {
  597. window_->SetMenu(menu->model());
  598. }
  599. menu_.Reset(isolate, menu.ToV8());
  600. } else if (value->IsNull()) {
  601. RemoveMenu();
  602. } else {
  603. isolate->ThrowException(
  604. v8::Exception::TypeError(gin::StringToV8(isolate, "Invalid Menu")));
  605. }
  606. }
  607. void BaseWindow::RemoveMenu() {
  608. menu_.Reset();
  609. window_->SetMenu(nullptr);
  610. }
  611. void BaseWindow::SetParentWindow(v8::Local<v8::Value> value,
  612. gin_helper::Arguments* args) {
  613. if (IsModal()) {
  614. args->ThrowError("Can not be called for modal window");
  615. return;
  616. }
  617. gin::Handle<BaseWindow> parent;
  618. if (value->IsNull() || value->IsUndefined()) {
  619. RemoveFromParentChildWindows();
  620. parent_window_.Reset();
  621. window_->SetParentWindow(nullptr);
  622. } else if (gin::ConvertFromV8(isolate(), value, &parent)) {
  623. RemoveFromParentChildWindows();
  624. parent_window_.Reset(isolate(), value);
  625. window_->SetParentWindow(parent->window_.get());
  626. parent->child_windows_.Set(isolate(), weak_map_id(), GetWrapper());
  627. } else {
  628. args->ThrowError("Must pass BaseWindow instance or null");
  629. }
  630. }
  631. std::string BaseWindow::GetMediaSourceId() const {
  632. return window_->GetDesktopMediaID().ToString();
  633. }
  634. v8::Local<v8::Value> BaseWindow::GetNativeWindowHandle() {
  635. // TODO(MarshallOfSound): Replace once
  636. // https://chromium-review.googlesource.com/c/chromium/src/+/1253094/ has
  637. // landed
  638. NativeWindowHandle handle = window_->GetNativeWindowHandle();
  639. return ToBuffer(isolate(), &handle, sizeof(handle));
  640. }
  641. void BaseWindow::SetProgressBar(double progress, gin_helper::Arguments* args) {
  642. gin_helper::Dictionary options;
  643. std::string mode;
  644. args->GetNext(&options) && options.Get("mode", &mode);
  645. NativeWindow::ProgressState state = NativeWindow::ProgressState::kNormal;
  646. if (mode == "error")
  647. state = NativeWindow::ProgressState::kError;
  648. else if (mode == "paused")
  649. state = NativeWindow::ProgressState::kPaused;
  650. else if (mode == "indeterminate")
  651. state = NativeWindow::ProgressState::kIndeterminate;
  652. else if (mode == "none")
  653. state = NativeWindow::ProgressState::kNone;
  654. window_->SetProgressBar(progress, state);
  655. }
  656. void BaseWindow::SetOverlayIcon(const gfx::Image& overlay,
  657. const std::string& description) {
  658. window_->SetOverlayIcon(overlay, description);
  659. }
  660. void BaseWindow::SetVisibleOnAllWorkspaces(bool visible,
  661. gin_helper::Arguments* args) {
  662. gin_helper::Dictionary options;
  663. bool visibleOnFullScreen = false;
  664. bool skipTransformProcessType = false;
  665. if (args->GetNext(&options)) {
  666. options.Get("visibleOnFullScreen", &visibleOnFullScreen);
  667. options.Get("skipTransformProcessType", &skipTransformProcessType);
  668. }
  669. return window_->SetVisibleOnAllWorkspaces(visible, visibleOnFullScreen,
  670. skipTransformProcessType);
  671. }
  672. bool BaseWindow::IsVisibleOnAllWorkspaces() const {
  673. return window_->IsVisibleOnAllWorkspaces();
  674. }
  675. void BaseWindow::SetAutoHideCursor(bool auto_hide) {
  676. window_->SetAutoHideCursor(auto_hide);
  677. }
  678. void BaseWindow::SetVibrancy(v8::Isolate* isolate,
  679. v8::Local<v8::Value> value,
  680. gin_helper::Arguments* args) {
  681. std::string type = gin::V8ToString(isolate, value);
  682. gin_helper::Dictionary options;
  683. int animation_duration_ms = 0;
  684. if (args->GetNext(&options)) {
  685. options.Get("animationDuration", &animation_duration_ms);
  686. }
  687. window_->SetVibrancy(type, animation_duration_ms);
  688. }
  689. void BaseWindow::SetBackgroundMaterial(const std::string& material_type) {
  690. window_->SetBackgroundMaterial(material_type);
  691. }
  692. #if BUILDFLAG(IS_MAC)
  693. std::string BaseWindow::GetAlwaysOnTopLevel() const {
  694. return window_->GetAlwaysOnTopLevel();
  695. }
  696. void BaseWindow::SetWindowButtonVisibility(bool visible) {
  697. window_->SetWindowButtonVisibility(visible);
  698. }
  699. bool BaseWindow::GetWindowButtonVisibility() const {
  700. return window_->GetWindowButtonVisibility();
  701. }
  702. void BaseWindow::SetWindowButtonPosition(std::optional<gfx::Point> position) {
  703. window_->SetWindowButtonPosition(std::move(position));
  704. }
  705. std::optional<gfx::Point> BaseWindow::GetWindowButtonPosition() const {
  706. return window_->GetWindowButtonPosition();
  707. }
  708. #endif
  709. #if BUILDFLAG(IS_MAC)
  710. bool BaseWindow::IsHiddenInMissionControl() {
  711. return window_->IsHiddenInMissionControl();
  712. }
  713. void BaseWindow::SetHiddenInMissionControl(bool hidden) {
  714. window_->SetHiddenInMissionControl(hidden);
  715. }
  716. #endif
  717. void BaseWindow::SetTouchBar(
  718. std::vector<gin_helper::PersistentDictionary> items) {
  719. window_->SetTouchBar(std::move(items));
  720. }
  721. void BaseWindow::RefreshTouchBarItem(const std::string& item_id) {
  722. window_->RefreshTouchBarItem(item_id);
  723. }
  724. void BaseWindow::SetEscapeTouchBarItem(gin_helper::PersistentDictionary item) {
  725. window_->SetEscapeTouchBarItem(std::move(item));
  726. }
  727. void BaseWindow::SelectPreviousTab() {
  728. window_->SelectPreviousTab();
  729. }
  730. void BaseWindow::SelectNextTab() {
  731. window_->SelectNextTab();
  732. }
  733. void BaseWindow::ShowAllTabs() {
  734. window_->ShowAllTabs();
  735. }
  736. void BaseWindow::MergeAllWindows() {
  737. window_->MergeAllWindows();
  738. }
  739. void BaseWindow::MoveTabToNewWindow() {
  740. window_->MoveTabToNewWindow();
  741. }
  742. void BaseWindow::ToggleTabBar() {
  743. window_->ToggleTabBar();
  744. }
  745. void BaseWindow::AddTabbedWindow(NativeWindow* window,
  746. gin_helper::Arguments* args) {
  747. if (!window_->AddTabbedWindow(window))
  748. args->ThrowError("AddTabbedWindow cannot be called by a window on itself.");
  749. }
  750. v8::Local<v8::Value> BaseWindow::GetTabbingIdentifier() {
  751. auto tabbing_id = window_->GetTabbingIdentifier();
  752. if (!tabbing_id.has_value())
  753. return v8::Undefined(isolate());
  754. return gin::ConvertToV8(isolate(), tabbing_id.value());
  755. }
  756. void BaseWindow::SetAutoHideMenuBar(bool auto_hide) {
  757. window_->SetAutoHideMenuBar(auto_hide);
  758. }
  759. bool BaseWindow::IsMenuBarAutoHide() const {
  760. return window_->IsMenuBarAutoHide();
  761. }
  762. void BaseWindow::SetMenuBarVisibility(bool visible) {
  763. window_->SetMenuBarVisibility(visible);
  764. }
  765. bool BaseWindow::IsMenuBarVisible() const {
  766. return window_->IsMenuBarVisible();
  767. }
  768. void BaseWindow::SetAspectRatio(double aspect_ratio,
  769. gin_helper::Arguments* args) {
  770. gfx::Size extra_size;
  771. args->GetNext(&extra_size);
  772. window_->SetAspectRatio(aspect_ratio, extra_size);
  773. }
  774. void BaseWindow::PreviewFile(const std::string& path,
  775. gin_helper::Arguments* args) {
  776. std::string display_name;
  777. if (!args->GetNext(&display_name))
  778. display_name = path;
  779. window_->PreviewFile(path, display_name);
  780. }
  781. void BaseWindow::CloseFilePreview() {
  782. window_->CloseFilePreview();
  783. }
  784. void BaseWindow::SetGTKDarkThemeEnabled(bool use_dark_theme) {
  785. window_->SetGTKDarkThemeEnabled(use_dark_theme);
  786. }
  787. v8::Local<v8::Value> BaseWindow::GetContentView() const {
  788. if (content_view_.IsEmpty())
  789. return v8::Null(isolate());
  790. else
  791. return v8::Local<v8::Value>::New(isolate(), content_view_);
  792. }
  793. v8::Local<v8::Value> BaseWindow::GetParentWindow() const {
  794. if (parent_window_.IsEmpty())
  795. return v8::Null(isolate());
  796. else
  797. return v8::Local<v8::Value>::New(isolate(), parent_window_);
  798. }
  799. std::vector<v8::Local<v8::Object>> BaseWindow::GetChildWindows() const {
  800. return child_windows_.Values(isolate());
  801. }
  802. bool BaseWindow::IsModal() const {
  803. return window_->is_modal();
  804. }
  805. bool BaseWindow::SetThumbarButtons(gin_helper::Arguments* args) {
  806. #if BUILDFLAG(IS_WIN)
  807. std::vector<TaskbarHost::ThumbarButton> buttons;
  808. if (!args->GetNext(&buttons)) {
  809. args->ThrowError();
  810. return false;
  811. }
  812. auto* window = static_cast<NativeWindowViews*>(window_.get());
  813. return window->taskbar_host().SetThumbarButtons(
  814. window_->GetAcceleratedWidget(), buttons);
  815. #else
  816. return false;
  817. #endif
  818. }
  819. #if defined(TOOLKIT_VIEWS)
  820. void BaseWindow::SetIcon(v8::Isolate* isolate, v8::Local<v8::Value> icon) {
  821. SetIconImpl(isolate, icon, NativeImage::OnConvertError::kThrow);
  822. }
  823. void BaseWindow::SetIconImpl(v8::Isolate* isolate,
  824. v8::Local<v8::Value> icon,
  825. NativeImage::OnConvertError on_error) {
  826. NativeImage* native_image = nullptr;
  827. if (!NativeImage::TryConvertNativeImage(isolate, icon, &native_image,
  828. on_error))
  829. return;
  830. #if BUILDFLAG(IS_WIN)
  831. static_cast<NativeWindowViews*>(window_.get())
  832. ->SetIcon(native_image->GetHICON(GetSystemMetrics(SM_CXSMICON)),
  833. native_image->GetHICON(GetSystemMetrics(SM_CXICON)));
  834. #elif BUILDFLAG(IS_LINUX)
  835. static_cast<NativeWindowViews*>(window_.get())
  836. ->SetIcon(native_image->image().AsImageSkia());
  837. #endif
  838. }
  839. #endif
  840. #if BUILDFLAG(IS_WIN)
  841. bool BaseWindow::HookWindowMessage(UINT message,
  842. const MessageCallback& callback) {
  843. messages_callback_map_[message] = callback;
  844. return true;
  845. }
  846. void BaseWindow::UnhookWindowMessage(UINT message) {
  847. messages_callback_map_.erase(message);
  848. }
  849. bool BaseWindow::IsWindowMessageHooked(UINT message) {
  850. return messages_callback_map_.contains(message);
  851. }
  852. void BaseWindow::UnhookAllWindowMessages() {
  853. messages_callback_map_.clear();
  854. }
  855. bool BaseWindow::SetThumbnailClip(const gfx::Rect& region) {
  856. auto* window = static_cast<NativeWindowViews*>(window_.get());
  857. return window->taskbar_host().SetThumbnailClip(
  858. window_->GetAcceleratedWidget(), region);
  859. }
  860. bool BaseWindow::SetThumbnailToolTip(const std::string& tooltip) {
  861. auto* window = static_cast<NativeWindowViews*>(window_.get());
  862. return window->taskbar_host().SetThumbnailToolTip(
  863. window_->GetAcceleratedWidget(), tooltip);
  864. }
  865. void BaseWindow::SetAppDetails(const gin_helper::Dictionary& options) {
  866. std::wstring app_id;
  867. base::FilePath app_icon_path;
  868. int app_icon_index = 0;
  869. std::wstring relaunch_command;
  870. std::wstring relaunch_display_name;
  871. options.Get("appId", &app_id);
  872. options.Get("appIconPath", &app_icon_path);
  873. options.Get("appIconIndex", &app_icon_index);
  874. options.Get("relaunchCommand", &relaunch_command);
  875. options.Get("relaunchDisplayName", &relaunch_display_name);
  876. ui::win::SetAppDetailsForWindow(app_id, app_icon_path, app_icon_index,
  877. relaunch_command, relaunch_display_name,
  878. window_->GetAcceleratedWidget());
  879. }
  880. bool BaseWindow::IsSnapped() const {
  881. return window_->IsSnapped();
  882. }
  883. #endif
  884. #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX)
  885. void BaseWindow::SetTitleBarOverlay(const gin_helper::Dictionary& options,
  886. gin_helper::Arguments* args) {
  887. // Ensure WCO is already enabled on this window
  888. if (!window_->IsWindowControlsOverlayEnabled()) {
  889. args->ThrowError("Titlebar overlay is not enabled");
  890. return;
  891. }
  892. auto* window = static_cast<NativeWindowViews*>(window_.get());
  893. bool updated = false;
  894. // Check and update the button color
  895. std::string btn_color;
  896. if (options.Get(options::kOverlayButtonColor, &btn_color)) {
  897. // Parse the string as a CSS color
  898. SkColor color;
  899. if (!content::ParseCssColorString(btn_color, &color)) {
  900. args->ThrowError("Could not parse color as CSS color");
  901. return;
  902. }
  903. // Update the view
  904. window->set_overlay_button_color(color);
  905. updated = true;
  906. }
  907. // Check and update the symbol color
  908. std::string symbol_color;
  909. if (options.Get(options::kOverlaySymbolColor, &symbol_color)) {
  910. // Parse the string as a CSS color
  911. SkColor color;
  912. if (!content::ParseCssColorString(symbol_color, &color)) {
  913. args->ThrowError("Could not parse symbol color as CSS color");
  914. return;
  915. }
  916. // Update the view
  917. window->set_overlay_symbol_color(color);
  918. updated = true;
  919. }
  920. // Check and update the height
  921. int height = 0;
  922. if (options.Get(options::kOverlayHeight, &height)) {
  923. window->set_titlebar_overlay_height(height);
  924. updated = true;
  925. }
  926. if (!updated)
  927. return;
  928. // If anything was updated, ensure the overlay is repainted.
  929. #if BUILDFLAG(IS_WIN)
  930. auto* frame_view = static_cast<WinFrameView*>(
  931. window->widget()->non_client_view()->frame_view());
  932. #else
  933. auto* frame_view = static_cast<OpaqueFrameView*>(
  934. window->widget()->non_client_view()->frame_view());
  935. #endif
  936. frame_view->InvalidateCaptionButtons();
  937. }
  938. #endif
  939. int32_t BaseWindow::GetID() const {
  940. return weak_map_id();
  941. }
  942. void BaseWindow::RemoveFromParentChildWindows() {
  943. if (parent_window_.IsEmpty())
  944. return;
  945. gin::Handle<BaseWindow> parent;
  946. if (!gin::ConvertFromV8(isolate(), GetParentWindow(), &parent) ||
  947. parent.IsEmpty()) {
  948. return;
  949. }
  950. parent->child_windows_.Remove(weak_map_id());
  951. }
  952. // static
  953. gin_helper::WrappableBase* BaseWindow::New(gin_helper::Arguments* args) {
  954. auto options = gin_helper::Dictionary::CreateEmpty(args->isolate());
  955. args->GetNext(&options);
  956. return new BaseWindow(args, options);
  957. }
  958. // static
  959. void BaseWindow::BuildPrototype(v8::Isolate* isolate,
  960. v8::Local<v8::FunctionTemplate> prototype) {
  961. prototype->SetClassName(gin::StringToV8(isolate, "BaseWindow"));
  962. gin_helper::Destroyable::MakeDestroyable(isolate, prototype);
  963. gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
  964. .SetMethod("setContentView", &BaseWindow::SetContentView)
  965. .SetMethod("close", &BaseWindow::Close)
  966. .SetMethod("focus", &BaseWindow::Focus)
  967. .SetMethod("blur", &BaseWindow::Blur)
  968. .SetMethod("isFocused", &BaseWindow::IsFocused)
  969. .SetMethod("show", &BaseWindow::Show)
  970. .SetMethod("showInactive", &BaseWindow::ShowInactive)
  971. .SetMethod("hide", &BaseWindow::Hide)
  972. .SetMethod("isVisible", &BaseWindow::IsVisible)
  973. .SetMethod("isEnabled", &BaseWindow::IsEnabled)
  974. .SetMethod("setEnabled", &BaseWindow::SetEnabled)
  975. .SetMethod("maximize", &BaseWindow::Maximize)
  976. .SetMethod("unmaximize", &BaseWindow::Unmaximize)
  977. .SetMethod("isMaximized", &BaseWindow::IsMaximized)
  978. .SetMethod("minimize", &BaseWindow::Minimize)
  979. .SetMethod("restore", &BaseWindow::Restore)
  980. .SetMethod("isMinimized", &BaseWindow::IsMinimized)
  981. .SetMethod("setFullScreen", &BaseWindow::SetFullScreen)
  982. .SetMethod("isFullScreen", &BaseWindow::IsFullscreen)
  983. .SetMethod("setBounds", &BaseWindow::SetBounds)
  984. .SetMethod("getBounds", &BaseWindow::GetBounds)
  985. .SetMethod("isNormal", &BaseWindow::IsNormal)
  986. .SetMethod("getNormalBounds", &BaseWindow::GetNormalBounds)
  987. .SetMethod("setSize", &BaseWindow::SetSize)
  988. .SetMethod("getSize", &BaseWindow::GetSize)
  989. .SetMethod("setContentBounds", &BaseWindow::SetContentBounds)
  990. .SetMethod("getContentBounds", &BaseWindow::GetContentBounds)
  991. .SetMethod("setContentSize", &BaseWindow::SetContentSize)
  992. .SetMethod("getContentSize", &BaseWindow::GetContentSize)
  993. .SetMethod("setMinimumSize", &BaseWindow::SetMinimumSize)
  994. .SetMethod("getMinimumSize", &BaseWindow::GetMinimumSize)
  995. .SetMethod("setMaximumSize", &BaseWindow::SetMaximumSize)
  996. .SetMethod("getMaximumSize", &BaseWindow::GetMaximumSize)
  997. .SetMethod("setSheetOffset", &BaseWindow::SetSheetOffset)
  998. .SetMethod("moveAbove", &BaseWindow::MoveAbove)
  999. .SetMethod("moveTop", &BaseWindow::MoveTop)
  1000. .SetMethod("setResizable", &BaseWindow::SetResizable)
  1001. .SetMethod("isResizable", &BaseWindow::IsResizable)
  1002. .SetMethod("setMovable", &BaseWindow::SetMovable)
  1003. .SetMethod("isMovable", &BaseWindow::IsMovable)
  1004. .SetMethod("setMinimizable", &BaseWindow::SetMinimizable)
  1005. .SetMethod("isMinimizable", &BaseWindow::IsMinimizable)
  1006. .SetMethod("setMaximizable", &BaseWindow::SetMaximizable)
  1007. .SetMethod("isMaximizable", &BaseWindow::IsMaximizable)
  1008. .SetMethod("setFullScreenable", &BaseWindow::SetFullScreenable)
  1009. .SetMethod("isFullScreenable", &BaseWindow::IsFullScreenable)
  1010. .SetMethod("setClosable", &BaseWindow::SetClosable)
  1011. .SetMethod("isClosable", &BaseWindow::IsClosable)
  1012. .SetMethod("setAlwaysOnTop", &BaseWindow::SetAlwaysOnTop)
  1013. .SetMethod("isAlwaysOnTop", &BaseWindow::IsAlwaysOnTop)
  1014. .SetMethod("center", &BaseWindow::Center)
  1015. .SetMethod("setPosition", &BaseWindow::SetPosition)
  1016. .SetMethod("getPosition", &BaseWindow::GetPosition)
  1017. .SetMethod("setTitle", &BaseWindow::SetTitle)
  1018. .SetMethod("getTitle", &BaseWindow::GetTitle)
  1019. .SetProperty("accessibleTitle", &BaseWindow::GetAccessibleTitle,
  1020. &BaseWindow::SetAccessibleTitle)
  1021. .SetMethod("flashFrame", &BaseWindow::FlashFrame)
  1022. .SetMethod("setSkipTaskbar", &BaseWindow::SetSkipTaskbar)
  1023. .SetMethod("setSimpleFullScreen", &BaseWindow::SetSimpleFullScreen)
  1024. .SetMethod("isSimpleFullScreen", &BaseWindow::IsSimpleFullScreen)
  1025. .SetMethod("setKiosk", &BaseWindow::SetKiosk)
  1026. .SetMethod("isKiosk", &BaseWindow::IsKiosk)
  1027. .SetMethod("isTabletMode", &BaseWindow::IsTabletMode)
  1028. .SetMethod("setBackgroundColor", &BaseWindow::SetBackgroundColor)
  1029. .SetMethod("getBackgroundColor", &BaseWindow::GetBackgroundColor)
  1030. .SetMethod("setHasShadow", &BaseWindow::SetHasShadow)
  1031. .SetMethod("hasShadow", &BaseWindow::HasShadow)
  1032. .SetMethod("setOpacity", &BaseWindow::SetOpacity)
  1033. .SetMethod("getOpacity", &BaseWindow::GetOpacity)
  1034. .SetMethod("setShape", &BaseWindow::SetShape)
  1035. .SetMethod("setRepresentedFilename", &BaseWindow::SetRepresentedFilename)
  1036. .SetMethod("getRepresentedFilename", &BaseWindow::GetRepresentedFilename)
  1037. .SetMethod("setDocumentEdited", &BaseWindow::SetDocumentEdited)
  1038. .SetMethod("isDocumentEdited", &BaseWindow::IsDocumentEdited)
  1039. .SetMethod("setIgnoreMouseEvents", &BaseWindow::SetIgnoreMouseEvents)
  1040. .SetMethod("setContentProtection", &BaseWindow::SetContentProtection)
  1041. .SetMethod("_isContentProtected", &BaseWindow::IsContentProtected)
  1042. .SetMethod("setFocusable", &BaseWindow::SetFocusable)
  1043. .SetMethod("isFocusable", &BaseWindow::IsFocusable)
  1044. .SetMethod("setMenu", &BaseWindow::SetMenu)
  1045. .SetMethod("removeMenu", &BaseWindow::RemoveMenu)
  1046. .SetMethod("setParentWindow", &BaseWindow::SetParentWindow)
  1047. .SetMethod("getMediaSourceId", &BaseWindow::GetMediaSourceId)
  1048. .SetMethod("getNativeWindowHandle", &BaseWindow::GetNativeWindowHandle)
  1049. .SetMethod("setProgressBar", &BaseWindow::SetProgressBar)
  1050. .SetMethod("setOverlayIcon", &BaseWindow::SetOverlayIcon)
  1051. .SetMethod("setVisibleOnAllWorkspaces",
  1052. &BaseWindow::SetVisibleOnAllWorkspaces)
  1053. .SetMethod("isVisibleOnAllWorkspaces",
  1054. &BaseWindow::IsVisibleOnAllWorkspaces)
  1055. #if BUILDFLAG(IS_MAC)
  1056. .SetMethod("invalidateShadow", &BaseWindow::InvalidateShadow)
  1057. .SetMethod("_getAlwaysOnTopLevel", &BaseWindow::GetAlwaysOnTopLevel)
  1058. .SetMethod("setAutoHideCursor", &BaseWindow::SetAutoHideCursor)
  1059. #endif
  1060. .SetMethod("setVibrancy", &BaseWindow::SetVibrancy)
  1061. .SetMethod("setBackgroundMaterial", &BaseWindow::SetBackgroundMaterial)
  1062. #if BUILDFLAG(IS_MAC)
  1063. .SetMethod("isHiddenInMissionControl",
  1064. &BaseWindow::IsHiddenInMissionControl)
  1065. .SetMethod("setHiddenInMissionControl",
  1066. &BaseWindow::SetHiddenInMissionControl)
  1067. #endif
  1068. .SetMethod("_setTouchBarItems", &BaseWindow::SetTouchBar)
  1069. .SetMethod("_refreshTouchBarItem", &BaseWindow::RefreshTouchBarItem)
  1070. .SetMethod("_setEscapeTouchBarItem", &BaseWindow::SetEscapeTouchBarItem)
  1071. #if BUILDFLAG(IS_MAC)
  1072. .SetMethod("selectPreviousTab", &BaseWindow::SelectPreviousTab)
  1073. .SetMethod("selectNextTab", &BaseWindow::SelectNextTab)
  1074. .SetMethod("showAllTabs", &BaseWindow::ShowAllTabs)
  1075. .SetMethod("mergeAllWindows", &BaseWindow::MergeAllWindows)
  1076. .SetMethod("moveTabToNewWindow", &BaseWindow::MoveTabToNewWindow)
  1077. .SetMethod("toggleTabBar", &BaseWindow::ToggleTabBar)
  1078. .SetMethod("addTabbedWindow", &BaseWindow::AddTabbedWindow)
  1079. .SetProperty("tabbingIdentifier", &BaseWindow::GetTabbingIdentifier)
  1080. .SetMethod("setWindowButtonVisibility",
  1081. &BaseWindow::SetWindowButtonVisibility)
  1082. .SetMethod("_getWindowButtonVisibility",
  1083. &BaseWindow::GetWindowButtonVisibility)
  1084. .SetMethod("setWindowButtonPosition",
  1085. &BaseWindow::SetWindowButtonPosition)
  1086. .SetMethod("getWindowButtonPosition",
  1087. &BaseWindow::GetWindowButtonPosition)
  1088. .SetProperty("excludedFromShownWindowsMenu",
  1089. &BaseWindow::IsExcludedFromShownWindowsMenu,
  1090. &BaseWindow::SetExcludedFromShownWindowsMenu)
  1091. #endif
  1092. .SetMethod("setAutoHideMenuBar", &BaseWindow::SetAutoHideMenuBar)
  1093. .SetMethod("isMenuBarAutoHide", &BaseWindow::IsMenuBarAutoHide)
  1094. .SetMethod("setMenuBarVisibility", &BaseWindow::SetMenuBarVisibility)
  1095. .SetMethod("isMenuBarVisible", &BaseWindow::IsMenuBarVisible)
  1096. .SetMethod("setAspectRatio", &BaseWindow::SetAspectRatio)
  1097. .SetMethod("previewFile", &BaseWindow::PreviewFile)
  1098. .SetMethod("closeFilePreview", &BaseWindow::CloseFilePreview)
  1099. .SetMethod("getContentView", &BaseWindow::GetContentView)
  1100. .SetProperty("contentView", &BaseWindow::GetContentView,
  1101. &BaseWindow::SetContentView)
  1102. .SetMethod("getParentWindow", &BaseWindow::GetParentWindow)
  1103. .SetMethod("getChildWindows", &BaseWindow::GetChildWindows)
  1104. .SetMethod("isModal", &BaseWindow::IsModal)
  1105. .SetMethod("setThumbarButtons", &BaseWindow::SetThumbarButtons)
  1106. #if defined(TOOLKIT_VIEWS)
  1107. .SetMethod("setIcon", &BaseWindow::SetIcon)
  1108. #endif
  1109. #if BUILDFLAG(IS_WIN)
  1110. .SetMethod("isSnapped", &BaseWindow::IsSnapped)
  1111. .SetProperty("snapped", &BaseWindow::IsSnapped)
  1112. .SetMethod("hookWindowMessage", &BaseWindow::HookWindowMessage)
  1113. .SetMethod("isWindowMessageHooked", &BaseWindow::IsWindowMessageHooked)
  1114. .SetMethod("unhookWindowMessage", &BaseWindow::UnhookWindowMessage)
  1115. .SetMethod("unhookAllWindowMessages",
  1116. &BaseWindow::UnhookAllWindowMessages)
  1117. .SetMethod("setThumbnailClip", &BaseWindow::SetThumbnailClip)
  1118. .SetMethod("setThumbnailToolTip", &BaseWindow::SetThumbnailToolTip)
  1119. .SetMethod("setAppDetails", &BaseWindow::SetAppDetails)
  1120. #endif
  1121. #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX)
  1122. .SetMethod("setTitleBarOverlay", &BaseWindow::SetTitleBarOverlay)
  1123. #endif
  1124. .SetProperty("id", &BaseWindow::GetID);
  1125. }
  1126. } // namespace electron::api
  1127. namespace {
  1128. using electron::api::BaseWindow;
  1129. void Initialize(v8::Local<v8::Object> exports,
  1130. v8::Local<v8::Value> unused,
  1131. v8::Local<v8::Context> context,
  1132. void* priv) {
  1133. v8::Isolate* isolate = context->GetIsolate();
  1134. BaseWindow::SetConstructor(isolate, base::BindRepeating(&BaseWindow::New));
  1135. gin_helper::Dictionary constructor(isolate,
  1136. BaseWindow::GetConstructor(isolate)
  1137. ->GetFunction(context)
  1138. .ToLocalChecked());
  1139. constructor.SetMethod("fromId", &BaseWindow::FromWeakMapID);
  1140. constructor.SetMethod("getAllWindows", &BaseWindow::GetAll);
  1141. gin_helper::Dictionary dict(isolate, exports);
  1142. dict.Set("BaseWindow", constructor);
  1143. }
  1144. } // namespace
  1145. NODE_LINKED_BINDING_CONTEXT_AWARE(electron_browser_base_window, Initialize)