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

Fixed preferred size calculation for connection states in flyout menu #13374

Merged
merged 1 commit into from
May 19, 2022
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
69 changes: 54 additions & 15 deletions browser/ui/views/toolbar/brave_vpn_status_label.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,43 @@
using ConnectionState = brave_vpn::mojom::ConnectionState;
using PurchasedState = brave_vpn::mojom::PurchasedState;

namespace {

int GetStringIdForConnectionState(ConnectionState state) {
switch (state) {
case ConnectionState::CONNECTING:
return IDS_BRAVE_VPN_STATUS_LABEL_CONNECTING;
break;
case ConnectionState::CONNECTED:
return IDS_BRAVE_VPN_STATUS_LABEL_CONNECTED;
break;
case ConnectionState::DISCONNECTING:
return IDS_BRAVE_VPN_STATUS_LABEL_DISCONNECTING;
break;
default:
break;
}
return IDS_BRAVE_VPN_STATUS_LABEL_DISCONNECTED;
}

int GetLongestStringIdForConnectionState() {
size_t max = 0;
int longest_string_id =
GetStringIdForConnectionState(ConnectionState::DISCONNECTED);
for (auto state :
{ConnectionState::CONNECTING, ConnectionState::CONNECTED,
ConnectionState::DISCONNECTING, ConnectionState::DISCONNECTED}) {
auto id = GetStringIdForConnectionState(state);
auto text = brave_l10n::GetLocalizedResourceUTF16String(id);
if (text.length() > max) {
max = text.length();
longest_string_id = id;
}
}
return longest_string_id;
}
} // namespace

BraveVPNStatusLabel::BraveVPNStatusLabel(Browser* browser)
: browser_(browser),
service_(brave_vpn::BraveVpnServiceFactory::GetForProfile(
Expand All @@ -34,6 +71,7 @@ BraveVPNStatusLabel::BraveVPNStatusLabel(Browser* browser)
SetEnabledColor(provider->GetColor(
BraveThemeProperties::COLOR_MENU_ITEM_SUB_TEXT_COLOR));
}
longest_state_string_id_ = GetLongestStringIdForConnectionState();
}

BraveVPNStatusLabel::~BraveVPNStatusLabel() = default;
Expand All @@ -42,22 +80,23 @@ void BraveVPNStatusLabel::OnConnectionStateChanged(ConnectionState state) {
UpdateState();
}

gfx::Size BraveVPNStatusLabel::CalculatePreferredSize() const {
auto size = views::Label::CalculatePreferredSize();
if (longest_state_string_id_ == -1)
return size;
auto text =
brave_l10n::GetLocalizedResourceUTF16String(longest_state_string_id_);
if (text == GetText())
return size;
size.set_width(font_list().GetExpectedTextWidth(text.length()) +
GetInsets().width());
size.set_height(GetHeightForWidth(size.width()));
return size;
}

void BraveVPNStatusLabel::UpdateState() {
const auto state = service_->connection_state();
int string_id = IDS_BRAVE_VPN_STATUS_LABEL_DISCONNECTED;
switch (state) {
case ConnectionState::CONNECTING:
string_id = IDS_BRAVE_VPN_STATUS_LABEL_CONNECTING;
break;
case ConnectionState::CONNECTED:
string_id = IDS_BRAVE_VPN_STATUS_LABEL_CONNECTED;
break;
case ConnectionState::DISCONNECTING:
string_id = IDS_BRAVE_VPN_STATUS_LABEL_DISCONNECTING;
break;
default:
break;
}

SetText(brave_l10n::GetLocalizedResourceUTF16String(string_id));
SetText(brave_l10n::GetLocalizedResourceUTF16String(
GetStringIdForConnectionState(state)));
}
3 changes: 3 additions & 0 deletions browser/ui/views/toolbar/brave_vpn_status_label.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ class BraveVPNStatusLabel : public views::Label,
BraveVPNStatusLabel(const BraveVPNStatusLabel&) = delete;
BraveVPNStatusLabel& operator=(const BraveVPNStatusLabel&) = delete;

gfx::Size CalculatePreferredSize() const override;

private:
// brave_vpn::BraveVPNServiceObserver overrides:
void OnConnectionStateChanged(
brave_vpn::mojom::ConnectionState state) override;

void UpdateState();

int longest_state_string_id_ = -1;
raw_ptr<Browser> browser_ = nullptr;
raw_ptr<brave_vpn::BraveVpnService> service_ = nullptr;
};
Expand Down