forked from electron/electron
/
atom_api_top_level_window.cc
1120 lines (924 loc) · 33.5 KB
/
atom_api_top_level_window.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2018 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/browser/api/atom_api_top_level_window.h"
#include <string>
#include <vector>
#include "atom/browser/api/atom_api_browser_view.h"
#include "atom/browser/api/atom_api_menu.h"
#include "atom/browser/api/atom_api_view.h"
#include "atom/browser/api/atom_api_web_contents.h"
#include "atom/common/color_util.h"
#include "atom/common/native_mate_converters/callback.h"
#include "atom/common/native_mate_converters/file_path_converter.h"
#include "atom/common/native_mate_converters/gfx_converter.h"
#include "atom/common/native_mate_converters/image_converter.h"
#include "atom/common/native_mate_converters/string16_converter.h"
#include "atom/common/native_mate_converters/value_converter.h"
#include "atom/common/options_switches.h"
#include "native_mate/handle.h"
#include "native_mate/persistent_dictionary.h"
#if defined(TOOLKIT_VIEWS)
#include "atom/browser/native_window_views.h"
#endif
#if defined(OS_WIN)
#include "atom/browser/ui/win/taskbar_host.h"
#include "ui/base/win/shell.h"
#endif
#include "atom/common/node_includes.h"
#if defined(OS_WIN)
namespace mate {
template <>
struct Converter<atom::TaskbarHost::ThumbarButton> {
static bool FromV8(v8::Isolate* isolate,
v8::Handle<v8::Value> val,
atom::TaskbarHost::ThumbarButton* out) {
mate::Dictionary dict;
if (!ConvertFromV8(isolate, val, &dict))
return false;
dict.Get("click", &(out->clicked_callback));
dict.Get("tooltip", &(out->tooltip));
dict.Get("flags", &out->flags);
return dict.Get("icon", &(out->icon));
}
};
} // namespace mate
#endif
namespace atom {
namespace api {
namespace {
// Converts binary data to Buffer.
v8::Local<v8::Value> ToBuffer(v8::Isolate* isolate, void* val, int size) {
auto buffer = node::Buffer::Copy(isolate, static_cast<char*>(val), size);
if (buffer.IsEmpty())
return v8::Null(isolate);
else
return buffer.ToLocalChecked();
}
} // namespace
TopLevelWindow::TopLevelWindow(v8::Isolate* isolate,
const mate::Dictionary& options)
: weak_factory_(this) {
// The parent window.
mate::Handle<TopLevelWindow> parent;
if (options.Get("parent", &parent) && !parent.IsEmpty())
parent_window_.Reset(isolate, parent.ToV8());
#if defined(ENABLE_OSR)
// Offscreen windows are always created frameless.
mate::Dictionary web_preferences;
bool offscreen;
if (options.Get(options::kWebPreferences, &web_preferences) &&
web_preferences.Get(options::kOffscreen, &offscreen) && offscreen) {
const_cast<mate::Dictionary&>(options).Set(options::kFrame, false);
}
#endif
// Creates NativeWindow.
window_.reset(NativeWindow::Create(
options, parent.IsEmpty() ? nullptr : parent->window_.get()));
window_->AddObserver(this);
#if defined(TOOLKIT_VIEWS)
// Sets the window icon.
mate::Handle<NativeImage> icon;
if (options.Get(options::kIcon, &icon) && !icon.IsEmpty())
SetIcon(icon);
#endif
}
TopLevelWindow::TopLevelWindow(v8::Isolate* isolate,
v8::Local<v8::Object> wrapper,
const mate::Dictionary& options)
: TopLevelWindow(isolate, options) {
InitWith(isolate, wrapper);
// Init window after everything has been setup.
window()->InitFromOptions(options);
}
TopLevelWindow::~TopLevelWindow() {
if (!window_->IsClosed())
window_->CloseImmediately();
// Destroy the native window in next tick because the native code might be
// iterating all windows.
base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, window_.release());
}
void TopLevelWindow::InitWith(v8::Isolate* isolate,
v8::Local<v8::Object> wrapper) {
AttachAsUserData(window_.get());
mate::TrackableObject<TopLevelWindow>::InitWith(isolate, wrapper);
// We can only append this window to parent window's child windows after this
// window's JS wrapper gets initialized.
if (!parent_window_.IsEmpty()) {
mate::Handle<TopLevelWindow> parent;
mate::ConvertFromV8(isolate, GetParentWindow(), &parent);
DCHECK(!parent.IsEmpty());
parent->child_windows_.Set(isolate, weak_map_id(), wrapper);
}
}
void TopLevelWindow::WillCloseWindow(bool* prevent_default) {
if (Emit("close")) {
*prevent_default = true;
}
}
void TopLevelWindow::OnWindowClosed() {
RemoveFromWeakMap();
window_->RemoveObserver(this);
// We can not call Destroy here because we need to call Emit first, but we
// also do not want any method to be used, so just mark as destroyed here.
MarkDestroyed();
Emit("closed");
RemoveFromParentChildWindows();
ResetBrowserView();
// Destroy the native class when window is closed.
base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, GetDestroyClosure());
}
void TopLevelWindow::OnWindowEndSession() {
Emit("session-end");
}
void TopLevelWindow::OnWindowBlur() {
Emit("blur");
}
void TopLevelWindow::OnWindowFocus() {
Emit("focus");
}
void TopLevelWindow::OnWindowShow() {
Emit("show");
}
void TopLevelWindow::OnWindowHide() {
Emit("hide");
}
void TopLevelWindow::OnWindowMaximize() {
Emit("maximize");
}
void TopLevelWindow::OnWindowUnmaximize() {
Emit("unmaximize");
}
void TopLevelWindow::OnWindowMinimize() {
Emit("minimize");
}
void TopLevelWindow::OnWindowRestore() {
Emit("restore");
}
void TopLevelWindow::OnWindowWillResize(const gfx::Rect& new_bounds,
bool* prevent_default) {
if (Emit("will-resize", new_bounds)) {
*prevent_default = true;
}
}
void TopLevelWindow::OnWindowResize() {
Emit("resize");
}
void TopLevelWindow::OnWindowWillMove(const gfx::Rect& new_bounds,
bool* prevent_default) {
if (Emit("will-move", new_bounds)) {
*prevent_default = true;
}
}
void TopLevelWindow::OnWindowMove() {
Emit("move");
}
void TopLevelWindow::OnWindowMoved() {
Emit("moved");
}
void TopLevelWindow::OnWindowEnterFullScreen() {
Emit("enter-full-screen");
}
void TopLevelWindow::OnWindowLeaveFullScreen() {
Emit("leave-full-screen");
}
void TopLevelWindow::OnWindowScrollTouchBegin() {
Emit("scroll-touch-begin");
}
void TopLevelWindow::OnWindowScrollTouchEnd() {
Emit("scroll-touch-end");
}
void TopLevelWindow::OnWindowSwipe(const std::string& direction) {
Emit("swipe", direction);
}
void TopLevelWindow::OnWindowSheetBegin() {
Emit("sheet-begin");
}
void TopLevelWindow::OnWindowSheetEnd() {
Emit("sheet-end");
}
void TopLevelWindow::OnWindowEnterHtmlFullScreen() {
Emit("enter-html-full-screen");
}
void TopLevelWindow::OnWindowLeaveHtmlFullScreen() {
Emit("leave-html-full-screen");
}
void TopLevelWindow::OnExecuteWindowsCommand(const std::string& command_name) {
Emit("app-command", command_name);
}
void TopLevelWindow::OnTouchBarItemResult(
const std::string& item_id,
const base::DictionaryValue& details) {
Emit("-touch-bar-interaction", item_id, details);
}
void TopLevelWindow::OnNewWindowForTab() {
Emit("new-window-for-tab");
}
#if defined(OS_WIN)
void TopLevelWindow::OnWindowMessage(UINT message,
WPARAM w_param,
LPARAM l_param) {
if (IsWindowMessageHooked(message)) {
messages_callback_map_[message].Run(
ToBuffer(isolate(), static_cast<void*>(&w_param), sizeof(WPARAM)),
ToBuffer(isolate(), static_cast<void*>(&l_param), sizeof(LPARAM)));
}
}
#endif
void TopLevelWindow::SetContentView(mate::Handle<View> view) {
content_view_.Reset(isolate(), view.ToV8());
window_->SetContentView(view->view());
}
void TopLevelWindow::Close() {
window_->Close();
}
void TopLevelWindow::Focus() {
window_->Focus(true);
}
void TopLevelWindow::Blur() {
window_->Focus(false);
}
bool TopLevelWindow::IsFocused() {
return window_->IsFocused();
}
void TopLevelWindow::Show() {
window_->Show();
}
void TopLevelWindow::ShowInactive() {
// This method doesn't make sense for modal window.
if (IsModal())
return;
window_->ShowInactive();
}
void TopLevelWindow::Hide() {
window_->Hide();
}
bool TopLevelWindow::IsVisible() {
return window_->IsVisible();
}
bool TopLevelWindow::IsEnabled() {
return window_->IsEnabled();
}
void TopLevelWindow::SetEnabled(bool enable) {
window_->SetEnabled(enable);
}
void TopLevelWindow::Maximize() {
window_->Maximize();
}
void TopLevelWindow::Unmaximize() {
window_->Unmaximize();
}
bool TopLevelWindow::IsMaximized() {
return window_->IsMaximized();
}
void TopLevelWindow::Minimize() {
window_->Minimize();
}
void TopLevelWindow::Restore() {
window_->Restore();
}
bool TopLevelWindow::IsMinimized() {
return window_->IsMinimized();
}
void TopLevelWindow::SetFullScreen(bool fullscreen) {
window_->SetFullScreen(fullscreen);
}
bool TopLevelWindow::IsFullscreen() {
return window_->IsFullscreen();
}
void TopLevelWindow::SetBounds(const gfx::Rect& bounds, mate::Arguments* args) {
bool animate = false;
args->GetNext(&animate);
window_->SetBounds(bounds, animate);
}
gfx::Rect TopLevelWindow::GetBounds() {
return window_->GetBounds();
}
void TopLevelWindow::SetContentBounds(const gfx::Rect& bounds,
mate::Arguments* args) {
bool animate = false;
args->GetNext(&animate);
window_->SetContentBounds(bounds, animate);
}
gfx::Rect TopLevelWindow::GetContentBounds() {
return window_->GetContentBounds();
}
void TopLevelWindow::SetSize(int width, int height, mate::Arguments* args) {
bool animate = false;
args->GetNext(&animate);
window_->SetSize(gfx::Size(width, height), animate);
}
std::vector<int> TopLevelWindow::GetSize() {
std::vector<int> result(2);
gfx::Size size = window_->GetSize();
result[0] = size.width();
result[1] = size.height();
return result;
}
void TopLevelWindow::SetContentSize(int width,
int height,
mate::Arguments* args) {
bool animate = false;
args->GetNext(&animate);
window_->SetContentSize(gfx::Size(width, height), animate);
}
std::vector<int> TopLevelWindow::GetContentSize() {
std::vector<int> result(2);
gfx::Size size = window_->GetContentSize();
result[0] = size.width();
result[1] = size.height();
return result;
}
void TopLevelWindow::SetMinimumSize(int width, int height) {
window_->SetMinimumSize(gfx::Size(width, height));
}
std::vector<int> TopLevelWindow::GetMinimumSize() {
std::vector<int> result(2);
gfx::Size size = window_->GetMinimumSize();
result[0] = size.width();
result[1] = size.height();
return result;
}
void TopLevelWindow::SetMaximumSize(int width, int height) {
window_->SetMaximumSize(gfx::Size(width, height));
}
std::vector<int> TopLevelWindow::GetMaximumSize() {
std::vector<int> result(2);
gfx::Size size = window_->GetMaximumSize();
result[0] = size.width();
result[1] = size.height();
return result;
}
void TopLevelWindow::SetSheetOffset(double offsetY, mate::Arguments* args) {
double offsetX = 0.0;
args->GetNext(&offsetX);
window_->SetSheetOffset(offsetX, offsetY);
}
void TopLevelWindow::SetResizable(bool resizable) {
window_->SetResizable(resizable);
}
bool TopLevelWindow::IsResizable() {
return window_->IsResizable();
}
void TopLevelWindow::SetMovable(bool movable) {
window_->SetMovable(movable);
}
bool TopLevelWindow::IsMovable() {
return window_->IsMovable();
}
void TopLevelWindow::SetMinimizable(bool minimizable) {
window_->SetMinimizable(minimizable);
}
bool TopLevelWindow::IsMinimizable() {
return window_->IsMinimizable();
}
void TopLevelWindow::SetMaximizable(bool maximizable) {
window_->SetMaximizable(maximizable);
}
bool TopLevelWindow::IsMaximizable() {
return window_->IsMaximizable();
}
void TopLevelWindow::SetFullScreenable(bool fullscreenable) {
window_->SetFullScreenable(fullscreenable);
}
bool TopLevelWindow::IsFullScreenable() {
return window_->IsFullScreenable();
}
void TopLevelWindow::SetClosable(bool closable) {
window_->SetClosable(closable);
}
bool TopLevelWindow::IsClosable() {
return window_->IsClosable();
}
void TopLevelWindow::SetAlwaysOnTop(bool top, mate::Arguments* args) {
std::string level = "floating";
int relative_level = 0;
args->GetNext(&level);
args->GetNext(&relative_level);
std::string error;
window_->SetAlwaysOnTop(top, level, relative_level, &error);
if (!error.empty())
args->ThrowError(error);
}
bool TopLevelWindow::IsAlwaysOnTop() {
return window_->IsAlwaysOnTop();
}
void TopLevelWindow::Center() {
window_->Center();
}
void TopLevelWindow::SetPosition(int x, int y, mate::Arguments* args) {
bool animate = false;
args->GetNext(&animate);
window_->SetPosition(gfx::Point(x, y), animate);
}
std::vector<int> TopLevelWindow::GetPosition() {
std::vector<int> result(2);
gfx::Point pos = window_->GetPosition();
result[0] = pos.x();
result[1] = pos.y();
return result;
}
#if defined(OS_WIN) || defined(OS_MACOSX)
void TopLevelWindow::MoveTop() {
window_->MoveTop();
}
#endif
void TopLevelWindow::SetTitle(const std::string& title) {
window_->SetTitle(title);
}
std::string TopLevelWindow::GetTitle() {
return window_->GetTitle();
}
void TopLevelWindow::FlashFrame(bool flash) {
window_->FlashFrame(flash);
}
void TopLevelWindow::SetSkipTaskbar(bool skip) {
window_->SetSkipTaskbar(skip);
}
void TopLevelWindow::SetSimpleFullScreen(bool simple_fullscreen) {
window_->SetSimpleFullScreen(simple_fullscreen);
}
bool TopLevelWindow::IsSimpleFullScreen() {
return window_->IsSimpleFullScreen();
}
void TopLevelWindow::SetKiosk(bool kiosk) {
window_->SetKiosk(kiosk);
}
bool TopLevelWindow::IsKiosk() {
return window_->IsKiosk();
}
void TopLevelWindow::SetBackgroundColor(const std::string& color_name) {
SkColor color = ParseHexColor(color_name);
window_->SetBackgroundColor(color);
}
void TopLevelWindow::SetHasShadow(bool has_shadow) {
window_->SetHasShadow(has_shadow);
}
bool TopLevelWindow::HasShadow() {
return window_->HasShadow();
}
void TopLevelWindow::SetOpacity(const double opacity) {
window_->SetOpacity(opacity);
}
double TopLevelWindow::GetOpacity() {
return window_->GetOpacity();
}
void TopLevelWindow::SetShape(const std::vector<gfx::Rect>& rects) {
window_->widget()->SetShape(std::make_unique<std::vector<gfx::Rect>>(rects));
}
void TopLevelWindow::SetRepresentedFilename(const std::string& filename) {
window_->SetRepresentedFilename(filename);
}
std::string TopLevelWindow::GetRepresentedFilename() {
return window_->GetRepresentedFilename();
}
void TopLevelWindow::SetDocumentEdited(bool edited) {
window_->SetDocumentEdited(edited);
}
bool TopLevelWindow::IsDocumentEdited() {
return window_->IsDocumentEdited();
}
void TopLevelWindow::SetIgnoreMouseEvents(bool ignore, mate::Arguments* args) {
mate::Dictionary options;
bool forward = false;
args->GetNext(&options) && options.Get("forward", &forward);
return window_->SetIgnoreMouseEvents(ignore, forward);
}
void TopLevelWindow::SetContentProtection(bool enable) {
return window_->SetContentProtection(enable);
}
void TopLevelWindow::SetFocusable(bool focusable) {
return window_->SetFocusable(focusable);
}
void TopLevelWindow::SetMenu(v8::Isolate* isolate, v8::Local<v8::Value> value) {
mate::Handle<Menu> menu;
if (value->IsObject() &&
mate::V8ToString(value->ToObject()->GetConstructorName()) == "Menu" &&
mate::ConvertFromV8(isolate, value, &menu) && !menu.IsEmpty()) {
menu_.Reset(isolate, menu.ToV8());
window_->SetMenu(menu->model());
} else if (value->IsNull()) {
menu_.Reset();
window_->SetMenu(nullptr);
} else {
isolate->ThrowException(
v8::Exception::TypeError(mate::StringToV8(isolate, "Invalid Menu")));
}
}
void TopLevelWindow::SetParentWindow(v8::Local<v8::Value> value,
mate::Arguments* args) {
if (IsModal()) {
args->ThrowError("Can not be called for modal window");
return;
}
mate::Handle<TopLevelWindow> parent;
if (value->IsNull() || value->IsUndefined()) {
RemoveFromParentChildWindows();
parent_window_.Reset();
window_->SetParentWindow(nullptr);
} else if (mate::ConvertFromV8(isolate(), value, &parent)) {
parent_window_.Reset(isolate(), value);
window_->SetParentWindow(parent->window_.get());
parent->child_windows_.Set(isolate(), weak_map_id(), GetWrapper());
} else {
args->ThrowError("Must pass TopLevelWindow instance or null");
}
}
void TopLevelWindow::SetBrowserView(v8::Local<v8::Value> value) {
ResetBrowserView();
mate::Handle<BrowserView> browser_view;
if (value->IsNull() || value->IsUndefined()) {
window_->SetBrowserView(nullptr);
} else if (mate::ConvertFromV8(isolate(), value, &browser_view)) {
window_->SetBrowserView(browser_view->view());
browser_view->web_contents()->SetOwnerWindow(window_.get());
browser_view_.Reset(isolate(), value);
}
}
v8::Local<v8::Value> TopLevelWindow::GetNativeWindowHandle() {
gfx::AcceleratedWidget handle = window_->GetAcceleratedWidget();
return ToBuffer(isolate(), static_cast<void*>(&handle), sizeof(handle));
}
void TopLevelWindow::SetProgressBar(double progress, mate::Arguments* args) {
mate::Dictionary options;
std::string mode;
args->GetNext(&options) && options.Get("mode", &mode);
NativeWindow::ProgressState state = NativeWindow::PROGRESS_NORMAL;
if (mode == "error")
state = NativeWindow::PROGRESS_ERROR;
else if (mode == "paused")
state = NativeWindow::PROGRESS_PAUSED;
else if (mode == "indeterminate")
state = NativeWindow::PROGRESS_INDETERMINATE;
else if (mode == "none")
state = NativeWindow::PROGRESS_NONE;
window_->SetProgressBar(progress, state);
}
void TopLevelWindow::SetOverlayIcon(const gfx::Image& overlay,
const std::string& description) {
window_->SetOverlayIcon(overlay, description);
}
void TopLevelWindow::SetVisibleOnAllWorkspaces(bool visible) {
return window_->SetVisibleOnAllWorkspaces(visible);
}
bool TopLevelWindow::IsVisibleOnAllWorkspaces() {
return window_->IsVisibleOnAllWorkspaces();
}
void TopLevelWindow::SetAutoHideCursor(bool auto_hide) {
window_->SetAutoHideCursor(auto_hide);
}
void TopLevelWindow::SetVibrancy(v8::Isolate* isolate,
v8::Local<v8::Value> value) {
std::string type = mate::V8ToString(value);
window_->SetVibrancy(type);
}
void TopLevelWindow::SetTouchBar(
const std::vector<mate::PersistentDictionary>& items) {
window_->SetTouchBar(items);
}
void TopLevelWindow::RefreshTouchBarItem(const std::string& item_id) {
window_->RefreshTouchBarItem(item_id);
}
void TopLevelWindow::SetEscapeTouchBarItem(
const mate::PersistentDictionary& item) {
window_->SetEscapeTouchBarItem(item);
}
void TopLevelWindow::SelectPreviousTab() {
window_->SelectPreviousTab();
}
void TopLevelWindow::SelectNextTab() {
window_->SelectNextTab();
}
void TopLevelWindow::MergeAllWindows() {
window_->MergeAllWindows();
}
void TopLevelWindow::MoveTabToNewWindow() {
window_->MoveTabToNewWindow();
}
void TopLevelWindow::ToggleTabBar() {
window_->ToggleTabBar();
}
void TopLevelWindow::AddTabbedWindow(NativeWindow* window,
mate::Arguments* args) {
if (!window_->AddTabbedWindow(window))
args->ThrowError("AddTabbedWindow cannot be called by a window on itself.");
}
void TopLevelWindow::SetWindowButtonVisibility(bool visible,
mate::Arguments* args) {
if (!window_->SetWindowButtonVisibility(visible)) {
args->ThrowError("Not supported for this window");
}
}
void TopLevelWindow::SetAutoHideMenuBar(bool auto_hide) {
window_->SetAutoHideMenuBar(auto_hide);
}
bool TopLevelWindow::IsMenuBarAutoHide() {
return window_->IsMenuBarAutoHide();
}
void TopLevelWindow::SetMenuBarVisibility(bool visible) {
window_->SetMenuBarVisibility(visible);
}
bool TopLevelWindow::IsMenuBarVisible() {
return window_->IsMenuBarVisible();
}
void TopLevelWindow::SetAspectRatio(double aspect_ratio,
mate::Arguments* args) {
gfx::Size extra_size;
args->GetNext(&extra_size);
window_->SetAspectRatio(aspect_ratio, extra_size);
}
void TopLevelWindow::PreviewFile(const std::string& path,
mate::Arguments* args) {
std::string display_name;
if (!args->GetNext(&display_name))
display_name = path;
window_->PreviewFile(path, display_name);
}
void TopLevelWindow::CloseFilePreview() {
window_->CloseFilePreview();
}
v8::Local<v8::Value> TopLevelWindow::GetContentView() const {
if (content_view_.IsEmpty())
return v8::Null(isolate());
else
return v8::Local<v8::Value>::New(isolate(), content_view_);
}
v8::Local<v8::Value> TopLevelWindow::GetParentWindow() const {
if (parent_window_.IsEmpty())
return v8::Null(isolate());
else
return v8::Local<v8::Value>::New(isolate(), parent_window_);
}
std::vector<v8::Local<v8::Object>> TopLevelWindow::GetChildWindows() const {
return child_windows_.Values(isolate());
}
v8::Local<v8::Value> TopLevelWindow::GetBrowserView() const {
if (browser_view_.IsEmpty()) {
return v8::Null(isolate());
}
return v8::Local<v8::Value>::New(isolate(), browser_view_);
}
bool TopLevelWindow::IsModal() const {
return window_->is_modal();
}
bool TopLevelWindow::SetThumbarButtons(mate::Arguments* args) {
#if defined(OS_WIN)
std::vector<TaskbarHost::ThumbarButton> buttons;
if (!args->GetNext(&buttons)) {
args->ThrowError();
return false;
}
auto* window = static_cast<NativeWindowViews*>(window_.get());
return window->taskbar_host().SetThumbarButtons(
window_->GetAcceleratedWidget(), buttons);
#else
return false;
#endif
}
#if defined(TOOLKIT_VIEWS)
void TopLevelWindow::SetIcon(mate::Handle<NativeImage> icon) {
#if defined(OS_WIN)
static_cast<NativeWindowViews*>(window_.get())
->SetIcon(icon->GetHICON(GetSystemMetrics(SM_CXSMICON)),
icon->GetHICON(GetSystemMetrics(SM_CXICON)));
#elif defined(USE_X11)
static_cast<NativeWindowViews*>(window_.get())
->SetIcon(icon->image().AsImageSkia());
#endif
}
#endif
#if defined(OS_WIN)
bool TopLevelWindow::HookWindowMessage(UINT message,
const MessageCallback& callback) {
messages_callback_map_[message] = callback;
return true;
}
void TopLevelWindow::UnhookWindowMessage(UINT message) {
if (!ContainsKey(messages_callback_map_, message))
return;
messages_callback_map_.erase(message);
}
bool TopLevelWindow::IsWindowMessageHooked(UINT message) {
return ContainsKey(messages_callback_map_, message);
}
void TopLevelWindow::UnhookAllWindowMessages() {
messages_callback_map_.clear();
}
bool TopLevelWindow::SetThumbnailClip(const gfx::Rect& region) {
auto* window = static_cast<NativeWindowViews*>(window_.get());
return window->taskbar_host().SetThumbnailClip(
window_->GetAcceleratedWidget(), region);
}
bool TopLevelWindow::SetThumbnailToolTip(const std::string& tooltip) {
auto* window = static_cast<NativeWindowViews*>(window_.get());
return window->taskbar_host().SetThumbnailToolTip(
window_->GetAcceleratedWidget(), tooltip);
}
void TopLevelWindow::SetAppDetails(const mate::Dictionary& options) {
base::string16 app_id;
base::FilePath app_icon_path;
int app_icon_index = 0;
base::string16 relaunch_command;
base::string16 relaunch_display_name;
options.Get("appId", &app_id);
options.Get("appIconPath", &app_icon_path);
options.Get("appIconIndex", &app_icon_index);
options.Get("relaunchCommand", &relaunch_command);
options.Get("relaunchDisplayName", &relaunch_display_name);
ui::win::SetAppDetailsForWindow(app_id, app_icon_path, app_icon_index,
relaunch_command, relaunch_display_name,
window_->GetAcceleratedWidget());
}
#endif
int32_t TopLevelWindow::GetID() const {
return weak_map_id();
}
void TopLevelWindow::ResetBrowserView() {
if (browser_view_.IsEmpty())
return;
mate::Handle<BrowserView> browser_view;
if (mate::ConvertFromV8(isolate(), GetBrowserView(), &browser_view) &&
!browser_view.IsEmpty()) {
browser_view->web_contents()->SetOwnerWindow(nullptr);
}
browser_view_.Reset();
}
void TopLevelWindow::RemoveFromParentChildWindows() {
if (parent_window_.IsEmpty())
return;
mate::Handle<TopLevelWindow> parent;
if (!mate::ConvertFromV8(isolate(), GetParentWindow(), &parent) ||
parent.IsEmpty()) {
return;
}
parent->child_windows_.Remove(weak_map_id());
}
// static
mate::WrappableBase* TopLevelWindow::New(mate::Arguments* args) {
mate::Dictionary options;
if (!(args->Length() == 1 && args->GetNext(&options)))
options = mate::Dictionary::CreateEmpty(args->isolate());
return new TopLevelWindow(args->isolate(), args->GetThis(), options);
}
// static
void TopLevelWindow::BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype) {
prototype->SetClassName(mate::StringToV8(isolate, "TopLevelWindow"));
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
.MakeDestroyable()
.SetMethod("setContentView", &TopLevelWindow::SetContentView)
.SetMethod("close", &TopLevelWindow::Close)
.SetMethod("focus", &TopLevelWindow::Focus)
.SetMethod("blur", &TopLevelWindow::Blur)
.SetMethod("isFocused", &TopLevelWindow::IsFocused)
.SetMethod("show", &TopLevelWindow::Show)
.SetMethod("showInactive", &TopLevelWindow::ShowInactive)
.SetMethod("hide", &TopLevelWindow::Hide)
.SetMethod("isVisible", &TopLevelWindow::IsVisible)
.SetMethod("isEnabled", &TopLevelWindow::IsEnabled)
.SetMethod("setEnabled", &TopLevelWindow::SetEnabled)
.SetMethod("maximize", &TopLevelWindow::Maximize)
.SetMethod("unmaximize", &TopLevelWindow::Unmaximize)
.SetMethod("isMaximized", &TopLevelWindow::IsMaximized)
.SetMethod("minimize", &TopLevelWindow::Minimize)
.SetMethod("restore", &TopLevelWindow::Restore)
.SetMethod("isMinimized", &TopLevelWindow::IsMinimized)
.SetMethod("setFullScreen", &TopLevelWindow::SetFullScreen)
.SetMethod("isFullScreen", &TopLevelWindow::IsFullscreen)
.SetMethod("setBounds", &TopLevelWindow::SetBounds)
.SetMethod("getBounds", &TopLevelWindow::GetBounds)
.SetMethod("setSize", &TopLevelWindow::SetSize)
.SetMethod("getSize", &TopLevelWindow::GetSize)
.SetMethod("setContentBounds", &TopLevelWindow::SetContentBounds)
.SetMethod("getContentBounds", &TopLevelWindow::GetContentBounds)
.SetMethod("setContentSize", &TopLevelWindow::SetContentSize)
.SetMethod("getContentSize", &TopLevelWindow::GetContentSize)
.SetMethod("setMinimumSize", &TopLevelWindow::SetMinimumSize)
.SetMethod("getMinimumSize", &TopLevelWindow::GetMinimumSize)
.SetMethod("setMaximumSize", &TopLevelWindow::SetMaximumSize)
.SetMethod("getMaximumSize", &TopLevelWindow::GetMaximumSize)
.SetMethod("setSheetOffset", &TopLevelWindow::SetSheetOffset)
.SetMethod("setResizable", &TopLevelWindow::SetResizable)
.SetMethod("isResizable", &TopLevelWindow::IsResizable)
.SetMethod("setMovable", &TopLevelWindow::SetMovable)
#if defined(OS_WIN) || defined(OS_MACOSX)
.SetMethod("moveTop", &TopLevelWindow::MoveTop)
#endif
.SetMethod("isMovable", &TopLevelWindow::IsMovable)
.SetMethod("setMinimizable", &TopLevelWindow::SetMinimizable)
.SetMethod("isMinimizable", &TopLevelWindow::IsMinimizable)
.SetMethod("setMaximizable", &TopLevelWindow::SetMaximizable)
.SetMethod("isMaximizable", &TopLevelWindow::IsMaximizable)
.SetMethod("setFullScreenable", &TopLevelWindow::SetFullScreenable)
.SetMethod("isFullScreenable", &TopLevelWindow::IsFullScreenable)