File: browser_window_command_handler.mm

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (154 lines) | stat: -rw-r--r-- 5,376 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "chrome/browser/ui/cocoa/browser_window_command_handler.h"

#import "base/apple/foundation_util.h"
#include "base/check.h"
#include "base/notreached.h"
#include "base/strings/sys_string_conversions.h"
#include "chrome/app/chrome_command_ids.h"
#import "chrome/browser/app_controller_mac.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_window.h"
#include "components/remote_cocoa/app_shim/native_widget_ns_window_bridge.h"
#include "components/remote_cocoa/common/native_widget_ns_window_host.mojom.h"
#include "content/public/browser/web_contents.h"
#import "ui/base/cocoa/cocoa_base_utils.h"
#include "ui/gfx/native_widget_types.h"

namespace {

void SetToggleState(bool toggled, id item) {
  NSMenuItem* menuItem = base::apple::ObjCCast<NSMenuItem>(item);
  NSButton* buttonItem = base::apple::ObjCCast<NSButton>(item);
  if (menuItem) {
    NSControlStateValue old_state = [menuItem state];
    NSControlStateValue new_state =
        toggled ? NSControlStateValueOn : NSControlStateValueOff;
    if (old_state != new_state) {
      [menuItem setState:new_state];
    }
  } else if (buttonItem) {
    NSControlStateValue old_state = [buttonItem state];
    NSControlStateValue new_state =
        toggled ? NSControlStateValueOn : NSControlStateValueOff;
    if (old_state != new_state) {
      [buttonItem setState:new_state];
    }
  }
}

// Identify the actual Browser to which the command should be dispatched. It
// might belong to a background window, yet another dispatcher gets it because
// it is the foreground window's dispatcher and thus in the responder chain.
// Some senders don't have this problem (for example, menus only operate on the
// foreground window), so this is only an issue for senders that are part of
// windows.
remote_cocoa::NativeWidgetNSWindowBridge* FindBridgeForSender(
    id sender,
    NSWindow* window) {
  NSWindow* targetWindow = window;
  if ([sender respondsToSelector:@selector(window)]) {
    targetWindow = [sender window];
  }
  auto* bridge =
      remote_cocoa::NativeWidgetNSWindowBridge::GetFromNSWindow(targetWindow);
  DCHECK(bridge);
  return bridge;
}

}  // namespace

@implementation BrowserWindowCommandHandler

- (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)item
                           window:(NSWindow*)window {
  if ([item action] != @selector(commandDispatch:) &&
      [item action] != @selector(commandDispatchUsingKeyModifiers:)) {
    // NSWindow should only forward the above selectors here. All other
    // selectors must be handled by the default -[NSWindow
    // validateUserInterfaceItem:window:].
    NOTREACHED();
  }

  auto* bridge =
      remote_cocoa::NativeWidgetNSWindowBridge::GetFromNSWindow(window);
  DCHECK(bridge);

  remote_cocoa::mojom::ValidateUserInterfaceItemResultPtr result;
  if (!bridge->host()->ValidateUserInterfaceItem([item tag], &result)) {
    return NO;
  }

  if (result->set_toggle_state) {
    SetToggleState(result->new_toggle_state, item);
  }

  if (NSMenuItem* menuItem = base::apple::ObjCCast<NSMenuItem>(item)) {
    if (result->disable_if_has_no_key_equivalent) {
      result->enable &= !![[menuItem keyEquivalent] length];
    }

    if (result->set_hidden_state) {
      [menuItem setHidden:result->new_hidden_state];
    }

    if (result->new_title) {
      [menuItem setTitle:base::SysUTF16ToNSString(*result->new_title)];
    }
  }

  return result->enable;
}

- (void)commandDispatch:(id)sender window:(NSWindow*)window {
  DCHECK(sender);
  int command = [sender tag];
  bool was_executed = false;
  FindBridgeForSender(sender, window)
      ->host()
      ->ExecuteCommand(command, WindowOpenDisposition::CURRENT_TAB,
                       /*is_before_first_responder=*/false, &was_executed);
  DCHECK(was_executed);
}

- (void)commandDispatchUsingKeyModifiers:(id)sender window:(NSWindow*)window {
  DCHECK(sender);

  if (![sender isEnabled]) {
    // This code is reachable e.g. if the user mashes the back button, queuing
    // up a bunch of events before the button's enabled state is updated:
    // http://crbug.com/63254
    return;
  }

  NSInteger command = [sender tag];
  NSUInteger modifierFlags = [[NSApp currentEvent] modifierFlags];
  if ((command == IDC_RELOAD) &&
      (modifierFlags &
       (NSEventModifierFlagShift | NSEventModifierFlagControl))) {
    command = IDC_RELOAD_BYPASSING_CACHE;
    // Mask off Shift and Control so they don't affect the disposition below.
    modifierFlags &= ~(NSEventModifierFlagShift | NSEventModifierFlagControl);
  }
  if (![[sender window] isMainWindow]) {
    // Remove the command key from the flags, it means "keep the window in
    // the background" in this case.
    modifierFlags &= ~NSEventModifierFlagCommand;
  }

  bool was_executed = false;
  FindBridgeForSender(sender, window)
      ->host()
      ->ExecuteCommand(command,
                       ui::WindowOpenDispositionFromNSEventWithFlags(
                           [NSApp currentEvent], modifierFlags),
                       /*is_before_first_responder=*/false, &was_executed);
  DCHECK(was_executed);
}

@end