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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ui/views/controls/tabbed_pane/tabbed_pane.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/views/controls/tabbed_pane/tabbed_pane.cc
diff --git a/ui/views/controls/tabbed_pane/tabbed_pane.cc b/ui/views/controls/tabbed_pane/tabbed_pane.cc
index 07fe5a69545640c34d3bab5c1b58bf003a80811b..6790bca67a9ade00e18f872fdbbfc25cedb474b8 100644
--- a/ui/views/controls/tabbed_pane/tabbed_pane.cc
+++ b/ui/views/controls/tabbed_pane/tabbed_pane.cc
@@ -68,6 +68,8 @@ class Tab : public View {
gfx::Size GetPreferredSize() const override;
void Layout() override;
const char* GetClassName() const override;
+ void OnFocus() override;
+ void OnBlur() override;
protected:
Label* title() { return title_; }
@@ -75,6 +77,9 @@ class Tab : public View {
// Called whenever |tab_state_| changes.
virtual void OnStateChanged();
+ // Returns whether the containing TabStrip has focus.
+ bool ContainerHasFocus();
+
private:
enum TabState {
TAB_INACTIVE,
@@ -103,6 +108,9 @@ class MdTab : public Tab {
// Overridden from Tab:
void OnStateChanged() override;
+ // Overridden from View:
+ void OnPaintBorder(gfx::Canvas* canvas) override;
+
private:
DISALLOW_COPY_AND_ASSIGN(MdTab);
};
@@ -169,6 +177,12 @@ Tab::~Tab() {}
void Tab::SetSelected(bool selected) {
contents_->SetVisible(selected);
SetState(selected ? TAB_ACTIVE : TAB_INACTIVE);
+#if defined(OS_MACOSX)
+ SetFocusBehavior(selected ? FocusBehavior::ACCESSIBLE_ONLY
+ : FocusBehavior::NEVER);
+#else
+ SetFocusBehavior(selected ? FocusBehavior::ALWAYS : FocusBehavior::NEVER);
+#endif
}
void Tab::OnStateChanged() {
@@ -252,6 +266,22 @@ void Tab::SetState(TabState tab_state) {
SchedulePaint();
}
+bool Tab::ContainerHasFocus() {
+ return tabbed_pane_->HasFocus();
+}
+
+void Tab::OnFocus() {
+ OnStateChanged();
+ if (contents())
+ contents()->NotifyAccessibilityEvent(ui::AX_EVENT_FOCUS, true);
+ SchedulePaint();
+}
+
+void Tab::OnBlur() {
+ OnStateChanged();
+ SchedulePaint();
+}
+
MdTab::MdTab(TabbedPane* tabbed_pane,
const base::string16& title,
View* contents)
@@ -263,12 +293,6 @@ MdTab::~MdTab() {}
void MdTab::OnStateChanged() {
ui::NativeTheme* theme = GetNativeTheme();
- SkColor border_color = theme->GetSystemColor(
- selected() ? ui::NativeTheme::kColorId_FocusedBorderColor
- : ui::NativeTheme::kColorId_UnfocusedBorderColor);
- int border_thickness = selected() ? 2 : 1;
- SetBorder(
- Border::CreateSolidSidedBorder(0, 0, border_thickness, 0, border_color));
SkColor font_color = selected()
? theme->GetSystemColor(ui::NativeTheme::kColorId_ProminentButtonColor)
@@ -286,6 +310,41 @@ void MdTab::OnStateChanged() {
gfx::Font::NORMAL, font_weight));
}
+void MdTab::OnPaintBorder(gfx::Canvas* canvas) {
+ const int kBorderStrokeWidth = 2;
+ if (!HasFocus()) {
+ SkColor color = GetNativeTheme()->GetSystemColor(
+ selected() ? ui::NativeTheme::kColorId_FocusedBorderColor
+ : ui::NativeTheme::kColorId_UnfocusedBorderColor);
+ int thickness = selected() ? kBorderStrokeWidth : kBorderStrokeWidth / 2;
+ canvas->FillRect(
+ gfx::Rect(0, height() - thickness, width(), thickness),
+ color);
+ return;
+ }
+
+ // TODO(ellyjones): should this 0x66 be part of NativeTheme somehow?
+ SkColor base_color = GetNativeTheme()->GetSystemColor(
+ ui::NativeTheme::kColorId_FocusedBorderColor);
+ SkColor light_color = SkColorSetA(base_color, 0x66);
+
+ SkPaint paint;
+ paint.setColor(light_color);
+ paint.setStyle(SkPaint::kStroke_Style);
+ paint.setStrokeWidth(kBorderStrokeWidth);
+
+ gfx::Rect bounds = GetLocalBounds();
+ 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.
+
+ // Draw the lighter-colored stroke first, then draw the heavier stroke over
+ // the bottom of it. This is fine because the heavier stroke has 1.0 alpha, so
+ // the lighter stroke won't show through.
+ 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.
+ canvas->FillRect(gfx::Rect(0, height() - kBorderStrokeWidth, width(),
+ kBorderStrokeWidth),
+ base_color);
+}
+
// static
const char TabStrip::kViewClassName[] = "TabStrip";
@@ -386,12 +445,6 @@ TabbedPane::TabbedPane()
: new TabStrip(this)),
contents_(new View()),
selected_tab_index_(-1) {
-#if defined(OS_MACOSX)
- SetFocusBehavior(FocusBehavior::ACCESSIBLE_ONLY);
-#else
- SetFocusBehavior(FocusBehavior::ALWAYS);
-#endif
-
AddChildView(tab_strip_);
AddChildView(contents_);
}
@@ -403,11 +456,6 @@ int TabbedPane::GetTabCount() {
return contents_->child_count();
}
-View* TabbedPane::GetSelectedTab() {
- return selected_tab_index() < 0 ?
- NULL : GetTabAt(selected_tab_index())->contents();
-}
-
void TabbedPane::AddTab(const base::string16& title, View* contents) {
AddTabAtIndex(tab_strip_->child_count(), title, contents);
}
@@ -435,12 +483,14 @@ void TabbedPane::SelectTabAt(int index) {
if (index == selected_tab_index())
return;
- if (selected_tab_index() >= 0)
- GetTabAt(selected_tab_index())->SetSelected(false);
-
- selected_tab_index_ = index;
Tab* tab = GetTabAt(index);
tab->SetSelected(true);
+ 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.
+ if (GetSelectedTab()->HasFocus())
+ tab->RequestFocus();
+ GetSelectedTab()->SetSelected(false);
+ }
+ selected_tab_index_ = index;
tab_strip_->SchedulePaint();
FocusManager* focus_manager = tab->contents()->GetFocusManager();
@@ -473,6 +523,21 @@ Tab* TabbedPane::GetTabAt(int index) {
return static_cast<Tab*>(tab_strip_->child_at(index));
}
+Tab* TabbedPane::GetSelectedTab() {
+ return selected_tab_index() >= 0 ? GetTabAt(selected_tab_index()) : nullptr;
+}
+
+bool TabbedPane::MoveSelectionBy(int delta) {
+ const int tab_count = GetTabCount();
+ if (tab_count <= 1)
+ return false;
+ int next_selected_index = (selected_tab_index() + delta) % tab_count;
+ if (next_selected_index < 0)
+ next_selected_index += tab_count;
+ SelectTabAt(next_selected_index);
+ return true;
+}
+
void TabbedPane::Layout() {
const gfx::Size size = tab_strip_->GetPreferredSize();
tab_strip_->SetBounds(0, 0, width(), size.height());
@@ -495,32 +560,22 @@ void TabbedPane::ViewHierarchyChanged(
bool TabbedPane::AcceleratorPressed(const ui::Accelerator& accelerator) {
// Handle Ctrl+Tab and Ctrl+Shift+Tab navigation of pages.
DCHECK(accelerator.key_code() == ui::VKEY_TAB && accelerator.IsCtrlDown());
- const int tab_count = GetTabCount();
- if (tab_count <= 1)
+ return MoveSelectionBy(accelerator.IsShiftDown() ? -1 : 1);
+}
+
+bool TabbedPane::OnKeyPressed(const ui::KeyEvent& event) {
+ if (!GetSelectedTab() || !GetSelectedTab()->HasFocus())
return false;
- const int increment = accelerator.IsShiftDown() ? -1 : 1;
- int next_tab_index = (selected_tab_index() + increment) % tab_count;
- // Wrap around.
- if (next_tab_index < 0)
- next_tab_index += tab_count;
- SelectTabAt(next_tab_index);
- return true;
+ ui::KeyboardCode key = event.key_code();
+ if (key != ui::VKEY_LEFT && key != ui::VKEY_RIGHT)
+ return false;
+ return MoveSelectionBy(key == ui::VKEY_RIGHT ? 1 : -1);
}
const char* TabbedPane::GetClassName() const {
return kViewClassName;
}
-void TabbedPane::OnFocus() {
- View::OnFocus();
-
- View* selected_tab = GetSelectedTab();
- if (selected_tab) {
- selected_tab->NotifyAccessibilityEvent(
- ui::AX_EVENT_FOCUS, true);
- }
-}
-
void TabbedPane::GetAccessibleState(ui::AXViewState* state) {
state->role = ui::AX_ROLE_TAB_LIST;
}
« 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