OLD | NEW |
| (Empty) |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "mandoline/ui/aura/native_widget_view_manager.h" | |
6 | |
7 #include "components/mus/public/cpp/view.h" | |
8 #include "mandoline/ui/aura/input_method_mandoline.h" | |
9 #include "mandoline/ui/aura/window_tree_host_mojo.h" | |
10 #include "mojo/converters/geometry/geometry_type_converters.h" | |
11 #include "mojo/converters/input_events/input_events_type_converters.h" | |
12 #include "ui/aura/client/default_capture_client.h" | |
13 #include "ui/aura/window.h" | |
14 #include "ui/aura/window_event_dispatcher.h" | |
15 #include "ui/base/ime/input_method_delegate.h" | |
16 #include "ui/wm/core/base_focus_rules.h" | |
17 #include "ui/wm/core/capture_controller.h" | |
18 #include "ui/wm/core/focus_controller.h" | |
19 | |
20 namespace mandoline { | |
21 namespace { | |
22 | |
23 // TODO: figure out what this should be. | |
24 class FocusRulesImpl : public wm::BaseFocusRules { | |
25 public: | |
26 FocusRulesImpl() {} | |
27 ~FocusRulesImpl() override {} | |
28 | |
29 bool SupportsChildActivation(aura::Window* window) const override { | |
30 return true; | |
31 } | |
32 | |
33 private: | |
34 DISALLOW_COPY_AND_ASSIGN(FocusRulesImpl); | |
35 }; | |
36 | |
37 class NativeWidgetViewObserver : public mus::ViewObserver { | |
38 public: | |
39 NativeWidgetViewObserver(NativeWidgetViewManager* view_manager) | |
40 : view_manager_(view_manager) { | |
41 view_manager_->view_->AddObserver(this); | |
42 } | |
43 | |
44 ~NativeWidgetViewObserver() override { | |
45 if (view_manager_->view_) | |
46 view_manager_->view_->RemoveObserver(this); | |
47 } | |
48 | |
49 private: | |
50 // ViewObserver: | |
51 void OnViewDestroyed(mus::View* view) override { | |
52 DCHECK_EQ(view, view_manager_->view_); | |
53 view->RemoveObserver(this); | |
54 view_manager_->view_ = nullptr; | |
55 // TODO(sky): WindowTreeHostMojo assumes the View outlives it. | |
56 // NativeWidgetViewObserver needs to deal, likely by deleting this. | |
57 } | |
58 | |
59 void OnViewBoundsChanged(mus::View* view, | |
60 const mojo::Rect& old_bounds, | |
61 const mojo::Rect& new_bounds) override { | |
62 gfx::Rect view_rect = view->bounds().To<gfx::Rect>(); | |
63 view_manager_->SetBounds(gfx::Rect(view_rect.size())); | |
64 } | |
65 | |
66 void OnViewFocusChanged(mus::View* gained_focus, | |
67 mus::View* lost_focus) override { | |
68 if (gained_focus == view_manager_->view_) | |
69 view_manager_->window_tree_host_->GetInputMethod()->OnFocus(); | |
70 else if (lost_focus == view_manager_->view_) | |
71 view_manager_->window_tree_host_->GetInputMethod()->OnBlur(); | |
72 } | |
73 | |
74 void OnViewInputEvent(mus::View* view, const mojo::EventPtr& event) override { | |
75 scoped_ptr<ui::Event> ui_event(event.To<scoped_ptr<ui::Event>>()); | |
76 if (!ui_event) | |
77 return; | |
78 | |
79 if (ui_event->IsKeyEvent()) { | |
80 view_manager_->window_tree_host_->GetInputMethod()->DispatchKeyEvent( | |
81 static_cast<ui::KeyEvent*>(ui_event.get())); | |
82 } else { | |
83 view_manager_->window_tree_host_->SendEventToProcessor(ui_event.get()); | |
84 } | |
85 } | |
86 | |
87 NativeWidgetViewManager* const view_manager_; | |
88 | |
89 DISALLOW_COPY_AND_ASSIGN(NativeWidgetViewObserver); | |
90 }; | |
91 | |
92 } // namespace | |
93 | |
94 NativeWidgetViewManager::NativeWidgetViewManager( | |
95 views::internal::NativeWidgetDelegate* delegate, | |
96 mojo::Shell* shell, | |
97 mus::View* view) | |
98 : NativeWidgetAura(delegate), view_(view) { | |
99 window_tree_host_.reset(new WindowTreeHostMojo(shell, view_)); | |
100 window_tree_host_->InitHost(); | |
101 | |
102 focus_client_.reset(new wm::FocusController(new FocusRulesImpl)); | |
103 | |
104 aura::client::SetFocusClient(window_tree_host_->window(), | |
105 focus_client_.get()); | |
106 aura::client::SetActivationClient(window_tree_host_->window(), | |
107 focus_client_.get()); | |
108 window_tree_host_->window()->AddPreTargetHandler(focus_client_.get()); | |
109 | |
110 capture_client_.reset( | |
111 new aura::client::DefaultCaptureClient(window_tree_host_->window())); | |
112 | |
113 view_observer_.reset(new NativeWidgetViewObserver(this)); | |
114 } | |
115 | |
116 NativeWidgetViewManager::~NativeWidgetViewManager() {} | |
117 | |
118 void NativeWidgetViewManager::InitNativeWidget( | |
119 const views::Widget::InitParams& in_params) { | |
120 views::Widget::InitParams params(in_params); | |
121 params.parent = window_tree_host_->window(); | |
122 NativeWidgetAura::InitNativeWidget(params); | |
123 } | |
124 | |
125 void NativeWidgetViewManager::OnWindowVisibilityChanged(aura::Window* window, | |
126 bool visible) { | |
127 view_->SetVisible(visible); | |
128 // NOTE: We could also update aura::Window's visibility when the View's | |
129 // visibility changes, but this code isn't going to be around for very long so | |
130 // I'm not bothering. | |
131 } | |
132 | |
133 } // namespace mandoline | |
OLD | NEW |