Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(95)

Side by Side Diff: mash/simple_wm/simple_wm.cc

Issue 2511233002: Add the beginning of a simple window manager that we'll support on Windows. (Closed)
Patch Set: . Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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 "mash/simple_wm/simple_wm.h"
6
7 #include "ui/aura/client/aura_constants.h"
8 #include "ui/display/screen_base.h"
9 #include "ui/gfx/canvas.h"
10 #include "ui/gfx/geometry/mojo/geometry.mojom.h"
11 #include "ui/views/controls/label.h"
12 #include "ui/views/mus/aura_init.h"
13 #include "ui/views/mus/mus_client.h"
14 #include "ui/views/widget/native_widget_aura.h"
15 #include "ui/views/widget/widget.h"
16 #include "ui/views/widget/widget_delegate.h"
17
18 namespace simple_wm {
19
20 namespace {
21
22 const int kNonClientTopHeight = 30;
23 const int kNonClientSize = 5;
24
25 } // namespace
26
27 class SimpleWM::FrameView : public views::WidgetDelegateView,
28 public aura::WindowObserver {
29 public:
30 explicit FrameView(aura::Window* client_window)
31 : client_window_(client_window),
32 title_(new views::Label(L"(default)")) {
33 client_window_->AddObserver(this);
34 title_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
35 AddChildView(title_);
36 }
37 ~FrameView() override {}
38
39 private:
40 // views::WidgetDelegateView:
41 void OnPaint(gfx::Canvas* canvas) override {
42 canvas->FillRect(GetLocalBounds(), SK_ColorRED);
43 }
44 void Layout() override {
45 gfx::Size ps = title_->GetPreferredSize();
46 gfx::Rect title_bounds = GetLocalBounds();
47 int v_offset = (kNonClientTopHeight - ps.height()) / 2;
48 title_bounds.SetRect(kNonClientSize, v_offset,
49 title_bounds.width() - 2 * kNonClientSize,
50 ps.height());
51 title_->SetBoundsRect(title_bounds);
52
53 client_window_->SetBounds(GetLocalBounds());
sky 2016/11/30 21:25:46 Don't you want to offset the bounds by the title?
54 }
55
56 // aura::WindowObserver:
57 void OnWindowPropertyChanged(aura::Window* window, const void* key,
58 intptr_t old) override {
59 if (key == aura::client::kTitleKey) {
60 base::string16 title = *window->GetProperty(aura::client::kTitleKey);
61 title_->SetText(title);
62 }
63 }
64
65 aura::Window* client_window_;
66 views::Label* title_;
67
68 DISALLOW_COPY_AND_ASSIGN(FrameView);
69 };
70
71 SimpleWM::SimpleWM() {}
72
73 SimpleWM::~SimpleWM() {
74 // WindowTreeHost uses state from WindowTreeClient, so destroy it first.
75 window_tree_host_.reset();
76
77 // WindowTreeClient destruction may callback to us.
78 window_tree_client_.reset();
79
80 gpu_service_.reset();
81
82 display::Screen::SetScreenInstance(nullptr);
83 }
84
85 void SimpleWM::OnStart() {
86 CHECK(!started_);
87 started_ = true;
88 screen_ = base::MakeUnique<display::ScreenBase>();
89 display::Screen::SetScreenInstance(screen_.get());
90 aura_init_ = base::MakeUnique<views::AuraInit>(
91 context()->connector(), context()->identity(), "views_mus_resources.pak",
92 std::string(), nullptr, views::AuraInit::Mode::AURA_MUS_WINDOW_MANAGER);
93 gpu_service_ = ui::GpuService::Create(context()->connector(), nullptr);
94 compositor_context_factory_ =
95 base::MakeUnique<aura::MusContextFactory>(gpu_service_.get());
96 aura::Env::GetInstance()->set_context_factory(
97 compositor_context_factory_.get());
98 window_tree_client_ = base::MakeUnique<aura::WindowTreeClient>(
99 context()->connector(), this, this);
100 aura::Env::GetInstance()->SetWindowTreeClient(window_tree_client_.get());
101 window_tree_client_->ConnectAsWindowManager();
102 }
103
104 bool SimpleWM::OnConnect(
105 const service_manager::ServiceInfo& remote_info,
106 service_manager::InterfaceRegistry* registry) {
107 return true;
108 }
109
110 void SimpleWM::OnEmbed(
111 std::unique_ptr<aura::WindowTreeHostMus> window_tree_host) {
112 // WindowTreeClients configured as the window manager should never get
113 // OnEmbed().
114 NOTREACHED();
115 }
116
117 void SimpleWM::OnLostConnection(aura::WindowTreeClient* client) {
118 window_tree_host_.reset();
119 window_tree_client_.reset();
120 }
121
122 void SimpleWM::OnEmbedRootDestroyed(aura::Window* root) {
123 // WindowTreeClients configured as the window manager should never get
124 // OnEmbedRootDestroyed().
125 NOTREACHED();
126 }
127
128 void SimpleWM::OnPointerEventObserved(const ui::PointerEvent& event,
129 aura::Window* target) {
130 // Don't care.
131 }
132
133 aura::client::CaptureClient* SimpleWM::GetCaptureClient() {
134 return wm_state_.capture_controller();
135 }
136
137 aura::PropertyConverter* SimpleWM::GetPropertyConverter() {
138 return &property_converter_;
139 }
140
141 void SimpleWM::SetWindowManagerClient(
142 aura::WindowManagerClient* client) {
143 window_manager_client_ = client;
144 }
145
146 bool SimpleWM::OnWmSetBounds(aura::Window* window, gfx::Rect* bounds) {
147 FrameView* frame_view = GetFrameViewForClientWindow(window);
148 frame_view->GetWidget()->SetBounds(*bounds);
149 return true;
150 }
151
152 bool SimpleWM::OnWmSetProperty(
153 aura::Window* window,
154 const std::string& name,
155 std::unique_ptr<std::vector<uint8_t>>* new_data) {
156 return true;
157 }
158
159 aura::Window* SimpleWM::OnWmCreateTopLevelWindow(
160 ui::mojom::WindowType window_type,
161 std::map<std::string, std::vector<uint8_t>>* properties) {
162 aura::Window* client_window = new aura::Window(nullptr);
163 SetWindowType(client_window, window_type);
164 client_window->Init(ui::LAYER_NOT_DRAWN);
165
166 views::Widget* frame_widget = new views::Widget;
167 views::NativeWidgetAura* frame_native_widget =
168 new views::NativeWidgetAura(frame_widget);
sky 2016/11/30 21:25:46 You should pass in an additional parameter of true
169 views::Widget::InitParams params(views::Widget::InitParams::TYPE_CONTROL);
sky 2016/11/30 21:25:46 Don't you want TYPE_WINDOW?
170 FrameView* frame_view = new FrameView(client_window);
171 params.delegate = frame_view;
172 params.native_widget = frame_native_widget;
173 params.parent = root_;
174 params.bounds = gfx::Rect(10, 10, 500, 500);
175 frame_widget->Init(params);
176 frame_widget->Show();
177
178 frame_widget->GetNativeWindow()->AddChild(client_window);
179
180 client_window_to_frame_view_[client_window] = frame_view;
181 // TODO(beng): probably need to observe client_window from now on so we can
182 // clean up this map.
183
184 return client_window;
185 }
186
187 void SimpleWM::OnWmClientJankinessChanged(
188 const std::set<aura::Window*>& client_windows,
189 bool janky) {
190 // Don't care.
191 }
192
193 void SimpleWM::OnWmWillCreateDisplay(const display::Display& display) {
194 screen_->display_list().AddDisplay(display,
195 display::DisplayList::Type::PRIMARY);
196 }
197
198 void SimpleWM::OnWmNewDisplay(
199 std::unique_ptr<aura::WindowTreeHostMus> window_tree_host,
200 const display::Display& display) {
201 // Only handles a single root.
202 DCHECK(!root_);
203 window_tree_host_ = std::move(window_tree_host);
204 window_tree_host_->InitCompositor();
205 root_ = window_tree_host_->window();
206 DCHECK(window_manager_client_);
207 window_manager_client_->AddActivationParent(root_);
208 ui::mojom::FrameDecorationValuesPtr frame_decoration_values =
209 ui::mojom::FrameDecorationValues::New();
210 frame_decoration_values->normal_client_area_insets.Set(
211 kNonClientTopHeight, kNonClientSize, kNonClientSize, kNonClientSize);
212 frame_decoration_values->max_title_bar_button_width = 0;
213 window_manager_client_->SetFrameDecorationValues(
214 std::move(frame_decoration_values));
215 aura::client::SetFocusClient(root_, &focus_client_);
216 }
217
218 void SimpleWM::OnWmDisplayRemoved(
219 aura::WindowTreeHostMus* window_tree_host) {
220 DCHECK_EQ(window_tree_host, window_tree_host_.get());
221 root_ = nullptr;
222 window_tree_host_.reset();
223 }
224
225 void SimpleWM::OnWmDisplayModified(const display::Display& display) {}
226
227 void SimpleWM::OnWmPerformMoveLoop(
228 aura::Window* window,
229 ui::mojom::MoveLoopSource source,
230 const gfx::Point& cursor_location,
231 const base::Callback<void(bool)>& on_done) {
232 // Don't care.
233 }
234
235 void SimpleWM::OnWmCancelMoveLoop(aura::Window* window) {}
236
237 void SimpleWM::OnWmSetClientArea(
238 aura::Window* window,
239 const gfx::Insets& insets,
240 const std::vector<gfx::Rect>& additional_client_areas) {}
241
242 SimpleWM::FrameView* SimpleWM::GetFrameViewForClientWindow(
243 aura::Window* client_window) {
244 auto it = client_window_to_frame_view_.find(client_window);
245 return it != client_window_to_frame_view_.end() ? it->second : nullptr;
246 }
247
248 } // namespace simple_wm
249
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698