Fix a multiple instances issue

This commit is contained in:
Sunny Young 2023-04-28 15:28:20 +08:00
parent 673bc4da2f
commit 9bf679f8f7

View File

@ -14,7 +14,7 @@
static void __attribute__((constructor)) tweak(void) { static void __attribute__((constructor)) tweak(void) {
[objc_getClass("CUtility") jr_swizzleClassMethod:NSSelectorFromString(@"HasWechatInstance") withClassMethod:@selector(tweak_HasWechatInstance) error:nil]; [objc_getClass("CUtility") jr_swizzleClassMethod:NSSelectorFromString(@"HasWechatInstance") withClassMethod:@selector(tweak_HasWechatInstance) error:nil];
[objc_getClass("NSRunningApplication") jr_swizzleClassMethod:NSSelectorFromString(@"runningApplicationsWithBundleIdentifier:") withClassMethod:@selector(tweak_runningApplicationsWithBundleIdentifier:) error:nil]; [objc_getClass("NSRunningApplication") jr_swizzleClassMethod:NSSelectorFromString(@"runningApplicationsWithBundleIdentifier:") withClassMethod:@selector(tweak_runningApplicationsWithBundleIdentifier:) error:nil];
class_addMethod(objc_getClass("AppDelegate"), @selector(applicationDockMenu:), method_getImplementation(class_getInstanceMethod(objc_getClass("AppDelegate"), @selector(tweak_applicationDockMenu:))), "@:@"); [objc_getClass("AppDelegate") jr_swizzleMethod:NSSelectorFromString(@"applicationDockMenu:") withMethod:@selector(tweak_applicationDockMenu:) error:nil];
} }
+ (BOOL)tweak_HasWechatInstance { + (BOOL)tweak_HasWechatInstance {
@ -30,11 +30,23 @@ static void __attribute__((constructor)) tweak(void) {
} }
- (NSMenu *)tweak_applicationDockMenu:(NSApplication *)sender { - (NSMenu *)tweak_applicationDockMenu:(NSApplication *)sender {
NSMenu *menu = [[NSMenu alloc] init]; NSMenu *menu = [self tweak_applicationDockMenu:sender];
NSMenuItem *menuItem = [[NSMenuItem alloc] initWithTitle:[NSBundle.tweakBundle localizedStringForKey:@"Tweak.Title.LoginAnotherAccount"] NSMenuItem *menuItem = ({
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:[NSBundle.tweakBundle localizedStringForKey:@"Tweak.Title.LoginAnotherAccount"]
action:@selector(openNewWeChatInstace:) action:@selector(openNewWeChatInstace:)
keyEquivalent:@""]; keyEquivalent:@""];
[menu insertItem:menuItem atIndex:0]; item.tag = 9527;
item;
});
__block BOOL added = NO;
[menu.itemArray enumerateObjectsUsingBlock:^(NSMenuItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (obj.tag == 9527) {
*stop = added = YES;
}
}];
if (!added) {
[menu insertItem:menuItem atIndex:0];
}
return menu; return menu;
} }