OLD | NEW |
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 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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::WidgetDelegateView { |
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::WidgetDelegateView overrides: |
111 void OnPaint(gfx::Canvas* canvas) override; | 114 void OnPaint(gfx::Canvas* canvas) override; |
| 115 void DeleteDelegate() override; |
112 | 116 |
113 void SetKeyState(ui::EventFlags modifier, StickyKeyState state); | 117 void SetKeyState(ui::EventFlags modifier, StickyKeyState state); |
114 | 118 |
115 StickyKeyState GetKeyState(ui::EventFlags modifier); | 119 StickyKeyState GetKeyState(ui::EventFlags modifier); |
116 | 120 |
117 void SetModifierVisible(ui::EventFlags modifier, bool visible); | 121 void SetModifierVisible(ui::EventFlags modifier, bool visible); |
118 bool GetModifierVisible(ui::EventFlags modifier); | 122 bool GetModifierVisible(ui::EventFlags modifier); |
119 | 123 |
120 private: | 124 private: |
121 void AddKeyLabel(ui::EventFlags modifier, const std::string& key_label); | 125 void AddKeyLabel(ui::EventFlags modifier, const std::string& key_label); |
122 | 126 |
123 typedef std::map<ui::EventFlags, StickyKeyOverlayLabel*> ModifierLabelMap; | 127 typedef std::map<ui::EventFlags, StickyKeyOverlayLabel*> ModifierLabelMap; |
124 ModifierLabelMap modifier_label_map_; | 128 ModifierLabelMap modifier_label_map_; |
125 | 129 |
126 DISALLOW_COPY_AND_ASSIGN(StickyKeysOverlayView); | 130 DISALLOW_COPY_AND_ASSIGN(StickyKeysOverlayView); |
127 }; | 131 }; |
128 | 132 |
129 StickyKeysOverlayView::StickyKeysOverlayView() { | 133 StickyKeysOverlayView::StickyKeysOverlayView() { |
| 134 // The parent StickyKeysOverlay object owns this view. |
| 135 set_owned_by_client(); |
| 136 |
130 const gfx::Font& font = | 137 const gfx::Font& font = |
131 ui::ResourceBundle::GetSharedInstance().GetFont(kKeyLabelFontStyle); | 138 ui::ResourceBundle::GetSharedInstance().GetFont(kKeyLabelFontStyle); |
132 int font_size = font.GetFontSize(); | 139 int font_size = font.GetFontSize(); |
133 int font_padding = font.GetHeight() - font.GetBaseline(); | 140 int font_padding = font.GetHeight() - font.GetBaseline(); |
134 | 141 |
135 // Text should have a margin of 0.5 times the font size on each side, so | 142 // 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. | 143 // the spacing between two labels will be the same as the font size. |
137 int horizontal_spacing = font_size / 2; | 144 int horizontal_spacing = font_size / 2; |
138 int vertical_spacing = font_size / 2 - font_padding; | 145 int vertical_spacing = font_size / 2 - font_padding; |
139 int child_spacing = font_size - 2 * font_padding; | 146 int child_spacing = font_size - 2 * font_padding; |
(...skipping 19 matching lines...) Expand all Loading... |
159 StickyKeysOverlayView::~StickyKeysOverlayView() {} | 166 StickyKeysOverlayView::~StickyKeysOverlayView() {} |
160 | 167 |
161 void StickyKeysOverlayView::OnPaint(gfx::Canvas* canvas) { | 168 void StickyKeysOverlayView::OnPaint(gfx::Canvas* canvas) { |
162 SkPaint paint; | 169 SkPaint paint; |
163 paint.setStyle(SkPaint::kFill_Style); | 170 paint.setStyle(SkPaint::kFill_Style); |
164 paint.setColor(SkColorSetARGB(0xB3, 0x55, 0x55, 0x55)); | 171 paint.setColor(SkColorSetARGB(0xB3, 0x55, 0x55, 0x55)); |
165 canvas->DrawRoundRect(GetLocalBounds(), 2, paint); | 172 canvas->DrawRoundRect(GetLocalBounds(), 2, paint); |
166 views::WidgetDelegateView::OnPaint(canvas); | 173 views::WidgetDelegateView::OnPaint(canvas); |
167 } | 174 } |
168 | 175 |
| 176 void StickyKeysOverlayView::DeleteDelegate() { |
| 177 // This view object is owned by StickyKeysOverlay, so no-op here. |
| 178 } |
| 179 |
169 void StickyKeysOverlayView::SetKeyState(ui::EventFlags modifier, | 180 void StickyKeysOverlayView::SetKeyState(ui::EventFlags modifier, |
170 StickyKeyState state) { | 181 StickyKeyState state) { |
171 ModifierLabelMap::iterator it = modifier_label_map_.find(modifier); | 182 ModifierLabelMap::iterator it = modifier_label_map_.find(modifier); |
172 DCHECK(it != modifier_label_map_.end()); | 183 DCHECK(it != modifier_label_map_.end()); |
173 if (it != modifier_label_map_.end()) { | 184 if (it != modifier_label_map_.end()) { |
174 StickyKeyOverlayLabel* label = it->second; | 185 StickyKeyOverlayLabel* label = it->second; |
175 label->SetKeyState(state); | 186 label->SetKeyState(state); |
176 } | 187 } |
177 } | 188 } |
178 | 189 |
(...skipping 29 matching lines...) Expand all Loading... |
208 : is_visible_(false), | 219 : is_visible_(false), |
209 overlay_view_(new StickyKeysOverlayView), | 220 overlay_view_(new StickyKeysOverlayView), |
210 widget_size_(overlay_view_->GetPreferredSize()) { | 221 widget_size_(overlay_view_->GetPreferredSize()) { |
211 views::Widget::InitParams params; | 222 views::Widget::InitParams params; |
212 params.type = views::Widget::InitParams::TYPE_POPUP; | 223 params.type = views::Widget::InitParams::TYPE_POPUP; |
213 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; | 224 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; |
214 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 225 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
215 params.accept_events = false; | 226 params.accept_events = false; |
216 params.keep_on_top = true; | 227 params.keep_on_top = true; |
217 params.remove_standard_frame = true; | 228 params.remove_standard_frame = true; |
218 params.delegate = overlay_view_; | 229 params.delegate = overlay_view_.get(); |
219 params.bounds = CalculateOverlayBounds(); | 230 params.bounds = CalculateOverlayBounds(); |
220 params.parent = Shell::GetContainer(Shell::GetTargetRootWindow(), | 231 params.parent = Shell::GetContainer(Shell::GetTargetRootWindow(), |
221 kShellWindowId_OverlayContainer); | 232 kShellWindowId_OverlayContainer); |
222 overlay_widget_.reset(new views::Widget); | 233 overlay_widget_.reset(new views::Widget); |
223 overlay_widget_->Init(params); | 234 overlay_widget_->Init(params); |
224 overlay_widget_->SetVisibilityChangedAnimationsEnabled(false); | 235 overlay_widget_->SetVisibilityChangedAnimationsEnabled(false); |
225 overlay_widget_->SetContentsView(overlay_view_); | 236 overlay_widget_->SetContentsView(overlay_view_.get()); |
226 overlay_widget_->GetNativeView()->SetName("StickyKeysOverlay"); | 237 overlay_widget_->GetNativeView()->SetName("StickyKeysOverlay"); |
227 } | 238 } |
228 | 239 |
229 StickyKeysOverlay::~StickyKeysOverlay() {} | 240 StickyKeysOverlay::~StickyKeysOverlay() {} |
230 | 241 |
231 void StickyKeysOverlay::Show(bool visible) { | 242 void StickyKeysOverlay::Show(bool visible) { |
232 if (is_visible_ == visible) | 243 if (is_visible_ == visible) |
233 return; | 244 return; |
234 | 245 |
235 is_visible_ = visible; | 246 is_visible_ = visible; |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 ui::LayerAnimator* animator = overlay_widget_->GetLayer()->GetAnimator(); | 309 ui::LayerAnimator* animator = overlay_widget_->GetLayer()->GetAnimator(); |
299 if (animator) | 310 if (animator) |
300 animator->RemoveObserver(this); | 311 animator->RemoveObserver(this); |
301 } | 312 } |
302 | 313 |
303 void StickyKeysOverlay::OnLayerAnimationScheduled( | 314 void StickyKeysOverlay::OnLayerAnimationScheduled( |
304 ui::LayerAnimationSequence* sequence) { | 315 ui::LayerAnimationSequence* sequence) { |
305 } | 316 } |
306 | 317 |
307 } // namespace ash | 318 } // namespace ash |
OLD | NEW |