| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012 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 "chrome/browser/extensions/api/wm/wm_api.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "ash/wm/window_util.h" |
| 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "chrome/browser/extensions/api/wm/wm_utils.h" |
| 12 #include "chrome/common/extensions/api/experimental_wm.h" |
| 13 #include "content/public/browser/browser_thread.h" |
| 14 #include "ui/aura/client/aura_constants.h" |
| 15 #include "ui/aura/window.h" |
| 16 #include "ui/base/ui_base_types.h" |
| 17 #include "ui/compositor/layer.h" |
| 18 #include "ui/compositor/scoped_layer_animation_settings.h" |
| 19 #include "ui/gfx/display.h" |
| 20 #include "ui/gfx/point3.h" |
| 21 #include "ui/gfx/screen.h" |
| 22 |
| 23 namespace { |
| 24 |
| 25 const char kWindowOrderActivity[] = "activity"; |
| 26 |
| 27 } |
| 28 |
| 29 namespace extensions { |
| 30 namespace api { |
| 31 |
| 32 //////////////////////////////////////////////////////////////////////////////// |
| 33 // WindowFunction |
| 34 |
| 35 WindowFunction::WindowFunction() : window_id_(0) { |
| 36 } |
| 37 |
| 38 WindowFunction::~WindowFunction() {} |
| 39 |
| 40 bool WindowFunction::Prepare() { |
| 41 if (!args_.get() || args_->empty() || |
| 42 !args_->GetInteger(0, &window_id_)) |
| 43 return false; |
| 44 EXTENSION_FUNCTION_VALIDATE(window_id_); |
| 45 set_work_thread_id(content::BrowserThread::UI); |
| 46 return true; |
| 47 } |
| 48 |
| 49 void WindowFunction::Work() { |
| 50 const aura::Window* window = wm::WindowIdTracker::GetInstance()-> |
| 51 ExtensionIdWindow(window_id_); |
| 52 if (!window) { |
| 53 SetResult(base::Value::CreateNullValue()); |
| 54 return; |
| 55 } |
| 56 |
| 57 bool result = WorkOnWindow(const_cast<aura::Window*>(window)); |
| 58 SetResult(base::Value::CreateBooleanValue(result)); |
| 59 } |
| 60 |
| 61 bool WindowFunction::Respond() { |
| 62 return true; |
| 63 } |
| 64 |
| 65 //////////////////////////////////////////////////////////////////////////////// |
| 66 // WmCloseFunction |
| 67 |
| 68 WmCloseFunction::WmCloseFunction() {} |
| 69 |
| 70 WmCloseFunction::~WmCloseFunction() {} |
| 71 |
| 72 bool WmCloseFunction::WorkOnWindow(aura::Window* window) { |
| 73 delete window; |
| 74 return true; |
| 75 } |
| 76 |
| 77 //////////////////////////////////////////////////////////////////////////////// |
| 78 // WmMinimizeFunction |
| 79 |
| 80 WmMinimizeFunction::WmMinimizeFunction() {} |
| 81 |
| 82 WmMinimizeFunction::~WmMinimizeFunction() {} |
| 83 |
| 84 bool WmMinimizeFunction::WorkOnWindow(aura::Window* window) { |
| 85 ash::wm::MinimizeWindow(window); |
| 86 return true; |
| 87 } |
| 88 |
| 89 //////////////////////////////////////////////////////////////////////////////// |
| 90 // WmMaximizeFunction |
| 91 |
| 92 WmMaximizeFunction::WmMaximizeFunction() {} |
| 93 |
| 94 WmMaximizeFunction::~WmMaximizeFunction() {} |
| 95 |
| 96 bool WmMaximizeFunction::WorkOnWindow(aura::Window* window) { |
| 97 ash::wm::MaximizeWindow(window); |
| 98 return true; |
| 99 } |
| 100 |
| 101 //////////////////////////////////////////////////////////////////////////////// |
| 102 // WmFullscreenFunction |
| 103 |
| 104 WmFullscreenFunction::WmFullscreenFunction() {} |
| 105 |
| 106 WmFullscreenFunction::~WmFullscreenFunction() {} |
| 107 |
| 108 bool WmFullscreenFunction::WorkOnWindow(aura::Window* window) { |
| 109 window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_FULLSCREEN); |
| 110 return true; |
| 111 } |
| 112 |
| 113 //////////////////////////////////////////////////////////////////////////////// |
| 114 // WmRestoreFunction |
| 115 |
| 116 WmRestoreFunction::WmRestoreFunction() {} |
| 117 |
| 118 WmRestoreFunction::~WmRestoreFunction() {} |
| 119 |
| 120 bool WmRestoreFunction::WorkOnWindow(aura::Window* window) { |
| 121 if (ash::wm::IsWindowFullscreen(window) || ash::wm::IsWindowMaximized(window)) |
| 122 ash::wm::RestoreWindow(window); |
| 123 return true; |
| 124 } |
| 125 |
| 126 //////////////////////////////////////////////////////////////////////////////// |
| 127 // WindowLayerFunction |
| 128 |
| 129 WindowLayerFunction::WindowLayerFunction(int num_arguments) |
| 130 : duration_(0), |
| 131 num_arguments_(num_arguments) { |
| 132 } |
| 133 |
| 134 WindowLayerFunction::~WindowLayerFunction() {} |
| 135 |
| 136 bool WindowLayerFunction::Prepare() { |
| 137 if (!WindowFunction::Prepare()) |
| 138 return false; |
| 139 if (!args_->GetInteger(num_arguments_ + 1, &duration_)) |
| 140 duration_ = 0; |
| 141 return true; |
| 142 } |
| 143 |
| 144 bool WindowLayerFunction::WorkOnWindow(aura::Window* window) { |
| 145 ui::Layer* layer = window->layer(); |
| 146 if (duration_ > 0) { |
| 147 ui::ScopedLayerAnimationSettings settings(layer->GetAnimator()); |
| 148 settings.SetTransitionDuration( |
| 149 base::TimeDelta::FromMilliseconds(duration_)); |
| 150 WorkOnLayer(layer); |
| 151 } else { |
| 152 WorkOnLayer(layer); |
| 153 } |
| 154 return true; |
| 155 } |
| 156 |
| 157 //////////////////////////////////////////////////////////////////////////////// |
| 158 // WmOpacityFunction |
| 159 |
| 160 WmOpacityFunction::WmOpacityFunction() |
| 161 : WindowLayerFunction(1), |
| 162 opacity_(1.) { |
| 163 } |
| 164 |
| 165 WmOpacityFunction::~WmOpacityFunction() {} |
| 166 |
| 167 bool WmOpacityFunction::Prepare() { |
| 168 if (!WindowLayerFunction::Prepare()) |
| 169 return false; |
| 170 |
| 171 if (!args_->GetDouble(1, &opacity_)) |
| 172 return false; |
| 173 EXTENSION_FUNCTION_VALIDATE(opacity_); |
| 174 return true; |
| 175 } |
| 176 |
| 177 void WmOpacityFunction::WorkOnLayer(ui::Layer* layer) { |
| 178 layer->SetOpacity(static_cast<float>(opacity_)); |
| 179 } |
| 180 |
| 181 //////////////////////////////////////////////////////////////////////////////// |
| 182 // WmTransformFunction |
| 183 |
| 184 WmTransformFunction::WmTransformFunction() |
| 185 : WindowLayerFunction(1) { |
| 186 } |
| 187 |
| 188 WmTransformFunction::~WmTransformFunction() {} |
| 189 |
| 190 bool WmTransformFunction::Prepare() { |
| 191 if (!WindowLayerFunction::Prepare()) |
| 192 return false; |
| 193 |
| 194 base::DictionaryValue* dict = NULL; |
| 195 if (!args_->GetDictionary(1, &dict) || !dict) |
| 196 return false; |
| 197 dictionary_.reset(dict->DeepCopyWithoutEmptyChildren()); |
| 198 return true; |
| 199 } |
| 200 |
| 201 void WmTransformFunction::WorkOnLayer(ui::Layer* layer) { |
| 202 ui::Transform transform; |
| 203 |
| 204 double rotate = 0; |
| 205 if (dictionary_->GetDouble("rotate", &rotate)) { |
| 206 gfx::Point3f axis(1, 0, 0); |
| 207 transform.ConcatRotateAbout(axis, rotate); |
| 208 } |
| 209 |
| 210 double perspective = 0; |
| 211 if (dictionary_->GetDouble("perspective", &perspective)) |
| 212 transform.SetPerspectiveDepth(perspective); |
| 213 |
| 214 double scale; |
| 215 if (dictionary_->GetDouble("scale", &scale)) |
| 216 transform.ConcatScale(scale, scale); |
| 217 |
| 218 double translate_x = 0, translate_y = 0; |
| 219 if (!dictionary_->GetDouble("translateX", &translate_x)) |
| 220 translate_x = 0; |
| 221 if (!dictionary_->GetDouble("translateY", &translate_y)) |
| 222 translate_y = 0; |
| 223 if (translate_x != 0 || translate_y != 0) |
| 224 transform.ConcatTranslate(translate_x, translate_y); |
| 225 |
| 226 layer->SetTransform(transform); |
| 227 } |
| 228 |
| 229 //////////////////////////////////////////////////////////////////////////////// |
| 230 // WmSetWindowBoundsFunction |
| 231 |
| 232 void WmSetWindowBoundsFunction::WorkOnLayer(ui::Layer* layer) { |
| 233 int x, y, width, height; |
| 234 gfx::Rect bounds = layer->GetTargetBounds(); |
| 235 |
| 236 if (dictionary_->GetInteger("x", &x)) |
| 237 bounds.set_x(x); |
| 238 if (dictionary_->GetInteger("y", &y)) |
| 239 bounds.set_y(y); |
| 240 if (dictionary_->GetInteger("width", &width)) |
| 241 bounds.set_width(width); |
| 242 if (dictionary_->GetInteger("height", &height)) |
| 243 bounds.set_height(height); |
| 244 |
| 245 // TODO(sad): Note that if the window is maximized/fullscreen, this has really |
| 246 // nasty side-effects. Handle these cases gracefully. |
| 247 layer->SetBounds(bounds); |
| 248 } |
| 249 |
| 250 //////////////////////////////////////////////////////////////////////////////// |
| 251 // WmWindowsFunction |
| 252 |
| 253 bool WmWindowsFunction::RunImpl() { |
| 254 std::string list_order; |
| 255 std::vector<const aura::Window*> windows; |
| 256 if (args_->GetString(0, &list_order) && list_order == kWindowOrderActivity) { |
| 257 windows = wm::WindowIdTracker::GetInstance()->AllWindows(true); |
| 258 } else { |
| 259 windows = wm::WindowIdTracker::GetInstance()->AllWindows(false); |
| 260 } |
| 261 |
| 262 ListValue* windowlist = new ListValue; |
| 263 for (std::vector<const aura::Window*>::iterator iter = windows.begin(); |
| 264 iter != windows.end(); |
| 265 ++iter) { |
| 266 experimental_wm::AshWindow extension_window; |
| 267 wm::utils::AuraWindowToExtensionWindow(*iter, &extension_window); |
| 268 windowlist->Append( |
| 269 extension_window.ToValue()->DeepCopyWithoutEmptyChildren()); |
| 270 } |
| 271 SetResult(windowlist); |
| 272 return true; |
| 273 } |
| 274 |
| 275 //////////////////////////////////////////////////////////////////////////////// |
| 276 // WmScreenBoundsFunction |
| 277 |
| 278 bool WmScreenBoundsFunction::RunImpl() { |
| 279 base::DictionaryValue* dict = new base::DictionaryValue; |
| 280 gfx::Rect bounds = gfx::Screen::GetPrimaryDisplay().bounds(); |
| 281 dict->SetInteger("x", bounds.x()); |
| 282 dict->SetInteger("y", bounds.y()); |
| 283 dict->SetInteger("width", bounds.width()); |
| 284 dict->SetInteger("height", bounds.height()); |
| 285 SetResult(dict); |
| 286 return true; |
| 287 } |
| 288 |
| 289 //////////////////////////////////////////////////////////////////////////////// |
| 290 // WmStackWindowAboveFunction |
| 291 |
| 292 bool WmStackWindowAboveFunction::RunImpl() { |
| 293 int above_window_id, below_window_id; |
| 294 if (!args_->GetInteger(0, &above_window_id) || |
| 295 !args_->GetInteger(1, &below_window_id)) |
| 296 return false; |
| 297 |
| 298 aura::Window* above_window = const_cast<aura::Window*>( |
| 299 wm::WindowIdTracker::GetInstance()->ExtensionIdWindow(above_window_id)); |
| 300 aura::Window* below_window = const_cast<aura::Window*>( |
| 301 wm::WindowIdTracker::GetInstance()->ExtensionIdWindow(below_window_id)); |
| 302 |
| 303 if (!above_window || !below_window || |
| 304 above_window->parent() != below_window->parent()) |
| 305 return false; |
| 306 |
| 307 above_window->parent()->StackChildAbove(above_window, below_window); |
| 308 return true; |
| 309 } |
| 310 |
| 311 } // namespace api |
| 312 } // namespace extensions |
| OLD | NEW |