oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 1 | // Copyright 2015 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/host/ash_window_tree_host_unified.h" |
dcheng | cbf0d9d | 2015-12-27 22:49:23 | [diff] [blame^] | 6 | |
| 7 | #include <utility> |
| 8 | |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 9 | #include "ash/host/root_window_transformer.h" |
oshima | 5e0fff8 | 2015-06-27 01:31:30 | [diff] [blame] | 10 | #include "ash/ime/input_method_event_handler.h" |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 11 | #include "base/logging.h" |
| 12 | #include "ui/aura/window.h" |
| 13 | #include "ui/aura/window_event_dispatcher.h" |
| 14 | #include "ui/aura/window_targeter.h" |
| 15 | #include "ui/compositor/compositor.h" |
| 16 | #include "ui/events/event_processor.h" |
oshima | b8fb0b7 | 2015-04-28 18:25:10 | [diff] [blame] | 17 | #include "ui/events/null_event_targeter.h" |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 18 | #include "ui/gfx/geometry/insets.h" |
| 19 | |
| 20 | namespace ash { |
| 21 | |
| 22 | class UnifiedEventTargeter : public aura::WindowTargeter { |
| 23 | public: |
| 24 | UnifiedEventTargeter(aura::Window* src_root, aura::Window* dst_root) |
| 25 | : src_root_(src_root), dst_root_(dst_root) {} |
| 26 | |
| 27 | ui::EventTarget* FindTargetForEvent(ui::EventTarget* root, |
| 28 | ui::Event* event) override { |
| 29 | if (root == src_root_ && !event->target()) { |
| 30 | if (event->IsLocatedEvent()) { |
| 31 | ui::LocatedEvent* located_event = static_cast<ui::LocatedEvent*>(event); |
| 32 | located_event->ConvertLocationToTarget( |
| 33 | static_cast<aura::Window*>(nullptr), dst_root_); |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 34 | } |
| 35 | ignore_result( |
| 36 | dst_root_->GetHost()->event_processor()->OnEventFromSource(event)); |
| 37 | return nullptr; |
| 38 | } else { |
oshima | f8b2769 | 2015-04-30 07:23:00 | [diff] [blame] | 39 | NOTREACHED() << "event type:" << event->type(); |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 40 | return aura::WindowTargeter::FindTargetForEvent(root, event); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | aura::Window* src_root_; |
| 45 | aura::Window* dst_root_; |
| 46 | |
| 47 | DISALLOW_COPY_AND_ASSIGN(UnifiedEventTargeter); |
| 48 | }; |
| 49 | |
| 50 | AshWindowTreeHostUnified::AshWindowTreeHostUnified( |
| 51 | const gfx::Rect& initial_bounds) |
oshima | 38dffad | 2015-05-05 17:22:56 | [diff] [blame] | 52 | : bounds_(gfx::Rect(initial_bounds.size())), transformer_helper_(this) { |
sievers | b2a31d337 | 2015-08-25 19:27:13 | [diff] [blame] | 53 | CreateCompositor(); |
| 54 | OnAcceleratedWidgetAvailable(); |
oshima | 38dffad | 2015-05-05 17:22:56 | [diff] [blame] | 55 | transformer_helper_.Init(); |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | AshWindowTreeHostUnified::~AshWindowTreeHostUnified() { |
oshima | c2f6958 | 2015-05-20 22:04:54 | [diff] [blame] | 59 | for (auto* ash_host : mirroring_hosts_) |
| 60 | ash_host->AsWindowTreeHost()->window()->RemoveObserver(this); |
| 61 | |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 62 | DestroyCompositor(); |
| 63 | DestroyDispatcher(); |
| 64 | } |
| 65 | |
| 66 | void AshWindowTreeHostUnified::PrepareForShutdown() { |
oshima | b8fb0b7 | 2015-04-28 18:25:10 | [diff] [blame] | 67 | window()->SetEventTargeter( |
| 68 | scoped_ptr<ui::EventTargeter>(new ui::NullEventTargeter)); |
| 69 | |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 70 | for (auto host : mirroring_hosts_) |
| 71 | host->PrepareForShutdown(); |
| 72 | } |
| 73 | |
| 74 | void AshWindowTreeHostUnified::RegisterMirroringHost( |
| 75 | AshWindowTreeHost* mirroring_ash_host) { |
| 76 | aura::Window* src_root = mirroring_ash_host->AsWindowTreeHost()->window(); |
| 77 | src_root->SetEventTargeter( |
| 78 | make_scoped_ptr(new UnifiedEventTargeter(src_root, window()))); |
| 79 | DCHECK(std::find(mirroring_hosts_.begin(), mirroring_hosts_.end(), |
| 80 | mirroring_ash_host) == mirroring_hosts_.end()); |
| 81 | mirroring_hosts_.push_back(mirroring_ash_host); |
| 82 | mirroring_ash_host->AsWindowTreeHost()->window()->AddObserver(this); |
| 83 | } |
| 84 | |
| 85 | void AshWindowTreeHostUnified::ToggleFullScreen() { |
| 86 | } |
| 87 | |
| 88 | bool AshWindowTreeHostUnified::ConfineCursorToRootWindow() { |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | void AshWindowTreeHostUnified::UnConfineCursor() { |
| 93 | } |
| 94 | |
| 95 | void AshWindowTreeHostUnified::SetRootWindowTransformer( |
| 96 | scoped_ptr<RootWindowTransformer> transformer) { |
dcheng | cbf0d9d | 2015-12-27 22:49:23 | [diff] [blame^] | 97 | transformer_helper_.SetRootWindowTransformer(std::move(transformer)); |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | gfx::Insets AshWindowTreeHostUnified::GetHostInsets() const { |
oshima | 38dffad | 2015-05-05 17:22:56 | [diff] [blame] | 101 | return transformer_helper_.GetHostInsets(); |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | aura::WindowTreeHost* AshWindowTreeHostUnified::AsWindowTreeHost() { |
| 105 | return this; |
| 106 | } |
| 107 | |
| 108 | ui::EventSource* AshWindowTreeHostUnified::GetEventSource() { |
| 109 | return this; |
| 110 | } |
| 111 | |
| 112 | gfx::AcceleratedWidget AshWindowTreeHostUnified::GetAcceleratedWidget() { |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 113 | return gfx::kNullAcceleratedWidget; |
| 114 | } |
| 115 | |
jbauman | 8da589a | 2015-06-04 02:23:12 | [diff] [blame] | 116 | void AshWindowTreeHostUnified::ShowImpl() { |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 117 | } |
| 118 | |
jbauman | 8da589a | 2015-06-04 02:23:12 | [diff] [blame] | 119 | void AshWindowTreeHostUnified::HideImpl() { |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | gfx::Rect AshWindowTreeHostUnified::GetBounds() const { |
| 123 | return bounds_; |
| 124 | } |
| 125 | |
| 126 | void AshWindowTreeHostUnified::SetBounds(const gfx::Rect& bounds) { |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 127 | bounds_.set_size(bounds.size()); |
| 128 | OnHostResized(bounds_.size()); |
| 129 | } |
| 130 | |
oshima | 38dffad | 2015-05-05 17:22:56 | [diff] [blame] | 131 | gfx::Transform AshWindowTreeHostUnified::GetRootTransform() const { |
| 132 | return transformer_helper_.GetTransform(); |
| 133 | } |
| 134 | |
| 135 | void AshWindowTreeHostUnified::SetRootTransform( |
| 136 | const gfx::Transform& transform) { |
| 137 | transformer_helper_.SetTransform(transform); |
| 138 | } |
| 139 | |
| 140 | gfx::Transform AshWindowTreeHostUnified::GetInverseRootTransform() const { |
| 141 | return transformer_helper_.GetInverseTransform(); |
| 142 | } |
| 143 | |
| 144 | void AshWindowTreeHostUnified::UpdateRootWindowSize( |
| 145 | const gfx::Size& host_size) { |
| 146 | transformer_helper_.UpdateWindowSize(host_size); |
| 147 | } |
| 148 | |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 149 | void AshWindowTreeHostUnified::SetCapture() { |
| 150 | } |
| 151 | |
| 152 | void AshWindowTreeHostUnified::ReleaseCapture() { |
| 153 | } |
| 154 | |
| 155 | gfx::Point AshWindowTreeHostUnified::GetLocationOnNativeScreen() const { |
| 156 | return gfx::Point(); |
| 157 | } |
| 158 | |
| 159 | void AshWindowTreeHostUnified::SetCursorNative(gfx::NativeCursor cursor) { |
| 160 | for (auto host : mirroring_hosts_) |
| 161 | host->AsWindowTreeHost()->SetCursor(cursor); |
| 162 | } |
| 163 | |
| 164 | void AshWindowTreeHostUnified::MoveCursorToNative(const gfx::Point& location) { |
oshima | af6e857 | 2015-07-30 19:41:57 | [diff] [blame] | 165 | // No native cursor in offscreen surface. |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | void AshWindowTreeHostUnified::OnCursorVisibilityChangedNative(bool show) { |
| 169 | for (auto host : mirroring_hosts_) |
| 170 | host->AsWindowTreeHost()->OnCursorVisibilityChanged(show); |
| 171 | } |
| 172 | |
| 173 | void AshWindowTreeHostUnified::OnWindowDestroying(aura::Window* window) { |
| 174 | auto iter = |
| 175 | std::find_if(mirroring_hosts_.begin(), mirroring_hosts_.end(), |
| 176 | [window](AshWindowTreeHost* ash_host) { |
| 177 | return ash_host->AsWindowTreeHost()->window() == window; |
| 178 | }); |
| 179 | DCHECK(iter != mirroring_hosts_.end()); |
| 180 | window->RemoveObserver(this); |
| 181 | mirroring_hosts_.erase(iter); |
| 182 | } |
| 183 | |
shuchen | 59c2a2cf | 2015-08-04 09:47:50 | [diff] [blame] | 184 | ui::EventDispatchDetails AshWindowTreeHostUnified::DispatchKeyEventPostIME( |
| 185 | ui::KeyEvent* event) { |
oshima | 5e0fff8 | 2015-06-27 01:31:30 | [diff] [blame] | 186 | input_method_handler()->SetPostIME(true); |
shuchen | 2bfd5db | 2015-07-23 01:28:38 | [diff] [blame] | 187 | ui::EventDispatchDetails details = |
shuchen | 59c2a2cf | 2015-08-04 09:47:50 | [diff] [blame] | 188 | event_processor()->OnEventFromSource(event); |
| 189 | if (!details.dispatcher_destroyed) |
| 190 | input_method_handler()->SetPostIME(false); |
| 191 | return details; |
oshima | 5e0fff8 | 2015-06-27 01:31:30 | [diff] [blame] | 192 | } |
| 193 | |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 194 | } // namespace ash |