Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tab layout (Uplift to 1.52.x) #19154

Merged
merged 1 commit into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions browser/ui/views/tabs/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,23 @@ source_set("browser_tests") {
]
}
}

source_set("unit_tests") {
testonly = true

sources = [
"//chrome/browser/ui/views/tabs/fake_tab_slot_controller.cc",
"//chrome/browser/ui/views/tabs/fake_tab_slot_controller.h",
"brave_tab_unittest.cc",
]

deps = [
"//base",
"//brave/common",
"//chrome/browser/ui",
"//chrome/test:test_support",
"//content/test:test_support",
"//testing/gmock",
"//testing/gtest",
]
}
30 changes: 30 additions & 0 deletions browser/ui/views/tabs/brave_tab.cc
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,17 @@ void BraveTab::UpdateIconVisibility() {
showing_icon_ = !showing_alert_indicator_ && !showing_close_button_;
}
}

// Supplement extra left side padding by updating border.
// Upstream gives extra padding to balance with right side padding space but
// it's gone when tab doesn't have sufficient available width. In our case,
// As we have more narrow left & right padding than upstream, icon seems stick
// to left side when extra padding is not used.
// We only need to do that when |extra_padding_before_content_| is false.
// We update border here because |extra_padding_before_content_| is updated
// by Tab::UpdateIconVisibility(). After that layout happens. So, it's good
// time to update border now.
// UpdateBorder();
}

void BraveTab::OnLayerBoundsChanged(const gfx::Rect& old_bounds,
Expand Down Expand Up @@ -226,6 +237,25 @@ void BraveTab::Layout() {
}
}

gfx::Insets BraveTab::GetInsets() const {
// Supplement extra left side padding.
// Upstream gives extra padding to balance with right side padding space but
// it's gone when tab doesn't have sufficient available width. In our case,
// As we have more narrow left & right padding than upstream, icon seems stick
// to left side when extra padding is not used.
// We only need to do that when |extra_padding_before_content_| is false.
int extra_left_padding = 0;

// Add extra padding if upstream tab doesn't have it.
if (!extra_padding_before_content_) {
extra_left_padding = kExtraLeftPadding;
}

auto insets = Tab::GetInsets();
insets.set_left(insets.left() + extra_left_padding);
return insets;
}

void BraveTab::ReorderChildLayers(ui::Layer* parent_layer) {
Tab::ReorderChildLayers(parent_layer);

Expand Down
5 changes: 5 additions & 0 deletions browser/ui/views/tabs/brave_tab.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ class BraveTab : public Tab {

void OnLayerBoundsChanged(const gfx::Rect& old_bounds,
ui::PropertyChangeReason reason) override;
gfx::Insets GetInsets() const override;

private:
friend class BraveTabTest;

bool IsAtMinWidthForVerticalTabStrip() const;

void UpdateShadowForActiveTab();
Expand All @@ -57,6 +60,8 @@ class BraveTab : public Tab {
std::unique_ptr<ui::Layer> shadow_layer_;
gfx::FontList normal_font_;
gfx::FontList active_tab_font_;

static constexpr int kExtraLeftPadding = 4;
};

#endif // BRAVE_BROWSER_UI_VIEWS_TABS_BRAVE_TAB_H_
44 changes: 44 additions & 0 deletions browser/ui/views/tabs/brave_tab_unittest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) 2023 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

#include "brave/browser/ui/views/tabs/brave_tab.h"
#include "chrome/browser/ui/views/tabs/fake_tab_slot_controller.h"
#include "chrome/test/views/chrome_views_test_base.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/views/test/views_test_utils.h"

class BraveTabTest : public ChromeViewsTestBase {
public:
BraveTabTest() = default;
~BraveTabTest() override = default;

void LayoutAndCheckBorder(BraveTab* tab,
const gfx::Rect& bounds,
bool gave_extra_padding) {
tab->SetBoundsRect(bounds);
views::test::RunScheduledLayout(tab);

auto insets = tab->tab_style_views()->GetContentsInsets();
int left_inset = insets.left();
if (gave_extra_padding) {
left_inset += BraveTab::kExtraLeftPadding;
}
EXPECT_EQ(left_inset, tab->GetInsets().left());
}
};

TEST_F(BraveTabTest, ExtraPaddingLayoutTest) {
FakeTabSlotController tab_slot_controller;
BraveTab tab(&tab_slot_controller);

// Smaller width tab will be given extra padding.
LayoutAndCheckBorder(&tab, {0, 0, 30, 50}, true);
LayoutAndCheckBorder(&tab, {0, 0, 50, 50}, true);
LayoutAndCheckBorder(&tab, {0, 0, 100, 50}, false);
LayoutAndCheckBorder(&tab, {0, 0, 150, 50}, false);
LayoutAndCheckBorder(&tab, {0, 0, 30, 50}, true);
}
1 change: 1 addition & 0 deletions test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ test("brave_unit_tests") {
"//brave/browser/ui/commands:unit_tests",
"//brave/browser/ui/toolbar:brave_app_menu_unit_test",
"//brave/browser/ui/views/download/bubble:unit_tests",
"//brave/browser/ui/views/tabs:unit_tests",
"//brave/browser/ui/webui/settings:unittests",
"//brave/browser/ui/whats_new:unit_test",
"//brave/components/brave_shields/common:mojom",
Expand Down