Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(168)

Side by Side Diff: chrome/browser/chromeos/status/clock_menu_button.cc

Issue 6811025: Change status button menu implementation from Menu2 to MenuItemView. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Changed refcount guard to use OS_CHROMEOS. Created 9 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/chromeos/status/clock_menu_button.h" 5 #include "chrome/browser/chromeos/status/clock_menu_button.h"
6 6
7 #include "base/i18n/time_formatting.h" 7 #include "base/i18n/time_formatting.h"
8 #include "base/string_util.h" 8 #include "base/string_util.h"
9 #include "base/time.h" 9 #include "base/time.h"
10 #include "base/utf_string_conversions.h" 10 #include "base/utf_string_conversions.h"
11 #include "chrome/browser/chromeos/cros/cros_library.h" 11 #include "chrome/browser/chromeos/cros/cros_library.h"
12 #include "chrome/browser/chromeos/status/status_area_host.h" 12 #include "chrome/browser/chromeos/status/status_area_host.h"
13 #include "grit/generated_resources.h" 13 #include "grit/generated_resources.h"
14 #include "ui/base/l10n/l10n_util.h" 14 #include "ui/base/l10n/l10n_util.h"
15 #include "ui/base/resource/resource_bundle.h" 15 #include "ui/base/resource/resource_bundle.h"
16 #include "ui/gfx/canvas.h" 16 #include "ui/gfx/canvas.h"
17 #include "ui/gfx/font.h" 17 #include "ui/gfx/font.h"
18 #include "views/widget/widget.h"
19 #include "views/window/window.h"
20
21 using views::MenuItemView;
18 22
19 namespace chromeos { 23 namespace chromeos {
20 24
25 // MenuItemView item ids
26 enum {
27 CLOCK_DISPLAY_ITEM,
28 CLOCK_OPEN_OPTIONS_ITEM,
29 };
30
21 // Amount of slop to add into the timer to make sure we're into the next minute 31 // Amount of slop to add into the timer to make sure we're into the next minute
22 // when the timer goes off. 32 // when the timer goes off.
23 const int kTimerSlopSeconds = 1; 33 const int kTimerSlopSeconds = 1;
24 34
25 #if defined(CROS_FONTS_USING_BCI) 35 #if defined(CROS_FONTS_USING_BCI)
26 const int kFontSizeDelta = 0; 36 const int kFontSizeDelta = 0;
27 #else 37 #else
28 const int kFontSizeDelta = 1; 38 const int kFontSizeDelta = 1;
29 #endif 39 #endif
oshima 2011/04/14 17:27:31 can you move these (enum to kFontSizeDelta) into a
rhashimoto 2011/04/14 18:27:04 Done.
30 40
31 ClockMenuButton::ClockMenuButton(StatusAreaHost* host) 41 ClockMenuButton::ClockMenuButton(StatusAreaHost* host)
32 : StatusAreaButton(this), 42 : StatusAreaButton(this),
33 host_(host) { 43 host_(host) {
34 // Add as SystemLibrary observer. We update the clock if timezone changes. 44 // Add as SystemLibrary observer. We update the clock if timezone changes.
35 CrosLibrary::Get()->GetSystemLibrary()->AddObserver(this); 45 CrosLibrary::Get()->GetSystemLibrary()->AddObserver(this);
36 CrosLibrary::Get()->GetPowerLibrary()->AddObserver(this); 46 CrosLibrary::Get()->GetPowerLibrary()->AddObserver(this);
37 47
38 set_border(NULL); 48 set_border(NULL);
39 set_use_menu_button_paint(true); 49 set_use_menu_button_paint(true);
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
75 } 85 }
76 86
77 void ClockMenuButton::UpdateText() { 87 void ClockMenuButton::UpdateText() {
78 base::Time time(base::Time::Now()); 88 base::Time time(base::Time::Now());
79 SetText(UTF16ToWide(base::TimeFormatTimeOfDay(time))); 89 SetText(UTF16ToWide(base::TimeFormatTimeOfDay(time)));
80 SetTooltipText(UTF16ToWide(base::TimeFormatShortDate(time))); 90 SetTooltipText(UTF16ToWide(base::TimeFormatShortDate(time)));
81 SchedulePaint(); 91 SchedulePaint();
82 } 92 }
83 93
84 //////////////////////////////////////////////////////////////////////////////// 94 ////////////////////////////////////////////////////////////////////////////////
85 // ClockMenuButton, ui::MenuModel implementation: 95 // ClockMenuButton, views::MenuDelegate implementation:
86 96 std::wstring ClockMenuButton::GetLabel(int id) const
87 int ClockMenuButton::GetItemCount() const { 97 {
88 // If options dialog is unavailable, don't count a separator and configure 98 DCHECK(id == CLOCK_DISPLAY_ITEM);
oshima 2011/04/14 17:27:31 DCHECK_EQ note: expected value first
rhashimoto 2011/04/14 18:27:04 Done.
89 // menu item. 99 const string16 label = base::TimeFormatFriendlyDate(base::Time::Now());
90 return host_->ShouldOpenButtonOptions(this) ? 3 : 1; 100 return UTF16ToWide(label);
91 } 101 }
92 102
93 ui::MenuModel::ItemType ClockMenuButton::GetTypeAt(int index) const { 103 bool ClockMenuButton::IsCommandEnabled(int id) const
94 // There's a separator between the current date and the menu item to open 104 {
95 // the options menu. 105 DCHECK(id == CLOCK_DISPLAY_ITEM || id == CLOCK_OPEN_OPTIONS_ITEM);
96 return index == 1 ? ui::MenuModel::TYPE_SEPARATOR: 106 if (id == CLOCK_DISPLAY_ITEM)
oshima 2011/04/14 17:27:31 return id == CLOCK_OPEN_OPTIONS_ITEM
rhashimoto 2011/04/14 18:27:04 Done.
97 ui::MenuModel::TYPE_COMMAND; 107 return false;
108 else
109 return true;
98 } 110 }
99 111
100 string16 ClockMenuButton::GetLabelAt(int index) const { 112 void ClockMenuButton::ExecuteCommand(int id)
101 if (index == 0) 113 {
102 return base::TimeFormatFriendlyDate(base::Time::Now()); 114 DCHECK(id == CLOCK_OPEN_OPTIONS_ITEM);
oshima 2011/04/14 17:27:31 DCHECK_EQ
rhashimoto 2011/04/14 18:27:04 Done.
103 return l10n_util::GetStringUTF16(IDS_STATUSBAR_CLOCK_OPEN_OPTIONS_DIALOG);
104 }
105
106 bool ClockMenuButton::IsEnabledAt(int index) const {
107 // The 1st item is the current date, which is disabled.
108 return index != 0;
109 }
110
111 void ClockMenuButton::ActivatedAt(int index) {
112 host_->OpenButtonOptions(this); 115 host_->OpenButtonOptions(this);
113 } 116 }
114 117
115 /////////////////////////////////////////////////////////////////////////////// 118 ///////////////////////////////////////////////////////////////////////////////
116 // ClockMenuButton, PowerLibrary::Observer implementation: 119 // ClockMenuButton, PowerLibrary::Observer implementation:
117 120
118 void ClockMenuButton::SystemResumed() { 121 void ClockMenuButton::SystemResumed() {
119 UpdateText(); 122 UpdateText();
120 } 123 }
121 124
122 /////////////////////////////////////////////////////////////////////////////// 125 ///////////////////////////////////////////////////////////////////////////////
123 // ClockMenuButton, SystemLibrary::Observer implementation: 126 // ClockMenuButton, SystemLibrary::Observer implementation:
124 127
125 void ClockMenuButton::TimezoneChanged(const icu::TimeZone& timezone) { 128 void ClockMenuButton::TimezoneChanged(const icu::TimeZone& timezone) {
126 UpdateText(); 129 UpdateText();
127 } 130 }
128 131
129 //////////////////////////////////////////////////////////////////////////////// 132 ////////////////////////////////////////////////////////////////////////////////
130 // ClockMenuButton, views::ViewMenuDelegate implementation: 133 // ClockMenuButton, views::ViewMenuDelegate implementation:
131 134
132 void ClockMenuButton::RunMenu(views::View* source, const gfx::Point& pt) { 135 void ClockMenuButton::RunMenu(views::View* source, const gfx::Point& pt) {
133 if (!clock_menu_.get()) 136 // View passed in must be a views::MenuButton, i.e. the ClockMenuButton.
134 clock_menu_.reset(new views::Menu2(this)); 137 DCHECK(source == this);
oshima 2011/04/14 17:27:31 DCHECK_EQ
rhashimoto 2011/04/14 18:27:04 Done.
135 else 138
136 clock_menu_->Rebuild(); 139 if (!menu_.get()) {
137 clock_menu_->UpdateStates(); 140 menu_.reset(new MenuItemView(this));
138 clock_menu_->RunMenuAt(pt, views::Menu2::ALIGN_TOPRIGHT); 141
142 // Text for this item will be set by GetLabel().
143 menu_->AppendDelegateMenuItem(CLOCK_DISPLAY_ITEM);
144
145 // If options dialog is unavailable, don't count a separator and configure
146 // menu item.
147 if (host_->ShouldOpenButtonOptions(this)) {
148 menu_->AppendSeparator();
149
150 const string16 clock_open_options_label =
151 l10n_util::GetStringUTF16(IDS_STATUSBAR_CLOCK_OPEN_OPTIONS_DIALOG);
152 menu_->AppendMenuItemWithLabel(
153 CLOCK_OPEN_OPTIONS_ITEM,
154 UTF16ToWide(clock_open_options_label));
155 }
156 }
oshima 2011/04/14 17:27:31 It's probably better to separate above code. I'm f
rhashimoto 2011/04/14 18:27:04 Done.
157
158 // TODO(rhashimoto): Remove this workaround when WebUI provides a
159 // top-level widget on the ChromeOS login screen that is a window.
160 // The current BackgroundView class for the ChromeOS login screen
161 // creates a owning Widget that has a native GtkWindow but is not a
162 // Window. This makes it impossible to get the NativeWindow via
163 // the views API. This workaround casts the top-level NativeWidget
164 // to a NativeWindow that we can pass to MenuItemView::RunMenuAt().
oshima 2011/04/14 17:27:31 won't this work? source->GetWidget()->GetToplevel
rhashimoto 2011/04/14 18:27:04 That gets the NativeView, but we have to pass a Na
oshima 2011/04/15 17:40:12 NativeView is gtk_windonw instance in gtk_widget t
rhashimoto 2011/04/15 21:20:27 I think this is fundamentally what I've done, i.e.
165 gfx::NativeWindow window;
166 if (source->GetWindow()) {
167 // This is the normal case with a browser.
168 window = source->GetWindow()->GetNativeWindow();
169 } else {
170 #if defined(OS_WIN)
171 NOTREACHED();
172 #elif defined(USE_X11)
173 window = GTK_WINDOW(source->GetWidget()->GetNativeView());
174 #endif
oshima 2011/04/15 17:40:12 no need for ifdefs as ths is only for chromeos
rhashimoto 2011/04/15 21:20:27 Done. I was thinking that the #ifdefs signalled t
175 }
176
177 gfx::Point screen_loc;
178 views::View::ConvertPointToScreen(source, &screen_loc);
179 gfx::Rect bounds(screen_loc, source->size());
180 menu_->RunMenuAt(
181 window,
182 this,
183 bounds,
184 base::i18n::IsRTL() ? MenuItemView::TOPLEFT : MenuItemView::TOPRIGHT,
185 true);
139 } 186 }
140 187
141 } // namespace chromeos 188 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698