| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ash/display/display_error_dialog.h" | 5 #include "ash/display/display_error_dialog.h" |
| 6 | 6 |
| 7 #include "ash/screen_ash.h" | 7 #include "ash/screen_ash.h" |
| 8 #include "ash/shell.h" | 8 #include "ash/shell.h" |
| 9 #include "grit/ash_strings.h" | 9 #include "grit/ash_strings.h" |
| 10 #include "ui/aura/root_window.h" | 10 #include "ui/aura/root_window.h" |
| 11 #include "ui/aura/window.h" | 11 #include "ui/aura/window.h" |
| 12 #include "ui/base/l10n/l10n_util.h" | 12 #include "ui/base/l10n/l10n_util.h" |
| 13 #include "ui/base/ui_base_types.h" | 13 #include "ui/base/ui_base_types.h" |
| 14 #include "ui/gfx/display.h" | 14 #include "ui/gfx/display.h" |
| 15 #include "ui/gfx/screen.h" | 15 #include "ui/gfx/screen.h" |
| 16 #include "ui/views/border.h" | 16 #include "ui/views/border.h" |
| 17 #include "ui/views/controls/label.h" | 17 #include "ui/views/controls/label.h" |
| 18 #include "ui/views/widget/widget.h" | 18 #include "ui/views/widget/widget.h" |
| 19 | 19 |
| 20 namespace ash { | 20 namespace ash { |
| 21 namespace internal { | 21 namespace internal { |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 // The width of the area to show the error message. | 24 // The width of the area to show the error message. |
| 25 const int kDialogMessageWidthPixel = 300; | 25 const int kDialogMessageWidthPixel = 300; |
| 26 | 26 |
| 27 // The margin width from the error message to the edge of the dialog. | 27 // The margin width from the error message to the edge of the dialog. |
| 28 const int kDialogMessageMarginWidthPixel = 5; | 28 const int kDialogMessageMarginWidthPixel = 5; |
| 29 | 29 |
| 30 DisplayErrorDialog* g_instance = NULL; | |
| 31 | |
| 32 } // namespace | 30 } // namespace |
| 33 | 31 |
| 34 // static | 32 // static |
| 35 void DisplayErrorDialog::ShowDialog() { | 33 DisplayErrorDialog* DisplayErrorDialog::ShowDialog( |
| 36 if (g_instance) { | 34 chromeos::OutputState new_state) { |
| 37 DCHECK(g_instance->GetWidget()); | |
| 38 g_instance->GetWidget()->StackAtTop(); | |
| 39 g_instance->GetWidget()->Activate(); | |
| 40 return; | |
| 41 } | |
| 42 | |
| 43 gfx::Screen* screen = Shell::GetScreen(); | 35 gfx::Screen* screen = Shell::GetScreen(); |
| 44 const gfx::Display& target_display = | 36 const gfx::Display& target_display = |
| 45 (screen->GetNumDisplays() > 1) ? | 37 (screen->GetNumDisplays() > 1) ? |
| 46 ScreenAsh::GetSecondaryDisplay() : screen->GetPrimaryDisplay(); | 38 ScreenAsh::GetSecondaryDisplay() : screen->GetPrimaryDisplay(); |
| 47 | 39 |
| 48 g_instance = new DisplayErrorDialog(); | 40 DisplayErrorDialog* dialog = new DisplayErrorDialog(new_state); |
| 49 views::Widget* widget = new views::Widget; | 41 views::Widget* widget = new views::Widget; |
| 50 views::Widget::InitParams params(views::Widget::InitParams::TYPE_WINDOW); | 42 views::Widget::InitParams params(views::Widget::InitParams::TYPE_WINDOW); |
| 51 params.delegate = g_instance; | 43 params.delegate = dialog; |
| 52 // Makes |widget| belong to the target display. Size and location are | 44 // Makes |widget| belong to the target display. Size and location are |
| 53 // fixed by CenterWindow() below. | 45 // fixed by CenterWindow() below. |
| 54 params.bounds = target_display.bounds(); | 46 params.bounds = target_display.bounds(); |
| 55 DisplayController* display_controller = | 47 DisplayController* display_controller = |
| 56 Shell::GetInstance()->display_controller(); | 48 Shell::GetInstance()->display_controller(); |
| 57 params.context = | 49 params.context = |
| 58 display_controller->GetRootWindowForDisplayId(target_display.id()); | 50 display_controller->GetRootWindowForDisplayId(target_display.id()); |
| 59 params.keep_on_top = true; | 51 params.keep_on_top = true; |
| 60 widget->Init(params); | 52 widget->Init(params); |
| 61 | 53 |
| 62 widget->GetNativeView()->SetName("DisplayErrorDialog"); | 54 widget->GetNativeView()->SetName("DisplayErrorDialog"); |
| 63 widget->CenterWindow(widget->GetRootView()->GetPreferredSize()); | 55 widget->CenterWindow(widget->GetRootView()->GetPreferredSize()); |
| 64 widget->Show(); | 56 widget->Show(); |
| 57 return dialog; |
| 65 } | 58 } |
| 66 | 59 |
| 67 DisplayErrorDialog::DisplayErrorDialog() { | 60 void DisplayErrorDialog::UpdateMessageForState( |
| 61 chromeos::OutputState new_state) { |
| 62 int message_id = -1; |
| 63 switch (new_state) { |
| 64 case chromeos::STATE_DUAL_MIRROR: |
| 65 message_id = IDS_ASH_DISPLAY_FAILURE_ON_MIRRORING; |
| 66 break; |
| 67 case chromeos::STATE_DUAL_EXTENDED: |
| 68 message_id = IDS_ASH_DISPLAY_FAILURE_ON_EXTENDED; |
| 69 break; |
| 70 default: |
| 71 // The error dialog would appear only for mirroring or extended. |
| 72 // It's quite unlikely to happen with other status (single-display / |
| 73 // invalid / unknown status), but set an unknown error message |
| 74 // instead of NOTREACHED() just in case for safety. |
| 75 LOG(ERROR) << "Unexpected failure for new state: " << new_state; |
| 76 message_id = IDS_ASH_DISPLAY_FAILURE_UNKNOWN; |
| 77 break; |
| 78 } |
| 79 label_->SetText(l10n_util::GetStringUTF16(message_id)); |
| 80 label_->SizeToFit(kDialogMessageWidthPixel); |
| 81 Layout(); |
| 82 SchedulePaint(); |
| 83 } |
| 84 |
| 85 DisplayErrorDialog::DisplayErrorDialog(chromeos::OutputState new_state) { |
| 68 Shell::GetInstance()->display_controller()->AddObserver(this); | 86 Shell::GetInstance()->display_controller()->AddObserver(this); |
| 69 label_ = new views::Label( | 87 label_ = new views::Label(); |
| 70 l10n_util::GetStringUTF16(IDS_ASH_DISPLAY_FAILURE_ON_MIRRORING)); | |
| 71 AddChildView(label_); | 88 AddChildView(label_); |
| 72 | 89 |
| 73 label_->SetMultiLine(true); | 90 label_->SetMultiLine(true); |
| 74 label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 91 label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 75 label_->set_border(views::Border::CreateEmptyBorder( | 92 label_->set_border(views::Border::CreateEmptyBorder( |
| 76 kDialogMessageMarginWidthPixel, | 93 kDialogMessageMarginWidthPixel, |
| 77 kDialogMessageMarginWidthPixel, | 94 kDialogMessageMarginWidthPixel, |
| 78 kDialogMessageMarginWidthPixel, | 95 kDialogMessageMarginWidthPixel, |
| 79 kDialogMessageMarginWidthPixel)); | 96 kDialogMessageMarginWidthPixel)); |
| 80 label_->SizeToFit(kDialogMessageWidthPixel); | 97 |
| 98 UpdateMessageForState(new_state); |
| 81 } | 99 } |
| 82 | 100 |
| 83 DisplayErrorDialog::~DisplayErrorDialog() { | 101 DisplayErrorDialog::~DisplayErrorDialog() { |
| 84 Shell::GetInstance()->display_controller()->RemoveObserver(this); | 102 Shell::GetInstance()->display_controller()->RemoveObserver(this); |
| 85 g_instance = NULL; | |
| 86 } | 103 } |
| 87 | 104 |
| 88 int DisplayErrorDialog::GetDialogButtons() const { | 105 int DisplayErrorDialog::GetDialogButtons() const { |
| 89 return ui::DIALOG_BUTTON_OK; | 106 return ui::DIALOG_BUTTON_OK; |
| 90 } | 107 } |
| 91 | 108 |
| 92 ui::ModalType DisplayErrorDialog::GetModalType() const { | 109 ui::ModalType DisplayErrorDialog::GetModalType() const { |
| 93 return ui::MODAL_TYPE_NONE; | 110 return ui::MODAL_TYPE_NONE; |
| 94 } | 111 } |
| 95 | 112 |
| 96 gfx::Size DisplayErrorDialog::GetPreferredSize() { | 113 gfx::Size DisplayErrorDialog::GetPreferredSize() { |
| 97 return label_->GetPreferredSize(); | 114 return label_->GetPreferredSize(); |
| 98 } | 115 } |
| 99 | 116 |
| 100 void DisplayErrorDialog::OnDisplayConfigurationChanging() { | 117 void DisplayErrorDialog::OnDisplayConfigurationChanging() { |
| 101 GetWidget()->Close(); | 118 GetWidget()->Close(); |
| 102 } | 119 } |
| 103 | 120 |
| 104 // static | 121 DisplayErrorObserver::DisplayErrorObserver() |
| 105 DisplayErrorDialog* DisplayErrorDialog::GetInstanceForTest() { | 122 : dialog_(NULL) { |
| 106 return g_instance; | 123 } |
| 124 |
| 125 DisplayErrorObserver::~DisplayErrorObserver() { |
| 126 DCHECK(!dialog_); |
| 127 } |
| 128 |
| 129 void DisplayErrorObserver::OnDisplayModeChangeFailed( |
| 130 chromeos::OutputState new_state) { |
| 131 if (dialog_) { |
| 132 DCHECK(dialog_->GetWidget()); |
| 133 dialog_->UpdateMessageForState(new_state); |
| 134 dialog_->GetWidget()->StackAtTop(); |
| 135 dialog_->GetWidget()->Activate(); |
| 136 } else { |
| 137 dialog_ = DisplayErrorDialog::ShowDialog(new_state); |
| 138 dialog_->GetWidget()->AddObserver(this); |
| 139 } |
| 140 } |
| 141 |
| 142 void DisplayErrorObserver::OnWidgetClosing(views::Widget* widget) { |
| 143 DCHECK(dialog_); |
| 144 DCHECK_EQ(dialog_->GetWidget(), widget); |
| 145 widget->RemoveObserver(this); |
| 146 dialog_ = NULL; |
| 107 } | 147 } |
| 108 | 148 |
| 109 } // namespace internal | 149 } // namespace internal |
| 110 } // namespace ash | 150 } // namespace ash |
| OLD | NEW |