Commit ebbe6377 authored by oshima's avatar oshima Committed by Commit bot

Tooltip Cleanup

This is prep for tooltip improvement: https://codereview.chromium.org/924433002/ (WIP)

* Consolidated GetMaxWidth
* Eliminate screen type argument. This can be obtained from tooltip window.
* Move tooltip truncate code from TooltipManager to TooltipController

BUG=None
TEST=no functional change. all test should pass.

Review URL: https://codereview.chromium.org/916423002

Cr-Commit-Position: refs/heads/master@{#317081}
parent b71febc4
...@@ -990,10 +990,8 @@ void Shell::Init(const ShellInitParams& init_params) { ...@@ -990,10 +990,8 @@ void Shell::Init(const ShellInitParams& init_params) {
window_selector_controller_.reset(new WindowSelectorController()); window_selector_controller_.reset(new WindowSelectorController());
window_cycle_controller_.reset(new WindowCycleController()); window_cycle_controller_.reset(new WindowCycleController());
tooltip_controller_.reset( tooltip_controller_.reset(new views::corewm::TooltipController(
new views::corewm::TooltipController( scoped_ptr<views::corewm::Tooltip>(new views::corewm::TooltipAura)));
scoped_ptr<views::corewm::Tooltip>(
new views::corewm::TooltipAura(gfx::SCREEN_TYPE_ALTERNATE))));
AddPreTargetHandler(tooltip_controller_.get()); AddPreTargetHandler(tooltip_controller_.get());
event_client_.reset(new EventClientImpl); event_client_.reset(new EventClientImpl);
......
...@@ -690,11 +690,10 @@ base::string16 BookmarkBarView::CreateToolTipForURLAndTitle( ...@@ -690,11 +690,10 @@ base::string16 BookmarkBarView::CreateToolTipForURLAndTitle(
const GURL& url, const GURL& url,
const base::string16& title, const base::string16& title,
Profile* profile) { Profile* profile) {
int max_width = views::TooltipManager::GetMaxWidth( const views::TooltipManager* tooltip_manager = widget->GetTooltipManager();
screen_loc.x(), int max_width = tooltip_manager->GetMaxWidth(screen_loc,
screen_loc.y(), widget->GetNativeView());
widget->GetNativeView()); const gfx::FontList tt_fonts = tooltip_manager->GetFontList();
const gfx::FontList tt_fonts = widget->GetTooltipManager()->GetFontList();
base::string16 result; base::string16 result;
// First the title. // First the title.
......
...@@ -25,6 +25,10 @@ class VIEWS_EXPORT Tooltip { ...@@ -25,6 +25,10 @@ class VIEWS_EXPORT Tooltip {
public: public:
virtual ~Tooltip() {} virtual ~Tooltip() {}
// Returns the max width of the tooltip when shown at the specified location.
virtual int GetMaxWidth(const gfx::Point& location,
aura::Window* context) const = 0;
// Updates the text on the tooltip and resizes to fit. // Updates the text on the tooltip and resizes to fit.
virtual void SetText(aura::Window* window, virtual void SetText(aura::Window* window,
const base::string16& tooltip_text, const base::string16& tooltip_text,
......
...@@ -47,9 +47,8 @@ views::Widget* CreateTooltipWidget(aura::Window* tooltip_window) { ...@@ -47,9 +47,8 @@ views::Widget* CreateTooltipWidget(aura::Window* tooltip_window) {
namespace views { namespace views {
namespace corewm { namespace corewm {
TooltipAura::TooltipAura(gfx::ScreenType screen_type) TooltipAura::TooltipAura()
: screen_type_(screen_type), : widget_(NULL),
widget_(NULL),
tooltip_window_(NULL) { tooltip_window_(NULL) {
label_.set_owned_by_client(); label_.set_owned_by_client();
label_.SetMultiLine(true); label_.SetMultiLine(true);
...@@ -142,19 +141,11 @@ void TooltipAura::TrimTooltipToFit(const gfx::FontList& font_list, ...@@ -142,19 +141,11 @@ void TooltipAura::TrimTooltipToFit(const gfx::FontList& font_list,
*text = result; *text = result;
} }
int TooltipAura::GetMaxWidth(const gfx::Point& location) const {
// TODO(varunjain): implementation duplicated in tooltip_manager_aura. Figure
// out a way to merge.
gfx::Screen* screen = gfx::Screen::GetScreenByType(screen_type_);
gfx::Rect display_bounds(screen->GetDisplayNearestPoint(location).bounds());
return (display_bounds.width() + 1) / 2;
}
void TooltipAura::SetTooltipBounds(const gfx::Point& mouse_pos, void TooltipAura::SetTooltipBounds(const gfx::Point& mouse_pos,
const gfx::Size& tooltip_size) { const gfx::Size& tooltip_size) {
gfx::Rect tooltip_rect(mouse_pos, tooltip_size); gfx::Rect tooltip_rect(mouse_pos, tooltip_size);
tooltip_rect.Offset(kCursorOffsetX, kCursorOffsetY); tooltip_rect.Offset(kCursorOffsetX, kCursorOffsetY);
gfx::Screen* screen = gfx::Screen::GetScreenByType(screen_type_); gfx::Screen* screen = gfx::Screen::GetScreenFor(tooltip_window_);
gfx::Rect display_bounds(screen->GetDisplayNearestPoint(mouse_pos).bounds()); gfx::Rect display_bounds(screen->GetDisplayNearestPoint(mouse_pos).bounds());
// If tooltip is out of bounds on the x axis, we simply shift it // If tooltip is out of bounds on the x axis, we simply shift it
...@@ -181,6 +172,13 @@ void TooltipAura::DestroyWidget() { ...@@ -181,6 +172,13 @@ void TooltipAura::DestroyWidget() {
} }
} }
int TooltipAura::GetMaxWidth(const gfx::Point& location,
aura::Window* context) const {
gfx::Screen* screen = gfx::Screen::GetScreenFor(context);
gfx::Rect display_bounds(screen->GetDisplayNearestPoint(location).bounds());
return std::min(kTooltipMaxWidthPixels, (display_bounds.width() + 1) / 2);
}
void TooltipAura::SetText(aura::Window* window, void TooltipAura::SetText(aura::Window* window,
const base::string16& tooltip_text, const base::string16& tooltip_text,
const gfx::Point& location) { const gfx::Point& location) {
...@@ -188,8 +186,8 @@ void TooltipAura::SetText(aura::Window* window, ...@@ -188,8 +186,8 @@ void TooltipAura::SetText(aura::Window* window,
int max_width = 0; int max_width = 0;
int line_count = 0; int line_count = 0;
base::string16 trimmed_text(tooltip_text); base::string16 trimmed_text(tooltip_text);
TrimTooltipToFit(label_.font_list(), GetMaxWidth(location), &trimmed_text, TrimTooltipToFit(label_.font_list(), GetMaxWidth(location, window),
&max_width, &line_count); &trimmed_text, &max_width, &line_count);
label_.SetText(trimmed_text); label_.SetText(trimmed_text);
if (!widget_) { if (!widget_) {
......
...@@ -23,7 +23,7 @@ namespace corewm { ...@@ -23,7 +23,7 @@ namespace corewm {
// Implementation of Tooltip that shows the tooltip using a Widget and Label. // Implementation of Tooltip that shows the tooltip using a Widget and Label.
class VIEWS_EXPORT TooltipAura : public Tooltip, public WidgetObserver { class VIEWS_EXPORT TooltipAura : public Tooltip, public WidgetObserver {
public: public:
explicit TooltipAura(gfx::ScreenType screen_type); TooltipAura();
~TooltipAura() override; ~TooltipAura() override;
// Trims the tooltip to fit in the width |max_width|, setting |text| to the // Trims the tooltip to fit in the width |max_width|, setting |text| to the
...@@ -37,9 +37,6 @@ class VIEWS_EXPORT TooltipAura : public Tooltip, public WidgetObserver { ...@@ -37,9 +37,6 @@ class VIEWS_EXPORT TooltipAura : public Tooltip, public WidgetObserver {
int* line_count); int* line_count);
private: private:
// Returns the max width of the tooltip when shown at the specified location.
int GetMaxWidth(const gfx::Point& location) const;
// Adjusts the bounds given by the arguments to fit inside the desktop // Adjusts the bounds given by the arguments to fit inside the desktop
// and applies the adjusted bounds to the label_. // and applies the adjusted bounds to the label_.
void SetTooltipBounds(const gfx::Point& mouse_pos, void SetTooltipBounds(const gfx::Point& mouse_pos,
...@@ -49,6 +46,8 @@ class VIEWS_EXPORT TooltipAura : public Tooltip, public WidgetObserver { ...@@ -49,6 +46,8 @@ class VIEWS_EXPORT TooltipAura : public Tooltip, public WidgetObserver {
void DestroyWidget(); void DestroyWidget();
// Tooltip: // Tooltip:
int GetMaxWidth(const gfx::Point& location,
aura::Window* context) const override;
void SetText(aura::Window* window, void SetText(aura::Window* window,
const base::string16& tooltip_text, const base::string16& tooltip_text,
const gfx::Point& location) override; const gfx::Point& location) override;
...@@ -59,8 +58,6 @@ class VIEWS_EXPORT TooltipAura : public Tooltip, public WidgetObserver { ...@@ -59,8 +58,6 @@ class VIEWS_EXPORT TooltipAura : public Tooltip, public WidgetObserver {
// WidgetObserver: // WidgetObserver:
void OnWidgetDestroying(Widget* widget) override; void OnWidgetDestroying(Widget* widget) override;
const gfx::ScreenType screen_type_;
// The label showing the tooltip. // The label showing the tooltip.
Label label_; Label label_;
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include "ui/gfx/font.h" #include "ui/gfx/font.h"
#include "ui/gfx/geometry/rect.h" #include "ui/gfx/geometry/rect.h"
#include "ui/gfx/screen.h" #include "ui/gfx/screen.h"
#include "ui/gfx/text_elider.h"
#include "ui/views/corewm/tooltip.h" #include "ui/views/corewm/tooltip.h"
#include "ui/views/widget/tooltip_manager.h" #include "ui/views/widget/tooltip_manager.h"
#include "ui/wm/public/drag_drop_client.h" #include "ui/wm/public/drag_drop_client.h"
...@@ -27,6 +28,7 @@ namespace { ...@@ -27,6 +28,7 @@ namespace {
const int kTooltipTimeoutMs = 500; const int kTooltipTimeoutMs = 500;
const int kDefaultTooltipShownTimeoutMs = 10000; const int kDefaultTooltipShownTimeoutMs = 10000;
const size_t kMaxTooltipLength = 1024;
// Returns true if |target| is a valid window to get the tooltip from. // Returns true if |target| is a valid window to get the tooltip from.
// |event_target| is the original target from the event and |target| the window // |event_target| is the original target from the event and |target| the window
...@@ -129,6 +131,11 @@ TooltipController::~TooltipController() { ...@@ -129,6 +131,11 @@ TooltipController::~TooltipController() {
tooltip_window_->RemoveObserver(this); tooltip_window_->RemoveObserver(this);
} }
int TooltipController::GetMaxWidth(const gfx::Point& location,
gfx::NativeView context) const {
return tooltip_->GetMaxWidth(location, context);
}
void TooltipController::UpdateTooltip(aura::Window* target) { void TooltipController::UpdateTooltip(aura::Window* target) {
// If tooltip is visible, we may want to hide it. If it is not, we are ok. // If tooltip is visible, we may want to hide it. If it is not, we are ok.
if (tooltip_window_ == target && tooltip_->IsVisible()) if (tooltip_window_ == target && tooltip_->IsVisible())
...@@ -298,8 +305,8 @@ void TooltipController::UpdateIfRequired() { ...@@ -298,8 +305,8 @@ void TooltipController::UpdateIfRequired() {
if (tooltip_text_ != tooltip_text || !tooltip_->IsVisible() || ids_differ) { if (tooltip_text_ != tooltip_text || !tooltip_->IsVisible() || ids_differ) {
tooltip_shown_timer_.Stop(); tooltip_shown_timer_.Stop();
tooltip_text_ = tooltip_text; tooltip_text_ = tooltip_text;
base::string16 trimmed_text(tooltip_text_); base::string16 trimmed_text =
views::TooltipManager::TrimTooltipText(&trimmed_text); gfx::TruncateString(tooltip_text_, kMaxTooltipLength, gfx::WORD_BREAK);
// If the string consists entirely of whitespace, then don't both showing it // If the string consists entirely of whitespace, then don't both showing it
// (an empty tooltip is useless). // (an empty tooltip is useless).
base::string16 whitespace_removed_text; base::string16 whitespace_removed_text;
......
...@@ -38,6 +38,8 @@ class VIEWS_EXPORT TooltipController : public aura::client::TooltipClient, ...@@ -38,6 +38,8 @@ class VIEWS_EXPORT TooltipController : public aura::client::TooltipClient,
~TooltipController() override; ~TooltipController() override;
// Overridden from aura::client::TooltipClient. // Overridden from aura::client::TooltipClient.
int GetMaxWidth(const gfx::Point& location,
aura::Window* context) const override;
void UpdateTooltip(aura::Window* target) override; void UpdateTooltip(aura::Window* target) override;
void SetTooltipShownTimeout(aura::Window* target, int timeout_in_ms) override; void SetTooltipShownTimeout(aura::Window* target, int timeout_in_ms) override;
void SetTooltipsEnabled(bool enable) override; void SetTooltipsEnabled(bool enable) override;
......
...@@ -90,7 +90,7 @@ class TooltipControllerTest : public aura::test::AuraTestBase { ...@@ -90,7 +90,7 @@ class TooltipControllerTest : public aura::test::AuraTestBase {
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
controller_.reset(new TooltipController( controller_.reset(new TooltipController(
scoped_ptr<views::corewm::Tooltip>( scoped_ptr<views::corewm::Tooltip>(
new views::corewm::TooltipAura(gfx::SCREEN_TYPE_ALTERNATE)))); new views::corewm::TooltipAura)));
root_window()->AddPreTargetHandler(controller_.get()); root_window()->AddPreTargetHandler(controller_.get());
SetTooltipClient(root_window(), controller_.get()); SetTooltipClient(root_window(), controller_.get());
#endif #endif
...@@ -532,6 +532,10 @@ class TestTooltip : public Tooltip { ...@@ -532,6 +532,10 @@ class TestTooltip : public Tooltip {
const base::string16& tooltip_text() const { return tooltip_text_; } const base::string16& tooltip_text() const { return tooltip_text_; }
// Tooltip: // Tooltip:
int GetMaxWidth(const gfx::Point& location,
aura::Window* context) const override {
return 100;
}
void SetText(aura::Window* window, void SetText(aura::Window* window,
const base::string16& tooltip_text, const base::string16& tooltip_text,
const gfx::Point& location) override { const gfx::Point& location) override {
......
...@@ -95,6 +95,16 @@ void TooltipWin::PositionTooltip() { ...@@ -95,6 +95,16 @@ void TooltipWin::PositionTooltip() {
0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE); 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
} }
int TooltipWin::GetMaxWidth(const gfx::Point& location,
aura::Window* context) const {
// This code only runs for non-metro, so GetNativeScreen() is fine.
const gfx::Point screen_point = gfx::win::DIPToScreenPoint(location);
gfx::Display display(
gfx::Screen::GetNativeScreen()->GetDisplayNearestPoint(screen_point));
const gfx::Rect monitor_bounds = display.bounds();
return (monitor_bounds.width() + 1) / 2;
}
void TooltipWin::SetText(aura::Window* window, void TooltipWin::SetText(aura::Window* window,
const base::string16& tooltip_text, const base::string16& tooltip_text,
const gfx::Point& location) { const gfx::Point& location) {
...@@ -116,12 +126,7 @@ void TooltipWin::SetText(aura::Window* window, ...@@ -116,12 +126,7 @@ void TooltipWin::SetText(aura::Window* window,
SendMessage(tooltip_hwnd_, TTM_SETTOOLINFO, 0, SendMessage(tooltip_hwnd_, TTM_SETTOOLINFO, 0,
reinterpret_cast<LPARAM>(&toolinfo_)); reinterpret_cast<LPARAM>(&toolinfo_));
// This code only runs for non-metro, so GetNativeScreen() is fine. int max_width = GetMaxWidth(location_, window);
const gfx::Point screen_point = gfx::win::DIPToScreenPoint(location_);
gfx::Display display(
gfx::Screen::GetNativeScreen()->GetDisplayNearestPoint(screen_point));
const gfx::Rect monitor_bounds = display.bounds();
int max_width = (monitor_bounds.width() + 1) / 2;
SendMessage(tooltip_hwnd_, TTM_SETMAXTIPWIDTH, 0, max_width); SendMessage(tooltip_hwnd_, TTM_SETMAXTIPWIDTH, 0, max_width);
} }
......
...@@ -22,7 +22,7 @@ namespace corewm { ...@@ -22,7 +22,7 @@ namespace corewm {
class VIEWS_EXPORT TooltipWin : public Tooltip { class VIEWS_EXPORT TooltipWin : public Tooltip {
public: public:
explicit TooltipWin(HWND parent); explicit TooltipWin(HWND parent);
virtual ~TooltipWin(); ~TooltipWin() override;
// HandleNotify() is forwarded from DesktopWindowTreeHostWin to keep the // HandleNotify() is forwarded from DesktopWindowTreeHostWin to keep the
// native tooltip in sync. // native tooltip in sync.
...@@ -37,12 +37,14 @@ class VIEWS_EXPORT TooltipWin : public Tooltip { ...@@ -37,12 +37,14 @@ class VIEWS_EXPORT TooltipWin : public Tooltip {
void PositionTooltip(); void PositionTooltip();
// Tooltip: // Tooltip:
virtual void SetText(aura::Window* window, int GetMaxWidth(const gfx::Point& location,
const base::string16& tooltip_text, aura::Window* context) const override;
const gfx::Point& location) override; void SetText(aura::Window* window,
virtual void Show() override; const base::string16& tooltip_text,
virtual void Hide() override; const gfx::Point& location) override;
virtual bool IsVisible() override; void Show() override;
void Hide() override;
bool IsVisible() override;
// The window |tooltip_hwnd_| is parented to. // The window |tooltip_hwnd_| is parented to.
HWND parent_hwnd_; HWND parent_hwnd_;
......
...@@ -289,7 +289,7 @@ void DesktopWindowTreeHostX11::OnNativeWidgetCreated( ...@@ -289,7 +289,7 @@ void DesktopWindowTreeHostX11::OnNativeWidgetCreated(
} }
scoped_ptr<corewm::Tooltip> DesktopWindowTreeHostX11::CreateTooltip() { scoped_ptr<corewm::Tooltip> DesktopWindowTreeHostX11::CreateTooltip() {
return make_scoped_ptr(new corewm::TooltipAura(gfx::SCREEN_TYPE_NATIVE)); return make_scoped_ptr(new corewm::TooltipAura);
} }
scoped_ptr<aura::client::DragDropClient> scoped_ptr<aura::client::DragDropClient>
......
...@@ -4,33 +4,9 @@ ...@@ -4,33 +4,9 @@
#include "ui/views/widget/tooltip_manager.h" #include "ui/views/widget/tooltip_manager.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/screen.h"
#include "ui/gfx/text_elider.h"
namespace views { namespace views {
const size_t kMaxTooltipLength = 1024;
// static // static
const char TooltipManager::kGroupingPropertyKey[] = "GroupingPropertyKey"; const char TooltipManager::kGroupingPropertyKey[] = "GroupingPropertyKey";
// static
int TooltipManager::GetMaxWidth(int x, int y, gfx::NativeView context) {
return GetMaxWidth(gfx::Screen::GetScreenFor(context)->GetDisplayNearestPoint(
gfx::Point(x, y)));
}
// static
int TooltipManager::GetMaxWidth(const gfx::Display& display) {
return (display.bounds().width() + 1) / 2;
}
// static
void TooltipManager::TrimTooltipText(base::string16* text) {
// Clamp the tooltip length to kMaxTooltipLength so that we don't
// accidentally DOS the user with a mega tooltip.
*text = gfx::TruncateString(*text, kMaxTooltipLength, gfx::WORD_BREAK);
}
} // namespace views } // namespace views
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
namespace gfx { namespace gfx {
class Display; class Display;
class FontList; class FontList;
class Point;
} // namespace gfx } // namespace gfx
namespace views { namespace views {
...@@ -38,17 +39,11 @@ class VIEWS_EXPORT TooltipManager { ...@@ -38,17 +39,11 @@ class VIEWS_EXPORT TooltipManager {
TooltipManager() {} TooltipManager() {}
virtual ~TooltipManager() {} virtual ~TooltipManager() {}
// Returns the maximum width of the tooltip. |x| and |y| give the location // Returns the maximum width of the tooltip. |point| gives the location
// the tooltip is to be displayed on in screen coordinates. |context| is // the tooltip is to be displayed on in screen coordinates. |context| is
// used to determine which gfx::Screen should be used. // used to determine which gfx::Screen should be used.
static int GetMaxWidth(int x, int y, gfx::NativeView context); virtual int GetMaxWidth(const gfx::Point& location,
gfx::NativeView context) const = 0;
// Same as GetMaxWidth(), but takes a Display.
static int GetMaxWidth(const gfx::Display& display);
// If necessary trims the text of a tooltip to ensure we don't try to display
// a mega-tooltip.
static void TrimTooltipText(base::string16* text);
// Returns the font list used for tooltips. // Returns the font list used for tooltips.
virtual const gfx::FontList& GetFontList() const = 0; virtual const gfx::FontList& GetFontList() const = 0;
......
...@@ -81,6 +81,12 @@ const gfx::FontList& TooltipManagerAura::GetFontList() const { ...@@ -81,6 +81,12 @@ const gfx::FontList& TooltipManagerAura::GetFontList() const {
return GetDefaultFontList(); return GetDefaultFontList();
} }
int TooltipManagerAura::GetMaxWidth(const gfx::Point& point,
aura::Window* context) const {
return aura::client::GetTooltipClient(context->GetRootWindow())->
GetMaxWidth(point, context);
}
void TooltipManagerAura::UpdateTooltip() { void TooltipManagerAura::UpdateTooltip() {
aura::Window* root_window = GetWindow()->GetRootWindow(); aura::Window* root_window = GetWindow()->GetRootWindow();
if (aura::client::GetTooltipClient(root_window)) { if (aura::client::GetTooltipClient(root_window)) {
......
...@@ -38,6 +38,8 @@ class TooltipManagerAura : public TooltipManager { ...@@ -38,6 +38,8 @@ class TooltipManagerAura : public TooltipManager {
static const gfx::FontList& GetDefaultFontList(); static const gfx::FontList& GetDefaultFontList();
// TooltipManager: // TooltipManager:
int GetMaxWidth(const gfx::Point& location,
aura::Window* context) const override;
const gfx::FontList& GetFontList() const override; const gfx::FontList& GetFontList() const override;
void UpdateTooltip() override; void UpdateTooltip() override;
void TooltipTextChanged(View* view) override; void TooltipTextChanged(View* view) override;
......
...@@ -8,6 +8,10 @@ ...@@ -8,6 +8,10 @@
#include "ui/aura/aura_export.h" #include "ui/aura/aura_export.h"
#include "ui/gfx/font.h" #include "ui/gfx/font.h"
namespace gfx {
class Point;
}
namespace aura { namespace aura {
class Window; class Window;
namespace client { namespace client {
...@@ -16,6 +20,10 @@ class ScopedTooltipDisabler; ...@@ -16,6 +20,10 @@ class ScopedTooltipDisabler;
class AURA_EXPORT TooltipClient { class AURA_EXPORT TooltipClient {
public: public:
// Returns the max width of the tooltip when shown at the specified location.
virtual int GetMaxWidth(const gfx::Point& point,
aura::Window* context) const = 0;
// Informs the shell tooltip manager of change in tooltip for window |target|. // Informs the shell tooltip manager of change in tooltip for window |target|.
virtual void UpdateTooltip(Window* target) = 0; virtual void UpdateTooltip(Window* target) = 0;
......
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