-
Notifications
You must be signed in to change notification settings - Fork 7k
/
widget.cc
1952 lines (1613 loc) · 62.1 KB
/
widget.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) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/views/widget/widget.h"
#include <set>
#include <utility>
#include "base/auto_reset.h"
#include "base/bind.h"
#include "base/check_op.h"
#include "base/containers/adapters.h"
#include "base/feature_list.h"
#include "base/i18n/rtl.h"
#include "base/notreached.h"
#include "base/strings/utf_string_conversions.h"
#include "base/trace_event/trace_event.h"
#include "ui/base/cursor/cursor.h"
#include "ui/base/default_style.h"
#include "ui/base/hit_test.h"
#include "ui/base/ime/input_method.h"
#include "ui/base/l10n/l10n_font_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/models/image_model.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/color/color_provider_manager.h"
#include "ui/compositor/compositor.h"
#include "ui/compositor/layer.h"
#include "ui/display/screen.h"
#include "ui/events/event.h"
#include "ui/events/event_utils.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/views/controls/menu/menu_controller.h"
#include "ui/views/event_monitor.h"
#include "ui/views/focus/focus_manager.h"
#include "ui/views/focus/focus_manager_factory.h"
#include "ui/views/focus/widget_focus_manager.h"
#include "ui/views/image_model_utils.h"
#include "ui/views/views_delegate.h"
#include "ui/views/views_features.h"
#include "ui/views/widget/any_widget_observer_singleton.h"
#include "ui/views/widget/native_widget_private.h"
#include "ui/views/widget/root_view.h"
#include "ui/views/widget/tooltip_manager.h"
#include "ui/views/widget/widget_delegate.h"
#include "ui/views/widget/widget_deletion_observer.h"
#include "ui/views/widget/widget_observer.h"
#include "ui/views/widget/widget_removals_observer.h"
#include "ui/views/window/custom_frame_view.h"
#include "ui/views/window/dialog_delegate.h"
#if defined(OS_LINUX)
#include "ui/views/linux_ui/linux_ui.h"
#endif
namespace views {
namespace {
// If |view| has a layer the layer is added to |layers|. Else this recurses
// through the children. This is used to build a list of the layers created by
// views that are direct children of the Widgets layer.
void BuildViewsWithLayers(View* view, View::Views* views) {
if (view->layer()) {
views->push_back(view);
} else {
for (View* child : view->children())
BuildViewsWithLayers(child, views);
}
}
// Create a native widget implementation.
// First, use the supplied one if non-NULL.
// Finally, make a default one.
NativeWidget* CreateNativeWidget(const Widget::InitParams& params,
internal::NativeWidgetDelegate* delegate) {
if (params.native_widget)
return params.native_widget;
const auto& factory = ViewsDelegate::GetInstance()->native_widget_factory();
if (!factory.is_null()) {
NativeWidget* native_widget = factory.Run(params, delegate);
if (native_widget)
return native_widget;
}
return internal::NativeWidgetPrivate::CreateNativeWidget(delegate);
}
void NotifyCaretBoundsChanged(ui::InputMethod* input_method) {
if (!input_method)
return;
ui::TextInputClient* client = input_method->GetTextInputClient();
if (client)
input_method->OnCaretBoundsChanged(client);
}
} // namespace
// static
Widget::DisableActivationChangeHandlingType
Widget::g_disable_activation_change_handling_ =
Widget::DisableActivationChangeHandlingType::kNone;
// A default implementation of WidgetDelegate, used by Widget when no
// WidgetDelegate is supplied.
class DefaultWidgetDelegate : public WidgetDelegate {
public:
DefaultWidgetDelegate() {
// In most situations where a Widget is used without a delegate the Widget
// is used as a container, so that we want focus to advance to the top-level
// widget. A good example of this is the find bar.
SetOwnedByWidget(true);
SetFocusTraversesOut(true);
}
DefaultWidgetDelegate(const DefaultWidgetDelegate&) = delete;
DefaultWidgetDelegate& operator=(const DefaultWidgetDelegate&) = delete;
~DefaultWidgetDelegate() override = default;
};
////////////////////////////////////////////////////////////////////////////////
// Widget, PaintAsActiveLock:
Widget::PaintAsActiveLock::PaintAsActiveLock() = default;
Widget::PaintAsActiveLock::~PaintAsActiveLock() = default;
////////////////////////////////////////////////////////////////////////////////
// Widget, PaintAsActiveLockImpl:
class Widget::PaintAsActiveLockImpl : public Widget::PaintAsActiveLock {
public:
explicit PaintAsActiveLockImpl(base::WeakPtr<Widget>&& widget)
: widget_(widget) {}
~PaintAsActiveLockImpl() override {
Widget* const widget = widget_.get();
if (widget)
widget->UnlockPaintAsActive();
}
private:
base::WeakPtr<Widget> widget_;
};
////////////////////////////////////////////////////////////////////////////////
// Widget, InitParams:
Widget::InitParams::InitParams() = default;
Widget::InitParams::InitParams(Type type) : type(type) {}
Widget::InitParams::InitParams(InitParams&& other) = default;
Widget::InitParams::~InitParams() = default;
bool Widget::InitParams::CanActivate() const {
if (activatable != InitParams::Activatable::kDefault)
return activatable == InitParams::Activatable::kYes;
return type != InitParams::TYPE_CONTROL && type != InitParams::TYPE_POPUP &&
type != InitParams::TYPE_MENU && type != InitParams::TYPE_TOOLTIP &&
type != InitParams::TYPE_DRAG;
}
ui::ZOrderLevel Widget::InitParams::EffectiveZOrderLevel() const {
if (z_order.has_value())
return z_order.value();
switch (type) {
case TYPE_MENU:
return ui::ZOrderLevel::kFloatingWindow;
case TYPE_DRAG:
return ui::ZOrderLevel::kFloatingUIElement;
default:
return ui::ZOrderLevel::kNormal;
}
}
////////////////////////////////////////////////////////////////////////////////
// Widget, public:
Widget::Widget() = default;
Widget::Widget(InitParams params) {
Init(std::move(params));
}
Widget::~Widget() {
if (widget_delegate_)
widget_delegate_->WidgetDestroying();
if (ownership_ == InitParams::WIDGET_OWNS_NATIVE_WIDGET) {
delete native_widget_;
DCHECK(native_widget_destroyed_);
} else {
// TODO(crbug.com/937381): Revert to DCHECK once we figure out the reason.
CHECK(native_widget_destroyed_)
<< "Destroying a widget with a live native widget. "
<< "Widget probably should use WIDGET_OWNS_NATIVE_WIDGET ownership.";
}
// Destroy RootView after the native widget, so in case the WidgetDelegate is
// a View in the RootView hierarchy it gets destroyed as a WidgetDelegate
// first.
// This makes destruction order for WidgetDelegate consistent between
// different Widget/NativeWidget ownership models (WidgetDelegate is always
// deleted before here, which may have removed it as a View from the
// View hierarchy).
DestroyRootView();
}
// static
Widget* Widget::CreateWindowWithParent(WidgetDelegate* delegate,
gfx::NativeView parent,
const gfx::Rect& bounds) {
Widget::InitParams params;
params.delegate = delegate;
params.parent = parent;
params.bounds = bounds;
return new Widget(std::move(params));
}
Widget* Widget::CreateWindowWithParent(std::unique_ptr<WidgetDelegate> delegate,
gfx::NativeView parent,
const gfx::Rect& bounds) {
DCHECK(delegate->owned_by_widget());
return CreateWindowWithParent(delegate.release(), parent, bounds);
}
// static
Widget* Widget::CreateWindowWithContext(WidgetDelegate* delegate,
gfx::NativeWindow context,
const gfx::Rect& bounds) {
Widget::InitParams params;
params.delegate = delegate;
params.context = context;
params.bounds = bounds;
return new Widget(std::move(params));
}
// static
Widget* Widget::CreateWindowWithContext(
std::unique_ptr<WidgetDelegate> delegate,
gfx::NativeWindow context,
const gfx::Rect& bounds) {
DCHECK(delegate->owned_by_widget());
return CreateWindowWithContext(delegate.release(), context, bounds);
}
// static
Widget* Widget::GetWidgetForNativeView(gfx::NativeView native_view) {
internal::NativeWidgetPrivate* native_widget =
internal::NativeWidgetPrivate::GetNativeWidgetForNativeView(native_view);
return native_widget ? native_widget->GetWidget() : nullptr;
}
// static
Widget* Widget::GetWidgetForNativeWindow(gfx::NativeWindow native_window) {
internal::NativeWidgetPrivate* native_widget =
internal::NativeWidgetPrivate::GetNativeWidgetForNativeWindow(
native_window);
return native_widget ? native_widget->GetWidget() : nullptr;
}
// static
Widget* Widget::GetTopLevelWidgetForNativeView(gfx::NativeView native_view) {
internal::NativeWidgetPrivate* native_widget =
internal::NativeWidgetPrivate::GetTopLevelNativeWidget(native_view);
return native_widget ? native_widget->GetWidget() : nullptr;
}
// static
void Widget::GetAllChildWidgets(gfx::NativeView native_view,
Widgets* children) {
internal::NativeWidgetPrivate::GetAllChildWidgets(native_view, children);
}
// static
void Widget::GetAllOwnedWidgets(gfx::NativeView native_view, Widgets* owned) {
internal::NativeWidgetPrivate::GetAllOwnedWidgets(native_view, owned);
}
// static
void Widget::ReparentNativeView(gfx::NativeView native_view,
gfx::NativeView new_parent) {
internal::NativeWidgetPrivate::ReparentNativeView(native_view, new_parent);
Widget* child_widget = GetWidgetForNativeView(native_view);
Widget* parent_widget =
new_parent ? GetWidgetForNativeView(new_parent) : nullptr;
if (child_widget) {
child_widget->parent_ = parent_widget;
// Release the paint-as-active lock on the old parent.
bool has_lock_on_parent = !!child_widget->parent_paint_as_active_lock_;
child_widget->parent_paint_as_active_lock_.reset();
child_widget->parent_paint_as_active_subscription_ =
base::CallbackListSubscription();
// Lock and subscribe to parent's paint-as-active.
if (parent_widget) {
if (has_lock_on_parent)
child_widget->parent_paint_as_active_lock_ =
parent_widget->LockPaintAsActive();
child_widget->parent_paint_as_active_subscription_ =
parent_widget->RegisterPaintAsActiveChangedCallback(
base::BindRepeating(&Widget::OnParentShouldPaintAsActiveChanged,
base::Unretained(child_widget)));
}
}
}
// static
int Widget::GetLocalizedContentsWidth(int col_resource_id) {
return ui::GetLocalizedContentsWidthForFontList(
col_resource_id,
ui::ResourceBundle::GetSharedInstance().GetFontListWithDelta(
ui::kMessageFontSizeDelta));
}
// static
int Widget::GetLocalizedContentsHeight(int row_resource_id) {
return ui::GetLocalizedContentsHeightForFontList(
row_resource_id,
ui::ResourceBundle::GetSharedInstance().GetFontListWithDelta(
ui::kMessageFontSizeDelta));
}
// static
gfx::Size Widget::GetLocalizedContentsSize(int col_resource_id,
int row_resource_id) {
return gfx::Size(GetLocalizedContentsWidth(col_resource_id),
GetLocalizedContentsHeight(row_resource_id));
}
// static
bool Widget::RequiresNonClientView(InitParams::Type type) {
return type == InitParams::TYPE_WINDOW || type == InitParams::TYPE_BUBBLE;
}
void Widget::Init(InitParams params) {
TRACE_EVENT0("views", "Widget::Init");
if (params.name.empty() && params.delegate) {
params.name = params.delegate->internal_name();
// If an internal name was not provided the class name of the contents view
// is a reasonable default.
if (params.name.empty() && params.delegate->GetContentsView())
params.name = params.delegate->GetContentsView()->GetClassName();
}
parent_ = params.parent ? GetWidgetForNativeView(params.parent) : nullptr;
// Subscripbe to parent's paint-as-active change.
if (parent_) {
parent_paint_as_active_subscription_ =
parent_->RegisterPaintAsActiveChangedCallback(
base::BindRepeating(&Widget::OnParentShouldPaintAsActiveChanged,
base::Unretained(this)));
}
params.child |= (params.type == InitParams::TYPE_CONTROL);
is_top_level_ = !params.child;
if (params.opacity == views::Widget::InitParams::WindowOpacity::kInferred &&
params.type != views::Widget::InitParams::TYPE_WINDOW) {
params.opacity = views::Widget::InitParams::WindowOpacity::kOpaque;
}
{
// ViewsDelegate::OnBeforeWidgetInit() may change `params.delegate` either
// by setting it to null or assigning a different value to it, so handle
// both cases.
auto default_widget_delegate = std::make_unique<DefaultWidgetDelegate>();
widget_delegate_ =
params.delegate ? params.delegate : default_widget_delegate.get();
ViewsDelegate::GetInstance()->OnBeforeWidgetInit(¶ms, this);
widget_delegate_ =
params.delegate ? params.delegate : default_widget_delegate.release();
}
DCHECK(widget_delegate_);
if (params.opacity == views::Widget::InitParams::WindowOpacity::kInferred)
params.opacity = views::Widget::InitParams::WindowOpacity::kOpaque;
bool can_activate = params.CanActivate();
params.activatable = can_activate ? InitParams::Activatable::kYes
: InitParams::Activatable::kNo;
widget_delegate_->SetCanActivate(can_activate);
// Henceforth, ensure the delegate outlives the Widget.
widget_delegate_->can_delete_this_ = false;
if (params.delegate)
params.delegate->WidgetInitializing(this);
ownership_ = params.ownership;
native_widget_ = CreateNativeWidget(params, this)->AsNativeWidgetPrivate();
root_view_.reset(CreateRootView());
// Copy the elements of params that will be used after it is moved.
const InitParams::Type type = params.type;
const gfx::Rect bounds = params.bounds;
const ui::WindowShowState show_state = params.show_state;
WidgetDelegate* delegate = params.delegate;
native_widget_->InitNativeWidget(std::move(params));
if (type == InitParams::TYPE_MENU)
is_mouse_button_pressed_ = native_widget_->IsMouseButtonDown();
if (RequiresNonClientView(type)) {
non_client_view_ =
new NonClientView(widget_delegate_->CreateClientView(this));
non_client_view_->SetFrameView(CreateNonClientFrameView());
non_client_view_->SetOverlayView(widget_delegate_->CreateOverlayView());
// Bypass the Layout() that happens in Widget::SetContentsView(). Layout()
// will occur after setting the initial bounds below. The RootView's size is
// not valid until that happens.
root_view_->SetContentsView(non_client_view_);
// Initialize the window's icon and title before setting the window's
// initial bounds; the frame view's preferred height may depend on the
// presence of an icon or a title.
UpdateWindowIcon();
UpdateWindowTitle();
non_client_view_->ResetWindowControls();
SetInitialBounds(bounds);
// Perform the initial layout. This handles the case where the size might
// not actually change when setting the initial bounds. If it did, child
// views won't have a dirty Layout state, so won't do any work.
root_view_->Layout();
if (show_state == ui::SHOW_STATE_MAXIMIZED) {
Maximize();
} else if (show_state == ui::SHOW_STATE_MINIMIZED) {
Minimize();
saved_show_state_ = ui::SHOW_STATE_MINIMIZED;
}
} else if (delegate) {
SetContentsView(delegate->TransferOwnershipOfContentsView());
SetInitialBoundsForFramelessWindow(bounds);
}
native_theme_observation_.Observe(GetNativeTheme());
native_widget_initialized_ = true;
native_widget_->OnWidgetInitDone();
if (delegate)
delegate->WidgetInitialized();
internal::AnyWidgetObserverSingleton::GetInstance()->OnAnyWidgetInitialized(
this);
}
void Widget::ShowEmojiPanel() {
native_widget_->ShowEmojiPanel();
}
// Unconverted methods (see header) --------------------------------------------
gfx::NativeView Widget::GetNativeView() const {
if (native_widget_destroyed_)
return gfx::kNullNativeView;
return native_widget_->GetNativeView();
}
gfx::NativeWindow Widget::GetNativeWindow() const {
if (native_widget_destroyed_)
return gfx::kNullNativeWindow;
return native_widget_->GetNativeWindow();
}
void Widget::AddObserver(WidgetObserver* observer) {
// Make sure that there is no nullptr in observer list. crbug.com/471649.
CHECK(observer);
observers_.AddObserver(observer);
}
void Widget::RemoveObserver(WidgetObserver* observer) {
observers_.RemoveObserver(observer);
}
bool Widget::HasObserver(const WidgetObserver* observer) const {
return observers_.HasObserver(observer);
}
void Widget::AddRemovalsObserver(WidgetRemovalsObserver* observer) {
removals_observers_.AddObserver(observer);
}
void Widget::RemoveRemovalsObserver(WidgetRemovalsObserver* observer) {
removals_observers_.RemoveObserver(observer);
}
bool Widget::HasRemovalsObserver(const WidgetRemovalsObserver* observer) const {
return removals_observers_.HasObserver(observer);
}
bool Widget::GetAccelerator(int cmd_id, ui::Accelerator* accelerator) const {
return false;
}
void Widget::ViewHierarchyChanged(const ViewHierarchyChangedDetails& details) {
if (!details.is_add) {
if (details.child == dragged_view_)
dragged_view_ = nullptr;
FocusManager* focus_manager = GetFocusManager();
if (focus_manager)
focus_manager->ViewRemoved(details.child);
if (!native_widget_destroyed_)
native_widget_->ViewRemoved(details.child);
}
}
void Widget::NotifyNativeViewHierarchyWillChange() {
// During tear-down the top-level focus manager becomes unavailable to
// GTK tabbed panes and their children, so normal deregistration via
// |FocusManager::ViewRemoved()| calls are fouled. We clear focus here
// to avoid these redundant steps and to avoid accessing deleted views
// that may have been in focus.
ClearFocusFromWidget();
native_widget_->OnNativeViewHierarchyWillChange();
}
void Widget::NotifyNativeViewHierarchyChanged() {
native_widget_->OnNativeViewHierarchyChanged();
root_view_->NotifyNativeViewHierarchyChanged();
}
void Widget::NotifyWillRemoveView(View* view) {
for (WidgetRemovalsObserver& observer : removals_observers_)
observer.OnWillRemoveView(this, view);
}
// Converted methods (see header) ----------------------------------------------
Widget* Widget::GetTopLevelWidget() {
return const_cast<Widget*>(
static_cast<const Widget*>(this)->GetTopLevelWidget());
}
const Widget* Widget::GetTopLevelWidget() const {
// GetTopLevelNativeWidget doesn't work during destruction because
// property is gone after gobject gets deleted. Short circuit here
// for toplevel so that InputMethod can remove itself from
// focus manager.
if (is_top_level())
return this;
return native_widget_destroyed_ ? nullptr
: native_widget_->GetTopLevelWidget();
}
Widget* Widget::GetPrimaryWindowWidget() {
return GetTopLevelWidget();
}
const Widget* Widget::GetPrimaryWindowWidget() const {
return const_cast<Widget*>(this)->GetPrimaryWindowWidget();
}
void Widget::SetContentsView(View* view) {
// Do not SetContentsView() again if it is already set to the same view.
if (view == GetContentsView())
return;
// |non_client_view_| can only be non-null here if RequiresNonClientView() was
// true when the widget was initialized. Creating widgets with non-client
// views and then setting the contents view can cause subtle problems on
// Windows, where the native widget thinks there is still a
// |non_client_view_|. If you get this error, either use a different type when
// initializing the widget, or don't call SetContentsView().
DCHECK(!non_client_view_);
root_view_->SetContentsView(view);
// Force a layout now, since the attached hierarchy won't be ready for the
// containing window's bounds. Note that we call Layout directly rather than
// calling the widget's size changed handler, since the RootView's bounds may
// not have changed, which will cause the Layout not to be done otherwise.
root_view_->Layout();
}
View* Widget::GetContentsView() {
return root_view_->GetContentsView();
}
gfx::Rect Widget::GetWindowBoundsInScreen() const {
return native_widget_->GetWindowBoundsInScreen();
}
gfx::Rect Widget::GetClientAreaBoundsInScreen() const {
return native_widget_->GetClientAreaBoundsInScreen();
}
gfx::Rect Widget::GetRestoredBounds() const {
return native_widget_->GetRestoredBounds();
}
std::string Widget::GetWorkspace() const {
return native_widget_->GetWorkspace();
}
void Widget::SetBounds(const gfx::Rect& bounds) {
native_widget_->SetBounds(bounds);
}
void Widget::SetSize(const gfx::Size& size) {
native_widget_->SetSize(size);
}
void Widget::CenterWindow(const gfx::Size& size) {
native_widget_->CenterWindow(size);
}
void Widget::SetBoundsConstrained(const gfx::Rect& bounds) {
native_widget_->SetBoundsConstrained(bounds);
}
void Widget::SetVisibilityChangedAnimationsEnabled(bool value) {
native_widget_->SetVisibilityChangedAnimationsEnabled(value);
}
void Widget::SetVisibilityAnimationDuration(const base::TimeDelta& duration) {
native_widget_->SetVisibilityAnimationDuration(duration);
}
void Widget::SetVisibilityAnimationTransition(VisibilityTransition transition) {
native_widget_->SetVisibilityAnimationTransition(transition);
}
Widget::MoveLoopResult Widget::RunMoveLoop(
const gfx::Vector2d& drag_offset,
MoveLoopSource source,
MoveLoopEscapeBehavior escape_behavior) {
return native_widget_->RunMoveLoop(drag_offset, source, escape_behavior);
}
void Widget::EndMoveLoop() {
native_widget_->EndMoveLoop();
}
void Widget::StackAboveWidget(Widget* widget) {
native_widget_->StackAbove(widget->GetNativeView());
}
void Widget::StackAbove(gfx::NativeView native_view) {
native_widget_->StackAbove(native_view);
}
void Widget::StackAtTop() {
native_widget_->StackAtTop();
}
void Widget::SetShape(std::unique_ptr<ShapeRects> shape) {
native_widget_->SetShape(std::move(shape));
}
void Widget::CloseWithReason(ClosedReason closed_reason) {
if (widget_closed_) {
// It appears we can hit this code path if you close a modal dialog then
// close the last browser before the destructor is hit, which triggers
// invoking Close again.
return;
}
if (block_close_) {
return;
}
if (non_client_view_ && non_client_view_->OnWindowCloseRequested() ==
CloseRequestResult::kCannotClose)
return;
// This is the last chance to cancel closing.
if (widget_delegate_ && !widget_delegate_->OnCloseRequested(closed_reason))
return;
// Cancel widget close on focus lost. This is used in UI Devtools to lock
// bubbles and in some tests where we want to ignore spurious deactivation.
if (closed_reason == ClosedReason::kLostFocus &&
(g_disable_activation_change_handling_ ==
DisableActivationChangeHandlingType::kIgnore ||
g_disable_activation_change_handling_ ==
DisableActivationChangeHandlingType::kIgnoreDeactivationOnly))
return;
// The actions below can cause this function to be called again, so mark
// |this| as closed early. See crbug.com/714334
widget_closed_ = true;
closed_reason_ = closed_reason;
SaveWindowPlacement();
ClearFocusFromWidget();
for (WidgetObserver& observer : observers_)
observer.OnWidgetClosing(this);
internal::AnyWidgetObserverSingleton::GetInstance()->OnAnyWidgetClosing(this);
if (widget_delegate_)
widget_delegate_->WindowWillClose();
native_widget_->Close();
}
void Widget::Close() {
CloseWithReason(ClosedReason::kUnspecified);
}
void Widget::CloseNow() {
for (WidgetObserver& observer : observers_)
observer.OnWidgetClosing(this);
internal::AnyWidgetObserverSingleton::GetInstance()->OnAnyWidgetClosing(this);
native_widget_->CloseNow();
}
bool Widget::IsClosed() const {
return widget_closed_;
}
void Widget::Show() {
const ui::Layer* layer = GetLayer();
TRACE_EVENT1("views", "Widget::Show", "layer",
layer ? layer->name() : "none");
ui::WindowShowState preferred_show_state =
CanActivate() ? ui::SHOW_STATE_NORMAL : ui::SHOW_STATE_INACTIVE;
if (non_client_view_) {
// While initializing, the kiosk mode will go to full screen before the
// widget gets shown. In that case we stay in full screen mode, regardless
// of the |saved_show_state_| member.
if (saved_show_state_ == ui::SHOW_STATE_MAXIMIZED &&
!initial_restored_bounds_.IsEmpty() && !IsFullscreen()) {
native_widget_->Show(ui::SHOW_STATE_MAXIMIZED, initial_restored_bounds_);
} else {
native_widget_->Show(
IsFullscreen() ? ui::SHOW_STATE_FULLSCREEN : saved_show_state_,
gfx::Rect());
}
// |saved_show_state_| only applies the first time the window is shown.
// If we don't reset the value the window may be shown maximized every time
// it is subsequently shown after being hidden.
saved_show_state_ = preferred_show_state;
} else {
native_widget_->Show(preferred_show_state, gfx::Rect());
}
internal::AnyWidgetObserverSingleton::GetInstance()->OnAnyWidgetShown(this);
}
void Widget::Hide() {
native_widget_->Hide();
internal::AnyWidgetObserverSingleton::GetInstance()->OnAnyWidgetHidden(this);
}
void Widget::ShowInactive() {
// If this gets called with saved_show_state_ == ui::SHOW_STATE_MAXIMIZED,
// call SetBounds()with the restored bounds to set the correct size. This
// normally should not happen, but if it does we should avoid showing unsized
// windows.
if (saved_show_state_ == ui::SHOW_STATE_MAXIMIZED &&
!initial_restored_bounds_.IsEmpty()) {
SetBounds(initial_restored_bounds_);
saved_show_state_ = ui::SHOW_STATE_NORMAL;
}
native_widget_->Show(ui::SHOW_STATE_INACTIVE, gfx::Rect());
internal::AnyWidgetObserverSingleton::GetInstance()->OnAnyWidgetShown(this);
}
void Widget::Activate() {
if (CanActivate())
native_widget_->Activate();
}
void Widget::Deactivate() {
native_widget_->Deactivate();
}
bool Widget::IsActive() const {
return native_widget_->IsActive();
}
void Widget::SetZOrderLevel(ui::ZOrderLevel order) {
native_widget_->SetZOrderLevel(order);
}
ui::ZOrderLevel Widget::GetZOrderLevel() const {
return native_widget_->GetZOrderLevel();
}
void Widget::SetVisibleOnAllWorkspaces(bool always_visible) {
native_widget_->SetVisibleOnAllWorkspaces(always_visible);
}
bool Widget::IsVisibleOnAllWorkspaces() const {
return native_widget_->IsVisibleOnAllWorkspaces();
}
void Widget::Maximize() {
native_widget_->Maximize();
}
void Widget::Minimize() {
native_widget_->Minimize();
}
void Widget::Restore() {
native_widget_->Restore();
}
bool Widget::IsMaximized() const {
return native_widget_->IsMaximized();
}
bool Widget::IsMinimized() const {
return native_widget_->IsMinimized();
}
void Widget::SetFullscreen(bool fullscreen, base::TimeDelta delay) {
if (IsFullscreen() == fullscreen)
return;
auto weak_ptr = GetWeakPtr();
native_widget_->SetFullscreen(fullscreen, delay);
if (!weak_ptr)
return;
if (non_client_view_)
non_client_view_->InvalidateLayout();
}
bool Widget::IsFullscreen() const {
return native_widget_->IsFullscreen();
}
void Widget::SetCanAppearInExistingFullscreenSpaces(
bool can_appear_in_existing_fullscreen_spaces) {
native_widget_->SetCanAppearInExistingFullscreenSpaces(
can_appear_in_existing_fullscreen_spaces);
}
void Widget::SetOpacity(float opacity) {
DCHECK(opacity >= 0.0f);
DCHECK(opacity <= 1.0f);
native_widget_->SetOpacity(opacity);
}
void Widget::SetAspectRatio(const gfx::SizeF& aspect_ratio) {
native_widget_->SetAspectRatio(aspect_ratio);
}
void Widget::FlashFrame(bool flash) {
native_widget_->FlashFrame(flash);
}
View* Widget::GetRootView() {
return root_view_.get();
}
const View* Widget::GetRootView() const {
return root_view_.get();
}
bool Widget::IsVisible() const {
return native_widget_->IsVisible();
}
const ui::ThemeProvider* Widget::GetThemeProvider() const {
const Widget* root_widget = GetTopLevelWidget();
return (root_widget && root_widget != this) ? root_widget->GetThemeProvider()
: nullptr;
}
ui::ColorProviderManager::InitializerSupplier* Widget::GetCustomTheme() const {
return nullptr;
}
FocusManager* Widget::GetFocusManager() {
Widget* toplevel_widget = GetTopLevelWidget();
return toplevel_widget ? toplevel_widget->focus_manager_.get() : nullptr;
}
const FocusManager* Widget::GetFocusManager() const {
const Widget* toplevel_widget = GetTopLevelWidget();
return toplevel_widget ? toplevel_widget->focus_manager_.get() : nullptr;
}
ui::InputMethod* Widget::GetInputMethod() {
if (is_top_level()) {
// Only creates the shared the input method instance on top level widget.
return native_widget_private()->GetInputMethod();
} else {
Widget* toplevel = GetTopLevelWidget();
// If GetTopLevelWidget() returns itself which is not toplevel,
// the widget is detached from toplevel widget.
// TODO(oshima): Fix GetTopLevelWidget() to return NULL
// if there is no toplevel. We probably need to add GetTopMostWidget()
// to replace some use cases.
return (toplevel && toplevel != this) ? toplevel->GetInputMethod()
: nullptr;
}
}
void Widget::RunShellDrag(View* view,
std::unique_ptr<ui::OSExchangeData> data,
const gfx::Point& location,
int operation,
ui::mojom::DragEventSource source) {
dragged_view_ = view;
OnDragWillStart();
for (WidgetObserver& observer : observers_)
observer.OnWidgetDragWillStart(this);
WidgetDeletionObserver widget_deletion_observer(this);
native_widget_->RunShellDrag(view, std::move(data), location, operation,
source);
// The widget may be destroyed during the drag operation.
if (!widget_deletion_observer.IsWidgetAlive())
return;
// If the view is removed during the drag operation, dragged_view_ is set to
// NULL.
if (view && dragged_view_ == view) {
dragged_view_ = nullptr;
view->OnDragDone();
}
OnDragComplete();
for (WidgetObserver& observer : observers_)
observer.OnWidgetDragComplete(this);
}
void Widget::SchedulePaintInRect(const gfx::Rect& rect) {
// This happens when DestroyRootView removes all children from the
// RootView which triggers a SchedulePaint that ends up here. This happens
// after in ~Widget after native_widget_ is destroyed.
if (native_widget_destroyed_)
return;
native_widget_->SchedulePaintInRect(rect);
}
void Widget::ScheduleLayout() {
native_widget_->ScheduleLayout();
}
void Widget::SetCursor(gfx::NativeCursor cursor) {
native_widget_->SetCursor(cursor);
}
bool Widget::IsMouseEventsEnabled() const {
return native_widget_->IsMouseEventsEnabled();
}
void Widget::SetNativeWindowProperty(const char* name, void* value) {
native_widget_->SetNativeWindowProperty(name, value);
}
void* Widget::GetNativeWindowProperty(const char* name) const {
return native_widget_->GetNativeWindowProperty(name);
}
void Widget::UpdateWindowTitle() {
if (!non_client_view_)
return;
// Update the native frame's text. We do this regardless of whether or not
// the native frame is being used, since this also updates the taskbar, etc.
std::u16string window_title = widget_delegate_->GetWindowTitle();
base::i18n::AdjustStringForLocaleDirection(&window_title);
if (!native_widget_->SetWindowTitle(window_title))
return;
non_client_view_->UpdateWindowTitle();
}
void Widget::UpdateWindowIcon() {
if (non_client_view_)
non_client_view_->UpdateWindowIcon();
gfx::ImageSkia window_icon = GetImageSkiaFromImageModel(
widget_delegate_->GetWindowIcon(), GetColorProvider());
// In general, icon information is read from a |widget_delegate_| and then
// passed to |native_widget_|. On ChromeOS, for lacros-chrome to support the
// initial window state as minimized state, a valid icon is added to
// |native_widget_| earlier stage of widget initialization. See
// https://crbug.com/1189981. As only lacros-chrome on ChromeOS supports this
// behavior other overrides of |native_widget_| will always have no icon
// information. This is also true for |app_icon| referred below.
if (window_icon.isNull()) {
const gfx::ImageSkia* icon = native_widget_->GetWindowIcon();
if (icon && !icon->isNull())
window_icon = *icon;
}
gfx::ImageSkia app_icon = GetImageSkiaFromImageModel(
widget_delegate_->GetWindowAppIcon(), GetColorProvider());
if (app_icon.isNull()) {
const gfx::ImageSkia* icon = native_widget_->GetWindowAppIcon();
if (icon && !icon->isNull())