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) {
window_selector_controller_.reset(new WindowSelectorController());
window_cycle_controller_.reset(new WindowCycleController());
tooltip_controller_.reset(
new views::corewm::TooltipController(
scoped_ptr<views::corewm::Tooltip>(
new views::corewm::TooltipAura(gfx::SCREEN_TYPE_ALTERNATE))));
tooltip_controller_.reset(new views::corewm::TooltipController(
scoped_ptr<views::corewm::Tooltip>(new views::corewm::TooltipAura)));
AddPreTargetHandler(tooltip_controller_.get());
event_client_.reset(new EventClientImpl);
......
......@@ -690,11 +690,10 @@ base::string16 BookmarkBarView::CreateToolTipForURLAndTitle(
const GURL& url,
const base::string16& title,
Profile* profile) {
int max_width = views::TooltipManager::GetMaxWidth(
screen_loc.x(),
screen_loc.y(),
widget->GetNativeView());
const gfx::FontList tt_fonts = widget->GetTooltipManager()->GetFontList();
const views::TooltipManager* tooltip_manager = widget->GetTooltipManager();
int max_width = tooltip_manager->GetMaxWidth(screen_loc,
widget->GetNativeView());
const gfx::FontList tt_fonts = tooltip_manager->GetFontList();
base::string16 result;
// First the title.
......
......@@ -25,6 +25,10 @@ class VIEWS_EXPORT Tooltip {
public:
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.
virtual void SetText(aura::Window* window,
const base::string16& tooltip_text,
......
......@@ -47,9 +47,8 @@ views::Widget* CreateTooltipWidget(aura::Window* tooltip_window) {
namespace views {
namespace corewm {
TooltipAura::TooltipAura(gfx::ScreenType screen_type)
: screen_type_(screen_type),
widget_(NULL),
TooltipAura::TooltipAura()
: widget_(NULL),
tooltip_window_(NULL) {
label_.set_owned_by_client();
label_.SetMultiLine(true);
......@@ -142,19 +141,11 @@ void TooltipAura::TrimTooltipToFit(const gfx::FontList& font_list,
*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,
const gfx::Size& tooltip_size) {
gfx::Rect tooltip_rect(mouse_pos, tooltip_size);
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());
// If tooltip is out of bounds on the x axis, we simply shift it
......@@ -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,
const base::string16& tooltip_text,
const gfx::Point& location) {
......@@ -188,8 +186,8 @@ void TooltipAura::SetText(aura::Window* window,
int max_width = 0;
int line_count = 0;
base::string16 trimmed_text(tooltip_text);
TrimTooltipToFit(label_.font_list(), GetMaxWidth(location), &trimmed_text,
&max_width, &line_count);
TrimTooltipToFit(label_.font_list(), GetMaxWidth(location, window),
&trimmed_text, &max_width, &line_count);
label_.SetText(trimmed_text);
if (!widget_) {
......
......@@ -23,7 +23,7 @@ namespace corewm {
// Implementation of Tooltip that shows the tooltip using a Widget and Label.
class VIEWS_EXPORT TooltipAura : public Tooltip, public WidgetObserver {
public:
explicit TooltipAura(gfx::ScreenType screen_type);
TooltipAura();
~TooltipAura() override;
// 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 {
int* line_count);
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
// and applies the adjusted bounds to the label_.
void SetTooltipBounds(const gfx::Point& mouse_pos,
......@@ -49,6 +46,8 @@ class VIEWS_EXPORT TooltipAura : public Tooltip, public WidgetObserver {
void DestroyWidget();
// Tooltip:
int GetMaxWidth(const gfx::Point& location,
aura::Window* context) const override;
void SetText(aura::Window* window,
const base::string16& tooltip_text,
const gfx::Point& location) override;
......@@ -59,8 +58,6 @@ class VIEWS_EXPORT TooltipAura : public Tooltip, public WidgetObserver {
// WidgetObserver:
void OnWidgetDestroying(Widget* widget) override;
const gfx::ScreenType screen_type_;
// The label showing the tooltip.
Label label_;
......
......@@ -17,6 +17,7 @@
#include "ui/gfx/font.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/screen.h"
#include "ui/gfx/text_elider.h"
#include "ui/views/corewm/tooltip.h"
#include "ui/views/widget/tooltip_manager.h"
#include "ui/wm/public/drag_drop_client.h"
......@@ -27,6 +28,7 @@ namespace {
const int kTooltipTimeoutMs = 500;
const int kDefaultTooltipShownTimeoutMs = 10000;
const size_t kMaxTooltipLength = 1024;
// 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
......@@ -129,6 +131,11 @@ TooltipController::~TooltipController() {
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) {
// If tooltip is visible, we may want to hide it. If it is not, we are ok.
if (tooltip_window_ == target && tooltip_->IsVisible())
......@@ -298,8 +305,8 @@ void TooltipController::UpdateIfRequired() {
if (tooltip_text_ != tooltip_text || !tooltip_->IsVisible() || ids_differ) {
tooltip_shown_timer_.Stop();
tooltip_text_ = tooltip_text;
base::string16 trimmed_text(tooltip_text_);
views::TooltipManager::TrimTooltipText(&trimmed_text);
base::string16 trimmed_text =
gfx::TruncateString(tooltip_text_, kMaxTooltipLength, gfx::WORD_BREAK);
// If the string consists entirely of whitespace, then don't both showing it
// (an empty tooltip is useless).
base::string16 whitespace_removed_text;
......
......@@ -38,6 +38,8 @@ class VIEWS_EXPORT TooltipController : public aura::client::TooltipClient,
~TooltipController() override;
// Overridden from aura::client::TooltipClient.
int GetMaxWidth(const gfx::Point& location,
aura::Window* context) const override;
void UpdateTooltip(aura::Window* target) override;
void SetTooltipShownTimeout(aura::Window* target, int timeout_in_ms) override;
void SetTooltipsEnabled(bool enable) override;
......
......@@ -90,7 +90,7 @@ class TooltipControllerTest : public aura::test::AuraTestBase {
#if defined(OS_CHROMEOS)
controller_.reset(new TooltipController(
scoped_ptr<views::corewm::Tooltip>(
new views::corewm::TooltipAura(gfx::SCREEN_TYPE_ALTERNATE))));
new views::corewm::TooltipAura)));
root_window()->AddPreTargetHandler(controller_.get());
SetTooltipClient(root_window(), controller_.get());
#endif
......@@ -532,6 +532,10 @@ class TestTooltip : public Tooltip {
const base::string16& tooltip_text() const { return tooltip_text_; }
// Tooltip:
int GetMaxWidth(const gfx::Point& location,
aura::Window* context) const override {
return 100;
}
void SetText(aura::Window* window,
const base::string16& tooltip_text,
const gfx::Point& location) override {
......
......@@ -95,6 +95,16 @@ void TooltipWin::PositionTooltip() {
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,
const base::string16& tooltip_text,
const gfx::Point& location) {
......@@ -116,12 +126,7 @@ void TooltipWin::SetText(aura::Window* window,
SendMessage(tooltip_hwnd_, TTM_SETTOOLINFO, 0,
reinterpret_cast<LPARAM>(&toolinfo_));
// 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();
int max_width = (monitor_bounds.width() + 1) / 2;
int max_width = GetMaxWidth(location_, window);
SendMessage(tooltip_hwnd_, TTM_SETMAXTIPWIDTH, 0, max_width);
}
......
......@@ -22,7 +22,7 @@ namespace corewm {
class VIEWS_EXPORT TooltipWin : public Tooltip {
public:
explicit TooltipWin(HWND parent);
virtual ~TooltipWin();
~TooltipWin() override;
// HandleNotify() is forwarded from DesktopWindowTreeHostWin to keep the
// native tooltip in sync.
......@@ -37,12 +37,14 @@ class VIEWS_EXPORT TooltipWin : public Tooltip {
void PositionTooltip();
// Tooltip:
virtual void SetText(aura::Window* window,
const base::string16& tooltip_text,
const gfx::Point& location) override;
virtual void Show() override;
virtual void Hide() override;
virtual bool IsVisible() override;
int GetMaxWidth(const gfx::Point& location,
aura::Window* context) const override;
void SetText(aura::Window* window,
const base::string16& tooltip_text,
const gfx::Point& location) override;
void Show() override;
void Hide() override;
bool IsVisible() override;
// The window |tooltip_hwnd_| is parented to.
HWND parent_hwnd_;
......
......@@ -289,7 +289,7 @@ void DesktopWindowTreeHostX11::OnNativeWidgetCreated(
}
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>
......
......@@ -4,33 +4,9 @@
#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 {
const size_t kMaxTooltipLength = 1024;
// static
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
......@@ -15,6 +15,7 @@
namespace gfx {
class Display;
class FontList;
class Point;
} // namespace gfx
namespace views {
......@@ -38,17 +39,11 @@ class VIEWS_EXPORT TooltipManager {
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
// used to determine which gfx::Screen should be used.
static int GetMaxWidth(int x, int y, gfx::NativeView context);
// 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);
virtual int GetMaxWidth(const gfx::Point& location,
gfx::NativeView context) const = 0;
// Returns the font list used for tooltips.
virtual const gfx::FontList& GetFontList() const = 0;
......
......@@ -81,6 +81,12 @@ const gfx::FontList& TooltipManagerAura::GetFontList() const {
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() {
aura::Window* root_window = GetWindow()->GetRootWindow();
if (aura::client::GetTooltipClient(root_window)) {
......
......@@ -38,6 +38,8 @@ class TooltipManagerAura : public TooltipManager {
static const gfx::FontList& GetDefaultFontList();
// TooltipManager:
int GetMaxWidth(const gfx::Point& location,
aura::Window* context) const override;
const gfx::FontList& GetFontList() const override;
void UpdateTooltip() override;
void TooltipTextChanged(View* view) override;
......
......@@ -8,6 +8,10 @@
#include "ui/aura/aura_export.h"
#include "ui/gfx/font.h"
namespace gfx {
class Point;
}
namespace aura {
class Window;
namespace client {
......@@ -16,6 +20,10 @@ class ScopedTooltipDisabler;
class AURA_EXPORT TooltipClient {
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|.
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