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

Side by Side Diff: ui/views/controls/tabbed_pane/tabbed_pane.cc

Issue 2368283002: views: add focus to TabbedPane (Closed)
Patch Set: DomKey -> KeyboardCode Created 4 years, 2 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
« no previous file with comments | « ui/views/controls/tabbed_pane/tabbed_pane.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "ui/views/controls/tabbed_pane/tabbed_pane.h" 5 #include "ui/views/controls/tabbed_pane/tabbed_pane.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/macros.h" 8 #include "base/macros.h"
9 #include "third_party/skia/include/core/SkPaint.h" 9 #include "third_party/skia/include/core/SkPaint.h"
10 #include "third_party/skia/include/core/SkPath.h" 10 #include "third_party/skia/include/core/SkPath.h"
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 static const char kViewClassName[]; 53 static const char kViewClassName[];
54 54
55 Tab(TabbedPane* tabbed_pane, const base::string16& title, View* contents); 55 Tab(TabbedPane* tabbed_pane, const base::string16& title, View* contents);
56 ~Tab() override; 56 ~Tab() override;
57 57
58 View* contents() const { return contents_; } 58 View* contents() const { return contents_; }
59 59
60 bool selected() const { return contents_->visible(); } 60 bool selected() const { return contents_->visible(); }
61 void SetSelected(bool selected); 61 void SetSelected(bool selected);
62 62
63 // This is a bit odd: since Tabs are not actually focusable, but rather the
64 // containing TabStrip is, Tabs need to be told when the TabStrip gains or
65 // loses focus so they can swap to/from the focus ring border.
66 void OnContainerFocusChanged();
Evan Stade 2016/09/28 17:32:32 this doesn't seem necessary any more? At least the
Elly Fong-Jones 2016/09/28 18:01:02 Done.
67
63 // Overridden from View: 68 // Overridden from View:
64 bool OnMousePressed(const ui::MouseEvent& event) override; 69 bool OnMousePressed(const ui::MouseEvent& event) override;
65 void OnMouseEntered(const ui::MouseEvent& event) override; 70 void OnMouseEntered(const ui::MouseEvent& event) override;
66 void OnMouseExited(const ui::MouseEvent& event) override; 71 void OnMouseExited(const ui::MouseEvent& event) override;
67 void OnGestureEvent(ui::GestureEvent* event) override; 72 void OnGestureEvent(ui::GestureEvent* event) override;
68 gfx::Size GetPreferredSize() const override; 73 gfx::Size GetPreferredSize() const override;
69 void Layout() override; 74 void Layout() override;
70 const char* GetClassName() const override; 75 const char* GetClassName() const override;
76 void OnFocus() override;
77 void OnBlur() override;
71 78
72 protected: 79 protected:
73 Label* title() { return title_; } 80 Label* title() { return title_; }
74 81
75 // Called whenever |tab_state_| changes. 82 // Called whenever |tab_state_| changes.
76 virtual void OnStateChanged(); 83 virtual void OnStateChanged();
77 84
85 // Returns whether the containing TabStrip has focus.
86 bool ContainerHasFocus();
87
78 private: 88 private:
79 enum TabState { 89 enum TabState {
80 TAB_INACTIVE, 90 TAB_INACTIVE,
81 TAB_ACTIVE, 91 TAB_ACTIVE,
82 TAB_HOVERED, 92 TAB_HOVERED,
83 }; 93 };
84 94
85 void SetState(TabState tab_state); 95 void SetState(TabState tab_state);
86 96
87 TabbedPane* tabbed_pane_; 97 TabbedPane* tabbed_pane_;
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 172
163 SetState(TAB_INACTIVE); 173 SetState(TAB_INACTIVE);
164 AddChildView(title_); 174 AddChildView(title_);
165 } 175 }
166 176
167 Tab::~Tab() {} 177 Tab::~Tab() {}
168 178
169 void Tab::SetSelected(bool selected) { 179 void Tab::SetSelected(bool selected) {
170 contents_->SetVisible(selected); 180 contents_->SetVisible(selected);
171 SetState(selected ? TAB_ACTIVE : TAB_INACTIVE); 181 SetState(selected ? TAB_ACTIVE : TAB_INACTIVE);
182 #if defined(OS_MACOSX)
183 SetFocusBehavior(selected ? FocusBehavior::ACCESSIBLE_ONLY
184 : FocusBehavior::NEVER);
185 #else
186 SetFocusBehavior(selected ? FocusBehavior::ALWAYS : FocusBehavior::NEVER);
187 #endif
172 } 188 }
173 189
174 void Tab::OnStateChanged() { 190 void Tab::OnStateChanged() {
175 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 191 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
176 switch (tab_state_) { 192 switch (tab_state_) {
177 case TAB_INACTIVE: 193 case TAB_INACTIVE:
178 title_->SetEnabledColor(kTabTitleColor_Inactive); 194 title_->SetEnabledColor(kTabTitleColor_Inactive);
179 title_->SetFontList(rb.GetFontListWithDelta( 195 title_->SetFontList(rb.GetFontListWithDelta(
180 ui::kLabelFontSizeDelta, gfx::Font::NORMAL, kInactiveWeight)); 196 ui::kLabelFontSizeDelta, gfx::Font::NORMAL, kInactiveWeight));
181 break; 197 break;
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
245 } 261 }
246 262
247 void Tab::SetState(TabState tab_state) { 263 void Tab::SetState(TabState tab_state) {
248 if (tab_state == tab_state_) 264 if (tab_state == tab_state_)
249 return; 265 return;
250 tab_state_ = tab_state; 266 tab_state_ = tab_state;
251 OnStateChanged(); 267 OnStateChanged();
252 SchedulePaint(); 268 SchedulePaint();
253 } 269 }
254 270
271 bool Tab::ContainerHasFocus() {
272 return tabbed_pane_->HasFocus();
273 }
274
275 void Tab::OnContainerFocusChanged() {
276 OnStateChanged();
277 SchedulePaint();
278 }
279
280 void Tab::OnFocus() {
281 OnStateChanged();
282 if (contents())
283 contents()->NotifyAccessibilityEvent(ui::AX_EVENT_FOCUS, true);
284 SchedulePaint();
285 }
286
287 void Tab::OnBlur() {
288 OnStateChanged();
289 SchedulePaint();
290 }
291
292 // The border used for MdTabs when they are both focused and selected: a
293 // rectangle with the bottom side in a base color and the other sides in a
294 // lightened version of the base color.
295 class MdTabFocusRingBorder : public Border {
296 public:
297 MdTabFocusRingBorder();
298 ~MdTabFocusRingBorder() override;
299
300 private:
301 void Paint(const View& view, gfx::Canvas* canvas) override;
302 gfx::Insets GetInsets() const override;
303 gfx::Size GetMinimumSize() const override;
304 };
305
306 MdTabFocusRingBorder::MdTabFocusRingBorder() {}
307 MdTabFocusRingBorder::~MdTabFocusRingBorder() {}
308
309 void MdTabFocusRingBorder::Paint(const View& view, gfx::Canvas* canvas) {
310 gfx::Insets insets = GetInsets();
311 // TODO(ellyjones): should this 0x66 be part of NativeTheme somehow?
312 SkColor base_color = view.GetNativeTheme()->GetSystemColor(
313 ui::NativeTheme::kColorId_FocusedBorderColor);
314 SkColor light_color = SkColorSetA(base_color, 0x66);
315 const int kBorderStrokeWidth = 2;
316
317 SkPaint paint;
318 paint.setColor(light_color);
319 paint.setStyle(SkPaint::kStroke_Style);
320 paint.setStrokeWidth(kBorderStrokeWidth);
321
322 gfx::Rect bounds = gfx::Rect(0, 0, view.width(), view.height());
323 bounds.Inset(kBorderStrokeWidth / 2, kBorderStrokeWidth / 2);
324
325 // Draw the lighter-colored stroke first, then draw the heavier stroke over
326 // the bottom of it. This is fine because the heavier stroke has 1.0 alpha, so
327 // the lighter stroke won't show through.
328 canvas->DrawRect(bounds, paint);
329 canvas->FillRect(gfx::Rect(0, view.height() - insets.bottom(), view.width(),
330 insets.bottom()),
331 base_color);
332 }
333
334 gfx::Insets MdTabFocusRingBorder::GetInsets() const {
335 return gfx::Insets(2, 2);
336 }
337
338 gfx::Size MdTabFocusRingBorder::GetMinimumSize() const {
339 return gfx::Size(GetInsets().width(), GetInsets().height());
340 }
341
255 MdTab::MdTab(TabbedPane* tabbed_pane, 342 MdTab::MdTab(TabbedPane* tabbed_pane,
256 const base::string16& title, 343 const base::string16& title,
257 View* contents) 344 View* contents)
258 : Tab(tabbed_pane, title, contents) { 345 : Tab(tabbed_pane, title, contents) {
259 OnStateChanged(); 346 OnStateChanged();
260 } 347 }
261 348
262 MdTab::~MdTab() {} 349 MdTab::~MdTab() {}
263 350
264 void MdTab::OnStateChanged() { 351 void MdTab::OnStateChanged() {
265 ui::NativeTheme* theme = GetNativeTheme(); 352 ui::NativeTheme* theme = GetNativeTheme();
266 SkColor border_color = theme->GetSystemColor( 353 SkColor border_color = theme->GetSystemColor(
267 selected() ? ui::NativeTheme::kColorId_FocusedBorderColor 354 selected() ? ui::NativeTheme::kColorId_FocusedBorderColor
268 : ui::NativeTheme::kColorId_UnfocusedBorderColor); 355 : ui::NativeTheme::kColorId_UnfocusedBorderColor);
269 int border_thickness = selected() ? 2 : 1; 356 int border_thickness = selected() ? 2 : 1;
270 SetBorder( 357 if (HasFocus() && selected()) {
271 Border::CreateSolidSidedBorder(0, 0, border_thickness, 0, border_color)); 358 SetBorder(base::MakeUnique<MdTabFocusRingBorder>());
359 } else {
360 SetBorder(Border::CreateSolidSidedBorder(0, 0, border_thickness, 0,
361 border_color));
362 }
272 363
273 SkColor font_color = selected() 364 SkColor font_color = selected()
274 ? theme->GetSystemColor(ui::NativeTheme::kColorId_CallToActionColor) 365 ? theme->GetSystemColor(ui::NativeTheme::kColorId_CallToActionColor)
275 : theme->GetSystemColor(ui::NativeTheme::kColorId_ButtonEnabledColor); 366 : theme->GetSystemColor(ui::NativeTheme::kColorId_ButtonEnabledColor);
276 title()->SetEnabledColor(font_color); 367 title()->SetEnabledColor(font_color);
277 368
278 gfx::Font::Weight font_weight = gfx::Font::Weight::MEDIUM; 369 gfx::Font::Weight font_weight = gfx::Font::Weight::MEDIUM;
279 #if defined(OS_WIN) 370 #if defined(OS_WIN)
280 if (selected()) 371 if (selected())
281 font_weight = gfx::Font::Weight::BOLD; 372 font_weight = gfx::Font::Weight::BOLD;
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
379 OnPaintBackground(canvas); 470 OnPaintBackground(canvas);
380 } 471 }
381 472
382 TabbedPane::TabbedPane() 473 TabbedPane::TabbedPane()
383 : listener_(NULL), 474 : listener_(NULL),
384 tab_strip_(ui::MaterialDesignController::IsSecondaryUiMaterial() 475 tab_strip_(ui::MaterialDesignController::IsSecondaryUiMaterial()
385 ? new MdTabStrip(this) 476 ? new MdTabStrip(this)
386 : new TabStrip(this)), 477 : new TabStrip(this)),
387 contents_(new View()), 478 contents_(new View()),
388 selected_tab_index_(-1) { 479 selected_tab_index_(-1) {
389 #if defined(OS_MACOSX)
390 SetFocusBehavior(FocusBehavior::ACCESSIBLE_ONLY);
391 #else
392 SetFocusBehavior(FocusBehavior::ALWAYS);
393 #endif
394
395 AddChildView(tab_strip_); 480 AddChildView(tab_strip_);
396 AddChildView(contents_); 481 AddChildView(contents_);
397 } 482 }
398 483
399 TabbedPane::~TabbedPane() {} 484 TabbedPane::~TabbedPane() {}
400 485
401 int TabbedPane::GetTabCount() { 486 int TabbedPane::GetTabCount() {
402 DCHECK_EQ(tab_strip_->child_count(), contents_->child_count()); 487 DCHECK_EQ(tab_strip_->child_count(), contents_->child_count());
403 return contents_->child_count(); 488 return contents_->child_count();
404 } 489 }
405 490
406 View* TabbedPane::GetSelectedTab() {
407 return selected_tab_index() < 0 ?
408 NULL : GetTabAt(selected_tab_index())->contents();
409 }
410
411 void TabbedPane::AddTab(const base::string16& title, View* contents) { 491 void TabbedPane::AddTab(const base::string16& title, View* contents) {
412 AddTabAtIndex(tab_strip_->child_count(), title, contents); 492 AddTabAtIndex(tab_strip_->child_count(), title, contents);
413 } 493 }
414 494
415 void TabbedPane::AddTabAtIndex(int index, 495 void TabbedPane::AddTabAtIndex(int index,
416 const base::string16& title, 496 const base::string16& title,
417 View* contents) { 497 View* contents) {
418 DCHECK(index >= 0 && index <= GetTabCount()); 498 DCHECK(index >= 0 && index <= GetTabCount());
419 contents->SetVisible(false); 499 contents->SetVisible(false);
420 500
421 tab_strip_->AddChildViewAt( 501 tab_strip_->AddChildViewAt(
422 ui::MaterialDesignController::IsSecondaryUiMaterial() 502 ui::MaterialDesignController::IsSecondaryUiMaterial()
423 ? new MdTab(this, title, contents) 503 ? new MdTab(this, title, contents)
424 : new Tab(this, title, contents), 504 : new Tab(this, title, contents),
425 index); 505 index);
426 contents_->AddChildViewAt(contents, index); 506 contents_->AddChildViewAt(contents, index);
427 if (selected_tab_index() < 0) 507 if (selected_tab_index() < 0)
428 SelectTabAt(index); 508 SelectTabAt(index);
429 509
430 PreferredSizeChanged(); 510 PreferredSizeChanged();
431 } 511 }
432 512
433 void TabbedPane::SelectTabAt(int index) { 513 void TabbedPane::SelectTabAt(int index) {
434 DCHECK(index >= 0 && index < GetTabCount()); 514 DCHECK(index >= 0 && index < GetTabCount());
515 bool give_focus = false;
Evan Stade 2016/09/28 17:32:32 nit: declare after early return
Elly Fong-Jones 2016/09/28 18:01:02 Done.
435 if (index == selected_tab_index()) 516 if (index == selected_tab_index())
436 return; 517 return;
437 518
438 if (selected_tab_index() >= 0) 519 if (GetSelectedTab()) {
Evan Stade 2016/09/28 17:32:32 can we move this below the tab->SetSelected call b
Elly Fong-Jones 2016/09/28 18:01:02 Ooh, yes, good thinking! Done.
439 GetTabAt(selected_tab_index())->SetSelected(false); 520 give_focus = GetSelectedTab()->HasFocus();
521 GetSelectedTab()->SetSelected(false);
522 }
440 523
441 selected_tab_index_ = index; 524 selected_tab_index_ = index;
442 Tab* tab = GetTabAt(index); 525 Tab* tab = GetTabAt(index);
443 tab->SetSelected(true); 526 tab->SetSelected(true);
444 tab_strip_->SchedulePaint(); 527 tab_strip_->SchedulePaint();
445 528
529 if (give_focus)
530 tab->RequestFocus();
531
446 FocusManager* focus_manager = tab->contents()->GetFocusManager(); 532 FocusManager* focus_manager = tab->contents()->GetFocusManager();
447 if (focus_manager) { 533 if (focus_manager) {
448 const View* focused_view = focus_manager->GetFocusedView(); 534 const View* focused_view = focus_manager->GetFocusedView();
449 if (focused_view && contents_->Contains(focused_view) && 535 if (focused_view && contents_->Contains(focused_view) &&
450 !tab->contents()->Contains(focused_view)) 536 !tab->contents()->Contains(focused_view))
451 focus_manager->SetFocusedView(tab->contents()); 537 focus_manager->SetFocusedView(tab->contents());
452 } 538 }
453 539
454 if (listener()) 540 if (listener())
455 listener()->TabSelectedAt(index); 541 listener()->TabSelectedAt(index);
(...skipping 10 matching lines...) Expand all
466 for (int i = 0; i < contents_->child_count(); ++i) 552 for (int i = 0; i < contents_->child_count(); ++i)
467 size.SetToMax(contents_->child_at(i)->GetPreferredSize()); 553 size.SetToMax(contents_->child_at(i)->GetPreferredSize());
468 size.Enlarge(0, tab_strip_->GetPreferredSize().height()); 554 size.Enlarge(0, tab_strip_->GetPreferredSize().height());
469 return size; 555 return size;
470 } 556 }
471 557
472 Tab* TabbedPane::GetTabAt(int index) { 558 Tab* TabbedPane::GetTabAt(int index) {
473 return static_cast<Tab*>(tab_strip_->child_at(index)); 559 return static_cast<Tab*>(tab_strip_->child_at(index));
474 } 560 }
475 561
562 Tab* TabbedPane::GetSelectedTab() {
563 return selected_tab_index() >= 0 ? GetTabAt(selected_tab_index()) : nullptr;
564 }
565
566 bool TabbedPane::MoveSelectionBy(int delta) {
567 const int tab_count = GetTabCount();
568 if (tab_count <= 1)
569 return false;
570 int next_selected_index = (selected_tab_index() + delta) % tab_count;
571 if (next_selected_index < 0)
572 next_selected_index += tab_count;
Evan Stade 2016/09/28 17:32:32 nit: I think you can just add tab_count in with de
Elly Fong-Jones 2016/09/28 18:01:01 I think the reason this code is done this way is t
Evan Stade 2016/09/28 21:25:54 good point
573 SelectTabAt(next_selected_index);
574 return true;
575 }
576
476 void TabbedPane::Layout() { 577 void TabbedPane::Layout() {
477 const gfx::Size size = tab_strip_->GetPreferredSize(); 578 const gfx::Size size = tab_strip_->GetPreferredSize();
478 tab_strip_->SetBounds(0, 0, width(), size.height()); 579 tab_strip_->SetBounds(0, 0, width(), size.height());
479 contents_->SetBounds(0, tab_strip_->bounds().bottom(), width(), 580 contents_->SetBounds(0, tab_strip_->bounds().bottom(), width(),
480 std::max(0, height() - size.height())); 581 std::max(0, height() - size.height()));
481 for (int i = 0; i < contents_->child_count(); ++i) 582 for (int i = 0; i < contents_->child_count(); ++i)
482 contents_->child_at(i)->SetSize(contents_->size()); 583 contents_->child_at(i)->SetSize(contents_->size());
483 } 584 }
484 585
485 void TabbedPane::ViewHierarchyChanged( 586 void TabbedPane::ViewHierarchyChanged(
486 const ViewHierarchyChangedDetails& details) { 587 const ViewHierarchyChangedDetails& details) {
487 if (details.is_add) { 588 if (details.is_add) {
488 // Support navigating tabs by Ctrl+Tab and Ctrl+Shift+Tab. 589 // Support navigating tabs by Ctrl+Tab and Ctrl+Shift+Tab.
489 AddAccelerator(ui::Accelerator(ui::VKEY_TAB, 590 AddAccelerator(ui::Accelerator(ui::VKEY_TAB,
490 ui::EF_CONTROL_DOWN | ui::EF_SHIFT_DOWN)); 591 ui::EF_CONTROL_DOWN | ui::EF_SHIFT_DOWN));
491 AddAccelerator(ui::Accelerator(ui::VKEY_TAB, ui::EF_CONTROL_DOWN)); 592 AddAccelerator(ui::Accelerator(ui::VKEY_TAB, ui::EF_CONTROL_DOWN));
492 } 593 }
493 } 594 }
494 595
495 bool TabbedPane::AcceleratorPressed(const ui::Accelerator& accelerator) { 596 bool TabbedPane::AcceleratorPressed(const ui::Accelerator& accelerator) {
496 // Handle Ctrl+Tab and Ctrl+Shift+Tab navigation of pages. 597 // Handle Ctrl+Tab and Ctrl+Shift+Tab navigation of pages.
497 DCHECK(accelerator.key_code() == ui::VKEY_TAB && accelerator.IsCtrlDown()); 598 DCHECK(accelerator.key_code() == ui::VKEY_TAB && accelerator.IsCtrlDown());
498 const int tab_count = GetTabCount(); 599 return MoveSelectionBy(accelerator.IsShiftDown() ? -1 : 1);
499 if (tab_count <= 1) 600 }
601
602 bool TabbedPane::OnKeyPressed(const ui::KeyEvent& event) {
603 if (!GetSelectedTab() || !GetSelectedTab()->HasFocus())
500 return false; 604 return false;
501 const int increment = accelerator.IsShiftDown() ? -1 : 1; 605 ui::KeyboardCode key = event.key_code();
502 int next_tab_index = (selected_tab_index() + increment) % tab_count; 606 if (key != ui::VKEY_LEFT && key != ui::VKEY_RIGHT)
503 // Wrap around. 607 return false;
504 if (next_tab_index < 0) 608 return MoveSelectionBy(key == ui::VKEY_RIGHT ? 1 : -1);
505 next_tab_index += tab_count;
506 SelectTabAt(next_tab_index);
507 return true;
508 } 609 }
509 610
510 const char* TabbedPane::GetClassName() const { 611 const char* TabbedPane::GetClassName() const {
511 return kViewClassName; 612 return kViewClassName;
512 } 613 }
513 614
514 void TabbedPane::OnFocus() { 615 void TabbedPane::OnFocus() {
515 View::OnFocus(); 616 View::OnFocus();
516 617
517 View* selected_tab = GetSelectedTab(); 618 Tab* selected = GetSelectedTab();
518 if (selected_tab) { 619 if (selected) {
519 selected_tab->NotifyAccessibilityEvent( 620 selected->OnContainerFocusChanged();
520 ui::AX_EVENT_FOCUS, true); 621 if (selected->contents())
622 selected->contents()->NotifyAccessibilityEvent(ui::AX_EVENT_FOCUS, true);
521 } 623 }
522 } 624 }
523 625
626 void TabbedPane::OnBlur() {
627 View::OnBlur();
628 Tab* selected_tab = GetSelectedTab();
629 if (selected_tab)
630 selected_tab->OnContainerFocusChanged();
631 }
632
524 void TabbedPane::GetAccessibleState(ui::AXViewState* state) { 633 void TabbedPane::GetAccessibleState(ui::AXViewState* state) {
525 state->role = ui::AX_ROLE_TAB_LIST; 634 state->role = ui::AX_ROLE_TAB_LIST;
526 } 635 }
527 636
528 } // namespace views 637 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/controls/tabbed_pane/tabbed_pane.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698