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