Commit 2e94d89b authored by Olesia Marukhno's avatar Olesia Marukhno Committed by Commit Bot

Add handling of combined permission delegate (camera & mic).

Add restarting the timer on user interaction.

Bug: 1019129
Change-Id: I9c2fd0320761747cefbf5b0407ed6989ca804699
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2214948
Commit-Queue: Olesia Marukhno <olesiamarukhno@google.com>
Reviewed-by: default avatarBret Sepulveda <bsep@chromium.org>
Cr-Commit-Position: refs/heads/master@{#772698}
parent 2fe83d2d
...@@ -29,6 +29,18 @@ ...@@ -29,6 +29,18 @@
#include "ui/views/style/typography.h" #include "ui/views/style/typography.h"
#include "ui/views/widget/widget.h" #include "ui/views/widget/widget.h"
namespace {
bool IsCameraPermission(permissions::PermissionRequestType type) {
return type ==
permissions::PermissionRequestType::PERMISSION_MEDIASTREAM_CAMERA;
}
bool IsCameraOrMicPermission(permissions::PermissionRequestType type) {
return IsCameraPermission(type) ||
type == permissions::PermissionRequestType::PERMISSION_MEDIASTREAM_MIC;
}
} // namespace
PermissionChip::PermissionChip(Browser* browser) PermissionChip::PermissionChip(Browser* browser)
: views::AnimationDelegateViews(nullptr), browser_(browser) { : views::AnimationDelegateViews(nullptr), browser_(browser) {
SetLayoutManager(std::make_unique<views::FillLayout>()); SetLayoutManager(std::make_unique<views::FillLayout>());
...@@ -54,9 +66,20 @@ void PermissionChip::Show(permissions::PermissionPrompt::Delegate* delegate) { ...@@ -54,9 +66,20 @@ void PermissionChip::Show(permissions::PermissionPrompt::Delegate* delegate) {
DCHECK(delegate); DCHECK(delegate);
delegate_ = delegate; delegate_ = delegate;
// TODO(olesiamarukhno): Update this to use real strings. const std::vector<permissions::PermissionRequest*>& requests =
chip_button_->SetText(GetPermissionRequest()->GetMessageTextFragment() + delegate_->Requests();
base::ASCIIToUTF16("?"));
// TODO(olesiamarukhno): Add combined camera & microphone permission and
// update delegate to contain only one request at a time.
DCHECK(requests.size() == 1u || requests.size() == 2u);
if (requests.size() == 2) {
DCHECK(IsCameraOrMicPermission(requests[0]->GetPermissionRequestType()));
DCHECK(IsCameraOrMicPermission(requests[1]->GetPermissionRequestType()));
DCHECK_NE(requests[0]->GetPermissionRequestType(),
requests[1]->GetPermissionRequestType());
}
chip_button_->SetText(GetPermissionMessage());
UpdatePermissionIconAndTextColor(); UpdatePermissionIconAndTextColor();
SetVisible(true); SetVisible(true);
...@@ -81,6 +104,11 @@ gfx::Size PermissionChip::CalculatePreferredSize() const { ...@@ -81,6 +104,11 @@ gfx::Size PermissionChip::CalculatePreferredSize() const {
return gfx::Size(width, GetHeightForWidth(width)); return gfx::Size(width, GetHeightForWidth(width));
} }
void PermissionChip::OnMouseEntered(const ui::MouseEvent& event) {
// Restart the timer after user hovers the view.
StartCollapseTimer();
}
void PermissionChip::OnThemeChanged() { void PermissionChip::OnThemeChanged() {
View::OnThemeChanged(); View::OnThemeChanged();
UpdatePermissionIconAndTextColor(); UpdatePermissionIconAndTextColor();
...@@ -89,13 +117,24 @@ void PermissionChip::OnThemeChanged() { ...@@ -89,13 +117,24 @@ void PermissionChip::OnThemeChanged() {
void PermissionChip::ButtonPressed(views::Button* sender, void PermissionChip::ButtonPressed(views::Button* sender,
const ui::Event& event) { const ui::Event& event) {
DCHECK_EQ(chip_button_, sender); DCHECK_EQ(chip_button_, sender);
if (prompt_bubble_) {
prompt_bubble_->GetWidget()->Close(); // The prompt bubble is either not opened yet or already closed on
// deactivation.
DCHECK(!prompt_bubble_);
// TODO(olesiamarukhno): Remove ink drop animation when the bubble is opened.
if (is_bubble_showing_) {
// If the user clicks on the chip when the bubble is open, they probably
// don't want to see the chip so we collapse it immediately.
animation_->Hide();
} else { } else {
prompt_bubble_ = new PermissionPromptBubbleView(browser_, delegate_); prompt_bubble_ = new PermissionPromptBubbleView(browser_, delegate_);
prompt_bubble_->Show(); prompt_bubble_->Show();
prompt_bubble_->GetWidget()->AddObserver(this); prompt_bubble_->GetWidget()->AddObserver(this);
// Restart the timer after user clicks on the chip to open the bubble.
StartCollapseTimer();
} }
is_bubble_showing_ = !is_bubble_showing_;
} }
void PermissionChip::AnimationEnded(const gfx::Animation* animation) { void PermissionChip::AnimationEnded(const gfx::Animation* animation) {
...@@ -116,8 +155,6 @@ void PermissionChip::OnWidgetClosing(views::Widget* widget) { ...@@ -116,8 +155,6 @@ void PermissionChip::OnWidgetClosing(views::Widget* widget) {
} }
void PermissionChip::Collapse() { void PermissionChip::Collapse() {
// TODO(olesiamarukhno): Check the edge case when user unhovers/closes the
// prompt just before collapsing chip.
if (IsMouseHovered() || prompt_bubble_) { if (IsMouseHovered() || prompt_bubble_) {
StartCollapseTimer(); StartCollapseTimer();
} else { } else {
...@@ -137,8 +174,7 @@ int PermissionChip::GetIconSize() const { ...@@ -137,8 +174,7 @@ int PermissionChip::GetIconSize() const {
} }
void PermissionChip::UpdatePermissionIconAndTextColor() { void PermissionChip::UpdatePermissionIconAndTextColor() {
auto* request = GetPermissionRequest(); if (!delegate_)
if (!request)
return; return;
// Set label and icon color to be the same color. // Set label and icon color to be the same color.
...@@ -149,18 +185,27 @@ void PermissionChip::UpdatePermissionIconAndTextColor() { ...@@ -149,18 +185,27 @@ void PermissionChip::UpdatePermissionIconAndTextColor() {
chip_button_->SetEnabledTextColors(enabled_text_color); chip_button_->SetEnabledTextColors(enabled_text_color);
chip_button_->SetImage( chip_button_->SetImage(
views::Button::STATE_NORMAL, views::Button::STATE_NORMAL,
gfx::CreateVectorIcon(request->GetIconId(), GetIconSize(), gfx::CreateVectorIcon(GetPermissionIconId(), GetIconSize(),
enabled_text_color)); enabled_text_color));
} }
permissions::PermissionRequest* PermissionChip::GetPermissionRequest() { const gfx::VectorIcon& PermissionChip::GetPermissionIconId() {
if (!delegate_) auto requests = delegate_->Requests();
return nullptr; if (requests.size() == 1)
return requests[0]->GetIconId();
const std::vector<permissions::PermissionRequest*>& requests = // When we have two requests, it must be microphone & camera. Then we need to
delegate_->Requests(); // use the icon from the camera request.
DCHECK_GT(requests.size(), 0u); return IsCameraPermission(requests[0]->GetPermissionRequestType())
? requests[0]->GetIconId()
: requests[1]->GetIconId();
}
base::string16 PermissionChip::GetPermissionMessage() {
auto requests = delegate_->Requests();
// TODO(olesiamarukhno): Need to handle more than 1 request. // TODO(olesiamarukhno): Update this to use real strings.
return requests[0]; return requests.size() == 1
? requests[0]->GetMessageTextFragment() + base::ASCIIToUTF16("?")
: base::ASCIIToUTF16("Use camera & microphone?");
} }
...@@ -49,6 +49,7 @@ class PermissionChip : public views::View, ...@@ -49,6 +49,7 @@ class PermissionChip : public views::View,
// views::View: // views::View:
gfx::Size CalculatePreferredSize() const override; gfx::Size CalculatePreferredSize() const override;
void OnMouseEntered(const ui::MouseEvent& event) override;
void OnThemeChanged() override; void OnThemeChanged() override;
// views::ButtonListener: // views::ButtonListener:
...@@ -62,7 +63,8 @@ class PermissionChip : public views::View, ...@@ -62,7 +63,8 @@ class PermissionChip : public views::View,
void StartCollapseTimer(); void StartCollapseTimer();
int GetIconSize() const; int GetIconSize() const;
void UpdatePermissionIconAndTextColor(); void UpdatePermissionIconAndTextColor();
permissions::PermissionRequest* GetPermissionRequest(); base::string16 GetPermissionMessage();
const gfx::VectorIcon& GetPermissionIconId();
Browser* browser_ = nullptr; Browser* browser_ = nullptr;
permissions::PermissionPrompt::Delegate* delegate_ = nullptr; permissions::PermissionPrompt::Delegate* delegate_ = nullptr;
...@@ -76,6 +78,8 @@ class PermissionChip : public views::View, ...@@ -76,6 +78,8 @@ class PermissionChip : public views::View,
// The button that displays the icon and text. // The button that displays the icon and text.
views::MdTextButton* chip_button_; views::MdTextButton* chip_button_;
bool is_bubble_showing_ = false;
}; };
#endif // CHROME_BROWSER_UI_VIEWS_LOCATION_BAR_PERMISSION_CHIP_H_ #endif // CHROME_BROWSER_UI_VIEWS_LOCATION_BAR_PERMISSION_CHIP_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment