From 2fa90141ebd3bd886518daa5b8dfc9b3bf684f40 Mon Sep 17 00:00:00 2001 From: cepera Date: Wed, 18 May 2022 16:29:40 +0300 Subject: [PATCH] Fixed preferred size calculation for connection states in flyout menu --- .../views/toolbar/brave_vpn_status_label.cc | 69 +++++++++++++++---- .../ui/views/toolbar/brave_vpn_status_label.h | 3 + 2 files changed, 57 insertions(+), 15 deletions(-) diff --git a/browser/ui/views/toolbar/brave_vpn_status_label.cc b/browser/ui/views/toolbar/brave_vpn_status_label.cc index c74c346434fa..0b59d78b66b0 100644 --- a/browser/ui/views/toolbar/brave_vpn_status_label.cc +++ b/browser/ui/views/toolbar/brave_vpn_status_label.cc @@ -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( @@ -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; @@ -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))); } diff --git a/browser/ui/views/toolbar/brave_vpn_status_label.h b/browser/ui/views/toolbar/brave_vpn_status_label.h index 1c7c45c4ad5f..a53a299f9841 100644 --- a/browser/ui/views/toolbar/brave_vpn_status_label.h +++ b/browser/ui/views/toolbar/brave_vpn_status_label.h @@ -25,6 +25,8 @@ 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( @@ -32,6 +34,7 @@ class BraveVPNStatusLabel : public views::Label, void UpdateState(); + int longest_state_string_id_ = -1; raw_ptr browser_ = nullptr; raw_ptr service_ = nullptr; };