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

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

Issue 2368283002: views: add focus to TabbedPane (Closed)
Patch Set: remove MdTabFocusRingBorder 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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 void SetSelected(bool selected); 61 void SetSelected(bool selected);
62 62
63 // Overridden from View: 63 // Overridden from View:
64 bool OnMousePressed(const ui::MouseEvent& event) override; 64 bool OnMousePressed(const ui::MouseEvent& event) override;
65 void OnMouseEntered(const ui::MouseEvent& event) override; 65 void OnMouseEntered(const ui::MouseEvent& event) override;
66 void OnMouseExited(const ui::MouseEvent& event) override; 66 void OnMouseExited(const ui::MouseEvent& event) override;
67 void OnGestureEvent(ui::GestureEvent* event) override; 67 void OnGestureEvent(ui::GestureEvent* event) override;
68 gfx::Size GetPreferredSize() const override; 68 gfx::Size GetPreferredSize() const override;
69 void Layout() override; 69 void Layout() override;
70 const char* GetClassName() const override; 70 const char* GetClassName() const override;
71 void OnFocus() override;
72 void OnBlur() override;
71 73
72 protected: 74 protected:
73 Label* title() { return title_; } 75 Label* title() { return title_; }
74 76
75 // Called whenever |tab_state_| changes. 77 // Called whenever |tab_state_| changes.
76 virtual void OnStateChanged(); 78 virtual void OnStateChanged();
77 79
80 // Returns whether the containing TabStrip has focus.
81 bool ContainerHasFocus();
82
78 private: 83 private:
79 enum TabState { 84 enum TabState {
80 TAB_INACTIVE, 85 TAB_INACTIVE,
81 TAB_ACTIVE, 86 TAB_ACTIVE,
82 TAB_HOVERED, 87 TAB_HOVERED,
83 }; 88 };
84 89
85 void SetState(TabState tab_state); 90 void SetState(TabState tab_state);
86 91
87 TabbedPane* tabbed_pane_; 92 TabbedPane* tabbed_pane_;
88 Label* title_; 93 Label* title_;
89 gfx::Size preferred_title_size_; 94 gfx::Size preferred_title_size_;
90 TabState tab_state_; 95 TabState tab_state_;
91 // The content view associated with this tab. 96 // The content view associated with this tab.
92 View* contents_; 97 View* contents_;
93 98
94 DISALLOW_COPY_AND_ASSIGN(Tab); 99 DISALLOW_COPY_AND_ASSIGN(Tab);
95 }; 100 };
96 101
97 // A subclass of Tab that implements the Harmony visual styling. 102 // A subclass of Tab that implements the Harmony visual styling.
98 class MdTab : public Tab { 103 class MdTab : public Tab {
99 public: 104 public:
100 MdTab(TabbedPane* tabbed_pane, const base::string16& title, View* contents); 105 MdTab(TabbedPane* tabbed_pane, const base::string16& title, View* contents);
101 ~MdTab() override; 106 ~MdTab() override;
102 107
103 // Overridden from Tab: 108 // Overridden from Tab:
104 void OnStateChanged() override; 109 void OnStateChanged() override;
105 110
111 // Overridden from View:
112 void OnPaintBorder(gfx::Canvas* canvas) override;
113
106 private: 114 private:
107 DISALLOW_COPY_AND_ASSIGN(MdTab); 115 DISALLOW_COPY_AND_ASSIGN(MdTab);
108 }; 116 };
109 117
110 // The tab strip shown above the tab contents. 118 // The tab strip shown above the tab contents.
111 class TabStrip : public View { 119 class TabStrip : public View {
112 public: 120 public:
113 // Internal class name. 121 // Internal class name.
114 static const char kViewClassName[]; 122 static const char kViewClassName[];
115 123
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 170
163 SetState(TAB_INACTIVE); 171 SetState(TAB_INACTIVE);
164 AddChildView(title_); 172 AddChildView(title_);
165 } 173 }
166 174
167 Tab::~Tab() {} 175 Tab::~Tab() {}
168 176
169 void Tab::SetSelected(bool selected) { 177 void Tab::SetSelected(bool selected) {
170 contents_->SetVisible(selected); 178 contents_->SetVisible(selected);
171 SetState(selected ? TAB_ACTIVE : TAB_INACTIVE); 179 SetState(selected ? TAB_ACTIVE : TAB_INACTIVE);
180 #if defined(OS_MACOSX)
181 SetFocusBehavior(selected ? FocusBehavior::ACCESSIBLE_ONLY
182 : FocusBehavior::NEVER);
183 #else
184 SetFocusBehavior(selected ? FocusBehavior::ALWAYS : FocusBehavior::NEVER);
185 #endif
172 } 186 }
173 187
174 void Tab::OnStateChanged() { 188 void Tab::OnStateChanged() {
175 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 189 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
176 switch (tab_state_) { 190 switch (tab_state_) {
177 case TAB_INACTIVE: 191 case TAB_INACTIVE:
178 title_->SetEnabledColor(kTabTitleColor_Inactive); 192 title_->SetEnabledColor(kTabTitleColor_Inactive);
179 title_->SetFontList(rb.GetFontListWithDelta( 193 title_->SetFontList(rb.GetFontListWithDelta(
180 ui::kLabelFontSizeDelta, gfx::Font::NORMAL, kInactiveWeight)); 194 ui::kLabelFontSizeDelta, gfx::Font::NORMAL, kInactiveWeight));
181 break; 195 break;
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
245 } 259 }
246 260
247 void Tab::SetState(TabState tab_state) { 261 void Tab::SetState(TabState tab_state) {
248 if (tab_state == tab_state_) 262 if (tab_state == tab_state_)
249 return; 263 return;
250 tab_state_ = tab_state; 264 tab_state_ = tab_state;
251 OnStateChanged(); 265 OnStateChanged();
252 SchedulePaint(); 266 SchedulePaint();
253 } 267 }
254 268
269 bool Tab::ContainerHasFocus() {
270 return tabbed_pane_->HasFocus();
271 }
272
273 void Tab::OnFocus() {
274 OnStateChanged();
275 if (contents())
276 contents()->NotifyAccessibilityEvent(ui::AX_EVENT_FOCUS, true);
277 SchedulePaint();
278 }
279
280 void Tab::OnBlur() {
281 OnStateChanged();
282 SchedulePaint();
283 }
284
255 MdTab::MdTab(TabbedPane* tabbed_pane, 285 MdTab::MdTab(TabbedPane* tabbed_pane,
256 const base::string16& title, 286 const base::string16& title,
257 View* contents) 287 View* contents)
258 : Tab(tabbed_pane, title, contents) { 288 : Tab(tabbed_pane, title, contents) {
259 OnStateChanged(); 289 OnStateChanged();
260 } 290 }
261 291
262 MdTab::~MdTab() {} 292 MdTab::~MdTab() {}
263 293
264 void MdTab::OnStateChanged() { 294 void MdTab::OnStateChanged() {
265 ui::NativeTheme* theme = GetNativeTheme(); 295 ui::NativeTheme* theme = GetNativeTheme();
266 SkColor border_color = theme->GetSystemColor(
267 selected() ? ui::NativeTheme::kColorId_FocusedBorderColor
268 : ui::NativeTheme::kColorId_UnfocusedBorderColor);
269 int border_thickness = selected() ? 2 : 1;
270 SetBorder(
271 Border::CreateSolidSidedBorder(0, 0, border_thickness, 0, border_color));
272 296
273 SkColor font_color = selected() 297 SkColor font_color = selected()
274 ? theme->GetSystemColor(ui::NativeTheme::kColorId_ProminentButtonColor) 298 ? theme->GetSystemColor(ui::NativeTheme::kColorId_ProminentButtonColor)
275 : theme->GetSystemColor(ui::NativeTheme::kColorId_ButtonEnabledColor); 299 : theme->GetSystemColor(ui::NativeTheme::kColorId_ButtonEnabledColor);
276 title()->SetEnabledColor(font_color); 300 title()->SetEnabledColor(font_color);
277 301
278 gfx::Font::Weight font_weight = gfx::Font::Weight::MEDIUM; 302 gfx::Font::Weight font_weight = gfx::Font::Weight::MEDIUM;
279 #if defined(OS_WIN) 303 #if defined(OS_WIN)
280 if (selected()) 304 if (selected())
281 font_weight = gfx::Font::Weight::BOLD; 305 font_weight = gfx::Font::Weight::BOLD;
282 #endif 306 #endif
283 307
284 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 308 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
285 title()->SetFontList(rb.GetFontListWithDelta(ui::kLabelFontSizeDelta, 309 title()->SetFontList(rb.GetFontListWithDelta(ui::kLabelFontSizeDelta,
286 gfx::Font::NORMAL, font_weight)); 310 gfx::Font::NORMAL, font_weight));
287 } 311 }
288 312
313 void MdTab::OnPaintBorder(gfx::Canvas* canvas) {
314 const int kBorderStrokeWidth = 2;
315 if (!HasFocus()) {
316 SkColor color = GetNativeTheme()->GetSystemColor(
317 selected() ? ui::NativeTheme::kColorId_FocusedBorderColor
318 : ui::NativeTheme::kColorId_UnfocusedBorderColor);
319 int thickness = selected() ? kBorderStrokeWidth : kBorderStrokeWidth / 2;
320 canvas->FillRect(
321 gfx::Rect(0, height() - thickness, width(), thickness),
322 color);
323 return;
324 }
325
326 // TODO(ellyjones): should this 0x66 be part of NativeTheme somehow?
327 SkColor base_color = GetNativeTheme()->GetSystemColor(
328 ui::NativeTheme::kColorId_FocusedBorderColor);
329 SkColor light_color = SkColorSetA(base_color, 0x66);
330
331 SkPaint paint;
332 paint.setColor(light_color);
333 paint.setStyle(SkPaint::kStroke_Style);
334 paint.setStrokeWidth(kBorderStrokeWidth);
335
336 gfx::Rect bounds = GetLocalBounds();
337 bounds.Inset(kBorderStrokeWidth / 2, kBorderStrokeWidth / 2);
Evan Stade 2016/10/03 19:15:16 nit: can be bounds.Inset(gfx::Insets(kBorderStr
Elly Fong-Jones 2016/10/04 18:04:40 Done.
338
339 // Draw the lighter-colored stroke first, then draw the heavier stroke over
340 // the bottom of it. This is fine because the heavier stroke has 1.0 alpha, so
341 // the lighter stroke won't show through.
342 canvas->DrawRect(bounds, paint);
Evan Stade 2016/10/03 19:15:15 this version of DrawRect is deprecated (see canvas
Elly Fong-Jones 2016/10/04 18:04:40 Done.
343 canvas->FillRect(gfx::Rect(0, height() - kBorderStrokeWidth, width(),
344 kBorderStrokeWidth),
345 base_color);
346 }
347
289 // static 348 // static
290 const char TabStrip::kViewClassName[] = "TabStrip"; 349 const char TabStrip::kViewClassName[] = "TabStrip";
291 350
292 TabStrip::TabStrip(TabbedPane* tabbed_pane) : tabbed_pane_(tabbed_pane) {} 351 TabStrip::TabStrip(TabbedPane* tabbed_pane) : tabbed_pane_(tabbed_pane) {}
293 352
294 TabStrip::~TabStrip() {} 353 TabStrip::~TabStrip() {}
295 354
296 gfx::Size TabStrip::GetPreferredSize() const { 355 gfx::Size TabStrip::GetPreferredSize() const {
297 gfx::Size size; 356 gfx::Size size;
298 for (int i = 0; i < child_count(); ++i) { 357 for (int i = 0; i < child_count(); ++i) {
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
379 OnPaintBackground(canvas); 438 OnPaintBackground(canvas);
380 } 439 }
381 440
382 TabbedPane::TabbedPane() 441 TabbedPane::TabbedPane()
383 : listener_(NULL), 442 : listener_(NULL),
384 tab_strip_(ui::MaterialDesignController::IsSecondaryUiMaterial() 443 tab_strip_(ui::MaterialDesignController::IsSecondaryUiMaterial()
385 ? new MdTabStrip(this) 444 ? new MdTabStrip(this)
386 : new TabStrip(this)), 445 : new TabStrip(this)),
387 contents_(new View()), 446 contents_(new View()),
388 selected_tab_index_(-1) { 447 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_); 448 AddChildView(tab_strip_);
396 AddChildView(contents_); 449 AddChildView(contents_);
397 } 450 }
398 451
399 TabbedPane::~TabbedPane() {} 452 TabbedPane::~TabbedPane() {}
400 453
401 int TabbedPane::GetTabCount() { 454 int TabbedPane::GetTabCount() {
402 DCHECK_EQ(tab_strip_->child_count(), contents_->child_count()); 455 DCHECK_EQ(tab_strip_->child_count(), contents_->child_count());
403 return contents_->child_count(); 456 return contents_->child_count();
404 } 457 }
405 458
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) { 459 void TabbedPane::AddTab(const base::string16& title, View* contents) {
412 AddTabAtIndex(tab_strip_->child_count(), title, contents); 460 AddTabAtIndex(tab_strip_->child_count(), title, contents);
413 } 461 }
414 462
415 void TabbedPane::AddTabAtIndex(int index, 463 void TabbedPane::AddTabAtIndex(int index,
416 const base::string16& title, 464 const base::string16& title,
417 View* contents) { 465 View* contents) {
418 DCHECK(index >= 0 && index <= GetTabCount()); 466 DCHECK(index >= 0 && index <= GetTabCount());
419 contents->SetVisible(false); 467 contents->SetVisible(false);
420 468
421 tab_strip_->AddChildViewAt( 469 tab_strip_->AddChildViewAt(
422 ui::MaterialDesignController::IsSecondaryUiMaterial() 470 ui::MaterialDesignController::IsSecondaryUiMaterial()
423 ? new MdTab(this, title, contents) 471 ? new MdTab(this, title, contents)
424 : new Tab(this, title, contents), 472 : new Tab(this, title, contents),
425 index); 473 index);
426 contents_->AddChildViewAt(contents, index); 474 contents_->AddChildViewAt(contents, index);
427 if (selected_tab_index() < 0) 475 if (selected_tab_index() < 0)
428 SelectTabAt(index); 476 SelectTabAt(index);
429 477
430 PreferredSizeChanged(); 478 PreferredSizeChanged();
431 } 479 }
432 480
433 void TabbedPane::SelectTabAt(int index) { 481 void TabbedPane::SelectTabAt(int index) {
434 DCHECK(index >= 0 && index < GetTabCount()); 482 DCHECK(index >= 0 && index < GetTabCount());
435 if (index == selected_tab_index()) 483 if (index == selected_tab_index())
436 return; 484 return;
437 485
438 if (selected_tab_index() >= 0)
439 GetTabAt(selected_tab_index())->SetSelected(false);
440
441 selected_tab_index_ = index;
442 Tab* tab = GetTabAt(index); 486 Tab* tab = GetTabAt(index);
443 tab->SetSelected(true); 487 tab->SetSelected(true);
488 if (GetSelectedTab()) {
Evan Stade 2016/10/03 19:15:15 this is confusing on a couple levels: a) "tab" is
Elly Fong-Jones 2016/10/04 18:04:40 Done.
489 if (GetSelectedTab()->HasFocus())
490 tab->RequestFocus();
491 GetSelectedTab()->SetSelected(false);
492 }
493 selected_tab_index_ = index;
444 tab_strip_->SchedulePaint(); 494 tab_strip_->SchedulePaint();
445 495
446 FocusManager* focus_manager = tab->contents()->GetFocusManager(); 496 FocusManager* focus_manager = tab->contents()->GetFocusManager();
447 if (focus_manager) { 497 if (focus_manager) {
448 const View* focused_view = focus_manager->GetFocusedView(); 498 const View* focused_view = focus_manager->GetFocusedView();
449 if (focused_view && contents_->Contains(focused_view) && 499 if (focused_view && contents_->Contains(focused_view) &&
450 !tab->contents()->Contains(focused_view)) 500 !tab->contents()->Contains(focused_view))
451 focus_manager->SetFocusedView(tab->contents()); 501 focus_manager->SetFocusedView(tab->contents());
452 } 502 }
453 503
(...skipping 12 matching lines...) Expand all
466 for (int i = 0; i < contents_->child_count(); ++i) 516 for (int i = 0; i < contents_->child_count(); ++i)
467 size.SetToMax(contents_->child_at(i)->GetPreferredSize()); 517 size.SetToMax(contents_->child_at(i)->GetPreferredSize());
468 size.Enlarge(0, tab_strip_->GetPreferredSize().height()); 518 size.Enlarge(0, tab_strip_->GetPreferredSize().height());
469 return size; 519 return size;
470 } 520 }
471 521
472 Tab* TabbedPane::GetTabAt(int index) { 522 Tab* TabbedPane::GetTabAt(int index) {
473 return static_cast<Tab*>(tab_strip_->child_at(index)); 523 return static_cast<Tab*>(tab_strip_->child_at(index));
474 } 524 }
475 525
526 Tab* TabbedPane::GetSelectedTab() {
527 return selected_tab_index() >= 0 ? GetTabAt(selected_tab_index()) : nullptr;
528 }
529
530 bool TabbedPane::MoveSelectionBy(int delta) {
531 const int tab_count = GetTabCount();
532 if (tab_count <= 1)
533 return false;
534 int next_selected_index = (selected_tab_index() + delta) % tab_count;
535 if (next_selected_index < 0)
536 next_selected_index += tab_count;
537 SelectTabAt(next_selected_index);
538 return true;
539 }
540
476 void TabbedPane::Layout() { 541 void TabbedPane::Layout() {
477 const gfx::Size size = tab_strip_->GetPreferredSize(); 542 const gfx::Size size = tab_strip_->GetPreferredSize();
478 tab_strip_->SetBounds(0, 0, width(), size.height()); 543 tab_strip_->SetBounds(0, 0, width(), size.height());
479 contents_->SetBounds(0, tab_strip_->bounds().bottom(), width(), 544 contents_->SetBounds(0, tab_strip_->bounds().bottom(), width(),
480 std::max(0, height() - size.height())); 545 std::max(0, height() - size.height()));
481 for (int i = 0; i < contents_->child_count(); ++i) 546 for (int i = 0; i < contents_->child_count(); ++i)
482 contents_->child_at(i)->SetSize(contents_->size()); 547 contents_->child_at(i)->SetSize(contents_->size());
483 } 548 }
484 549
485 void TabbedPane::ViewHierarchyChanged( 550 void TabbedPane::ViewHierarchyChanged(
486 const ViewHierarchyChangedDetails& details) { 551 const ViewHierarchyChangedDetails& details) {
487 if (details.is_add) { 552 if (details.is_add) {
488 // Support navigating tabs by Ctrl+Tab and Ctrl+Shift+Tab. 553 // Support navigating tabs by Ctrl+Tab and Ctrl+Shift+Tab.
489 AddAccelerator(ui::Accelerator(ui::VKEY_TAB, 554 AddAccelerator(ui::Accelerator(ui::VKEY_TAB,
490 ui::EF_CONTROL_DOWN | ui::EF_SHIFT_DOWN)); 555 ui::EF_CONTROL_DOWN | ui::EF_SHIFT_DOWN));
491 AddAccelerator(ui::Accelerator(ui::VKEY_TAB, ui::EF_CONTROL_DOWN)); 556 AddAccelerator(ui::Accelerator(ui::VKEY_TAB, ui::EF_CONTROL_DOWN));
492 } 557 }
493 } 558 }
494 559
495 bool TabbedPane::AcceleratorPressed(const ui::Accelerator& accelerator) { 560 bool TabbedPane::AcceleratorPressed(const ui::Accelerator& accelerator) {
496 // Handle Ctrl+Tab and Ctrl+Shift+Tab navigation of pages. 561 // Handle Ctrl+Tab and Ctrl+Shift+Tab navigation of pages.
497 DCHECK(accelerator.key_code() == ui::VKEY_TAB && accelerator.IsCtrlDown()); 562 DCHECK(accelerator.key_code() == ui::VKEY_TAB && accelerator.IsCtrlDown());
498 const int tab_count = GetTabCount(); 563 return MoveSelectionBy(accelerator.IsShiftDown() ? -1 : 1);
499 if (tab_count <= 1) 564 }
565
566 bool TabbedPane::OnKeyPressed(const ui::KeyEvent& event) {
567 if (!GetSelectedTab() || !GetSelectedTab()->HasFocus())
500 return false; 568 return false;
501 const int increment = accelerator.IsShiftDown() ? -1 : 1; 569 ui::KeyboardCode key = event.key_code();
502 int next_tab_index = (selected_tab_index() + increment) % tab_count; 570 if (key != ui::VKEY_LEFT && key != ui::VKEY_RIGHT)
503 // Wrap around. 571 return false;
504 if (next_tab_index < 0) 572 return MoveSelectionBy(key == ui::VKEY_RIGHT ? 1 : -1);
505 next_tab_index += tab_count;
506 SelectTabAt(next_tab_index);
507 return true;
508 } 573 }
509 574
510 const char* TabbedPane::GetClassName() const { 575 const char* TabbedPane::GetClassName() const {
511 return kViewClassName; 576 return kViewClassName;
512 } 577 }
513 578
514 void TabbedPane::OnFocus() {
515 View::OnFocus();
516
517 View* selected_tab = GetSelectedTab();
518 if (selected_tab) {
519 selected_tab->NotifyAccessibilityEvent(
520 ui::AX_EVENT_FOCUS, true);
521 }
522 }
523
524 void TabbedPane::GetAccessibleState(ui::AXViewState* state) { 579 void TabbedPane::GetAccessibleState(ui::AXViewState* state) {
525 state->role = ui::AX_ROLE_TAB_LIST; 580 state->role = ui::AX_ROLE_TAB_LIST;
526 } 581 }
527 582
528 } // namespace views 583 } // 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