杨锴
2025-04-16 09a372bc45fde16fd42257ab6f78b8deeecf720b
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
/**
 * Tencent is pleased to support the open source community by making QMUI_iOS available.
 * Copyright (C) 2016-2021 THL A29 Limited, a Tencent company. All rights reserved.
 * Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * http://opensource.org/licenses/MIT
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 */
//
//  UIMenuController+QMUI.m
//  QMUIKit
//
//  Created by 陈志宏 on 2019/7/21.
//
 
#import "UIMenuController+QMUI.h"
#import "QMUICore.h"
 
@interface UIMenuController ()
 
@property(nonatomic, assign) NSInteger qmui_originWindowLevel;
@property(nonatomic, assign) BOOL qmui_windowLevelChanged;
 
@end
 
@implementation UIMenuController (QMUI)
 
QMUISynthesizeNSIntegerProperty(qmui_originWindowLevel, setQmui_originWindowLevel);
QMUISynthesizeBOOLProperty(qmui_windowLevelChanged, setQmui_windowLevelChanged);
 
static UIWindow *kMenuControllerWindow = nil;
static BOOL kHasAddedMenuControllerNotification = NO;
 
+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        OverrideImplementation(object_getClass([UIMenuController class]), @selector(sharedMenuController), ^id(__unsafe_unretained Class originClass, SEL originCMD, IMP (^originalIMPProvider)(void)) {
            return ^(UIMenuController *selfObject) {
                
                // call super
                UIMenuController *(*originSelectorIMP)(id, SEL);
                originSelectorIMP = (UIMenuController *(*)(id, SEL))originalIMPProvider();
                UIMenuController *menuController = originSelectorIMP(selfObject, originCMD);
                
                /// 修复 issue:https://github.com/Tencent/QMUI_iOS/issues/659
                if (!kHasAddedMenuControllerNotification) {
                    kHasAddedMenuControllerNotification = YES;
                    [[NSNotificationCenter defaultCenter] addObserver:menuController selector:@selector(handleMenuWillShowNotification:) name:UIMenuControllerWillShowMenuNotification object:nil];
                    [[NSNotificationCenter defaultCenter] addObserver:menuController selector:@selector(handleMenuWillHideNotification:) name:UIMenuControllerWillHideMenuNotification object:nil];
                }
                
                return menuController;
            };
        });
    });
}
 
- (void)handleMenuWillShowNotification:(NSNotification *)notification {
    UIWindow *window = [self menuControllerWindow];
    UIWindow *targetWindow = [self windowForFirstResponder];
    if (window && targetWindow && ![QMUIHelper isKeyboardVisible]) {
        QMUILog(NSStringFromClass(self.class), @"show menu - cur window level = %@, origin window level = %@ target window level = %@", @(window.windowLevel), @(self.qmui_originWindowLevel), @(targetWindow.windowLevel));
        self.qmui_windowLevelChanged = YES;
        self.qmui_originWindowLevel = window.windowLevel;
        window.windowLevel = targetWindow.windowLevel + 1;
    }
}
 
- (void)handleMenuWillHideNotification:(NSNotification *)notification {
    UIWindow *window = [self menuControllerWindow];
    if (window && self.qmui_windowLevelChanged) {
        QMUILog(NSStringFromClass(self.class), @"hide menu - cur window level = %@, origin window level = %@", @(window.windowLevel), @(self.qmui_originWindowLevel));
        window.windowLevel = self.qmui_originWindowLevel;
        self.qmui_originWindowLevel = 0;
        self.qmui_windowLevelChanged = NO;
    }
}
 
- (UIWindow *)menuControllerWindow {
    if (kMenuControllerWindow && !kMenuControllerWindow.hidden) {
        return kMenuControllerWindow;
    }
    [UIApplication.sharedApplication.windows enumerateObjectsUsingBlock:^(__kindof UIWindow * _Nonnull window, NSUInteger idx, BOOL * _Nonnull stop) {
        NSString *windowString = [NSString stringWithFormat:@"UI%@%@", @"Text", @"EffectsWindow"];
        if ([window isKindOfClass:NSClassFromString(windowString)] && !window.hidden) {
            [window.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull subview, NSUInteger idx, BOOL * _Nonnull stop) {
                NSString *targetView = [NSString stringWithFormat:@"UI%@%@", @"Callout", @"Bar"];
                if ([subview isKindOfClass:NSClassFromString(targetView)]) {
                    kMenuControllerWindow = window;
                    *stop = YES;
                }
            }];
        }
    }];
    return kMenuControllerWindow;
}
 
- (UIWindow *)windowForFirstResponder {
    __block UIWindow *resultWindow = nil;
    [UIApplication.sharedApplication.windows enumerateObjectsUsingBlock:^(__kindof UIWindow * _Nonnull window, NSUInteger idx, BOOL * _Nonnull stop) {
        if (window != UIApplication.sharedApplication.delegate.window) {
            UIResponder *responder = [self findFirstResponderInView:window];
            if (responder) {
                resultWindow = window;
                *stop = YES;
            }
        }
    }];
    return resultWindow;
}
 
- (UIResponder *)findFirstResponderInView:(UIView *)view {
    if (view.isFirstResponder) {
        return view;
    }
    for (UIView *subView in view.subviews) {
        id responder = [self findFirstResponderInView:subView];
        if (responder) {
            return responder;
        }
    }
    return nil;
}
 
@end