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

Unified Diff: ui/views/controls/tabbed_pane/tabbed_pane.cc

Issue 2368283002: views: add focus to TabbedPane (Closed)
Patch Set: move insets Created 4 years, 3 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..f31ebe19f9212d1f4cc076ba4a92b941721bd337 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,
@@ -169,6 +174,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,10 +263,92 @@ 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();
+}
+
+// The border used for MdTabs when they are both focused and selected: a
+// rectangle with the bottom side in a base color and the other sides in a
+// lightened version of the base color.
+class MdTabFocusRingBorder : public Border {
+ public:
+ 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.
+ ~MdTabFocusRingBorder() override;
+
+ private:
+ static const int kBorderStrokeWidth = 2;
+
+ void Paint(const View& view, gfx::Canvas* canvas) override;
+ gfx::Insets GetInsets() const override;
+ gfx::Size GetMinimumSize() const override;
+
+ MdTab* tab_;
+};
+
+MdTabFocusRingBorder::MdTabFocusRingBorder(MdTab* tab) : tab_(tab) {}
+MdTabFocusRingBorder::~MdTabFocusRingBorder() {}
+
+void MdTabFocusRingBorder::Paint(const View& view, gfx::Canvas* canvas) {
+ if (!view.HasFocus()) {
+ SkColor color = view.GetNativeTheme()->GetSystemColor(
+ tab_->selected() ? ui::NativeTheme::kColorId_FocusedBorderColor
+ : ui::NativeTheme::kColorId_UnfocusedBorderColor);
+ int thickness =
+ tab_->selected() ? kBorderStrokeWidth : kBorderStrokeWidth / 2;
+ canvas->FillRect(
+ gfx::Rect(0, view.height() - thickness, view.width(), thickness),
+ color);
+ return;
+ }
+
+ // TODO(ellyjones): should this 0x66 be part of NativeTheme somehow?
+ SkColor base_color = view.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 = view.GetLocalBounds();
+ bounds.Inset(kBorderStrokeWidth / 2, kBorderStrokeWidth / 2);
+ gfx::Insets insets = GetInsets();
+
+ // 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);
+ canvas->FillRect(gfx::Rect(0, view.height() - insets.bottom(), view.width(),
+ insets.bottom()),
+ base_color);
+}
+
+gfx::Insets MdTabFocusRingBorder::GetInsets() const {
+ return gfx::Insets(kBorderStrokeWidth);
+}
+
+gfx::Size MdTabFocusRingBorder::GetMinimumSize() const {
+ return gfx::Size(GetInsets().width(), GetInsets().height());
+}
+
MdTab::MdTab(TabbedPane* tabbed_pane,
const base::string16& title,
View* contents)
: Tab(tabbed_pane, title, contents) {
+ SetBorder(base::MakeUnique<MdTabFocusRingBorder>(this));
OnStateChanged();
}
@@ -263,12 +356,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)
@@ -386,12 +473,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 +484,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 +511,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()) {
+ if (GetSelectedTab()->HasFocus())
+ tab->RequestFocus();
+ GetSelectedTab()->SetSelected(false);
+ }
+ selected_tab_index_ = index;
tab_strip_->SchedulePaint();
FocusManager* focus_manager = tab->contents()->GetFocusManager();
@@ -473,6 +551,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 +588,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