1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/views/examples/tabbed_pane_example.h"
#include <memory>
#include <utility>
#include "base/format_macros.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/controls/tabbed_pane/tabbed_pane.h"
#include "ui/views/examples/examples_window.h"
#include "ui/views/examples/grit/views_examples_resources.h"
#include "ui/views/layout/flex_layout.h"
#include "ui/views/layout/flex_layout_types.h"
#include "ui/views/view_class_properties.h"
using l10n_util::GetStringUTF16;
using l10n_util::GetStringUTF8;
namespace views::examples {
TabbedPaneExample::TabbedPaneExample()
: ExampleBase(GetStringUTF8(IDS_TABBED_PANE_SELECT_LABEL).c_str()) {}
TabbedPaneExample::~TabbedPaneExample() {
if (tabbed_pane_) {
tabbed_pane_->SetListener(nullptr);
}
}
void TabbedPaneExample::CreateExampleView(View* container) {
container->SetLayoutManager(std::make_unique<views::FlexLayout>())
->SetOrientation(LayoutOrientation::kVertical);
// Add control buttons horizontally.
auto* const button_panel = container->AddChildView(std::make_unique<View>());
button_panel->SetLayoutManager(std::make_unique<views::FlexLayout>());
button_panel->AddChildView(std::make_unique<LabelButton>(
base::BindRepeating(&TabbedPaneExample::SwapLayout,
base::Unretained(this)),
GetStringUTF16(IDS_TABBED_PANE_SWAP_LAYOUT_LABEL)));
toggle_highlighted_ =
button_panel->AddChildView(std::make_unique<LabelButton>(
base::BindRepeating(&TabbedPaneExample::ToggleHighlighted,
base::Unretained(this)),
GetStringUTF16(IDS_TABBED_PANE_TOGGLE_HIGHLIGHTED_LABEL)));
button_panel->AddChildView(std::make_unique<LabelButton>(
base::BindRepeating(&TabbedPaneExample::AddTab, base::Unretained(this),
GetStringUTF16(IDS_TABBED_PANE_ADDED_LABEL)),
GetStringUTF16(IDS_TABBED_PANE_ADD_LABEL)));
button_panel->AddChildView(std::make_unique<LabelButton>(
base::BindRepeating(&TabbedPaneExample::AddAt, base::Unretained(this)),
GetStringUTF16(IDS_TABBED_PANE_ADD_1_LABEL)));
button_panel->AddChildView(std::make_unique<LabelButton>(
base::BindRepeating(&TabbedPaneExample::SelectAt, base::Unretained(this)),
GetStringUTF16(IDS_TABBED_PANE_SELECT_1_LABEL)));
const auto full_flex = FlexSpecification(MinimumFlexSizeRule::kScaleToZero,
MaximumFlexSizeRule::kUnbounded)
.WithWeight(1);
for (View* view : button_panel->children()) {
view->SetProperty(views::kFlexBehaviorKey, full_flex);
}
CreateTabbedPane(container, TabbedPane::Orientation::kHorizontal,
TabbedPane::TabStripStyle::kBorder);
}
void TabbedPaneExample::TabSelectedAt(int index) {
// Just print the status when selection changes.
PrintCurrentStatus();
}
void TabbedPaneExample::CreateTabbedPane(View* container,
TabbedPane::Orientation orientation,
TabbedPane::TabStripStyle style) {
// Tabbed panes only support highlighted style for vertical tabs.
if (orientation == TabbedPane::Orientation::kHorizontal) {
style = TabbedPane::TabStripStyle::kBorder;
}
tabbed_pane_ = container->AddChildViewAt(
std::make_unique<TabbedPane>(orientation, style), 0);
tabbed_pane_->SetListener(this);
toggle_highlighted_->SetEnabled(orientation ==
TabbedPane::Orientation::kVertical);
const auto full_flex = FlexSpecification(MinimumFlexSizeRule::kScaleToZero,
MaximumFlexSizeRule::kUnbounded)
.WithWeight(1);
tabbed_pane_->SetProperty(views::kFlexBehaviorKey, full_flex);
AddTab(GetStringUTF16(IDS_TABBED_PANE_TAB_1_LABEL));
AddTab(GetStringUTF16(IDS_TABBED_PANE_TAB_2_LABEL));
AddTab(GetStringUTF16(IDS_TABBED_PANE_TAB_3_LABEL));
}
void TabbedPaneExample::PrintCurrentStatus() {
PrintStatus(base::StringPrintf("Tab Count:%" PRIuS ", Selected Tab:%" PRIuS,
tabbed_pane_->GetTabCount(),
tabbed_pane_->GetSelectedTabIndex()));
}
void TabbedPaneExample::SwapLayout() {
auto* const container = tabbed_pane_->parent();
const auto orientation =
(tabbed_pane_->GetOrientation() == TabbedPane::Orientation::kHorizontal)
? TabbedPane::Orientation::kVertical
: TabbedPane::Orientation::kHorizontal;
const auto style = tabbed_pane_->GetStyle();
container->RemoveChildView(tabbed_pane_);
CreateTabbedPane(container, orientation, style);
}
void TabbedPaneExample::ToggleHighlighted() {
auto* const container = tabbed_pane_->parent();
const auto orientation = tabbed_pane_->GetOrientation();
const auto style =
(tabbed_pane_->GetStyle() == TabbedPane::TabStripStyle::kBorder)
? TabbedPane::TabStripStyle::kHighlight
: TabbedPane::TabStripStyle::kBorder;
container->RemoveChildView(tabbed_pane_);
CreateTabbedPane(container, orientation, style);
}
void TabbedPaneExample::AddTab(const std::u16string& label) {
tabbed_pane_->AddTab(
label, std::make_unique<LabelButton>(Button::PressedCallback(), label));
PrintCurrentStatus();
}
void TabbedPaneExample::AddAt() {
const std::u16string label = GetStringUTF16(IDS_TABBED_PANE_ADDED_1_LABEL);
tabbed_pane_->AddTabAtIndex(
1, label,
std::make_unique<LabelButton>(Button::PressedCallback(), label));
PrintCurrentStatus();
}
void TabbedPaneExample::SelectAt() {
if (tabbed_pane_->GetTabCount() > 1) {
tabbed_pane_->SelectTabAt(1);
}
}
} // namespace views::examples
|