1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef UI_MESSAGE_CENTER_VIEWS_MESSAGE_POPUP_VIEW_H_
#define UI_MESSAGE_CENTER_VIEWS_MESSAGE_POPUP_VIEW_H_
#include <optional>
#include "base/memory/raw_ptr.h"
#include "base/scoped_observation.h"
#include "ui/message_center/message_center_export.h"
#include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_delegate.h"
#include "ui/views/widget/widget_observer.h"
namespace message_center {
class MessagePopupCollection;
class MessageView;
class Notification;
// The widget delegate of a notification popup. The view is owned by the widget.
class MESSAGE_CENTER_EXPORT MessagePopupView
: public views::FocusChangeListener,
public views::WidgetDelegateView {
METADATA_HEADER(MessagePopupView, views::WidgetDelegateView)
public:
MessagePopupView(MessageView* message_view,
MessagePopupCollection* popup_collection,
bool a11y_feedback_on_init);
MessagePopupView(const MessagePopupView&) = delete;
MessagePopupView& operator=(const MessagePopupView&) = delete;
~MessagePopupView() override;
// Update notification contents to |notification|. Virtual for unit testing.
virtual void UpdateContents(const Notification& notification);
// Updates the content of the child notification view inside this popup.
// Virtual for unit testing.
virtual void UpdateContentsForChildNotification(
const std::string& notification_id,
const Notification& notification);
// Return opacity of the widget.
float GetOpacity() const;
// Sets widget bounds.
void SetPopupBounds(const gfx::Rect& bounds);
// Sets widget transform.
void SetPopupTransform(const gfx::Transform& transform);
// Set widget opacity.
void SetOpacity(float opacity);
// Collapses the notification unless the user is interacting with it. The
// request can be ignored. Virtual for unit testing.
virtual void AutoCollapse();
// Shows popup. After this call, MessagePopupView should be owned by the
// widget.
std::unique_ptr<views::Widget> Show();
// Closes popup. It should be callable even if Show() is not called, and
// in such case MessagePopupView should be deleted. Virtual for unit testing.
virtual void Close();
// views::FocusChangeListener,
void OnDidChangeFocus(views::View* before, views::View* now) override;
// views::WidgetDelegateView:
void OnMouseEntered(const ui::MouseEvent& event) override;
void OnMouseExited(const ui::MouseEvent& event) override;
void ChildPreferredSizeChanged(views::View* child) override;
void OnDisplayChanged() override;
void OnWorkAreaChanged() override;
void OnFocus() override;
void AddedToWidget() override;
void RemovedFromWidget() override;
// Returns the cached height if `width` is the same as last call. Otherwise,
// call `GetHeightForWidth` to recalculate.
int GetCachedHeightForWidth(int width);
bool is_hovered() const { return is_hovered_; }
bool is_focused() const { return is_focused_; }
MessageView* message_view() { return message_view_; }
bool view_added_to_widget() { return view_added_to_widget_; }
protected:
// For unit testing.
explicit MessagePopupView(MessagePopupCollection* popup_collection);
private:
// True if the view has a widget and the widget is not closed.
bool IsWidgetValid() const;
void UpdateAccessibleName(const std::u16string& new_name) const;
void OnMessageViewNameUpdated(
bool should_make_spoken_feedback_for_popup_updates);
// Owned by views hierarchy.
raw_ptr<MessageView> message_view_;
// Unowned.
const raw_ptr<MessagePopupCollection> popup_collection_;
const bool a11y_feedback_on_init_;
bool is_hovered_ = false;
bool is_focused_ = false;
// Was this view ever hosted in a Widget? If so, the Widget will "own" this
// view and delete it accordingly. Otherwise, the MessagePopupCollection is
// responsible for its destruction.
bool view_added_to_widget_ = false;
// Owned by the widget associated with this view.
raw_ptr<views::FocusManager> focus_manager_ = nullptr;
std::optional<gfx::Size> cached_preferred_size_;
base::WeakPtrFactory<MessagePopupView> weak_ptr_factory_{this};
};
} // namespace message_center
#endif // UI_MESSAGE_CENTER_VIEWS_MESSAGE_POPUP_VIEW_H_
|