OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "ui/views/widget/widget.h" | 5 #include "ui/views/widget/widget.h" |
6 | 6 |
7 #include "base/debug/trace_event.h" | 7 #include "base/debug/trace_event.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
161 ownership_(InitParams::NATIVE_WIDGET_OWNS_WIDGET), | 161 ownership_(InitParams::NATIVE_WIDGET_OWNS_WIDGET), |
162 is_secondary_widget_(true), | 162 is_secondary_widget_(true), |
163 frame_type_(FRAME_TYPE_DEFAULT), | 163 frame_type_(FRAME_TYPE_DEFAULT), |
164 disable_inactive_rendering_(false), | 164 disable_inactive_rendering_(false), |
165 widget_closed_(false), | 165 widget_closed_(false), |
166 saved_show_state_(ui::SHOW_STATE_DEFAULT), | 166 saved_show_state_(ui::SHOW_STATE_DEFAULT), |
167 focus_on_creation_(true), | 167 focus_on_creation_(true), |
168 is_top_level_(false), | 168 is_top_level_(false), |
169 native_widget_initialized_(false), | 169 native_widget_initialized_(false), |
170 native_widget_destroyed_(false), | 170 native_widget_destroyed_(false), |
171 is_mouse_button_pressed_(false), | |
172 ignore_capture_loss_(false), | 171 ignore_capture_loss_(false), |
173 last_mouse_event_was_move_(false), | 172 last_mouse_event_was_move_(false), |
174 auto_release_capture_(true), | 173 auto_release_capture_(true), |
175 root_layers_dirty_(false), | 174 root_layers_dirty_(false), |
176 movement_disabled_(false), | 175 movement_disabled_(false), |
177 observer_manager_(this) { | 176 observer_manager_(this) { |
178 } | 177 } |
179 | 178 |
180 Widget::~Widget() { | 179 Widget::~Widget() { |
181 DestroyRootView(); | 180 DestroyRootView(); |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
344 | 343 |
345 widget_delegate_ = params.delegate ? | 344 widget_delegate_ = params.delegate ? |
346 params.delegate : new DefaultWidgetDelegate(this); | 345 params.delegate : new DefaultWidgetDelegate(this); |
347 widget_delegate_->set_can_activate(can_activate); | 346 widget_delegate_->set_can_activate(can_activate); |
348 | 347 |
349 ownership_ = params.ownership; | 348 ownership_ = params.ownership; |
350 native_widget_ = CreateNativeWidget(params.native_widget, this)-> | 349 native_widget_ = CreateNativeWidget(params.native_widget, this)-> |
351 AsNativeWidgetPrivate(); | 350 AsNativeWidgetPrivate(); |
352 root_view_.reset(CreateRootView()); | 351 root_view_.reset(CreateRootView()); |
353 default_theme_provider_.reset(new ui::DefaultThemeProvider); | 352 default_theme_provider_.reset(new ui::DefaultThemeProvider); |
354 if (params.type == InitParams::TYPE_MENU) { | |
355 is_mouse_button_pressed_ = | |
356 internal::NativeWidgetPrivate::IsMouseButtonDown(); | |
357 } | |
358 native_widget_->InitNativeWidget(params); | 353 native_widget_->InitNativeWidget(params); |
359 if (RequiresNonClientView(params.type)) { | 354 if (RequiresNonClientView(params.type)) { |
360 non_client_view_ = new NonClientView; | 355 non_client_view_ = new NonClientView; |
361 non_client_view_->SetFrameView(CreateNonClientFrameView()); | 356 non_client_view_->SetFrameView(CreateNonClientFrameView()); |
362 // Create the ClientView, add it to the NonClientView and add the | 357 // Create the ClientView, add it to the NonClientView and add the |
363 // NonClientView to the RootView. This will cause everything to be parented. | 358 // NonClientView to the RootView. This will cause everything to be parented. |
364 non_client_view_->set_client_view(widget_delegate_->CreateClientView(this)); | 359 non_client_view_->set_client_view(widget_delegate_->CreateClientView(this)); |
365 non_client_view_->SetOverlayView(widget_delegate_->CreateOverlayView()); | 360 non_client_view_->SetOverlayView(widget_delegate_->CreateOverlayView()); |
366 SetContentsView(non_client_view_); | 361 SetContentsView(non_client_view_); |
367 // Initialize the window's title before setting the window's initial bounds; | 362 // Initialize the window's title before setting the window's initial bounds; |
(...skipping 573 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
941 | 936 |
942 void Widget::SetCapture(View* view) { | 937 void Widget::SetCapture(View* view) { |
943 if (!native_widget_->HasCapture()) { | 938 if (!native_widget_->HasCapture()) { |
944 native_widget_->SetCapture(); | 939 native_widget_->SetCapture(); |
945 | 940 |
946 // Early return if setting capture was unsuccessful. | 941 // Early return if setting capture was unsuccessful. |
947 if (!native_widget_->HasCapture()) | 942 if (!native_widget_->HasCapture()) |
948 return; | 943 return; |
949 } | 944 } |
950 | 945 |
951 if (internal::NativeWidgetPrivate::IsMouseButtonDown()) | |
952 is_mouse_button_pressed_ = true; | |
953 root_view_->SetMouseHandler(view); | 946 root_view_->SetMouseHandler(view); |
954 } | 947 } |
955 | 948 |
956 void Widget::ReleaseCapture() { | 949 void Widget::ReleaseCapture() { |
957 if (native_widget_->HasCapture()) | 950 if (native_widget_->HasCapture()) |
958 native_widget_->ReleaseCapture(); | 951 native_widget_->ReleaseCapture(); |
959 } | 952 } |
960 | 953 |
961 bool Widget::HasCapture() { | 954 bool Widget::HasCapture() { |
962 return native_widget_->HasCapture(); | 955 return native_widget_->HasCapture(); |
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1186 SendEventToProcessor(event); | 1179 SendEventToProcessor(event); |
1187 } | 1180 } |
1188 | 1181 |
1189 // TODO(tdanderson): We should not be calling the OnMouse*() functions on | 1182 // TODO(tdanderson): We should not be calling the OnMouse*() functions on |
1190 // RootView from anywhere in Widget. Use | 1183 // RootView from anywhere in Widget. Use |
1191 // SendEventToProcessor() instead. See crbug.com/348087. | 1184 // SendEventToProcessor() instead. See crbug.com/348087. |
1192 void Widget::OnMouseEvent(ui::MouseEvent* event) { | 1185 void Widget::OnMouseEvent(ui::MouseEvent* event) { |
1193 View* root_view = GetRootView(); | 1186 View* root_view = GetRootView(); |
1194 switch (event->type()) { | 1187 switch (event->type()) { |
1195 case ui::ET_MOUSE_PRESSED: { | 1188 case ui::ET_MOUSE_PRESSED: { |
1189 // Terry - move this out of each case to before switch? | |
sadrul
2014/09/25 16:19:38
Sounds like a good plan, yeah.
tdanderson
2014/09/26 18:49:40
Done.
| |
1196 last_mouse_event_was_move_ = false; | 1190 last_mouse_event_was_move_ = false; |
1197 | 1191 |
1198 // We may get deleted by the time we return from OnMousePressed. So we | 1192 // We may get deleted by the time we return from OnMousePressed. So we |
1199 // use an observer to make sure we are still alive. | 1193 // use an observer to make sure we are still alive. |
1200 WidgetDeletionObserver widget_deletion_observer(this); | 1194 WidgetDeletionObserver widget_deletion_observer(this); |
1201 | 1195 |
1202 // Make sure we're still visible before we attempt capture as the mouse | 1196 // Make sure we're still visible before we attempt capture as the mouse |
1203 // press processing may have made the window hide (as happens with menus). | 1197 // press processing may have made the window hide (as happens with menus). |
1204 | 1198 |
1205 // It is possible for a View to show a context menu on mouse-press. Since | 1199 // It is possible for a View to show a context menu on mouse-press. Since |
1206 // the menu does a capture and starts a nested message-loop, the release | 1200 // the menu does a capture and starts a nested message-loop, the release |
1207 // would go to the menu. The next click (i.e. both mouse-press and release | 1201 // would go to the menu. The next click (i.e. both mouse-press and release |
1208 // events) also go to the menu. The menu (and the nested message-loop) | 1202 // events) also go to the menu. The menu (and the nested message-loop) |
1209 // gets closed after this second release event. The code then resumes from | 1203 // gets closed after this second release event. The code then resumes from |
1210 // here. So make sure that the mouse-button is still down before doing a | 1204 // here. So make sure that the mouse-button is still down before doing a |
1211 // capture. | 1205 // capture. |
1212 if (root_view && root_view->OnMousePressed(*event) && | 1206 if (root_view && root_view->OnMousePressed(*event) && |
1213 widget_deletion_observer.IsWidgetAlive() && IsVisible() && | 1207 widget_deletion_observer.IsWidgetAlive() && IsVisible() && |
1214 internal::NativeWidgetPrivate::IsMouseButtonDown()) { | 1208 internal::NativeWidgetPrivate::IsMouseButtonDown()) { |
1215 is_mouse_button_pressed_ = true; | |
1216 if (!native_widget_->HasCapture()) | 1209 if (!native_widget_->HasCapture()) |
1217 native_widget_->SetCapture(); | 1210 native_widget_->SetCapture(); |
1218 event->SetHandled(); | 1211 event->SetHandled(); |
1219 } | 1212 } |
1220 return; | 1213 return; |
1221 } | 1214 } |
1222 | 1215 |
1223 case ui::ET_MOUSE_RELEASED: | 1216 case ui::ET_MOUSE_RELEASED: |
1224 last_mouse_event_was_move_ = false; | 1217 last_mouse_event_was_move_ = false; |
1225 is_mouse_button_pressed_ = false; | |
1226 // Release capture first, to avoid confusion if OnMouseReleased blocks. | 1218 // Release capture first, to avoid confusion if OnMouseReleased blocks. |
1227 if (auto_release_capture_ && native_widget_->HasCapture()) { | 1219 if (auto_release_capture_ && native_widget_->HasCapture()) { |
1228 base::AutoReset<bool> resetter(&ignore_capture_loss_, true); | 1220 base::AutoReset<bool> resetter(&ignore_capture_loss_, true); |
1229 native_widget_->ReleaseCapture(); | 1221 native_widget_->ReleaseCapture(); |
1230 } | 1222 } |
1231 if (root_view) | 1223 if (root_view) |
1232 root_view->OnMouseReleased(*event); | 1224 root_view->OnMouseReleased(*event); |
1233 if ((event->flags() & ui::EF_IS_NON_CLIENT) == 0) | 1225 if ((event->flags() & ui::EF_IS_NON_CLIENT) == 0) |
1234 event->SetHandled(); | 1226 event->SetHandled(); |
1235 return; | 1227 return; |
1236 | 1228 |
1237 case ui::ET_MOUSE_MOVED: | 1229 case ui::ET_MOUSE_MOVED: |
1238 case ui::ET_MOUSE_DRAGGED: | 1230 if (!last_mouse_event_was_move_ || |
1239 if (native_widget_->HasCapture() && is_mouse_button_pressed_) { | 1231 last_mouse_event_position_ != event->location()) { |
1240 last_mouse_event_was_move_ = false; | |
1241 if (root_view) | |
1242 root_view->OnMouseDragged(*event); | |
1243 } else if (!last_mouse_event_was_move_ || | |
1244 last_mouse_event_position_ != event->location()) { | |
1245 last_mouse_event_position_ = event->location(); | 1232 last_mouse_event_position_ = event->location(); |
1246 last_mouse_event_was_move_ = true; | 1233 last_mouse_event_was_move_ = true; |
1247 if (root_view) | 1234 if (root_view) |
1248 root_view->OnMouseMoved(*event); | 1235 root_view->OnMouseMoved(*event); |
1249 } | 1236 } |
1250 return; | 1237 return; |
1251 | 1238 |
1239 case ui::ET_MOUSE_DRAGGED: | |
1240 last_mouse_event_was_move_ = false; | |
1241 if (root_view) | |
1242 root_view->OnMouseDragged(*event); | |
1243 return; | |
1244 | |
1252 case ui::ET_MOUSE_EXITED: | 1245 case ui::ET_MOUSE_EXITED: |
1253 last_mouse_event_was_move_ = false; | 1246 last_mouse_event_was_move_ = false; |
1254 if (root_view) | 1247 if (root_view) |
1255 root_view->OnMouseExited(*event); | 1248 root_view->OnMouseExited(*event); |
1256 return; | 1249 return; |
1257 | 1250 |
1258 case ui::ET_MOUSEWHEEL: | 1251 case ui::ET_MOUSEWHEEL: |
1259 if (root_view && root_view->OnMouseWheel( | 1252 if (root_view && root_view->OnMouseWheel( |
1260 static_cast<const ui::MouseWheelEvent&>(*event))) | 1253 static_cast<const ui::MouseWheelEvent&>(*event))) |
1261 event->SetHandled(); | 1254 event->SetHandled(); |
1262 return; | 1255 return; |
1263 | 1256 |
1264 default: | 1257 default: |
1265 return; | 1258 return; |
1266 } | 1259 } |
1267 } | 1260 } |
1268 | 1261 |
1269 void Widget::OnMouseCaptureLost() { | 1262 void Widget::OnMouseCaptureLost() { |
1270 if (ignore_capture_loss_) | 1263 if (ignore_capture_loss_) |
1271 return; | 1264 return; |
1272 | 1265 |
1273 View* root_view = GetRootView(); | 1266 View* root_view = GetRootView(); |
1274 if (root_view) | 1267 if (root_view) |
1275 root_view->OnMouseCaptureLost(); | 1268 root_view->OnMouseCaptureLost(); |
1276 is_mouse_button_pressed_ = false; | |
1277 } | 1269 } |
1278 | 1270 |
1279 void Widget::OnScrollEvent(ui::ScrollEvent* event) { | 1271 void Widget::OnScrollEvent(ui::ScrollEvent* event) { |
1280 ui::ScrollEvent event_copy(*event); | 1272 ui::ScrollEvent event_copy(*event); |
1281 SendEventToProcessor(&event_copy); | 1273 SendEventToProcessor(&event_copy); |
1282 | 1274 |
1283 // Convert unhandled ui::ET_SCROLL events into ui::ET_MOUSEWHEEL events. | 1275 // Convert unhandled ui::ET_SCROLL events into ui::ET_MOUSEWHEEL events. |
1284 if (!event_copy.handled() && event_copy.type() == ui::ET_SCROLL) { | 1276 if (!event_copy.handled() && event_copy.type() == ui::ET_SCROLL) { |
1285 ui::MouseWheelEvent wheel(*event); | 1277 ui::MouseWheelEvent wheel(*event); |
1286 OnMouseEvent(&wheel); | 1278 OnMouseEvent(&wheel); |
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1521 | 1513 |
1522 //////////////////////////////////////////////////////////////////////////////// | 1514 //////////////////////////////////////////////////////////////////////////////// |
1523 // internal::NativeWidgetPrivate, NativeWidget implementation: | 1515 // internal::NativeWidgetPrivate, NativeWidget implementation: |
1524 | 1516 |
1525 internal::NativeWidgetPrivate* NativeWidgetPrivate::AsNativeWidgetPrivate() { | 1517 internal::NativeWidgetPrivate* NativeWidgetPrivate::AsNativeWidgetPrivate() { |
1526 return this; | 1518 return this; |
1527 } | 1519 } |
1528 | 1520 |
1529 } // namespace internal | 1521 } // namespace internal |
1530 } // namespace views | 1522 } // namespace views |
OLD | NEW |