OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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/ui/views/new_back_shortcut_bubble.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "base/message_loop/message_loop.h" | |
10 #include "base/strings/utf_string_conversions.h" | |
11 #include "chrome/browser/ui/views/exclusive_access_bubble_views_context.h" | |
12 #include "chrome/browser/ui/views/subtle_notification_view.h" | |
13 #include "chrome/grit/generated_resources.h" | |
14 #include "ui/base/l10n/l10n_util.h" | |
15 #include "ui/gfx/animation/animation.h" | |
16 #include "ui/gfx/animation/slide_animation.h" | |
17 #include "ui/gfx/geometry/rect.h" | |
18 #include "ui/gfx/geometry/size.h" | |
19 #include "ui/strings/grit/ui_strings.h" | |
20 #include "ui/views/border.h" | |
21 #include "ui/views/view.h" | |
22 #include "ui/views/widget/widget.h" | |
23 | |
24 namespace { | |
25 | |
26 const int kPopupTopPx = 45; | |
27 const int kSlideInDurationMs = 350; | |
28 const int kSlideOutDurationMs = 700; | |
29 const int kShowDurationMs = 3800; | |
30 | |
31 } | |
32 | |
33 NewBackShortcutBubble::NewBackShortcutBubble( | |
34 ExclusiveAccessBubbleViewsContext* context, | |
35 bool forward) | |
36 : bubble_view_context_(context), | |
37 popup_(nullptr), | |
38 animation_(new gfx::SlideAnimation(this)) { | |
39 // Initially hide the bubble. | |
40 double initial_value = 0; | |
41 animation_->Reset(initial_value); | |
Peter Kasting
2016/05/24 23:35:08
Nit: AFAICT this block does nothing, as animations
Matt Giuca
2016/05/26 04:37:35
Done.
| |
42 | |
43 // Create the contents view. | |
44 view_ = new SubtleNotificationView(nullptr); | |
Peter Kasting
2016/05/24 23:35:08
Nit: Can this be in the initializer list?
Matt Giuca
2016/05/26 04:37:35
Done.
| |
45 | |
46 // Initialize the popup. | |
47 popup_ = SubtleNotificationView::CreatePopupWidget( | |
48 bubble_view_context_->GetBubbleParentView(), view_, false); | |
Peter Kasting
2016/05/24 23:35:08
Nit: If declared after |view_|, could also be in t
Matt Giuca
2016/05/26 04:37:35
Done.
| |
49 | |
50 UpdateContent(forward); | |
51 } | |
52 | |
53 NewBackShortcutBubble::~NewBackShortcutBubble() { | |
54 // Must delete the widget asynchronously. See rationale in | |
55 // ~ExclusiveAccessBubbleViews. | |
Peter Kasting
2016/05/24 23:35:09
Nit: It sounded like you weren't sure whether or n
Matt Giuca
2016/05/26 04:37:35
Done.
| |
56 popup_->Close(); | |
57 base::MessageLoop::current()->DeleteSoon(FROM_HERE, popup_); | |
58 } | |
59 | |
60 void NewBackShortcutBubble::UpdateContent(bool forward) { | |
61 // Note: The key names are parameters so that we can vary by operating system | |
62 // or change the direction of the arrow as necessary (see | |
63 // https://crbug.com/612685). | |
64 | |
65 #if defined(OS_MACOSX) | |
66 // U+2318 = PLACE OF INTEREST SIGN (Mac Command symbol). | |
67 base::string16 accelerator = base::WideToUTF16(L"\x2318"); | |
68 #else | |
69 base::string16 accelerator = l10n_util::GetStringUTF16(IDS_APP_ALT_KEY); | |
70 #endif | |
71 | |
72 int message_id = forward ? IDS_PRESS_ALT_RIGHT_TO_GO_FORWARD | |
73 : IDS_PRESS_ALT_LEFT_TO_GO_BACK; | |
74 // U+2192 = RIGHTWARDS ARROW; U+2190 = LEFTWARDS ARROW. | |
75 base::string16 arrow_key = base::WideToUTF16(forward ? L"\x2192" : L"\x2190"); | |
76 view_->UpdateContent( | |
77 l10n_util::GetStringFUTF16(message_id, accelerator, arrow_key), | |
78 base::string16()); | |
79 | |
80 view_->SetSize(GetPopupRect(true).size()); | |
81 // Bounds are in screen coordinates. | |
Peter Kasting
2016/05/24 23:35:09
Nit: It's a little unclear to me why this comment
Matt Giuca
2016/05/26 04:37:35
Done.
| |
82 popup_->SetBounds(GetPopupRect(false)); | |
83 Show(); | |
84 } | |
85 | |
86 void NewBackShortcutBubble::AnimationProgressed( | |
87 const gfx::Animation* animation) { | |
88 int opacity = animation_->CurrentValueBetween(0, 255); | |
89 if (opacity == 0) { | |
90 popup_->Hide(); | |
91 } else { | |
92 popup_->Show(); | |
Peter Kasting
2016/05/24 23:35:09
Nit: It looks like maybe Widget::Show() is complic
Matt Giuca
2016/05/26 04:37:35
Done.
| |
93 popup_->SetOpacity(opacity); | |
94 } | |
95 } | |
96 | |
97 void NewBackShortcutBubble::AnimationEnded(const gfx::Animation* animation) { | |
98 AnimationProgressed(animation); | |
99 } | |
100 | |
101 gfx::Rect NewBackShortcutBubble::GetPopupRect( | |
102 bool ignore_animation_state) const { | |
103 gfx::Size size = view_->GetPreferredSize(); | |
104 gfx::Rect widget_bounds = bubble_view_context_->GetClientAreaBoundsInScreen(); | |
105 int x = widget_bounds.x() + (widget_bounds.width() - size.width()) / 2; | |
106 int top_container_bottom = widget_bounds.y(); | |
Peter Kasting
2016/05/24 23:35:08
Nit: I might inline this particular variable into
Matt Giuca
2016/05/26 04:37:35
Done.
| |
107 // |desired_top| is the top of the bubble area including the shadow. | |
108 int desired_top = kPopupTopPx - view_->border()->GetInsets().top(); | |
109 int y = top_container_bottom + desired_top; | |
110 return gfx::Rect(gfx::Point(x, y), size); | |
111 } | |
112 | |
113 void NewBackShortcutBubble::Hide() { | |
Peter Kasting
2016/05/24 23:35:08
Nit: AFAICT both Hide() and Show() are called in o
Matt Giuca
2016/05/26 04:37:35
Done.
| |
114 animation_->SetSlideDuration(kSlideOutDurationMs); | |
115 animation_->Hide(); | |
116 } | |
117 | |
118 void NewBackShortcutBubble::Show() { | |
119 animation_->SetSlideDuration(kSlideInDurationMs); | |
120 animation_->Show(); | |
121 | |
122 // Wait a few seconds before hiding. | |
123 hide_timeout_.Start(FROM_HERE, | |
124 base::TimeDelta::FromMilliseconds(kShowDurationMs), this, | |
125 &NewBackShortcutBubble::OnTimerElapsed); | |
126 } | |
127 | |
128 void NewBackShortcutBubble::OnTimerElapsed() { | |
129 Hide(); | |
130 } | |
OLD | NEW |