OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 "athena/env/public/athena_env.h" |
| 6 |
| 7 #include "athena/common/fill_layout_manager.h" |
| 8 #include "ui/aura/client/aura_constants.h" |
| 9 #include "ui/aura/client/cursor_client.h" |
| 10 #include "ui/aura/client/default_capture_client.h" |
| 11 #include "ui/aura/env.h" |
| 12 #include "ui/aura/test/test_screen.h" |
| 13 #include "ui/aura/window_event_dispatcher.h" |
| 14 #include "ui/aura/window_tree_host.h" |
| 15 #include "ui/aura/window_tree_host_observer.h" |
| 16 #include "ui/base/cursor/cursor.h" |
| 17 #include "ui/base/cursor/image_cursors.h" |
| 18 #include "ui/chromeos/user_activity_power_manager_notifier.h" |
| 19 #include "ui/display/chromeos/display_configurator.h" |
| 20 #include "ui/display/types/chromeos/display_mode.h" |
| 21 #include "ui/display/types/chromeos/display_snapshot.h" |
| 22 #include "ui/gfx/screen.h" |
| 23 #include "ui/wm/core/compound_event_filter.h" |
| 24 #include "ui/wm/core/cursor_manager.h" |
| 25 #include "ui/wm/core/input_method_event_filter.h" |
| 26 #include "ui/wm/core/native_cursor_manager.h" |
| 27 #include "ui/wm/core/native_cursor_manager_delegate.h" |
| 28 #include "ui/wm/core/user_activity_detector.h" |
| 29 |
| 30 namespace athena { |
| 31 |
| 32 namespace { |
| 33 |
| 34 AthenaEnv* instance = NULL; |
| 35 |
| 36 // A class that bridges the gap between CursorManager and Aura. It borrows |
| 37 // heavily from AshNativeCursorManager. |
| 38 class AthenaNativeCursorManager : public wm::NativeCursorManager { |
| 39 public: |
| 40 explicit AthenaNativeCursorManager(aura::WindowTreeHost* host) |
| 41 : host_(host), image_cursors_(new ui::ImageCursors) {} |
| 42 virtual ~AthenaNativeCursorManager() {} |
| 43 |
| 44 // wm::NativeCursorManager overrides. |
| 45 virtual void SetDisplay(const gfx::Display& display, |
| 46 wm::NativeCursorManagerDelegate* delegate) OVERRIDE { |
| 47 if (image_cursors_->SetDisplay(display, display.device_scale_factor())) |
| 48 SetCursor(delegate->GetCursor(), delegate); |
| 49 } |
| 50 |
| 51 virtual void SetCursor(gfx::NativeCursor cursor, |
| 52 wm::NativeCursorManagerDelegate* delegate) OVERRIDE { |
| 53 image_cursors_->SetPlatformCursor(&cursor); |
| 54 cursor.set_device_scale_factor(image_cursors_->GetScale()); |
| 55 delegate->CommitCursor(cursor); |
| 56 |
| 57 if (delegate->IsCursorVisible()) |
| 58 ApplyCursor(cursor); |
| 59 } |
| 60 |
| 61 virtual void SetVisibility( |
| 62 bool visible, |
| 63 wm::NativeCursorManagerDelegate* delegate) OVERRIDE { |
| 64 delegate->CommitVisibility(visible); |
| 65 |
| 66 if (visible) { |
| 67 SetCursor(delegate->GetCursor(), delegate); |
| 68 } else { |
| 69 gfx::NativeCursor invisible_cursor(ui::kCursorNone); |
| 70 image_cursors_->SetPlatformCursor(&invisible_cursor); |
| 71 ApplyCursor(invisible_cursor); |
| 72 } |
| 73 } |
| 74 |
| 75 virtual void SetCursorSet( |
| 76 ui::CursorSetType cursor_set, |
| 77 wm::NativeCursorManagerDelegate* delegate) OVERRIDE { |
| 78 image_cursors_->SetCursorSet(cursor_set); |
| 79 delegate->CommitCursorSet(cursor_set); |
| 80 if (delegate->IsCursorVisible()) |
| 81 SetCursor(delegate->GetCursor(), delegate); |
| 82 } |
| 83 |
| 84 virtual void SetMouseEventsEnabled( |
| 85 bool enabled, |
| 86 wm::NativeCursorManagerDelegate* delegate) OVERRIDE { |
| 87 delegate->CommitMouseEventsEnabled(enabled); |
| 88 SetVisibility(delegate->IsCursorVisible(), delegate); |
| 89 } |
| 90 |
| 91 private: |
| 92 // Sets |cursor| as the active cursor within Aura. |
| 93 void ApplyCursor(gfx::NativeCursor cursor) { host_->SetCursor(cursor); } |
| 94 |
| 95 aura::WindowTreeHost* host_; // Not owned. |
| 96 |
| 97 scoped_ptr<ui::ImageCursors> image_cursors_; |
| 98 |
| 99 DISALLOW_COPY_AND_ASSIGN(AthenaNativeCursorManager); |
| 100 }; |
| 101 |
| 102 class AthenaEnvImpl : public AthenaEnv, |
| 103 public aura::WindowTreeHostObserver, |
| 104 public ui::DisplayConfigurator::Observer { |
| 105 public: |
| 106 AthenaEnvImpl() : display_configurator_(new ui::DisplayConfigurator) { |
| 107 display_configurator_->Init(false); |
| 108 display_configurator_->ForceInitialConfigure(0); |
| 109 display_configurator_->AddObserver(this); |
| 110 |
| 111 gfx::Size screen_size = GetPrimaryDisplaySize(); |
| 112 if (screen_size.IsEmpty()) |
| 113 screen_size.SetSize(1280, 720); |
| 114 screen_.reset(aura::TestScreen::Create(screen_size)); |
| 115 |
| 116 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen_.get()); |
| 117 host_.reset(screen_->CreateHostForPrimaryDisplay()); |
| 118 host_->InitHost(); |
| 119 |
| 120 aura::Window* root_window = GetHost()->window(); |
| 121 input_method_filter_.reset( |
| 122 new wm::InputMethodEventFilter(host_->GetAcceleratedWidget())); |
| 123 input_method_filter_->SetInputMethodPropertyInRootWindow(root_window); |
| 124 |
| 125 root_window_event_filter_.reset(new wm::CompoundEventFilter); |
| 126 host_->window()->AddPreTargetHandler(root_window_event_filter_.get()); |
| 127 |
| 128 input_method_filter_.reset( |
| 129 new wm::InputMethodEventFilter(host_->GetAcceleratedWidget())); |
| 130 input_method_filter_->SetInputMethodPropertyInRootWindow(host_->window()); |
| 131 root_window_event_filter_->AddHandler(input_method_filter_.get()); |
| 132 |
| 133 capture_client_.reset( |
| 134 new aura::client::DefaultCaptureClient(host_->window())); |
| 135 |
| 136 // Ensure new windows fill the display. |
| 137 root_window->SetLayoutManager(new FillLayoutManager(root_window)); |
| 138 |
| 139 cursor_manager_.reset( |
| 140 new wm::CursorManager(scoped_ptr<wm::NativeCursorManager>( |
| 141 new AthenaNativeCursorManager(host_.get())))); |
| 142 cursor_manager_->SetDisplay( |
| 143 gfx::Screen::GetNativeScreen()->GetPrimaryDisplay()); |
| 144 cursor_manager_->SetCursor(ui::kCursorPointer); |
| 145 aura::client::SetCursorClient(host_->window(), cursor_manager_.get()); |
| 146 |
| 147 user_activity_detector_.reset(new wm::UserActivityDetector); |
| 148 host_->event_processor()->GetRootTarget()->AddPreTargetHandler( |
| 149 user_activity_detector_.get()); |
| 150 user_activity_notifier_.reset(new ui::UserActivityPowerManagerNotifier( |
| 151 user_activity_detector_.get())); |
| 152 |
| 153 host_->AddObserver(this); |
| 154 host_->Show(); |
| 155 |
| 156 DCHECK(!instance); |
| 157 instance = this; |
| 158 } |
| 159 |
| 160 virtual ~AthenaEnvImpl() { |
| 161 instance = NULL; |
| 162 |
| 163 host_->RemoveObserver(this); |
| 164 if (input_method_filter_) |
| 165 root_window_event_filter_->RemoveHandler(input_method_filter_.get()); |
| 166 if (user_activity_detector_) { |
| 167 host_->event_processor()->GetRootTarget()->RemovePreTargetHandler( |
| 168 user_activity_detector_.get()); |
| 169 } |
| 170 root_window_event_filter_.reset(); |
| 171 capture_client_.reset(); |
| 172 input_method_filter_.reset(); |
| 173 cursor_manager_.reset(); |
| 174 user_activity_notifier_.reset(); |
| 175 user_activity_detector_.reset(); |
| 176 |
| 177 input_method_filter_.reset(); |
| 178 host_.reset(); |
| 179 screen_.reset(); |
| 180 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, NULL); |
| 181 |
| 182 aura::Env::DeleteInstance(); |
| 183 |
| 184 display_configurator_->RemoveObserver(this); |
| 185 display_configurator_.reset(); |
| 186 } |
| 187 |
| 188 private: |
| 189 virtual aura::WindowTreeHost* GetHost() OVERRIDE { return host_.get(); } |
| 190 |
| 191 // AthenaEnv: |
| 192 virtual void SetDisplayWorkAreaInsets(const gfx::Insets& insets) OVERRIDE { |
| 193 screen_->SetWorkAreaInsets(insets); |
| 194 } |
| 195 |
| 196 // ui::DisplayConfigurator::Observer: |
| 197 virtual void OnDisplayModeChanged(const std::vector< |
| 198 ui::DisplayConfigurator::DisplayState>& displays) OVERRIDE { |
| 199 gfx::Size size = GetPrimaryDisplaySize(); |
| 200 if (!size.IsEmpty()) |
| 201 host_->UpdateRootWindowSize(size); |
| 202 } |
| 203 |
| 204 // aura::WindowTreeHostObserver: |
| 205 virtual void OnHostCloseRequested(const aura::WindowTreeHost* host) OVERRIDE { |
| 206 base::MessageLoopForUI::current()->PostTask( |
| 207 FROM_HERE, base::MessageLoop::QuitClosure()); |
| 208 } |
| 209 |
| 210 gfx::Size GetPrimaryDisplaySize() const { |
| 211 const std::vector<ui::DisplayConfigurator::DisplayState>& displays = |
| 212 display_configurator_->cached_displays(); |
| 213 if (displays.empty()) |
| 214 return gfx::Size(); |
| 215 const ui::DisplayMode* mode = displays[0].display->current_mode(); |
| 216 return mode ? mode->size() : gfx::Size(); |
| 217 } |
| 218 |
| 219 scoped_ptr<aura::TestScreen> screen_; |
| 220 scoped_ptr<aura::WindowTreeHost> host_; |
| 221 |
| 222 scoped_ptr<wm::InputMethodEventFilter> input_method_filter_; |
| 223 scoped_ptr<wm::CompoundEventFilter> root_window_event_filter_; |
| 224 scoped_ptr<aura::client::DefaultCaptureClient> capture_client_; |
| 225 scoped_ptr<wm::CursorManager> cursor_manager_; |
| 226 scoped_ptr<wm::UserActivityDetector> user_activity_detector_; |
| 227 scoped_ptr<ui::DisplayConfigurator> display_configurator_; |
| 228 scoped_ptr<ui::UserActivityPowerManagerNotifier> user_activity_notifier_; |
| 229 |
| 230 DISALLOW_COPY_AND_ASSIGN(AthenaEnvImpl); |
| 231 }; |
| 232 |
| 233 } // namespace |
| 234 |
| 235 // static |
| 236 void AthenaEnv::Create() { |
| 237 DCHECK(!instance); |
| 238 new AthenaEnvImpl(); |
| 239 } |
| 240 |
| 241 AthenaEnv* AthenaEnv::Get() { |
| 242 DCHECK(instance); |
| 243 return instance; |
| 244 } |
| 245 |
| 246 // static |
| 247 |
| 248 // static |
| 249 void AthenaEnv::Shutdown() { |
| 250 DCHECK(instance); |
| 251 delete instance; |
| 252 } |
| 253 |
| 254 } // namespace athena |
OLD | NEW |