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

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

Issue 2368283002: views: add focus to TabbedPane (Closed)
Patch Set: move insets 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_;
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 167
163 SetState(TAB_INACTIVE); 168 SetState(TAB_INACTIVE);
164 AddChildView(title_); 169 AddChildView(title_);
165 } 170 }
166 171
167 Tab::~Tab() {} 172 Tab::~Tab() {}
168 173
169 void Tab::SetSelected(bool selected) { 174 void Tab::SetSelected(bool selected) {
170 contents_->SetVisible(selected); 175 contents_->SetVisible(selected);
171 SetState(selected ? TAB_ACTIVE : TAB_INACTIVE); 176 SetState(selected ? TAB_ACTIVE : TAB_INACTIVE);
177 #if defined(OS_MACOSX)
178 SetFocusBehavior(selected ? FocusBehavior::ACCESSIBLE_ONLY
179 : FocusBehavior::NEVER);
180 #else
181 SetFocusBehavior(selected ? FocusBehavior::ALWAYS : FocusBehavior::NEVER);
182 #endif
172 } 183 }
173 184
174 void Tab::OnStateChanged() { 185 void Tab::OnStateChanged() {
175 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 186 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
176 switch (tab_state_) { 187 switch (tab_state_) {
177 case TAB_INACTIVE: 188 case TAB_INACTIVE:
178 title_->SetEnabledColor(kTabTitleColor_Inactive); 189 title_->SetEnabledColor(kTabTitleColor_Inactive);
179 title_->SetFontList(rb.GetFontListWithDelta( 190 title_->SetFontList(rb.GetFontListWithDelta(
180 ui::kLabelFontSizeDelta, gfx::Font::NORMAL, kInactiveWeight)); 191 ui::kLabelFontSizeDelta, gfx::Font::NORMAL, kInactiveWeight));
181 break; 192 break;
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
245 } 256 }
246 257
247 void Tab::SetState(TabState tab_state) { 258 void Tab::SetState(TabState tab_state) {
248 if (tab_state == tab_state_) 259 if (tab_state == tab_state_)
249 return; 260 return;
250 tab_state_ = tab_state; 261 tab_state_ = tab_state;
251 OnStateChanged(); 262 OnStateChanged();
252 SchedulePaint(); 263 SchedulePaint();
253 } 264 }
254 265
266 bool Tab::ContainerHasFocus() {
267 return tabbed_pane_->HasFocus();
268 }
269
270 void Tab::OnFocus() {
271 OnStateChanged();
272 if (contents())
273 contents()->NotifyAccessibilityEvent(ui::AX_EVENT_FOCUS, true);
274 SchedulePaint();
275 }
276
277 void Tab::OnBlur() {
278 OnStateChanged();
279 SchedulePaint();
280 }
281
282 // The border used for MdTabs when they are both focused and selected: a
283 // rectangle with the bottom side in a base color and the other sides in a
284 // lightened version of the base color.
285 class MdTabFocusRingBorder : public Border {
286 public:
287 MdTabFocusRingBorder(MdTab* tab);
Evan Stade 2016/09/30 17:24:41 passing in the tab is what makes me think the bord
Elly Fong-Jones 2016/10/03 13:59:49 Done.
288 ~MdTabFocusRingBorder() override;
289
290 private:
291 static const int kBorderStrokeWidth = 2;
292
293 void Paint(const View& view, gfx::Canvas* canvas) override;
294 gfx::Insets GetInsets() const override;
295 gfx::Size GetMinimumSize() const override;
296
297 MdTab* tab_;
298 };
299
300 MdTabFocusRingBorder::MdTabFocusRingBorder(MdTab* tab) : tab_(tab) {}
301 MdTabFocusRingBorder::~MdTabFocusRingBorder() {}
302
303 void MdTabFocusRingBorder::Paint(const View& view, gfx::Canvas* canvas) {
304 if (!view.HasFocus()) {
305 SkColor color = view.GetNativeTheme()->GetSystemColor(
306 tab_->selected() ? ui::NativeTheme::kColorId_FocusedBorderColor
307 : ui::NativeTheme::kColorId_UnfocusedBorderColor);
308 int thickness =
309 tab_->selected() ? kBorderStrokeWidth : kBorderStrokeWidth / 2;
310 canvas->FillRect(
311 gfx::Rect(0, view.height() - thickness, view.width(), thickness),
312 color);
313 return;
314 }
315
316 // TODO(ellyjones): should this 0x66 be part of NativeTheme somehow?
317 SkColor base_color = view.GetNativeTheme()->GetSystemColor(
318 ui::NativeTheme::kColorId_FocusedBorderColor);
319 SkColor light_color = SkColorSetA(base_color, 0x66);
320
321 SkPaint paint;
322 paint.setColor(light_color);
323 paint.setStyle(SkPaint::kStroke_Style);
324 paint.setStrokeWidth(kBorderStrokeWidth);
325
326 gfx::Rect bounds = view.GetLocalBounds();
327 bounds.Inset(kBorderStrokeWidth / 2, kBorderStrokeWidth / 2);
328 gfx::Insets insets = GetInsets();
329
330 // Draw the lighter-colored stroke first, then draw the heavier stroke over
331 // the bottom of it. This is fine because the heavier stroke has 1.0 alpha, so
332 // the lighter stroke won't show through.
333 canvas->DrawRect(bounds, paint);
334 canvas->FillRect(gfx::Rect(0, view.height() - insets.bottom(), view.width(),
335 insets.bottom()),
336 base_color);
337 }
338
339 gfx::Insets MdTabFocusRingBorder::GetInsets() const {
340 return gfx::Insets(kBorderStrokeWidth);
341 }
342
343 gfx::Size MdTabFocusRingBorder::GetMinimumSize() const {
344 return gfx::Size(GetInsets().width(), GetInsets().height());
345 }
346
255 MdTab::MdTab(TabbedPane* tabbed_pane, 347 MdTab::MdTab(TabbedPane* tabbed_pane,
256 const base::string16& title, 348 const base::string16& title,
257 View* contents) 349 View* contents)
258 : Tab(tabbed_pane, title, contents) { 350 : Tab(tabbed_pane, title, contents) {
351 SetBorder(base::MakeUnique<MdTabFocusRingBorder>(this));
259 OnStateChanged(); 352 OnStateChanged();
260 } 353 }
261 354
262 MdTab::~MdTab() {} 355 MdTab::~MdTab() {}
263 356
264 void MdTab::OnStateChanged() { 357 void MdTab::OnStateChanged() {
265 ui::NativeTheme* theme = GetNativeTheme(); 358 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 359
273 SkColor font_color = selected() 360 SkColor font_color = selected()
274 ? theme->GetSystemColor(ui::NativeTheme::kColorId_ProminentButtonColor) 361 ? theme->GetSystemColor(ui::NativeTheme::kColorId_ProminentButtonColor)
275 : theme->GetSystemColor(ui::NativeTheme::kColorId_ButtonEnabledColor); 362 : theme->GetSystemColor(ui::NativeTheme::kColorId_ButtonEnabledColor);
276 title()->SetEnabledColor(font_color); 363 title()->SetEnabledColor(font_color);
277 364
278 gfx::Font::Weight font_weight = gfx::Font::Weight::MEDIUM; 365 gfx::Font::Weight font_weight = gfx::Font::Weight::MEDIUM;
279 #if defined(OS_WIN) 366 #if defined(OS_WIN)
280 if (selected()) 367 if (selected())
281 font_weight = gfx::Font::Weight::BOLD; 368 font_weight = gfx::Font::Weight::BOLD;
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
379 OnPaintBackground(canvas); 466 OnPaintBackground(canvas);
380 } 467 }
381 468
382 TabbedPane::TabbedPane() 469 TabbedPane::TabbedPane()
383 : listener_(NULL), 470 : listener_(NULL),
384 tab_strip_(ui::MaterialDesignController::IsSecondaryUiMaterial() 471 tab_strip_(ui::MaterialDesignController::IsSecondaryUiMaterial()
385 ? new MdTabStrip(this) 472 ? new MdTabStrip(this)
386 : new TabStrip(this)), 473 : new TabStrip(this)),
387 contents_(new View()), 474 contents_(new View()),
388 selected_tab_index_(-1) { 475 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_); 476 AddChildView(tab_strip_);
396 AddChildView(contents_); 477 AddChildView(contents_);
397 } 478 }
398 479
399 TabbedPane::~TabbedPane() {} 480 TabbedPane::~TabbedPane() {}
400 481
401 int TabbedPane::GetTabCount() { 482 int TabbedPane::GetTabCount() {
402 DCHECK_EQ(tab_strip_->child_count(), contents_->child_count()); 483 DCHECK_EQ(tab_strip_->child_count(), contents_->child_count());
403 return contents_->child_count(); 484 return contents_->child_count();
404 } 485 }
405 486
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) { 487 void TabbedPane::AddTab(const base::string16& title, View* contents) {
412 AddTabAtIndex(tab_strip_->child_count(), title, contents); 488 AddTabAtIndex(tab_strip_->child_count(), title, contents);
413 } 489 }
414 490
415 void TabbedPane::AddTabAtIndex(int index, 491 void TabbedPane::AddTabAtIndex(int index,
416 const base::string16& title, 492 const base::string16& title,
417 View* contents) { 493 View* contents) {
418 DCHECK(index >= 0 && index <= GetTabCount()); 494 DCHECK(index >= 0 && index <= GetTabCount());
419 contents->SetVisible(false); 495 contents->SetVisible(false);
420 496
421 tab_strip_->AddChildViewAt( 497 tab_strip_->AddChildViewAt(
422 ui::MaterialDesignController::IsSecondaryUiMaterial() 498 ui::MaterialDesignController::IsSecondaryUiMaterial()
423 ? new MdTab(this, title, contents) 499 ? new MdTab(this, title, contents)
424 : new Tab(this, title, contents), 500 : new Tab(this, title, contents),
425 index); 501 index);
426 contents_->AddChildViewAt(contents, index); 502 contents_->AddChildViewAt(contents, index);
427 if (selected_tab_index() < 0) 503 if (selected_tab_index() < 0)
428 SelectTabAt(index); 504 SelectTabAt(index);
429 505
430 PreferredSizeChanged(); 506 PreferredSizeChanged();
431 } 507 }
432 508
433 void TabbedPane::SelectTabAt(int index) { 509 void TabbedPane::SelectTabAt(int index) {
434 DCHECK(index >= 0 && index < GetTabCount()); 510 DCHECK(index >= 0 && index < GetTabCount());
435 if (index == selected_tab_index()) 511 if (index == selected_tab_index())
436 return; 512 return;
437 513
438 if (selected_tab_index() >= 0)
439 GetTabAt(selected_tab_index())->SetSelected(false);
440
441 selected_tab_index_ = index;
442 Tab* tab = GetTabAt(index); 514 Tab* tab = GetTabAt(index);
443 tab->SetSelected(true); 515 tab->SetSelected(true);
516 if (GetSelectedTab()) {
517 if (GetSelectedTab()->HasFocus())
518 tab->RequestFocus();
519 GetSelectedTab()->SetSelected(false);
520 }
521 selected_tab_index_ = index;
444 tab_strip_->SchedulePaint(); 522 tab_strip_->SchedulePaint();
445 523
446 FocusManager* focus_manager = tab->contents()->GetFocusManager(); 524 FocusManager* focus_manager = tab->contents()->GetFocusManager();
447 if (focus_manager) { 525 if (focus_manager) {
448 const View* focused_view = focus_manager->GetFocusedView(); 526 const View* focused_view = focus_manager->GetFocusedView();
449 if (focused_view && contents_->Contains(focused_view) && 527 if (focused_view && contents_->Contains(focused_view) &&
450 !tab->contents()->Contains(focused_view)) 528 !tab->contents()->Contains(focused_view))
451 focus_manager->SetFocusedView(tab->contents()); 529 focus_manager->SetFocusedView(tab->contents());
452 } 530 }
453 531
(...skipping 12 matching lines...) Expand all
466 for (int i = 0; i < contents_->child_count(); ++i) 544 for (int i = 0; i < contents_->child_count(); ++i)
467 size.SetToMax(contents_->child_at(i)->GetPreferredSize()); 545 size.SetToMax(contents_->child_at(i)->GetPreferredSize());
468 size.Enlarge(0, tab_strip_->GetPreferredSize().height()); 546 size.Enlarge(0, tab_strip_->GetPreferredSize().height());
469 return size; 547 return size;
470 } 548 }
471 549
472 Tab* TabbedPane::GetTabAt(int index) { 550 Tab* TabbedPane::GetTabAt(int index) {
473 return static_cast<Tab*>(tab_strip_->child_at(index)); 551 return static_cast<Tab*>(tab_strip_->child_at(index));
474 } 552 }
475 553
554 Tab* TabbedPane::GetSelectedTab() {
555 return selected_tab_index() >= 0 ? GetTabAt(selected_tab_index()) : nullptr;
556 }
557
558 bool TabbedPane::MoveSelectionBy(int delta) {
559 const int tab_count = GetTabCount();
560 if (tab_count <= 1)
561 return false;
562 int next_selected_index = (selected_tab_index() + delta) % tab_count;
563 if (next_selected_index < 0)
564 next_selected_index += tab_count;
565 SelectTabAt(next_selected_index);
566 return true;
567 }
568
476 void TabbedPane::Layout() { 569 void TabbedPane::Layout() {
477 const gfx::Size size = tab_strip_->GetPreferredSize(); 570 const gfx::Size size = tab_strip_->GetPreferredSize();
478 tab_strip_->SetBounds(0, 0, width(), size.height()); 571 tab_strip_->SetBounds(0, 0, width(), size.height());
479 contents_->SetBounds(0, tab_strip_->bounds().bottom(), width(), 572 contents_->SetBounds(0, tab_strip_->bounds().bottom(), width(),
480 std::max(0, height() - size.height())); 573 std::max(0, height() - size.height()));
481 for (int i = 0; i < contents_->child_count(); ++i) 574 for (int i = 0; i < contents_->child_count(); ++i)
482 contents_->child_at(i)->SetSize(contents_->size()); 575 contents_->child_at(i)->SetSize(contents_->size());
483 } 576 }
484 577
485 void TabbedPane::ViewHierarchyChanged( 578 void TabbedPane::ViewHierarchyChanged(
486 const ViewHierarchyChangedDetails& details) { 579 const ViewHierarchyChangedDetails& details) {
487 if (details.is_add) { 580 if (details.is_add) {
488 // Support navigating tabs by Ctrl+Tab and Ctrl+Shift+Tab. 581 // Support navigating tabs by Ctrl+Tab and Ctrl+Shift+Tab.
489 AddAccelerator(ui::Accelerator(ui::VKEY_TAB, 582 AddAccelerator(ui::Accelerator(ui::VKEY_TAB,
490 ui::EF_CONTROL_DOWN | ui::EF_SHIFT_DOWN)); 583 ui::EF_CONTROL_DOWN | ui::EF_SHIFT_DOWN));
491 AddAccelerator(ui::Accelerator(ui::VKEY_TAB, ui::EF_CONTROL_DOWN)); 584 AddAccelerator(ui::Accelerator(ui::VKEY_TAB, ui::EF_CONTROL_DOWN));
492 } 585 }
493 } 586 }
494 587
495 bool TabbedPane::AcceleratorPressed(const ui::Accelerator& accelerator) { 588 bool TabbedPane::AcceleratorPressed(const ui::Accelerator& accelerator) {
496 // Handle Ctrl+Tab and Ctrl+Shift+Tab navigation of pages. 589 // Handle Ctrl+Tab and Ctrl+Shift+Tab navigation of pages.
497 DCHECK(accelerator.key_code() == ui::VKEY_TAB && accelerator.IsCtrlDown()); 590 DCHECK(accelerator.key_code() == ui::VKEY_TAB && accelerator.IsCtrlDown());
498 const int tab_count = GetTabCount(); 591 return MoveSelectionBy(accelerator.IsShiftDown() ? -1 : 1);
499 if (tab_count <= 1) 592 }
593
594 bool TabbedPane::OnKeyPressed(const ui::KeyEvent& event) {
595 if (!GetSelectedTab() || !GetSelectedTab()->HasFocus())
500 return false; 596 return false;
501 const int increment = accelerator.IsShiftDown() ? -1 : 1; 597 ui::KeyboardCode key = event.key_code();
502 int next_tab_index = (selected_tab_index() + increment) % tab_count; 598 if (key != ui::VKEY_LEFT && key != ui::VKEY_RIGHT)
503 // Wrap around. 599 return false;
504 if (next_tab_index < 0) 600 return MoveSelectionBy(key == ui::VKEY_RIGHT ? 1 : -1);
505 next_tab_index += tab_count;
506 SelectTabAt(next_tab_index);
507 return true;
508 } 601 }
509 602
510 const char* TabbedPane::GetClassName() const { 603 const char* TabbedPane::GetClassName() const {
511 return kViewClassName; 604 return kViewClassName;
512 } 605 }
513 606
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) { 607 void TabbedPane::GetAccessibleState(ui::AXViewState* state) {
525 state->role = ui::AX_ROLE_TAB_LIST; 608 state->role = ui::AX_ROLE_TAB_LIST;
526 } 609 }
527 610
528 } // namespace views 611 } // 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