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 "base/i18n/rtl.h" | |
6 #include "base/strings/utf_string_conversions.h" | |
7 #include "chrome/browser/extensions/bundle_installer.h" | 5 #include "chrome/browser/extensions/bundle_installer.h" |
8 #include "chrome/browser/ui/browser.h" | 6 #include "chrome/browser/ui/browser.h" |
9 #include "chrome/browser/ui/views/frame/browser_view.h" | 7 #include "chrome/browser/ui/views/frame/browser_view.h" |
10 #include "chrome/browser/ui/views/toolbar/toolbar_view.h" | 8 #include "chrome/browser/ui/views/toolbar/toolbar_view.h" |
11 #include "chrome/browser/ui/views/toolbar/wrench_toolbar_button.h" | 9 #include "chrome/browser/ui/views/toolbar/wrench_toolbar_button.h" |
12 #include "chrome/grit/generated_resources.h" | |
13 #include "ui/base/l10n/l10n_util.h" | |
14 #include "ui/base/resource/resource_bundle.h" | 10 #include "ui/base/resource/resource_bundle.h" |
15 #include "ui/resources/grit/ui_resources.h" | 11 #include "ui/resources/grit/ui_resources.h" |
16 #include "ui/views/bubble/bubble_delegate.h" | 12 #include "ui/views/bubble/bubble_delegate.h" |
17 #include "ui/views/controls/button/image_button.h" | 13 #include "ui/views/controls/button/image_button.h" |
18 #include "ui/views/controls/image_view.h" | |
19 #include "ui/views/controls/label.h" | 14 #include "ui/views/controls/label.h" |
20 #include "ui/views/layout/grid_layout.h" | 15 #include "ui/views/layout/grid_layout.h" |
21 #include "ui/views/layout/layout_constants.h" | 16 #include "ui/views/layout/layout_constants.h" |
22 | 17 |
23 using extensions::BundleInstaller; | 18 using extensions::BundleInstaller; |
24 using views::GridLayout; | 19 using views::GridLayout; |
25 | 20 |
26 namespace { | 21 namespace { |
27 | 22 |
28 // The ID of the column set for the bubble. | 23 // The ID of the column set that holds the headings and the close button. |
29 const int kColumnSetId = 0; | 24 const int kHeadingColumnSetId = 0; |
30 | 25 |
31 // The width of the left column. | 26 // The width of the columns that hold the heading texts and extension names. |
32 const int kLeftColumnWidth = 325; | 27 const int kTextColumnWidth = 325; |
| 28 |
| 29 // The ID of the column set that holds extension icons and names. |
| 30 const int kItemsColumnSetId = 1; |
| 31 |
| 32 // The size of extension icons, and width of the corresponding column. |
| 33 const int kIconSize = 32; |
33 | 34 |
34 class BundleInstalledBubble : public views::BubbleDelegateView, | 35 class BundleInstalledBubble : public views::BubbleDelegateView, |
35 public views::ButtonListener { | 36 public views::ButtonListener { |
36 public: | 37 public: |
37 BundleInstalledBubble(const BundleInstaller* bundle, | 38 BundleInstalledBubble(const BundleInstaller* bundle, |
38 View* anchor_view, | 39 View* anchor_view, |
39 views::BubbleBorder::Arrow arrow) | 40 views::BubbleBorder::Arrow arrow) |
40 : views::BubbleDelegateView(anchor_view, arrow) { | 41 : views::BubbleDelegateView(anchor_view, arrow) { |
41 GridLayout* layout = GridLayout::CreatePanel(this); | 42 GridLayout* layout = GridLayout::CreatePanel(this); |
42 SetLayoutManager(layout); | 43 SetLayoutManager(layout); |
43 views::ColumnSet* column_set = layout->AddColumnSet(kColumnSetId); | |
44 | 44 |
45 column_set->AddColumn(GridLayout::LEADING, | 45 views::ColumnSet* heading_column_set = |
46 GridLayout::FILL, | 46 layout->AddColumnSet(kHeadingColumnSetId); |
47 0, // no resizing | 47 heading_column_set->AddColumn(GridLayout::LEADING, |
48 GridLayout::USE_PREF, | 48 GridLayout::FILL, |
49 0, // no fixed with | 49 100.0f, // take all available space |
50 kLeftColumnWidth); | 50 GridLayout::USE_PREF, |
51 column_set->AddPaddingColumn(0, views::kPanelHorizMargin); | 51 0, // no fixed width |
52 column_set->AddColumn(GridLayout::LEADING, | 52 kTextColumnWidth); |
53 GridLayout::LEADING, | 53 heading_column_set->AddPaddingColumn(0, views::kPanelHorizMargin); |
54 0, // no resizing | 54 heading_column_set->AddColumn(GridLayout::TRAILING, |
55 GridLayout::USE_PREF, | 55 GridLayout::LEADING, |
56 0, // no fixed width | 56 0, // no resizing |
57 0); // no min width (only holds close button) | 57 GridLayout::USE_PREF, |
| 58 0, // no fixed width |
| 59 0); // no min width (only holds close button) |
58 | 60 |
59 layout->StartRow(0, kColumnSetId); | 61 views::ColumnSet* items_column_set = |
| 62 layout->AddColumnSet(kItemsColumnSetId); |
| 63 items_column_set->AddColumn(GridLayout::CENTER, |
| 64 GridLayout::CENTER, |
| 65 0, // no resizing |
| 66 GridLayout::FIXED, |
| 67 kIconSize, |
| 68 kIconSize); |
| 69 items_column_set->AddPaddingColumn(0, views::kPanelHorizMargin); |
| 70 items_column_set->AddColumn(GridLayout::LEADING, |
| 71 GridLayout::CENTER, |
| 72 100.0f, // take all available space |
| 73 GridLayout::USE_PREF, |
| 74 0, // no fixed width |
| 75 kTextColumnWidth); |
60 | 76 |
61 AddContent(layout, bundle); | 77 AddContent(layout, bundle); |
62 } | 78 } |
63 | 79 |
64 ~BundleInstalledBubble() override {} | 80 ~BundleInstalledBubble() override {} |
65 | 81 |
66 private: | 82 private: |
67 void AddContent(GridLayout* layout, const BundleInstaller* bundle) { | 83 void AddContent(GridLayout* layout, const BundleInstaller* bundle) { |
68 base::string16 installed_heading = bundle->GetHeadingTextFor( | 84 bool has_installed_items = bundle->HasItemWithState( |
69 BundleInstaller::Item::STATE_INSTALLED); | 85 BundleInstaller::Item::STATE_INSTALLED); |
70 base::string16 failed_heading = bundle->GetHeadingTextFor( | 86 bool has_failed_items = bundle->HasItemWithState( |
71 BundleInstaller::Item::STATE_FAILED); | 87 BundleInstaller::Item::STATE_FAILED); |
72 | 88 |
73 // Insert the list of installed items. | 89 // Insert the list of installed items. |
74 if (!installed_heading.empty()) { | 90 if (has_installed_items) { |
75 layout->StartRow(0, kColumnSetId); | 91 layout->StartRow(0, kHeadingColumnSetId); |
76 AddHeading(layout, installed_heading); | 92 AddHeading(layout, bundle->GetHeadingTextFor( |
| 93 BundleInstaller::Item::STATE_INSTALLED)); |
77 AddCloseButton(layout, this); | 94 AddCloseButton(layout, this); |
78 AddItemList(layout, bundle->GetItemsWithState( | 95 AddItemList(layout, bundle->GetItemsWithState( |
79 BundleInstaller::Item::STATE_INSTALLED)); | 96 BundleInstaller::Item::STATE_INSTALLED)); |
80 | 97 |
81 // Insert a line of padding if we're showing both sections. | 98 // Insert a line of padding if we're showing both sections. |
82 if (!failed_heading.empty()) | 99 if (has_failed_items) |
83 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); | 100 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); |
84 } | 101 } |
85 | 102 |
86 // Insert the list of failed items. | 103 // Insert the list of failed items. |
87 if (!failed_heading.empty()) { | 104 if (has_failed_items) { |
88 layout->StartRow(0, kColumnSetId); | 105 layout->StartRow(0, kHeadingColumnSetId); |
89 AddHeading(layout, failed_heading); | 106 AddHeading(layout, bundle->GetHeadingTextFor( |
| 107 BundleInstaller::Item::STATE_FAILED)); |
90 | 108 |
91 // The close button should be in the second column of the first row, so | 109 // The close button should be in the second column of the first row, so |
92 // we add it here if there was no installed items section. | 110 // we add it here if there was no installed items section. |
93 if (installed_heading.empty()) | 111 if (!has_installed_items) |
94 AddCloseButton(layout, this); | 112 AddCloseButton(layout, this); |
95 | 113 |
96 AddItemList(layout, bundle->GetItemsWithState( | 114 AddItemList(layout, bundle->GetItemsWithState( |
97 BundleInstaller::Item::STATE_FAILED)); | 115 BundleInstaller::Item::STATE_FAILED)); |
98 } | 116 } |
99 | 117 |
100 views::BubbleDelegateView::CreateBubble(this)->Show(); | 118 views::BubbleDelegateView::CreateBubble(this)->Show(); |
101 } | 119 } |
102 | 120 |
103 void AddItemList(GridLayout* layout, const BundleInstaller::ItemList& items) { | 121 void AddItemList(GridLayout* layout, const BundleInstaller::ItemList& items) { |
104 for (size_t i = 0; i < items.size(); ++i) { | 122 for (const BundleInstaller::Item& item : items) { |
105 base::string16 extension_name = | |
106 base::UTF8ToUTF16(items[i].localized_name); | |
107 base::i18n::AdjustStringForLocaleDirection(&extension_name); | |
108 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); | 123 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); |
109 layout->StartRow(0, kColumnSetId); | 124 layout->StartRow(0, kItemsColumnSetId); |
110 views::Label* extension_label = new views::Label( | 125 gfx::ImageSkia image = gfx::ImageSkia::CreateFrom1xBitmap(item.icon); |
111 l10n_util::GetStringFUTF16( | 126 gfx::Size size(image.width(), image.height()); |
112 IDS_EXTENSION_PERMISSION_LINE, extension_name)); | 127 if (size.width() > kIconSize || size.height() > kIconSize) |
| 128 size = gfx::Size(kIconSize, kIconSize); |
| 129 views::ImageView* image_view = new views::ImageView; |
| 130 image_view->SetImage(image); |
| 131 image_view->SetImageSize(size); |
| 132 layout->AddView(image_view); |
| 133 |
| 134 views::Label* extension_label = |
| 135 new views::Label(item.GetNameForDisplay()); |
113 extension_label->SetMultiLine(true); | 136 extension_label->SetMultiLine(true); |
114 extension_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 137 extension_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
115 extension_label->SizeToFit(kLeftColumnWidth); | 138 extension_label->SizeToFit(kTextColumnWidth); |
116 layout->AddView(extension_label); | 139 layout->AddView(extension_label); |
117 } | 140 } |
118 } | 141 } |
119 | 142 |
120 void AddCloseButton(GridLayout* layout, views::ButtonListener* listener) { | 143 void AddCloseButton(GridLayout* layout, views::ButtonListener* listener) { |
121 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 144 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
122 | 145 |
123 views::ImageButton* button = new views::ImageButton(listener); | 146 views::ImageButton* button = new views::ImageButton(listener); |
124 button->SetImage(views::CustomButton::STATE_NORMAL, | 147 button->SetImage(views::CustomButton::STATE_NORMAL, |
125 rb.GetImageSkiaNamed(IDR_CLOSE_2)); | 148 rb.GetImageSkiaNamed(IDR_CLOSE_2)); |
126 button->SetImage(views::CustomButton::STATE_HOVERED, | 149 button->SetImage(views::CustomButton::STATE_HOVERED, |
127 rb.GetImageSkiaNamed(IDR_CLOSE_2_H)); | 150 rb.GetImageSkiaNamed(IDR_CLOSE_2_H)); |
128 button->SetImage(views::CustomButton::STATE_PRESSED, | 151 button->SetImage(views::CustomButton::STATE_PRESSED, |
129 rb.GetImageSkiaNamed(IDR_CLOSE_2_P)); | 152 rb.GetImageSkiaNamed(IDR_CLOSE_2_P)); |
130 layout->AddView(button); | 153 layout->AddView(button); |
131 } | 154 } |
132 | 155 |
133 void AddHeading(GridLayout* layout, const base::string16& heading) { | 156 void AddHeading(GridLayout* layout, const base::string16& heading) { |
134 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 157 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
135 views::Label* heading_label = new views::Label( | 158 views::Label* heading_label = new views::Label( |
136 heading, rb.GetFontList(ui::ResourceBundle::MediumFont)); | 159 heading, rb.GetFontList(ui::ResourceBundle::MediumFont)); |
137 heading_label->SetMultiLine(true); | 160 heading_label->SetMultiLine(true); |
138 heading_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 161 heading_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
139 heading_label->SizeToFit(kLeftColumnWidth); | 162 heading_label->SizeToFit(kTextColumnWidth); |
140 layout->AddView(heading_label); | 163 layout->AddView(heading_label); |
141 } | 164 } |
142 | 165 |
143 // views::ButtonListener implementation: | 166 // views::ButtonListener implementation: |
144 void ButtonPressed(views::Button* sender, const ui::Event& event) override { | 167 void ButtonPressed(views::Button* sender, const ui::Event& event) override { |
145 GetWidget()->Close(); | 168 GetWidget()->Close(); |
146 } | 169 } |
147 | 170 |
148 DISALLOW_COPY_AND_ASSIGN(BundleInstalledBubble); | 171 DISALLOW_COPY_AND_ASSIGN(BundleInstalledBubble); |
149 }; | 172 }; |
150 | 173 |
151 } // namespace | 174 } // namespace |
152 | 175 |
153 void BundleInstaller::ShowInstalledBubble( | 176 void BundleInstaller::ShowInstalledBubble( |
154 const BundleInstaller* bundle, Browser* browser) { | 177 const BundleInstaller* bundle, Browser* browser) { |
155 BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser); | 178 BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser); |
156 views::View* anchor = browser_view->GetToolbarView()->app_menu(); | 179 views::View* anchor = browser_view->GetToolbarView()->app_menu(); |
157 new BundleInstalledBubble(bundle, anchor, views::BubbleBorder::TOP_RIGHT); | 180 new BundleInstalledBubble(bundle, anchor, views::BubbleBorder::TOP_RIGHT); |
158 } | 181 } |
OLD | NEW |