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

Side by Side Diff: ash/sticky_keys/sticky_keys_overlay.cc

Issue 754763005: Speculative fix for sticky keys overlay crash. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: remove WidgetDelegateView inheritance Created 6 years 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "ash/sticky_keys/sticky_keys_overlay.h" 5 #include "ash/sticky_keys/sticky_keys_overlay.h"
6 6
7 #include "ash/shell.h" 7 #include "ash/shell.h"
8 #include "ash/shell_window_ids.h" 8 #include "ash/shell_window_ids.h"
9 #include "ash/sticky_keys/sticky_keys_controller.h" 9 #include "ash/sticky_keys/sticky_keys_controller.h"
10 #include "base/strings/string_util.h" 10 #include "base/strings/string_util.h"
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 label_color = SkColorSetA(enabled_color(), 0x80); 94 label_color = SkColorSetA(enabled_color(), 0x80);
95 } 95 }
96 96
97 SetEnabledColor(label_color); 97 SetEnabledColor(label_color);
98 SetDisabledColor(label_color); 98 SetDisabledColor(label_color);
99 SetFontList(font_list().DeriveWithStyle(style)); 99 SetFontList(font_list().DeriveWithStyle(style));
100 } 100 }
101 101
102 /////////////////////////////////////////////////////////////////////////////// 102 ///////////////////////////////////////////////////////////////////////////////
103 // StickyKeysOverlayView 103 // StickyKeysOverlayView
104 class StickyKeysOverlayView : public views::WidgetDelegateView { 104 class StickyKeysOverlayView : public views::View {
105 public: 105 public:
106 // This object is not owned by the views hiearchy or by the widget. The
107 // StickyKeysOverlay is the only class that should create and destroy this
108 // view.
106 StickyKeysOverlayView(); 109 StickyKeysOverlayView();
107 110
108 ~StickyKeysOverlayView() override; 111 ~StickyKeysOverlayView() override;
109 112
110 // views::WidgetDelegateView overrides: 113 // views::View overrides:
111 void OnPaint(gfx::Canvas* canvas) override; 114 void OnPaint(gfx::Canvas* canvas) override;
112 115
113 void SetKeyState(ui::EventFlags modifier, StickyKeyState state); 116 void SetKeyState(ui::EventFlags modifier, StickyKeyState state);
114 117
115 StickyKeyState GetKeyState(ui::EventFlags modifier); 118 StickyKeyState GetKeyState(ui::EventFlags modifier);
116 119
117 void SetModifierVisible(ui::EventFlags modifier, bool visible); 120 void SetModifierVisible(ui::EventFlags modifier, bool visible);
118 bool GetModifierVisible(ui::EventFlags modifier); 121 bool GetModifierVisible(ui::EventFlags modifier);
119 122
120 private: 123 private:
121 void AddKeyLabel(ui::EventFlags modifier, const std::string& key_label); 124 void AddKeyLabel(ui::EventFlags modifier, const std::string& key_label);
122 125
123 typedef std::map<ui::EventFlags, StickyKeyOverlayLabel*> ModifierLabelMap; 126 typedef std::map<ui::EventFlags, StickyKeyOverlayLabel*> ModifierLabelMap;
124 ModifierLabelMap modifier_label_map_; 127 ModifierLabelMap modifier_label_map_;
125 128
126 DISALLOW_COPY_AND_ASSIGN(StickyKeysOverlayView); 129 DISALLOW_COPY_AND_ASSIGN(StickyKeysOverlayView);
127 }; 130 };
128 131
129 StickyKeysOverlayView::StickyKeysOverlayView() { 132 StickyKeysOverlayView::StickyKeysOverlayView() {
133 // The parent StickyKeysOverlay object owns this view.
134 set_owned_by_client();
135
130 const gfx::Font& font = 136 const gfx::Font& font =
131 ui::ResourceBundle::GetSharedInstance().GetFont(kKeyLabelFontStyle); 137 ui::ResourceBundle::GetSharedInstance().GetFont(kKeyLabelFontStyle);
132 int font_size = font.GetFontSize(); 138 int font_size = font.GetFontSize();
133 int font_padding = font.GetHeight() - font.GetBaseline(); 139 int font_padding = font.GetHeight() - font.GetBaseline();
134 140
135 // Text should have a margin of 0.5 times the font size on each side, so 141 // Text should have a margin of 0.5 times the font size on each side, so
136 // the spacing between two labels will be the same as the font size. 142 // the spacing between two labels will be the same as the font size.
137 int horizontal_spacing = font_size / 2; 143 int horizontal_spacing = font_size / 2;
138 int vertical_spacing = font_size / 2 - font_padding; 144 int vertical_spacing = font_size / 2 - font_padding;
139 int child_spacing = font_size - 2 * font_padding; 145 int child_spacing = font_size - 2 * font_padding;
(...skipping 16 matching lines...) Expand all
156 l10n_util::GetStringUTF8(IDS_ASH_MOD3_KEY)); 162 l10n_util::GetStringUTF8(IDS_ASH_MOD3_KEY));
157 } 163 }
158 164
159 StickyKeysOverlayView::~StickyKeysOverlayView() {} 165 StickyKeysOverlayView::~StickyKeysOverlayView() {}
160 166
161 void StickyKeysOverlayView::OnPaint(gfx::Canvas* canvas) { 167 void StickyKeysOverlayView::OnPaint(gfx::Canvas* canvas) {
162 SkPaint paint; 168 SkPaint paint;
163 paint.setStyle(SkPaint::kFill_Style); 169 paint.setStyle(SkPaint::kFill_Style);
164 paint.setColor(SkColorSetARGB(0xB3, 0x55, 0x55, 0x55)); 170 paint.setColor(SkColorSetARGB(0xB3, 0x55, 0x55, 0x55));
165 canvas->DrawRoundRect(GetLocalBounds(), 2, paint); 171 canvas->DrawRoundRect(GetLocalBounds(), 2, paint);
166 views::WidgetDelegateView::OnPaint(canvas); 172 views::View::OnPaint(canvas);
167 } 173 }
168 174
169 void StickyKeysOverlayView::SetKeyState(ui::EventFlags modifier, 175 void StickyKeysOverlayView::SetKeyState(ui::EventFlags modifier,
170 StickyKeyState state) { 176 StickyKeyState state) {
171 ModifierLabelMap::iterator it = modifier_label_map_.find(modifier); 177 ModifierLabelMap::iterator it = modifier_label_map_.find(modifier);
172 DCHECK(it != modifier_label_map_.end()); 178 DCHECK(it != modifier_label_map_.end());
173 if (it != modifier_label_map_.end()) { 179 if (it != modifier_label_map_.end()) {
174 StickyKeyOverlayLabel* label = it->second; 180 StickyKeyOverlayLabel* label = it->second;
175 label->SetKeyState(state); 181 label->SetKeyState(state);
176 } 182 }
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 : is_visible_(false), 214 : is_visible_(false),
209 overlay_view_(new StickyKeysOverlayView), 215 overlay_view_(new StickyKeysOverlayView),
210 widget_size_(overlay_view_->GetPreferredSize()) { 216 widget_size_(overlay_view_->GetPreferredSize()) {
211 views::Widget::InitParams params; 217 views::Widget::InitParams params;
212 params.type = views::Widget::InitParams::TYPE_POPUP; 218 params.type = views::Widget::InitParams::TYPE_POPUP;
213 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; 219 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
214 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; 220 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
215 params.accept_events = false; 221 params.accept_events = false;
216 params.keep_on_top = true; 222 params.keep_on_top = true;
217 params.remove_standard_frame = true; 223 params.remove_standard_frame = true;
218 params.delegate = overlay_view_;
219 params.bounds = CalculateOverlayBounds(); 224 params.bounds = CalculateOverlayBounds();
220 params.parent = Shell::GetContainer(Shell::GetTargetRootWindow(), 225 params.parent = Shell::GetContainer(Shell::GetTargetRootWindow(),
221 kShellWindowId_OverlayContainer); 226 kShellWindowId_OverlayContainer);
222 overlay_widget_.reset(new views::Widget); 227 overlay_widget_.reset(new views::Widget);
223 overlay_widget_->Init(params); 228 overlay_widget_->Init(params);
224 overlay_widget_->SetVisibilityChangedAnimationsEnabled(false); 229 overlay_widget_->SetVisibilityChangedAnimationsEnabled(false);
225 overlay_widget_->SetContentsView(overlay_view_); 230 overlay_widget_->SetContentsView(overlay_view_.get());
226 overlay_widget_->GetNativeView()->SetName("StickyKeysOverlay"); 231 overlay_widget_->GetNativeView()->SetName("StickyKeysOverlay");
227 } 232 }
228 233
229 StickyKeysOverlay::~StickyKeysOverlay() {} 234 StickyKeysOverlay::~StickyKeysOverlay() {}
230 235
231 void StickyKeysOverlay::Show(bool visible) { 236 void StickyKeysOverlay::Show(bool visible) {
232 if (is_visible_ == visible) 237 if (is_visible_ == visible)
233 return; 238 return;
234 239
235 is_visible_ = visible; 240 is_visible_ = visible;
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
272 void StickyKeysOverlay::SetModifierKeyState(ui::EventFlags modifier, 277 void StickyKeysOverlay::SetModifierKeyState(ui::EventFlags modifier,
273 StickyKeyState state) { 278 StickyKeyState state) {
274 overlay_view_->SetKeyState(modifier, state); 279 overlay_view_->SetKeyState(modifier, state);
275 } 280 }
276 281
277 StickyKeyState StickyKeysOverlay::GetModifierKeyState( 282 StickyKeyState StickyKeysOverlay::GetModifierKeyState(
278 ui::EventFlags modifier) { 283 ui::EventFlags modifier) {
279 return overlay_view_->GetKeyState(modifier); 284 return overlay_view_->GetKeyState(modifier);
280 } 285 }
281 286
287 views::Widget* StickyKeysOverlay::GetWidgetForTesting() {
288 return overlay_widget_.get();
289 }
290
282 gfx::Rect StickyKeysOverlay::CalculateOverlayBounds() { 291 gfx::Rect StickyKeysOverlay::CalculateOverlayBounds() {
283 int x = is_visible_ ? kHorizontalOverlayOffset : -widget_size_.width(); 292 int x = is_visible_ ? kHorizontalOverlayOffset : -widget_size_.width();
284 return gfx::Rect(gfx::Point(x, kVerticalOverlayOffset), widget_size_); 293 return gfx::Rect(gfx::Point(x, kVerticalOverlayOffset), widget_size_);
285 } 294 }
286 295
287 void StickyKeysOverlay::OnLayerAnimationEnded( 296 void StickyKeysOverlay::OnLayerAnimationEnded(
288 ui::LayerAnimationSequence* sequence) { 297 ui::LayerAnimationSequence* sequence) {
289 ui::LayerAnimator* animator = overlay_widget_->GetLayer()->GetAnimator(); 298 ui::LayerAnimator* animator = overlay_widget_->GetLayer()->GetAnimator();
290 if (animator) 299 if (animator)
291 animator->RemoveObserver(this); 300 animator->RemoveObserver(this);
292 if (!is_visible_) 301 if (!is_visible_)
293 overlay_widget_->Hide(); 302 overlay_widget_->Hide();
294 } 303 }
295 304
296 void StickyKeysOverlay::OnLayerAnimationAborted( 305 void StickyKeysOverlay::OnLayerAnimationAborted(
297 ui::LayerAnimationSequence* sequence) { 306 ui::LayerAnimationSequence* sequence) {
298 ui::LayerAnimator* animator = overlay_widget_->GetLayer()->GetAnimator(); 307 ui::LayerAnimator* animator = overlay_widget_->GetLayer()->GetAnimator();
299 if (animator) 308 if (animator)
300 animator->RemoveObserver(this); 309 animator->RemoveObserver(this);
301 } 310 }
302 311
303 void StickyKeysOverlay::OnLayerAnimationScheduled( 312 void StickyKeysOverlay::OnLayerAnimationScheduled(
304 ui::LayerAnimationSequence* sequence) { 313 ui::LayerAnimationSequence* sequence) {
305 } 314 }
306 315
307 } // namespace ash 316 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698