Chromium Code Reviews| 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" | |
| 7 #include "base/strings/utf_string_conversions.h" | |
| 6 #include "mojo/public/cpp/application/application.h" | 8 #include "mojo/public/cpp/application/application.h" |
| 7 #include "mojo/services/navigation/navigation.mojom.h" | 9 #include "mojo/services/navigation/navigation.mojom.h" |
| 8 #include "mojo/services/public/cpp/view_manager/node.h" | 10 #include "mojo/services/public/cpp/view_manager/node.h" |
| 9 #include "mojo/services/public/cpp/view_manager/view.h" | 11 #include "mojo/services/public/cpp/view_manager/view.h" |
| 10 #include "mojo/services/public/cpp/view_manager/view_manager.h" | 12 #include "mojo/services/public/cpp/view_manager/view_manager.h" |
| 11 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" | 13 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" |
| 12 #include "mojo/services/public/cpp/view_manager/view_observer.h" | 14 #include "mojo/services/public/cpp/view_manager/view_observer.h" |
| 13 #include "mojo/services/public/interfaces/launcher/launcher.mojom.h" | |
| 14 #include "mojo/views/native_widget_view_manager.h" | 15 #include "mojo/views/native_widget_view_manager.h" |
| 15 #include "mojo/views/views_init.h" | 16 #include "mojo/views/views_init.h" |
| 17 #include "ui/events/event.h" | |
| 16 #include "ui/views/controls/textfield/textfield.h" | 18 #include "ui/views/controls/textfield/textfield.h" |
| 17 #include "ui/views/controls/textfield/textfield_controller.h" | 19 #include "ui/views/controls/textfield/textfield_controller.h" |
| 18 #include "ui/views/layout/layout_manager.h" | 20 #include "ui/views/layout/layout_manager.h" |
| 19 #include "ui/views/widget/widget.h" | 21 #include "ui/views/widget/widget.h" |
| 20 #include "ui/views/widget/widget_delegate.h" | 22 #include "ui/views/widget/widget_delegate.h" |
| 21 #include "url/gurl.h" | 23 #include "url/gurl.h" |
| 22 | 24 |
| 23 namespace mojo { | 25 namespace mojo { |
| 24 namespace examples { | 26 namespace examples { |
| 25 | 27 |
| 26 class NodeView : public views::View { | 28 class NodeView : public views::View { |
|
Ben Goodger (Google)
2014/06/19 02:48:44
i think you can delete this class now too
Aaron Boodman
2014/06/19 06:20:07
Done.
| |
| 27 public: | 29 public: |
| 28 explicit NodeView(view_manager::Node* node) : node_(node) { | 30 explicit NodeView(view_manager::Node* node) : node_(node) { |
| 29 // This class is provisional and assumes that the node has already been | 31 // This class is provisional and assumes that the node has already been |
| 30 // added to a parent. I suspect we'll want to make an improved version of | 32 // added to a parent. I suspect we'll want to make an improved version of |
| 31 // this that lives in ui/views akin to NativeViewHost that properly | 33 // this that lives in ui/views akin to NativeViewHost that properly |
| 32 // attaches/detaches when the view is. | 34 // attaches/detaches when the view is. |
| 33 DCHECK(node->parent()); | 35 DCHECK(node->parent()); |
| 34 } | 36 } |
| 35 virtual ~NodeView() {} | 37 virtual ~NodeView() {} |
| 36 | 38 |
| 37 // Overridden from views::View: | 39 // Overridden from views::View: |
| 38 virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE { | 40 virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE { |
| 39 node_->SetBounds(ConvertRectToWidget(GetLocalBounds())); | 41 node_->SetBounds(ConvertRectToWidget(GetLocalBounds())); |
| 40 } | 42 } |
| 41 | 43 |
| 42 private: | 44 private: |
| 43 view_manager::Node* node_; | 45 view_manager::Node* node_; |
| 44 | 46 |
| 45 DISALLOW_COPY_AND_ASSIGN(NodeView); | 47 DISALLOW_COPY_AND_ASSIGN(NodeView); |
| 46 }; | 48 }; |
| 47 | 49 |
| 48 class BrowserLayoutManager : public views::LayoutManager { | 50 class BrowserLayoutManager : public views::LayoutManager { |
| 49 public: | 51 public: |
| 50 BrowserLayoutManager() {} | 52 BrowserLayoutManager() {} |
| 51 virtual ~BrowserLayoutManager() {} | 53 virtual ~BrowserLayoutManager() {} |
| 52 | 54 |
| 53 private: | 55 private: |
| 54 // Overridden from views::LayoutManager: | 56 // Overridden from views::LayoutManager: |
| 55 virtual void Layout(views::View* host) OVERRIDE { | 57 virtual void Layout(views::View* host) OVERRIDE { |
| 56 // Browser view has two children: | 58 // Browser view has one child, a text input field. |
| 57 // 1. text input field. | 59 DCHECK_EQ(1, host->child_count()); |
| 58 // 2. content view. | |
| 59 DCHECK_EQ(2, host->child_count()); | |
| 60 views::View* text_field = host->child_at(0); | 60 views::View* text_field = host->child_at(0); |
| 61 gfx::Size ps = text_field->GetPreferredSize(); | 61 gfx::Size ps = text_field->GetPreferredSize(); |
| 62 text_field->SetBoundsRect(gfx::Rect(host->width(), ps.height())); | 62 text_field->SetBoundsRect(gfx::Rect(host->width(), ps.height())); |
| 63 views::View* content_area = host->child_at(1); | |
| 64 content_area->SetBounds(0, text_field->bounds().bottom(), host->width(), | |
| 65 host->height() - text_field->bounds().bottom()); | |
| 66 } | 63 } |
| 67 virtual gfx::Size GetPreferredSize(const views::View* host) const OVERRIDE { | 64 virtual gfx::Size GetPreferredSize(const views::View* host) const OVERRIDE { |
| 68 return gfx::Size(); | 65 return gfx::Size(); |
| 69 } | 66 } |
| 70 | 67 |
| 71 DISALLOW_COPY_AND_ASSIGN(BrowserLayoutManager); | 68 DISALLOW_COPY_AND_ASSIGN(BrowserLayoutManager); |
| 72 }; | 69 }; |
| 73 | 70 |
| 74 // This is the basics of creating a views widget with a textfield. | 71 // This is the basics of creating a views widget with a textfield. |
| 75 // TODO: cleanup! | 72 // TODO: cleanup! |
| 76 class Browser : public Application, | 73 class Browser : public Application, |
| 77 public view_manager::ViewManagerDelegate, | 74 public view_manager::ViewManagerDelegate, |
| 78 public views::TextfieldController, | 75 public views::TextfieldController { |
| 79 public InterfaceImpl<launcher::LauncherClient> { | |
| 80 public: | 76 public: |
| 81 Browser() : view_manager_(NULL), view_(NULL), content_node_(NULL) {} | 77 Browser() : view_manager_(NULL), view_(NULL) {} |
| 82 | 78 |
| 83 virtual ~Browser() { | 79 virtual ~Browser() { |
| 84 } | 80 } |
| 85 | 81 |
| 86 private: | 82 private: |
| 87 // Overridden from Application: | 83 // Overridden from Application: |
| 88 virtual void Initialize() MOJO_OVERRIDE { | 84 virtual void Initialize() MOJO_OVERRIDE { |
| 89 views_init_.reset(new ViewsInit); | 85 views_init_.reset(new ViewsInit); |
| 90 view_manager::ViewManager::Create(this, this); | 86 view_manager::ViewManager::Create(this, this); |
| 91 ConnectTo("mojo:mojo_launcher", &launcher_); | 87 ConnectTo("mojo:mojo_window_manager", &navigator_host_); |
| 92 launcher_.set_client(this); | |
| 93 } | 88 } |
| 94 | 89 |
| 95 void CreateWidget(const gfx::Size& size) { | 90 void CreateWidget(const gfx::Size& size) { |
| 96 views::Textfield* textfield = new views::Textfield; | 91 views::Textfield* textfield = new views::Textfield; |
| 97 textfield->set_controller(this); | 92 textfield->set_controller(this); |
| 98 | 93 |
| 99 views::WidgetDelegateView* widget_delegate = new views::WidgetDelegateView; | 94 views::WidgetDelegateView* widget_delegate = new views::WidgetDelegateView; |
| 100 widget_delegate->GetContentsView()->AddChildView(textfield); | 95 widget_delegate->GetContentsView()->AddChildView(textfield); |
| 101 widget_delegate->GetContentsView()->AddChildView( | |
| 102 new NodeView(content_node_)); | |
| 103 widget_delegate->GetContentsView()->SetLayoutManager( | 96 widget_delegate->GetContentsView()->SetLayoutManager( |
| 104 new BrowserLayoutManager); | 97 new BrowserLayoutManager); |
| 105 | 98 |
| 106 views::Widget* widget = new views::Widget; | 99 views::Widget* widget = new views::Widget; |
| 107 views::Widget::InitParams params(views::Widget::InitParams::TYPE_WINDOW); | 100 views::Widget::InitParams params( |
| 101 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); | |
| 108 params.native_widget = new NativeWidgetViewManager(widget, view_); | 102 params.native_widget = new NativeWidgetViewManager(widget, view_); |
| 109 params.delegate = widget_delegate; | 103 params.delegate = widget_delegate; |
| 110 params.bounds = gfx::Rect(size.width(), size.height()); | 104 params.bounds = gfx::Rect(size.width(), size.height()); |
| 111 widget->Init(params); | 105 widget->Init(params); |
| 112 widget->Show(); | 106 widget->Show(); |
| 113 textfield->RequestFocus(); | 107 textfield->RequestFocus(); |
| 114 } | 108 } |
| 115 | 109 |
| 116 // view_manager::ViewManagerDelegate: | 110 // view_manager::ViewManagerDelegate: |
| 117 virtual void OnRootAdded(view_manager::ViewManager* view_manager, | 111 virtual void OnRootAdded(view_manager::ViewManager* view_manager, |
| 118 view_manager::Node* root) OVERRIDE { | 112 view_manager::Node* root) OVERRIDE { |
| 119 // TODO: deal with OnRootAdded() being invoked multiple times. | 113 // TODO: deal with OnRootAdded() being invoked multiple times. |
| 120 view_manager_ = view_manager; | 114 view_manager_ = view_manager; |
| 121 view_ = view_manager::View::Create(view_manager_); | 115 view_ = view_manager::View::Create(view_manager_); |
| 122 view_manager_->GetRoots().front()->SetActiveView(view_); | 116 view_manager_->GetRoots().front()->SetActiveView(view_); |
| 123 | |
| 124 content_node_ = view_manager::Node::Create(view_manager_); | |
| 125 root->AddChild(content_node_); | |
| 126 | |
| 127 root->SetFocus(); | 117 root->SetFocus(); |
| 128 | |
| 129 CreateWidget(root->bounds().size()); | 118 CreateWidget(root->bounds().size()); |
| 130 } | 119 } |
| 131 | 120 |
| 132 // views::TextfieldController: | 121 // views::TextfieldController: |
| 133 virtual bool HandleKeyEvent(views::Textfield* sender, | 122 virtual bool HandleKeyEvent(views::Textfield* sender, |
| 134 const ui::KeyEvent& key_event) OVERRIDE { | 123 const ui::KeyEvent& key_event) OVERRIDE { |
| 135 if (key_event.key_code() == ui::VKEY_RETURN) { | 124 if (key_event.key_code() == ui::VKEY_RETURN) { |
| 136 GURL url(sender->text()); | 125 GURL url(sender->text()); |
| 137 printf("User entered this URL: %s\n", url.spec().c_str()); | 126 printf("User entered this URL: %s\n", url.spec().c_str()); |
| 138 launcher_->Launch(url.spec()); | 127 navigation::NavigationDetailsPtr nav_details( |
| 128 navigation::NavigationDetails::New()); | |
| 129 nav_details->url = url.spec(); | |
| 130 navigator_host_->RequestNavigate(view_manager_->GetRoots().front()->id(), | |
|
Ben Goodger (Google)
2014/06/19 02:48:44
cool!
| |
| 131 nav_details.Pass()); | |
| 139 } | 132 } |
| 140 return false; | 133 return false; |
| 141 } | 134 } |
| 142 | 135 |
| 143 // launcher::LauncherClient: | 136 virtual void ContentsChanged(views::Textfield* sender, |
| 144 virtual void OnLaunch( | 137 const base::string16& new_contents) OVERRIDE { |
| 145 const String& handler_url, | 138 // Ghetto workaround for crbug.com/386256. |
|
Ben Goodger (Google)
2014/06/19 02:48:44
lol
| |
| 146 URLResponsePtr response, | 139 if (EndsWith(new_contents, base::ASCIIToUTF16("]"), false)) { |
| 147 ScopedDataPipeConsumerHandle response_body_stream) OVERRIDE { | 140 sender->SetText(new_contents.substr(0, new_contents.size() - 1)); |
| 148 content_node_->Embed(handler_url); | 141 HandleKeyEvent(sender, ui::KeyEvent(ui::ET_KEY_RELEASED, ui::VKEY_RETURN, |
| 149 | 142 0, false)); |
| 150 navigation::NavigationDetailsPtr details( | 143 } |
| 151 navigation::NavigationDetails::New()); | |
| 152 details->url = response->url; | |
| 153 details->response = response.Pass(); | |
| 154 details->response_body_stream = response_body_stream.Pass(); | |
| 155 | |
| 156 navigation::NavigatorPtr navigator; | |
| 157 ConnectTo(handler_url, &navigator); | |
| 158 navigator->Navigate(content_node_->id(), details.Pass()); | |
| 159 } | 144 } |
| 160 | 145 |
| 161 scoped_ptr<ViewsInit> views_init_; | 146 scoped_ptr<ViewsInit> views_init_; |
| 162 | 147 |
| 163 view_manager::ViewManager* view_manager_; | 148 view_manager::ViewManager* view_manager_; |
| 164 view_manager::View* view_; | 149 view_manager::View* view_; |
| 165 view_manager::Node* content_node_; | 150 navigation::NavigatorHostPtr navigator_host_; |
| 166 launcher::LauncherPtr launcher_; | |
| 167 | 151 |
| 168 DISALLOW_COPY_AND_ASSIGN(Browser); | 152 DISALLOW_COPY_AND_ASSIGN(Browser); |
| 169 }; | 153 }; |
| 170 | 154 |
| 171 } // namespace examples | 155 } // namespace examples |
| 172 | 156 |
| 173 // static | 157 // static |
| 174 Application* Application::Create() { | 158 Application* Application::Create() { |
| 175 return new examples::Browser; | 159 return new examples::Browser; |
| 176 } | 160 } |
| 177 | 161 |
| 178 } // namespace mojo | 162 } // namespace mojo |
| OLD | NEW |