Commit 0f75f3b2 authored by Melissa Zhang's avatar Melissa Zhang Committed by Commit Bot

[Sharesheet] Add API for ShareAction to control bubble size.

This CL adds the SetSharesheetSize function to SharesheetController
which can be called to resize the sharesheet to a given size. The
anchor position in the sharesheet bubble is updated to accommodate
the new bubble sizing.

Bug: 1097623
Change-Id: I0093c4b7ae69119928d677bed12cd19ca7d64e1a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2362067
Commit-Queue: Melissa Zhang <melzhang@chromium.org>
Reviewed-by: default avatarAvi Drissman <avi@chromium.org>
Reviewed-by: default avatarDominick Ng <dominickn@chromium.org>
Cr-Commit-Position: refs/heads/master@{#799506}
parent 184a8fc8
......@@ -20,6 +20,7 @@ class ShareAction {
virtual const base::string16 GetActionName() = 0;
// Icon DIP (Density Independent Pixel) size must be 40 x 40.
virtual const gfx::ImageSkia GetActionIcon() = 0;
// LaunchAction should synchronously create all UI needed and fill
......
......@@ -5,10 +5,12 @@
#include "chrome/browser/sharesheet/sharesheet_action_cache.h"
#include "chrome/browser/sharesheet/share_action.h"
#include "chrome/browser/sharesheet/sharesheet_types.h"
namespace sharesheet {
SharesheetActionCache::SharesheetActionCache() = default;
// ShareActions will be initialised here by calling AddShareAction.
SharesheetActionCache::~SharesheetActionCache() = default;
......@@ -30,4 +32,10 @@ ShareAction* SharesheetActionCache::GetActionFromName(
return nullptr;
}
void SharesheetActionCache::AddShareAction(
std::unique_ptr<ShareAction> action) {
DCHECK_EQ(action->GetActionIcon().size(), gfx::Size(kIconSize, kIconSize));
share_actions_.push_back(std::move(action));
}
} // namespace sharesheet
......@@ -29,6 +29,8 @@ class SharesheetActionCache {
const std::vector<std::unique_ptr<ShareAction>>& GetShareActions();
private:
void AddShareAction(std::unique_ptr<ShareAction> action);
std::vector<std::unique_ptr<ShareAction>> share_actions_;
};
......
......@@ -17,6 +17,10 @@ class SharesheetController {
// different invocations of the sharesheet.
virtual uint32_t GetId() = 0;
// When called will set the bubble size to |width| and |height|.
// |width| and |height| must be set to a positive int.
virtual void SetSharesheetSize(const int& width, const int& height) = 0;
// Called by ShareAction to notify SharesheetBubbleView that ShareAction
// has completed.
virtual void CloseSharesheet() = 0;
......
......@@ -20,11 +20,6 @@
#include "ui/display/types/display_constants.h"
#include "ui/views/view.h"
namespace {
// In px.
constexpr int kIconSize = 40;
} // namespace
namespace sharesheet {
SharesheetService::SharesheetService(Profile* profile)
......
......@@ -63,6 +63,13 @@ uint32_t SharesheetServiceDelegate::GetId() {
return id_;
}
void SharesheetServiceDelegate::SetSharesheetSize(const int& width,
const int& height) {
DCHECK_GT(width, 0);
DCHECK_GT(height, 0);
sharesheet_bubble_view_->ResizeBubble(width, height);
}
void SharesheetServiceDelegate::CloseSharesheet() {
sharesheet_bubble_view_->CloseBubble();
}
......
......@@ -52,6 +52,7 @@ class SharesheetServiceDelegate : public SharesheetController {
// SharesheetController overrides
uint32_t GetId() override;
void SetSharesheetSize(const int& width, const int& height) override;
void CloseSharesheet() override;
private:
......
......@@ -10,6 +10,9 @@
namespace sharesheet {
// In DIP (Density Independent Pixel).
constexpr int kIconSize = 40;
// The type of a target.
enum class TargetType {
kUnknown = 0,
......@@ -34,6 +37,7 @@ struct TargetInfo {
TargetType type;
// The icon to be displayed for this target in the sharesheet bubble.
// DIP size must be kIconSize
gfx::ImageSkia icon;
// The string used to launch this target. Represents an Android package name
......
......@@ -48,7 +48,7 @@ constexpr int kCornerRadius = 12;
constexpr int kMaxTargetsPerRow = 4;
// TargetViewHeight is 2*kButtonHeight + kButtonPadding
constexpr int kTargetViewHeight = 216;
constexpr int kBubbleWidth = 416;
constexpr int kDefaultBubbleWidth = 416;
constexpr int kShortSpacing = 20;
constexpr int kSpacing = 24;
constexpr int kTitleLineHeight = 24;
......@@ -129,18 +129,8 @@ SharesheetBubbleView::SharesheetBubbleView(
->GetCurrentAppWindowForApp(extension_misc::kFilesManagerAppId)
->GetNativeWindow();
set_parent_window(parent);
View* parent_view =
views::Widget::GetWidgetForNativeWindow(parent)->GetRootView();
// Horizontally centered
int x_within_parent_view = parent_view->GetMirroredXInView(
(parent_view->bounds().width() - kBubbleWidth) / 2);
// Get position in screen, taking parent view origin into account. This is
// 0,0 in fullscreen on the primary display, but not on secondary displays, or
// in Hosted App windows.
gfx::Point origin = parent_view->GetBoundsInScreen().origin();
origin += gfx::Vector2d(x_within_parent_view, kBubbleTopPaddingFromWindow);
SetAnchorRect(gfx::Rect(origin, gfx::Size()));
parent_view_ = views::Widget::GetWidgetForNativeWindow(parent)->GetRootView();
UpdateAnchorPosition();
CreateBubble();
}
......@@ -213,6 +203,9 @@ void SharesheetBubbleView::ShowBubble(std::vector<TargetInfo> targets,
views::Widget* widget = views::BubbleDialogDelegateView::CreateBubble(this);
GetWidget()->GetRootView()->Layout();
widget->Show();
SetToDefaultBubbleSizing();
UpdateAnchorPosition();
}
void SharesheetBubbleView::ShowActionView() {
......@@ -220,9 +213,20 @@ void SharesheetBubbleView::ShowActionView() {
share_action_view_->SetVisible(true);
}
void SharesheetBubbleView::ResizeBubble(const int& width, const int& height) {
width_ = width;
height_ = height;
UpdateAnchorPosition();
}
void SharesheetBubbleView::CloseBubble() {
views::Widget* widget = View::GetWidget();
widget->CloseWithReason(views::Widget::ClosedReason::kAcceptButtonClicked);
// Reset all bubble values.
targets_.clear();
active_target_ = base::string16();
intent_.reset();
SetToDefaultBubbleSizing();
}
void SharesheetBubbleView::ButtonPressed(views::Button* sender,
......@@ -253,7 +257,7 @@ void SharesheetBubbleView::OnWidgetDestroyed(views::Widget* widget) {
}
gfx::Size SharesheetBubbleView::CalculatePreferredSize() const {
return gfx::Size(kBubbleWidth, GetHeightForWidth(kBubbleWidth));
return gfx::Size(width_, height_);
}
void SharesheetBubbleView::CreateBubble() {
......@@ -280,3 +284,27 @@ void SharesheetBubbleView::CreateBubble() {
share_action_view_ = AddChildView(std::move(share_action_view));
share_action_view_->SetVisible(false);
}
void SharesheetBubbleView::UpdateAnchorPosition() {
// If |width_| is not set, set to default value.
if (width_ == 0) {
SetToDefaultBubbleSizing();
}
// Horizontally centered
int x_within_parent_view = parent_view_->GetMirroredXInView(
(parent_view_->bounds().width() - width_) / 2);
// Get position in screen, taking parent view origin into account. This is
// 0,0 in fullscreen on the primary display, but not on secondary displays, or
// in Hosted App windows.
gfx::Point origin = parent_view_->GetBoundsInScreen().origin();
origin += gfx::Vector2d(x_within_parent_view, kBubbleTopPaddingFromWindow);
// SetAnchorRect will CalculatePreferredSize when called.
SetAnchorRect(gfx::Rect(origin, gfx::Size()));
}
void SharesheetBubbleView::SetToDefaultBubbleSizing() {
width_ = kDefaultBubbleWidth;
height_ = GetHeightForWidth(width_);
}
......@@ -36,6 +36,7 @@ class SharesheetBubbleView : public views::BubbleDialogDelegateView,
void ShowBubble(std::vector<TargetInfo> targets,
apps::mojom::IntentPtr intent);
void ShowActionView();
void ResizeBubble(const int& width, const int& height);
void CloseBubble();
// views::ButtonListener overrides
......@@ -51,15 +52,21 @@ class SharesheetBubbleView : public views::BubbleDialogDelegateView,
private:
void CreateBubble();
void UpdateAnchorPosition();
void SetToDefaultBubbleSizing();
// Owns this class.
sharesheet::SharesheetServiceDelegate* delegate_;
std::vector<TargetInfo> targets_;
base::string16 active_target_;
apps::mojom::IntentPtr intent_;
int width_ = 0;
int height_ = 0;
views::View* root_view_ = nullptr;
views::View* main_view_ = nullptr;
views::View* share_action_view_ = nullptr;
views::View* parent_view_ = nullptr;
};
#endif // CHROME_BROWSER_UI_VIEWS_SHARESHEET_BUBBLE_VIEW_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