File: menu_cocoa_watcher_mac.mm

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (117 lines) | stat: -rw-r--r-- 4,097 bytes parent folder | download | duplicates (6)
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
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/views/controls/menu/menu_cocoa_watcher_mac.h"

#import <Cocoa/Cocoa.h>
#include <dispatch/dispatch.h>

#include <memory>
#include <utility>

namespace views {
namespace {

// Returns the global notification filter.
MacNotificationFilter& NotificationFilterInternal() {
  static MacNotificationFilter filter =
      MacNotificationFilter::DontIgnoreNotifications;
  return filter;
}

// Returns YES if `notification` should be ignored based on the current value of
// the notification filter.
BOOL ShouldIgnoreNotification(NSNotification* notification) {
  switch (NotificationFilterInternal()) {
    case MacNotificationFilter::DontIgnoreNotifications:
      return NO;
    case MacNotificationFilter::IgnoreWorkspaceNotifications:
      return [notification.name
          isEqualToString:NSWorkspaceDidActivateApplicationNotification];
    case MacNotificationFilter::IgnoreAllNotifications:
      return YES;
  }

  return NO;
}
}  // namespace

struct MenuCocoaWatcherMac::ObjCStorage {
  // Tokens representing the notification observers.
  id __strong observer_token_other_menu;
  id __strong observer_token_new_window_focus;
  id __strong observer_token_app_change;
};

MenuCocoaWatcherMac::MenuCocoaWatcherMac(base::OnceClosure callback)
    : callback_(std::move(callback)),
      objc_storage_(std::make_unique<ObjCStorage>()) {
  objc_storage_->observer_token_other_menu =
      [[NSNotificationCenter defaultCenter]
          addObserverForName:NSMenuDidBeginTrackingNotification
                      object:nil
                       queue:nil
                  usingBlock:^(NSNotification* notification) {
                    if (ShouldIgnoreNotification(notification)) {
                      return;
                    }

                    ExecuteCallback();
                  }];
  objc_storage_->observer_token_new_window_focus =
      [[NSNotificationCenter defaultCenter]
          addObserverForName:NSWindowDidBecomeKeyNotification
                      object:nil
                       queue:nil
                  usingBlock:^(NSNotification* notification) {
                    if (ShouldIgnoreNotification(notification)) {
                      return;
                    }

                    ExecuteCallback();
                  }];
  objc_storage_->observer_token_app_change =
      [[[NSWorkspace sharedWorkspace] notificationCenter]
          addObserverForName:NSWorkspaceDidActivateApplicationNotification
                      object:nil
                       queue:nil
                  usingBlock:^(NSNotification* notification) {
                    if (ShouldIgnoreNotification(notification)) {
                      return;
                    }

                    // Only destroy menus if the browser is losing focus, not if
                    // it's gaining focus. This is to ensure that we can invoke
                    // a context menu while focused on another app, and still be
                    // able to click on menu items without dismissing the menu.
                    if (![[NSRunningApplication currentApplication] isActive]) {
                      ExecuteCallback();
                    }
                  }];
}

MenuCocoaWatcherMac::~MenuCocoaWatcherMac() {
  [NSNotificationCenter.defaultCenter
      removeObserver:objc_storage_->observer_token_other_menu];
  [NSNotificationCenter.defaultCenter
      removeObserver:objc_storage_->observer_token_new_window_focus];
  [NSWorkspace.sharedWorkspace.notificationCenter
      removeObserver:objc_storage_->observer_token_app_change];
}

void MenuCocoaWatcherMac::SetNotificationFilterForTesting(
    MacNotificationFilter filter) {
  NotificationFilterInternal() = filter;
}

void MenuCocoaWatcherMac::ExecuteCallback() {
  __block base::OnceClosure callback = std::move(callback_);
  dispatch_async(dispatch_get_main_queue(), ^{
    if (callback) {
      std::move(callback).Run();
    }
  });
}

}  // namespace views