| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ash/autoclick/mus/autoclick_application.h" |
| 6 |
| 7 #include "ash/public/interfaces/container.mojom.h" |
| 8 #include "base/macros.h" |
| 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "services/shell/public/cpp/connector.h" |
| 11 #include "services/ui/public/cpp/property_type_converters.h" |
| 12 #include "services/ui/public/interfaces/window_manager_constants.mojom.h" |
| 13 #include "ui/views/mus/aura_init.h" |
| 14 #include "ui/views/mus/native_widget_mus.h" |
| 15 #include "ui/views/mus/pointer_watcher_event_router.h" |
| 16 #include "ui/views/mus/window_manager_connection.h" |
| 17 #include "ui/views/pointer_watcher.h" |
| 18 #include "ui/views/widget/widget.h" |
| 19 #include "ui/views/widget/widget_delegate.h" |
| 20 |
| 21 namespace ash { |
| 22 namespace autoclick { |
| 23 |
| 24 // The default wait time between last mouse movement and sending |
| 25 // the autoclick. |
| 26 const int kDefaultAutoclickDelayMs = 1000; |
| 27 |
| 28 // AutoclickUI handles events to the autoclick app. |
| 29 class AutoclickUI : public views::WidgetDelegateView, |
| 30 public views::PointerWatcher { |
| 31 public: |
| 32 AutoclickUI(views::WindowManagerConnection* window_manager_connection, |
| 33 AutoclickControllerCommon* autoclick_controller_common) |
| 34 : window_manager_connection_(window_manager_connection), |
| 35 autoclick_controller_common_(autoclick_controller_common) { |
| 36 window_manager_connection_->pointer_watcher_event_router() |
| 37 ->AddPointerWatcher(this, true /* want_moves */); |
| 38 } |
| 39 ~AutoclickUI() override { |
| 40 window_manager_connection_->pointer_watcher_event_router() |
| 41 ->RemovePointerWatcher(this); |
| 42 } |
| 43 |
| 44 private: |
| 45 // Overridden from views::WidgetDelegate: |
| 46 views::View* GetContentsView() override { return this; } |
| 47 base::string16 GetWindowTitle() const override { |
| 48 // TODO(beng): use resources. |
| 49 return base::ASCIIToUTF16("Autoclick"); |
| 50 } |
| 51 |
| 52 // Overridden from views::PointerWatcher: |
| 53 void OnPointerEventObserved(const ui::PointerEvent& event, |
| 54 const gfx::Point& location_in_screen, |
| 55 views::Widget* target) override { |
| 56 if (event.IsTouchPointerEvent()) { |
| 57 autoclick_controller_common_->CancelAutoclick(); |
| 58 } else if (event.IsMousePointerEvent()) { |
| 59 if (event.type() == ui::ET_POINTER_WHEEL_CHANGED) { |
| 60 autoclick_controller_common_->HandleMouseEvent( |
| 61 ui::MouseWheelEvent(event)); |
| 62 } else { |
| 63 autoclick_controller_common_->HandleMouseEvent(ui::MouseEvent(event)); |
| 64 } |
| 65 } |
| 66 } |
| 67 |
| 68 views::WindowManagerConnection* window_manager_connection_; |
| 69 AutoclickControllerCommon* autoclick_controller_common_; |
| 70 |
| 71 DISALLOW_COPY_AND_ASSIGN(AutoclickUI); |
| 72 }; |
| 73 |
| 74 AutoclickApplication::AutoclickApplication() |
| 75 : launchable_binding_(this), autoclick_binding_(this) {} |
| 76 |
| 77 AutoclickApplication::~AutoclickApplication() {} |
| 78 |
| 79 void AutoclickApplication::OnStart(const shell::Identity& identity) { |
| 80 aura_init_.reset(new views::AuraInit(connector(), "views_mus_resources.pak")); |
| 81 window_manager_connection_ = |
| 82 views::WindowManagerConnection::Create(connector(), identity); |
| 83 autoclick_controller_common_.reset(new AutoclickControllerCommon( |
| 84 base::TimeDelta::FromMilliseconds(kDefaultAutoclickDelayMs), this)); |
| 85 } |
| 86 |
| 87 bool AutoclickApplication::OnConnect(const shell::Identity& remote_identity, |
| 88 shell::InterfaceRegistry* registry) { |
| 89 registry->AddInterface<mash::mojom::Launchable>(this); |
| 90 registry->AddInterface<mojom::AutoclickController>(this); |
| 91 return true; |
| 92 } |
| 93 |
| 94 void AutoclickApplication::Launch(uint32_t what, mash::mojom::LaunchMode how) { |
| 95 if (!widget_) { |
| 96 widget_.reset(new views::Widget); |
| 97 views::Widget::InitParams params( |
| 98 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); |
| 99 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; |
| 100 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 101 params.activatable = views::Widget::InitParams::ACTIVATABLE_NO; |
| 102 params.accept_events = false; |
| 103 params.delegate = new AutoclickUI(window_manager_connection_.get(), |
| 104 autoclick_controller_common_.get()); |
| 105 |
| 106 std::map<std::string, std::vector<uint8_t>> properties; |
| 107 properties[ash::mojom::kWindowContainer_Property] = |
| 108 mojo::ConvertTo<std::vector<uint8_t>>( |
| 109 static_cast<int32_t>(ash::mojom::Container::OVERLAY)); |
| 110 properties[ui::mojom::WindowManager::kShowState_Property] = |
| 111 mojo::ConvertTo<std::vector<uint8_t>>( |
| 112 static_cast<int32_t>(ui::mojom::ShowState::FULLSCREEN)); |
| 113 ui::Window* window = |
| 114 window_manager_connection_.get()->NewWindow(properties); |
| 115 params.native_widget = new views::NativeWidgetMus( |
| 116 widget_.get(), window, ui::mojom::SurfaceType::DEFAULT); |
| 117 widget_->Init(params); |
| 118 } else { |
| 119 widget_->Close(); |
| 120 base::MessageLoop::current()->QuitWhenIdle(); |
| 121 } |
| 122 } |
| 123 |
| 124 void AutoclickApplication::SetAutoclickDelay(uint32_t delay_in_milliseconds) { |
| 125 autoclick_controller_common_->SetAutoclickDelay( |
| 126 base::TimeDelta::FromMilliseconds(delay_in_milliseconds)); |
| 127 } |
| 128 |
| 129 void AutoclickApplication::Create(const shell::Identity& remote_identity, |
| 130 mash::mojom::LaunchableRequest request) { |
| 131 launchable_binding_.Close(); |
| 132 launchable_binding_.Bind(std::move(request)); |
| 133 } |
| 134 |
| 135 void AutoclickApplication::Create(const shell::Identity& remote_identity, |
| 136 mojom::AutoclickControllerRequest request) { |
| 137 autoclick_binding_.Close(); |
| 138 autoclick_binding_.Bind(std::move(request)); |
| 139 } |
| 140 |
| 141 views::Widget* AutoclickApplication::CreateAutoclickRingWidget( |
| 142 const gfx::Point& event_location) { |
| 143 return widget_.get(); |
| 144 } |
| 145 |
| 146 void AutoclickApplication::UpdateAutoclickRingWidget( |
| 147 views::Widget* widget, |
| 148 const gfx::Point& event_location) { |
| 149 // Not used in mus. |
| 150 } |
| 151 |
| 152 void AutoclickApplication::DoAutoclick(const gfx::Point& event_location, |
| 153 const int mouse_event_flags) { |
| 154 // TODO(riajiang): Currently not working. Need to know how to generate events |
| 155 // in mus world (crbug.com/628665). |
| 156 } |
| 157 |
| 158 void AutoclickApplication::OnAutoclickCanceled() { |
| 159 // Not used in mus. |
| 160 } |
| 161 |
| 162 } // namespace autoclick |
| 163 } // namespace ash |
| OLD | NEW |