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