diff options
author | tfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-11-16 17:26:28 +0000 |
---|---|---|
committer | tfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-11-16 17:26:28 +0000 |
commit | a5a3689b36825d4b24e92f90d338b8eac43a5435 (patch) | |
tree | 6ae20243e769312917c4e04ae6ec94740dd4f698 /views/touchui | |
parent | 96f2010e7418681d7dc8ac8256ab7eee44dba826 (diff) | |
download | chromium_src-a5a3689b36825d4b24e92f90d338b8eac43a5435.zip chromium_src-a5a3689b36825d4b24e92f90d338b8eac43a5435.tar.gz chromium_src-a5a3689b36825d4b24e92f90d338b8eac43a5435.tar.bz2 |
views: Move desktop and touchui directories to ui/views/.
BUG=104039
R=ben@chromium.org
Review URL: http://codereview.chromium.org/8568022
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@110316 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'views/touchui')
-rw-r--r-- | views/touchui/OWNERS | 1 | ||||
-rw-r--r-- | views/touchui/gesture_manager.cc | 69 | ||||
-rw-r--r-- | views/touchui/gesture_manager.h | 57 | ||||
-rw-r--r-- | views/touchui/touch_selection_controller.cc | 16 | ||||
-rw-r--r-- | views/touchui/touch_selection_controller.h | 56 | ||||
-rw-r--r-- | views/touchui/touch_selection_controller_impl.cc | 508 | ||||
-rw-r--r-- | views/touchui/touch_selection_controller_impl.h | 85 | ||||
-rw-r--r-- | views/touchui/touch_selection_controller_impl_unittest.cc | 341 |
8 files changed, 0 insertions, 1133 deletions
diff --git a/views/touchui/OWNERS b/views/touchui/OWNERS deleted file mode 100644 index b8e32c1..0000000 --- a/views/touchui/OWNERS +++ /dev/null @@ -1 +0,0 @@ -sadrul@chromium.org diff --git a/views/touchui/gesture_manager.cc b/views/touchui/gesture_manager.cc deleted file mode 100644 index 0ffc1e5..0000000 --- a/views/touchui/gesture_manager.cc +++ /dev/null @@ -1,69 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "views/touchui/gesture_manager.h" -#ifndef NDEBUG -#include <ostream> -#endif - -#include "base/logging.h" -#include "views/events/event.h" -#include "views/view.h" -#include "views/views_delegate.h" -#include "views/widget/widget.h" - -namespace views { - -GestureManager::~GestureManager() { -} - -GestureManager* GestureManager::GetInstance() { - return Singleton<GestureManager>::get(); -} - -bool GestureManager::ProcessTouchEventForGesture(const TouchEvent& event, - View* source, - ui::TouchStatus status) { - if (status != ui::TOUCH_STATUS_UNKNOWN) - return false; // The event was consumed by a touch sequence. - - // TODO(rjkroege): A realistic version of the GestureManager will - // appear in a subsequent CL. This interim version permits verifying that the - // event distribution code works by turning all touch inputs into - // mouse approximations. - - // Conver the touch-event into a mouse-event. This mouse-event gets its - // location information from the native-event, so it needs to convert the - // coordinate to the target widget. - MouseEvent mouseev(event); - if (ViewsDelegate::views_delegate->GetDefaultParentView()) { - // TODO(oshima): We may need to send the event back through - // window manager to handle mouse capture correctly. - Widget* desktop = - ViewsDelegate::views_delegate->GetDefaultParentView()->GetWidget(); - Widget* source_widget = source->GetWidget(); - MouseEvent converted( - mouseev, desktop->GetRootView(), source_widget->GetRootView()); - source_widget->OnMouseEvent(converted); - } else { - Widget* source_widget = source->GetWidget(); - Widget* top_widget = source_widget->GetTopLevelWidget(); - if (source_widget != top_widget && top_widget) { - // This is necessary as TYPE_CHILD widget is still NativeWidgetGtk. - // Fix this once TYPE_CHILD is switched to NativeWidgetViews. - MouseEvent converted(mouseev, - top_widget->GetRootView(), - source_widget->GetRootView()); - source_widget->OnMouseEvent(mouseev); - } else { - source_widget->OnMouseEvent(mouseev); - } - } - return true; -} - -GestureManager::GestureManager() { -} - -} // namespace views diff --git a/views/touchui/gesture_manager.h b/views/touchui/gesture_manager.h deleted file mode 100644 index 12fee961b7..0000000 --- a/views/touchui/gesture_manager.h +++ /dev/null @@ -1,57 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef VIEWS_TOUCHUI_GESTURE_MANAGER_H_ -#define VIEWS_TOUCHUI_GESTURE_MANAGER_H_ -#pragma once - -#include "base/memory/singleton.h" -#include "views/view.h" - -namespace ui { -enum TouchStatus; -} - -namespace views { -class TouchEvent; - -// A GestureManager singleton detects gestures occurring in the -// incoming feed of touch events across all of the RootViews in -// the system. In response to a given touch event, the GestureManager -// updates its internal state and optionally dispatches synthetic -// events to the invoking view. -// -class VIEWS_EXPORT GestureManager { - public: - virtual ~GestureManager(); - - static GestureManager* GetInstance(); - - // Invoked for each touch event that could contribute to the current gesture. - // Takes the event and the View that originated it and which will also - // be the target of any generated synthetic event. Finally, status - // specifies if a touch sequence is in progress or not, so that the - // GestureManager state can correctly reflect events that are handled - // already. - // Returns true if the event resulted in firing a synthetic event. - virtual bool ProcessTouchEventForGesture(const TouchEvent& event, - View* source, - ui::TouchStatus status); - - // TODO(rjkroege): Write the remainder of this class. - // It will appear in a subsequent CL. - - protected: - GestureManager(); - - private: - friend struct DefaultSingletonTraits<GestureManager>; - - DISALLOW_COPY_AND_ASSIGN(GestureManager); -}; - - -} // namespace views - -#endif // VIEWS_TOUCHUI_GESTURE_MANAGER_H_ diff --git a/views/touchui/touch_selection_controller.cc b/views/touchui/touch_selection_controller.cc deleted file mode 100644 index 6d0a69a..0000000 --- a/views/touchui/touch_selection_controller.cc +++ /dev/null @@ -1,16 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "views/touchui/touch_selection_controller.h" - -namespace views { - -#if !defined(TOUCH_UI) -TouchSelectionController* TouchSelectionController::create( - TouchSelectionClientView* client_view) { - return NULL; -} -#endif - -} // namespace views. diff --git a/views/touchui/touch_selection_controller.h b/views/touchui/touch_selection_controller.h deleted file mode 100644 index 58f6f91..0000000 --- a/views/touchui/touch_selection_controller.h +++ /dev/null @@ -1,56 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef VIEWS_TOUCHUI_TOUCH_SELECTION_CONTROLLER_H_ -#define VIEWS_TOUCHUI_TOUCH_SELECTION_CONTROLLER_H_ -#pragma once - -#include "ui/base/models/simple_menu_model.h" -#include "ui/gfx/point.h" -#include "views/view.h" - -namespace views { - -// An interface implemented by a View that has text that can be selected. -class VIEWS_EXPORT TouchSelectionClientView - : public View, - public ui::SimpleMenuModel::Delegate { - public: - // Select everything between start and end (points are in view's local - // coordinate system). |start| is the logical start and |end| is the logical - // end of selection. Visually, |start| may lie after |end|. - virtual void SelectRect(const gfx::Point& start, const gfx::Point& end) = 0; - - protected: - virtual ~TouchSelectionClientView() {} -}; - -// This defines the callback interface for other code to be notified of changes -// in the state of a TouchSelectionClientView. -class VIEWS_EXPORT TouchSelectionController { - public: - virtual ~TouchSelectionController() {} - - // Creates a TouchSelectionController. Caller owns the returned object. - static TouchSelectionController* create( - TouchSelectionClientView* client_view); - - // Notification that the text selection in TouchSelectionClientView has - // changed. p1 and p2 are lower corners of the start and end of selection: - // ____________________________________ - // | textfield with |selected text| | - // ------------------------------------ - // ^p1 ^p2 - // - // p1 is always the start and p2 is always the end of selection. Hence, - // p1 could be to the right of p2 in the figure above. - virtual void SelectionChanged(const gfx::Point& p1, const gfx::Point& p2) = 0; - - // Notification that the TouchSelectionClientView has lost focus. - virtual void ClientViewLostFocus() = 0; -}; - -} // namespace views - -#endif // VIEWS_TOUCHUI_TOUCH_SELECTION_CONTROLLER_H_ diff --git a/views/touchui/touch_selection_controller_impl.cc b/views/touchui/touch_selection_controller_impl.cc deleted file mode 100644 index e590216..0000000 --- a/views/touchui/touch_selection_controller_impl.cc +++ /dev/null @@ -1,508 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "views/touchui/touch_selection_controller_impl.h" - -#include "base/time.h" -#include "base/utf_string_conversions.h" -#include "grit/ui_strings.h" -#include "third_party/skia/include/effects/SkGradientShader.h" -#include "ui/base/l10n/l10n_util.h" -#include "ui/base/resource/resource_bundle.h" -#include "ui/gfx/canvas.h" -#include "ui/gfx/canvas_skia.h" -#include "ui/gfx/path.h" -#include "ui/gfx/rect.h" -#include "ui/gfx/screen.h" -#include "ui/gfx/size.h" -#include "ui/gfx/transform.h" -#include "views/background.h" -#include "views/controls/button/button.h" -#include "views/controls/button/custom_button.h" -#include "views/controls/button/text_button.h" -#include "views/controls/label.h" -#include "views/controls/menu/menu_config.h" -#include "views/layout/box_layout.h" -#include "views/widget/widget.h" - -namespace { - -// Constants defining the visual attributes of selection handles -const int kSelectionHandleRadius = 10; -const int kSelectionHandleCursorHeight = 10; -const int kSelectionHandleAlpha = 0x7F; -const SkColor kSelectionHandleColor = - SkColorSetA(SK_ColorBLUE, kSelectionHandleAlpha); - -// The minimum selection size to trigger selection controller. -const int kMinSelectionSize = 4; - -const int kContextMenuCommands[] = {IDS_APP_CUT, - IDS_APP_COPY, -// TODO(varunjain): PASTE is acting funny due to some gtk clipboard issue. -// Uncomment the following when that is fixed. -// IDS_APP_PASTE, - IDS_APP_DELETE, - IDS_APP_SELECT_ALL}; -const int kContextMenuPadding = 2; -const int kContextMenuTimoutMs = 1000; -const int kContextMenuVerticalOffset = 25; - -// Convenience struct to represent a circle shape. -struct Circle { - int radius; - gfx::Point center; - SkColor color; -}; - -// Creates a widget to host SelectionHandleView. -views::Widget* CreateTouchSelectionPopupWidget() { - views::Widget* widget = new views::Widget; - views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP); - params.can_activate = false; - params.transparent = true; - params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; - widget->Init(params); - return widget; -} - -void PaintCircle(const Circle& circle, gfx::Canvas* canvas) { - SkPaint paint; - paint.setAntiAlias(true); - paint.setStyle(SkPaint::kFill_Style); - paint.setColor(circle.color); - gfx::Path path; - gfx::Rect bounds(circle.center.x() - circle.radius, - circle.center.y() - circle.radius, - circle.radius * 2, - circle.radius * 2); - SkRect rect; - rect.set(SkIntToScalar(bounds.x()), SkIntToScalar(bounds.y()), - SkIntToScalar(bounds.right()), SkIntToScalar(bounds.bottom())); - SkScalar radius = SkIntToScalar(circle.radius); - path.addRoundRect(rect, radius, radius); - canvas->GetSkCanvas()->drawPath(path, paint); -} - -// The points may not match exactly, since the selection range computation may -// introduce some floating point errors. So check for a minimum size to decide -// whether or not there is any selection. -bool IsEmptySelection(const gfx::Point& p1, const gfx::Point& p2) { - int delta_x = p2.x() - p1.x(); - int delta_y = p2.y() - p1.y(); - return (abs(delta_x) < kMinSelectionSize && abs(delta_y) < kMinSelectionSize); -} - -} // namespace - -namespace views { - -// A View that displays the text selection handle. -class TouchSelectionControllerImpl::SelectionHandleView : public View { - public: - SelectionHandleView(TouchSelectionControllerImpl* controller) - : controller_(controller) { - widget_.reset(CreateTouchSelectionPopupWidget()); - widget_->SetContentsView(this); - widget_->SetAlwaysOnTop(true); - - // We are owned by the TouchSelectionController. - set_parent_owned(false); - } - - virtual ~SelectionHandleView() { - } - - virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE { - Circle circle = {kSelectionHandleRadius, gfx::Point(kSelectionHandleRadius, - kSelectionHandleRadius + kSelectionHandleCursorHeight), - kSelectionHandleColor}; - PaintCircle(circle, canvas); - canvas->DrawLineInt(kSelectionHandleColor, kSelectionHandleRadius, 0, - kSelectionHandleRadius, kSelectionHandleCursorHeight); - } - - virtual bool OnMousePressed(const MouseEvent& event) OVERRIDE { - controller_->dragging_handle_ = this; - return true; - } - - virtual bool OnMouseDragged(const MouseEvent& event) OVERRIDE { - controller_->SelectionHandleDragged(event.location()); - return true; - } - - virtual void OnMouseReleased(const MouseEvent& event) OVERRIDE { - controller_->dragging_handle_ = NULL; - } - - virtual void OnMouseCaptureLost() OVERRIDE { - controller_->dragging_handle_ = NULL; - } - - virtual void SetVisible(bool visible) OVERRIDE { - // We simply show/hide the container widget. - if (visible != widget_->IsVisible()) { - if (visible) - widget_->Show(); - else - widget_->Hide(); - } - View::SetVisible(visible); - } - - virtual gfx::Size GetPreferredSize() OVERRIDE { - return gfx::Size(2 * kSelectionHandleRadius, - 2 * kSelectionHandleRadius + kSelectionHandleCursorHeight); - } - - void SetScreenPosition(const gfx::Point& position) { - gfx::Rect widget_bounds(position.x() - kSelectionHandleRadius, position.y(), - 2 * kSelectionHandleRadius, - 2 * kSelectionHandleRadius + kSelectionHandleCursorHeight); - widget_->SetBounds(widget_bounds); - } - - gfx::Point GetScreenPosition() { - return widget_->GetClientAreaScreenBounds().origin(); - } - - private: - scoped_ptr<Widget> widget_; - TouchSelectionControllerImpl* controller_; - - DISALLOW_COPY_AND_ASSIGN(SelectionHandleView); -}; - -class ContextMenuButtonBackground : public Background { - public: - ContextMenuButtonBackground() {} - - virtual void Paint(gfx::Canvas* canvas, View* view) const OVERRIDE { - CustomButton::ButtonState state = static_cast<CustomButton*>(view)->state(); - SkColor background_color, border_color; - if (state == CustomButton::BS_NORMAL) { - background_color = SkColorSetARGB(102, 255, 255, 255); - border_color = SkColorSetARGB(36, 0, 0, 0); - } else { - background_color = SkColorSetARGB(13, 0, 0, 0); - border_color = SkColorSetARGB(72, 0, 0, 0); - } - int w = view->width(); - int h = view->height(); - canvas->FillRect(background_color, gfx::Rect(1, 1, w - 2, h - 2)); - canvas->FillRect(border_color, gfx::Rect(2, 0, w - 4, 1)); - canvas->FillRect(border_color, gfx::Rect(1, 1, 1, 1)); - canvas->FillRect(border_color, gfx::Rect(0, 2, 1, h - 4)); - canvas->FillRect(border_color, gfx::Rect(1, h - 2, 1, 1)); - canvas->FillRect(border_color, gfx::Rect(2, h - 1, w - 4, 1)); - canvas->FillRect(border_color, gfx::Rect(w - 2, 1, 1, 1)); - canvas->FillRect(border_color, gfx::Rect(w - 1, 2, 1, h - 4)); - canvas->FillRect(border_color, gfx::Rect(w - 2, h - 2, 1, 1)); - } - - private: - DISALLOW_COPY_AND_ASSIGN(ContextMenuButtonBackground); -}; - -// A View that displays the touch context menu. -class TouchSelectionControllerImpl::TouchContextMenuView - : public ButtonListener, - public View { - public: - TouchContextMenuView(TouchSelectionControllerImpl* controller) - : controller_(controller) { - widget_.reset(CreateTouchSelectionPopupWidget()); - widget_->SetContentsView(this); - widget_->SetAlwaysOnTop(true); - - // We are owned by the TouchSelectionController. - set_parent_owned(false); - SetLayoutManager(new BoxLayout(BoxLayout::kHorizontal, kContextMenuPadding, - kContextMenuPadding, kContextMenuPadding)); - } - - virtual ~TouchContextMenuView() { - } - - virtual void SetVisible(bool visible) OVERRIDE { - // We simply show/hide the container widget. - if (visible != widget_->IsVisible()) { - if (visible) - widget_->Show(); - else - widget_->Hide(); - } - View::SetVisible(visible); - } - - void SetScreenPosition(const gfx::Point& position) { - RefreshButtonsAndSetWidgetPosition(position); - } - - gfx::Point GetScreenPosition() { - return widget_->GetClientAreaScreenBounds().origin(); - } - - void OnPaintBackground(gfx::Canvas* canvas) OVERRIDE { - // TODO(varunjain): the following color scheme is copied from - // menu_scroll_view_container.cc. Figure out how to consolidate the two - // pieces of code. -#if defined(OS_CHROMEOS) - static const SkColor kGradientColors[2] = { - SK_ColorWHITE, - SkColorSetRGB(0xF0, 0xF0, 0xF0) - }; - - static const SkScalar kGradientPoints[2] = { - SkIntToScalar(0), - SkIntToScalar(1) - }; - - SkPoint points[2]; - points[0].set(SkIntToScalar(0), SkIntToScalar(0)); - points[1].set(SkIntToScalar(0), SkIntToScalar(height())); - - SkShader* shader = SkGradientShader::CreateLinear(points, - kGradientColors, kGradientPoints, arraysize(kGradientPoints), - SkShader::kRepeat_TileMode); - DCHECK(shader); - - SkPaint paint; - paint.setShader(shader); - shader->unref(); - - paint.setStyle(SkPaint::kFill_Style); - paint.setXfermodeMode(SkXfermode::kSrc_Mode); - - canvas->DrawRectInt(0, 0, width(), height(), paint); -#else - // This is the same as COLOR_TOOLBAR. - canvas->GetSkCanvas()->drawColor(SkColorSetRGB(210, 225, 246), - SkXfermode::kSrc_Mode); -#endif - } - - // ButtonListener - virtual void ButtonPressed(Button* sender, const views::Event& event) { - controller_->ExecuteCommand(sender->tag()); - } - - private: - // Queries the client view for what elements to show in the menu and sizes - // the menu appropriately. - void RefreshButtonsAndSetWidgetPosition(const gfx::Point& position) { - RemoveAllChildViews(true); - int total_width = 0; - int height = 0; - for (size_t i = 0; i < arraysize(kContextMenuCommands); i++) { - int command_id = kContextMenuCommands[i]; - if (controller_->IsCommandIdEnabled(command_id)) { - TextButton* button = new TextButton( - this, l10n_util::GetStringUTF16(command_id)); - button->set_focusable(true); - button->set_request_focus_on_press(false); - button->set_prefix_type(TextButton::PREFIX_HIDE); - button->SetEnabledColor(MenuConfig::instance().text_color); - button->set_background(new ContextMenuButtonBackground()); - button->set_alignment(TextButton::ALIGN_CENTER); - button->SetFont(ui::ResourceBundle::GetSharedInstance().GetFont( - ui::ResourceBundle::LargeFont)); - button->set_tag(command_id); - AddChildView(button); - gfx::Size button_size = button->GetPreferredSize(); - total_width += button_size.width() + kContextMenuPadding; - if (height < button_size.height()) - height = button_size.height(); - } - } - gfx::Rect widget_bounds(position.x() - total_width / 2, - position.y() - height, - total_width, - height); - gfx::Rect monitor_bounds = - gfx::Screen::GetMonitorAreaNearestPoint(position); - widget_->SetBounds(widget_bounds.AdjustToFit(monitor_bounds)); - Layout(); - } - - scoped_ptr<Widget> widget_; - TouchSelectionControllerImpl* controller_; - - DISALLOW_COPY_AND_ASSIGN(TouchContextMenuView); -}; - -TouchSelectionControllerImpl::TouchSelectionControllerImpl( - TouchSelectionClientView* client_view) - : client_view_(client_view), - selection_handle_1_(new SelectionHandleView(this)), - selection_handle_2_(new SelectionHandleView(this)), - context_menu_(new TouchContextMenuView(this)), - dragging_handle_(NULL) { -} - -TouchSelectionControllerImpl::~TouchSelectionControllerImpl() { -} - -void TouchSelectionControllerImpl::SelectionChanged(const gfx::Point& p1, - const gfx::Point& p2) { - gfx::Point screen_pos_1(p1); - View::ConvertPointToScreen(client_view_, &screen_pos_1); - gfx::Point screen_pos_2(p2); - View::ConvertPointToScreen(client_view_, &screen_pos_2); - - if (dragging_handle_) { - // We need to reposition only the selection handle that is being dragged. - // The other handle stays the same. Also, the selection handle being dragged - // will always be at the end of selection, while the other handle will be at - // the start. - dragging_handle_->SetScreenPosition(screen_pos_2); - } else { - UpdateContextMenu(p1, p2); - - // Check if there is any selection at all. - if (IsEmptySelection(screen_pos_2, screen_pos_1)) { - selection_handle_1_->SetVisible(false); - selection_handle_2_->SetVisible(false); - return; - } - - if (client_view_->bounds().Contains(p1)) { - selection_handle_1_->SetScreenPosition(screen_pos_1); - selection_handle_1_->SetVisible(true); - } else { - selection_handle_1_->SetVisible(false); - } - - if (client_view_->bounds().Contains(p2)) { - selection_handle_2_->SetScreenPosition(screen_pos_2); - selection_handle_2_->SetVisible(true); - } else { - selection_handle_2_->SetVisible(false); - } - } -} - -void TouchSelectionControllerImpl::ClientViewLostFocus() { - selection_handle_1_->SetVisible(false); - selection_handle_2_->SetVisible(false); - HideContextMenu(); -} - -void TouchSelectionControllerImpl::SelectionHandleDragged( - const gfx::Point& drag_pos) { - // We do not want to show the context menu while dragging. - HideContextMenu(); - context_menu_timer_.Start( - FROM_HERE, - base::TimeDelta::FromMilliseconds(kContextMenuTimoutMs), - this, - &TouchSelectionControllerImpl::ContextMenuTimerFired); - - if (client_view_->GetWidget()) { - DCHECK(dragging_handle_); - // Find the stationary selection handle. - SelectionHandleView* fixed_handle = selection_handle_1_.get(); - if (fixed_handle == dragging_handle_) - fixed_handle = selection_handle_2_.get(); - - // Find selection end points in client_view's coordinate system. - gfx::Point p1(drag_pos.x() + kSelectionHandleRadius, drag_pos.y()); - ConvertPointToClientView(dragging_handle_, &p1); - - gfx::Point p2(kSelectionHandleRadius, 0); - ConvertPointToClientView(fixed_handle, &p2); - - // Instruct client_view to select the region between p1 and p2. The position - // of |fixed_handle| is the start and that of |dragging_handle| is the end - // of selection. - client_view_->SelectRect(p2, p1); - } -} - -void TouchSelectionControllerImpl::ConvertPointToClientView( - SelectionHandleView* source, gfx::Point* point) { - View::ConvertPointToScreen(source, point); - gfx::Rect r = client_view_->GetWidget()->GetClientAreaScreenBounds(); - point->SetPoint(point->x() - r.x(), point->y() - r.y()); - View::ConvertPointFromWidget(client_view_, point); -} - -bool TouchSelectionControllerImpl::IsCommandIdEnabled(int command_id) const { - return client_view_->IsCommandIdEnabled(command_id); -} - -void TouchSelectionControllerImpl::ExecuteCommand(int command_id) { - HideContextMenu(); - client_view_->ExecuteCommand(command_id); -} - -void TouchSelectionControllerImpl::ContextMenuTimerFired() { - // Get selection end points in client_view's space. - gfx::Point p1(kSelectionHandleRadius, 0); - ConvertPointToClientView(selection_handle_1_.get(), &p1); - gfx::Point p2(kSelectionHandleRadius, 0); - ConvertPointToClientView(selection_handle_2_.get(), &p2); - - // if selection is completely inside the view, we display the context menu - // in the middle of the end points on the top. Else, we show the menu on the - // top border of the view in the center. - gfx::Point menu_pos; - if (client_view_->bounds().Contains(p1) && - client_view_->bounds().Contains(p2)) { - menu_pos.set_x((p1.x() + p2.x()) / 2); - menu_pos.set_y(std::min(p1.y(), p2.y()) - kContextMenuVerticalOffset); - } else { - menu_pos.set_x(client_view_->x() + client_view_->width() / 2); - menu_pos.set_y(client_view_->y()); - } - - View::ConvertPointToScreen(client_view_, &menu_pos); - - context_menu_->SetScreenPosition(menu_pos); - context_menu_->SetVisible(true); -} - -void TouchSelectionControllerImpl::UpdateContextMenu(const gfx::Point& p1, - const gfx::Point& p2) { - // Hide context menu to be shown when the timer fires. - HideContextMenu(); - - // If there is selection, we restart the context menu timer. - if (!IsEmptySelection(p1, p2)) { - context_menu_timer_.Start( - FROM_HERE, - base::TimeDelta::FromMilliseconds(kContextMenuTimoutMs), - this, - &TouchSelectionControllerImpl::ContextMenuTimerFired); - } -} - -void TouchSelectionControllerImpl::HideContextMenu() { - context_menu_->SetVisible(false); - context_menu_timer_.Stop(); -} - -gfx::Point TouchSelectionControllerImpl::GetSelectionHandle1Position() { - return selection_handle_1_->GetScreenPosition(); -} - -gfx::Point TouchSelectionControllerImpl::GetSelectionHandle2Position() { - return selection_handle_2_->GetScreenPosition(); -} - -bool TouchSelectionControllerImpl::IsSelectionHandle1Visible() { - return selection_handle_1_->IsVisible(); -} - -bool TouchSelectionControllerImpl::IsSelectionHandle2Visible() { - return selection_handle_2_->IsVisible(); -} - -TouchSelectionController* TouchSelectionController::create( - TouchSelectionClientView* client_view) { - return new TouchSelectionControllerImpl(client_view); -} - -} // namespace views diff --git a/views/touchui/touch_selection_controller_impl.h b/views/touchui/touch_selection_controller_impl.h deleted file mode 100644 index 480485d..0000000 --- a/views/touchui/touch_selection_controller_impl.h +++ /dev/null @@ -1,85 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef VIEWS_TOUCHUI_TOUCH_SELECTION_CONTROLLER_IMPL_H_ -#define VIEWS_TOUCHUI_TOUCH_SELECTION_CONTROLLER_IMPL_H_ -#pragma once - -#include "base/timer.h" -#include "ui/gfx/point.h" -#include "views/touchui/touch_selection_controller.h" -#include "views/view.h" -#include "views/views_export.h" - -namespace views { - -// Touch specific implementation of TouchSelectionController. Responsible for -// displaying selection handles and menu elements relevant in a touch interface. -class VIEWS_EXPORT TouchSelectionControllerImpl - : public TouchSelectionController { - public: - // Use TextSelectionController::create(). - explicit TouchSelectionControllerImpl(TouchSelectionClientView* client_view); - - virtual ~TouchSelectionControllerImpl(); - - // TextSelectionController. - virtual void SelectionChanged(const gfx::Point& p1, - const gfx::Point& p2) OVERRIDE; - - virtual void ClientViewLostFocus() OVERRIDE; - - private: - friend class TouchSelectionControllerImplTest; - class SelectionHandleView; - class TouchContextMenuView; - - // Callback to inform the client view that the selection handle has been - // dragged, hence selection may need to be updated. - void SelectionHandleDragged(const gfx::Point& drag_pos); - - // Convenience method to convert a point from a selection handle's coordinate - // system to that of the client view. - void ConvertPointToClientView(SelectionHandleView* source, gfx::Point* point); - - // Checks if the client view supports a context menu command. - bool IsCommandIdEnabled(int command_id) const; - - // Sends a context menu command to the client view. - void ExecuteCommand(int command_id); - - // Time to show context menu. - void ContextMenuTimerFired(); - - // Convenience method to update the position/visibility of the context menu. - void UpdateContextMenu(const gfx::Point& p1, const gfx::Point& p2); - - // Convenience method for hiding context menu. - void HideContextMenu(); - - // Convenience methods for testing. - gfx::Point GetSelectionHandle1Position(); - gfx::Point GetSelectionHandle2Position(); - bool IsSelectionHandle1Visible(); - bool IsSelectionHandle2Visible(); - - TouchSelectionClientView* client_view_; - scoped_ptr<SelectionHandleView> selection_handle_1_; - scoped_ptr<SelectionHandleView> selection_handle_2_; - scoped_ptr<TouchContextMenuView> context_menu_; - - // Timer to trigger |context_menu| (|context_menu| is not shown if the - // selection handles are being updated. It appears only when the handles are - // stationary for a certain amount of time). - base::OneShotTimer<TouchSelectionControllerImpl> context_menu_timer_; - - // Pointer to the SelectionHandleView being dragged during a drag session. - SelectionHandleView* dragging_handle_; - - DISALLOW_COPY_AND_ASSIGN(TouchSelectionControllerImpl); -}; - -} // namespace views - -#endif // VIEWS_TOUCHUI_TOUCH_SELECTION_CONTROLLER_IMPL_H_ diff --git a/views/touchui/touch_selection_controller_impl_unittest.cc b/views/touchui/touch_selection_controller_impl_unittest.cc deleted file mode 100644 index 9741c26..0000000 --- a/views/touchui/touch_selection_controller_impl_unittest.cc +++ /dev/null @@ -1,341 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "base/utf_string_conversions.h" -#include "ui/gfx/point.h" -#include "ui/gfx/rect.h" -#include "ui/gfx/render_text.h" -#include "views/controls/textfield/native_textfield_views.h" -#include "views/controls/textfield/textfield.h" -#include "views/test/views_test_base.h" -#include "views/touchui/touch_selection_controller.h" -#include "views/touchui/touch_selection_controller_impl.h" -#include "views/widget/widget.h" - -namespace views { - -class TouchSelectionControllerImplTest : public ViewsTestBase { - public: - TouchSelectionControllerImplTest() - : widget_(NULL), - textfield_(NULL), - textfield_view_(NULL) { - } - - virtual void TearDown() { - if (widget_) - widget_->Close(); - ViewsTestBase::TearDown(); - } - - void CreateTextfield() { - textfield_ = new Textfield(); - widget_ = new Widget; - Widget::InitParams params(Widget::InitParams::TYPE_POPUP); - params.bounds = gfx::Rect(0, 0, 200, 200); - widget_->Init(params); - View* container = new View(); - widget_->SetContentsView(container); - container->AddChildView(textfield_); - - textfield_view_ = static_cast<NativeTextfieldViews*>( - textfield_->GetNativeWrapperForTesting()); - textfield_view_->SetBoundsRect(params.bounds); - textfield_->set_id(1); - - DCHECK(textfield_view_); - textfield_->RequestFocus(); - } - - protected: - gfx::Point GetCursorPosition(const gfx::SelectionModel& sel) { - gfx::RenderText* render_text = textfield_view_->GetRenderText(); - gfx::Rect cursor_bounds = render_text->GetCursorBounds(sel, true); - return gfx::Point(cursor_bounds.x(), cursor_bounds.bottom() - 1); - } - - TouchSelectionControllerImpl* GetSelectionController() { - return static_cast<TouchSelectionControllerImpl*>( - textfield_view_->touch_selection_controller_.get()); - } - - void SimulateSelectionHandleDrag(gfx::Point p, int selection_handle) { - TouchSelectionControllerImpl* controller = GetSelectionController(); - // Do the work of OnMousePressed(). - if (selection_handle == 1) - controller->dragging_handle_ = controller->selection_handle_1_.get(); - else - controller->dragging_handle_ = controller->selection_handle_2_.get(); - - controller->SelectionHandleDragged(p); - - // Do the work of OnMouseReleased(). - controller->dragging_handle_ = NULL; - } - - // If textfield has selection, this method verifies that the selection handles - // are visible and at the correct positions (at the end points of selection). - // |cursor_at_selection_handle_1| is used to decide whether selection - // handle 1's position is matched against the start of selection or the end. - void VerifySelectionHandlePositions(bool cursor_at_selection_handle_1) { - if (textfield_->HasSelection()) { - EXPECT_TRUE(GetSelectionController()->IsSelectionHandle1Visible()); - EXPECT_TRUE(GetSelectionController()->IsSelectionHandle2Visible()); - gfx::SelectionModel sel; - textfield_view_->GetSelectionModel(&sel); - gfx::SelectionModel sel_start = textfield_view_->GetRenderText()-> - GetSelectionModelForSelectionStart(); - gfx::Point selection_start = GetCursorPosition(sel_start); - gfx::Point selection_end = GetCursorPosition(sel); - gfx::Point sh1 = GetSelectionController()->GetSelectionHandle1Position(); - gfx::Point sh2 = GetSelectionController()->GetSelectionHandle2Position(); - sh1.Offset(10, 0); // offset by kSelectionHandleRadius. - sh2.Offset(10, 0); - - if (cursor_at_selection_handle_1) { - EXPECT_EQ(sh1, selection_end); - EXPECT_EQ(sh2, selection_start); - } else { - EXPECT_EQ(sh1, selection_start); - EXPECT_EQ(sh2, selection_end); - } - } else { - EXPECT_FALSE(GetSelectionController()->IsSelectionHandle1Visible()); - EXPECT_FALSE(GetSelectionController()->IsSelectionHandle2Visible()); - } - } - - Widget* widget_; - - Textfield* textfield_; - NativeTextfieldViews* textfield_view_; - - private: - DISALLOW_COPY_AND_ASSIGN(TouchSelectionControllerImplTest); -}; - -// Tests that the selection handles are placed appropriately when selection in -// a Textfield changes. -TEST_F(TouchSelectionControllerImplTest, SelectionInTextfieldTest) { - CreateTextfield(); - textfield_->SetText(ASCIIToUTF16("some text")); - - // Test selecting a range. - textfield_->SelectRange(ui::Range(3, 7)); - VerifySelectionHandlePositions(false); - - // Test selecting everything. - textfield_->SelectAll(); - VerifySelectionHandlePositions(false); - - // Test with no selection. - textfield_->ClearSelection(); - VerifySelectionHandlePositions(false); - - // Test with lost focus. - widget_->GetFocusManager()->ClearFocus(); - VerifySelectionHandlePositions(false); - - // Test with focus re-gained. - widget_->GetFocusManager()->SetFocusedView(textfield_); - VerifySelectionHandlePositions(false); -} - -// Tests that the selection handles are placed appropriately in bidi text. -TEST_F(TouchSelectionControllerImplTest, SelectionInBidiTextfieldTest) { - CreateTextfield(); - textfield_->SetText(WideToUTF16(L"abc\x05d0\x05d1\x05d2")); - - // Test cursor at run boundary and with empty selection. - textfield_->SelectSelectionModel( - gfx::SelectionModel(3, 2, gfx::SelectionModel::TRAILING)); - VerifySelectionHandlePositions(false); - - // Test selection range inside one run and starts or ends at run boundary. - textfield_->SelectRange(ui::Range(2, 3)); - VerifySelectionHandlePositions(false); - - textfield_->SelectRange(ui::Range(3, 2)); - VerifySelectionHandlePositions(false); - - textfield_->SelectRange(ui::Range(3, 4)); - VerifySelectionHandlePositions(false); - - textfield_->SelectRange(ui::Range(4, 3)); - VerifySelectionHandlePositions(false); - - textfield_->SelectRange(ui::Range(3, 6)); - VerifySelectionHandlePositions(false); - - textfield_->SelectRange(ui::Range(6, 3)); - VerifySelectionHandlePositions(false); - - // Test selection range accross runs. - textfield_->SelectRange(ui::Range(0, 6)); - VerifySelectionHandlePositions(false); - - textfield_->SelectRange(ui::Range(6, 0)); - VerifySelectionHandlePositions(false); - - textfield_->SelectRange(ui::Range(1, 4)); - VerifySelectionHandlePositions(false); - - textfield_->SelectRange(ui::Range(4, 1)); - VerifySelectionHandlePositions(false); -} - -// Tests if the SelectRect callback is called appropriately when selection -// handles are moved. -TEST_F(TouchSelectionControllerImplTest, SelectRectCallbackTest) { - CreateTextfield(); - textfield_->SetText(ASCIIToUTF16("textfield with selected text")); - textfield_->SelectRange(ui::Range(3, 7)); - - EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "tfie"); - VerifySelectionHandlePositions(false); - - // Drag selection handle 2 to right by 3 chars. - int x = textfield_->font().GetStringWidth(ASCIIToUTF16("ld ")); - SimulateSelectionHandleDrag(gfx::Point(x, 0), 2); - EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "tfield "); - VerifySelectionHandlePositions(false); - - // Drag selection handle 1 to the left by a large amount (selection should - // just stick to the beginning of the textfield). - SimulateSelectionHandleDrag(gfx::Point(-50, 0), 1); - EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "textfield "); - VerifySelectionHandlePositions(true); - - // Drag selection handle 1 across selection handle 2. - x = textfield_->font().GetStringWidth(ASCIIToUTF16("textfield with ")); - SimulateSelectionHandleDrag(gfx::Point(x, 0), 1); - EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "with "); - VerifySelectionHandlePositions(true); - - // Drag selection handle 2 across selection handle 1. - x = textfield_->font().GetStringWidth(ASCIIToUTF16("with selected ")); - SimulateSelectionHandleDrag(gfx::Point(x, 0), 2); - EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "selected "); - VerifySelectionHandlePositions(false); -} - -TEST_F(TouchSelectionControllerImplTest, SelectRectInBidiCallbackTest) { - CreateTextfield(); - textfield_->SetText(WideToUTF16(L"abc\x05e1\x05e2\x05e3"L"def")); - - // Select [c] from left to right. - textfield_->SelectRange(ui::Range(2, 3)); - EXPECT_EQ(WideToUTF16(L"c"), textfield_->GetSelectedText()); - VerifySelectionHandlePositions(false); - - // Drag selection handle 2 to right by 1 char. - int x = textfield_->font().GetStringWidth(WideToUTF16(L"\x05e3")); - SimulateSelectionHandleDrag(gfx::Point(x, 0), 2); - EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText()); - VerifySelectionHandlePositions(false); - - // Drag selection handle 1 to left by 1 char. - x = textfield_->font().GetStringWidth(WideToUTF16(L"b")); - SimulateSelectionHandleDrag(gfx::Point(-x, 0), 1); - EXPECT_EQ(WideToUTF16(L"bc\x05e1\x05e2"), textfield_->GetSelectedText()); - VerifySelectionHandlePositions(true); - - // Select [c] from right to left. - textfield_->SelectRange(ui::Range(3, 2)); - EXPECT_EQ(WideToUTF16(L"c"), textfield_->GetSelectedText()); - VerifySelectionHandlePositions(false); - - // Drag selection handle 1 to right by 1 char. - x = textfield_->font().GetStringWidth(WideToUTF16(L"\x05e3")); - SimulateSelectionHandleDrag(gfx::Point(x, 0), 1); - EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText()); - VerifySelectionHandlePositions(true); - - // Drag selection handle 2 to left by 1 char. - x = textfield_->font().GetStringWidth(WideToUTF16(L"b")); - SimulateSelectionHandleDrag(gfx::Point(-x, 0), 2); - EXPECT_EQ(WideToUTF16(L"bc\x05e1\x05e2"), textfield_->GetSelectedText()); - VerifySelectionHandlePositions(false); - - // Select [\x5e1] from right to left. - textfield_->SelectRange(ui::Range(3, 4)); - EXPECT_EQ(WideToUTF16(L"\x05e1"), textfield_->GetSelectedText()); - VerifySelectionHandlePositions(false); - - /* TODO(xji): for bidi text "abcDEF" whose display is "abcFEDhij", when click - right of 'D' and select [D] then move the left selection handle to left - by one character, it should select [ED], instead it selects [F]. - Reason: click right of 'D' and left of 'h' return the same x-axis position, - pass this position to FindCursorPosition() returns index of 'h'. which - means the selection start changed from 3 to 6. - Need further investigation on whether this is a bug in Pango and how to - work around it. - // Drag selection handle 2 to left by 1 char. - x = textfield_->font().GetStringWidth(WideToUTF16(L"\x05e2")); - SimulateSelectionHandleDrag(gfx::Point(-x, 0), 2); - EXPECT_EQ(WideToUTF16(L"\x05e1\x05e2"), textfield_->GetSelectedText()); - VerifySelectionHandlePositions(false); - */ - - // Drag selection handle 1 to right by 1 char. - x = textfield_->font().GetStringWidth(WideToUTF16(L"d")); - SimulateSelectionHandleDrag(gfx::Point(x, 0), 1); - EXPECT_EQ(WideToUTF16(L"\x05e2\x05e3"L"d"), textfield_->GetSelectedText()); - VerifySelectionHandlePositions(true); - - // Select [\x5e1] from left to right. - textfield_->SelectRange(ui::Range(4, 3)); - EXPECT_EQ(WideToUTF16(L"\x05e1"), textfield_->GetSelectedText()); - VerifySelectionHandlePositions(false); - - /* TODO(xji): see detail of above commented out test case. - // Drag selection handle 1 to left by 1 char. - x = textfield_->font().GetStringWidth(WideToUTF16(L"\x05e2")); - SimulateSelectionHandleDrag(gfx::Point(-x, 0), 1); - EXPECT_EQ(WideToUTF16(L"\x05e1\x05e2"), textfield_->GetSelectedText()); - VerifySelectionHandlePositions(true); - */ - - // Drag selection handle 2 to right by 1 char. - x = textfield_->font().GetStringWidth(WideToUTF16(L"d")); - SimulateSelectionHandleDrag(gfx::Point(x, 0), 2); - EXPECT_EQ(WideToUTF16(L"\x05e2\x05e3"L"d"), textfield_->GetSelectedText()); - VerifySelectionHandlePositions(false); - - // Select [\x05r3] from right to left. - textfield_->SelectRange(ui::Range(5, 6)); - EXPECT_EQ(WideToUTF16(L"\x05e3"), textfield_->GetSelectedText()); - VerifySelectionHandlePositions(false); - - // Drag selection handle 2 to left by 1 char. - x = textfield_->font().GetStringWidth(WideToUTF16(L"c")); - SimulateSelectionHandleDrag(gfx::Point(-x, 0), 2); - EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText()); - VerifySelectionHandlePositions(false); - - // Drag selection handle 1 to right by 1 char. - x = textfield_->font().GetStringWidth(WideToUTF16(L"\x05e2")); - SimulateSelectionHandleDrag(gfx::Point(x, 0), 1); - EXPECT_EQ(WideToUTF16(L"c\x05e1"), textfield_->GetSelectedText()); - VerifySelectionHandlePositions(true); - - // Select [\x05r3] from left to right. - textfield_->SelectRange(ui::Range(6, 5)); - EXPECT_EQ(WideToUTF16(L"\x05e3"), textfield_->GetSelectedText()); - VerifySelectionHandlePositions(false); - - // Drag selection handle 1 to left by 1 char. - x = textfield_->font().GetStringWidth(WideToUTF16(L"c")); - SimulateSelectionHandleDrag(gfx::Point(-x, 0), 1); - EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText()); - VerifySelectionHandlePositions(true); - - // Drag selection handle 2 to right by 1 char. - x = textfield_->font().GetStringWidth(WideToUTF16(L"\x05e2")); - SimulateSelectionHandleDrag(gfx::Point(x, 0), 2); - EXPECT_EQ(WideToUTF16(L"c\x05e1"), textfield_->GetSelectedText()); - VerifySelectionHandlePositions(false); -} - -} // namespace views |