Skip to content

Commit

Permalink
Fix a multiple instances issue
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnyyoung committed Apr 28, 2023
1 parent 673bc4d commit 9bf679f
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions WeChatTweak/MultipleInstances.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ @implementation NSObject (MultipleInstances)
static void __attribute__((constructor)) tweak(void) {
[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];
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 {
Expand All @@ -30,11 +30,23 @@ + (BOOL)tweak_HasWechatInstance {
}

- (NSMenu *)tweak_applicationDockMenu:(NSApplication *)sender {
NSMenu *menu = [[NSMenu alloc] init];
NSMenuItem *menuItem = [[NSMenuItem alloc] initWithTitle:[NSBundle.tweakBundle localizedStringForKey:@"Tweak.Title.LoginAnotherAccount"]
NSMenu *menu = [self tweak_applicationDockMenu:sender];
NSMenuItem *menuItem = ({
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:[NSBundle.tweakBundle localizedStringForKey:@"Tweak.Title.LoginAnotherAccount"]
action:@selector(openNewWeChatInstace:)
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;
}

Expand Down

0 comments on commit 9bf679f

Please sign in to comment.