Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / views / widget / widget.cc
blob154d107a63b298fbb7f6d0eb764d924ecc1a734b
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/views/widget/widget.h"
7 #include "base/logging.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/trace_event/trace_event.h"
11 #include "ui/base/cursor/cursor.h"
12 #include "ui/base/default_theme_provider.h"
13 #include "ui/base/hit_test.h"
14 #include "ui/base/ime/input_method.h"
15 #include "ui/base/l10n/l10n_font_util.h"
16 #include "ui/base/resource/resource_bundle.h"
17 #include "ui/compositor/compositor.h"
18 #include "ui/compositor/layer.h"
19 #include "ui/events/event.h"
20 #include "ui/events/event_utils.h"
21 #include "ui/gfx/image/image_skia.h"
22 #include "ui/gfx/screen.h"
23 #include "ui/views/controls/menu/menu_controller.h"
24 #include "ui/views/event_monitor.h"
25 #include "ui/views/focus/focus_manager.h"
26 #include "ui/views/focus/focus_manager_factory.h"
27 #include "ui/views/focus/view_storage.h"
28 #include "ui/views/focus/widget_focus_manager.h"
29 #include "ui/views/views_delegate.h"
30 #include "ui/views/widget/native_widget_private.h"
31 #include "ui/views/widget/root_view.h"
32 #include "ui/views/widget/tooltip_manager.h"
33 #include "ui/views/widget/widget_delegate.h"
34 #include "ui/views/widget/widget_deletion_observer.h"
35 #include "ui/views/widget/widget_observer.h"
36 #include "ui/views/widget/widget_removals_observer.h"
37 #include "ui/views/window/custom_frame_view.h"
38 #include "ui/views/window/dialog_delegate.h"
40 namespace views {
42 namespace {
44 // If |view| has a layer the layer is added to |layers|. Else this recurses
45 // through the children. This is used to build a list of the layers created by
46 // views that are direct children of the Widgets layer.
47 void BuildRootLayers(View* view, std::vector<ui::Layer*>* layers) {
48 if (view->layer()) {
49 layers->push_back(view->layer());
50 } else {
51 for (int i = 0; i < view->child_count(); ++i)
52 BuildRootLayers(view->child_at(i), layers);
56 // Create a native widget implementation.
57 // First, use the supplied one if non-NULL.
58 // Finally, make a default one.
59 NativeWidget* CreateNativeWidget(NativeWidget* native_widget,
60 internal::NativeWidgetDelegate* delegate) {
61 if (!native_widget) {
62 native_widget =
63 internal::NativeWidgetPrivate::CreateNativeWidget(delegate);
65 return native_widget;
68 void NotifyCaretBoundsChanged(ui::InputMethod* input_method) {
69 if (!input_method)
70 return;
71 ui::TextInputClient* client = input_method->GetTextInputClient();
72 if (client)
73 input_method->OnCaretBoundsChanged(client);
76 } // namespace
78 // A default implementation of WidgetDelegate, used by Widget when no
79 // WidgetDelegate is supplied.
80 class DefaultWidgetDelegate : public WidgetDelegate {
81 public:
82 explicit DefaultWidgetDelegate(Widget* widget) : widget_(widget) {
84 ~DefaultWidgetDelegate() override {}
86 // Overridden from WidgetDelegate:
87 void DeleteDelegate() override { delete this; }
88 Widget* GetWidget() override { return widget_; }
89 const Widget* GetWidget() const override { return widget_; }
90 bool ShouldAdvanceFocusToTopLevelWidget() const override {
91 // In most situations where a Widget is used without a delegate the Widget
92 // is used as a container, so that we want focus to advance to the top-level
93 // widget. A good example of this is the find bar.
94 return true;
97 private:
98 Widget* widget_;
100 DISALLOW_COPY_AND_ASSIGN(DefaultWidgetDelegate);
103 ////////////////////////////////////////////////////////////////////////////////
104 // Widget, InitParams:
106 Widget::InitParams::InitParams()
107 : type(TYPE_WINDOW),
108 delegate(NULL),
109 child(false),
110 opacity(INFER_OPACITY),
111 accept_events(true),
112 activatable(ACTIVATABLE_DEFAULT),
113 keep_on_top(false),
114 visible_on_all_workspaces(false),
115 ownership(NATIVE_WIDGET_OWNS_WIDGET),
116 mirror_origin_in_rtl(false),
117 shadow_type(SHADOW_TYPE_DEFAULT),
118 remove_standard_frame(false),
119 use_system_default_icon(false),
120 show_state(ui::SHOW_STATE_DEFAULT),
121 parent(NULL),
122 native_widget(NULL),
123 desktop_window_tree_host(NULL),
124 layer_type(ui::LAYER_TEXTURED),
125 context(NULL),
126 force_show_in_taskbar(false) {
129 Widget::InitParams::InitParams(Type type)
130 : type(type),
131 delegate(NULL),
132 child(false),
133 opacity(INFER_OPACITY),
134 accept_events(true),
135 activatable(ACTIVATABLE_DEFAULT),
136 keep_on_top(type == TYPE_MENU || type == TYPE_DRAG),
137 visible_on_all_workspaces(false),
138 ownership(NATIVE_WIDGET_OWNS_WIDGET),
139 mirror_origin_in_rtl(false),
140 shadow_type(SHADOW_TYPE_DEFAULT),
141 remove_standard_frame(false),
142 use_system_default_icon(false),
143 show_state(ui::SHOW_STATE_DEFAULT),
144 parent(NULL),
145 native_widget(NULL),
146 desktop_window_tree_host(NULL),
147 layer_type(ui::LAYER_TEXTURED),
148 context(NULL),
149 force_show_in_taskbar(false) {
152 Widget::InitParams::~InitParams() {
155 ////////////////////////////////////////////////////////////////////////////////
156 // Widget, public:
158 Widget::Widget()
159 : native_widget_(NULL),
160 widget_delegate_(NULL),
161 non_client_view_(NULL),
162 dragged_view_(NULL),
163 ownership_(InitParams::NATIVE_WIDGET_OWNS_WIDGET),
164 is_secondary_widget_(true),
165 frame_type_(FRAME_TYPE_DEFAULT),
166 disable_inactive_rendering_(false),
167 widget_closed_(false),
168 saved_show_state_(ui::SHOW_STATE_DEFAULT),
169 focus_on_creation_(true),
170 is_top_level_(false),
171 native_widget_initialized_(false),
172 native_widget_destroyed_(false),
173 is_mouse_button_pressed_(false),
174 ignore_capture_loss_(false),
175 last_mouse_event_was_move_(false),
176 auto_release_capture_(true),
177 root_layers_dirty_(false),
178 movement_disabled_(false),
179 observer_manager_(this) {
182 Widget::~Widget() {
183 DestroyRootView();
184 if (ownership_ == InitParams::WIDGET_OWNS_NATIVE_WIDGET) {
185 delete native_widget_;
186 } else {
187 DCHECK(native_widget_destroyed_)
188 << "Destroying a widget with a live native widget. "
189 << "Widget probably should use WIDGET_OWNS_NATIVE_WIDGET ownership.";
193 // static
194 Widget* Widget::CreateWindow(WidgetDelegate* delegate) {
195 return CreateWindowWithBounds(delegate, gfx::Rect());
198 // static
199 Widget* Widget::CreateWindowWithBounds(WidgetDelegate* delegate,
200 const gfx::Rect& bounds) {
201 Widget* widget = new Widget;
202 Widget::InitParams params;
203 params.bounds = bounds;
204 params.delegate = delegate;
205 widget->Init(params);
206 return widget;
209 // static
210 Widget* Widget::CreateWindowWithParent(WidgetDelegate* delegate,
211 gfx::NativeView parent) {
212 return CreateWindowWithParentAndBounds(delegate, parent, gfx::Rect());
215 // static
216 Widget* Widget::CreateWindowWithParentAndBounds(WidgetDelegate* delegate,
217 gfx::NativeView parent,
218 const gfx::Rect& bounds) {
219 Widget* widget = new Widget;
220 Widget::InitParams params;
221 params.delegate = delegate;
222 params.parent = parent;
223 params.bounds = bounds;
224 widget->Init(params);
225 return widget;
228 // static
229 Widget* Widget::CreateWindowWithContext(WidgetDelegate* delegate,
230 gfx::NativeWindow context) {
231 return CreateWindowWithContextAndBounds(delegate, context, gfx::Rect());
234 // static
235 Widget* Widget::CreateWindowWithContextAndBounds(WidgetDelegate* delegate,
236 gfx::NativeWindow context,
237 const gfx::Rect& bounds) {
238 Widget* widget = new Widget;
239 Widget::InitParams params;
240 params.delegate = delegate;
241 params.context = context;
242 params.bounds = bounds;
243 widget->Init(params);
244 return widget;
247 // static
248 Widget* Widget::GetWidgetForNativeView(gfx::NativeView native_view) {
249 internal::NativeWidgetPrivate* native_widget =
250 internal::NativeWidgetPrivate::GetNativeWidgetForNativeView(native_view);
251 return native_widget ? native_widget->GetWidget() : NULL;
254 // static
255 Widget* Widget::GetWidgetForNativeWindow(gfx::NativeWindow native_window) {
256 internal::NativeWidgetPrivate* native_widget =
257 internal::NativeWidgetPrivate::GetNativeWidgetForNativeWindow(
258 native_window);
259 return native_widget ? native_widget->GetWidget() : NULL;
262 // static
263 Widget* Widget::GetTopLevelWidgetForNativeView(gfx::NativeView native_view) {
264 internal::NativeWidgetPrivate* native_widget =
265 internal::NativeWidgetPrivate::GetTopLevelNativeWidget(native_view);
266 return native_widget ? native_widget->GetWidget() : NULL;
270 // static
271 void Widget::GetAllChildWidgets(gfx::NativeView native_view,
272 Widgets* children) {
273 internal::NativeWidgetPrivate::GetAllChildWidgets(native_view, children);
276 // static
277 void Widget::GetAllOwnedWidgets(gfx::NativeView native_view,
278 Widgets* owned) {
279 internal::NativeWidgetPrivate::GetAllOwnedWidgets(native_view, owned);
282 // static
283 void Widget::ReparentNativeView(gfx::NativeView native_view,
284 gfx::NativeView new_parent) {
285 internal::NativeWidgetPrivate::ReparentNativeView(native_view, new_parent);
288 // static
289 int Widget::GetLocalizedContentsWidth(int col_resource_id) {
290 return ui::GetLocalizedContentsWidthForFont(col_resource_id,
291 ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont));
294 // static
295 int Widget::GetLocalizedContentsHeight(int row_resource_id) {
296 return ui::GetLocalizedContentsHeightForFont(row_resource_id,
297 ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont));
300 // static
301 gfx::Size Widget::GetLocalizedContentsSize(int col_resource_id,
302 int row_resource_id) {
303 return gfx::Size(GetLocalizedContentsWidth(col_resource_id),
304 GetLocalizedContentsHeight(row_resource_id));
307 // static
308 bool Widget::RequiresNonClientView(InitParams::Type type) {
309 return type == InitParams::TYPE_WINDOW ||
310 type == InitParams::TYPE_PANEL ||
311 type == InitParams::TYPE_BUBBLE;
314 void Widget::Init(const InitParams& in_params) {
315 TRACE_EVENT0("views", "Widget::Init");
316 InitParams params = in_params;
318 params.child |= (params.type == InitParams::TYPE_CONTROL);
319 is_top_level_ = !params.child;
321 if (params.opacity == views::Widget::InitParams::INFER_OPACITY &&
322 params.type != views::Widget::InitParams::TYPE_WINDOW &&
323 params.type != views::Widget::InitParams::TYPE_PANEL)
324 params.opacity = views::Widget::InitParams::OPAQUE_WINDOW;
326 if (ViewsDelegate::GetInstance())
327 ViewsDelegate::GetInstance()->OnBeforeWidgetInit(&params, this);
329 if (params.opacity == views::Widget::InitParams::INFER_OPACITY)
330 params.opacity = views::Widget::InitParams::OPAQUE_WINDOW;
332 bool can_activate = false;
333 if (params.activatable != InitParams::ACTIVATABLE_DEFAULT) {
334 can_activate = (params.activatable == InitParams::ACTIVATABLE_YES);
335 } else if (params.type != InitParams::TYPE_CONTROL &&
336 params.type != InitParams::TYPE_POPUP &&
337 params.type != InitParams::TYPE_MENU &&
338 params.type != InitParams::TYPE_TOOLTIP &&
339 params.type != InitParams::TYPE_DRAG) {
340 can_activate = true;
341 params.activatable = InitParams::ACTIVATABLE_YES;
342 } else {
343 can_activate = false;
344 params.activatable = InitParams::ACTIVATABLE_NO;
347 widget_delegate_ = params.delegate ?
348 params.delegate : new DefaultWidgetDelegate(this);
349 widget_delegate_->set_can_activate(can_activate);
351 ownership_ = params.ownership;
352 native_widget_ = CreateNativeWidget(params.native_widget, this)->
353 AsNativeWidgetPrivate();
354 root_view_.reset(CreateRootView());
355 default_theme_provider_.reset(new ui::DefaultThemeProvider);
356 if (params.type == InitParams::TYPE_MENU) {
357 is_mouse_button_pressed_ =
358 internal::NativeWidgetPrivate::IsMouseButtonDown();
360 native_widget_->InitNativeWidget(params);
361 if (RequiresNonClientView(params.type)) {
362 non_client_view_ = new NonClientView;
363 non_client_view_->SetFrameView(CreateNonClientFrameView());
364 // Create the ClientView, add it to the NonClientView and add the
365 // NonClientView to the RootView. This will cause everything to be parented.
366 non_client_view_->set_client_view(widget_delegate_->CreateClientView(this));
367 non_client_view_->SetOverlayView(widget_delegate_->CreateOverlayView());
368 SetContentsView(non_client_view_);
369 // Initialize the window's icon and title before setting the window's
370 // initial bounds; the frame view's preferred height may depend on the
371 // presence of an icon or a title.
372 UpdateWindowIcon();
373 UpdateWindowTitle();
374 non_client_view_->ResetWindowControls();
375 SetInitialBounds(params.bounds);
376 if (params.show_state == ui::SHOW_STATE_MAXIMIZED)
377 Maximize();
378 else if (params.show_state == ui::SHOW_STATE_MINIMIZED)
379 Minimize();
380 } else if (params.delegate) {
381 SetContentsView(params.delegate->GetContentsView());
382 SetInitialBoundsForFramelessWindow(params.bounds);
384 // This must come after SetContentsView() or it might not be able to find
385 // the correct NativeTheme (on Linux). See http://crbug.com/384492
386 observer_manager_.Add(GetNativeTheme());
387 native_widget_initialized_ = true;
390 // Unconverted methods (see header) --------------------------------------------
392 gfx::NativeView Widget::GetNativeView() const {
393 return native_widget_->GetNativeView();
396 gfx::NativeWindow Widget::GetNativeWindow() const {
397 return native_widget_->GetNativeWindow();
400 void Widget::AddObserver(WidgetObserver* observer) {
401 // Make sure that there is no nullptr in observer list. crbug.com/471649.
402 CHECK(observer);
403 observers_.AddObserver(observer);
406 void Widget::RemoveObserver(WidgetObserver* observer) {
407 observers_.RemoveObserver(observer);
410 bool Widget::HasObserver(const WidgetObserver* observer) const {
411 return observers_.HasObserver(observer);
414 void Widget::AddRemovalsObserver(WidgetRemovalsObserver* observer) {
415 removals_observers_.AddObserver(observer);
418 void Widget::RemoveRemovalsObserver(WidgetRemovalsObserver* observer) {
419 removals_observers_.RemoveObserver(observer);
422 bool Widget::HasRemovalsObserver(const WidgetRemovalsObserver* observer) const {
423 return removals_observers_.HasObserver(observer);
426 bool Widget::GetAccelerator(int cmd_id, ui::Accelerator* accelerator) const {
427 return false;
430 void Widget::ViewHierarchyChanged(
431 const View::ViewHierarchyChangedDetails& details) {
432 if (!details.is_add) {
433 if (details.child == dragged_view_)
434 dragged_view_ = NULL;
435 FocusManager* focus_manager = GetFocusManager();
436 if (focus_manager)
437 focus_manager->ViewRemoved(details.child);
438 ViewStorage::GetInstance()->ViewRemoved(details.child);
439 native_widget_->ViewRemoved(details.child);
443 void Widget::NotifyNativeViewHierarchyWillChange() {
444 FocusManager* focus_manager = GetFocusManager();
445 // We are being removed from a window hierarchy. Treat this as
446 // the root_view_ being removed.
447 if (focus_manager)
448 focus_manager->ViewRemoved(root_view_.get());
451 void Widget::NotifyNativeViewHierarchyChanged() {
452 root_view_->NotifyNativeViewHierarchyChanged();
455 void Widget::NotifyWillRemoveView(View* view) {
456 FOR_EACH_OBSERVER(WidgetRemovalsObserver,
457 removals_observers_,
458 OnWillRemoveView(this, view));
461 // Converted methods (see header) ----------------------------------------------
463 Widget* Widget::GetTopLevelWidget() {
464 return const_cast<Widget*>(
465 static_cast<const Widget*>(this)->GetTopLevelWidget());
468 const Widget* Widget::GetTopLevelWidget() const {
469 // GetTopLevelNativeWidget doesn't work during destruction because
470 // property is gone after gobject gets deleted. Short circuit here
471 // for toplevel so that InputMethod can remove itself from
472 // focus manager.
473 return is_top_level() ? this : native_widget_->GetTopLevelWidget();
476 void Widget::SetContentsView(View* view) {
477 // Do not SetContentsView() again if it is already set to the same view.
478 if (view == GetContentsView())
479 return;
480 root_view_->SetContentsView(view);
481 if (non_client_view_ != view) {
482 // |non_client_view_| can only be non-NULL here if RequiresNonClientView()
483 // was true when the widget was initialized. Creating widgets with non
484 // client views and then setting the contents view can cause subtle
485 // problems on Windows, where the native widget thinks there is still a
486 // |non_client_view_|. If you get this error, either use a different type
487 // when initializing the widget, or don't call SetContentsView().
488 DCHECK(!non_client_view_);
489 non_client_view_ = NULL;
493 View* Widget::GetContentsView() {
494 return root_view_->GetContentsView();
497 gfx::Rect Widget::GetWindowBoundsInScreen() const {
498 return native_widget_->GetWindowBoundsInScreen();
501 gfx::Rect Widget::GetClientAreaBoundsInScreen() const {
502 return native_widget_->GetClientAreaBoundsInScreen();
505 gfx::Rect Widget::GetRestoredBounds() const {
506 return native_widget_->GetRestoredBounds();
509 void Widget::SetBounds(const gfx::Rect& bounds) {
510 native_widget_->SetBounds(bounds);
513 void Widget::SetSize(const gfx::Size& size) {
514 native_widget_->SetSize(size);
517 void Widget::CenterWindow(const gfx::Size& size) {
518 native_widget_->CenterWindow(size);
521 void Widget::SetBoundsConstrained(const gfx::Rect& bounds) {
522 gfx::Rect work_area =
523 gfx::Screen::GetScreenFor(GetNativeView())->GetDisplayNearestPoint(
524 bounds.origin()).work_area();
525 if (work_area.IsEmpty()) {
526 SetBounds(bounds);
527 } else {
528 // Inset the work area slightly.
529 work_area.Inset(10, 10, 10, 10);
530 work_area.AdjustToFit(bounds);
531 SetBounds(work_area);
535 void Widget::SetVisibilityChangedAnimationsEnabled(bool value) {
536 native_widget_->SetVisibilityChangedAnimationsEnabled(value);
539 void Widget::SetVisibilityAnimationDuration(const base::TimeDelta& duration) {
540 native_widget_->SetVisibilityAnimationDuration(duration);
543 void Widget::SetVisibilityAnimationTransition(VisibilityTransition transition) {
544 native_widget_->SetVisibilityAnimationTransition(transition);
547 Widget::MoveLoopResult Widget::RunMoveLoop(
548 const gfx::Vector2d& drag_offset,
549 MoveLoopSource source,
550 MoveLoopEscapeBehavior escape_behavior) {
551 return native_widget_->RunMoveLoop(drag_offset, source, escape_behavior);
554 void Widget::EndMoveLoop() {
555 native_widget_->EndMoveLoop();
558 void Widget::StackAboveWidget(Widget* widget) {
559 native_widget_->StackAbove(widget->GetNativeView());
562 void Widget::StackAbove(gfx::NativeView native_view) {
563 native_widget_->StackAbove(native_view);
566 void Widget::StackAtTop() {
567 native_widget_->StackAtTop();
570 void Widget::StackBelow(gfx::NativeView native_view) {
571 native_widget_->StackBelow(native_view);
574 void Widget::SetShape(SkRegion* shape) {
575 native_widget_->SetShape(shape);
578 void Widget::Close() {
579 if (widget_closed_) {
580 // It appears we can hit this code path if you close a modal dialog then
581 // close the last browser before the destructor is hit, which triggers
582 // invoking Close again.
583 return;
586 bool can_close = true;
587 if (non_client_view_)
588 can_close = non_client_view_->CanClose();
589 if (can_close) {
590 SaveWindowPlacement();
592 // During tear-down the top-level focus manager becomes unavailable to
593 // GTK tabbed panes and their children, so normal deregistration via
594 // |FormManager::ViewRemoved()| calls are fouled. We clear focus here
595 // to avoid these redundant steps and to avoid accessing deleted views
596 // that may have been in focus.
597 if (is_top_level() && focus_manager_.get())
598 focus_manager_->SetFocusedView(NULL);
600 FOR_EACH_OBSERVER(WidgetObserver, observers_, OnWidgetClosing(this));
601 native_widget_->Close();
602 widget_closed_ = true;
606 void Widget::CloseNow() {
607 FOR_EACH_OBSERVER(WidgetObserver, observers_, OnWidgetClosing(this));
608 native_widget_->CloseNow();
611 bool Widget::IsClosed() const {
612 return widget_closed_;
615 void Widget::Show() {
616 const ui::Layer* layer = GetLayer();
617 TRACE_EVENT1("views", "Widget::Show", "layer",
618 layer ? layer->name() : "none");
619 if (non_client_view_) {
620 // While initializing, the kiosk mode will go to full screen before the
621 // widget gets shown. In that case we stay in full screen mode, regardless
622 // of the |saved_show_state_| member.
623 if (saved_show_state_ == ui::SHOW_STATE_MAXIMIZED &&
624 !initial_restored_bounds_.IsEmpty() &&
625 !IsFullscreen()) {
626 native_widget_->ShowMaximizedWithBounds(initial_restored_bounds_);
627 } else {
628 native_widget_->ShowWithWindowState(
629 IsFullscreen() ? ui::SHOW_STATE_FULLSCREEN : saved_show_state_);
631 // |saved_show_state_| only applies the first time the window is shown.
632 // If we don't reset the value the window may be shown maximized every time
633 // it is subsequently shown after being hidden.
634 saved_show_state_ = ui::SHOW_STATE_NORMAL;
635 } else {
636 CanActivate()
637 ? native_widget_->Show()
638 : native_widget_->ShowWithWindowState(ui::SHOW_STATE_INACTIVE);
642 void Widget::Hide() {
643 native_widget_->Hide();
646 void Widget::ShowInactive() {
647 // If this gets called with saved_show_state_ == ui::SHOW_STATE_MAXIMIZED,
648 // call SetBounds()with the restored bounds to set the correct size. This
649 // normally should not happen, but if it does we should avoid showing unsized
650 // windows.
651 if (saved_show_state_ == ui::SHOW_STATE_MAXIMIZED &&
652 !initial_restored_bounds_.IsEmpty()) {
653 SetBounds(initial_restored_bounds_);
654 saved_show_state_ = ui::SHOW_STATE_NORMAL;
656 native_widget_->ShowWithWindowState(ui::SHOW_STATE_INACTIVE);
659 void Widget::Activate() {
660 native_widget_->Activate();
663 void Widget::Deactivate() {
664 native_widget_->Deactivate();
667 bool Widget::IsActive() const {
668 return native_widget_->IsActive();
671 void Widget::DisableInactiveRendering() {
672 SetInactiveRenderingDisabled(true);
675 void Widget::SetAlwaysOnTop(bool on_top) {
676 native_widget_->SetAlwaysOnTop(on_top);
679 bool Widget::IsAlwaysOnTop() const {
680 return native_widget_->IsAlwaysOnTop();
683 void Widget::SetVisibleOnAllWorkspaces(bool always_visible) {
684 native_widget_->SetVisibleOnAllWorkspaces(always_visible);
687 void Widget::Maximize() {
688 native_widget_->Maximize();
691 void Widget::Minimize() {
692 native_widget_->Minimize();
695 void Widget::Restore() {
696 native_widget_->Restore();
699 bool Widget::IsMaximized() const {
700 return native_widget_->IsMaximized();
703 bool Widget::IsMinimized() const {
704 return native_widget_->IsMinimized();
707 void Widget::SetFullscreen(bool fullscreen) {
708 if (IsFullscreen() == fullscreen)
709 return;
711 native_widget_->SetFullscreen(fullscreen);
713 if (non_client_view_)
714 non_client_view_->Layout();
717 bool Widget::IsFullscreen() const {
718 return native_widget_->IsFullscreen();
721 void Widget::SetOpacity(unsigned char opacity) {
722 native_widget_->SetOpacity(opacity);
725 void Widget::SetUseDragFrame(bool use_drag_frame) {
726 native_widget_->SetUseDragFrame(use_drag_frame);
729 void Widget::FlashFrame(bool flash) {
730 native_widget_->FlashFrame(flash);
733 View* Widget::GetRootView() {
734 return root_view_.get();
737 const View* Widget::GetRootView() const {
738 return root_view_.get();
741 bool Widget::IsVisible() const {
742 return native_widget_->IsVisible();
745 ui::ThemeProvider* Widget::GetThemeProvider() const {
746 const Widget* root_widget = GetTopLevelWidget();
747 if (root_widget && root_widget != this) {
748 // Attempt to get the theme provider, and fall back to the default theme
749 // provider if not found.
750 ui::ThemeProvider* provider = root_widget->GetThemeProvider();
751 if (provider)
752 return provider;
754 provider = root_widget->default_theme_provider_.get();
755 if (provider)
756 return provider;
758 return default_theme_provider_.get();
761 const ui::NativeTheme* Widget::GetNativeTheme() const {
762 return native_widget_->GetNativeTheme();
765 FocusManager* Widget::GetFocusManager() {
766 Widget* toplevel_widget = GetTopLevelWidget();
767 return toplevel_widget ? toplevel_widget->focus_manager_.get() : NULL;
770 const FocusManager* Widget::GetFocusManager() const {
771 const Widget* toplevel_widget = GetTopLevelWidget();
772 return toplevel_widget ? toplevel_widget->focus_manager_.get() : NULL;
775 ui::InputMethod* Widget::GetInputMethod() {
776 if (is_top_level()) {
777 // Only creates the shared the input method instance on top level widget.
778 return native_widget_private()->GetInputMethod();
779 } else {
780 Widget* toplevel = GetTopLevelWidget();
781 // If GetTopLevelWidget() returns itself which is not toplevel,
782 // the widget is detached from toplevel widget.
783 // TODO(oshima): Fix GetTopLevelWidget() to return NULL
784 // if there is no toplevel. We probably need to add GetTopMostWidget()
785 // to replace some use cases.
786 return (toplevel && toplevel != this) ? toplevel->GetInputMethod()
787 : nullptr;
791 void Widget::RunShellDrag(View* view,
792 const ui::OSExchangeData& data,
793 const gfx::Point& location,
794 int operation,
795 ui::DragDropTypes::DragEventSource source) {
796 dragged_view_ = view;
797 OnDragWillStart();
799 WidgetDeletionObserver widget_deletion_observer(this);
800 native_widget_->RunShellDrag(view, data, location, operation, source);
802 // The widget may be destroyed during the drag operation.
803 if (!widget_deletion_observer.IsWidgetAlive())
804 return;
806 // If the view is removed during the drag operation, dragged_view_ is set to
807 // NULL.
808 if (view && dragged_view_ == view) {
809 dragged_view_ = NULL;
810 view->OnDragDone();
812 OnDragComplete();
815 void Widget::SchedulePaintInRect(const gfx::Rect& rect) {
816 native_widget_->SchedulePaintInRect(rect);
819 void Widget::SetCursor(gfx::NativeCursor cursor) {
820 native_widget_->SetCursor(cursor);
823 bool Widget::IsMouseEventsEnabled() const {
824 return native_widget_->IsMouseEventsEnabled();
827 void Widget::SetNativeWindowProperty(const char* name, void* value) {
828 native_widget_->SetNativeWindowProperty(name, value);
831 void* Widget::GetNativeWindowProperty(const char* name) const {
832 return native_widget_->GetNativeWindowProperty(name);
835 void Widget::UpdateWindowTitle() {
836 if (!non_client_view_)
837 return;
839 // Update the native frame's text. We do this regardless of whether or not
840 // the native frame is being used, since this also updates the taskbar, etc.
841 base::string16 window_title = widget_delegate_->GetWindowTitle();
842 base::i18n::AdjustStringForLocaleDirection(&window_title);
843 if (!native_widget_->SetWindowTitle(window_title))
844 return;
845 non_client_view_->UpdateWindowTitle();
847 // If the non-client view is rendering its own title, it'll need to relayout
848 // now and to get a paint update later on.
849 non_client_view_->Layout();
852 void Widget::UpdateWindowIcon() {
853 if (non_client_view_)
854 non_client_view_->UpdateWindowIcon();
855 native_widget_->SetWindowIcons(widget_delegate_->GetWindowIcon(),
856 widget_delegate_->GetWindowAppIcon());
859 FocusTraversable* Widget::GetFocusTraversable() {
860 return static_cast<internal::RootView*>(root_view_.get());
863 void Widget::ThemeChanged() {
864 root_view_->ThemeChanged();
867 void Widget::LocaleChanged() {
868 root_view_->LocaleChanged();
871 void Widget::DeviceScaleFactorChanged(float device_scale_factor) {
872 root_view_->DeviceScaleFactorChanged(device_scale_factor);
875 void Widget::SetFocusTraversableParent(FocusTraversable* parent) {
876 root_view_->SetFocusTraversableParent(parent);
879 void Widget::SetFocusTraversableParentView(View* parent_view) {
880 root_view_->SetFocusTraversableParentView(parent_view);
883 void Widget::ClearNativeFocus() {
884 native_widget_->ClearNativeFocus();
887 NonClientFrameView* Widget::CreateNonClientFrameView() {
888 NonClientFrameView* frame_view =
889 widget_delegate_->CreateNonClientFrameView(this);
890 if (!frame_view)
891 frame_view = native_widget_->CreateNonClientFrameView();
892 if (!frame_view && ViewsDelegate::GetInstance()) {
893 frame_view =
894 ViewsDelegate::GetInstance()->CreateDefaultNonClientFrameView(this);
896 if (frame_view)
897 return frame_view;
899 CustomFrameView* custom_frame_view = new CustomFrameView;
900 custom_frame_view->Init(this);
901 return custom_frame_view;
904 bool Widget::ShouldUseNativeFrame() const {
905 if (frame_type_ != FRAME_TYPE_DEFAULT)
906 return frame_type_ == FRAME_TYPE_FORCE_NATIVE;
907 return native_widget_->ShouldUseNativeFrame();
910 bool Widget::ShouldWindowContentsBeTransparent() const {
911 return native_widget_->ShouldWindowContentsBeTransparent();
914 void Widget::DebugToggleFrameType() {
915 if (frame_type_ == FRAME_TYPE_DEFAULT) {
916 frame_type_ = ShouldUseNativeFrame() ? FRAME_TYPE_FORCE_CUSTOM :
917 FRAME_TYPE_FORCE_NATIVE;
918 } else {
919 frame_type_ = frame_type_ == FRAME_TYPE_FORCE_CUSTOM ?
920 FRAME_TYPE_FORCE_NATIVE : FRAME_TYPE_FORCE_CUSTOM;
922 FrameTypeChanged();
925 void Widget::FrameTypeChanged() {
926 native_widget_->FrameTypeChanged();
929 const ui::Compositor* Widget::GetCompositor() const {
930 return native_widget_->GetCompositor();
933 const ui::Layer* Widget::GetLayer() const {
934 return native_widget_->GetLayer();
937 void Widget::ReorderNativeViews() {
938 native_widget_->ReorderNativeViews();
941 void Widget::UpdateRootLayers() {
942 // Calculate the layers requires traversing the tree, and since nearly any
943 // mutation of the tree can trigger this call we delay until absolutely
944 // necessary.
945 root_layers_dirty_ = true;
948 const NativeWidget* Widget::native_widget() const {
949 return native_widget_;
952 NativeWidget* Widget::native_widget() {
953 return native_widget_;
956 void Widget::SetCapture(View* view) {
957 if (!native_widget_->HasCapture()) {
958 native_widget_->SetCapture();
960 // Early return if setting capture was unsuccessful.
961 if (!native_widget_->HasCapture())
962 return;
965 if (internal::NativeWidgetPrivate::IsMouseButtonDown())
966 is_mouse_button_pressed_ = true;
967 root_view_->SetMouseHandler(view);
970 void Widget::ReleaseCapture() {
971 if (native_widget_->HasCapture())
972 native_widget_->ReleaseCapture();
975 bool Widget::HasCapture() {
976 return native_widget_->HasCapture();
979 TooltipManager* Widget::GetTooltipManager() {
980 return native_widget_->GetTooltipManager();
983 const TooltipManager* Widget::GetTooltipManager() const {
984 return native_widget_->GetTooltipManager();
987 gfx::Rect Widget::GetWorkAreaBoundsInScreen() const {
988 return native_widget_->GetWorkAreaBoundsInScreen();
991 void Widget::SynthesizeMouseMoveEvent() {
992 // In screen coordinate.
993 gfx::Point mouse_location = EventMonitor::GetLastMouseLocation();
994 if (!GetWindowBoundsInScreen().Contains(mouse_location))
995 return;
997 // Convert: screen coordinate -> widget coordinate.
998 View::ConvertPointFromScreen(root_view_.get(), &mouse_location);
999 last_mouse_event_was_move_ = false;
1000 ui::MouseEvent mouse_event(ui::ET_MOUSE_MOVED, mouse_location,
1001 mouse_location, ui::EventTimeForNow(),
1002 ui::EF_IS_SYNTHESIZED, 0);
1003 root_view_->OnMouseMoved(mouse_event);
1006 void Widget::OnRootViewLayout() {
1007 native_widget_->OnRootViewLayout();
1010 bool Widget::IsTranslucentWindowOpacitySupported() const {
1011 return native_widget_->IsTranslucentWindowOpacitySupported();
1014 void Widget::OnSizeConstraintsChanged() {
1015 native_widget_->OnSizeConstraintsChanged();
1016 non_client_view_->SizeConstraintsChanged();
1019 void Widget::OnOwnerClosing() {
1022 ////////////////////////////////////////////////////////////////////////////////
1023 // Widget, NativeWidgetDelegate implementation:
1025 bool Widget::IsModal() const {
1026 return widget_delegate_->GetModalType() != ui::MODAL_TYPE_NONE;
1029 bool Widget::IsDialogBox() const {
1030 return !!widget_delegate_->AsDialogDelegate();
1033 bool Widget::CanActivate() const {
1034 return widget_delegate_->CanActivate();
1037 bool Widget::IsInactiveRenderingDisabled() const {
1038 return disable_inactive_rendering_;
1041 void Widget::EnableInactiveRendering() {
1042 SetInactiveRenderingDisabled(false);
1045 void Widget::OnNativeWidgetActivationChanged(bool active) {
1046 // On windows we may end up here before we've completed initialization (from
1047 // an WM_NCACTIVATE). If that happens the WidgetDelegate likely doesn't know
1048 // the Widget and will crash attempting to access it.
1049 if (!active && native_widget_initialized_)
1050 SaveWindowPlacement();
1052 FOR_EACH_OBSERVER(WidgetObserver, observers_,
1053 OnWidgetActivationChanged(this, active));
1055 if (IsVisible() && non_client_view())
1056 non_client_view()->frame_view()->SchedulePaint();
1059 void Widget::OnNativeFocus() {
1060 WidgetFocusManager::GetInstance()->OnNativeFocusChanged(GetNativeView());
1063 void Widget::OnNativeBlur() {
1064 WidgetFocusManager::GetInstance()->OnNativeFocusChanged(nullptr);
1067 void Widget::OnNativeWidgetVisibilityChanging(bool visible) {
1068 FOR_EACH_OBSERVER(WidgetObserver, observers_,
1069 OnWidgetVisibilityChanging(this, visible));
1072 void Widget::OnNativeWidgetVisibilityChanged(bool visible) {
1073 View* root = GetRootView();
1074 if (root)
1075 root->PropagateVisibilityNotifications(root, visible);
1076 FOR_EACH_OBSERVER(WidgetObserver, observers_,
1077 OnWidgetVisibilityChanged(this, visible));
1078 if (GetCompositor() && root && root->layer())
1079 root->layer()->SetVisible(visible);
1082 void Widget::OnNativeWidgetCreated(bool desktop_widget) {
1083 if (is_top_level())
1084 focus_manager_.reset(FocusManagerFactory::Create(this, desktop_widget));
1086 native_widget_->InitModalType(widget_delegate_->GetModalType());
1088 FOR_EACH_OBSERVER(WidgetObserver, observers_, OnWidgetCreated(this));
1091 void Widget::OnNativeWidgetDestroying() {
1092 // Tell the focus manager (if any) that root_view is being removed
1093 // in case that the focused view is under this root view.
1094 if (GetFocusManager())
1095 GetFocusManager()->ViewRemoved(root_view_.get());
1096 FOR_EACH_OBSERVER(WidgetObserver, observers_, OnWidgetDestroying(this));
1097 if (non_client_view_)
1098 non_client_view_->WindowClosing();
1099 widget_delegate_->WindowClosing();
1102 void Widget::OnNativeWidgetDestroyed() {
1103 FOR_EACH_OBSERVER(WidgetObserver, observers_, OnWidgetDestroyed(this));
1104 widget_delegate_->DeleteDelegate();
1105 widget_delegate_ = NULL;
1106 native_widget_destroyed_ = true;
1109 gfx::Size Widget::GetMinimumSize() const {
1110 return non_client_view_ ? non_client_view_->GetMinimumSize() : gfx::Size();
1113 gfx::Size Widget::GetMaximumSize() const {
1114 return non_client_view_ ? non_client_view_->GetMaximumSize() : gfx::Size();
1117 void Widget::OnNativeWidgetMove() {
1118 widget_delegate_->OnWidgetMove();
1119 NotifyCaretBoundsChanged(GetInputMethod());
1121 FOR_EACH_OBSERVER(WidgetObserver, observers_, OnWidgetBoundsChanged(
1122 this,
1123 GetWindowBoundsInScreen()));
1126 void Widget::OnNativeWidgetSizeChanged(const gfx::Size& new_size) {
1127 View* root = GetRootView();
1128 if (root)
1129 root->SetSize(new_size);
1131 NotifyCaretBoundsChanged(GetInputMethod());
1132 SaveWindowPlacementIfInitialized();
1134 FOR_EACH_OBSERVER(WidgetObserver, observers_, OnWidgetBoundsChanged(
1135 this,
1136 GetWindowBoundsInScreen()));
1139 void Widget::OnNativeWidgetWindowShowStateChanged() {
1140 SaveWindowPlacementIfInitialized();
1143 void Widget::OnNativeWidgetBeginUserBoundsChange() {
1144 widget_delegate_->OnWindowBeginUserBoundsChange();
1147 void Widget::OnNativeWidgetEndUserBoundsChange() {
1148 widget_delegate_->OnWindowEndUserBoundsChange();
1151 bool Widget::HasFocusManager() const {
1152 return !!focus_manager_.get();
1155 void Widget::OnNativeWidgetPaint(const ui::PaintContext& context) {
1156 // On Linux Aura, we can get here during Init() because of the
1157 // SetInitialBounds call.
1158 if (!native_widget_initialized_)
1159 return;
1160 GetRootView()->Paint(context);
1163 int Widget::GetNonClientComponent(const gfx::Point& point) {
1164 int component = non_client_view_ ?
1165 non_client_view_->NonClientHitTest(point) :
1166 HTNOWHERE;
1168 if (movement_disabled_ && (component == HTCAPTION || component == HTSYSMENU))
1169 return HTNOWHERE;
1171 return component;
1174 void Widget::OnKeyEvent(ui::KeyEvent* event) {
1175 SendEventToProcessor(event);
1178 // TODO(tdanderson): We should not be calling the OnMouse*() functions on
1179 // RootView from anywhere in Widget. Use
1180 // SendEventToProcessor() instead. See crbug.com/348087.
1181 void Widget::OnMouseEvent(ui::MouseEvent* event) {
1182 View* root_view = GetRootView();
1183 switch (event->type()) {
1184 case ui::ET_MOUSE_PRESSED: {
1185 last_mouse_event_was_move_ = false;
1187 // We may get deleted by the time we return from OnMousePressed. So we
1188 // use an observer to make sure we are still alive.
1189 WidgetDeletionObserver widget_deletion_observer(this);
1191 // Make sure we're still visible before we attempt capture as the mouse
1192 // press processing may have made the window hide (as happens with menus).
1194 // It is possible for a View to show a context menu on mouse-press. Since
1195 // the menu does a capture and starts a nested message-loop, the release
1196 // would go to the menu. The next click (i.e. both mouse-press and release
1197 // events) also go to the menu. The menu (and the nested message-loop)
1198 // gets closed after this second release event. The code then resumes from
1199 // here. So make sure that the mouse-button is still down before doing a
1200 // capture.
1201 if (root_view && root_view->OnMousePressed(*event) &&
1202 widget_deletion_observer.IsWidgetAlive() && IsVisible() &&
1203 internal::NativeWidgetPrivate::IsMouseButtonDown()) {
1204 is_mouse_button_pressed_ = true;
1205 if (!native_widget_->HasCapture())
1206 native_widget_->SetCapture();
1207 event->SetHandled();
1209 return;
1212 case ui::ET_MOUSE_RELEASED:
1213 last_mouse_event_was_move_ = false;
1214 is_mouse_button_pressed_ = false;
1215 // Release capture first, to avoid confusion if OnMouseReleased blocks.
1216 if (auto_release_capture_ && native_widget_->HasCapture()) {
1217 base::AutoReset<bool> resetter(&ignore_capture_loss_, true);
1218 native_widget_->ReleaseCapture();
1220 if (root_view)
1221 root_view->OnMouseReleased(*event);
1222 if ((event->flags() & ui::EF_IS_NON_CLIENT) == 0)
1223 event->SetHandled();
1224 return;
1226 case ui::ET_MOUSE_MOVED:
1227 case ui::ET_MOUSE_DRAGGED:
1228 if (native_widget_->HasCapture() && is_mouse_button_pressed_) {
1229 last_mouse_event_was_move_ = false;
1230 if (root_view)
1231 root_view->OnMouseDragged(*event);
1232 } else if (!last_mouse_event_was_move_ ||
1233 last_mouse_event_position_ != event->location()) {
1234 last_mouse_event_position_ = event->location();
1235 last_mouse_event_was_move_ = true;
1236 if (root_view)
1237 root_view->OnMouseMoved(*event);
1239 return;
1241 case ui::ET_MOUSE_EXITED:
1242 last_mouse_event_was_move_ = false;
1243 if (root_view)
1244 root_view->OnMouseExited(*event);
1245 return;
1247 case ui::ET_MOUSEWHEEL:
1248 if (root_view && root_view->OnMouseWheel(
1249 static_cast<const ui::MouseWheelEvent&>(*event)))
1250 event->SetHandled();
1251 return;
1253 default:
1254 return;
1258 void Widget::OnMouseCaptureLost() {
1259 if (ignore_capture_loss_)
1260 return;
1262 View* root_view = GetRootView();
1263 if (root_view)
1264 root_view->OnMouseCaptureLost();
1265 is_mouse_button_pressed_ = false;
1268 void Widget::OnScrollEvent(ui::ScrollEvent* event) {
1269 ui::ScrollEvent event_copy(*event);
1270 SendEventToProcessor(&event_copy);
1272 // Convert unhandled ui::ET_SCROLL events into ui::ET_MOUSEWHEEL events.
1273 if (!event_copy.handled() && event_copy.type() == ui::ET_SCROLL) {
1274 ui::MouseWheelEvent wheel(*event);
1275 OnMouseEvent(&wheel);
1279 void Widget::OnGestureEvent(ui::GestureEvent* event) {
1280 // We explicitly do not capture here. Not capturing enables multiple widgets
1281 // to get tap events at the same time. Views (such as tab dragging) may
1282 // explicitly capture.
1283 SendEventToProcessor(event);
1286 bool Widget::ExecuteCommand(int command_id) {
1287 return widget_delegate_->ExecuteWindowsCommand(command_id);
1290 const std::vector<ui::Layer*>& Widget::GetRootLayers() {
1291 if (root_layers_dirty_) {
1292 root_layers_dirty_ = false;
1293 root_layers_.clear();
1294 BuildRootLayers(GetRootView(), &root_layers_);
1296 return root_layers_;
1299 bool Widget::HasHitTestMask() const {
1300 return widget_delegate_->WidgetHasHitTestMask();
1303 void Widget::GetHitTestMask(gfx::Path* mask) const {
1304 DCHECK(mask);
1305 widget_delegate_->GetWidgetHitTestMask(mask);
1308 Widget* Widget::AsWidget() {
1309 return this;
1312 const Widget* Widget::AsWidget() const {
1313 return this;
1316 bool Widget::SetInitialFocus(ui::WindowShowState show_state) {
1317 View* v = widget_delegate_->GetInitiallyFocusedView();
1318 if (!focus_on_creation_ || show_state == ui::SHOW_STATE_INACTIVE ||
1319 show_state == ui::SHOW_STATE_MINIMIZED) {
1320 // If not focusing the window now, tell the focus manager which view to
1321 // focus when the window is restored.
1322 if (v)
1323 focus_manager_->SetStoredFocusView(v);
1324 return true;
1326 if (v)
1327 v->RequestFocus();
1328 return !!v;
1331 ////////////////////////////////////////////////////////////////////////////////
1332 // Widget, ui::EventSource implementation:
1333 ui::EventProcessor* Widget::GetEventProcessor() {
1334 return root_view_.get();
1337 ////////////////////////////////////////////////////////////////////////////////
1338 // Widget, FocusTraversable implementation:
1340 FocusSearch* Widget::GetFocusSearch() {
1341 return root_view_->GetFocusSearch();
1344 FocusTraversable* Widget::GetFocusTraversableParent() {
1345 // We are a proxy to the root view, so we should be bypassed when traversing
1346 // up and as a result this should not be called.
1347 NOTREACHED();
1348 return NULL;
1351 View* Widget::GetFocusTraversableParentView() {
1352 // We are a proxy to the root view, so we should be bypassed when traversing
1353 // up and as a result this should not be called.
1354 NOTREACHED();
1355 return NULL;
1358 ////////////////////////////////////////////////////////////////////////////////
1359 // Widget, ui::NativeThemeObserver implementation:
1361 void Widget::OnNativeThemeUpdated(ui::NativeTheme* observed_theme) {
1362 DCHECK(observer_manager_.IsObserving(observed_theme));
1364 ui::NativeTheme* current_native_theme = GetNativeTheme();
1365 if (!observer_manager_.IsObserving(current_native_theme)) {
1366 observer_manager_.RemoveAll();
1367 observer_manager_.Add(current_native_theme);
1370 root_view_->PropagateNativeThemeChanged(current_native_theme);
1373 ////////////////////////////////////////////////////////////////////////////////
1374 // Widget, protected:
1376 internal::RootView* Widget::CreateRootView() {
1377 return new internal::RootView(this);
1380 void Widget::DestroyRootView() {
1381 non_client_view_ = NULL;
1382 root_view_.reset();
1385 void Widget::OnDragWillStart() {
1388 void Widget::OnDragComplete() {
1391 ////////////////////////////////////////////////////////////////////////////////
1392 // Widget, private:
1394 void Widget::SetInactiveRenderingDisabled(bool value) {
1395 if (value == disable_inactive_rendering_)
1396 return;
1398 disable_inactive_rendering_ = value;
1399 if (non_client_view_)
1400 non_client_view_->SetInactiveRenderingDisabled(value);
1403 void Widget::SaveWindowPlacement() {
1404 // The window delegate does the actual saving for us. It seems like (judging
1405 // by go/crash) that in some circumstances we can end up here after
1406 // WM_DESTROY, at which point the window delegate is likely gone. So just
1407 // bail.
1408 if (!widget_delegate_)
1409 return;
1411 ui::WindowShowState show_state = ui::SHOW_STATE_NORMAL;
1412 gfx::Rect bounds;
1413 native_widget_->GetWindowPlacement(&bounds, &show_state);
1414 widget_delegate_->SaveWindowPlacement(bounds, show_state);
1417 void Widget::SaveWindowPlacementIfInitialized() {
1418 if (native_widget_initialized_)
1419 SaveWindowPlacement();
1422 void Widget::SetInitialBounds(const gfx::Rect& bounds) {
1423 if (!non_client_view_)
1424 return;
1426 gfx::Rect saved_bounds;
1427 if (GetSavedWindowPlacement(&saved_bounds, &saved_show_state_)) {
1428 if (saved_show_state_ == ui::SHOW_STATE_MAXIMIZED) {
1429 // If we're going to maximize, wait until Show is invoked to set the
1430 // bounds. That way we avoid a noticeable resize.
1431 initial_restored_bounds_ = saved_bounds;
1432 } else if (!saved_bounds.IsEmpty()) {
1433 // If the saved bounds are valid, use them.
1434 SetBounds(saved_bounds);
1436 } else {
1437 if (bounds.IsEmpty()) {
1438 // No initial bounds supplied, so size the window to its content and
1439 // center over its parent.
1440 native_widget_->CenterWindow(non_client_view_->GetPreferredSize());
1441 } else {
1442 // Use the supplied initial bounds.
1443 SetBoundsConstrained(bounds);
1448 void Widget::SetInitialBoundsForFramelessWindow(const gfx::Rect& bounds) {
1449 if (bounds.IsEmpty()) {
1450 View* contents_view = GetContentsView();
1451 DCHECK(contents_view);
1452 // No initial bounds supplied, so size the window to its content and
1453 // center over its parent if preferred size is provided.
1454 gfx::Size size = contents_view->GetPreferredSize();
1455 if (!size.IsEmpty())
1456 native_widget_->CenterWindow(size);
1457 } else {
1458 // Use the supplied initial bounds.
1459 SetBoundsConstrained(bounds);
1463 bool Widget::GetSavedWindowPlacement(gfx::Rect* bounds,
1464 ui::WindowShowState* show_state) {
1465 // First we obtain the window's saved show-style and store it. We need to do
1466 // this here, rather than in Show() because by the time Show() is called,
1467 // the window's size will have been reset (below) and the saved maximized
1468 // state will have been lost. Sadly there's no way to tell on Windows when
1469 // a window is restored from maximized state, so we can't more accurately
1470 // track maximized state independently of sizing information.
1472 // Restore the window's placement from the controller.
1473 if (widget_delegate_->GetSavedWindowPlacement(this, bounds, show_state)) {
1474 if (!widget_delegate_->ShouldRestoreWindowSize()) {
1475 bounds->set_size(non_client_view_->GetPreferredSize());
1476 } else {
1477 gfx::Size minimum_size = GetMinimumSize();
1478 // Make sure the bounds are at least the minimum size.
1479 if (bounds->width() < minimum_size.width())
1480 bounds->set_width(minimum_size.width());
1482 if (bounds->height() < minimum_size.height())
1483 bounds->set_height(minimum_size.height());
1485 return true;
1487 return false;
1490 namespace internal {
1492 ////////////////////////////////////////////////////////////////////////////////
1493 // internal::NativeWidgetPrivate, NativeWidget implementation:
1495 internal::NativeWidgetPrivate* NativeWidgetPrivate::AsNativeWidgetPrivate() {
1496 return this;
1499 } // namespace internal
1500 } // namespace views