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
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/views/infobars/alternate_nav_infobar_view.h"
#include <stddef.h>
#include <utility>
#include "base/check_op.h"
#include "base/strings/strcat.h"
#include "build/build_config.h"
#include "chrome/browser/ui/omnibox/alternate_nav_infobar_delegate.h"
#include "ui/base/window_open_disposition.h"
#include "ui/gfx/text_elider.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/link.h"
// AlternateNavInfoBarDelegate -------------------------------------------------
// static
std::unique_ptr<infobars::InfoBar> AlternateNavInfoBarDelegate::CreateInfoBar(
std::unique_ptr<AlternateNavInfoBarDelegate> delegate) {
return std::make_unique<AlternateNavInfoBarView>(std::move(delegate));
}
// AlternateNavInfoBarView -----------------------------------------------------
AlternateNavInfoBarView::AlternateNavInfoBarView(
std::unique_ptr<AlternateNavInfoBarDelegate> delegate)
: InfoBarView(std::move(delegate)) {
auto* delegate_ptr = GetDelegate();
size_t offset;
std::u16string message_text = delegate_ptr->GetMessageTextWithOffset(&offset);
DCHECK_NE(std::u16string::npos, offset);
label_1_text_ = message_text.substr(0, offset);
label_1_ = AddContentChildView(CreateLabel(label_1_text_));
link_text_ = delegate_ptr->GetLinkText();
link_ = AddContentChildView(CreateLink(link_text_));
label_2_text_ = message_text.substr(offset);
label_2_ = AddContentChildView(CreateLabel(label_2_text_));
}
AlternateNavInfoBarView::~AlternateNavInfoBarView() = default;
// static
void AlternateNavInfoBarView::ElideLabels(Labels* labels, int available_width) {
views::Label* last_label = labels->back();
labels->pop_back();
int used_width = 0;
for (auto& label : *labels) {
used_width +=
label->GetPreferredSize(views::SizeBounds(label->width(), {})).width();
}
int last_label_width = std::min(
last_label->GetPreferredSize(views::SizeBounds(last_label->width(), {}))
.width(),
available_width - used_width);
if (last_label_width < last_label->GetMinimumSize().width()) {
last_label_width = 0;
if (!labels->empty()) {
labels->back()->SetText(
base::StrCat({labels->back()->GetText(), gfx::kEllipsisUTF16}));
}
}
last_label->SetSize(gfx::Size(last_label_width, last_label->height()));
if (!labels->empty()) {
ElideLabels(labels, available_width - last_label_width);
}
}
void AlternateNavInfoBarView::Layout(PassKey) {
LayoutSuperclass<InfoBarView>(this);
label_1_->SetText(label_1_text_);
link_->SetText(link_text_);
label_2_->SetText(label_2_text_);
Labels labels;
labels.push_back(label_1_);
labels.push_back(link_);
labels.push_back(label_2_);
ElideLabels(&labels, GetEndX() - GetStartX());
label_1_->SetPosition(gfx::Point(GetStartX(), OffsetY(label_1_)));
link_->SetPosition(gfx::Point(label_1_->bounds().right(), OffsetY(link_)));
label_2_->SetPosition(gfx::Point(link_->bounds().right(), OffsetY(label_2_)));
}
int AlternateNavInfoBarView::GetContentMinimumWidth() const {
int label_1_width = label_1_->GetMinimumSize().width();
return label_1_width ? label_1_width : link_->GetMinimumSize().width();
}
AlternateNavInfoBarDelegate* AlternateNavInfoBarView::GetDelegate() {
return static_cast<AlternateNavInfoBarDelegate*>(delegate());
}
|