summaryrefslogtreecommitdiffstats
path: root/mojo/examples/browser/browser.cc
blob: fed88b512976bf939fa376ed6f81a264d0dd7aeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/basictypes.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "mojo/examples/window_manager/window_manager.mojom.h"
#include "mojo/public/cpp/application/application.h"
#include "mojo/services/public/cpp/geometry/geometry_type_converters.h"
#include "mojo/services/public/cpp/view_manager/node.h"
#include "mojo/services/public/cpp/view_manager/view.h"
#include "mojo/services/public/cpp/view_manager/view_manager.h"
#include "mojo/services/public/cpp/view_manager/view_manager_delegate.h"
#include "mojo/services/public/cpp/view_manager/view_observer.h"
#include "mojo/services/public/interfaces/navigation/navigation.mojom.h"
#include "mojo/views/native_widget_view_manager.h"
#include "mojo/views/views_init.h"
#include "ui/aura/client/focus_client.h"
#include "ui/events/event.h"
#include "ui/views/controls/textfield/textfield.h"
#include "ui/views/controls/textfield/textfield_controller.h"
#include "ui/views/focus/focus_manager.h"
#include "ui/views/layout/layout_manager.h"
#include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_delegate.h"
#include "ui/views/widget/widget_observer.h"
#include "url/gurl.h"

namespace mojo {
namespace examples {

class BrowserLayoutManager : public views::LayoutManager {
 public:
  BrowserLayoutManager() {}
  virtual ~BrowserLayoutManager() {}

 private:
  // Overridden from views::LayoutManager:
  virtual void Layout(views::View* host) OVERRIDE {
    // Browser view has one child, a text input field.
    DCHECK_EQ(1, host->child_count());
    views::View* text_field = host->child_at(0);
    gfx::Size ps = text_field->GetPreferredSize();
    text_field->SetBoundsRect(gfx::Rect(host->width(), ps.height()));
  }
  virtual gfx::Size GetPreferredSize(const views::View* host) const OVERRIDE {
    return gfx::Size();
  }

