platform_style_mac.mm 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. #include "ui/views/style/platform_style.h"
  5. #include "base/numerics/safe_conversions.h"
  6. #include "base/strings/sys_string_conversions.h"
  7. #include "ui/base/buildflags.h"
  8. #include "ui/gfx/color_utils.h"
  9. #include "ui/views/controls/button/label_button.h"
  10. #import "ui/views/controls/scrollbar/cocoa_scroll_bar.h"
  11. #import <Cocoa/Cocoa.h>
  12. extern "C" {
  13. // From CFString private headers.
  14. typedef CF_ENUM(CFIndex, CFStringCharacterClusterType) {
  15. kCFStringGraphemeCluster = 1, /* Unicode Grapheme Cluster */
  16. kCFStringComposedCharacterCluster =
  17. 2, /* Compose all non-base (including spacing marks) */
  18. kCFStringCursorMovementCluster =
  19. 3, /* Cluster suitable for cursor movements */
  20. kCFStringBackwardDeletionCluster =
  21. 4 /* Cluster suitable for backward deletion */
  22. };
  23. CFRange CFStringGetRangeOfCharacterClusterAtIndex(
  24. CFStringRef string,
  25. CFIndex index,
  26. CFStringCharacterClusterType type);
  27. }
  28. namespace views {
  29. const int PlatformStyle::kMinLabelButtonWidth = 32;
  30. const int PlatformStyle::kMinLabelButtonHeight = 30;
  31. const bool PlatformStyle::kDialogDefaultButtonCanBeCancel = false;
  32. const bool PlatformStyle::kSelectWordOnRightClick = true;
  33. const bool PlatformStyle::kSelectAllOnRightClickWhenUnfocused = true;
  34. const bool PlatformStyle::kTextfieldUsesDragCursorWhenDraggable = false;
  35. const bool PlatformStyle::kTableViewSupportsKeyboardNavigationByCell = false;
  36. const bool PlatformStyle::kTreeViewSelectionPaintsEntireRow = true;
  37. const bool PlatformStyle::kUseRipples = false;
  38. const bool PlatformStyle::kInactiveWidgetControlsAppearDisabled = true;
  39. const bool PlatformStyle::kAdjustBubbleIfOffscreen = false;
  40. const View::FocusBehavior PlatformStyle::kDefaultFocusBehavior =
  41. View::FocusBehavior::ACCESSIBLE_ONLY;
  42. const Button::KeyClickAction PlatformStyle::kKeyClickActionOnSpace =
  43. Button::KeyClickAction::kOnKeyPress;
  44. // On Mac, the Return key is used to perform the default action even when a
  45. // control is focused.
  46. const bool PlatformStyle::kReturnClicksFocusedControl = false;
  47. // static
  48. std::unique_ptr<ScrollBar> PlatformStyle::CreateScrollBar(bool is_horizontal) {
  49. return std::make_unique<CocoaScrollBar>(is_horizontal);
  50. }
  51. // static
  52. void PlatformStyle::OnTextfieldEditFailed() {
  53. NSBeep();
  54. }
  55. // static
  56. gfx::Range PlatformStyle::RangeToDeleteBackwards(const std::u16string& text,
  57. size_t cursor_position) {
  58. if (cursor_position == 0)
  59. return gfx::Range();
  60. base::ScopedCFTypeRef<CFStringRef> cf_string(CFStringCreateWithCharacters(
  61. kCFAllocatorDefault, reinterpret_cast<const UniChar*>(text.data()),
  62. base::checked_cast<CFIndex>(text.size())));
  63. CFRange range_to_delete = CFStringGetRangeOfCharacterClusterAtIndex(
  64. cf_string, base::checked_cast<CFIndex>(cursor_position - 1),
  65. kCFStringBackwardDeletionCluster);
  66. if (range_to_delete.location == NSNotFound)
  67. return gfx::Range();
  68. // The range needs to be reversed to undo correctly.
  69. return gfx::Range(base::checked_cast<size_t>(range_to_delete.location +
  70. range_to_delete.length),
  71. base::checked_cast<size_t>(range_to_delete.location));
  72. }
  73. } // namespace views