Add ICU message format support
[chromium-blink-merge.git] / ui / views / widget / widget.cc
blob280969a84e5e7d0067d1f10378dec9d6d15061fb
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/focus/focus_manager.h"
25 #include "ui/views/focus/focus_manager_factory.h"
26 #include "ui/views/focus/view_storage.h"
27 #include "ui/views/focus/widget_focus_manager.h"
28 #include "ui/views/views_delegate.h"
29 #include "ui/views/widget/native_widget_private.h"
30 #include "ui/views/widget/root_view.h"
31 #include "ui/views/widget/tooltip_manager.h"
32 #include "ui/views/widget/widget_delegate.h"
33 #include "ui/views/widget/widget_deletion_observer.h"
34 #include "ui/views/widget/widget_observer.h"
35 #include "ui/views/widget/widget_removals_observer.h"
36 #include "ui/views/window/custom_frame_view.h"
37 #include "ui/views/window/dialog_delegate.h"
39 namespace views {
41 namespace {
43 // If |view| has a layer the layer is added to |layers|. Else this recurses
44 // through the children. This is used to build a list of the layers created by
45 // views that are direct children of the Widgets layer.
46 void BuildRootLayers(View* view, std::vector<ui::Layer*>* layers) {
47 if (view->layer()) {
48 layers->push_back(view->layer());
49 } else {
50 for (int i = 0; i < view->child_count(); ++i)
51 BuildRootLayers(view->child_at(i), layers);
55 // Create a native widget implementation.
56 // First, use the supplied one if non-NULL.
57 // Finally, make a default one.
58 NativeWidget* CreateNativeWidget(NativeWidget* native_widget,
59 internal::NativeWidgetDelegate* delegate) {
60 if (!native_widget) {
61 native_widget =
62 internal::NativeWidgetPrivate::CreateNativeWidget(delegate);
64 return native_widget;
67 void NotifyCaretBoundsChanged(ui::InputMethod* input_method) {
68 if (!input_method)
69 return;
70 ui::TextInputClient* client = input_method->GetTextInputClient();
71 if (client)
72 input_method->OnCaretBoundsChanged(client);
75 } // namespace
77 // A default implementation of WidgetDelegate, used by Widget when no
78 // WidgetDelegate is supplied.
79 class DefaultWidgetDelegate : public WidgetDelegate {
80 public:
81 explicit DefaultWidgetDelegate(Widget* widget) : widget_(widget) {
83 ~DefaultWidgetDelegate() override {}
85 // Overridden from WidgetDelegate:
86 void DeleteDelegate() override { delete this; }
87 Widget* GetWidget() override { return widget_; }
88 const Widget* GetWidget() const override { return widget_; }
89 bool ShouldAdvanceFocusToTopLevelWidget() const override {
90 // In most situations where a Widget is used without a delegate the Widget
91 // is used as a container, so that we want focus to advance to the top-level
92 // widget. A good example of this is the find bar.
93 return true;
96 private:
97 Widget* widget_;
99 DISALLOW_COPY_AND_ASSIGN(DefaultWidgetDelegate);
102 ////////////////////////////////////////////////////////////////////////////////
103 // Widget, InitParams:
105 Widget::InitParams::InitParams()
106 : type(TYPE_WINDOW),
107 delegate(NULL),
108 child(false),
109 opacity(INFER_OPACITY),
110 accept_events(true),
111 activatable(ACTIVATABLE_DEFAULT),
112 keep_on_top(false),
113 visible_on_all_workspaces(false),
114 ownership(NATIVE_WIDGET_OWNS_WIDGET),
115 mirror_origin_in_rtl(false),
116 shadow_type(SHADOW_TYPE_DEFAULT),
117 remove_standard_frame(false),
118 use_system_default_icon(false),
119 show_state(ui::SHOW_STATE_DEFAULT),
120 parent(NULL),
121 native_widget(NULL),
122 desktop_window_tree_host(NULL),
123 layer_type(ui::LAYER_TEXTURED),
124 context(NULL),
125 force_show_in_taskbar(false) {
128 Widget::InitParams::InitParams(Type type)
129 : type(type),
130 delegate(NULL),
131 child(false),
132 opacity(INFER_OPACITY),
133 accept_events(true),
134 activatable(ACTIVATABLE_DEFAULT),
135 keep_on_top(type == TYPE_MENU || type == TYPE_DRAG),
136 visible_on_all_workspaces(false),
137 ownership(NATIVE_WIDGET_OWNS_WIDGET),
138 mirror_origin_in_rtl(false),
139 shadow_type(SHADOW_TYPE_DEFAULT),
140 remove_standard_frame(false),
141 use_system_default_icon(false),
142 show_state(ui::SHOW_STATE_DEFAULT),
143 parent(NULL),
144 native_widget(NULL),
145 desktop_window_tree_host(NULL),
146 layer_type(ui::LAYER_TEXTURED),
147 context(NULL),
148 force_show_in_taskbar(false) {
151 Widget::InitParams::~InitParams() {
154 ////////////////////////////////////////////////////////////////////////////////
155 // Widget, public:
157 Widget::Widget()
158 : native_widget_(NULL),
159 widget_delegate_(NULL),
160 non_client_view_(NULL),
161 dragged_view_(NULL),
162 ownership_(InitParams::NATIVE_WIDGET_OWNS_WIDGET),
163 is_secondary_widget_(true),
164 frame_type_(FRAME_TYPE_DEFAULT),
165 disable_inactive_rendering_(false),
166 widget_closed_(false),
167 saved_show_state_(ui::SHOW_STATE_DEFAULT),
168 focus_on_creation_(true),
169 is_top_level_(false),
170 native_widget_initialized_(false),
171 native_widget_destroyed_(false),
172 is_mouse_button_pressed_(false),
173 ignore_capture_loss_(false),
174 last_mouse_event_was_move_(false),
175 auto_release_capture_(true),
176 root_layers_dirty_(false),
177 movement_disabled_(false),
178 observer_manager_(this) {
181 Widget::~Widget() {
182 DestroyRootView();
183 if (ownership_ == InitParams::WIDGET_OWNS_NATIVE_WIDGET) {
184 delete native_widget_;
185 } else {
186 DCHECK(native_widget_destroyed_)
187 << "Destroying a widget with a live native widget. "
188 << "Widget probably should use WIDGET_OWNS_NATIVE_WIDGET ownership.";
192 // static
193 Widget* Widget::CreateWindow(WidgetDelegate* delegate) {
194 return CreateWindowWithBounds(delegate, gfx::Rect());
197 // static
198 Widget* Widget::CreateWindowWithBounds(WidgetDelegate* delegate,
199 const gfx::Rect& bounds) {
200 Widget* widget = new Widget;
201 Widget::InitParams params;
202 params.bounds = bounds;
203 params.delegate = delegate;
204 widget->Init(params);
205 return widget;
208 // static
209 Widget* Widget::CreateWindowWithParent(WidgetDelegate* delegate,
210 gfx::NativeView parent) {
211 return CreateWindowWithParentAndBounds(delegate, parent, gfx::Rect());
214 // static
215 Widget* Widget::CreateWindowWithParentAndBounds(WidgetDelegate* delegate,
216 gfx::NativeView parent,
217 const gfx::Rect& bounds) {
218 Widget* widget = new Widget;
219 Widget::InitParams params;
220 params.delegate = delegate;
221 params.parent = parent;
222 params.bounds = bounds;
223 widget->Init(params);
224 return widget;
227 // static
228 Widget* Widget::CreateWindowWithContext(WidgetDelegate* delegate,
229 gfx::NativeWindow context) {
230 return CreateWindowWithContextAndBounds(delegate, context, gfx::Rect());
233 // static
234 Widget* Widget::CreateWindowWithContextAndBounds(WidgetDelegate* delegate,
235 gfx::NativeWindow context,
236 const gfx::Rect& bounds) {
237 Widget* widget = new Widget;
238 Widget::InitParams params;
239 params.delegate = delegate;
240 params.context = context;
241 params.bounds = bounds;
242 widget->Init(params);
243 return widget;
246 // static
247 Widget* Widget::GetWidgetForNativeView(gfx::NativeView native_view) {
248 internal::NativeWidgetPrivate* native_widget =
249 internal::NativeWidgetPrivate::GetNativeWidgetForNativeView(native_view);
250 return native_widget ? native_widget->GetWidget() : NULL;
253 // static
254 Widget* Widget::GetWidgetForNativeWindow(gfx::NativeWindow native_window) {
255 internal::NativeWidgetPrivate* native_widget =
256 internal::NativeWidgetPrivate::GetNativeWidgetForNativeWindow(
257 native_window);
258 return native_widget ? native_widget->GetWidget() : NULL;
261 // static
262 Widget* Widget::GetTopLevelWidgetForNativeView(gfx::NativeView native_view) {
263 internal::NativeWidgetPrivate* native_widget =
264 internal::NativeWidgetPrivate::GetTopLevelNativeWidget(native_view);
265 return native_widget ? native_widget->GetWidget() : NULL;
269 // static
270 void Widget::GetAllChildWidgets(gfx::NativeView native_view,
271 Widgets* children) {
272 internal::NativeWidgetPrivate::GetAllChildWidgets(native_view, children);
275 // static
276 void Widget::GetAllOwnedWidgets(gfx::NativeView native_view,
277 Widgets* owned) {
278 internal::NativeWidgetPrivate::GetAllOwnedWidgets(native_view, owned);
281 // static
282 void Widget::ReparentNativeView(gfx::NativeView native_view,
283 gfx::NativeView new_parent) {
284 internal::NativeWidgetPrivate::ReparentNativeView(native_view, new_parent);
287 // static
288 int Widget::GetLocalizedContentsWidth(int col_resource_id) {
289 return ui::GetLocalizedContentsWidthForFont(col_resource_id,
290 ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont));
293 // static
294 int Widget::GetLocalizedContentsHeight(int row_resource_id) {
295 return ui::GetLocalizedContentsHeightForFont(row_resource_id,
296 ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont));
299 // static
300 gfx::Size Widget::GetLocalizedContentsSize(int col_resource_id,
301 int row_resource_id) {
302 return gfx::Size(GetLocalizedContentsWidth(col_resource_id),
303 GetLocalizedContentsHeight(row_resource_id));
306 // static
307 bool Widget::RequiresNonClientView(InitParams::Type type) {
308 return type == InitParams::TYPE_WINDOW ||
309 type == InitParams::TYPE_PANEL ||
310 type == InitParams::TYPE_BUBBLE;
313 void Widget::Init(const InitParams& in_params) {
314 TRACE_EVENT0("views", "Widget::Init");
315 InitParams params = in_params;
317 params.child |= (params.type == InitParams::TYPE_CONTROL);
318 is_top_level_ = !params.child;
320 if (params.opacity == views::Widget::InitParams::INFER_OPACITY &&
321 params.type != views::Widget::InitParams::TYPE_WINDOW &&
322 params.type != views::Widget::InitParams::TYPE_PANEL)
323 params.opacity = views::Widget::InitParams::OPAQUE_WINDOW;
325 if (ViewsDelegate::GetInstance())
326 ViewsDelegate::GetInstance()->OnBeforeWidgetInit(&params, this);
328 if (params.opacity == views::Widget::InitParams::INFER_OPACITY)
329 params.opacity = views::Widget::InitParams::OPAQUE_WINDOW;
331 bool can_activate = false;
332 if (params.activatable != InitParams::ACTIVATABLE_DEFAULT) {
333 can_activate = (params.activatable == InitParams::ACTIVATABLE_YES);
334 } else if (params.type != InitParams::TYPE_CONTROL &&
335 params.type != InitParams::TYPE_POPUP &&
336 params.type != InitParams::TYPE_MENU &&
337 params.type != InitParams::TYPE_TOOLTIP &&
338 params.type != InitParams::TYPE_DRAG) {
339 can_activate = true;
340 params.activatable = InitParams::ACTIVATABLE_YES;
341 } else {
342 can_activate = false;
343 params.activatable = InitParams::ACTIVATABLE_NO;
346 widget_delegate_ = params.delegate ?
347 params.delegate : new DefaultWidgetDelegate(this);
348 widget_delegate_->set_can_activate(can_activate);
350 ownership_ = params.ownership;
351 native_widget_ = CreateNativeWidget(params.native_widget, this)->
352 AsNativeWidgetPrivate();
353 root_view_.reset(CreateRootView());
354 default_theme_provider_.reset(new ui::DefaultThemeProvider);
355 if (params.type == InitParams::TYPE_MENU) {
356 is_mouse_button_pressed_ =
357 internal::NativeWidgetPrivate::IsMouseButtonDown();
359 native_widget_->InitNativeWidget(params);
360 if (RequiresNonClientView(params.type)) {
361 non_client_view_ = new NonClientView;
362 non_client_view_->SetFrameView(CreateNonClientFrameView());
363 // Create the ClientView, add it to the NonClientView and add the
364 // NonClientView to the RootView. This will cause everything to be parented.
365 non_client_view_->set_client_view(widget_delegate_->CreateClientView(this));
366 non_client_view_->SetOverlayView(widget_delegate_->CreateOverlayView());
367 SetContentsView(non_client_view_);
368 // Initialize the window's icon and title before setting the window's
369 // initial bounds; the frame view's preferred height may depend on the
370 // presence of an icon or a title.
371 UpdateWindowIcon();
372 UpdateWindowTitle();
373 non_client_view_->ResetWindowControls();
374 SetInitialBounds(params.bounds);
375 if (params.show_state == ui::SHOW_STATE_MAXIMIZED)
376 Maximize();
377 else if (params.show_state == ui::SHOW_STATE_MINIMIZED)
378 Minimize();
379 } else if (params.delegate) {
380 SetContentsView(params.delegate->GetContentsView());
381 SetInitialBoundsForFramelessWindow(params.bounds);
383 // This must come after SetContentsView() or it might not be able to find
384 // the correct NativeTheme (on Linux). See http://crbug.com/384492
385 observer_manager_.Add(GetNativeTheme());
386 native_widget_initialized_ = true;
389 // Unconverted methods (see header) --------------------------------------------
391 gfx::NativeView Widget::GetNativeView() const {
392 return native_widget_->GetNativeView();
395 gfx::NativeWindow Widget::GetNativeWindow() const {
396 return native_widget_->GetNativeWindow();
399 void Widget::AddObserver(WidgetObserver* observer) {
400 // Make sure that there is no nullptr in observer list. crbug.com/471649.
401 CHECK(observer);
402 observers_.AddObserver(observer);
405 void Widget::RemoveObserver(WidgetObserver* observer) {
406 observers_.RemoveObserver(observer);
409 bool Widget::HasObserver(const WidgetObserver* observer) const {
410 return observers_.HasObserver(observer);
413 void Widget::AddRemovalsObserver(WidgetRemovalsObserver* observer) {
414 removals_observers_.AddObserver(observer);
417 void Widget::RemoveRemovalsObserver(WidgetRemovalsObserver* observer) {
418 removals_observers_.RemoveObserver(observer);
421 bool Widget::HasRemovalsObserver(const WidgetRemovalsObserver* observer) const {
422 return removals_observers_.HasObserver(observer);
425 bool Widget::GetAccelerator(int cmd_id, ui::Accelerator* accelerator) const {
426 return false;
429 void Widget::ViewHierarchyChanged(
430 const View::ViewHierarchyChangedDetails& details) {
431 if (!details.is_add) {
432 if (details.child == dragged_view_)
433 dragged_view_ = NULL;
434 FocusManager* focus_manager = GetFocusManager();
435 if (focus_manager)
436 focus_manager->ViewRemoved(details.child);
437 ViewStorage::GetInstance()->ViewRemoved(details.child);
438 native_widget_->ViewRemoved(details.child);
442 void Widget::NotifyNativeViewHierarchyWillChange() {
443 FocusManager* focus_manager = GetFocusManager();
444 // We are being removed from a window hierarchy. Treat this as
445 // the root_view_ being removed.
446 if (focus_manager)
447 focus_manager->ViewRemoved(root_view_.get());
450 void Widget::NotifyNativeViewHierarchyChanged() {
451 root_view_->NotifyNativeViewHierarchyChanged();
454 void Widget::NotifyWillRemoveView(View* view) {
455 FOR_EACH_OBSERVER(WidgetRemovalsObserver,
456 removals_observers_,
457 OnWillRemoveView(this, view));
460 // Converted methods (see header) ----------------------------------------------
462 Widget* Widget::GetTopLevelWidget() {
463 return const_cast<Widget*>(
464 static_cast<const Widget*>(this)->GetTopLevelWidget());
467 const Widget* Widget::GetTopLevelWidget() const {
468 // GetTopLevelNativeWidget doesn't work during destruction because
469 // property is gone after gobject gets deleted. Short circuit here
470 // for toplevel so that InputMethod can remove itself from
471 // focus manager.
472 return is_top_level() ? this : native_widget_->GetTopLevelWidget();
475 void Widget::SetContentsView(View* view) {
476 // Do not SetContentsView() again if it is already set to the same view.
477 if (view == GetContentsView())
478 return;
479 root_view_->SetContentsView(view);
480 if (non_client_view_ != view) {
481 // |non_client_view_| can only be non-NULL here if RequiresNonClientView()
482 // was true when the widget was initialized. Creating widgets with non
483 // client views and then setting the contents view can cause subtle
484 // problems on Windows, where the native widget thinks there is still a
485 // |non_client_view_|. If you get this error, either use a different type
486 // when initializing the widget, or don't call SetContentsView().
487 DCHECK(!non_client_view_);
488 non_client_view_ = NULL;
492 View* Widget::GetContentsView() {
493 return root_view_->GetContentsView();
496 gfx::Rect Widget::GetWindowBoundsInScreen() const {
497 return native_widget_->GetWindowBoundsInScreen();
500 gfx::Rect Widget::GetClientAreaBoundsInScreen() const {
501 return native_widget_->GetClientAreaBoundsInScreen();
504 gfx::Rect Widget::GetRestoredBounds() const {
505 return native_widget_->GetRestoredBounds();
508 void Widget::SetBounds(const gfx::Rect& bounds) {
509 native_widget_->SetBounds(bounds);
512 void Widget::SetSize(const gfx::Size& size) {
513 native_widget_->SetSize(size);
516 void Widget::CenterWindow(const gfx::Size& size) {
517 native_widget_->CenterWindow(size);
520 void Widget::SetBoundsConstrained(const gfx::Rect& bounds) {
521 gfx::Rect work_area =
522 gfx::Screen::GetScreenFor(GetNativeView())->GetDisplayNearestPoint(
523 bounds.origin()).work_area();
524 if (work_area.IsEmpty()) {
525 SetBounds(bounds);
526 } else {
527 // Inset the work area slightly.
528 work_area.Inset(10, 10, 10, 10);
529 work_area.AdjustToFit(bounds);
530 SetBounds(work_area);
534 void Widget::SetVisibilityChangedAnimationsEnabled(bool value) {
535 native_widget_->SetVisibilityChangedAnimationsEnabled(value);
538 void Widget::SetVisibilityAnimationDuration(const base::TimeDelta& duration) {
539 native_widget_->SetVisibilityAnimationDuration(duration);
542 void Widget::SetVisibilityAnimationTransition(VisibilityTransition transition) {
543 native_widget_->SetVisibilityAnimationTransition(transition);
546 Widget::MoveLoopResult Widget::RunMoveLoop(
547 const gfx::Vector2d& drag_offset,
548 MoveLoopSource source,
549 MoveLoopEscapeBehavior escape_behavior) {
550 return native_widget_->RunMoveLoop(drag_offset, source, escape_behavior);
553 void Widget::EndMoveLoop() {
554 native_widget_->EndMoveLoop();
557 void Widget::StackAboveWidget(Widget* widget) {
558 native_widget_->StackAbove(widget->GetNativeView());
561 void Widget::StackAbove(gfx::NativeView native_view) {
562 native_widget_->StackAbove(native_view);
565 void Widget::StackAtTop() {
566 native_widget_->StackAtTop();
569 void Widget::StackBelow(gfx::NativeView native_view) {
570 native_widget_->StackBelow(native_view);
573 void Widget::SetShape(SkRegion* shape) {
574 native_widget_->SetShape(shape);
577 void Widget::Close() {
578 if (widget_closed_) {
579 // It appears we can hit this code path if you close a modal dialog then
580 // close the last browser before the destructor is hit, which triggers
581 // invoking Close again.
582 return;
585 bool can_close = true;
586 if (non_client_view_)
587 can_close = non_client_view_->CanClose();
588 if (can_close) {
589 SaveWindowPlacement();
591 // During tear-down the top-level focus manager becomes unavailable to
592 // GTK tabbed panes and their children, so normal deregistration via
593 // |FormManager::ViewRemoved()| calls are fouled. We clear focus here
594 // to avoid these redundant steps and to avoid accessing deleted views
595 // that may have been in focus.
596 if (is_top_level() && focus_manager_.get())
597 focus_manager_->SetFocusedView(NULL);
599 FOR_EACH_OBSERVER(WidgetObserver, observers_, OnWidgetClosing(this));
600 native_widget_->Close();
601 widget_closed_ = true;
605 void Widget::CloseNow() {
606 FOR_EACH_OBSERVER(WidgetObserver, observers_, OnWidgetClosing(this));
607 native_widget_->CloseNow();
610 bool Widget::IsClosed() const {
611 return widget_closed_;
614 void Widget::Show() {
615 const ui::Layer* layer = GetLayer();
616 TRACE_EVENT1("views", "Widget::Show", "layer",
617 layer ? layer->name() : "none");
618 if (non_client_view_) {
619 // While initializing, the kiosk mode will go to full screen before the
620 // widget gets shown. In that case we stay in full screen mode, regardless
621 // of the |saved_show_state_| member.
622 if (saved_show_state_ == ui::SHOW_STATE_MAXIMIZED &&
623 !initial_restored_bounds_.IsEmpty() &&
624 !IsFullscreen()) {
625 native_widget_->ShowMaximizedWithBounds(initial_restored_bounds_);
626 } else {
627 native_widget_->ShowWithWindowState(
628 IsFullscreen() ? ui::SHOW_STATE_FULLSCREEN : saved_show_state_);
630 // |saved_show_state_| only applies the first time the window is shown.
631 // If we don't reset the value the window may be shown maximized every time
632 // it is subsequently shown after being hidden.
633 saved_show_state_ = ui::SHOW_STATE_NORMAL;
634 } else {
635 CanActivate()
636 ? native_widget_->Show()
637 : native_widget_->ShowWithWindowState(ui::SHOW_STATE_INACTIVE);
641 void Widget::Hide() {
642 native_widget_->Hide();
645 void Widget::ShowInactive() {
646 // If this gets called with saved_show_state_ == ui::SHOW_STATE_MAXIMIZED,
647 // call SetBounds()with the restored bounds to set the correct size. This
648 // normally should not happen, but if it does we should avoid showing unsized
649 // windows.
650 if (saved_show_state_ == ui::SHOW_STATE_MAXIMIZED &&
651 !initial_restored_bounds_.IsEmpty()) {
652 SetBounds(initial_restored_bounds_);
653 saved_show_state_ = ui::SHOW_STATE_NORMAL;
655 native_widget_->ShowWithWindowState(ui::SHOW_STATE_INACTIVE);
658 void Widget::Activate() {
659 native_widget_->Activate();
662 void Widget::Deactivate() {
663 native_widget_->Deactivate();
666 bool Widget::IsActive() const {
667 return native_widget_->IsActive();
670 void Widget::DisableInactiveRendering() {
671 SetInactiveRenderingDisabled(true);
674 void Widget::SetAlwaysOnTop(bool on_top) {
675 native_widget_->SetAlwaysOnTop(on_top);
678 bool Widget::IsAlwaysOnTop() const {
679 return native_widget_->IsAlwaysOnTop();
682 void Widget::SetVisibleOnAllWorkspaces(bool always_visible) {
683 native_widget_->SetVisibleOnAllWorkspaces(always_visible);
686 void Widget::Maximize() {
687 native_widget_->Maximize();
690 void Widget::Minimize() {
691 native_widget_->Minimize();
694 void Widget::Restore() {
695 native_widget_->Restore();
698 bool Widget::IsMaximized() const {
699 return native_widget_->IsMaximized();
702 bool Widget::IsMinimized() const {
703 return native_widget_->IsMinimized();
706 void Widget::SetFullscreen(bool fullscreen) {
707 if (IsFullscreen() == fullscreen)
708 return;
710 native_widget_->SetFullscreen(fullscreen);
712 if (non_client_view_)
713 non_client_view_->Layout();
716 bool Widget::IsFullscreen() const {
717 return native_widget_->IsFullscreen();
720 void Widget::SetOpacity(unsigned char opacity) {
721 native_widget_->SetOpacity(opacity);
724 void Widget::SetUseDragFrame(bool use_drag_frame) {
725 native_widget_->SetUseDragFrame(use_drag_frame);
728 void Widget::FlashFrame(bool flash) {
729 native_widget_->FlashFrame(flash);
732 View* Widget::GetRootView() {
733 return root_view_.get();
736 const View* Widget::GetRootView() const {
737 return root_view_.get();
740 bool Widget::IsVisible() const {
741 return native_widget_->IsVisible();
744 ui::ThemeProvider* Widget::GetThemeProvider() const {
745 const Widget* root_widget = GetTopLevelWidget();
746 if (root_widget && root_widget != this) {
747 // Attempt to get the theme provider, and fall back to the default theme
748 // provider if not found.
749 ui::ThemeProvider* provider = root_widget->GetThemeProvider();
750 if (provider)
751 return provider;
753 provider = root_widget->default_theme_provider_.get();
754 if (provider)
755 return provider;
757 return default_theme_provider_.get();
760 const ui::NativeTheme* Widget::GetNativeTheme() const {
761 return native_widget_->GetNativeTheme();
764 FocusManager* Widget::GetFocusManager() {
765 Widget* toplevel_widget = GetTopLevelWidget();
766 return toplevel_widget ? toplevel_widget->focus_manager_.get() : NULL;
769 const FocusManager* Widget::GetFocusManager() const {
770 const Widget* toplevel_widget = GetTopLevelWidget();
771 return toplevel_widget ? toplevel_widget->focus_manager_.get() : NULL;
774 ui::InputMethod* Widget::GetInputMethod() {
775 if (is_top_level()) {
776 // Only creates the shared the input method instance on top level widget.
777 return native_widget_private()->GetInputMethod();
778 } else {
779 Widget* toplevel = GetTopLevelWidget();
780 // If GetTopLevelWidget() returns itself which is not toplevel,
781 // the widget is detached from toplevel widget.
782 // TODO(oshima): Fix GetTopLevelWidget() to return NULL
783 // if there is no toplevel. We probably need to add GetTopMostWidget()
784 // to replace some use cases.
785 return (toplevel && toplevel != this) ? toplevel->GetInputMethod()
786 : nullptr;
790 void Widget::RunShellDrag(View* view,
791 const ui::OSExchangeData& data,
792 const gfx::Point& location,
793 int operation,
794 ui::DragDropTypes::DragEventSource source) {
795 dragged_view_ = view;
796 OnDragWillStart();
798 WidgetDeletionObserver widget_deletion_observer(this);
799 native_widget_->RunShellDrag(view, data, location, operation, source);
801 // The widget may be destroyed during the drag operation.
802 if (!widget_deletion_observer.IsWidgetAlive())
803 return;
805 // If the view is removed during the drag operation, dragged_view_ is set to
806 // NULL.
807 if (view && dragged_view_ == view) {
808 dragged_view_ = NULL;
809 view->OnDragDone();
811 OnDragComplete();
814 void Widget::SchedulePaintInRect(const gfx::Rect& rect) {
815 native_widget_->SchedulePaintInRect(rect);
818 void Widget::SetCursor(gfx::NativeCursor cursor) {
819 native_widget_->SetCursor(cursor);
822 bool Widget::IsMouseEventsEnabled() const {
823 return native_widget_->IsMouseEventsEnabled();
826 void Widget::SetNativeWindowProperty(const char* name, void* value) {
827 native_widget_->SetNativeWindowProperty(name, value);
830 void* Widget::GetNativeWindowProperty(const char* name) const {
831 return native_widget_->GetNativeWindowProperty(name);
834 void Widget::UpdateWindowTitle() {
835 if (!non_client_view_)
836 return;
838 // Update the native frame's text. We do this regardless of whether or not
839 // the native frame is being used, since this also updates the taskbar, etc.
840 base::string16 window_title = widget_delegate_->GetWindowTitle();
841 base::i18n::AdjustStringForLocaleDirection(&window_title);
842 if (!native_widget_->SetWindowTitle(window_title))
843 return;
844 non_client_view_->UpdateWindowTitle();
846 // If the non-client view is rendering its own title, it'll need to relayout
847 // now and to get a paint update later on.
848 non_client_view_->Layout();
851 void Widget::UpdateWindowIcon() {
852 if (non_client_view_)
853 non_client_view_->UpdateWindowIcon();
854 native_widget_->SetWindowIcons(widget_delegate_->GetWindowIcon(),
855 widget_delegate_->GetWindowAppIcon());
858 FocusTraversable* Widget::GetFocusTraversable() {
859 return static_cast<internal::RootView*>(root_view_.get());
862 void Widget::ThemeChanged() {
863 root_view_->ThemeChanged();
866 void Widget::LocaleChanged() {
867 root_view_->LocaleChanged();
870 void Widget::DeviceScaleFactorChanged(float device_scale_factor) {
871 root_view_->DeviceScaleFactorChanged(device_scale_factor);
874 void Widget::SetFocusTraversableParent(FocusTraversable* parent) {
875 root_view_->SetFocusTraversableParent(parent);
878 void Widget::SetFocusTraversableParentView(View* parent_view) {
879 root_view_->SetFocusTraversableParentView(parent_view);
882 void Widget::ClearNativeFocus() {
883 native_widget_->ClearNativeFocus();
886 NonClientFrameView* Widget::CreateNonClientFrameView() {
887 NonClientFrameView* frame_view =
888 widget_delegate_->CreateNonClientFrameView(this);
889 if (!frame_view)
890 frame_view = native_widget_->CreateNonClientFrameView();
891 if (!frame_view && ViewsDelegate::GetInstance()) {
892 frame_view =
893 ViewsDelegate::GetInstance()->CreateDefaultNonClientFrameView(this);
895 if (frame_view)
896 return frame_view;
898 CustomFrameView* custom_frame_view = new CustomFrameView;
899 custom_frame_view->Init(this);
900 return custom_frame_view;
903 bool Widget::ShouldUseNativeFrame() const {
904 if (frame_type_ != FRAME_TYPE_DEFAULT)
905 return frame_type_ == FRAME_TYPE_FORCE_NATIVE;
906 return native_widget_->ShouldUseNativeFrame();
909 bool Widget::ShouldWindowContentsBeTransparent() const {
910 return native_widget_->ShouldWindowContentsBeTransparent();
913 void Widget::DebugToggleFrameType() {
914 if (frame_type_ == FRAME_TYPE_DEFAULT) {
915 frame_type_ = ShouldUseNativeFrame() ? FRAME_TYPE_FORCE_CUSTOM :
916 FRAME_TYPE_FORCE_NATIVE;
917 } else {
918 frame_type_ = frame_type_ == FRAME_TYPE_FORCE_CUSTOM ?
919 FRAME_TYPE_FORCE_NATIVE : FRAME_TYPE_FORCE_CUSTOM;
921 FrameTypeChanged();
924 void Widget::FrameTypeChanged() {
925 native_widget_->FrameTypeChanged();
928 const ui::Compositor* Widget::GetCompositor() const {
929 return native_widget_->GetCompositor();
932 const ui::Layer* Widget::GetLayer() const {
933 return native_widget_->GetLayer();
936 void Widget::ReorderNativeViews() {
937 native_widget_->ReorderNativeViews();
940 void Widget::UpdateRootLayers() {
941 // Calculate the layers requires traversing the tree, and since nearly any
942 // mutation of the tree can trigger this call we delay until absolutely
943 // necessary.
944 root_layers_dirty_ = true;
947 const NativeWidget* Widget::native_widget() const {
948 return native_widget_;
951 NativeWidget* Widget::native_widget() {
952 return native_widget_;
955 void Widget::SetCapture(View* view) {
956 if (!native_widget_->HasCapture()) {
957 native_widget_->SetCapture();
959 // Early return if setting capture was unsuccessful.
960 if (!native_widget_->HasCapture())
961 return;
964 if (internal::NativeWidgetPrivate::IsMouseButtonDown())
965 is_mouse_button_pressed_ = true;
966 root_view_->SetMouseHandler(view);
969 void Widget::ReleaseCapture() {
970 if (native_widget_->HasCapture())
971 native_widget_->ReleaseCapture();
974 bool Widget::HasCapture() {
975 return native_widget_->HasCapture();
978 TooltipManager* Widget::GetTooltipManager() {
979 return native_widget_->GetTooltipManager();
982 const TooltipManager* Widget::GetTooltipManager() const {
983 return native_widget_->GetTooltipManager();
986 gfx::Rect Widget::GetWorkAreaBoundsInScreen() const {
987 return native_widget_->GetWorkAreaBoundsInScreen();
990 void Widget::SynthesizeMouseMoveEvent() {
991 last_mouse_event_was_move_ = false;
992 ui::MouseEvent mouse_event(ui::ET_MOUSE_MOVED, last_mouse_event_position_,
993 last_mouse_event_position_, ui::EventTimeForNow(),
994 ui::EF_IS_SYNTHESIZED, 0);
995 root_view_->OnMouseMoved(mouse_event);
998 void Widget::OnRootViewLayout() {
999 native_widget_->OnRootViewLayout();
1002 bool Widget::IsTranslucentWindowOpacitySupported() const {
1003 return native_widget_->IsTranslucentWindowOpacitySupported();
1006 void Widget::OnSizeConstraintsChanged() {
1007 native_widget_->OnSizeConstraintsChanged();
1008 non_client_view_->SizeConstraintsChanged();
1011 void Widget::OnOwnerClosing() {
1014 ////////////////////////////////////////////////////////////////////////////////
1015 // Widget, NativeWidgetDelegate implementation:
1017 bool Widget::IsModal() const {
1018 return widget_delegate_->GetModalType() != ui::MODAL_TYPE_NONE;
1021 bool Widget::IsDialogBox() const {
1022 return !!widget_delegate_->AsDialogDelegate();
1025 bool Widget::CanActivate() const {
1026 return widget_delegate_->CanActivate();
1029 bool Widget::IsInactiveRenderingDisabled() const {
1030 return disable_inactive_rendering_;
1033 void Widget::EnableInactiveRendering() {
1034 SetInactiveRenderingDisabled(false);
1037 void Widget::OnNativeWidgetActivationChanged(bool active) {
1038 // On windows we may end up here before we've completed initialization (from
1039 // an WM_NCACTIVATE). If that happens the WidgetDelegate likely doesn't know
1040 // the Widget and will crash attempting to access it.
1041 if (!active && native_widget_initialized_)
1042 SaveWindowPlacement();
1044 FOR_EACH_OBSERVER(WidgetObserver, observers_,
1045 OnWidgetActivationChanged(this, active));
1047 if (IsVisible() && non_client_view())
1048 non_client_view()->frame_view()->SchedulePaint();
1051 void Widget::OnNativeFocus() {
1052 WidgetFocusManager::GetInstance()->OnNativeFocusChanged(GetNativeView());
1055 void Widget::OnNativeBlur() {
1056 WidgetFocusManager::GetInstance()->OnNativeFocusChanged(nullptr);
1059 void Widget::OnNativeWidgetVisibilityChanging(bool visible) {
1060 FOR_EACH_OBSERVER(WidgetObserver, observers_,
1061 OnWidgetVisibilityChanging(this, visible));
1064 void Widget::OnNativeWidgetVisibilityChanged(bool visible) {
1065 View* root = GetRootView();
1066 if (root)
1067 root->PropagateVisibilityNotifications(root, visible);
1068 FOR_EACH_OBSERVER(WidgetObserver, observers_,
1069 OnWidgetVisibilityChanged(this, visible));
1070 if (GetCompositor() && root && root->layer())
1071 root->layer()->SetVisible(visible);
1074 void Widget::OnNativeWidgetCreated(bool desktop_widget) {
1075 if (is_top_level())
1076 focus_manager_.reset(FocusManagerFactory::Create(this, desktop_widget));
1078 native_widget_->InitModalType(widget_delegate_->GetModalType());
1080 FOR_EACH_OBSERVER(WidgetObserver, observers_, OnWidgetCreated(this));
1083 void Widget::OnNativeWidgetDestroying() {
1084 // Tell the focus manager (if any) that root_view is being removed
1085 // in case that the focused view is under this root view.
1086 if (GetFocusManager())
1087 GetFocusManager()->ViewRemoved(root_view_.get());
1088 FOR_EACH_OBSERVER(WidgetObserver, observers_, OnWidgetDestroying(this));
1089 if (non_client_view_)
1090 non_client_view_->WindowClosing();
1091 widget_delegate_->WindowClosing();
1094 void Widget::OnNativeWidgetDestroyed() {
1095 FOR_EACH_OBSERVER(WidgetObserver, observers_, OnWidgetDestroyed(this));
1096 widget_delegate_->DeleteDelegate();
1097 widget_delegate_ = NULL;
1098 native_widget_destroyed_ = true;
1101 gfx::Size Widget::GetMinimumSize() const {
1102 return non_client_view_ ? non_client_view_->GetMinimumSize() : gfx::Size();
1105 gfx::Size Widget::GetMaximumSize() const {
1106 return non_client_view_ ? non_client_view_->GetMaximumSize() : gfx::Size();
1109 void Widget::OnNativeWidgetMove() {
1110 widget_delegate_->OnWidgetMove();
1111 NotifyCaretBoundsChanged(GetInputMethod());
1113 FOR_EACH_OBSERVER(WidgetObserver, observers_, OnWidgetBoundsChanged(
1114 this,
1115 GetWindowBoundsInScreen()));
1118 void Widget::OnNativeWidgetSizeChanged(const gfx::Size& new_size) {
1119 View* root = GetRootView();
1120 if (root)
1121 root->SetSize(new_size);
1123 NotifyCaretBoundsChanged(GetInputMethod());
1124 SaveWindowPlacementIfInitialized();
1126 FOR_EACH_OBSERVER(WidgetObserver, observers_, OnWidgetBoundsChanged(
1127 this,
1128 GetWindowBoundsInScreen()));
1131 void Widget::OnNativeWidgetWindowShowStateChanged() {
1132 SaveWindowPlacementIfInitialized();
1135 void Widget::OnNativeWidgetBeginUserBoundsChange() {
1136 widget_delegate_->OnWindowBeginUserBoundsChange();
1139 void Widget::OnNativeWidgetEndUserBoundsChange() {
1140 widget_delegate_->OnWindowEndUserBoundsChange();
1143 bool Widget::HasFocusManager() const {
1144 return !!focus_manager_.get();
1147 void Widget::OnNativeWidgetPaint(const ui::PaintContext& context) {
1148 // On Linux Aura, we can get here during Init() because of the
1149 // SetInitialBounds call.
1150 if (!native_widget_initialized_)
1151 return;
1152 GetRootView()->Paint(context);
1155 int Widget::GetNonClientComponent(const gfx::Point& point) {
1156 int component = non_client_view_ ?
1157 non_client_view_->NonClientHitTest(point) :
1158 HTNOWHERE;
1160 if (movement_disabled_ && (component == HTCAPTION || component == HTSYSMENU))
1161 return HTNOWHERE;
1163 return component;
1166 void Widget::OnKeyEvent(ui::KeyEvent* event) {
1167 SendEventToProcessor(event);
1170 // TODO(tdanderson): We should not be calling the OnMouse*() functions on
1171 // RootView from anywhere in Widget. Use
1172 // SendEventToProcessor() instead. See crbug.com/348087.
1173 void Widget::OnMouseEvent(ui::MouseEvent* event) {
1174 View* root_view = GetRootView();
1175 switch (event->type()) {
1176 case ui::ET_MOUSE_PRESSED: {
1177 last_mouse_event_was_move_ = false;
1179 // We may get deleted by the time we return from OnMousePressed. So we
1180 // use an observer to make sure we are still alive.
1181 WidgetDeletionObserver widget_deletion_observer(this);
1183 // Make sure we're still visible before we attempt capture as the mouse
1184 // press processing may have made the window hide (as happens with menus).
1186 // It is possible for a View to show a context menu on mouse-press. Since
1187 // the menu does a capture and starts a nested message-loop, the release
1188 // would go to the menu. The next click (i.e. both mouse-press and release
1189 // events) also go to the menu. The menu (and the nested message-loop)
1190 // gets closed after this second release event. The code then resumes from
1191 // here. So make sure that the mouse-button is still down before doing a
1192 // capture.
1193 if (root_view && root_view->OnMousePressed(*event) &&
1194 widget_deletion_observer.IsWidgetAlive() && IsVisible() &&
1195 internal::NativeWidgetPrivate::IsMouseButtonDown()) {
1196 is_mouse_button_pressed_ = true;
1197 if (!native_widget_->HasCapture())
1198 native_widget_->SetCapture();
1199 event->SetHandled();
1201 return;
1204 case ui::ET_MOUSE_RELEASED:
1205 last_mouse_event_was_move_ = false;
1206 is_mouse_button_pressed_ = false;
1207 // Release capture first, to avoid confusion if OnMouseReleased blocks.
1208 if (auto_release_capture_ && native_widget_->HasCapture()) {
1209 base::AutoReset<bool> resetter(&ignore_capture_loss_, true);
1210 native_widget_->ReleaseCapture();
1212 if (root_view)
1213 root_view->OnMouseReleased(*event);
1214 if ((event->flags() & ui::EF_IS_NON_CLIENT) == 0)
1215 event->SetHandled();
1216 return;
1218 case ui::ET_MOUSE_MOVED:
1219 case ui::ET_MOUSE_DRAGGED:
1220 if (native_widget_->HasCapture() && is_mouse_button_pressed_) {
1221 last_mouse_event_was_move_ = false;
1222 if (root_view)
1223 root_view->OnMouseDragged(*event);
1224 } else if (!last_mouse_event_was_move_ ||
1225 last_mouse_event_position_ != event->location()) {
1226 last_mouse_event_position_ = event->location();
1227 last_mouse_event_was_move_ = true;
1228 if (root_view)
1229 root_view->OnMouseMoved(*event);
1231 return;
1233 case ui::ET_MOUSE_EXITED:
1234 last_mouse_event_was_move_ = false;
1235 if (root_view)
1236 root_view->OnMouseExited(*event);
1237 return;
1239 case ui::ET_MOUSEWHEEL:
1240 if (root_view && root_view->OnMouseWheel(
1241 static_cast<const ui::MouseWheelEvent&>(*event)))
1242 event->SetHandled();
1243 return;
1245 default:
1246 return;
1250 void Widget::OnMouseCaptureLost() {
1251 if (ignore_capture_loss_)
1252 return;
1254 View* root_view = GetRootView();
1255 if (root_view)
1256 root_view->OnMouseCaptureLost();
1257 is_mouse_button_pressed_ = false;
1260 void Widget::OnScrollEvent(ui::ScrollEvent* event) {
1261 ui::ScrollEvent event_copy(*event);
1262 SendEventToProcessor(&event_copy);
1264 // Convert unhandled ui::ET_SCROLL events into ui::ET_MOUSEWHEEL events.
1265 if (!event_copy.handled() && event_copy.type() == ui::ET_SCROLL) {
1266 ui::MouseWheelEvent wheel(*event);
1267 OnMouseEvent(&wheel);
1271 void Widget::OnGestureEvent(ui::GestureEvent* event) {
1272 // We explicitly do not capture here. Not capturing enables multiple widgets
1273 // to get tap events at the same time. Views (such as tab dragging) may
1274 // explicitly capture.
1275 SendEventToProcessor(event);
1278 bool Widget::ExecuteCommand(int command_id) {
1279 return widget_delegate_->ExecuteWindowsCommand(command_id);
1282 const std::vector<ui::Layer*>& Widget::GetRootLayers() {
1283 if (root_layers_dirty_) {
1284 root_layers_dirty_ = false;
1285 root_layers_.clear();
1286 BuildRootLayers(GetRootView(), &root_layers_);
1288 return root_layers_;
1291 bool Widget::HasHitTestMask() const {
1292 return widget_delegate_->WidgetHasHitTestMask();
1295 void Widget::GetHitTestMask(gfx::Path* mask) const {
1296 DCHECK(mask);
1297 widget_delegate_->GetWidgetHitTestMask(mask);
1300 Widget* Widget::AsWidget() {
1301 return this;
1304 const Widget* Widget::AsWidget() const {
1305 return this;
1308 bool Widget::SetInitialFocus(ui::WindowShowState show_state) {
1309 View* v = widget_delegate_->GetInitiallyFocusedView();
1310 if (!focus_on_creation_ || show_state == ui::SHOW_STATE_INACTIVE ||
1311 show_state == ui::SHOW_STATE_MINIMIZED) {
1312 // If not focusing the window now, tell the focus manager which view to
1313 // focus when the window is restored.
1314 if (v)
1315 focus_manager_->SetStoredFocusView(v);
1316 return true;
1318 if (v)
1319 v->RequestFocus();
1320 return !!v;
1323 ////////////////////////////////////////////////////////////////////////////////
1324 // Widget, ui::EventSource implementation:
1325 ui::EventProcessor* Widget::GetEventProcessor() {
1326 return root_view_.get();
1329 ////////////////////////////////////////////////////////////////////////////////
1330 // Widget, FocusTraversable implementation:
1332 FocusSearch* Widget::GetFocusSearch() {
1333 return root_view_->GetFocusSearch();
1336 FocusTraversable* Widget::GetFocusTraversableParent() {
1337 // We are a proxy to the root view, so we should be bypassed when traversing
1338 // up and as a result this should not be called.
1339 NOTREACHED();
1340 return NULL;
1343 View* Widget::GetFocusTraversableParentView() {
1344 // We are a proxy to the root view, so we should be bypassed when traversing
1345 // up and as a result this should not be called.
1346 NOTREACHED();
1347 return NULL;
1350 ////////////////////////////////////////////////////////////////////////////////
1351 // Widget, ui::NativeThemeObserver implementation:
1353 void Widget::OnNativeThemeUpdated(ui::NativeTheme* observed_theme) {
1354 DCHECK(observer_manager_.IsObserving(observed_theme));
1356 ui::NativeTheme* current_native_theme = GetNativeTheme();
1357 if (!observer_manager_.IsObserving(current_native_theme)) {
1358 observer_manager_.RemoveAll();
1359 observer_manager_.Add(current_native_theme);
1362 root_view_->PropagateNativeThemeChanged(current_native_theme);
1365 ////////////////////////////////////////////////////////////////////////////////
1366 // Widget, protected:
1368 internal::RootView* Widget::CreateRootView() {
1369 return new internal::RootView(this);
1372 void Widget::DestroyRootView() {
1373 non_client_view_ = NULL;
1374 root_view_.reset();
1377 void Widget::OnDragWillStart() {
1380 void Widget::OnDragComplete() {
1383 ////////////////////////////////////////////////////////////////////////////////
1384 // Widget, private:
1386 void Widget::SetInactiveRenderingDisabled(bool value) {
1387 if (value == disable_inactive_rendering_)
1388 return;
1390 disable_inactive_rendering_ = value;
1391 if (non_client_view_)
1392 non_client_view_->SetInactiveRenderingDisabled(value);
1395 void Widget::SaveWindowPlacement() {
1396 // The window delegate does the actual saving for us. It seems like (judging
1397 // by go/crash) that in some circumstances we can end up here after
1398 // WM_DESTROY, at which point the window delegate is likely gone. So just
1399 // bail.
1400 if (!widget_delegate_)
1401 return;
1403 ui::WindowShowState show_state = ui::SHOW_STATE_NORMAL;
1404 gfx::Rect bounds;
1405 native_widget_->GetWindowPlacement(&bounds, &show_state);
1406 widget_delegate_->SaveWindowPlacement(bounds, show_state);
1409 void Widget::SaveWindowPlacementIfInitialized() {
1410 if (native_widget_initialized_)
1411 SaveWindowPlacement();
1414 void Widget::SetInitialBounds(const gfx::Rect& bounds) {
1415 if (!non_client_view_)
1416 return;
1418 gfx::Rect saved_bounds;
1419 if (GetSavedWindowPlacement(&saved_bounds, &saved_show_state_)) {
1420 if (saved_show_state_ == ui::SHOW_STATE_MAXIMIZED) {
1421 // If we're going to maximize, wait until Show is invoked to set the
1422 // bounds. That way we avoid a noticeable resize.
1423 initial_restored_bounds_ = saved_bounds;
1424 } else if (!saved_bounds.IsEmpty()) {
1425 // If the saved bounds are valid, use them.
1426 SetBounds(saved_bounds);
1428 } else {
1429 if (bounds.IsEmpty()) {
1430 // No initial bounds supplied, so size the window to its content and
1431 // center over its parent.
1432 native_widget_->CenterWindow(non_client_view_->GetPreferredSize());
1433 } else {
1434 // Use the supplied initial bounds.
1435 SetBoundsConstrained(bounds);
1440 void Widget::SetInitialBoundsForFramelessWindow(const gfx::Rect& bounds) {
1441 if (bounds.IsEmpty()) {
1442 View* contents_view = GetContentsView();
1443 DCHECK(contents_view);
1444 // No initial bounds supplied, so size the window to its content and
1445 // center over its parent if preferred size is provided.
1446 gfx::Size size = contents_view->GetPreferredSize();
1447 if (!size.IsEmpty())
1448 native_widget_->CenterWindow(size);
1449 } else {
1450 // Use the supplied initial bounds.
1451 SetBoundsConstrained(bounds);
1455 bool Widget::GetSavedWindowPlacement(gfx::Rect* bounds,
1456 ui::WindowShowState* show_state) {
1457 // First we obtain the window's saved show-style and store it. We need to do
1458 // this here, rather than in Show() because by the time Show() is called,
1459 // the window's size will have been reset (below) and the saved maximized
1460 // state will have been lost. Sadly there's no way to tell on Windows when
1461 // a window is restored from maximized state, so we can't more accurately
1462 // track maximized state independently of sizing information.
1464 // Restore the window's placement from the controller.
1465 if (widget_delegate_->GetSavedWindowPlacement(this, bounds, show_state)) {
1466 if (!widget_delegate_->ShouldRestoreWindowSize()) {
1467 bounds->set_size(non_client_view_->GetPreferredSize());
1468 } else {
1469 gfx::Size minimum_size = GetMinimumSize();
1470 // Make sure the bounds are at least the minimum size.
1471 if (bounds->width() < minimum_size.width())
1472 bounds->set_width(minimum_size.width());
1474 if (bounds->height() < minimum_size.height())
1475 bounds->set_height(minimum_size.height());
1477 return true;
1479 return false;
1482 namespace internal {
1484 ////////////////////////////////////////////////////////////////////////////////
1485 // internal::NativeWidgetPrivate, NativeWidget implementation:
1487 internal::NativeWidgetPrivate* NativeWidgetPrivate::AsNativeWidgetPrivate() {
1488 return this;
1491 } // namespace internal
1492 } // namespace views