OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/chromeos/volume_bubble.h" |
| 6 |
| 7 #include <gdk/gdk.h> |
| 8 |
| 9 #include "base/timer.h" |
| 10 #include "chrome/browser/chromeos/volume_bubble_view.h" |
| 11 #include "chrome/browser/views/info_bubble.h" |
| 12 #include "views/widget/root_view.h" |
| 13 |
| 14 namespace { |
| 15 const int kBubbleShowTimeoutSec = 2; |
| 16 const int kAnimationDurationMs = 200; |
| 17 const int kVolumeBubbleX = 300, kVolumeBubbleY = 700; |
| 18 } // namespace |
| 19 |
| 20 namespace chromeos { |
| 21 |
| 22 // Temporary helper routine. Returns currently shown Chrome widget or NULL. |
| 23 // TODO(glotov): remove this in favor of enabling InfoBubble class act |
| 24 // without |parent| specified. crosbug.com/4025 |
| 25 static views::Widget* GetToplevelWidget() { |
| 26 views::Widget* widget = NULL; |
| 27 GList *window_list = gtk_window_list_toplevels(); |
| 28 for (GList* iter = window_list; iter; iter = g_list_next(iter)) { |
| 29 GtkWindow* const window = GTK_WINDOW(iter->data); |
| 30 if (window && GTK_WIDGET(window)->window == |
| 31 gdk_screen_get_active_window(gdk_screen_get_default())) { |
| 32 views::RootView* root = views::Widget::FindRootView(window); |
| 33 if (root) |
| 34 widget = root->GetWidget(); |
| 35 break; |
| 36 } |
| 37 } |
| 38 g_list_free(window_list); |
| 39 return widget; |
| 40 } |
| 41 |
| 42 VolumeBubble::VolumeBubble() |
| 43 : previous_percent_(-1), |
| 44 current_percent_(-1), |
| 45 bubble_(NULL), |
| 46 view_(NULL), |
| 47 animation_(this) { |
| 48 animation_.SetSlideDuration(kAnimationDurationMs); |
| 49 animation_.SetTweenType(Tween::LINEAR); |
| 50 } |
| 51 |
| 52 void VolumeBubble::ShowVolumeBubble(int percent) { |
| 53 if (percent < 0) percent = 0; |
| 54 if (percent > 100) percent = 100; |
| 55 if (previous_percent_ == -1) previous_percent_ = percent; |
| 56 current_percent_ = percent; |
| 57 if (!bubble_) { |
| 58 views::Widget* widget = GetToplevelWidget(); |
| 59 if (widget == NULL) |
| 60 return; |
| 61 DCHECK(view_ == NULL); |
| 62 view_ = new VolumeBubbleView; |
| 63 view_->Init(previous_percent_); |
| 64 // TODO(glotov): Place volume bubble over the keys initiated the |
| 65 // volume change. This metric must be specific to the given |
| 66 // architecture. crosbug.com/4028 |
| 67 bubble_ = InfoBubble::Show(widget, |
| 68 gfx::Rect(kVolumeBubbleX, kVolumeBubbleY, 0, 0), |
| 69 BubbleBorder::FLOAT, view_, this); |
| 70 } else { |
| 71 DCHECK(view_); |
| 72 timeout_timer_.Stop(); |
| 73 } |
| 74 if (animation_.is_animating()) animation_.End(); |
| 75 animation_.Reset(); |
| 76 animation_.Show(); |
| 77 timeout_timer_.Start(base::TimeDelta::FromSeconds(kBubbleShowTimeoutSec), |
| 78 this, &VolumeBubble::OnTimeout); |
| 79 } |
| 80 |
| 81 void VolumeBubble::OnTimeout() { |
| 82 if (bubble_) |
| 83 bubble_->Close(); |
| 84 } |
| 85 |
| 86 void VolumeBubble::InfoBubbleClosing(InfoBubble* info_bubble, bool) { |
| 87 DCHECK(info_bubble == bubble_); |
| 88 timeout_timer_.Stop(); |
| 89 animation_.Stop(); |
| 90 bubble_ = NULL; |
| 91 view_ = NULL; |
| 92 } |
| 93 |
| 94 void VolumeBubble::AnimationEnded(const Animation* animation) { |
| 95 previous_percent_ = current_percent_; |
| 96 } |
| 97 |
| 98 void VolumeBubble::AnimationProgressed(const Animation* animation) { |
| 99 if (view_) { |
| 100 view_->Update( |
| 101 Tween::ValueBetween(animation->GetCurrentValue(), |
| 102 previous_percent_, |
| 103 current_percent_)); |
| 104 } |
| 105 } |
| 106 |
| 107 } // namespace chromeos |
OLD | NEW |