| 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/macros.h" | 5 #include "base/macros.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 "examples/window_manager/window_manager.mojom.h" | 8 #include "examples/window_manager/window_manager.mojom.h" |
| 9 #include "mojo/application/application_runner_chromium.h" | 9 #include "mojo/application/application_runner_chromium.h" |
| 10 #include "mojo/common/common_type_converters.h" | 10 #include "mojo/common/common_type_converters.h" |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 gfx::Size ps = text_field->GetPreferredSize(); | 51 gfx::Size ps = text_field->GetPreferredSize(); |
| 52 text_field->SetBoundsRect(gfx::Rect(host->width(), ps.height())); | 52 text_field->SetBoundsRect(gfx::Rect(host->width(), ps.height())); |
| 53 } | 53 } |
| 54 virtual gfx::Size GetPreferredSize(const views::View* host) const override { | 54 virtual gfx::Size GetPreferredSize(const views::View* host) const override { |
| 55 return gfx::Size(); | 55 return gfx::Size(); |
| 56 } | 56 } |
| 57 | 57 |
| 58 DISALLOW_COPY_AND_ASSIGN(BrowserLayoutManager); | 58 DISALLOW_COPY_AND_ASSIGN(BrowserLayoutManager); |
| 59 }; | 59 }; |
| 60 | 60 |
| 61 // KeyboardManager handles notifying the windowmanager when views are focused. | |
| 62 // To use create one and KeyboardManager will take care of all other details. | |
| 63 // | |
| 64 // TODO(sky): it would be nice if this were put in NativeWidgetViewManager, but | |
| 65 // that requires NativeWidgetViewManager to take an IWindowManager. That may be | |
| 66 // desirable anyway... | |
| 67 class KeyboardManager | |
| 68 : public views::FocusChangeListener, | |
| 69 public ui::EventHandler, | |
| 70 public views::WidgetObserver { | |
| 71 public: | |
| 72 KeyboardManager(views::Widget* widget, | |
| 73 IWindowManager* window_manager, | |
| 74 View* view) | |
| 75 : widget_(widget), | |
| 76 window_manager_(window_manager), | |
| 77 view_(view), | |
| 78 last_view_id_(0), | |
| 79 focused_view_(NULL) { | |
| 80 widget_->GetFocusManager()->AddFocusChangeListener(this); | |
| 81 widget_->AddObserver(this); | |
| 82 widget_->GetNativeView()->AddPostTargetHandler(this); | |
| 83 } | |
| 84 | |
| 85 private: | |
| 86 virtual ~KeyboardManager() { | |
| 87 widget_->GetFocusManager()->RemoveFocusChangeListener(this); | |
| 88 widget_->GetNativeView()->RemovePostTargetHandler(this); | |
| 89 widget_->RemoveObserver(this); | |
| 90 | |
| 91 HideKeyboard(); | |
| 92 } | |
| 93 | |
| 94 void ShowKeyboard(views::View* view) { | |
| 95 if (focused_view_ == view) | |
| 96 return; | |
| 97 | |
| 98 const gfx::Rect bounds_in_widget = | |
| 99 view->ConvertRectToWidget(gfx::Rect(view->bounds().size())); | |
| 100 last_view_id_ = view_->id(); | |
| 101 window_manager_->ShowKeyboard(last_view_id_, | |
| 102 Rect::From(bounds_in_widget)); | |
| 103 // TODO(sky): listen for view to be removed. | |
| 104 focused_view_ = view; | |
| 105 } | |
| 106 | |
| 107 void HideKeyboard() { | |
| 108 if (!focused_view_) | |
| 109 return; | |
| 110 | |
| 111 window_manager_->HideKeyboard(last_view_id_); | |
| 112 last_view_id_ = 0; | |
| 113 focused_view_ = NULL; | |
| 114 } | |
| 115 | |
| 116 // views::FocusChangeListener: | |
| 117 virtual void OnWillChangeFocus(views::View* focused_before, | |
| 118 views::View* focused_now) override { | |
| 119 } | |
| 120 virtual void OnDidChangeFocus(views::View* focused_before, | |
| 121 views::View* focused_now) override { | |
| 122 if (focused_view_ && focused_now != focused_view_) | |
| 123 HideKeyboard(); | |
| 124 } | |
| 125 | |
| 126 // ui::EventHandler: | |
| 127 virtual void OnMouseEvent(ui::MouseEvent* event) override { | |
| 128 views::View* focused_now = widget_->GetFocusManager()->GetFocusedView(); | |
| 129 if (focused_now && | |
| 130 focused_now->GetClassName() == views::Textfield::kViewClassName && | |
| 131 (event->flags() & ui::EF_FROM_TOUCH) != 0) { | |
| 132 ShowKeyboard(focused_now); | |
| 133 } | |
| 134 } | |
| 135 | |
| 136 // views::WidgetObserver: | |
| 137 virtual void OnWidgetDestroying(views::Widget* widget) override { | |
| 138 delete this; | |
| 139 } | |
| 140 | |
| 141 views::Widget* widget_; | |
| 142 IWindowManager* window_manager_; | |
| 143 View* view_; | |
| 144 Id last_view_id_; | |
| 145 views::View* focused_view_; | |
| 146 | |
| 147 DISALLOW_COPY_AND_ASSIGN(KeyboardManager); | |
| 148 }; | |
| 149 | |
| 150 // This is the basics of creating a views widget with a textfield. | 61 // This is the basics of creating a views widget with a textfield. |
| 151 // TODO: cleanup! | 62 // TODO: cleanup! |
| 152 class Browser : public ApplicationDelegate, | 63 class Browser : public ApplicationDelegate, |
| 153 public ViewManagerDelegate, | 64 public ViewManagerDelegate, |
| 154 public views::TextfieldController, | 65 public views::TextfieldController, |
| 155 public ViewObserver { | 66 public ViewObserver { |
| 156 public: | 67 public: |
| 157 Browser() : shell_(nullptr), root_(NULL), widget_(NULL) {} | 68 Browser() : shell_(nullptr), root_(NULL), widget_(NULL) {} |
| 158 | 69 |
| 159 virtual ~Browser() { | 70 virtual ~Browser() { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 188 widget_delegate->GetContentsView()->SetLayoutManager( | 99 widget_delegate->GetContentsView()->SetLayoutManager( |
| 189 new BrowserLayoutManager); | 100 new BrowserLayoutManager); |
| 190 | 101 |
| 191 widget_ = new views::Widget; | 102 widget_ = new views::Widget; |
| 192 views::Widget::InitParams params( | 103 views::Widget::InitParams params( |
| 193 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); | 104 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); |
| 194 params.native_widget = new NativeWidgetViewManager(widget_, shell_, view); | 105 params.native_widget = new NativeWidgetViewManager(widget_, shell_, view); |
| 195 params.delegate = widget_delegate; | 106 params.delegate = widget_delegate; |
| 196 params.bounds = gfx::Rect(view->bounds().width, view->bounds().height); | 107 params.bounds = gfx::Rect(view->bounds().width, view->bounds().height); |
| 197 widget_->Init(params); | 108 widget_->Init(params); |
| 198 // KeyboardManager handles deleting itself when the widget is destroyed. | |
| 199 new KeyboardManager(widget_, window_manager_.get(), view); | |
| 200 widget_->Show(); | 109 widget_->Show(); |
| 201 textfield->RequestFocus(); | 110 textfield->RequestFocus(); |
| 202 } | 111 } |
| 203 | 112 |
| 204 // ViewManagerDelegate: | 113 // ViewManagerDelegate: |
| 205 virtual void OnEmbed(View* root, | 114 virtual void OnEmbed(View* root, |
| 206 ServiceProviderImpl* exported_services, | 115 ServiceProviderImpl* exported_services, |
| 207 scoped_ptr<ServiceProvider> imported_services) override { | 116 scoped_ptr<ServiceProvider> imported_services) override { |
| 208 // TODO: deal with OnEmbed() being invoked multiple times. | 117 // TODO: deal with OnEmbed() being invoked multiple times. |
| 209 ConnectToService(imported_services.get(), &navigator_host_); | 118 ConnectToService(imported_services.get(), &navigator_host_); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 DISALLOW_COPY_AND_ASSIGN(Browser); | 172 DISALLOW_COPY_AND_ASSIGN(Browser); |
| 264 }; | 173 }; |
| 265 | 174 |
| 266 } // namespace examples | 175 } // namespace examples |
| 267 } // namespace mojo | 176 } // namespace mojo |
| 268 | 177 |
| 269 MojoResult MojoMain(MojoHandle shell_handle) { | 178 MojoResult MojoMain(MojoHandle shell_handle) { |
| 270 mojo::ApplicationRunnerChromium runner(new mojo::examples::Browser); | 179 mojo::ApplicationRunnerChromium runner(new mojo::examples::Browser); |
| 271 return runner.Run(shell_handle); | 180 return runner.Run(shell_handle); |
| 272 } | 181 } |
| OLD | NEW |