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" |
| 6 | #include "ash/host/root_window_transformer.h" |
oshima | 5e0fff8 | 2015-06-27 01:31:30 | [diff] [blame^] | 7 | #include "ash/ime/input_method_event_handler.h" |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 8 | #include "base/logging.h" |
| 9 | #include "ui/aura/window.h" |
| 10 | #include "ui/aura/window_event_dispatcher.h" |
| 11 | #include "ui/aura/window_targeter.h" |
| 12 | #include "ui/compositor/compositor.h" |
| 13 | #include "ui/events/event_processor.h" |
oshima | b8fb0b7 | 2015-04-28 18:25:10 | [diff] [blame] | 14 | #include "ui/events/null_event_targeter.h" |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 15 | #include "ui/gfx/geometry/insets.h" |
| 16 | |
| 17 | namespace ash { |
| 18 | |
| 19 | class UnifiedEventTargeter : public aura::WindowTargeter { |
| 20 | public: |
| 21 | UnifiedEventTargeter(aura::Window* src_root, aura::Window* dst_root) |
| 22 | : src_root_(src_root), dst_root_(dst_root) {} |
| 23 | |
| 24 | ui::EventTarget* FindTargetForEvent(ui::EventTarget* root, |
| 25 | ui::Event* event) override { |
| 26 | if (root == src_root_ && !event->target()) { |
| 27 | if (event->IsLocatedEvent()) { |
| 28 | ui::LocatedEvent* located_event = static_cast<ui::LocatedEvent*>(event); |
| 29 | located_event->ConvertLocationToTarget( |
| 30 | static_cast<aura::Window*>(nullptr), dst_root_); |
| 31 | located_event->UpdateForRootTransform( |
| 32 | dst_root_->GetHost()->GetRootTransform()); |
| 33 | } |
| 34 | ignore_result( |
| 35 | dst_root_->GetHost()->event_processor()->OnEventFromSource(event)); |
| 36 | return nullptr; |
| 37 | } else { |
oshima | f8b2769 | 2015-04-30 07:23:00 | [diff] [blame] | 38 | NOTREACHED() << "event type:" << event->type(); |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 39 | return aura::WindowTargeter::FindTargetForEvent(root, event); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | aura::Window* src_root_; |
| 44 | aura::Window* dst_root_; |
| 45 | |
| 46 | DISALLOW_COPY_AND_ASSIGN(UnifiedEventTargeter); |
| 47 | }; |
| 48 | |
| 49 | AshWindowTreeHostUnified::AshWindowTreeHostUnified( |
| 50 | const gfx::Rect& initial_bounds) |
oshima | 38dffad | 2015-05-05 17:22:56 | [diff] [blame] | 51 | : bounds_(gfx::Rect(initial_bounds.size())), transformer_helper_(this) { |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 52 | CreateCompositor(GetAcceleratedWidget()); |
oshima | 38dffad | 2015-05-05 17:22:56 | [diff] [blame] | 53 | transformer_helper_.Init(); |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | AshWindowTreeHostUnified::~AshWindowTreeHostUnified() { |
oshima | c2f6958 | 2015-05-20 22:04:54 | [diff] [blame] | 57 | for (auto* ash_host : mirroring_hosts_) |
| 58 | ash_host->AsWindowTreeHost()->window()->RemoveObserver(this); |
| 59 | |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 60 | DestroyCompositor(); |
| 61 | DestroyDispatcher(); |
| 62 | } |
| 63 | |
| 64 | void AshWindowTreeHostUnified::PrepareForShutdown() { |
oshima | b8fb0b7 | 2015-04-28 18:25:10 | [diff] [blame] | 65 | window()->SetEventTargeter( |
| 66 | scoped_ptr<ui::EventTargeter>(new ui::NullEventTargeter)); |
| 67 | |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 68 | for (auto host : mirroring_hosts_) |
| 69 | host->PrepareForShutdown(); |
| 70 | } |
| 71 | |
| 72 | void AshWindowTreeHostUnified::RegisterMirroringHost( |
| 73 | AshWindowTreeHost* mirroring_ash_host) { |
| 74 | aura::Window* src_root = mirroring_ash_host->AsWindowTreeHost()->window(); |
| 75 | src_root->SetEventTargeter( |
| 76 | make_scoped_ptr(new UnifiedEventTargeter(src_root, window()))); |
| 77 | DCHECK(std::find(mirroring_hosts_.begin(), mirroring_hosts_.end(), |
| 78 | mirroring_ash_host) == mirroring_hosts_.end()); |
| 79 | mirroring_hosts_.push_back(mirroring_ash_host); |
| 80 | mirroring_ash_host->AsWindowTreeHost()->window()->AddObserver(this); |
| 81 | } |
| 82 | |
| 83 | void AshWindowTreeHostUnified::ToggleFullScreen() { |
| 84 | } |
| 85 | |
| 86 | bool AshWindowTreeHostUnified::ConfineCursorToRootWindow() { |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | void AshWindowTreeHostUnified::UnConfineCursor() { |
| 91 | } |
| 92 | |
| 93 | void AshWindowTreeHostUnified::SetRootWindowTransformer( |
| 94 | scoped_ptr<RootWindowTransformer> transformer) { |
oshima | 38dffad | 2015-05-05 17:22:56 | [diff] [blame] | 95 | transformer_helper_.SetRootWindowTransformer(transformer.Pass()); |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | gfx::Insets AshWindowTreeHostUnified::GetHostInsets() const { |
oshima | 38dffad | 2015-05-05 17:22:56 | [diff] [blame] | 99 | return transformer_helper_.GetHostInsets(); |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | aura::WindowTreeHost* AshWindowTreeHostUnified::AsWindowTreeHost() { |
| 103 | return this; |
| 104 | } |
| 105 | |
| 106 | ui::EventSource* AshWindowTreeHostUnified::GetEventSource() { |
| 107 | return this; |
| 108 | } |
| 109 | |
| 110 | gfx::AcceleratedWidget AshWindowTreeHostUnified::GetAcceleratedWidget() { |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 111 | return gfx::kNullAcceleratedWidget; |
| 112 | } |
| 113 | |
jbauman | 8da589a | 2015-06-04 02:23:12 | [diff] [blame] | 114 | void AshWindowTreeHostUnified::ShowImpl() { |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 115 | } |
| 116 | |
jbauman | 8da589a | 2015-06-04 02:23:12 | [diff] [blame] | 117 | void AshWindowTreeHostUnified::HideImpl() { |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | gfx::Rect AshWindowTreeHostUnified::GetBounds() const { |
| 121 | return bounds_; |
| 122 | } |
| 123 | |
| 124 | void AshWindowTreeHostUnified::SetBounds(const gfx::Rect& bounds) { |
| 125 | if (bounds_.size() == bounds.size()) |
| 126 | return; |
| 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) { |
| 165 | // TODO(oshima): Find out if this is neceessary. |
| 166 | NOTIMPLEMENTED(); |
| 167 | } |
| 168 | |
| 169 | void AshWindowTreeHostUnified::OnCursorVisibilityChangedNative(bool show) { |
| 170 | for (auto host : mirroring_hosts_) |
| 171 | host->AsWindowTreeHost()->OnCursorVisibilityChanged(show); |
| 172 | } |
| 173 | |
| 174 | void AshWindowTreeHostUnified::OnWindowDestroying(aura::Window* window) { |
| 175 | auto iter = |
| 176 | std::find_if(mirroring_hosts_.begin(), mirroring_hosts_.end(), |
| 177 | [window](AshWindowTreeHost* ash_host) { |
| 178 | return ash_host->AsWindowTreeHost()->window() == window; |
| 179 | }); |
| 180 | DCHECK(iter != mirroring_hosts_.end()); |
| 181 | window->RemoveObserver(this); |
| 182 | mirroring_hosts_.erase(iter); |
| 183 | } |
| 184 | |
oshima | 5e0fff8 | 2015-06-27 01:31:30 | [diff] [blame^] | 185 | bool AshWindowTreeHostUnified::DispatchKeyEventPostIME( |
| 186 | const ui::KeyEvent& event) { |
| 187 | ui::KeyEvent event_copy(event); |
| 188 | input_method_handler()->SetPostIME(true); |
| 189 | ui::EventSource::DeliverEventToProcessor(&event_copy); |
| 190 | input_method_handler()->SetPostIME(false); |
| 191 | return event_copy.handled(); |
| 192 | } |
| 193 | |
| 194 | ui::EventDispatchDetails AshWindowTreeHostUnified::DeliverEventToProcessor( |
| 195 | ui::Event* event) { |
| 196 | return ui::EventSource::DeliverEventToProcessor(event); |
| 197 | } |
| 198 | |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 199 | } // namespace ash |