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