| 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 { | |
| 27 public: | |
| 28 explicit NodeView(view_manager::Node* node) : node_(node) { | |
| 29 // 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 | |
| 31 // this that lives in ui/views akin to NativeViewHost that properly | |
| 32 // attaches/detaches when the view is. | |
| 33 DCHECK(node->parent()); | |
| 34 } | |
| 35 virtual ~NodeView() {} | |
| 36 | |
| 37 // Overridden from views::View: | |
| 38 virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE { | |
| 39 node_->SetBounds(ConvertRectToWidget(GetLocalBounds())); | |
| 40 } | |
| 41 | |
| 42 private: | |
| 43 view_manager::Node* node_; | |
| 44 | |
| 45 DISALLOW_COPY_AND_ASSIGN(NodeView); | |
| 46 }; | |
| 47 | |
| 48 class BrowserLayoutManager : public views::LayoutManager { | 28 class BrowserLayoutManager : public views::LayoutManager { |
| 49 public: | 29 public: |
| 50 BrowserLayoutManager() {} | 30 BrowserLayoutManager() {} |
| 51 virtual ~BrowserLayoutManager() {} | 31 virtual ~BrowserLayoutManager() {} |
| 52 | 32 |
| 53 private: | 33 private: |
| 54 // Overridden from views::LayoutManager: | 34 // Overridden from views::LayoutManager: |
| 55 virtual void Layout(views::View* host) OVERRIDE { | 35 virtual void Layout(views::View* host) OVERRIDE { |
| 56 // Browser view has two children: | 36 // Browser view has one child, a text input field. |
| 57 // 1. text input field. | 37 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); | 38 views::View* text_field = host->child_at(0); |
| 61 gfx::Size ps = text_field->GetPreferredSize(); | 39 gfx::Size ps = text_field->GetPreferredSize(); |
| 62 text_field->SetBoundsRect(gfx::Rect(host->width(), ps.height())); | 40 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 } | 41 } |
| 67 virtual gfx::Size GetPreferredSize(const views::View* host) const OVERRIDE { | 42 virtual gfx::Size GetPreferredSize(const views::View* host) const OVERRIDE { |
| 68 return gfx::Size(); | 43 return gfx::Size(); |
| 69 } | 44 } |
| 70 | 45 |
| 71 DISALLOW_COPY_AND_ASSIGN(BrowserLayoutManager); | 46 DISALLOW_COPY_AND_ASSIGN(BrowserLayoutManager); |
| 72 }; | 47 }; |
| 73 | 48 |
| 74 // This is the basics of creating a views widget with a textfield. | 49 // This is the basics of creating a views widget with a textfield. |
| 75 // TODO: cleanup! | 50 // TODO: cleanup! |
| 76 class Browser : public Application, | 51 class Browser : public Application, |
| 77 public view_manager::ViewManagerDelegate, | 52 public view_manager::ViewManagerDelegate, |
| 78 public views::TextfieldController, | 53 public views::TextfieldController { |
| 79 public InterfaceImpl<launcher::LauncherClient> { | |
| 80 public: | 54 public: |
| 81 Browser() : view_manager_(NULL), view_(NULL), content_node_(NULL) {} | 55 Browser() : view_manager_(NULL), view_(NULL) {} |
| 82 | 56 |
| 83 virtual ~Browser() { | 57 virtual ~Browser() { |
| 84 } | 58 } |
| 85 | 59 |
| 86 private: | 60 private: |
| 87 // Overridden from Application: | 61 // Overridden from Application: |
| 88 virtual void Initialize() MOJO_OVERRIDE { | 62 virtual void Initialize() MOJO_OVERRIDE { |
| 89 views_init_.reset(new ViewsInit); | 63 views_init_.reset(new ViewsInit); |
| 90 view_manager::ViewManager::Create(this, this); | 64 view_manager::ViewManager::Create(this, this); |
| 91 ConnectTo("mojo:mojo_launcher", &launcher_); | 65 ConnectTo("mojo:mojo_window_manager", &navigator_host_); |
| 92 launcher_.set_client(this); | |
| 93 } | 66 } |
| 94 | 67 |
| 95 void CreateWidget(const gfx::Size& size) { | 68 void CreateWidget(const gfx::Size& size) { |
| 96 views::Textfield* textfield = new views::Textfield; | 69 views::Textfield* textfield = new views::Textfield; |
| 97 textfield->set_controller(this); | 70 textfield->set_controller(this); |
| 98 | 71 |
| 99 views::WidgetDelegateView* widget_delegate = new views::WidgetDelegateView; | 72 views::WidgetDelegateView* widget_delegate = new views::WidgetDelegateView; |
| 100 widget_delegate->GetContentsView()->AddChildView(textfield); | 73 widget_delegate->GetContentsView()->AddChildView(textfield); |
| 101 widget_delegate->GetContentsView()->AddChildView( | |
| 102 new NodeView(content_node_)); | |
| 103 widget_delegate->GetContentsView()->SetLayoutManager( | 74 widget_delegate->GetContentsView()->SetLayoutManager( |
| 104 new BrowserLayoutManager); | 75 new BrowserLayoutManager); |
| 105 | 76 |
| 106 views::Widget* widget = new views::Widget; | 77 views::Widget* widget = new views::Widget; |
| 107 views::Widget::InitParams params(views::Widget::InitParams::TYPE_WINDOW); | 78 views::Widget::InitParams params( |
| 79 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); |
| 108 params.native_widget = new NativeWidgetViewManager(widget, view_); | 80 params.native_widget = new NativeWidgetViewManager(widget, view_); |
| 109 params.delegate = widget_delegate; | 81 params.delegate = widget_delegate; |
| 110 params.bounds = gfx::Rect(size.width(), size.height()); | 82 params.bounds = gfx::Rect(size.width(), size.height()); |
| 111 widget->Init(params); | 83 widget->Init(params); |
| 112 widget->Show(); | 84 widget->Show(); |
| 113 textfield->RequestFocus(); | 85 textfield->RequestFocus(); |
| 114 } | 86 } |
| 115 | 87 |
| 116 // view_manager::ViewManagerDelegate: | 88 // view_manager::ViewManagerDelegate: |
| 117 virtual void OnRootAdded(view_manager::ViewManager* view_manager, | 89 virtual void OnRootAdded(view_manager::ViewManager* view_manager, |
| 118 view_manager::Node* root) OVERRIDE { | 90 view_manager::Node* root) OVERRIDE { |
| 119 // TODO: deal with OnRootAdded() being invoked multiple times. | 91 // TODO: deal with OnRootAdded() being invoked multiple times. |
| 120 view_manager_ = view_manager; | 92 view_manager_ = view_manager; |
| 121 view_ = view_manager::View::Create(view_manager_); | 93 view_ = view_manager::View::Create(view_manager_); |
| 122 view_manager_->GetRoots().front()->SetActiveView(view_); | 94 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(); | 95 root->SetFocus(); |
| 128 | |
| 129 CreateWidget(root->bounds().size()); | 96 CreateWidget(root->bounds().size()); |
| 130 } | 97 } |
| 131 | 98 |
| 132 // views::TextfieldController: | 99 // views::TextfieldController: |
| 133 virtual bool HandleKeyEvent(views::Textfield* sender, | 100 virtual bool HandleKeyEvent(views::Textfield* sender, |
| 134 const ui::KeyEvent& key_event) OVERRIDE { | 101 const ui::KeyEvent& key_event) OVERRIDE { |
| 135 if (key_event.key_code() == ui::VKEY_RETURN) { | 102 if (key_event.key_code() == ui::VKEY_RETURN) { |
| 136 GURL url(sender->text()); | 103 GURL url(sender->text()); |
| 137 printf("User entered this URL: %s\n", url.spec().c_str()); | 104 printf("User entered this URL: %s\n", url.spec().c_str()); |
| 138 launcher_->Launch(url.spec()); | 105 navigation::NavigationDetailsPtr nav_details( |
| 106 navigation::NavigationDetails::New()); |
| 107 nav_details->url = url.spec(); |
| 108 navigator_host_->RequestNavigate(view_manager_->GetRoots().front()->id(), |
| 109 nav_details.Pass()); |
| 139 } | 110 } |
| 140 return false; | 111 return false; |
| 141 } | 112 } |
| 142 | 113 |
| 143 // launcher::LauncherClient: | 114 virtual void ContentsChanged(views::Textfield* sender, |
| 144 virtual void OnLaunch( | 115 const base::string16& new_contents) OVERRIDE { |
| 145 const String& handler_url, | 116 // Ghetto workaround for crbug.com/386256. |
| 146 navigation::ResponseDetailsPtr response_details) OVERRIDE { | 117 if (EndsWith(new_contents, base::ASCIIToUTF16("]"), false)) { |
| 147 content_node_->Embed(handler_url); | 118 sender->SetText(new_contents.substr(0, new_contents.size() - 1)); |
| 148 | 119 HandleKeyEvent(sender, ui::KeyEvent(ui::ET_KEY_RELEASED, ui::VKEY_RETURN, |
| 149 navigation::NavigationDetailsPtr navigation_details( | 120 0, false)); |
| 150 navigation::NavigationDetails::New()); | 121 } |
| 151 navigation_details->url = response_details->response->url; | |
| 152 | |
| 153 navigation::NavigatorPtr navigator; | |
| 154 ConnectTo(handler_url, &navigator); | |
| 155 navigator->Navigate(content_node_->id(), | |
| 156 navigation_details.Pass(), | |
| 157 response_details.Pass()); | |
| 158 } | 122 } |
| 159 | 123 |
| 160 scoped_ptr<ViewsInit> views_init_; | 124 scoped_ptr<ViewsInit> views_init_; |
| 161 | 125 |
| 162 view_manager::ViewManager* view_manager_; | 126 view_manager::ViewManager* view_manager_; |
| 163 view_manager::View* view_; | 127 view_manager::View* view_; |
| 164 view_manager::Node* content_node_; | 128 navigation::NavigatorHostPtr navigator_host_; |
| 165 launcher::LauncherPtr launcher_; | |
| 166 | 129 |
| 167 DISALLOW_COPY_AND_ASSIGN(Browser); | 130 DISALLOW_COPY_AND_ASSIGN(Browser); |
| 168 }; | 131 }; |
| 169 | 132 |
| 170 } // namespace examples | 133 } // namespace examples |
| 171 | 134 |
| 172 // static | 135 // static |
| 173 Application* Application::Create() { | 136 Application* Application::Create() { |
| 174 return new examples::Browser; | 137 return new examples::Browser; |
| 175 } | 138 } |
| 176 | 139 |
| 177 } // namespace mojo | 140 } // namespace mojo |
| OLD | NEW |