| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/basictypes.h" | 5 #include "base/basictypes.h" |
| 6 #include "base/strings/string_util.h" | 6 #include "base/strings/string_util.h" |
| 7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "mojo/common/common_type_converters.h" | 8 #include "mojo/common/common_type_converters.h" |
| 9 #include "mojo/examples/window_manager/window_manager.mojom.h" | 9 #include "mojo/examples/window_manager/window_manager.mojom.h" |
| 10 #include "mojo/public/cpp/application/application_connection.h" | 10 #include "mojo/public/cpp/application/application_connection.h" |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 // TODO(sky): it would be nice if this were put in NativeWidgetViewManager, but | 63 // TODO(sky): it would be nice if this were put in NativeWidgetViewManager, but |
| 64 // that requires NativeWidgetViewManager to take an IWindowManager. That may be | 64 // that requires NativeWidgetViewManager to take an IWindowManager. That may be |
| 65 // desirable anyway... | 65 // desirable anyway... |
| 66 class KeyboardManager | 66 class KeyboardManager |
| 67 : public views::FocusChangeListener, | 67 : public views::FocusChangeListener, |
| 68 public ui::EventHandler, | 68 public ui::EventHandler, |
| 69 public views::WidgetObserver { | 69 public views::WidgetObserver { |
| 70 public: | 70 public: |
| 71 KeyboardManager(views::Widget* widget, | 71 KeyboardManager(views::Widget* widget, |
| 72 IWindowManager* window_manager, | 72 IWindowManager* window_manager, |
| 73 view_manager::Node* node) | 73 Node* node) |
| 74 : widget_(widget), | 74 : widget_(widget), |
| 75 window_manager_(window_manager), | 75 window_manager_(window_manager), |
| 76 node_(node), | 76 node_(node), |
| 77 last_view_id_(0), | 77 last_view_id_(0), |
| 78 focused_view_(NULL) { | 78 focused_view_(NULL) { |
| 79 widget_->GetFocusManager()->AddFocusChangeListener(this); | 79 widget_->GetFocusManager()->AddFocusChangeListener(this); |
| 80 widget_->AddObserver(this); | 80 widget_->AddObserver(this); |
| 81 widget_->GetNativeView()->AddPostTargetHandler(this); | 81 widget_->GetNativeView()->AddPostTargetHandler(this); |
| 82 } | 82 } |
| 83 | 83 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 } | 132 } |
| 133 } | 133 } |
| 134 | 134 |
| 135 // views::WidgetObserver: | 135 // views::WidgetObserver: |
| 136 virtual void OnWidgetDestroying(views::Widget* widget) OVERRIDE { | 136 virtual void OnWidgetDestroying(views::Widget* widget) OVERRIDE { |
| 137 delete this; | 137 delete this; |
| 138 } | 138 } |
| 139 | 139 |
| 140 views::Widget* widget_; | 140 views::Widget* widget_; |
| 141 IWindowManager* window_manager_; | 141 IWindowManager* window_manager_; |
| 142 view_manager::Node* node_; | 142 Node* node_; |
| 143 view_manager::Id last_view_id_; | 143 Id last_view_id_; |
| 144 views::View* focused_view_; | 144 views::View* focused_view_; |
| 145 | 145 |
| 146 DISALLOW_COPY_AND_ASSIGN(KeyboardManager); | 146 DISALLOW_COPY_AND_ASSIGN(KeyboardManager); |
| 147 }; | 147 }; |
| 148 | 148 |
| 149 // This is the basics of creating a views widget with a textfield. | 149 // This is the basics of creating a views widget with a textfield. |
| 150 // TODO: cleanup! | 150 // TODO: cleanup! |
| 151 class Browser : public ApplicationDelegate, | 151 class Browser : public ApplicationDelegate, |
| 152 public view_manager::ViewManagerDelegate, | 152 public ViewManagerDelegate, |
| 153 public views::TextfieldController, | 153 public views::TextfieldController, |
| 154 public view_manager::NodeObserver { | 154 public NodeObserver { |
| 155 public: | 155 public: |
| 156 Browser() | 156 Browser() |
| 157 : view_manager_(NULL), | 157 : view_manager_(NULL), |
| 158 view_manager_client_factory_(this), | 158 view_manager_client_factory_(this), |
| 159 root_(NULL), | 159 root_(NULL), |
| 160 widget_(NULL) {} | 160 widget_(NULL) {} |
| 161 | 161 |
| 162 virtual ~Browser() { | 162 virtual ~Browser() { |
| 163 if (root_) | 163 if (root_) |
| 164 root_->RemoveObserver(this); | 164 root_->RemoveObserver(this); |
| 165 } | 165 } |
| 166 | 166 |
| 167 private: | 167 private: |
| 168 // Overridden from ApplicationDelegate: | 168 // Overridden from ApplicationDelegate: |
| 169 virtual void Initialize(ApplicationImpl* app) MOJO_OVERRIDE { | 169 virtual void Initialize(ApplicationImpl* app) MOJO_OVERRIDE { |
| 170 views_init_.reset(new ViewsInit); | 170 views_init_.reset(new ViewsInit); |
| 171 app->ConnectToService("mojo:mojo_window_manager", &navigator_host_); | 171 app->ConnectToService("mojo:mojo_window_manager", &navigator_host_); |
| 172 app->ConnectToService("mojo:mojo_window_manager", &window_manager_); | 172 app->ConnectToService("mojo:mojo_window_manager", &window_manager_); |
| 173 } | 173 } |
| 174 | 174 |
| 175 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection) | 175 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection) |
| 176 MOJO_OVERRIDE { | 176 MOJO_OVERRIDE { |
| 177 connection->AddService(&view_manager_client_factory_); | 177 connection->AddService(&view_manager_client_factory_); |
| 178 return true; | 178 return true; |
| 179 } | 179 } |
| 180 | 180 |
| 181 void CreateWidget(view_manager::Node* node) { | 181 void CreateWidget(Node* node) { |
| 182 views::Textfield* textfield = new views::Textfield; | 182 views::Textfield* textfield = new views::Textfield; |
| 183 textfield->set_controller(this); | 183 textfield->set_controller(this); |
| 184 | 184 |
| 185 views::WidgetDelegateView* widget_delegate = new views::WidgetDelegateView; | 185 views::WidgetDelegateView* widget_delegate = new views::WidgetDelegateView; |
| 186 widget_delegate->GetContentsView()->set_background( | 186 widget_delegate->GetContentsView()->set_background( |
| 187 views::Background::CreateSolidBackground(SK_ColorBLUE)); | 187 views::Background::CreateSolidBackground(SK_ColorBLUE)); |
| 188 widget_delegate->GetContentsView()->AddChildView(textfield); | 188 widget_delegate->GetContentsView()->AddChildView(textfield); |
| 189 widget_delegate->GetContentsView()->SetLayoutManager( | 189 widget_delegate->GetContentsView()->SetLayoutManager( |
| 190 new BrowserLayoutManager); | 190 new BrowserLayoutManager); |
| 191 | 191 |
| 192 widget_ = new views::Widget; | 192 widget_ = new views::Widget; |
| 193 views::Widget::InitParams params( | 193 views::Widget::InitParams params( |
| 194 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); | 194 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); |
| 195 params.native_widget = new NativeWidgetViewManager(widget_, node); | 195 params.native_widget = new NativeWidgetViewManager(widget_, node); |
| 196 params.delegate = widget_delegate; | 196 params.delegate = widget_delegate; |
| 197 params.bounds = gfx::Rect(node->bounds().width(), node->bounds().height()); | 197 params.bounds = gfx::Rect(node->bounds().width(), node->bounds().height()); |
| 198 widget_->Init(params); | 198 widget_->Init(params); |
| 199 // KeyboardManager handles deleting itself when the widget is destroyed. | 199 // KeyboardManager handles deleting itself when the widget is destroyed. |
| 200 new KeyboardManager(widget_, window_manager_.get(), node); | 200 new KeyboardManager(widget_, window_manager_.get(), node); |
| 201 widget_->Show(); | 201 widget_->Show(); |
| 202 textfield->RequestFocus(); | 202 textfield->RequestFocus(); |
| 203 } | 203 } |
| 204 | 204 |
| 205 // view_manager::ViewManagerDelegate: | 205 // ViewManagerDelegate: |
| 206 virtual void OnRootAdded(view_manager::ViewManager* view_manager, | 206 virtual void OnRootAdded(ViewManager* view_manager, Node* root) OVERRIDE { |
| 207 view_manager::Node* root) OVERRIDE { | |
| 208 // TODO: deal with OnRootAdded() being invoked multiple times. | 207 // TODO: deal with OnRootAdded() being invoked multiple times. |
| 209 view_manager_ = view_manager; | 208 view_manager_ = view_manager; |
| 210 root_ = root; | 209 root_ = root; |
| 211 root_->AddObserver(this); | 210 root_->AddObserver(this); |
| 212 root_->SetActiveView(view_manager::View::Create(view_manager)); | 211 root_->SetActiveView(View::Create(view_manager)); |
| 213 root_->SetFocus(); | 212 root_->SetFocus(); |
| 214 CreateWidget(root_); | 213 CreateWidget(root_); |
| 215 } | 214 } |
| 216 virtual void OnViewManagerDisconnected( | 215 virtual void OnViewManagerDisconnected( |
| 217 view_manager::ViewManager* view_manager) OVERRIDE { | 216 ViewManager* view_manager) OVERRIDE { |
| 218 DCHECK_EQ(view_manager_, view_manager); | 217 DCHECK_EQ(view_manager_, view_manager); |
| 219 view_manager_ = NULL; | 218 view_manager_ = NULL; |
| 220 base::MessageLoop::current()->Quit(); | 219 base::MessageLoop::current()->Quit(); |
| 221 } | 220 } |
| 222 | 221 |
| 223 // views::TextfieldController: | 222 // views::TextfieldController: |
| 224 virtual bool HandleKeyEvent(views::Textfield* sender, | 223 virtual bool HandleKeyEvent(views::Textfield* sender, |
| 225 const ui::KeyEvent& key_event) OVERRIDE { | 224 const ui::KeyEvent& key_event) OVERRIDE { |
| 226 if (key_event.key_code() == ui::VKEY_RETURN) { | 225 if (key_event.key_code() == ui::VKEY_RETURN) { |
| 227 GURL url(sender->text()); | 226 GURL url(sender->text()); |
| 228 printf("User entered this URL: %s\n", url.spec().c_str()); | 227 printf("User entered this URL: %s\n", url.spec().c_str()); |
| 229 navigation::NavigationDetailsPtr nav_details( | 228 navigation::NavigationDetailsPtr nav_details( |
| 230 navigation::NavigationDetails::New()); | 229 navigation::NavigationDetails::New()); |
| 231 nav_details->url = String::From(url); | 230 nav_details->url = String::From(url); |
| 232 navigator_host_->RequestNavigate(view_manager_->GetRoots().front()->id(), | 231 navigator_host_->RequestNavigate(view_manager_->GetRoots().front()->id(), |
| 233 navigation::TARGET_NEW_NODE, | 232 navigation::TARGET_NEW_NODE, |
| 234 nav_details.Pass()); | 233 nav_details.Pass()); |
| 235 } | 234 } |
| 236 return false; | 235 return false; |
| 237 } | 236 } |
| 238 | 237 |
| 239 // NodeObserver: | 238 // NodeObserver: |
| 240 virtual void OnNodeFocusChanged(view_manager::Node* gained_focus, | 239 virtual void OnNodeFocusChanged(Node* gained_focus, |
| 241 view_manager::Node* lost_focus) OVERRIDE { | 240 Node* lost_focus) OVERRIDE { |
| 242 aura::client::FocusClient* focus_client = | 241 aura::client::FocusClient* focus_client = |
| 243 aura::client::GetFocusClient(widget_->GetNativeView()); | 242 aura::client::GetFocusClient(widget_->GetNativeView()); |
| 244 if (lost_focus == root_) | 243 if (lost_focus == root_) |
| 245 focus_client->FocusWindow(NULL); | 244 focus_client->FocusWindow(NULL); |
| 246 else if (gained_focus == root_) | 245 else if (gained_focus == root_) |
| 247 focus_client->FocusWindow(widget_->GetNativeView()); | 246 focus_client->FocusWindow(widget_->GetNativeView()); |
| 248 } | 247 } |
| 249 virtual void OnNodeDestroyed(view_manager::Node* node) OVERRIDE { | 248 virtual void OnNodeDestroyed(Node* node) OVERRIDE { |
| 250 DCHECK_EQ(root_, node); | 249 DCHECK_EQ(root_, node); |
| 251 node->RemoveObserver(this); | 250 node->RemoveObserver(this); |
| 252 root_ = NULL; | 251 root_ = NULL; |
| 253 } | 252 } |
| 254 | 253 |
| 255 scoped_ptr<ViewsInit> views_init_; | 254 scoped_ptr<ViewsInit> views_init_; |
| 256 | 255 |
| 257 view_manager::ViewManager* view_manager_; | 256 ViewManager* view_manager_; |
| 258 view_manager::ViewManagerClientFactory view_manager_client_factory_; | 257 ViewManagerClientFactory view_manager_client_factory_; |
| 259 view_manager::Node* root_; | 258 Node* root_; |
| 260 views::Widget* widget_; | 259 views::Widget* widget_; |
| 261 navigation::NavigatorHostPtr navigator_host_; | 260 navigation::NavigatorHostPtr navigator_host_; |
| 262 IWindowManagerPtr window_manager_; | 261 IWindowManagerPtr window_manager_; |
| 263 | 262 |
| 264 DISALLOW_COPY_AND_ASSIGN(Browser); | 263 DISALLOW_COPY_AND_ASSIGN(Browser); |
| 265 }; | 264 }; |
| 266 | 265 |
| 267 } // namespace examples | 266 } // namespace examples |
| 268 | 267 |
| 269 // static | 268 // static |
| 270 ApplicationDelegate* ApplicationDelegate::Create() { | 269 ApplicationDelegate* ApplicationDelegate::Create() { |
| 271 return new examples::Browser; | 270 return new examples::Browser; |
| 272 } | 271 } |
| 273 | 272 |
| 274 } // namespace mojo | 273 } // namespace mojo |
| OLD | NEW |