cc-pets 2.2.0 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CC_BRIDGE.md +5 -2
- package/CC_BRIDGE.zh-CN.md +4 -2
- package/CHANGELOG.md +85 -0
- package/CHANGELOG.zh-CN.md +85 -0
- package/CONTRIBUTING.md +28 -11
- package/CONTRIBUTING.zh-CN.md +23 -11
- package/README.md +127 -27
- package/README.zh-CN.md +104 -30
- package/Resources/zh-Hans.lproj/Localizable.strings +573 -17
- package/Sources/CCPets/{CCPetsAppDelegate+AgentEvents.m → App/CCPetsAppDelegate+AgentEvents.m} +15 -5
- package/Sources/CCPets/{CCPetsAppDelegate+AgentStatus.m → App/CCPetsAppDelegate+AgentStatus.m} +89 -31
- package/Sources/CCPets/{CCPetsAppDelegate+Bridge.m → App/CCPetsAppDelegate+Bridge.m} +35 -24
- package/Sources/CCPets/{CCPetsAppDelegate+Pets.m → App/CCPetsAppDelegate+Pets.m} +18 -4
- package/Sources/CCPets/{CCPetsAppDelegate+Private.h → App/CCPetsAppDelegate+Private.h} +90 -21
- package/Sources/CCPets/App/CCPetsAppDelegate+Profiles.m +238 -0
- package/Sources/CCPets/{CCPetsAppDelegate+Quota.m → App/CCPetsAppDelegate+Quota.m} +47 -32
- package/Sources/CCPets/App/CCPetsAppDelegate+Settings.m +269 -0
- package/Sources/CCPets/{CCPetsAppDelegate+Speech.m → App/CCPetsAppDelegate+Speech.m} +13 -7
- package/Sources/CCPets/{CCPetsAppDelegate+Update.m → App/CCPetsAppDelegate+Update.m} +71 -47
- package/Sources/CCPets/App/CCPetsAppDelegate+Workbench.m +139 -0
- package/Sources/CCPets/{CCPetsAppDelegate.h → App/CCPetsAppDelegate.h} +16 -2
- package/Sources/CCPets/{CCPetsAppDelegate.m → App/CCPetsAppDelegate.m} +110 -12
- package/Sources/CCPets/App/CCPetsOpenCommand.h +13 -0
- package/Sources/CCPets/App/CCPetsOpenCommand.m +99 -0
- package/Sources/CCPets/{main.m → App/main.m} +68 -1
- package/Sources/CCPets/{CCPetsBridge.h → Bridge/CCPetsBridge.h} +17 -4
- package/Sources/CCPets/{CCPetsBridge.m → Bridge/CCPetsBridge.m} +32 -17
- package/Sources/CCPets/Bridge/CCPetsBridgeCLI.h +10 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeCLI.m +792 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeCore.h +60 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeCore.m +558 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeSetup.h +24 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeSetup.m +501 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeStore.h +103 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeStore.m +779 -0
- package/Sources/CCPets/Integrations/CCPetsCLISupport.h +75 -0
- package/Sources/CCPets/Integrations/CCPetsCLISupport.m +342 -0
- package/Sources/CCPets/Integrations/CCPetsEvents.h +15 -0
- package/Sources/CCPets/{CCPetsEvents.m → Integrations/CCPetsEvents.m} +23 -0
- package/Sources/CCPets/Integrations/CCPetsIntegrations.h +51 -0
- package/Sources/CCPets/Integrations/CCPetsIntegrations.m +662 -0
- package/Sources/CCPets/Integrations/CCPetsProfiles.h +38 -0
- package/Sources/CCPets/Integrations/CCPetsProfiles.m +516 -0
- package/Sources/CCPets/{CCPetsTerminalFocus.m → Integrations/CCPetsTerminalFocus.m} +42 -12
- package/Sources/CCPets/Pet/CCPetsImageLoader.h +5 -0
- package/Sources/CCPets/{CCPetsImageLoader.m → Pet/CCPetsImageLoader.m} +10 -2
- package/Sources/CCPets/Pet/CCPetsPetSize.h +10 -0
- package/Sources/CCPets/Pet/CCPetsPetSize.m +21 -0
- package/Sources/CCPets/Pet/CCPetsPetStore.h +12 -0
- package/Sources/CCPets/Pet/CCPetsPetStore.m +476 -0
- package/Sources/CCPets/{CCPetsPhrases.h → Pet/CCPetsPhrases.h} +18 -2
- package/Sources/CCPets/{CCPetsPhrases.m → Pet/CCPetsPhrases.m} +53 -22
- package/Sources/CCPets/{CCPetsPhrasesEditor.m → Pet/CCPetsPhrasesEditor.m} +42 -2
- package/Sources/CCPets/Pet/PetView+Interaction.m +207 -0
- package/Sources/CCPets/Pet/PetView+Menu.m +598 -0
- package/Sources/CCPets/Pet/PetView+MicroBehavior.m +234 -0
- package/Sources/CCPets/Pet/PetView+Private.h +81 -0
- package/Sources/CCPets/{PetView.h → Pet/PetView.h} +3 -5
- package/Sources/CCPets/Pet/PetView.m +635 -0
- package/Sources/CCPets/Support/CCPetsDoctor.h +25 -0
- package/Sources/CCPets/Support/CCPetsDoctor.m +632 -0
- package/Sources/CCPets/Support/CCPetsJSON.h +27 -0
- package/Sources/CCPets/Support/CCPetsJSON.m +397 -0
- package/Sources/CCPets/{CCPetsL10n.h → Support/CCPetsL10n.h} +3 -1
- package/Sources/CCPets/{CCPetsL10n.m → Support/CCPetsL10n.m} +30 -1
- package/Sources/CCPets/Support/CCPetsPreferences.h +31 -0
- package/Sources/CCPets/Support/CCPetsPreferences.m +85 -0
- package/Sources/CCPets/UI/CCPetsDiagnosticsPage.h +25 -0
- package/Sources/CCPets/UI/CCPetsDiagnosticsPage.m +440 -0
- package/Sources/CCPets/UI/CCPetsFormBuilder.h +42 -0
- package/Sources/CCPets/UI/CCPetsFormBuilder.m +268 -0
- package/Sources/CCPets/{CCPetsGlassView.h → UI/CCPetsGlassView.h} +5 -1
- package/Sources/CCPets/{CCPetsGlassView.m → UI/CCPetsGlassView.m} +25 -7
- package/Sources/CCPets/UI/CCPetsMainWindow.h +55 -0
- package/Sources/CCPets/UI/CCPetsMainWindow.m +730 -0
- package/Sources/CCPets/UI/CCPetsSettingsPage.h +62 -0
- package/Sources/CCPets/UI/CCPetsSettingsPage.m +917 -0
- package/Sources/CCPets/UI/CCPetsUIStyle.h +132 -0
- package/Sources/CCPets/UI/CCPetsUIStyle.m +816 -0
- package/Sources/CCPets/UI/CCPetsUsageChartView.h +15 -0
- package/Sources/CCPets/UI/CCPetsUsageChartView.m +284 -0
- package/Sources/CCPets/UI/CCPetsUsagePage.h +32 -0
- package/Sources/CCPets/UI/CCPetsUsagePage.m +717 -0
- package/Sources/CCPets/UI/CCPetsUsageShared.h +53 -0
- package/Sources/CCPets/UI/CCPetsUsageStore.m +120 -0
- package/Sources/CCPets/UI/CCPetsUsageWidgets.h +30 -0
- package/Sources/CCPets/UI/CCPetsUsageWidgets.m +201 -0
- package/Sources/CCPets/UI/CCPetsWorkbench.h +40 -0
- package/Sources/CCPets/UI/CCPetsWorkbenchPages.h +13 -0
- package/Sources/CCPets/UI/CCPetsWorkbenchPages.m +783 -0
- package/Sources/CCPets/{MenuChoiceRow.m → UI/MenuChoiceRow.m} +3 -0
- package/Sources/CCPets/UI/MenuPetSizeView.h +5 -0
- package/Sources/CCPets/UI/MenuPetSizeView.m +73 -0
- package/Sources/CCPets/{MenuToggleSwitch.m → UI/MenuToggleSwitch.m} +1 -0
- package/Sources/CCPets/{CCPetsCodexRateLimits.h → Usage/CCPetsCodexRateLimits.h} +1 -0
- package/Sources/CCPets/{CCPetsCodexRateLimits.m → Usage/CCPetsCodexRateLimits.m} +10 -3
- package/Sources/CCPets/{CCPetsQuotaHistory.h → Usage/CCPetsQuotaHistory.h} +3 -0
- package/Sources/CCPets/{CCPetsQuotaHistory.m → Usage/CCPetsQuotaHistory.m} +58 -0
- package/Sources/CCPets/Usage/CCPetsUsage+ClaudeReader.m +436 -0
- package/Sources/CCPets/Usage/CCPetsUsage+CodexReader.m +392 -0
- package/Sources/CCPets/Usage/CCPetsUsage+CodexSessions.m +484 -0
- package/Sources/CCPets/Usage/CCPetsUsage+Internal.h +96 -0
- package/Sources/CCPets/Usage/CCPetsUsage+Models.m +285 -0
- package/Sources/CCPets/Usage/CCPetsUsage+TokenTotals.m +655 -0
- package/Sources/CCPets/Usage/CCPetsUsage.h +57 -0
- package/Sources/CCPets/Usage/CCPetsUsage.m +225 -0
- package/Sources/CCPets/Usage/CCPetsUsageMode.h +10 -0
- package/Sources/CCPets/Usage/CCPetsUsageMode.m +163 -0
- package/Sources/CCPets/{CCPetsUsageMonitor.h → Usage/CCPetsUsageMonitor.h} +3 -0
- package/Sources/CCPets/{CCPetsUsageMonitor.m → Usage/CCPetsUsageMonitor.m} +163 -6
- package/Sources/CCPets/Usage/QuotaDashboardView+Primitives.m +328 -0
- package/Sources/CCPets/Usage/QuotaDashboardView+Private.h +103 -0
- package/Sources/CCPets/Usage/QuotaDashboardView+Rendering.m +616 -0
- package/Sources/CCPets/{QuotaDashboardView.h → Usage/QuotaDashboardView.h} +17 -0
- package/Sources/CCPets/Usage/QuotaDashboardView.m +587 -0
- package/THIRD_PARTY_NOTICES.md +1 -1
- package/THIRD_PARTY_NOTICES.zh-CN.md +1 -1
- package/bin/cc-pets +8 -6
- package/bin/claude-with-pet +10 -2
- package/bin/codex-with-pet +13 -5
- package/package.json +1 -1
- package/scripts/build.sh +7 -3
- package/scripts/i18n.zsh +9 -52
- package/scripts/install-app.sh +4 -0
- package/scripts/install-shell-integration.sh +17 -10
- package/scripts/uninstall-shell-integration.sh +6 -1
- package/Sources/CCPets/CCPetsAppDelegate+Settings.m +0 -226
- package/Sources/CCPets/CCPetsEvents.h +0 -10
- package/Sources/CCPets/CCPetsImageLoader.h +0 -4
- package/Sources/CCPets/CCPetsUsage.h +0 -36
- package/Sources/CCPets/CCPetsUsage.m +0 -2069
- package/Sources/CCPets/PetView.m +0 -1806
- package/Sources/CCPets/QuotaDashboardView.m +0 -1145
- package/scripts/bridge/cli.mjs +0 -237
- package/scripts/bridge/core.mjs +0 -228
- package/scripts/bridge/hook.mjs +0 -150
- package/scripts/bridge/install.mjs +0 -311
- package/scripts/bridge/mcp-server.mjs +0 -308
- package/scripts/bridge/options.mjs +0 -87
- package/scripts/bridge/process.mjs +0 -151
- package/scripts/bridge/reservations.mjs +0 -248
- package/scripts/bridge/store.mjs +0 -433
- package/scripts/bridge/watch.mjs +0 -69
- package/scripts/check-l10n.mjs +0 -168
- package/scripts/configure-updater.mjs +0 -43
- package/scripts/detect-cli.mjs +0 -68
- package/scripts/doctor.mjs +0 -470
- package/scripts/i18n.mjs +0 -122
- package/scripts/install-claude-hooks.mjs +0 -253
- package/scripts/install-codex-hooks.mjs +0 -78
- package/scripts/integration-markers.mjs +0 -51
- package/scripts/locales/zh-Hans.json +0 -366
- package/scripts/pet-store.mjs +0 -415
- package/scripts/uninstall-integrations.mjs +0 -205
- /package/Sources/CCPets/{CCPetsVersion.h → App/CCPetsVersion.h} +0 -0
- /package/Sources/CCPets/{CCPetsVersion.m → App/CCPetsVersion.m} +0 -0
- /package/Sources/CCPets/{CCPetsTerminalFocus.h → Integrations/CCPetsTerminalFocus.h} +0 -0
- /package/Sources/CCPets/{CCPetsPhrasesEditor.h → Pet/CCPetsPhrasesEditor.h} +0 -0
- /package/Sources/CCPets/{CCPetsPreviews.h → Pet/CCPetsPreviews.h} +0 -0
- /package/Sources/CCPets/{CCPetsPreviews.m → Pet/CCPetsPreviews.m} +0 -0
- /package/Sources/CCPets/Resources/{/351/273/230/350/256/244.webp" → ByteMochi.webp} +0 -0
- /package/Sources/CCPets/{CCPetsCleanup.h → Support/CCPetsCleanup.h} +0 -0
- /package/Sources/CCPets/{CCPetsCleanup.m → Support/CCPetsCleanup.m} +0 -0
- /package/Sources/CCPets/{CCPetsPaths.h → Support/CCPetsPaths.h} +0 -0
- /package/Sources/CCPets/{CCPetsPaths.m → Support/CCPetsPaths.m} +0 -0
- /package/Sources/CCPets/{CCPetsSystemMonitor.h → Support/CCPetsSystemMonitor.h} +0 -0
- /package/Sources/CCPets/{CCPetsSystemMonitor.m → Support/CCPetsSystemMonitor.m} +0 -0
- /package/Sources/CCPets/{CCPetsGlassMenu.h → UI/CCPetsGlassMenu.h} +0 -0
- /package/Sources/CCPets/{CCPetsGlassMenu.m → UI/CCPetsGlassMenu.m} +0 -0
- /package/Sources/CCPets/{MenuChoiceRow.h → UI/MenuChoiceRow.h} +0 -0
- /package/Sources/CCPets/{MenuHintView.h → UI/MenuHintView.h} +0 -0
- /package/Sources/CCPets/{MenuHintView.m → UI/MenuHintView.m} +0 -0
- /package/Sources/CCPets/{MenuToggleSwitch.h → UI/MenuToggleSwitch.h} +0 -0
|
@@ -0,0 +1,730 @@
|
|
|
1
|
+
#import "CCPetsMainWindow.h"
|
|
2
|
+
#import "CCPetsL10n.h"
|
|
3
|
+
#import "CCPetsPreferences.h"
|
|
4
|
+
#import "CCPetsDiagnosticsPage.h"
|
|
5
|
+
#import "CCPetsSettingsPage.h"
|
|
6
|
+
#import "CCPetsUsagePage.h"
|
|
7
|
+
#import "CCPetsUIStyle.h"
|
|
8
|
+
#import "CCPetsWorkbenchPages.h"
|
|
9
|
+
|
|
10
|
+
NSString *const CCPetsMainWindowPageOverview = @"overview";
|
|
11
|
+
NSString *const CCPetsMainWindowPageSessions = @"sessions";
|
|
12
|
+
NSString *const CCPetsMainWindowPageUsage = @"usage";
|
|
13
|
+
NSString *const CCPetsMainWindowPagePet = @"pet";
|
|
14
|
+
NSString *const CCPetsMainWindowPageAccounts = @"accounts";
|
|
15
|
+
NSString *const CCPetsMainWindowPageAlerts = @"alerts";
|
|
16
|
+
NSString *const CCPetsMainWindowPageGeneral = @"general";
|
|
17
|
+
NSString *const CCPetsMainWindowPageDiagnostics = @"diagnostics";
|
|
18
|
+
NSNotificationName const CCPetsPetCardDidChangeNotification = @"CCPetsPetCardDidChange";
|
|
19
|
+
static NSString *const MainWindowLastPageKey = @"CCPetsMainWindowLastPage";
|
|
20
|
+
NSString *const CCPetsMainWindowAppearanceKey = @"CCPetsMainWindowAppearance";
|
|
21
|
+
|
|
22
|
+
NSString *CCPetsMainWindowAppearancePreference(void) {
|
|
23
|
+
NSString *value = [NSUserDefaults.standardUserDefaults stringForKey:CCPetsMainWindowAppearanceKey];
|
|
24
|
+
return [@[@"light", @"dark"] containsObject:value] ? value : @"system";
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// nil 表示跟随系统。
|
|
28
|
+
static NSAppearance *MainWindowAppearance(void) {
|
|
29
|
+
NSString *value = CCPetsMainWindowAppearancePreference();
|
|
30
|
+
if ([value isEqualToString:@"light"]) return [NSAppearance appearanceNamed:NSAppearanceNameAqua];
|
|
31
|
+
if ([value isEqualToString:@"dark"]) return [NSAppearance appearanceNamed:NSAppearanceNameDarkAqua];
|
|
32
|
+
return nil;
|
|
33
|
+
}
|
|
34
|
+
// 改版后默认窗口变大了(内容多了概览和表格)。换个名字,不沿用改版前记住的小窗口尺寸。
|
|
35
|
+
static NSString *const MainWindowFrameName = @"CCPetsMainWindow.v2";
|
|
36
|
+
|
|
37
|
+
// 侧边栏里的页面,按显示顺序。group 决定排在哪一组,footer 的那页钉在侧边栏底部。
|
|
38
|
+
// 标题现取,切换语言后重建侧边栏即可。
|
|
39
|
+
static NSArray<NSDictionary *> *MainWindowPages(void) {
|
|
40
|
+
return @[
|
|
41
|
+
@{@"id": CCPetsMainWindowPageOverview, @"title": L(@"Overview"), @"symbol": @"house",
|
|
42
|
+
@"group": @"workbench", @"class": CCPetsOverviewPage.class},
|
|
43
|
+
@{@"id": CCPetsMainWindowPageSessions, @"title": L(@"Sessions"), @"symbol": @"terminal",
|
|
44
|
+
@"group": @"workbench", @"class": CCPetsSessionsPage.class},
|
|
45
|
+
@{@"id": CCPetsMainWindowPageUsage, @"title": L(@"Usage"), @"symbol": @"chart.bar",
|
|
46
|
+
@"group": @"workbench", @"class": CCPetsUsagePage.class},
|
|
47
|
+
@{@"id": CCPetsMainWindowPagePet, @"title": L(@"Pet"), @"symbol": @"pawprint",
|
|
48
|
+
@"group": @"workbench", @"class": CCPetsPetPage.class},
|
|
49
|
+
@{@"id": CCPetsMainWindowPageAccounts, @"title": L(@"Multi-Account"), @"symbol": @"person.2",
|
|
50
|
+
@"group": @"workbench", @"class": CCPetsAccountsPage.class},
|
|
51
|
+
@{@"id": CCPetsMainWindowPageAlerts, @"title": L(@"Alerts"), @"symbol": @"bell",
|
|
52
|
+
@"group": @"settings", @"class": CCPetsAlertsPage.class},
|
|
53
|
+
@{@"id": CCPetsMainWindowPageGeneral, @"title": L(@"Settings"), @"symbol": @"gearshape",
|
|
54
|
+
@"group": @"settings", @"class": CCPetsGeneralPage.class},
|
|
55
|
+
@{@"id": CCPetsMainWindowPageDiagnostics, @"title": L(@"Diagnostics"), @"symbol": @"stethoscope",
|
|
56
|
+
@"group": @"footer", @"class": CCPetsDiagnosticsPage.class},
|
|
57
|
+
];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
NSArray<NSString *> *CCPetsMainWindowPageIDs(void) {
|
|
61
|
+
NSMutableArray<NSString *> *ids = [NSMutableArray array];
|
|
62
|
+
for (NSDictionary *entry in MainWindowPages()) [ids addObject:entry[@"id"]];
|
|
63
|
+
return ids;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
NSString *CCPetsMainWindowResolvePage(NSString *page) {
|
|
67
|
+
if (page.length == 0) return nil;
|
|
68
|
+
if ([CCPetsMainWindowPageIDs() containsObject:page]) return page;
|
|
69
|
+
// 改版前设置是一整页。旧脚本、旧文档里的 `cc-pets open settings` 落到宠物页(原来的第一组)。
|
|
70
|
+
if ([page isEqualToString:@"settings"]) return CCPetsMainWindowPagePet;
|
|
71
|
+
return nil;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
static NSDictionary *MainWindowPageWithID(NSString *page) {
|
|
75
|
+
for (NSDictionary *entry in MainWindowPages()) {
|
|
76
|
+
if ([entry[@"id"] isEqualToString:page]) return entry;
|
|
77
|
+
}
|
|
78
|
+
return nil;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// 有没有还开着的普通窗口。桌宠、状态卡、气泡、额度面板、NSAlert 都是 NSPanel,不算;
|
|
82
|
+
// 台词编辑器是 NSWindow,算——它开着时切回 Accessory,编辑器会跟着掉到别的 App 后面。
|
|
83
|
+
static BOOL HasVisibleRegularWindow(void) {
|
|
84
|
+
for (NSWindow *window in NSApp.windows) {
|
|
85
|
+
if ([window isKindOfClass:NSPanel.class] || !window.canBecomeMainWindow) continue;
|
|
86
|
+
// 最小化到 Dock 的窗口 isVisible 为 NO,但切回 Accessory 会让它连同 Dock 图标一起找不回来。
|
|
87
|
+
if (window.isVisible || window.isMiniaturized) return YES;
|
|
88
|
+
}
|
|
89
|
+
return NO;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
#pragma mark - 侧边栏部件
|
|
93
|
+
|
|
94
|
+
// 侧边栏的中文标题字与字之间各加一个空格("概 览"、"会 话"),两三个字的短标题排开后
|
|
95
|
+
// 更像一列整齐的菜单;全屏或铺满屏幕时侧边栏更宽,字间改成两个空格。只处理全是汉字的标题:
|
|
96
|
+
// 英文等其他语言原样显示。读屏仍用原标题。
|
|
97
|
+
static NSString *SidebarDisplayTitle(NSString *title, BOOL spacious) {
|
|
98
|
+
if (title.length < 2) return title;
|
|
99
|
+
NSCharacterSet *han = [NSCharacterSet characterSetWithRange:NSMakeRange(0x4E00, 0x9FFF - 0x4E00 + 1)];
|
|
100
|
+
if ([title rangeOfCharacterFromSet:han.invertedSet].location != NSNotFound) return title;
|
|
101
|
+
NSMutableArray<NSString *> *characters = [NSMutableArray array];
|
|
102
|
+
[title enumerateSubstringsInRange:NSMakeRange(0, title.length) options:NSStringEnumerationByComposedCharacterSequences
|
|
103
|
+
usingBlock:^(NSString *character, NSRange range, NSRange enclosing, BOOL *stop) {
|
|
104
|
+
[characters addObject:character];
|
|
105
|
+
}];
|
|
106
|
+
return [characters componentsJoinedByString:spacious ? @" " : @" "];
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// 侧边栏里的一项:单色图标 + 标题 + 可选角标。选中时整行铺淡青底,图标和文字换成强调色。
|
|
110
|
+
// 自己画而不用 source list 表格:选中色要跟主题走(表格只会用系统强调色),分组、底部钉住的
|
|
111
|
+
// 诊断项也更好排。
|
|
112
|
+
@interface CCPetsSidebarItem : NSView
|
|
113
|
+
@property(nonatomic, copy) NSString *pageID;
|
|
114
|
+
// 原标题;显示用的是加了空格的版本。
|
|
115
|
+
@property(nonatomic, copy) NSString *title;
|
|
116
|
+
@property(nonatomic) BOOL selected;
|
|
117
|
+
@property(nonatomic) CCPetsCountBadge *badge;
|
|
118
|
+
@property(nonatomic, copy) void (^onSelect)(NSString *pageID);
|
|
119
|
+
// spacious:全屏或铺满屏幕时用,图标和文字间多空 2pt,字间两个空格。
|
|
120
|
+
- (instancetype)initWithPage:(NSDictionary *)page spacious:(BOOL)spacious;
|
|
121
|
+
@end
|
|
122
|
+
|
|
123
|
+
@interface CCPetsSidebarItem ()
|
|
124
|
+
@property(nonatomic) NSImageView *icon;
|
|
125
|
+
@property(nonatomic) NSTextField *label;
|
|
126
|
+
@property(nonatomic) BOOL hovering;
|
|
127
|
+
@end
|
|
128
|
+
|
|
129
|
+
@implementation CCPetsSidebarItem
|
|
130
|
+
|
|
131
|
+
- (instancetype)initWithPage:(NSDictionary *)page spacious:(BOOL)spacious {
|
|
132
|
+
if ((self = [super initWithFrame:NSZeroRect])) {
|
|
133
|
+
self.pageID = page[@"id"];
|
|
134
|
+
self.wantsLayer = YES;
|
|
135
|
+
self.translatesAutoresizingMaskIntoConstraints = NO;
|
|
136
|
+
NSImageSymbolConfiguration *configuration = [NSImageSymbolConfiguration
|
|
137
|
+
configurationWithPointSize:13 weight:NSFontWeightRegular];
|
|
138
|
+
NSImage *image = [[NSImage imageWithSystemSymbolName:page[@"symbol"] accessibilityDescription:nil]
|
|
139
|
+
imageWithSymbolConfiguration:configuration] ?: [NSImage new];
|
|
140
|
+
NSImageView *icon = [NSImageView imageViewWithImage:image];
|
|
141
|
+
icon.symbolConfiguration = configuration;
|
|
142
|
+
NSTextField *label = CCPetsLabel(SidebarDisplayTitle(page[@"title"], spacious), 13, NSFontWeightRegular, CCPetsInkColor());
|
|
143
|
+
self.title = page[@"title"];
|
|
144
|
+
[label setContentCompressionResistancePriority:NSLayoutPriorityDefaultLow
|
|
145
|
+
forOrientation:NSLayoutConstraintOrientationHorizontal];
|
|
146
|
+
CCPetsCountBadge *badge = [CCPetsCountBadge new];
|
|
147
|
+
for (NSView *view in @[icon, label, badge]) {
|
|
148
|
+
view.translatesAutoresizingMaskIntoConstraints = NO;
|
|
149
|
+
[self addSubview:view];
|
|
150
|
+
}
|
|
151
|
+
[badge setContentCompressionResistancePriority:NSLayoutPriorityRequired
|
|
152
|
+
forOrientation:NSLayoutConstraintOrientationHorizontal];
|
|
153
|
+
self.icon = icon;
|
|
154
|
+
self.label = label;
|
|
155
|
+
self.badge = badge;
|
|
156
|
+
[NSLayoutConstraint activateConstraints:@[
|
|
157
|
+
[self.heightAnchor constraintEqualToConstant:30],
|
|
158
|
+
[icon.leadingAnchor constraintEqualToAnchor:self.leadingAnchor constant:10],
|
|
159
|
+
[icon.centerYAnchor constraintEqualToAnchor:self.centerYAnchor],
|
|
160
|
+
[icon.widthAnchor constraintEqualToConstant:18],
|
|
161
|
+
[label.leadingAnchor constraintEqualToAnchor:icon.trailingAnchor constant:spacious ? 10 : 8],
|
|
162
|
+
[label.centerYAnchor constraintEqualToAnchor:self.centerYAnchor],
|
|
163
|
+
[label.trailingAnchor constraintLessThanOrEqualToAnchor:badge.leadingAnchor constant:-6],
|
|
164
|
+
[badge.trailingAnchor constraintEqualToAnchor:self.trailingAnchor constant:-8],
|
|
165
|
+
[badge.centerYAnchor constraintEqualToAnchor:self.centerYAnchor],
|
|
166
|
+
]];
|
|
167
|
+
[self addTrackingArea:[[NSTrackingArea alloc] initWithRect:NSZeroRect
|
|
168
|
+
options:NSTrackingMouseEnteredAndExited | NSTrackingActiveInActiveApp | NSTrackingInVisibleRect
|
|
169
|
+
owner:self userInfo:nil]];
|
|
170
|
+
[self applyStyle];
|
|
171
|
+
}
|
|
172
|
+
return self;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
- (void)setSelected:(BOOL)selected {
|
|
176
|
+
_selected = selected;
|
|
177
|
+
[self applyStyle];
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
- (void)applyStyle {
|
|
181
|
+
self.icon.contentTintColor = self.selected ? CCPetsAccentColor() : CCPetsSecondaryInkColor();
|
|
182
|
+
self.label.font = [NSFont systemFontOfSize:13 weight:self.selected ? NSFontWeightSemibold : NSFontWeightRegular];
|
|
183
|
+
self.needsDisplay = YES;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
- (BOOL)wantsUpdateLayer {
|
|
187
|
+
return YES;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
- (void)updateLayer {
|
|
191
|
+
self.layer.cornerRadius = 7;
|
|
192
|
+
self.layer.backgroundColor = self.selected ? CCPetsSelectionColor().CGColor
|
|
193
|
+
: (self.hovering ? CCPetsHoverColor().CGColor : NSColor.clearColor.CGColor);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
- (void)mouseEntered:(NSEvent *)event {
|
|
197
|
+
self.hovering = YES;
|
|
198
|
+
self.needsDisplay = YES;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
- (void)mouseExited:(NSEvent *)event {
|
|
202
|
+
self.hovering = NO;
|
|
203
|
+
self.needsDisplay = YES;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
- (void)mouseDown:(NSEvent *)event {
|
|
207
|
+
if (self.onSelect) self.onSelect(self.pageID);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
- (BOOL)acceptsFirstMouse:(NSEvent *)event {
|
|
211
|
+
return YES;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
- (BOOL)isAccessibilityElement {
|
|
215
|
+
return YES;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
- (NSAccessibilityRole)accessibilityRole {
|
|
219
|
+
return NSAccessibilityButtonRole;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
- (NSString *)accessibilityLabel {
|
|
223
|
+
return self.title;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
- (BOOL)isAccessibilitySelected {
|
|
227
|
+
return self.selected;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
- (BOOL)accessibilityPerformPress {
|
|
231
|
+
if (self.onSelect) self.onSelect(self.pageID);
|
|
232
|
+
return YES;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
@end
|
|
236
|
+
|
|
237
|
+
// 侧边栏顶部的宠物名片:会动的当前宠物、名字、Agent 状态。
|
|
238
|
+
@interface CCPetsPetCardView : NSView
|
|
239
|
+
- (void)reload;
|
|
240
|
+
@end
|
|
241
|
+
|
|
242
|
+
@interface CCPetsPetCardView ()
|
|
243
|
+
@property(nonatomic) CCPetsSpriteView *sprite;
|
|
244
|
+
@property(nonatomic) NSTextField *nameLabel;
|
|
245
|
+
@property(nonatomic) NSView *statusDot;
|
|
246
|
+
@property(nonatomic) NSTextField *statusLabel;
|
|
247
|
+
@property(nonatomic) CCPetsStatusKind kind;
|
|
248
|
+
@end
|
|
249
|
+
|
|
250
|
+
@implementation CCPetsPetCardView
|
|
251
|
+
|
|
252
|
+
- (instancetype)initWithFrame:(NSRect)frame {
|
|
253
|
+
if ((self = [super initWithFrame:frame])) {
|
|
254
|
+
CCPetsSpriteView *sprite = [[CCPetsSpriteView alloc] initWithFrame:NSZeroRect];
|
|
255
|
+
self.sprite = sprite;
|
|
256
|
+
NSTextField *name = CCPetsLabel(@"", 13, NSFontWeightBold, CCPetsInkColor());
|
|
257
|
+
self.nameLabel = name;
|
|
258
|
+
NSView *dot = [NSView new];
|
|
259
|
+
dot.wantsLayer = YES;
|
|
260
|
+
dot.layer.cornerRadius = 3;
|
|
261
|
+
self.statusDot = dot;
|
|
262
|
+
NSTextField *status = CCPetsLabel(@"", 11, NSFontWeightRegular, CCPetsSecondaryInkColor());
|
|
263
|
+
self.statusLabel = status;
|
|
264
|
+
for (NSView *view in @[sprite, name, dot, status]) {
|
|
265
|
+
view.translatesAutoresizingMaskIntoConstraints = NO;
|
|
266
|
+
[self addSubview:view];
|
|
267
|
+
}
|
|
268
|
+
[name setContentCompressionResistancePriority:NSLayoutPriorityDefaultLow
|
|
269
|
+
forOrientation:NSLayoutConstraintOrientationHorizontal];
|
|
270
|
+
[status setContentCompressionResistancePriority:NSLayoutPriorityDefaultLow
|
|
271
|
+
forOrientation:NSLayoutConstraintOrientationHorizontal];
|
|
272
|
+
[NSLayoutConstraint activateConstraints:@[
|
|
273
|
+
// 素材格子是 140×150,按同样比例缩到 40pt 宽。
|
|
274
|
+
[sprite.leadingAnchor constraintEqualToAnchor:self.leadingAnchor constant:6],
|
|
275
|
+
[sprite.topAnchor constraintEqualToAnchor:self.topAnchor],
|
|
276
|
+
[sprite.bottomAnchor constraintEqualToAnchor:self.bottomAnchor],
|
|
277
|
+
[sprite.widthAnchor constraintEqualToConstant:40],
|
|
278
|
+
[sprite.heightAnchor constraintEqualToConstant:43],
|
|
279
|
+
[name.leadingAnchor constraintEqualToAnchor:sprite.trailingAnchor constant:8],
|
|
280
|
+
[name.trailingAnchor constraintLessThanOrEqualToAnchor:self.trailingAnchor constant:-6],
|
|
281
|
+
[name.bottomAnchor constraintEqualToAnchor:sprite.centerYAnchor constant:1],
|
|
282
|
+
[dot.leadingAnchor constraintEqualToAnchor:name.leadingAnchor constant:1],
|
|
283
|
+
[dot.widthAnchor constraintEqualToConstant:6],
|
|
284
|
+
[dot.heightAnchor constraintEqualToConstant:6],
|
|
285
|
+
[dot.centerYAnchor constraintEqualToAnchor:status.centerYAnchor],
|
|
286
|
+
[status.leadingAnchor constraintEqualToAnchor:dot.trailingAnchor constant:5],
|
|
287
|
+
[status.trailingAnchor constraintLessThanOrEqualToAnchor:self.trailingAnchor constant:-6],
|
|
288
|
+
[status.topAnchor constraintEqualToAnchor:name.bottomAnchor constant:1],
|
|
289
|
+
]];
|
|
290
|
+
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(reload)
|
|
291
|
+
name:CCPetsPetCardDidChangeNotification object:nil];
|
|
292
|
+
[self reload];
|
|
293
|
+
}
|
|
294
|
+
return self;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
- (void)dealloc {
|
|
298
|
+
[NSNotificationCenter.defaultCenter removeObserver:self];
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
- (void)viewDidChangeEffectiveAppearance {
|
|
302
|
+
[super viewDidChangeEffectiveAppearance];
|
|
303
|
+
[self applyDotColor];
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
- (void)applyDotColor {
|
|
307
|
+
[self.effectiveAppearance performAsCurrentDrawingAppearance:^{
|
|
308
|
+
self.statusDot.layer.backgroundColor = CCPetsStatusColor(self.kind).CGColor;
|
|
309
|
+
}];
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
- (void)reload {
|
|
313
|
+
id source = NSApp.delegate;
|
|
314
|
+
NSDictionary *snapshot = [source conformsToProtocol:@protocol(CCPetsPetCardSource)]
|
|
315
|
+
? [source petCardSnapshot] : nil;
|
|
316
|
+
[self.sprite setSheet:snapshot[@"sheet"] rowCount:[snapshot[@"rowCount"] integerValue] ?: 9];
|
|
317
|
+
NSString *state = snapshot[@"state"] ?: @"idle";
|
|
318
|
+
self.sprite.state = state;
|
|
319
|
+
self.nameLabel.stringValue = snapshot[@"name"] ?: @"CC Pets";
|
|
320
|
+
self.statusLabel.stringValue = snapshot[@"status"] ?: L(@"Idle");
|
|
321
|
+
self.kind = CCPetsStatusKindForAgentState(state);
|
|
322
|
+
[self applyDotColor];
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
@end
|
|
326
|
+
|
|
327
|
+
#pragma mark - 侧边栏
|
|
328
|
+
|
|
329
|
+
@interface CCPetsMainSidebarController : NSViewController
|
|
330
|
+
@property(nonatomic) CCPetsPetCardView *card;
|
|
331
|
+
@property(nonatomic) NSMutableDictionary<NSString *, CCPetsSidebarItem *> *items;
|
|
332
|
+
@property(nonatomic) NSStackView *navigation;
|
|
333
|
+
@property(nonatomic) NSView *footer;
|
|
334
|
+
@property(nonatomic, copy) NSString *currentPage;
|
|
335
|
+
@property(nonatomic, copy) void (^selectionHandler)(NSString *page);
|
|
336
|
+
@property(nonatomic) NSArray<NSLayoutConstraint *> *leadingConstraints;
|
|
337
|
+
@property(nonatomic) BOOL expanded;
|
|
338
|
+
- (void)selectPage:(NSString *)page;
|
|
339
|
+
// 全屏或铺满屏幕时(侧边栏同时加宽)整列内容右移 5pt,导航项换成宽松排版;退出时恢复。
|
|
340
|
+
- (void)setExpanded:(BOOL)expanded;
|
|
341
|
+
- (void)reload;
|
|
342
|
+
@end
|
|
343
|
+
|
|
344
|
+
@implementation CCPetsMainSidebarController
|
|
345
|
+
|
|
346
|
+
- (void)loadView {
|
|
347
|
+
NSView *root = [[NSView alloc] initWithFrame:NSMakeRect(0, 0, 210, 560)];
|
|
348
|
+
CCPetsPetCardView *card = [[CCPetsPetCardView alloc] initWithFrame:NSZeroRect];
|
|
349
|
+
card.translatesAutoresizingMaskIntoConstraints = NO;
|
|
350
|
+
self.card = card;
|
|
351
|
+
NSView *cardRule = [CCPetsLineView new];
|
|
352
|
+
NSStackView *navigation = [NSStackView stackViewWithViews:@[]];
|
|
353
|
+
navigation.orientation = NSUserInterfaceLayoutOrientationVertical;
|
|
354
|
+
navigation.alignment = NSLayoutAttributeLeading;
|
|
355
|
+
navigation.spacing = 2;
|
|
356
|
+
self.navigation = navigation;
|
|
357
|
+
NSView *footer = [NSView new];
|
|
358
|
+
self.footer = footer;
|
|
359
|
+
NSView *footerRule = [CCPetsLineView new];
|
|
360
|
+
for (NSView *view in @[card, cardRule, navigation, footer, footerRule]) {
|
|
361
|
+
view.translatesAutoresizingMaskIntoConstraints = NO;
|
|
362
|
+
[root addSubview:view];
|
|
363
|
+
}
|
|
364
|
+
NSLayoutConstraint *cardLeading = [card.leadingAnchor constraintEqualToAnchor:root.leadingAnchor constant:10];
|
|
365
|
+
NSLayoutConstraint *cardRuleLeading = [cardRule.leadingAnchor constraintEqualToAnchor:root.leadingAnchor constant:16];
|
|
366
|
+
NSLayoutConstraint *navigationLeading = [navigation.leadingAnchor constraintEqualToAnchor:root.leadingAnchor constant:10];
|
|
367
|
+
NSLayoutConstraint *footerRuleLeading = [footerRule.leadingAnchor constraintEqualToAnchor:root.leadingAnchor constant:16];
|
|
368
|
+
NSLayoutConstraint *footerLeading = [footer.leadingAnchor constraintEqualToAnchor:root.leadingAnchor constant:10];
|
|
369
|
+
self.leadingConstraints = @[cardLeading, cardRuleLeading, navigationLeading, footerRuleLeading, footerLeading];
|
|
370
|
+
NSLayoutConstraint *cardTop = [card.topAnchor constraintEqualToAnchor:root.safeAreaLayoutGuide.topAnchor constant:2];
|
|
371
|
+
cardTop.priority = NSLayoutPriorityDefaultHigh;
|
|
372
|
+
[NSLayoutConstraint activateConstraints:@[
|
|
373
|
+
// 名片从标题栏下沿开始,左右和导航项对齐。
|
|
374
|
+
cardTop,
|
|
375
|
+
// 全屏时工具栏收起、安全区变成 0,这时至少离顶边 18pt,和右侧页头标题大致齐平。
|
|
376
|
+
[card.topAnchor constraintGreaterThanOrEqualToAnchor:root.topAnchor constant:18],
|
|
377
|
+
cardLeading,
|
|
378
|
+
[card.trailingAnchor constraintEqualToAnchor:root.trailingAnchor constant:-10],
|
|
379
|
+
[cardRule.topAnchor constraintEqualToAnchor:card.bottomAnchor constant:12],
|
|
380
|
+
cardRuleLeading,
|
|
381
|
+
[cardRule.trailingAnchor constraintEqualToAnchor:root.trailingAnchor constant:-16],
|
|
382
|
+
[cardRule.heightAnchor constraintEqualToConstant:1],
|
|
383
|
+
[navigation.topAnchor constraintEqualToAnchor:cardRule.bottomAnchor constant:12],
|
|
384
|
+
navigationLeading,
|
|
385
|
+
[navigation.trailingAnchor constraintEqualToAnchor:root.trailingAnchor constant:-10],
|
|
386
|
+
[navigation.bottomAnchor constraintLessThanOrEqualToAnchor:footerRule.topAnchor constant:-8],
|
|
387
|
+
footerRuleLeading,
|
|
388
|
+
[footerRule.trailingAnchor constraintEqualToAnchor:root.trailingAnchor constant:-16],
|
|
389
|
+
[footerRule.heightAnchor constraintEqualToConstant:1],
|
|
390
|
+
[footerRule.bottomAnchor constraintEqualToAnchor:footer.topAnchor constant:-8],
|
|
391
|
+
footerLeading,
|
|
392
|
+
[footer.trailingAnchor constraintEqualToAnchor:root.trailingAnchor constant:-10],
|
|
393
|
+
[footer.bottomAnchor constraintEqualToAnchor:root.bottomAnchor constant:-10],
|
|
394
|
+
]];
|
|
395
|
+
self.view = root;
|
|
396
|
+
[self rebuildItems];
|
|
397
|
+
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(refreshBadges)
|
|
398
|
+
name:CCPetsPetCardDidChangeNotification object:nil];
|
|
399
|
+
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(refreshBadges)
|
|
400
|
+
name:CCPetsDiagnosticsDidChangeNotification object:nil];
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
- (void)dealloc {
|
|
404
|
+
[NSNotificationCenter.defaultCenter removeObserver:self];
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
- (NSTextField *)groupLabel:(NSString *)title {
|
|
408
|
+
NSTextField *label = CCPetsLabel(SidebarDisplayTitle(title, self.expanded), 11, NSFontWeightSemibold, CCPetsTertiaryInkColor());
|
|
409
|
+
label.accessibilityLabel = title;
|
|
410
|
+
return label;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
- (void)rebuildItems {
|
|
414
|
+
for (NSView *view in [self.navigation.arrangedSubviews copy]) {
|
|
415
|
+
[self.navigation removeArrangedSubview:view];
|
|
416
|
+
[view removeFromSuperview];
|
|
417
|
+
}
|
|
418
|
+
for (NSView *view in [self.footer.subviews copy]) [view removeFromSuperview];
|
|
419
|
+
self.items = [NSMutableDictionary dictionary];
|
|
420
|
+
__weak typeof(self) weakSelf = self;
|
|
421
|
+
NSString *group = nil;
|
|
422
|
+
NSDictionary *groupTitles = @{@"workbench": L(@"Workbench"), @"settings": L(@"General")};
|
|
423
|
+
for (NSDictionary *page in MainWindowPages()) {
|
|
424
|
+
CCPetsSidebarItem *item = [[CCPetsSidebarItem alloc] initWithPage:page spacious:self.expanded];
|
|
425
|
+
item.onSelect = ^(NSString *pageID) {
|
|
426
|
+
if (weakSelf.selectionHandler) weakSelf.selectionHandler(pageID);
|
|
427
|
+
};
|
|
428
|
+
self.items[page[@"id"]] = item;
|
|
429
|
+
if ([page[@"group"] isEqualToString:@"footer"]) {
|
|
430
|
+
[self.footer addSubview:item];
|
|
431
|
+
[NSLayoutConstraint activateConstraints:@[
|
|
432
|
+
[item.leadingAnchor constraintEqualToAnchor:self.footer.leadingAnchor],
|
|
433
|
+
[item.trailingAnchor constraintEqualToAnchor:self.footer.trailingAnchor],
|
|
434
|
+
[item.topAnchor constraintEqualToAnchor:self.footer.topAnchor],
|
|
435
|
+
[item.bottomAnchor constraintEqualToAnchor:self.footer.bottomAnchor],
|
|
436
|
+
]];
|
|
437
|
+
continue;
|
|
438
|
+
}
|
|
439
|
+
if (![page[@"group"] isEqualToString:group]) {
|
|
440
|
+
group = page[@"group"];
|
|
441
|
+
NSTextField *label = [self groupLabel:groupTitles[group] ?: @""];
|
|
442
|
+
NSView *last = self.navigation.arrangedSubviews.lastObject;
|
|
443
|
+
if (last) [self.navigation setCustomSpacing:14 afterView:last];
|
|
444
|
+
[self.navigation addArrangedSubview:label];
|
|
445
|
+
[self.navigation setCustomSpacing:4 afterView:label];
|
|
446
|
+
[label.leadingAnchor constraintEqualToAnchor:self.navigation.leadingAnchor constant:10].active = YES;
|
|
447
|
+
}
|
|
448
|
+
[self.navigation addArrangedSubview:item];
|
|
449
|
+
[item.leadingAnchor constraintEqualToAnchor:self.navigation.leadingAnchor].active = YES;
|
|
450
|
+
[item.trailingAnchor constraintEqualToAnchor:self.navigation.trailingAnchor].active = YES;
|
|
451
|
+
}
|
|
452
|
+
if (self.currentPage) [self selectPage:self.currentPage];
|
|
453
|
+
[self refreshBadges];
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
// 角标:会话页是在线会话数(有等审批的就用提醒色),诊断页是要处理的问题数。
|
|
457
|
+
- (void)refreshBadges {
|
|
458
|
+
id source = NSApp.delegate;
|
|
459
|
+
NSDictionary *snapshot = [source conformsToProtocol:@protocol(CCPetsPetCardSource)]
|
|
460
|
+
? [source petCardSnapshot] : nil;
|
|
461
|
+
CCPetsCountBadge *sessions = self.items[CCPetsMainWindowPageSessions].badge;
|
|
462
|
+
sessions.count = [snapshot[@"sessions"] integerValue];
|
|
463
|
+
sessions.warn = [snapshot[@"approvals"] integerValue] > 0;
|
|
464
|
+
CCPetsCountBadge *diagnostics = self.items[CCPetsMainWindowPageDiagnostics].badge;
|
|
465
|
+
diagnostics.count = (NSInteger)CCPetsDiagnosticsStore.issueCount;
|
|
466
|
+
diagnostics.warn = YES;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
- (void)reload {
|
|
470
|
+
[self.card reload];
|
|
471
|
+
[self rebuildItems];
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
- (void)setExpanded:(BOOL)expanded {
|
|
475
|
+
[self view];
|
|
476
|
+
_expanded = expanded;
|
|
477
|
+
// 每条左边距的基准值:名片、导航、底部诊断 10pt,两条分隔线 16pt。
|
|
478
|
+
static const CGFloat bases[] = {10, 16, 10, 16, 10};
|
|
479
|
+
for (NSUInteger index = 0; index < self.leadingConstraints.count; index++) {
|
|
480
|
+
self.leadingConstraints[index].constant = bases[index] + (expanded ? 5 : 0);
|
|
481
|
+
}
|
|
482
|
+
// 图标间距和字间空格是建导航项时定的,整列重建。
|
|
483
|
+
[self rebuildItems];
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
- (void)selectPage:(NSString *)page {
|
|
487
|
+
[self view];
|
|
488
|
+
self.currentPage = page;
|
|
489
|
+
[self.items enumerateKeysAndObjectsUsingBlock:^(NSString *pageID, CCPetsSidebarItem *item, BOOL *stop) {
|
|
490
|
+
item.selected = [pageID isEqualToString:page];
|
|
491
|
+
}];
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
@end
|
|
495
|
+
|
|
496
|
+
#pragma mark - 窗口
|
|
497
|
+
|
|
498
|
+
static const CGFloat PageTopLift = 36;
|
|
499
|
+
|
|
500
|
+
static NSLayoutConstraint *PageTopConstraint(NSView *view, NSView *host) {
|
|
501
|
+
NSLayoutConstraint *top = [view.topAnchor constraintEqualToAnchor:host.safeAreaLayoutGuide.topAnchor
|
|
502
|
+
constant:-PageTopLift];
|
|
503
|
+
top.priority = NSLayoutPriorityDefaultHigh;
|
|
504
|
+
return top;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
@interface CCPetsMainWindowController () <NSWindowDelegate>
|
|
508
|
+
@property(nonatomic) NSSplitViewController *splitController;
|
|
509
|
+
@property(nonatomic) CCPetsMainSidebarController *sidebar;
|
|
510
|
+
@property(nonatomic) NSViewController *contentController;
|
|
511
|
+
@property(nonatomic) NSViewController *pageController;
|
|
512
|
+
@property(nonatomic, copy) NSString *currentPage;
|
|
513
|
+
@property(nonatomic) NSSplitViewItem *sidebarItem;
|
|
514
|
+
// 进全屏 / 铺满屏幕前侧边栏的宽度,退出时还原。
|
|
515
|
+
@property(nonatomic) CGFloat windowedSidebarWidth;
|
|
516
|
+
@property(nonatomic) BOOL inFullScreen;
|
|
517
|
+
@property(nonatomic) BOOL sidebarExpanded;
|
|
518
|
+
@end
|
|
519
|
+
|
|
520
|
+
// 全屏或 zoom 铺满屏幕时窗口宽,侧边栏在常规宽度上加宽 10pt,内容右移 5pt,
|
|
521
|
+
// 和铺满的内容区比例更协调。
|
|
522
|
+
static const CGFloat SidebarExpandedExtraWidth = 10;
|
|
523
|
+
|
|
524
|
+
static CCPetsMainWindowController *SharedMainWindow;
|
|
525
|
+
static id WindowCloseObserver;
|
|
526
|
+
|
|
527
|
+
@implementation CCPetsMainWindowController
|
|
528
|
+
|
|
529
|
+
+ (void)presentPage:(NSString *)page {
|
|
530
|
+
if (!SharedMainWindow) SharedMainWindow = [CCPetsMainWindowController new];
|
|
531
|
+
NSString *target = CCPetsMainWindowResolvePage(page) ?:
|
|
532
|
+
CCPetsMainWindowResolvePage([NSUserDefaults.standardUserDefaults stringForKey:MainWindowLastPageKey]);
|
|
533
|
+
if (!target) target = MainWindowPages().firstObject[@"id"];
|
|
534
|
+
[SharedMainWindow showPage:target];
|
|
535
|
+
[self enterRegularMode];
|
|
536
|
+
[NSApp activateIgnoringOtherApps:YES];
|
|
537
|
+
[SharedMainWindow.window makeKeyAndOrderFront:nil];
|
|
538
|
+
// 上次关窗时是铺满屏幕的大小,恢复出来也要用加宽的侧边栏。
|
|
539
|
+
[SharedMainWindow updateSidebarExpansion];
|
|
540
|
+
// 侧边栏的诊断角标要先检查一遍;结果缓存一阵,频繁开关窗口不会反复跑。
|
|
541
|
+
[CCPetsDiagnosticsStore refreshIfStale];
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
+ (BOOL)restoreFromDock {
|
|
545
|
+
NSWindow *window = SharedMainWindow.window;
|
|
546
|
+
if (!window) return NO;
|
|
547
|
+
if (NSApp.isHidden) [NSApp unhide:nil];
|
|
548
|
+
if (window.isMiniaturized) [window deminiaturize:nil];
|
|
549
|
+
[window makeKeyAndOrderFront:nil];
|
|
550
|
+
return YES;
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
// 有 Dock 图标、能 ⌘Tab、显示菜单栏。顺带开始盯普通窗口的关闭,最后一个关掉时切回去。
|
|
554
|
+
+ (void)enterRegularMode {
|
|
555
|
+
if (NSApp.activationPolicy != NSApplicationActivationPolicyRegular) {
|
|
556
|
+
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
|
|
557
|
+
}
|
|
558
|
+
if (WindowCloseObserver) return;
|
|
559
|
+
WindowCloseObserver = [NSNotificationCenter.defaultCenter
|
|
560
|
+
addObserverForName:NSWindowWillCloseNotification object:nil queue:NSOperationQueue.mainQueue
|
|
561
|
+
usingBlock:^(NSNotification *note) {
|
|
562
|
+
// willClose 时窗口还在屏幕上,等它真的收起再判断。
|
|
563
|
+
dispatch_async(dispatch_get_main_queue(), ^{ [CCPetsMainWindowController leaveRegularModeIfIdle]; });
|
|
564
|
+
}];
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
+ (void)leaveRegularModeIfIdle {
|
|
568
|
+
if (HasVisibleRegularWindow()) return;
|
|
569
|
+
if (WindowCloseObserver) {
|
|
570
|
+
[NSNotificationCenter.defaultCenter removeObserver:WindowCloseObserver];
|
|
571
|
+
WindowCloseObserver = nil;
|
|
572
|
+
}
|
|
573
|
+
if (NSApp.activationPolicy == NSApplicationActivationPolicyAccessory) return;
|
|
574
|
+
[NSApp setActivationPolicy:NSApplicationActivationPolicyAccessory];
|
|
575
|
+
// 切回 Accessory 后菜单栏不会自己让出来,主动交还给下一个 App。
|
|
576
|
+
[NSApp deactivate];
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
- (instancetype)init {
|
|
580
|
+
NSWindow *window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 940, 640)
|
|
581
|
+
styleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable |
|
|
582
|
+
NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable |
|
|
583
|
+
NSWindowStyleMaskFullSizeContentView
|
|
584
|
+
backing:NSBackingStoreBuffered defer:YES];
|
|
585
|
+
if ((self = [super initWithWindow:window])) {
|
|
586
|
+
window.releasedWhenClosed = NO;
|
|
587
|
+
window.minSize = NSMakeSize(760, 520);
|
|
588
|
+
window.delegate = self;
|
|
589
|
+
window.appearance = MainWindowAppearance();
|
|
590
|
+
window.titlebarAppearsTransparent = YES;
|
|
591
|
+
window.titleVisibility = NSWindowTitleHidden;
|
|
592
|
+
// 内容区的底色;标题栏透明,延伸到标题栏底下的部分也是这个颜色。
|
|
593
|
+
window.backgroundColor = CCPetsBackgroundColor();
|
|
594
|
+
// 空工具栏 + unified:侧边栏一直延伸到标题栏,和系统设置一个样子。
|
|
595
|
+
window.toolbar = [[NSToolbar alloc] initWithIdentifier:@"CCPetsMainWindowToolbar"];
|
|
596
|
+
window.toolbarStyle = NSWindowToolbarStyleUnified;
|
|
597
|
+
|
|
598
|
+
self.sidebar = [CCPetsMainSidebarController new];
|
|
599
|
+
__weak typeof(self) weakSelf = self;
|
|
600
|
+
self.sidebar.selectionHandler = ^(NSString *page) { [weakSelf showPage:page]; };
|
|
601
|
+
self.contentController = [NSViewController new];
|
|
602
|
+
self.contentController.view = [[NSView alloc] initWithFrame:NSMakeRect(0, 0, 720, 640)];
|
|
603
|
+
|
|
604
|
+
NSSplitViewController *split = [NSSplitViewController new];
|
|
605
|
+
NSSplitViewItem *sidebarItem = [NSSplitViewItem sidebarWithViewController:self.sidebar];
|
|
606
|
+
sidebarItem.minimumThickness = 200;
|
|
607
|
+
sidebarItem.maximumThickness = 240;
|
|
608
|
+
sidebarItem.canCollapse = NO;
|
|
609
|
+
self.sidebarItem = sidebarItem;
|
|
610
|
+
[split addSplitViewItem:sidebarItem];
|
|
611
|
+
NSSplitViewItem *contentItem = [NSSplitViewItem splitViewItemWithViewController:self.contentController];
|
|
612
|
+
contentItem.minimumThickness = 520;
|
|
613
|
+
[split addSplitViewItem:contentItem];
|
|
614
|
+
self.splitController = split;
|
|
615
|
+
window.contentViewController = split;
|
|
616
|
+
[window setContentSize:NSMakeSize(940, 640)];
|
|
617
|
+
// 记住上次的位置和大小;第一次打开时居中。
|
|
618
|
+
if (![window setFrameUsingName:MainWindowFrameName]) [window center];
|
|
619
|
+
window.frameAutosaveName = MainWindowFrameName;
|
|
620
|
+
|
|
621
|
+
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(languageDidChange:)
|
|
622
|
+
name:CCPetsLanguageDidChangeNotification object:nil];
|
|
623
|
+
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(preferencesDidChange:)
|
|
624
|
+
name:CCPetsPreferencesDidChangeNotification object:nil];
|
|
625
|
+
}
|
|
626
|
+
return self;
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
// 颜色都是动态色,换了窗口外观会自己重新解析,不用重建页面。
|
|
630
|
+
- (void)preferencesDidChange:(NSNotification *)notification {
|
|
631
|
+
if (![notification.userInfo[CCPetsPreferencesChangedKeyKey] isEqualToString:CCPetsMainWindowAppearanceKey]) return;
|
|
632
|
+
self.window.appearance = MainWindowAppearance();
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
- (void)dealloc {
|
|
636
|
+
[NSNotificationCenter.defaultCenter removeObserver:self];
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
- (void)showPage:(NSString *)page {
|
|
640
|
+
NSDictionary *entry = MainWindowPageWithID(page);
|
|
641
|
+
if (!entry) return;
|
|
642
|
+
// 标题栏不显示标题(页头里已经有),但窗口菜单和调度中心要用它。
|
|
643
|
+
self.window.title = entry[@"title"];
|
|
644
|
+
[self.sidebar selectPage:page];
|
|
645
|
+
if ([self.currentPage isEqualToString:page] && self.pageController) return;
|
|
646
|
+
[self.pageController.view removeFromSuperview];
|
|
647
|
+
[self.pageController removeFromParentViewController];
|
|
648
|
+
NSViewController *controller = [entry[@"class"] new];
|
|
649
|
+
[self.contentController addChildViewController:controller];
|
|
650
|
+
NSView *host = self.contentController.view;
|
|
651
|
+
NSView *view = controller.view;
|
|
652
|
+
view.translatesAutoresizingMaskIntoConstraints = NO;
|
|
653
|
+
[host addSubview:view];
|
|
654
|
+
[NSLayoutConstraint activateConstraints:@[
|
|
655
|
+
[view.leadingAnchor constraintEqualToAnchor:host.leadingAnchor],
|
|
656
|
+
[view.trailingAnchor constraintEqualToAnchor:host.trailingAnchor],
|
|
657
|
+
// 窗口是 FullSizeContentView,内容区会延伸到标题栏底下。空工具栏让安全区有 66pt,
|
|
658
|
+
// 页头标题上方显得太空,往上收 36pt(原空白 72pt 的一半);红绿灯在侧边栏那一侧,压不到。
|
|
659
|
+
// 全屏时工具栏收起、安全区变成 0,这时至少离顶边 24pt,标题不贴着屏幕边。
|
|
660
|
+
[view.topAnchor constraintGreaterThanOrEqualToAnchor:host.topAnchor constant:24],
|
|
661
|
+
PageTopConstraint(view, host),
|
|
662
|
+
[view.bottomAnchor constraintEqualToAnchor:host.bottomAnchor],
|
|
663
|
+
]];
|
|
664
|
+
self.pageController = controller;
|
|
665
|
+
self.currentPage = page;
|
|
666
|
+
[NSUserDefaults.standardUserDefaults setObject:page forKey:MainWindowLastPageKey];
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
// 页面里的文案都是建页面时取的,换语言后整页重建,比逐个控件改标题省事也不会漏。
|
|
670
|
+
- (void)languageDidChange:(NSNotification *)notification {
|
|
671
|
+
[self.sidebar reload];
|
|
672
|
+
NSString *page = self.currentPage;
|
|
673
|
+
self.currentPage = nil;
|
|
674
|
+
[self showPage:page];
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
// 空工具栏只是为了让侧边栏延伸到标题栏。全屏时系统把它画成一条不透明的横条,
|
|
678
|
+
// 压在页头上(标题被盖住),所以全屏期间收起,退出时再放回来。
|
|
679
|
+
- (void)windowWillEnterFullScreen:(NSNotification *)notification {
|
|
680
|
+
self.window.toolbar.visible = NO;
|
|
681
|
+
self.inFullScreen = YES;
|
|
682
|
+
[self updateSidebarExpansion];
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
- (void)windowDidExitFullScreen:(NSNotification *)notification {
|
|
686
|
+
self.window.toolbar.visible = YES;
|
|
687
|
+
self.inFullScreen = NO;
|
|
688
|
+
[self updateSidebarExpansion];
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
// zoom(双击标题栏、按住 Option 点绿色按钮)铺满屏幕也会走到这里。拖边框的过程中不判断,
|
|
692
|
+
// 松手后再看。
|
|
693
|
+
- (void)windowDidResize:(NSNotification *)notification {
|
|
694
|
+
if (!self.window.inLiveResize) [self updateSidebarExpansion];
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
- (void)windowDidEndLiveResize:(NSNotification *)notification {
|
|
698
|
+
[self updateSidebarExpansion];
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
// 全屏或铺满屏幕时侧边栏加宽、内容换宽松排版;回到普通窗口大小时还原成之前的宽度。
|
|
702
|
+
- (void)updateSidebarExpansion {
|
|
703
|
+
BOOL expanded = self.inFullScreen || (self.window.isVisible && self.window.isZoomed);
|
|
704
|
+
if (expanded == self.sidebarExpanded) return;
|
|
705
|
+
self.sidebarExpanded = expanded;
|
|
706
|
+
if (expanded) {
|
|
707
|
+
CGFloat width = NSWidth(self.sidebar.view.frame);
|
|
708
|
+
self.windowedSidebarWidth = width > 0 ? width : 240;
|
|
709
|
+
CGFloat expandedWidth = self.windowedSidebarWidth + SidebarExpandedExtraWidth;
|
|
710
|
+
self.sidebarItem.maximumThickness = expandedWidth;
|
|
711
|
+
self.sidebarItem.minimumThickness = expandedWidth;
|
|
712
|
+
} else {
|
|
713
|
+
self.sidebarItem.minimumThickness = 200;
|
|
714
|
+
self.sidebarItem.maximumThickness = 240;
|
|
715
|
+
[self.splitController.splitView setPosition:self.windowedSidebarWidth ofDividerAtIndex:0];
|
|
716
|
+
}
|
|
717
|
+
self.sidebar.expanded = expanded;
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
- (void)windowWillClose:(NSNotification *)notification {
|
|
721
|
+
// 关窗即释放:页面持有的观察者、定时器随之结束,主窗口不占后台资源。
|
|
722
|
+
[self.pageController.view removeFromSuperview];
|
|
723
|
+
[self.pageController removeFromParentViewController];
|
|
724
|
+
self.pageController = nil;
|
|
725
|
+
if (SharedMainWindow == self) {
|
|
726
|
+
dispatch_async(dispatch_get_main_queue(), ^{ SharedMainWindow = nil; });
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
@end
|