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

Side by Side Diff: ui/views/cocoa/bridged_content_view.mm

Issue 2422993002: views::Label: Implement context menu, keyboard shortcuts for copy/select all. (Closed)
Patch Set: Address comments. Created 4 years, 1 month 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
« no previous file with comments | « no previous file | ui/views/controls/label.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #import "ui/views/cocoa/bridged_content_view.h" 5 #import "ui/views/cocoa/bridged_content_view.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #import "base/mac/mac_util.h" 8 #import "base/mac/mac_util.h"
9 #import "base/mac/scoped_nsobject.h" 9 #import "base/mac/scoped_nsobject.h"
10 #import "base/mac/sdk_forward_declarations.h" 10 #import "base/mac/sdk_forward_declarations.h"
(...skipping 12 matching lines...) Expand all
23 #import "ui/events/keycodes/keyboard_code_conversion_mac.h" 23 #import "ui/events/keycodes/keyboard_code_conversion_mac.h"
24 #include "ui/gfx/canvas_paint_mac.h" 24 #include "ui/gfx/canvas_paint_mac.h"
25 #include "ui/gfx/decorated_text.h" 25 #include "ui/gfx/decorated_text.h"
26 #include "ui/gfx/geometry/rect.h" 26 #include "ui/gfx/geometry/rect.h"
27 #import "ui/gfx/mac/coordinate_conversion.h" 27 #import "ui/gfx/mac/coordinate_conversion.h"
28 #include "ui/gfx/path.h" 28 #include "ui/gfx/path.h"
29 #import "ui/gfx/path_mac.h" 29 #import "ui/gfx/path_mac.h"
30 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" 30 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h"
31 #import "ui/views/cocoa/bridged_native_widget.h" 31 #import "ui/views/cocoa/bridged_native_widget.h"
32 #import "ui/views/cocoa/drag_drop_client_mac.h" 32 #import "ui/views/cocoa/drag_drop_client_mac.h"
33 #include "ui/views/controls/label.h"
33 #include "ui/views/controls/menu/menu_config.h" 34 #include "ui/views/controls/menu/menu_config.h"
34 #include "ui/views/controls/menu/menu_controller.h" 35 #include "ui/views/controls/menu/menu_controller.h"
35 #include "ui/views/view.h" 36 #include "ui/views/view.h"
36 #include "ui/views/widget/native_widget_mac.h" 37 #include "ui/views/widget/native_widget_mac.h"
37 #include "ui/views/widget/widget.h" 38 #include "ui/views/widget/widget.h"
38 #include "ui/views/word_lookup_client.h" 39 #include "ui/views/word_lookup_client.h"
39 40
40 using views::MenuController; 41 using views::MenuController;
41 42
42 namespace { 43 namespace {
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after
240 if (attribute.strike) 241 if (attribute.strike)
241 attrs[NSStrikethroughStyleAttributeName] = line_style; 242 attrs[NSStrikethroughStyleAttributeName] = line_style;
242 243
243 [str setAttributes:attrs range:range]; 244 [str setAttributes:attrs range:range];
244 } 245 }
245 246
246 [str endEditing]; 247 [str endEditing];
247 return str.autorelease(); 248 return str.autorelease();
248 } 249 }
249 250
251 ui::TextEditCommand GetTextEditCommandForMenuAction(SEL action) {
252 if (action == @selector(undo:))
253 return ui::TextEditCommand::UNDO;
254 if (action == @selector(redo:))
255 return ui::TextEditCommand::REDO;
256 if (action == @selector(cut:))
257 return ui::TextEditCommand::CUT;
258 if (action == @selector(copy:))
259 return ui::TextEditCommand::COPY;
260 if (action == @selector(paste:))
261 return ui::TextEditCommand::PASTE;
262 if (action == @selector(selectAll:))
263 return ui::TextEditCommand::SELECT_ALL;
264 return ui::TextEditCommand::INVALID_COMMAND;
265 }
266
250 } // namespace 267 } // namespace
251 268
252 @interface BridgedContentView () 269 @interface BridgedContentView ()
253 270
254 // Returns the active menu controller corresponding to |hostedView_|, 271 // Returns the active menu controller corresponding to |hostedView_|,
255 // nil otherwise. 272 // nil otherwise.
256 - (MenuController*)activeMenuController; 273 - (MenuController*)activeMenuController;
257 274
258 // Passes |event| to the InputMethod for dispatch. 275 // Passes |event| to the InputMethod for dispatch.
259 - (void)handleKeyEvent:(ui::KeyEvent*)event; 276 - (void)handleKeyEvent:(ui::KeyEvent*)event;
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after
513 } 530 }
514 } 531 }
515 532
516 - (views::DragDropClientMac*)dragDropClient { 533 - (views::DragDropClientMac*)dragDropClient {
517 views::BridgedNativeWidget* bridge = 534 views::BridgedNativeWidget* bridge =
518 views::NativeWidgetMac::GetBridgeForNativeWindow([self window]); 535 views::NativeWidgetMac::GetBridgeForNativeWindow([self window]);
519 return bridge ? bridge->drag_drop_client() : nullptr; 536 return bridge ? bridge->drag_drop_client() : nullptr;
520 } 537 }
521 538
522 - (void)undo:(id)sender { 539 - (void)undo:(id)sender {
523 // This DCHECK is more strict than a similar check in handleAction:. It can be
524 // done here because the actors sending these actions should be calling
525 // validateUserInterfaceItem: before enabling UI that allows these messages to
526 // be sent. Checking it here would be too late to provide correct UI feedback
527 // (e.g. there will be no "beep").
528 DCHECK(textInputClient_->IsTextEditCommandEnabled(ui::TextEditCommand::UNDO));
529 [self handleAction:ui::TextEditCommand::UNDO 540 [self handleAction:ui::TextEditCommand::UNDO
530 keyCode:ui::VKEY_Z 541 keyCode:ui::VKEY_Z
531 domCode:ui::DomCode::US_Z 542 domCode:ui::DomCode::US_Z
532 eventFlags:ui::EF_CONTROL_DOWN]; 543 eventFlags:ui::EF_CONTROL_DOWN];
533 } 544 }
534 545
535 - (void)redo:(id)sender { 546 - (void)redo:(id)sender {
536 DCHECK(textInputClient_->IsTextEditCommandEnabled(ui::TextEditCommand::REDO));
537 [self handleAction:ui::TextEditCommand::REDO 547 [self handleAction:ui::TextEditCommand::REDO
538 keyCode:ui::VKEY_Z 548 keyCode:ui::VKEY_Z
539 domCode:ui::DomCode::US_Z 549 domCode:ui::DomCode::US_Z
540 eventFlags:ui::EF_CONTROL_DOWN | ui::EF_SHIFT_DOWN]; 550 eventFlags:ui::EF_CONTROL_DOWN | ui::EF_SHIFT_DOWN];
541 } 551 }
542 552
543 - (void)cut:(id)sender { 553 - (void)cut:(id)sender {
544 DCHECK(textInputClient_->IsTextEditCommandEnabled(ui::TextEditCommand::CUT));
545 [self handleAction:ui::TextEditCommand::CUT 554 [self handleAction:ui::TextEditCommand::CUT
546 keyCode:ui::VKEY_X 555 keyCode:ui::VKEY_X
547 domCode:ui::DomCode::US_X 556 domCode:ui::DomCode::US_X
548 eventFlags:ui::EF_CONTROL_DOWN]; 557 eventFlags:ui::EF_CONTROL_DOWN];
549 } 558 }
550 559
551 - (void)copy:(id)sender { 560 - (void)copy:(id)sender {
552 DCHECK(textInputClient_->IsTextEditCommandEnabled(ui::TextEditCommand::COPY));
553 [self handleAction:ui::TextEditCommand::COPY 561 [self handleAction:ui::TextEditCommand::COPY
554 keyCode:ui::VKEY_C 562 keyCode:ui::VKEY_C
555 domCode:ui::DomCode::US_C 563 domCode:ui::DomCode::US_C
556 eventFlags:ui::EF_CONTROL_DOWN]; 564 eventFlags:ui::EF_CONTROL_DOWN];
557 } 565 }
558 566
559 - (void)paste:(id)sender { 567 - (void)paste:(id)sender {
560 DCHECK(
561 textInputClient_->IsTextEditCommandEnabled(ui::TextEditCommand::PASTE));
562 [self handleAction:ui::TextEditCommand::PASTE 568 [self handleAction:ui::TextEditCommand::PASTE
563 keyCode:ui::VKEY_V 569 keyCode:ui::VKEY_V
564 domCode:ui::DomCode::US_V 570 domCode:ui::DomCode::US_V
565 eventFlags:ui::EF_CONTROL_DOWN]; 571 eventFlags:ui::EF_CONTROL_DOWN];
566 } 572 }
567 573
568 - (void)selectAll:(id)sender { 574 - (void)selectAll:(id)sender {
569 DCHECK(textInputClient_->IsTextEditCommandEnabled(
570 ui::TextEditCommand::SELECT_ALL));
571 [self handleAction:ui::TextEditCommand::SELECT_ALL 575 [self handleAction:ui::TextEditCommand::SELECT_ALL
572 keyCode:ui::VKEY_A 576 keyCode:ui::VKEY_A
573 domCode:ui::DomCode::US_A 577 domCode:ui::DomCode::US_A
574 eventFlags:ui::EF_CONTROL_DOWN]; 578 eventFlags:ui::EF_CONTROL_DOWN];
575 } 579 }
576 580
577 // BaseView implementation. 581 // BaseView implementation.
578 582
579 // Don't use tracking areas from BaseView. BridgedContentView's tracks 583 // Don't use tracking areas from BaseView. BridgedContentView's tracks
580 // NSTrackingCursorUpdate and Apple's documentation suggests it's incompatible. 584 // NSTrackingCursorUpdate and Apple's documentation suggests it's incompatible.
(...skipping 790 matching lines...) Expand 10 before | Expand all | Expand 10 after
1371 textInputClient_->ConfirmCompositionText(); 1375 textInputClient_->ConfirmCompositionText();
1372 } 1376 }
1373 1377
1374 - (NSArray*)validAttributesForMarkedText { 1378 - (NSArray*)validAttributesForMarkedText {
1375 return @[]; 1379 return @[];
1376 } 1380 }
1377 1381
1378 // NSUserInterfaceValidations protocol implementation. 1382 // NSUserInterfaceValidations protocol implementation.
1379 1383
1380 - (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)item { 1384 - (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)item {
1381 if (!textInputClient_) 1385 ui::TextEditCommand command = GetTextEditCommandForMenuAction([item action]);
1386
1387 if (command == ui::TextEditCommand::INVALID_COMMAND)
1382 return NO; 1388 return NO;
1383 1389
1384 SEL action = [item action]; 1390 if (textInputClient_)
1391 return textInputClient_->IsTextEditCommandEnabled(command);
1385 1392
1386 if (action == @selector(undo:)) 1393 // views::Label does not implement the TextInputClient interface but still
1387 return textInputClient_->IsTextEditCommandEnabled( 1394 // needs to intercept the Copy and Select All menu actions.
1388 ui::TextEditCommand::UNDO); 1395 if (command != ui::TextEditCommand::COPY &&
1389 if (action == @selector(redo:)) 1396 command != ui::TextEditCommand::SELECT_ALL)
1390 return textInputClient_->IsTextEditCommandEnabled( 1397 return NO;
1391 ui::TextEditCommand::REDO);
1392 if (action == @selector(cut:))
1393 return textInputClient_->IsTextEditCommandEnabled(ui::TextEditCommand::CUT);
1394 if (action == @selector(copy:))
1395 return textInputClient_->IsTextEditCommandEnabled(
1396 ui::TextEditCommand::COPY);
1397 if (action == @selector(paste:))
1398 return textInputClient_->IsTextEditCommandEnabled(
1399 ui::TextEditCommand::PASTE);
1400 if (action == @selector(selectAll:))
1401 return textInputClient_->IsTextEditCommandEnabled(
1402 ui::TextEditCommand::SELECT_ALL);
1403 1398
1404 return NO; 1399 views::FocusManager* focus_manager =
1400 hostedView_->GetWidget()->GetFocusManager();
1401 return focus_manager && focus_manager->GetFocusedView() &&
1402 focus_manager->GetFocusedView()->GetClassName() ==
1403 views::Label::kViewClassName;
1405 } 1404 }
1406 1405
1407 // NSDraggingSource protocol implementation. 1406 // NSDraggingSource protocol implementation.
1408 1407
1409 - (NSDragOperation)draggingSession:(NSDraggingSession*)session 1408 - (NSDragOperation)draggingSession:(NSDraggingSession*)session
1410 sourceOperationMaskForDraggingContext:(NSDraggingContext)context { 1409 sourceOperationMaskForDraggingContext:(NSDraggingContext)context {
1411 return NSDragOperationEvery; 1410 return NSDragOperationEvery;
1412 } 1411 }
1413 1412
1414 - (void)draggingSession:(NSDraggingSession*)session 1413 - (void)draggingSession:(NSDraggingSession*)session
(...skipping 18 matching lines...) Expand all
1433 return [hostedView_->GetNativeViewAccessible() accessibilityHitTest:point]; 1432 return [hostedView_->GetNativeViewAccessible() accessibilityHitTest:point];
1434 } 1433 }
1435 1434
1436 - (id)accessibilityFocusedUIElement { 1435 - (id)accessibilityFocusedUIElement {
1437 if (!hostedView_) 1436 if (!hostedView_)
1438 return nil; 1437 return nil;
1439 return [hostedView_->GetNativeViewAccessible() accessibilityFocusedUIElement]; 1438 return [hostedView_->GetNativeViewAccessible() accessibilityFocusedUIElement];
1440 } 1439 }
1441 1440
1442 @end 1441 @end
OLDNEW
« no previous file with comments | « no previous file | ui/views/controls/label.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698