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 } |
| 78 label_->SetText(l10n_util::GetStringUTF16(message_id)); |
| 79 label_->SizeToFit(kDialogMessageWidthPixel); |
| 80 Layout(); |
| 81 SchedulePaint(); |
| 82 } |
| 83 |
| 84 DisplayErrorDialog::DisplayErrorDialog(chromeos::OutputState new_state) { |
68 Shell::GetInstance()->display_controller()->AddObserver(this); | 85 Shell::GetInstance()->display_controller()->AddObserver(this); |
69 label_ = new views::Label( | 86 label_ = new views::Label(); |
70 l10n_util::GetStringUTF16(IDS_ASH_DISPLAY_FAILURE_ON_MIRRORING)); | |
71 AddChildView(label_); | 87 AddChildView(label_); |
72 | 88 |
73 label_->SetMultiLine(true); | 89 label_->SetMultiLine(true); |
74 label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 90 label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
75 label_->set_border(views::Border::CreateEmptyBorder( | 91 label_->set_border(views::Border::CreateEmptyBorder( |
76 kDialogMessageMarginWidthPixel, | 92 kDialogMessageMarginWidthPixel, |
77 kDialogMessageMarginWidthPixel, | 93 kDialogMessageMarginWidthPixel, |
78 kDialogMessageMarginWidthPixel, | 94 kDialogMessageMarginWidthPixel, |
79 kDialogMessageMarginWidthPixel)); | 95 kDialogMessageMarginWidthPixel)); |
80 label_->SizeToFit(kDialogMessageWidthPixel); | 96 |
| 97 UpdateMessageForState(new_state); |
81 } | 98 } |
82 | 99 |
83 DisplayErrorDialog::~DisplayErrorDialog() { | 100 DisplayErrorDialog::~DisplayErrorDialog() { |
84 Shell::GetInstance()->display_controller()->RemoveObserver(this); | 101 Shell::GetInstance()->display_controller()->RemoveObserver(this); |
85 g_instance = NULL; | |
86 } | 102 } |
87 | 103 |
88 int DisplayErrorDialog::GetDialogButtons() const { | 104 int DisplayErrorDialog::GetDialogButtons() const { |
89 return ui::DIALOG_BUTTON_OK; | 105 return ui::DIALOG_BUTTON_OK; |
90 } | 106 } |
91 | 107 |
92 ui::ModalType DisplayErrorDialog::GetModalType() const { | 108 ui::ModalType DisplayErrorDialog::GetModalType() const { |
93 return ui::MODAL_TYPE_NONE; | 109 return ui::MODAL_TYPE_NONE; |
94 } | 110 } |
95 | 111 |
96 gfx::Size DisplayErrorDialog::GetPreferredSize() { | 112 gfx::Size DisplayErrorDialog::GetPreferredSize() { |
97 return label_->GetPreferredSize(); | 113 return label_->GetPreferredSize(); |
98 } | 114 } |
99 | 115 |
100 void DisplayErrorDialog::OnDisplayConfigurationChanging() { | 116 void DisplayErrorDialog::OnDisplayConfigurationChanging() { |
101 GetWidget()->Close(); | 117 GetWidget()->Close(); |
102 } | 118 } |
103 | 119 |
104 // static | 120 DisplayErrorObserver::DisplayErrorObserver() |
105 DisplayErrorDialog* DisplayErrorDialog::GetInstanceForTest() { | 121 : dialog_(NULL) { |
106 return g_instance; | 122 } |
| 123 |
| 124 DisplayErrorObserver::~DisplayErrorObserver() { |
| 125 DCHECK(!dialog_); |
| 126 } |
| 127 |
| 128 void DisplayErrorObserver::OnDisplayModeChangeFailed( |
| 129 chromeos::OutputState new_state) { |
| 130 if (dialog_) { |
| 131 DCHECK(dialog_->GetWidget()); |
| 132 dialog_->UpdateMessageForState(new_state); |
| 133 dialog_->GetWidget()->StackAtTop(); |
| 134 dialog_->GetWidget()->Activate(); |
| 135 } else { |
| 136 dialog_ = DisplayErrorDialog::ShowDialog(new_state); |
| 137 dialog_->GetWidget()->AddObserver(this); |
| 138 } |
| 139 } |
| 140 |
| 141 void DisplayErrorObserver::OnWidgetClosing(views::Widget* widget) { |
| 142 DCHECK(dialog_); |
| 143 DCHECK_EQ(dialog_->GetWidget(), widget); |
| 144 widget->RemoveObserver(this); |
| 145 dialog_ = NULL; |
107 } | 146 } |
108 | 147 |
109 } // namespace internal | 148 } // namespace internal |
110 } // namespace ash | 149 } // namespace ash |
OLD | NEW |