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 "chrome/browser/ui/views/importer/import_lock_dialog_view.h" | 5 #include "chrome/browser/ui/views/importer/import_lock_dialog_view.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/metrics/user_metrics.h" | 9 #include "base/metrics/user_metrics.h" |
10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
12 #include "base/threading/thread_task_runner_handle.h" | 12 #include "base/threading/thread_task_runner_handle.h" |
13 #include "chrome/browser/importer/importer_lock_dialog.h" | 13 #include "chrome/browser/importer/importer_lock_dialog.h" |
14 #include "chrome/browser/ui/views/harmony/layout_delegate.h" | 14 #include "chrome/browser/ui/views/harmony/layout_delegate.h" |
15 #include "chrome/grit/chromium_strings.h" | 15 #include "chrome/grit/chromium_strings.h" |
16 #include "chrome/grit/generated_resources.h" | 16 #include "chrome/grit/generated_resources.h" |
17 #include "chrome/grit/locale_settings.h" | 17 #include "chrome/grit/locale_settings.h" |
18 #include "ui/base/l10n/l10n_util.h" | 18 #include "ui/base/l10n/l10n_util.h" |
19 #include "ui/views/controls/label.h" | 19 #include "ui/views/controls/label.h" |
20 #include "ui/views/layout/layout_constants.h" | 20 #include "ui/views/layout/layout_constants.h" |
21 #include "ui/views/widget/widget.h" | 21 #include "ui/views/widget/widget.h" |
22 | 22 |
23 using base::UserMetricsAction; | 23 using base::UserMetricsAction; |
24 | 24 |
| 25 namespace { |
| 26 |
| 27 // A reference to the currently active widget backing the |
| 28 // import lock dialog, or null if the dialog is not open. |
| 29 static views::Widget* g_widget = nullptr; |
| 30 |
| 31 } |
| 32 |
25 namespace importer { | 33 namespace importer { |
26 | 34 |
27 void ShowImportLockDialog(gfx::NativeWindow parent, | 35 void ShowImportLockDialog(gfx::NativeWindow parent, |
28 const base::Callback<void(bool)>& callback) { | 36 const base::Callback<void(bool)>& callback) { |
29 ImportLockDialogView::Show(parent, callback); | 37 ImportLockDialogView::Show(parent, callback); |
30 base::RecordAction(UserMetricsAction("ImportLockDialogView_Shown")); | 38 base::RecordAction(UserMetricsAction("ImportLockDialogView_Shown")); |
31 } | 39 } |
32 | 40 |
| 41 void HideImportLockDialog() { |
| 42 ImportLockDialogView::Hide(); |
| 43 } |
| 44 |
33 } // namespace importer | 45 } // namespace importer |
34 | 46 |
35 // static | 47 // static |
36 void ImportLockDialogView::Show(gfx::NativeWindow parent, | 48 void ImportLockDialogView::Show(gfx::NativeWindow parent, |
37 const base::Callback<void(bool)>& callback) { | 49 const base::Callback<void(bool)>& callback) { |
38 views::DialogDelegate::CreateDialogWidget( | 50 ImportLockDialogView* dialog_view = new ImportLockDialogView(callback); |
39 new ImportLockDialogView(callback), NULL, NULL)->Show(); | 51 g_widget = views::DialogDelegate::CreateDialogWidget(dialog_view, NULL, NULL); |
| 52 g_widget->AddObserver(dialog_view); |
| 53 g_widget->Show(); |
| 54 } |
| 55 |
| 56 // static |
| 57 void ImportLockDialogView::Hide() { |
| 58 if (g_widget) |
| 59 g_widget->Close(); |
40 } | 60 } |
41 | 61 |
42 ImportLockDialogView::ImportLockDialogView( | 62 ImportLockDialogView::ImportLockDialogView( |
43 const base::Callback<void(bool)>& callback) | 63 const base::Callback<void(bool)>& callback) |
44 : description_label_(NULL), | 64 : description_label_(NULL), |
45 callback_(callback) { | 65 callback_(callback) { |
46 description_label_ = new views::Label( | 66 description_label_ = new views::Label( |
47 l10n_util::GetStringUTF16(IDS_IMPORTER_LOCK_TEXT)); | 67 l10n_util::GetStringUTF16(IDS_IMPORTER_LOCK_TEXT)); |
48 description_label_->SetMultiLine(true); | 68 description_label_->SetMultiLine(true); |
49 description_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 69 description_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
50 AddChildView(description_label_); | 70 AddChildView(description_label_); |
51 } | 71 } |
52 | 72 |
53 ImportLockDialogView::~ImportLockDialogView() { | 73 ImportLockDialogView::~ImportLockDialogView() { |
54 } | 74 } |
55 | 75 |
| 76 void ImportLockDialogView::OnWidgetDestroying(views::Widget* widget) { |
| 77 DCHECK_EQ(widget, g_widget); |
| 78 g_widget = nullptr; |
| 79 widget->RemoveObserver(this); |
| 80 } |
| 81 |
56 gfx::Size ImportLockDialogView::GetPreferredSize() const { | 82 gfx::Size ImportLockDialogView::GetPreferredSize() const { |
57 return gfx::Size(views::Widget::GetLocalizedContentsSize( | 83 return gfx::Size(views::Widget::GetLocalizedContentsSize( |
58 IDS_IMPORTLOCK_DIALOG_WIDTH_CHARS, | 84 IDS_IMPORTLOCK_DIALOG_WIDTH_CHARS, |
59 IDS_IMPORTLOCK_DIALOG_HEIGHT_LINES)); | 85 IDS_IMPORTLOCK_DIALOG_HEIGHT_LINES)); |
60 } | 86 } |
61 | 87 |
62 void ImportLockDialogView::Layout() { | 88 void ImportLockDialogView::Layout() { |
63 gfx::Rect bounds(GetLocalBounds()); | 89 gfx::Rect bounds(GetLocalBounds()); |
64 bounds.Inset(views::kButtonHEdgeMarginNew, | 90 bounds.Inset(views::kButtonHEdgeMarginNew, |
65 LayoutDelegate::Get()->GetMetric( | 91 LayoutDelegate::Get()->GetMetric( |
(...skipping 15 matching lines...) Expand all Loading... |
81 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 107 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
82 base::Bind(callback_, true)); | 108 base::Bind(callback_, true)); |
83 return true; | 109 return true; |
84 } | 110 } |
85 | 111 |
86 bool ImportLockDialogView::Cancel() { | 112 bool ImportLockDialogView::Cancel() { |
87 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 113 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
88 base::Bind(callback_, false)); | 114 base::Bind(callback_, false)); |
89 return true; | 115 return true; |
90 } | 116 } |
OLD | NEW |