bridged_content_view_touch_bar.mm 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. #include <os/availability.h>
  5. #import "base/mac/scoped_nsobject.h"
  6. #include "base/strings/sys_string_conversions.h"
  7. #import "components/remote_cocoa/app_shim/bridged_content_view.h"
  8. #import "components/remote_cocoa/app_shim/native_widget_ns_window_bridge.h"
  9. #include "components/remote_cocoa/common/native_widget_ns_window_host.mojom.h"
  10. namespace {
  11. NSString* const kTouchBarDialogButtonsGroupId =
  12. @"com.google.chrome-DIALOG-BUTTONS-GROUP";
  13. NSString* const kTouchBarOKId = @"com.google.chrome-OK";
  14. NSString* const kTouchBarCancelId = @"com.google.chrome-CANCEL";
  15. } // namespace
  16. @interface BridgedContentView (TouchBarAdditions) <NSTouchBarDelegate>
  17. - (void)touchBarButtonAction:(id)sender;
  18. @end
  19. @implementation BridgedContentView (TouchBarAdditions)
  20. - (void)touchBarButtonAction:(id)sender {
  21. ui::DialogButton type = static_cast<ui::DialogButton>([sender tag]);
  22. if (_bridge)
  23. _bridge->host()->DoDialogButtonAction(type);
  24. }
  25. // NSTouchBarDelegate protocol implementation.
  26. - (NSTouchBarItem*)touchBar:(NSTouchBar*)touchBar
  27. makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier {
  28. if (!_bridge)
  29. return nil;
  30. if ([identifier isEqualToString:kTouchBarDialogButtonsGroupId]) {
  31. NSMutableArray* items = [NSMutableArray arrayWithCapacity:2];
  32. for (NSTouchBarItemIdentifier i in @[ kTouchBarCancelId, kTouchBarOKId ]) {
  33. NSTouchBarItem* item = [self touchBar:touchBar makeItemForIdentifier:i];
  34. if (item)
  35. [items addObject:item];
  36. }
  37. if ([items count] == 0)
  38. return nil;
  39. return [NSClassFromString(@"NSGroupTouchBarItem")
  40. groupItemWithIdentifier:identifier
  41. items:items];
  42. }
  43. ui::DialogButton type = ui::DIALOG_BUTTON_NONE;
  44. if ([identifier isEqualToString:kTouchBarOKId])
  45. type = ui::DIALOG_BUTTON_OK;
  46. else if ([identifier isEqualToString:kTouchBarCancelId])
  47. type = ui::DIALOG_BUTTON_CANCEL;
  48. else
  49. return nil;
  50. bool buttonExists = false;
  51. std::u16string buttonLabel;
  52. bool isButtonEnabled = false;
  53. bool isButtonDefault = false;
  54. _bridge->host()->GetDialogButtonInfo(type, &buttonExists, &buttonLabel,
  55. &isButtonEnabled, &isButtonDefault);
  56. if (!buttonExists)
  57. return nil;
  58. base::scoped_nsobject<NSCustomTouchBarItem> item([[NSClassFromString(
  59. @"NSCustomTouchBarItem") alloc] initWithIdentifier:identifier]);
  60. NSButton* button =
  61. [NSButton buttonWithTitle:base::SysUTF16ToNSString(buttonLabel)
  62. target:self
  63. action:@selector(touchBarButtonAction:)];
  64. if (isButtonDefault) {
  65. // NSAlert uses a private NSButton subclass (_NSTouchBarGroupButton) with
  66. // more bells and whistles. It doesn't use -setBezelColor: directly, but
  67. // this gives an appearance matching the default _NSTouchBarGroupButton.
  68. [button setBezelColor:[NSColor colorWithSRGBRed:0.168
  69. green:0.51
  70. blue:0.843
  71. alpha:1.0]];
  72. }
  73. [button setEnabled:isButtonEnabled];
  74. [button setTag:type];
  75. [item setView:button];
  76. return item.autorelease();
  77. }
  78. // NSTouchBarProvider protocol implementation (via NSResponder category).
  79. - (NSTouchBar*)makeTouchBar {
  80. if (!_bridge)
  81. return nil;
  82. bool buttonsExist = false;
  83. _bridge->host()->GetDoDialogButtonsExist(&buttonsExist);
  84. if (!buttonsExist)
  85. return nil;
  86. base::scoped_nsobject<NSTouchBar> bar(
  87. [[NSClassFromString(@"NSTouchBar") alloc] init]);
  88. [bar setDelegate:self];
  89. // Use a group rather than individual items so they can be centered together.
  90. [bar setDefaultItemIdentifiers:@[ kTouchBarDialogButtonsGroupId ]];
  91. // Setting the group as principal will center it in the TouchBar.
  92. [bar setPrincipalItemIdentifier:kTouchBarDialogButtonsGroupId];
  93. return bar.autorelease();
  94. }
  95. @end