Chromium Code Reviews| Index: mash/shelf/shelf_tooltip_manager.cc |
| diff --git a/ash/shelf/shelf_tooltip_manager.cc b/mash/shelf/shelf_tooltip_manager.cc |
| similarity index 63% |
| copy from ash/shelf/shelf_tooltip_manager.cc |
| copy to mash/shelf/shelf_tooltip_manager.cc |
| index 0f65ee641e5df1fdb53f087f8489ed2493cae689..822fba782fd389c54b34cefbaea0b4d6bf76d19e 100644 |
| --- a/ash/shelf/shelf_tooltip_manager.cc |
| +++ b/mash/shelf/shelf_tooltip_manager.cc |
| @@ -2,29 +2,30 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| -#include "ash/shelf/shelf_tooltip_manager.h" |
| +#include "mash/shelf/shelf_tooltip_manager.h" |
| -#include "ash/shelf/shelf_layout_manager.h" |
| -#include "ash/shelf/shelf_view.h" |
| -#include "ash/shell.h" |
| -#include "ash/shell_window_ids.h" |
| -#include "ash/wm/window_animations.h" |
| #include "base/bind.h" |
| #include "base/message_loop/message_loop.h" |
| #include "base/time/time.h" |
| #include "base/timer/timer.h" |
| +#include "components/mus/public/cpp/property_type_converters.h" |
| +#include "mash/shelf/shelf_view.h" |
| +#include "mash/wm/public/interfaces/container.mojom.h" |
| +#include "mojo/shell/public/cpp/application_impl.h" |
| #include "ui/aura/window.h" |
| -#include "ui/aura/window_event_dispatcher.h" |
| #include "ui/events/event.h" |
| #include "ui/events/event_constants.h" |
| #include "ui/gfx/geometry/insets.h" |
| #include "ui/views/bubble/bubble_delegate.h" |
| -#include "ui/views/bubble/bubble_frame_view.h" |
| #include "ui/views/controls/label.h" |
| #include "ui/views/layout/fill_layout.h" |
| +#include "ui/views/mus/native_widget_mus.h" |
| +#include "ui/views/mus/window_manager_connection.h" |
| #include "ui/views/widget/widget.h" |
| +#include "ui/wm/core/window_animations.h" |
| -namespace ash { |
| +namespace mash { |
| +namespace shelf { |
| namespace { |
| const int kTooltipTopBottomMargin = 3; |
| const int kTooltipLeftRightMargin = 10; |
| @@ -49,14 +50,20 @@ class ShelfTooltipManager::ShelfTooltipBubble |
| : public views::BubbleDelegateView { |
| public: |
| ShelfTooltipBubble(views::View* anchor, |
| - views::BubbleBorder::Arrow arrow, |
| - ShelfTooltipManager* host); |
| + views::BubbleBorder::Arrow arrow, |
| + const base::string16& text, |
| + ShelfTooltipManager* host); |
| void SetText(const base::string16& text); |
| void Close(); |
| private: |
| + // views::BubbleDelegateView overrides: |
| + void OnBeforeBubbleWidgetInit(views::Widget::InitParams* params, |
| + views::Widget* widget) const override; |
| + |
| // views::WidgetDelegate overrides: |
| + bool ShouldShowWindowTitle() const override; |
| void WindowClosing() override; |
| // views::View overrides: |
| @@ -71,6 +78,7 @@ class ShelfTooltipManager::ShelfTooltipBubble |
| ShelfTooltipManager::ShelfTooltipBubble::ShelfTooltipBubble( |
| views::View* anchor, |
| views::BubbleBorder::Arrow arrow, |
| + const base::string16& text, |
| ShelfTooltipManager* host) |
| : views::BubbleDelegateView(anchor, arrow), host_(host) { |
| gfx::Insets insets = gfx::Insets(kArrowOffsetTopBottom, |
| @@ -91,39 +99,45 @@ ShelfTooltipManager::ShelfTooltipBubble::ShelfTooltipBubble( |
| kTooltipTopBottomMargin, kTooltipLeftRightMargin)); |
| set_shadow(views::BubbleBorder::SMALL_SHADOW); |
| SetLayoutManager(new views::FillLayout()); |
| - // The anchor may not have the widget in tests. |
| - if (anchor->GetWidget() && anchor->GetWidget()->GetNativeView()) { |
| - aura::Window* root_window = |
| - anchor->GetWidget()->GetNativeView()->GetRootWindow(); |
| - set_parent_window(ash::Shell::GetInstance()->GetContainer( |
| - root_window, ash::kShellWindowId_SettingBubbleContainer)); |
| - } |
| - label_ = new views::Label; |
| + label_ = new views::Label(text); |
| label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| label_->SetEnabledColor(kTooltipTextColor); |
| AddChildView(label_); |
| views::BubbleDelegateView::CreateBubble(this); |
| } |
| -void ShelfTooltipManager::ShelfTooltipBubble::SetText( |
| - const base::string16& text) { |
| - label_->SetText(text); |
| - SizeToContents(); |
| -} |
| - |
| void ShelfTooltipManager::ShelfTooltipBubble::Close() { |
| if (GetWidget()) { |
| - host_ = NULL; |
| + host_ = nullptr; |
| GetWidget()->Close(); |
| } |
| } |
| +void ShelfTooltipManager::ShelfTooltipBubble::OnBeforeBubbleWidgetInit( |
| + views::Widget::InitParams* params, |
| + views::Widget* widget) const { |
| + // Ensure the widget is treated as a tooltip by the window manager. |
| + std::map<std::string, std::vector<uint8_t>> properties; |
| + properties[mash::wm::mojom::kWindowContainer_Property] = |
| + mojo::TypeConverter<const std::vector<uint8_t>, int32_t>::Convert( |
| + static_cast<int32_t>(mash::wm::mojom::Container::TOOLTIPS)); |
| + mus::Window* window = |
| + views::WindowManagerConnection::Get()->NewWindow(properties); |
| + params->native_widget = new views::NativeWidgetMus( |
| + widget, host_->shelf_view()->app()->shell(), window, |
| + mus::mojom::SurfaceType::DEFAULT); |
| +} |
| + |
| void ShelfTooltipManager::ShelfTooltipBubble::WindowClosing() { |
| views::BubbleDelegateView::WindowClosing(); |
| if (host_) |
| host_->OnBubbleClosed(this); |
| } |
| +bool ShelfTooltipManager::ShelfTooltipBubble::ShouldShowWindowTitle() const { |
| + return false; |
| +} |
| + |
| gfx::Size ShelfTooltipManager::ShelfTooltipBubble::GetPreferredSize() const { |
| gfx::Size pref_size = views::BubbleDelegateView::GetPreferredSize(); |
| if (pref_size.height() < kTooltipMinHeight) |
| @@ -133,28 +147,20 @@ gfx::Size ShelfTooltipManager::ShelfTooltipBubble::GetPreferredSize() const { |
| return pref_size; |
| } |
| -ShelfTooltipManager::ShelfTooltipManager( |
| - ShelfLayoutManager* shelf_layout_manager, |
| - ShelfView* shelf_view) |
| - : view_(NULL), |
| - widget_(NULL), |
| - anchor_(NULL), |
| - shelf_layout_manager_(shelf_layout_manager), |
| +ShelfTooltipManager::ShelfTooltipManager(ShelfView* shelf_view) |
| + : view_(nullptr), |
| + widget_(nullptr), |
| shelf_view_(shelf_view), |
| weak_factory_(this) { |
| - if (shelf_layout_manager) |
| - shelf_layout_manager->AddObserver(this); |
| - if (Shell::HasInstance()) |
| - Shell::GetInstance()->AddPreTargetHandler(this); |
| + // Ensure mouse movements between the shelf and its buttons isn't an exit. |
| + shelf_view_->set_notify_enter_exit_on_child(true); |
| + shelf_view_->AddPreTargetHandler(this); |
| } |
| ShelfTooltipManager::~ShelfTooltipManager() { |
| CancelHidingAnimation(); |
| Close(); |
| - if (shelf_layout_manager_) |
| - shelf_layout_manager_->RemoveObserver(this); |
| - if (Shell::HasInstance()) |
| - Shell::GetInstance()->RemovePreTargetHandler(this); |
| + shelf_view_->RemovePreTargetHandler(this); |
| } |
| void ShelfTooltipManager::ShowDelayed(views::View* anchor, |
| @@ -168,7 +174,7 @@ void ShelfTooltipManager::ShowDelayed(views::View* anchor, |
| } |
| } |
| - if (shelf_layout_manager_ && !shelf_layout_manager_->IsVisible()) |
| + if (!shelf_view_->GetWidget() || !shelf_view_->GetWidget()->IsVisible()) |
| return; |
| CreateBubble(anchor, text); |
| @@ -184,7 +190,7 @@ void ShelfTooltipManager::ShowImmediately(views::View* anchor, |
| Close(); |
| } |
| - if (shelf_layout_manager_ && !shelf_layout_manager_->IsVisible()) |
| + if (!shelf_view_->GetWidget() || !shelf_view_->GetWidget()->IsVisible()) |
| return; |
| CreateBubble(anchor, text); |
| @@ -195,23 +201,15 @@ void ShelfTooltipManager::Close() { |
| StopTimer(); |
| if (view_) { |
| view_->Close(); |
| - view_ = NULL; |
| - widget_ = NULL; |
| + view_ = nullptr; |
| + widget_ = nullptr; |
| } |
| } |
| void ShelfTooltipManager::OnBubbleClosed(views::BubbleDelegateView* view) { |
| if (view == view_) { |
| - view_ = NULL; |
| - widget_ = NULL; |
| - } |
| -} |
| - |
| -void ShelfTooltipManager::UpdateArrow() { |
| - if (view_) { |
| - CancelHidingAnimation(); |
| - Close(); |
| - ShowImmediately(anchor_, text_); |
| + view_ = nullptr; |
| + widget_ = nullptr; |
| } |
| } |
| @@ -222,7 +220,7 @@ void ShelfTooltipManager::ResetTimer() { |
| } |
| // We don't start the timer if the shelf isn't visible. |
| - if (shelf_layout_manager_ && !shelf_layout_manager_->IsVisible()) |
| + if (!shelf_view_->GetWidget() || !shelf_view_->GetWidget()->IsVisible()) |
| return; |
| CreateTimer(kTooltipAppearanceDelay); |
| @@ -232,13 +230,17 @@ void ShelfTooltipManager::StopTimer() { |
| timer_.reset(); |
| } |
| -bool ShelfTooltipManager::IsVisible() { |
| +bool ShelfTooltipManager::IsVisible() const { |
| if (timer_.get() && timer_->IsRunning()) |
| return false; |
| return widget_ && widget_->IsVisible(); |
| } |
| +views::View* ShelfTooltipManager::GetCurrentAnchorView() const { |
| + return view_ ? view_->GetAnchorView() : nullptr; |
| +} |
| + |
| void ShelfTooltipManager::CreateZeroDelayTimerForTest() { |
| CreateTimer(0); |
| } |
| @@ -252,24 +254,18 @@ void ShelfTooltipManager::OnMouseEvent(ui::MouseEvent* event) { |
| DCHECK(view_); |
| DCHECK(shelf_view_); |
| - // Pressing the mouse button anywhere should close the tooltip. |
| - if (event->type() == ui::ET_MOUSE_PRESSED) { |
| - CloseSoon(); |
| - return; |
| - } |
| - |
| - aura::Window* target = static_cast<aura::Window*>(event->target()); |
| - if (widget_->GetNativeWindow()->GetRootWindow() != target->GetRootWindow()) { |
| + // Close the tooltip when the mouse is pressed or exits the shelf view area. |
| + if (event->type() == ui::ET_MOUSE_PRESSED || |
| + (event->type() == ui::ET_MOUSE_EXITED && |
| + event->target() == shelf_view_)) { |
| CloseSoon(); |
| return; |
| } |
| - gfx::Point location_in_shelf_view = event->location(); |
| - aura::Window::ConvertPointToTarget( |
| - target, shelf_view_->GetWidget()->GetNativeWindow(), |
| - &location_in_shelf_view); |
| - |
| - if (shelf_view_->ShouldHideTooltip(location_in_shelf_view)) { |
| + gfx::Point location = event->location(); |
| + views::View* target = static_cast<views::View*>(event->target()); |
| + views::View::ConvertPointToTarget(target, shelf_view_, &location); |
| + if (shelf_view_->ShouldHideTooltip(location)) { |
| // Because this mouse event may arrive to |view_|, here we just schedule |
| // the closing event rather than directly calling Close(). |
| CloseSoon(); |
| @@ -277,7 +273,8 @@ void ShelfTooltipManager::OnMouseEvent(ui::MouseEvent* event) { |
| } |
| void ShelfTooltipManager::OnTouchEvent(ui::TouchEvent* event) { |
|
msw
2016/01/27 07:43:41
this probably doesn't work... (not watching all to
|
| - aura::Window* target = static_cast<aura::Window*>(event->target()); |
| + views::View* view = static_cast<views::View*>(event->target()); |
| + aura::Window* target = view->GetWidget()->GetNativeWindow(); |
| if (widget_ && widget_->IsVisible() && widget_->GetNativeWindow() != target) |
| Close(); |
| } |
| @@ -294,39 +291,16 @@ void ShelfTooltipManager::OnCancelMode(ui::CancelModeEvent* event) { |
| Close(); |
| } |
| -void ShelfTooltipManager::WillDeleteShelf() { |
|
msw
2016/01/27 07:43:41
Add a comment to restore.
|
| - shelf_layout_manager_ = NULL; |
| -} |
| - |
| -void ShelfTooltipManager::WillChangeVisibilityState( |
| - ShelfVisibilityState new_state) { |
| - if (new_state == SHELF_HIDDEN) { |
| - StopTimer(); |
| - Close(); |
| - } |
| -} |
| - |
| -void ShelfTooltipManager::OnAutoHideStateChanged(ShelfAutoHideState new_state) { |
| - if (new_state == SHELF_AUTO_HIDE_HIDDEN) { |
| - StopTimer(); |
| - // AutoHide state change happens during an event filter, so immediate close |
| - // may cause a crash in the HandleMouseEvent() after the filter. So we just |
| - // schedule the Close here. |
| - CloseSoon(); |
| - } |
| -} |
| - |
| void ShelfTooltipManager::CancelHidingAnimation() { |
| if (!widget_ || !widget_->GetNativeView()) |
| return; |
| - gfx::NativeView native_view = widget_->GetNativeView(); |
| - wm::SetWindowVisibilityAnimationTransition( |
| - native_view, wm::ANIMATE_NONE); |
| + ::wm::SetWindowVisibilityAnimationTransition(widget_->GetNativeView(), |
| + ::wm::ANIMATE_NONE); |
| } |
| void ShelfTooltipManager::CloseSoon() { |
| - base::MessageLoopForUI::current()->PostTask( |
| + base::MessageLoop::current()->PostTask( |
| FROM_HERE, |
| base::Bind(&ShelfTooltipManager::Close, weak_factory_.GetWeakPtr())); |
| } |
| @@ -341,25 +315,17 @@ void ShelfTooltipManager::ShowInternal() { |
| void ShelfTooltipManager::CreateBubble(views::View* anchor, |
| const base::string16& text) { |
| DCHECK(!view_); |
| - |
| - anchor_ = anchor; |
| - text_ = text; |
| - views::BubbleBorder::Arrow arrow = |
| - shelf_layout_manager_->SelectValueForShelfAlignment( |
| - views::BubbleBorder::BOTTOM_CENTER, |
| - views::BubbleBorder::LEFT_CENTER, |
| - views::BubbleBorder::RIGHT_CENTER, |
| - views::BubbleBorder::TOP_CENTER); |
| - |
| - view_ = new ShelfTooltipBubble(anchor, arrow, this); |
| + views::BubbleBorder::Arrow arrow = shelf_view_->SelectValueForShelfAlignment( |
| + views::BubbleBorder::BOTTOM_CENTER, views::BubbleBorder::LEFT_CENTER, |
| + views::BubbleBorder::RIGHT_CENTER, views::BubbleBorder::TOP_CENTER); |
| + view_ = new ShelfTooltipBubble(anchor, arrow, text, this); |
| widget_ = view_->GetWidget(); |
| - view_->SetText(text_); |
| gfx::NativeView native_view = widget_->GetNativeView(); |
| - wm::SetWindowVisibilityAnimationType( |
| - native_view, wm::WINDOW_VISIBILITY_ANIMATION_TYPE_VERTICAL); |
| - wm::SetWindowVisibilityAnimationTransition( |
| - native_view, wm::ANIMATE_HIDE); |
| + ::wm::SetWindowVisibilityAnimationType( |
| + native_view, ::wm::WINDOW_VISIBILITY_ANIMATION_TYPE_VERTICAL); |
| + ::wm::SetWindowVisibilityAnimationTransition( |
| + native_view, ::wm::ANIMATE_HIDE); |
| } |
| void ShelfTooltipManager::CreateTimer(int delay_in_ms) { |
| @@ -371,4 +337,5 @@ void ShelfTooltipManager::CreateTimer(int delay_in_ms) { |
| timer_.reset(new_timer); |
| } |
| -} // namespace ash |
| +} // namespace shelf |
| +} // namespace mash |