RIG设置窗口控制器概述 | AI生成和翻译

Home 2025.10

概述

这段 Objective-C 代码实现了 RIGSettingWindowController 类,用于管理 “Reveal-In-GitHub” (RIG) macOS 应用程序(可能是一个用于在 GitHub 仓库中快速定位选中代码的 Xcode 插件)的设置窗口。该窗口允许用户为不同的 GitHub 仓库配置自定义菜单项、键盘快捷键和正则表达式模式。它使用类似表格的视图(RIGConfigCellsView)来显示和编辑最多 10 个配置槽位(为保持 UI 一致性而填充了空配置)。

该类遵循 NSTableViewDataSourceNSTableViewDelegate 协议,表明它处理自定义单元格视图内表格视图的数据和事件。它与应用范围内的单例(如用于持久化的 RIGSetting 和用于 UI 反馈的 RIGUtils)集成。

主要职责:

导入和定义

#import "RIGSettingWindowController.h"
#import "RIGConfigCellsView.h"
#import "RIGConfig.h"
#import "RIGPlugin.h"
#import "RIGUtils.h"
#import "RIGSetting.h"

#define kOutterXMargin 0
#define kOutterYMargin 0

私有接口

@interface RIGSettingWindowController ()<NSTableViewDataSource, NSTableViewDelegate>

@property (nonatomic, strong) NSArray *configs;
@property (nonatomic, strong) RIGConfigCellsView *configCellsView;
@property (weak) IBOutlet NSView *mainView;
@property (weak) IBOutlet NSView *configsView;

@end

实现

初始化方法

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)windowDidLoad {
    [super windowDidLoad];
    
    self.configs = [self displayConfigs];
    
    self.configCellsView = [[RIGConfigCellsView alloc] initWithFrame:CGRectMake(kOutterXMargin, kOutterYMargin, CGRectGetWidth(self.configsView.frame) - 2 * kOutterXMargin, [RIGConfigCellsView heightForConfigs:self.configs])];
    self.configCellsView.configs = self.configs;
    [self.configsView addSubview:self.configCellsView];
    [self.configCellsView reloadData];
}

有一个被注释掉的 updateConfigsViewHeight 调用,表明曾考虑动态调整大小但被禁用——可能是因为单元格视图自动调整大小或窗口是固定的。

- (void)updateConfigsViewHeight {
    CGRect frame = self.configsView.frame;
    frame.size.height = CGRectGetHeight(self.configCellsView.frame);
    self.configsView.frame = frame;
}

配置管理

- (NSMutableArray *)displayConfigs {
    NSMutableArray *configs = [NSMutableArray arrayWithArray:[RIGSetting setting].configs];
    while (configs.count < 10) {
        RIGConfig *config = [[RIGConfig alloc] init];
        config.menuTitle = @"";
        config.lastKey = @"";
        config.pattern = @"";
        [configs addObject:config];
    }
    return configs;
}
- (void)reloadConfigs {
    self.configs = [self displayConfigs];
    self.configCellsView.configs = self.configs;
    [self.configCellsView reloadData];
}
- (BOOL)isValidConfigs:(NSArray *)configs {
    for (RIGConfig *config in configs) {
        if (![config isValid]) {
            return NO;
        }
    }
    return YES;
}
- (NSArray *)filteredConfigs {
    NSMutableArray *filtered = [NSMutableArray array];
    NSArray *configs = self.configCellsView.configs;
    for (RIGConfig *config in configs) {
        if (config.menuTitle.length > 0 || config.lastKey.length > 0 || config.pattern.length > 0) {
            [filtered addObject:config];
        }
    }
    return filtered;
}

动作处理程序 (IBAction)

这些通过 Interface Builder 连接到 UI 中的按钮。

- (IBAction)saveButtonClcked:(id)sender {
    NSArray *configs = [self filteredConfigs];
    if (![self isValidConfigs:configs]) {
        [RIGUtils showMessage:@"Please complete the config, should at least have menuTitle and pattern."];
        return;
    }
    [RIGSetting setting].configs = self.configCellsView.configs;
    [RIGUtils showMessage:@"Save succeed. Will Take effect when reopen Xcode."];
}

方法名中的拼写错误:”Clcked” 应为 “Clicked”。

- (IBAction)clearButtonClicked:(id)sender {
    RIGSetting *setting = [RIGSetting settingForGitPath:self.gitRepo.localPath];
    NSString *defaultRepo = setting.defaultRepo;
    if (defaultRepo == nil) {
        [RIGUtils showMessage:@"There's no default repo setting."];
    } else {
        setting.defaultRepo = nil;
        [RIGUtils showMessage:[NSString stringWithFormat:@"Succeed to clear current default repo(%@) setting. In the next time to open github, will ask you to select new default repo.", defaultRepo]];
    }
}

注意:self.gitRepo 未在此处声明——可能是来自超类或类别的属性。

- (IBAction)resetMenusButtonClicked:(id)sender {
    [[RIGSetting setting] setConfigs:[RIGSetting defaultConfigs]];
    [self reloadConfigs];
}

潜在改进/注意事项

此代码来自 2015 年,因此使用了较旧的 Cocoa 模式(例如,手动设置框架而非自动布局)。对于现代的 SwiftUI,它将是一个带有 @State 用于配置的 View


Back

x-ai/grok-4-fast

Donate