  DISALLOW_COPY_AND_ASSIGN(BrowserLayoutManager);
};

// KeyboardManager handles notifying the windowmanager when views are focused.
// To use create one and KeyboardManager will take care of all other details.
//
// TODO(sky): it would be nice if this were put in NativeWidgetViewManager, but
// that requires NativeWidgetViewManager to take an IWindowManager. That may be
// desirable anyway...
class KeyboardManager : public views::FocusChangeListener,
                        public views::WidgetObserver {
 public:
  KeyboardManager(views::Widget* widget,
                  IWindowManager* window_manager,
                  view_manager::Node* node)
      : widget_(widget),
        window_manager_(window_manager),
        node_(node),
        last_view_id_(0) {
    widget_->AddObserver(this);
    widget_->GetFocusManager()->AddFocusChangeListener(this);
  }

 private:
  virtual ~KeyboardManager() {
    widget_->GetFocusManager()->RemoveFocusChangeListener(this);
    widget_->RemoveObserver(this);
  }

  // views::FocusChangeListener:
  virtual void OnWillChangeFocus(views::View* focused_before,
                                 views::View* focused_now) OVERRIDE {
  }
  virtual void OnDidChangeFocus(views::View* focused_before,
                                views::View* focused_now) OVERRIDE {
    if (focused_now &&
        focused_now->GetClassName() == views::Textfield::kViewClassName) {
      const gfx::Rect bounds_in_widget =
          focused_now->ConvertRectToWidget(
              gfx::Rect(focused_now->bounds().size()));
      last_view_id_ = node_->active_view()->id();
      window_manager_->ShowKeyboard(last_view_id_,
                                    Rect::From(bounds_in_widget));
    } else {
      window_manager_->HideKeyboard(last_view_id_);
      last_view_id_ = 0;
    }
  }

  // views::WidgetObserver:
  virtual void OnWidgetDestroying(views::Widget* widget) OVERRIDE {
    delete this;
  }

  views::Widget* widget_;
  IWindowManager* window_manager_;
  view_manager::Node* node_;
  view_manager::Id last_view_id_;

  DISALLOW_COPY_AND_ASSIGN(KeyboardManager);
};

// This is the basics of creating a views widget with a textfield.
// TODO: cleanup!
class Browser : public Application,
                public view_manager::ViewManagerDelegate,
                public views::TextfieldController,
                public view_manager::NodeObserver {
 public:
  Browser() : view_manager_(NULL), root_(NULL), widget_(NULL) {}

  virtual ~Browser() {
  }

 private:
  // Overridden from Application:
  virtual void Initialize() MOJO_OVERRIDE {
    views_init_.reset(new ViewsInit);
    view_manager::ViewManager::Create(this, this);
    ConnectTo("mojo:mojo_window_manager", &navigator_host_);
    ConnectTo("mojo:mojo_window_manager", &window_manager_);
  }

  void CreateWidget(view_manager::Node* node) {
    views::Textfield* textfield = new views::Textfield;
    textfield->set_controller(this);

    views::WidgetDelegateView* widget_delegate = new views::WidgetDelegateView;
    widget_delegate->GetContentsView()->AddChildView(textfield);
    widget_delegate->GetContentsView()->SetLayoutManager(
        new BrowserLayoutManager);

    widget_ = new views::Widget;
    views::Widget::InitParams params(
        views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
    params.native_widget = new NativeWidgetViewManager(widget_, node);
    params.delegate = widget_delegate;
    params.bounds = gfx::Rect(node->bounds().width(), node->bounds().height());
    widget_->Init(params);
    // KeyboardManager handles deleting itself when the widget is destroyed.
    new KeyboardManager(widget_, window_manager_.get(), node);
    widget_->Show();
    textfield->RequestFocus();
  }

  // view_manager::ViewManagerDelegate:
  virtual void OnRootAdded(view_manager::ViewManager* view_manager,
                           view_manager::Node* root) OVERRIDE {
    // TODO: deal with OnRootAdded() being invoked multiple times.
    view_manager_ = view_manager;
    root_ = root;
    root_->AddObserver(this);
    root_->SetActiveView(view_manager::View::Create(view_manager));
    root_->SetFocus();
    CreateWidget(root_);
  }

  // views::TextfieldController:
  virtual bool HandleKeyEvent(views::Textfield* sender,
                              const ui::KeyEvent& key_event) OVERRIDE {
    if (key_event.key_code() == ui::VKEY_RETURN) {
      GURL url(sender->text());
      printf("User entered this URL: %s\n", url.spec().c_str());
      navigation::NavigationDetailsPtr nav_details(
          navigation::NavigationDetails::New());
      nav_details->url = url.spec();
      navigator_host_->RequestNavigate(view_manager_->GetRoots().front()->id(),
                                       navigation::NEW_NODE,
                                       nav_details.Pass());
    }
    return false;
  }

  // NodeObserver:
  virtual void OnNodeFocusChanged(view_manager::Node* gained_focus,
                                  view_manager::Node* lost_focus) OVERRIDE {
    aura::client::FocusClient* focus_client =
        aura::client::GetFocusClient(widget_->GetNativeView());
    if (lost_focus == root_)
      focus_client->FocusWindow(NULL);
    else if (gained_focus == root_)
      focus_client->FocusWindow(widget_->GetNativeView());
  }

  scoped_ptr<ViewsInit> views_init_;

  view_manager::ViewManager* view_manager_;
  view_manager::Node* root_;
  views::Widget* widget_;
  navigation::NavigatorHostPtr navigator_host_;
  IWindowManagerPtr window_manager_;

  DISALLOW_COPY_AND_ASSIGN(Browser);
};

}  // namespace examples

// static
Application* Application::Create() {
  return new examples::Browser;
}

}  // namespace mojo