| 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" |
| 11 #include "mojo/public/cpp/application/application_delegate.h" | 11 #include "mojo/public/cpp/application/application_delegate.h" |
| 12 #include "mojo/public/cpp/application/application_impl.h" | 12 #include "mojo/public/cpp/application/application_impl.h" |
| 13 #include "mojo/services/public/cpp/geometry/geometry_type_converters.h" | 13 #include "mojo/services/public/cpp/geometry/geometry_type_converters.h" |
| 14 #include "mojo/services/public/cpp/view_manager/node.h" | 14 #include "mojo/services/public/cpp/view_manager/view.h" |
| 15 #include "mojo/services/public/cpp/view_manager/view_manager.h" | 15 #include "mojo/services/public/cpp/view_manager/view_manager.h" |
| 16 #include "mojo/services/public/cpp/view_manager/view_manager_client_factory.h" | 16 #include "mojo/services/public/cpp/view_manager/view_manager_client_factory.h" |
| 17 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" | 17 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" |
| 18 #include "mojo/services/public/interfaces/navigation/navigation.mojom.h" | 18 #include "mojo/services/public/interfaces/navigation/navigation.mojom.h" |
| 19 #include "mojo/views/native_widget_view_manager.h" | 19 #include "mojo/views/native_widget_view_manager.h" |
| 20 #include "mojo/views/views_init.h" | 20 #include "mojo/views/views_init.h" |
| 21 #include "ui/aura/client/focus_client.h" | 21 #include "ui/aura/client/focus_client.h" |
| 22 #include "ui/aura/window.h" | 22 #include "ui/aura/window.h" |
| 23 #include "ui/events/event.h" | 23 #include "ui/events/event.h" |
| 24 #include "ui/views/background.h" | 24 #include "ui/views/background.h" |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 // TODO(sky): it would be nice if this were put in NativeWidgetViewManager, but | 61 // TODO(sky): it would be nice if this were put in NativeWidgetViewManager, but |
| 62 // that requires NativeWidgetViewManager to take an IWindowManager. That may be | 62 // that requires NativeWidgetViewManager to take an IWindowManager. That may be |
| 63 // desirable anyway... | 63 // desirable anyway... |
| 64 class KeyboardManager | 64 class KeyboardManager |
| 65 : public views::FocusChangeListener, | 65 : public views::FocusChangeListener, |
| 66 public ui::EventHandler, | 66 public ui::EventHandler, |
| 67 public views::WidgetObserver { | 67 public views::WidgetObserver { |
| 68 public: | 68 public: |
| 69 KeyboardManager(views::Widget* widget, | 69 KeyboardManager(views::Widget* widget, |
| 70 IWindowManager* window_manager, | 70 IWindowManager* window_manager, |
| 71 Node* node) | 71 View* view) |
| 72 : widget_(widget), | 72 : widget_(widget), |
| 73 window_manager_(window_manager), | 73 window_manager_(window_manager), |
| 74 node_(node), | 74 view_(view), |
| 75 last_node_id_(0), | 75 last_view_id_(0), |
| 76 focused_view_(NULL) { | 76 focused_view_(NULL) { |
| 77 widget_->GetFocusManager()->AddFocusChangeListener(this); | 77 widget_->GetFocusManager()->AddFocusChangeListener(this); |
| 78 widget_->AddObserver(this); | 78 widget_->AddObserver(this); |
| 79 widget_->GetNativeView()->AddPostTargetHandler(this); | 79 widget_->GetNativeView()->AddPostTargetHandler(this); |
| 80 } | 80 } |
| 81 | 81 |
| 82 private: | 82 private: |
| 83 virtual ~KeyboardManager() { | 83 virtual ~KeyboardManager() { |
| 84 widget_->GetFocusManager()->RemoveFocusChangeListener(this); | 84 widget_->GetFocusManager()->RemoveFocusChangeListener(this); |
| 85 widget_->GetNativeView()->RemovePostTargetHandler(this); | 85 widget_->GetNativeView()->RemovePostTargetHandler(this); |
| 86 widget_->RemoveObserver(this); | 86 widget_->RemoveObserver(this); |
| 87 | 87 |
| 88 HideKeyboard(); | 88 HideKeyboard(); |
| 89 } | 89 } |
| 90 | 90 |
| 91 void ShowKeyboard(views::View* view) { | 91 void ShowKeyboard(views::View* view) { |
| 92 if (focused_view_ == view) | 92 if (focused_view_ == view) |
| 93 return; | 93 return; |
| 94 | 94 |
| 95 const gfx::Rect bounds_in_widget = | 95 const gfx::Rect bounds_in_widget = |
| 96 view->ConvertRectToWidget(gfx::Rect(view->bounds().size())); | 96 view->ConvertRectToWidget(gfx::Rect(view->bounds().size())); |
| 97 last_node_id_ = node_->id(); | 97 last_view_id_ = view_->id(); |
| 98 window_manager_->ShowKeyboard(last_node_id_, | 98 window_manager_->ShowKeyboard(last_view_id_, |
| 99 Rect::From(bounds_in_widget)); | 99 Rect::From(bounds_in_widget)); |
| 100 // TODO(sky): listen for view to be removed. | 100 // TODO(sky): listen for view to be removed. |
| 101 focused_view_ = view; | 101 focused_view_ = view; |
| 102 } | 102 } |
| 103 | 103 |
| 104 void HideKeyboard() { | 104 void HideKeyboard() { |
| 105 if (!focused_view_) | 105 if (!focused_view_) |
| 106 return; | 106 return; |
| 107 | 107 |
| 108 window_manager_->HideKeyboard(last_node_id_); | 108 window_manager_->HideKeyboard(last_view_id_); |
| 109 last_node_id_ = 0; | 109 last_view_id_ = 0; |
| 110 focused_view_ = NULL; | 110 focused_view_ = NULL; |
| 111 } | 111 } |
| 112 | 112 |
| 113 // views::FocusChangeListener: | 113 // views::FocusChangeListener: |
| 114 virtual void OnWillChangeFocus(views::View* focused_before, | 114 virtual void OnWillChangeFocus(views::View* focused_before, |
| 115 views::View* focused_now) OVERRIDE { | 115 views::View* focused_now) OVERRIDE { |
| 116 } | 116 } |
| 117 virtual void OnDidChangeFocus(views::View* focused_before, | 117 virtual void OnDidChangeFocus(views::View* focused_before, |
| 118 views::View* focused_now) OVERRIDE { | 118 views::View* focused_now) OVERRIDE { |
| 119 if (focused_view_ && focused_now != focused_view_) | 119 if (focused_view_ && focused_now != focused_view_) |
| (...skipping 10 matching lines...) Expand all Loading... |
| 130 } | 130 } |
| 131 } | 131 } |
| 132 | 132 |
| 133 // views::WidgetObserver: | 133 // views::WidgetObserver: |
| 134 virtual void OnWidgetDestroying(views::Widget* widget) OVERRIDE { | 134 virtual void OnWidgetDestroying(views::Widget* widget) OVERRIDE { |
| 135 delete this; | 135 delete this; |
| 136 } | 136 } |
| 137 | 137 |
| 138 views::Widget* widget_; | 138 views::Widget* widget_; |
| 139 IWindowManager* window_manager_; | 139 IWindowManager* window_manager_; |
| 140 Node* node_; | 140 View* view_; |
| 141 Id last_node_id_; | 141 Id last_view_id_; |
| 142 views::View* focused_view_; | 142 views::View* focused_view_; |
| 143 | 143 |
| 144 DISALLOW_COPY_AND_ASSIGN(KeyboardManager); | 144 DISALLOW_COPY_AND_ASSIGN(KeyboardManager); |
| 145 }; | 145 }; |
| 146 | 146 |
| 147 // This is the basics of creating a views widget with a textfield. | 147 // This is the basics of creating a views widget with a textfield. |
| 148 // TODO: cleanup! | 148 // TODO: cleanup! |
| 149 class Browser : public ApplicationDelegate, | 149 class Browser : public ApplicationDelegate, |
| 150 public ViewManagerDelegate, | 150 public ViewManagerDelegate, |
| 151 public views::TextfieldController, | 151 public views::TextfieldController, |
| 152 public NodeObserver { | 152 public ViewObserver { |
| 153 public: | 153 public: |
| 154 Browser() | 154 Browser() |
| 155 : view_manager_(NULL), | 155 : view_manager_(NULL), |
| 156 view_manager_client_factory_(this), | 156 view_manager_client_factory_(this), |
| 157 root_(NULL), | 157 root_(NULL), |
| 158 widget_(NULL) {} | 158 widget_(NULL) {} |
| 159 | 159 |
| 160 virtual ~Browser() { | 160 virtual ~Browser() { |
| 161 if (root_) | 161 if (root_) |
| 162 root_->RemoveObserver(this); | 162 root_->RemoveObserver(this); |
| 163 } | 163 } |
| 164 | 164 |
| 165 private: | 165 private: |
| 166 // Overridden from ApplicationDelegate: | 166 // Overridden from ApplicationDelegate: |
| 167 virtual void Initialize(ApplicationImpl* app) MOJO_OVERRIDE { | 167 virtual void Initialize(ApplicationImpl* app) MOJO_OVERRIDE { |
| 168 views_init_.reset(new ViewsInit); | 168 views_init_.reset(new ViewsInit); |
| 169 app->ConnectToService("mojo:mojo_window_manager", &navigator_host_); | 169 app->ConnectToService("mojo:mojo_window_manager", &navigator_host_); |
| 170 app->ConnectToService("mojo:mojo_window_manager", &window_manager_); | 170 app->ConnectToService("mojo:mojo_window_manager", &window_manager_); |
| 171 } | 171 } |
| 172 | 172 |
| 173 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection) | 173 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection) |
| 174 MOJO_OVERRIDE { | 174 MOJO_OVERRIDE { |
| 175 connection->AddService(&view_manager_client_factory_); | 175 connection->AddService(&view_manager_client_factory_); |
| 176 return true; | 176 return true; |
| 177 } | 177 } |
| 178 | 178 |
| 179 void CreateWidget(Node* node) { | 179 void CreateWidget(View* view) { |
| 180 views::Textfield* textfield = new views::Textfield; | 180 views::Textfield* textfield = new views::Textfield; |
| 181 textfield->set_controller(this); | 181 textfield->set_controller(this); |
| 182 | 182 |
| 183 views::WidgetDelegateView* widget_delegate = new views::WidgetDelegateView; | 183 views::WidgetDelegateView* widget_delegate = new views::WidgetDelegateView; |
| 184 widget_delegate->GetContentsView()->set_background( | 184 widget_delegate->GetContentsView()->set_background( |
| 185 views::Background::CreateSolidBackground(SK_ColorBLUE)); | 185 views::Background::CreateSolidBackground(SK_ColorBLUE)); |
| 186 widget_delegate->GetContentsView()->AddChildView(textfield); | 186 widget_delegate->GetContentsView()->AddChildView(textfield); |
| 187 widget_delegate->GetContentsView()->SetLayoutManager( | 187 widget_delegate->GetContentsView()->SetLayoutManager( |
| 188 new BrowserLayoutManager); | 188 new BrowserLayoutManager); |
| 189 | 189 |
| 190 widget_ = new views::Widget; | 190 widget_ = new views::Widget; |
| 191 views::Widget::InitParams params( | 191 views::Widget::InitParams params( |
| 192 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); | 192 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); |
| 193 params.native_widget = new NativeWidgetViewManager(widget_, node); | 193 params.native_widget = new NativeWidgetViewManager(widget_, view); |
| 194 params.delegate = widget_delegate; | 194 params.delegate = widget_delegate; |
| 195 params.bounds = gfx::Rect(node->bounds().width(), node->bounds().height()); | 195 params.bounds = gfx::Rect(view->bounds().width(), view->bounds().height()); |
| 196 widget_->Init(params); | 196 widget_->Init(params); |
| 197 // KeyboardManager handles deleting itself when the widget is destroyed. | 197 // KeyboardManager handles deleting itself when the widget is destroyed. |
| 198 new KeyboardManager(widget_, window_manager_.get(), node); | 198 new KeyboardManager(widget_, window_manager_.get(), view); |
| 199 widget_->Show(); | 199 widget_->Show(); |
| 200 textfield->RequestFocus(); | 200 textfield->RequestFocus(); |
| 201 } | 201 } |
| 202 | 202 |
| 203 // ViewManagerDelegate: | 203 // ViewManagerDelegate: |
| 204 virtual void OnEmbed(ViewManager* view_manager, | 204 virtual void OnEmbed(ViewManager* view_manager, |
| 205 Node* root, | 205 View* root, |
| 206 ServiceProviderImpl* exported_services, | 206 ServiceProviderImpl* exported_services, |
| 207 scoped_ptr<ServiceProvider> imported_services) OVERRIDE { | 207 scoped_ptr<ServiceProvider> imported_services) OVERRIDE { |
| 208 // TODO: deal with OnEmbed() being invoked multiple times. | 208 // TODO: deal with OnEmbed() being invoked multiple times. |
| 209 view_manager_ = view_manager; | 209 view_manager_ = view_manager; |
| 210 root_ = root; | 210 root_ = root; |
| 211 root_->AddObserver(this); | 211 root_->AddObserver(this); |
| 212 root_->SetFocus(); | 212 root_->SetFocus(); |
| 213 CreateWidget(root_); | 213 CreateWidget(root_); |
| 214 } | 214 } |
| 215 virtual void OnViewManagerDisconnected( | 215 virtual void OnViewManagerDisconnected( |
| (...skipping 11 matching lines...) Expand all Loading... |
| 227 printf("User entered this URL: %s\n", url.spec().c_str()); | 227 printf("User entered this URL: %s\n", url.spec().c_str()); |
| 228 NavigationDetailsPtr nav_details(NavigationDetails::New()); | 228 NavigationDetailsPtr nav_details(NavigationDetails::New()); |
| 229 nav_details->request->url = String::From(url); | 229 nav_details->request->url = String::From(url); |
| 230 navigator_host_->RequestNavigate(view_manager_->GetRoots().front()->id(), | 230 navigator_host_->RequestNavigate(view_manager_->GetRoots().front()->id(), |
| 231 TARGET_NEW_NODE, | 231 TARGET_NEW_NODE, |
| 232 nav_details.Pass()); | 232 nav_details.Pass()); |
| 233 } | 233 } |
| 234 return false; | 234 return false; |
| 235 } | 235 } |
| 236 | 236 |
| 237 // NodeObserver: | 237 // ViewObserver: |
| 238 virtual void OnNodeFocusChanged(Node* gained_focus, | 238 virtual void OnViewFocusChanged(View* gained_focus, |
| 239 Node* lost_focus) OVERRIDE { | 239 View* lost_focus) OVERRIDE { |
| 240 aura::client::FocusClient* focus_client = | 240 aura::client::FocusClient* focus_client = |
| 241 aura::client::GetFocusClient(widget_->GetNativeView()); | 241 aura::client::GetFocusClient(widget_->GetNativeView()); |
| 242 if (lost_focus == root_) | 242 if (lost_focus == root_) |
| 243 focus_client->FocusWindow(NULL); | 243 focus_client->FocusWindow(NULL); |
| 244 else if (gained_focus == root_) | 244 else if (gained_focus == root_) |
| 245 focus_client->FocusWindow(widget_->GetNativeView()); | 245 focus_client->FocusWindow(widget_->GetNativeView()); |
| 246 } | 246 } |
| 247 virtual void OnNodeDestroyed(Node* node) OVERRIDE { | 247 virtual void OnViewDestroyed(View* view) OVERRIDE { |
| 248 DCHECK_EQ(root_, node); | 248 DCHECK_EQ(root_, view); |
| 249 node->RemoveObserver(this); | 249 view->RemoveObserver(this); |
| 250 root_ = NULL; | 250 root_ = NULL; |
| 251 } | 251 } |
| 252 | 252 |
| 253 scoped_ptr<ViewsInit> views_init_; | 253 scoped_ptr<ViewsInit> views_init_; |
| 254 | 254 |
| 255 ViewManager* view_manager_; | 255 ViewManager* view_manager_; |
| 256 ViewManagerClientFactory view_manager_client_factory_; | 256 ViewManagerClientFactory view_manager_client_factory_; |
| 257 Node* root_; | 257 View* root_; |
| 258 views::Widget* widget_; | 258 views::Widget* widget_; |
| 259 NavigatorHostPtr navigator_host_; | 259 NavigatorHostPtr navigator_host_; |
| 260 IWindowManagerPtr window_manager_; | 260 IWindowManagerPtr window_manager_; |
| 261 | 261 |
| 262 DISALLOW_COPY_AND_ASSIGN(Browser); | 262 DISALLOW_COPY_AND_ASSIGN(Browser); |
| 263 }; | 263 }; |
| 264 | 264 |
| 265 } // namespace examples | 265 } // namespace examples |
| 266 | 266 |
| 267 // static | 267 // static |
| 268 ApplicationDelegate* ApplicationDelegate::Create() { | 268 ApplicationDelegate* ApplicationDelegate::Create() { |
| 269 return new examples::Browser; | 269 return new examples::Browser; |
| 270 } | 270 } |
| 271 | 271 |
| 272 } // namespace mojo | 272 } // namespace mojo |
| OLD | NEW |