@seekskyworld/open-browser 0.0.0-stage → 0.2.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/CONTRIBUTING.md +15 -0
- package/Docs/Agent/346/265/217/350/247/210/345/231/250/346/200/273/344/275/223/346/236/266/346/236/204.md +58 -0
- package/Docs/THIRD-PARTY-NOTICES.md +12 -0
- package/Docs/open-browser-api.d.ts +291 -0
- package/Docs/open-browser/344/273/273/345/212/241/350/257/204/346/265/213/346/214/207/345/215/227.md +59 -0
- package/Docs/open-browser/351/232/220/347/247/201/344/270/216/345/274/200/346/272/220/345/256/241/350/256/241.md +37 -0
- package/Docs//346/216/247/345/210/266/345/217/260/345/210/206/347/273/204/344/270/216/346/216/245/347/256/241/346/226/271/346/241/210.md +31 -0
- package/Docs//350/267/250/345/271/263/345/217/260/350/257/225/347/224/250/344/270/216/345/217/221/350/241/214.md +84 -0
- package/LICENSE +200 -0
- package/README.md +159 -2
- package/README.zh-CN.md +166 -0
- package/SECURITY.md +9 -0
- package/access-registry.mjs +156 -0
- package/action-execution.mjs +385 -0
- package/activity-notifier.mjs +34 -0
- package/api-contract.mjs +134 -0
- package/browser-extension/icons/open-browser-128.png +0 -0
- package/browser-extension/icons/open-browser-16.png +0 -0
- package/browser-extension/icons/open-browser-32.png +0 -0
- package/browser-extension/icons/open-browser-48.png +0 -0
- package/browser-extension/icons/open-browser-wordmark.png +0 -0
- package/browser-extension/manifest.json +35 -0
- package/browser-extension/newtab.css +26 -0
- package/browser-extension/newtab.html +19 -0
- package/browser-extension/newtab.js +174 -0
- package/browser-extension/phase-labels.js +9 -0
- package/browser-extension/popup.css +19 -0
- package/browser-extension/popup.html +56 -0
- package/browser-extension/popup.js +405 -0
- package/browser-extension/tab-action.js +138 -0
- package/browser-extension/worker.js +281 -0
- package/browser-extension/workspace-title.js +15 -0
- package/build-profile.mjs +23 -0
- package/capability-matrix.mjs +21 -0
- package/chromium-host.mjs +209 -0
- package/chromium-shell.mjs +179 -0
- package/cli.mjs +141 -0
- package/contract-validation.mjs +27 -0
- package/control.mjs +107 -0
- package/desktop/Info.plist +13 -0
- package/desktop/Launcher.m +117 -0
- package/desktop/OBControlBar.h +7 -0
- package/desktop/OBControlBar.m +217 -0
- package/desktop/OBPageView.h +6 -0
- package/desktop/OBPageView.m +348 -0
- package/desktop/WorkspacePanel.h +16 -0
- package/desktop/WorkspacePanel.m +916 -0
- package/desktop/archive-release.py +129 -0
- package/desktop/brand-keychain.py +111 -0
- package/desktop/brand-locales.py +93 -0
- package/desktop/brand-macos-icons.py +24 -0
- package/desktop/brand-windows-icons.mjs +48 -0
- package/desktop/build-macos.mjs +112 -0
- package/desktop/build-managed-input.mjs +37 -0
- package/desktop/build-portable.mjs +95 -0
- package/desktop/build-profile.mjs +6 -0
- package/desktop/build.mjs +13 -0
- package/desktop/create-shortcut.ps1 +9 -0
- package/desktop/generate-icons.py +39 -0
- package/desktop/icons/open-browser.icns +0 -0
- package/desktop/icons/open-browser.ico +0 -0
- package/desktop/install-menu-entry.mjs +24 -0
- package/desktop/macos-entitlements.plist +12 -0
- package/desktop/notarize-macos.mjs +145 -0
- package/desktop/package-assets.mjs +34 -0
- package/desktop/package-macos.mjs +54 -0
- package/desktop/patches/chromium-macos-keychain-service.patch +18 -0
- package/desktop/patches/chromium-managed-input.patch +144 -0
- package/desktop/portable-archive.py +22 -0
- package/desktop/sign.mjs +77 -0
- package/desktop/smoke-portable.mjs +65 -0
- package/desktop/start-profile.mjs +5 -0
- package/desktop/start.mjs +34 -0
- package/desktop/verify-macos-release.mjs +61 -0
- package/desktop/verify-upgrade.mjs +74 -0
- package/desktop/verify-version.mjs +12 -0
- package/desktop-page.mjs +211 -0
- package/diagnostics.mjs +37 -0
- package/downloads.mjs +110 -0
- package/engine-service.mjs +1158 -0
- package/hosted/README.md +74 -0
- package/hosted/cli.mjs +17 -0
- package/hosted/directory.mjs +261 -0
- package/hosted/init.mjs +37 -0
- package/hosted/server.mjs +129 -0
- package/managed-input.mjs +138 -0
- package/managed-popup.mjs +54 -0
- package/mcp-http.mjs +84 -0
- package/mcp-server.mjs +41 -0
- package/mcp.mjs +5 -0
- package/network-policy.mjs +107 -0
- package/output-store.mjs +96 -0
- package/package.json +77 -4
- package/page-activity.mjs +188 -0
- package/page-dialogs.mjs +53 -0
- package/page-observation.mjs +318 -0
- package/paths.mjs +22 -0
- package/privacy-config.mjs +95 -0
- package/receipt-store.mjs +198 -0
- package/runtime.mjs +53 -0
- package/scheduler.mjs +70 -0
- package/sdk.mjs +193 -0
- package/service.mjs +290 -0
- package/site-workflow.mjs +115 -0
- package/skill-links.mjs +90 -0
- package/skills/open-browser/SKILL.macos.md +112 -0
- package/skills/open-browser/SKILL.md +107 -0
- package/skills/open-browser/SKILL.windows.md +112 -0
- package/skills/open-browser/scripts/bootstrap.mjs +213 -0
- package/skills/open-browser/scripts/open-browser +21 -0
- package/skills/open-browser/scripts/open-browser.cmd +9 -0
- package/skills/open-browser/workflows/login-export.json +10 -0
- package/skills/open-browser/workflows/upload-verify.json +8 -0
- package/studio/demo-site.mjs +111 -0
- package/studio/demo.css +1 -0
- package/studio/index.html +66 -0
- package/studio/index.mjs +57 -0
- package/studio/native-demo.mjs +124 -0
- package/studio/studio.css +119 -0
- package/studio/studio.js +438 -0
- package/tab-adoption.mjs +77 -0
- package/uploads.mjs +63 -0
- package/visual-page.mjs +306 -0
- package/workspace-lifecycle.mjs +238 -0
- package/workspace-state.mjs +107 -0
|
@@ -0,0 +1,916 @@
|
|
|
1
|
+
#import "WorkspacePanel.h"
|
|
2
|
+
#import "OBPageView.h"
|
|
3
|
+
|
|
4
|
+
static NSDictionary *Map(id value) {
|
|
5
|
+
return [value isKindOfClass:NSDictionary.class] ? value : nil;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
static NSString *String(id value) {
|
|
9
|
+
return [value isKindOfClass:NSString.class] ? value : @"";
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
static NSTextField *Label(NSString *text, CGFloat size, NSFontWeight weight, NSColor *color) {
|
|
13
|
+
NSTextField *field = [NSTextField labelWithString:text];
|
|
14
|
+
field.font = [NSFont systemFontOfSize:size weight:weight];
|
|
15
|
+
field.textColor = color;
|
|
16
|
+
field.lineBreakMode = NSLineBreakByTruncatingTail;
|
|
17
|
+
[field setContentCompressionResistancePriority:NSLayoutPriorityDefaultLow forOrientation:NSLayoutConstraintOrientationHorizontal];
|
|
18
|
+
return field;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
static NSStackView *Stack(NSArray<NSView *> *items, BOOL vertical, CGFloat gap) {
|
|
22
|
+
NSStackView *stack = [NSStackView stackViewWithViews:items];
|
|
23
|
+
stack.orientation = vertical ? NSUserInterfaceLayoutOrientationVertical : NSUserInterfaceLayoutOrientationHorizontal;
|
|
24
|
+
stack.alignment = vertical ? NSLayoutAttributeLeading : NSLayoutAttributeCenterY;
|
|
25
|
+
stack.spacing = gap;
|
|
26
|
+
stack.translatesAutoresizingMaskIntoConstraints = NO;
|
|
27
|
+
return stack;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
static NSString *Operation(NSDictionary *op) {
|
|
31
|
+
if (!op) return @"";
|
|
32
|
+
NSDictionary *kinds = @{@"openPage":@"打开页面", @"observe":@"观察页面", @"act":@"页面操作",
|
|
33
|
+
@"click":@"点击", @"fill":@"填写", @"fillCredential":@"填写凭据"};
|
|
34
|
+
NSString *kind = kinds[String(op[@"kind"])] ?: @"操作";
|
|
35
|
+
NSString *step = kinds[String(op[@"stepKind"])];
|
|
36
|
+
NSDictionary *phases = @{@"actionability": @"检查目标", @"dispatching": @"正在派发",
|
|
37
|
+
@"waiting": @"等待页面", @"verifying": @"核对结果", @"running": @"执行中"};
|
|
38
|
+
if (step && [op[@"state"] isEqual:@"running"]) {
|
|
39
|
+
NSString *stage = phases[String(op[@"phase"])] ?: phases[@"running"];
|
|
40
|
+
return [NSString stringWithFormat:@"%@ · %@ · %@/%@ 步", step, stage,
|
|
41
|
+
op[@"completedSteps"] ?: @0, op[@"totalSteps"] ?: @0];
|
|
42
|
+
}
|
|
43
|
+
NSDictionary *states = @{@"queued":@"排队中", @"running":@"执行中", @"completed":@"已完成",
|
|
44
|
+
@"failed":@"失败", @"outcome_unknown":@"结果待核对", @"cancelled":@"已取消"};
|
|
45
|
+
NSString *state = [op[@"error"] isEqual:@"credential_required"] ? @"缺少凭据" : states[String(op[@"state"])];
|
|
46
|
+
return state ? [NSString stringWithFormat:@"%@ · %@", kind, state] : kind;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
static NSInteger Group(NSDictionary *workspace) {
|
|
50
|
+
NSDictionary *activity = workspace[@"activity"];
|
|
51
|
+
if (![workspace[@"mode"] isEqual:@"agent"]) return 2;
|
|
52
|
+
if ([activity[@"needsCredential"] boolValue] || [@[@"failed", @"outcome_unknown", @"needs_credential"] containsObject:String(activity[@"phase"])]) return 0;
|
|
53
|
+
if (Map(activity[@"current"]) || [activity[@"queued"] integerValue] || [@[@"queued", @"running"] containsObject:String(activity[@"phase"])]) return 1;
|
|
54
|
+
return 3;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
static NSString *Status(NSDictionary *workspace) {
|
|
58
|
+
if ([workspace[@"mode"] isEqual:@"human"]) return @"用户控制 · Agent 已暂停";
|
|
59
|
+
if ([workspace[@"mode"] isEqual:@"transferring"]) return @"正在交接控制权";
|
|
60
|
+
NSDictionary *activity = workspace[@"activity"];
|
|
61
|
+
if ([activity[@"needsHumanVerification"] boolValue]) return @"需要人工验证";
|
|
62
|
+
if ([activity[@"needsCredential"] boolValue]) return @"等待补充凭据";
|
|
63
|
+
NSString *current = Operation(Map(activity[@"current"]));
|
|
64
|
+
if (current.length) return current;
|
|
65
|
+
NSDictionary *phase = @{@"running":@"执行中", @"actionability":@"检查目标", @"dispatching":@"正在派发",
|
|
66
|
+
@"waiting":@"等待页面", @"verifying":@"核对结果", @"queued":@"排队中", @"completed":@"已完成", @"failed":@"执行失败",
|
|
67
|
+
@"outcome_unknown":@"结果待核对", @"cancelled":@"已取消", @"needs_credential":@"等待补充凭据", @"needs_human_verification":@"需要人工验证"};
|
|
68
|
+
return phase[String(activity[@"phase"])] ?: @"等待指令";
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
static NSDictionary *Find(NSArray *items, NSString *key, NSString *value) {
|
|
72
|
+
for (NSDictionary *item in items) if ([item[key] isEqual:value]) return item;
|
|
73
|
+
return nil;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
@interface WorkspacePanel () <NSTableViewDataSource, NSTableViewDelegate, NSWindowDelegate, NSTextFieldDelegate>
|
|
77
|
+
@property(copy) WorkspaceCall call;
|
|
78
|
+
@property(strong) NSWindow *window;
|
|
79
|
+
@property(strong) OBPageView *pageView;
|
|
80
|
+
@property(strong) OBPageView *secondaryPageView;
|
|
81
|
+
@property(strong) NSSplitView *pageSplit;
|
|
82
|
+
@property(copy) NSDictionary *splitPage;
|
|
83
|
+
@property(copy) NSArray *displayedPage;
|
|
84
|
+
@property(strong) NSMutableSet<NSString *> *collapsed;
|
|
85
|
+
@property(copy) NSString *addressIdentity, *currentURL;
|
|
86
|
+
@property(strong) NSMutableDictionary<NSString *,NSMutableArray *> *drafts;
|
|
87
|
+
@property BOOL addressEdited, canGoBack, canGoForward;
|
|
88
|
+
@property(copy) NSDictionary *locationPending;
|
|
89
|
+
@property(strong) NSTextField *address;
|
|
90
|
+
@property(strong) NSButton *back, *forward, *reload, *home, *splitButton, *tabButton;
|
|
91
|
+
@property(strong) NSPopUpButton *more;
|
|
92
|
+
@property(strong) NSMenuItem *preview, *credential, *feedback, *close, *closePageButton;
|
|
93
|
+
@property(strong) NSTableView *table;
|
|
94
|
+
@property(strong) NSTextField *status, *notice;
|
|
95
|
+
@property(strong) NSTextField *emptyTitle;
|
|
96
|
+
@property(strong) NSButton *emptyAction;
|
|
97
|
+
@property(strong) NSButton *create, *control;
|
|
98
|
+
@property(strong) NSStackView *emptyView;
|
|
99
|
+
@property(strong) NSArray *workspaces, *rows;
|
|
100
|
+
@property(copy) NSString *workspaceID, *pageID;
|
|
101
|
+
@property(strong) NSTimer *timer;
|
|
102
|
+
@property BOOL refreshing, syncingSelection, busy, connected;
|
|
103
|
+
@property NSUInteger revision;
|
|
104
|
+
@property(strong) NSAlert *previewSheet;
|
|
105
|
+
@property(strong) NSAlert *credentialSheet;
|
|
106
|
+
@property(strong) NSSecureTextField *secretField;
|
|
107
|
+
@property(copy) NSDictionary *pendingSelection;
|
|
108
|
+
@end
|
|
109
|
+
|
|
110
|
+
@implementation WorkspacePanel
|
|
111
|
+
|
|
112
|
+
- (instancetype)initWithCall:(WorkspaceCall)call {
|
|
113
|
+
if ((self = [super init])) {
|
|
114
|
+
_call = [call copy];
|
|
115
|
+
_workspaces = @[];
|
|
116
|
+
_rows = @[];
|
|
117
|
+
_collapsed = [NSMutableSet set];
|
|
118
|
+
_drafts = [NSMutableDictionary dictionary];
|
|
119
|
+
[self buildPanel];
|
|
120
|
+
}
|
|
121
|
+
return self;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
- (NSButton *)button:(NSString *)title action:(SEL)action {
|
|
125
|
+
NSButton *button = [NSButton buttonWithTitle:title target:self action:action];
|
|
126
|
+
button.controlSize = NSControlSizeSmall;
|
|
127
|
+
button.font = [NSFont systemFontOfSize:NSFont.smallSystemFontSize];
|
|
128
|
+
return button;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
- (NSButton *)icon:(NSString *)symbol label:(NSString *)label action:(SEL)action {
|
|
132
|
+
NSButton *button = [self button:@"" action:action];
|
|
133
|
+
button.image = [NSImage imageWithSystemSymbolName:symbol accessibilityDescription:label];
|
|
134
|
+
button.imagePosition = NSImageOnly;
|
|
135
|
+
button.bordered = NO;
|
|
136
|
+
button.toolTip = label;
|
|
137
|
+
[button setAccessibilityLabel:label];
|
|
138
|
+
[button.widthAnchor constraintEqualToConstant:25].active = YES;
|
|
139
|
+
[button.heightAnchor constraintEqualToConstant:25].active = YES;
|
|
140
|
+
return button;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
- (NSMenuItem *)menuItem:(NSString *)title action:(SEL)action {
|
|
144
|
+
NSMenuItem *item = [self.more.menu addItemWithTitle:title action:action keyEquivalent:@""];
|
|
145
|
+
item.target = self;
|
|
146
|
+
return item;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
- (void)buildPanel {
|
|
150
|
+
NSRect screen = (NSScreen.mainScreen ?: NSScreen.screens.firstObject).visibleFrame;
|
|
151
|
+
NSSize size = NSMakeSize(MIN(1180, MAX(760, screen.size.width - 72)), MIN(790, MAX(470, screen.size.height - 85)));
|
|
152
|
+
self.window = [[NSWindow alloc] initWithContentRect:(NSRect){NSZeroPoint, size}
|
|
153
|
+
styleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskResizable | NSWindowStyleMaskMiniaturizable
|
|
154
|
+
backing:NSBackingStoreBuffered defer:NO];
|
|
155
|
+
self.window.title = @"open-browser";
|
|
156
|
+
self.window.releasedWhenClosed = NO;
|
|
157
|
+
self.window.delegate = self;
|
|
158
|
+
self.window.contentMinSize = NSMakeSize(760, 470);
|
|
159
|
+
|
|
160
|
+
self.status = Label(@"", 11, NSFontWeightRegular, NSColor.secondaryLabelColor);
|
|
161
|
+
self.notice = [NSTextField wrappingLabelWithString:@""];
|
|
162
|
+
self.notice.font = [NSFont systemFontOfSize:12];
|
|
163
|
+
self.notice.maximumNumberOfLines = 2;
|
|
164
|
+
self.notice.hidden = YES;
|
|
165
|
+
self.create = [self icon:@"plus" label:@"新建工作区" action:@selector(createWorkspace:)];
|
|
166
|
+
self.back = [self icon:@"chevron.left" label:@"后退" action:@selector(goBack:)];
|
|
167
|
+
self.forward = [self icon:@"chevron.right" label:@"前进" action:@selector(goForward:)];
|
|
168
|
+
self.reload = [self icon:@"arrow.clockwise" label:@"刷新" action:@selector(reloadPage:)];
|
|
169
|
+
self.home = [self icon:@"house" label:@"主页" action:@selector(home:)];
|
|
170
|
+
self.splitButton = [self icon:@"rectangle.split.2x1" label:@"拆分页面" action:@selector(toggleSplit:)];
|
|
171
|
+
self.tabButton = [self icon:@"plus" label:@"新建标签页" action:@selector(newTab:)];
|
|
172
|
+
self.control = [self button:@"接管" action:@selector(changeControl:)];
|
|
173
|
+
self.more = [[NSPopUpButton alloc] initWithFrame:NSZeroRect pullsDown:YES];
|
|
174
|
+
self.more.bordered = NO;
|
|
175
|
+
self.more.controlSize = NSControlSizeSmall;
|
|
176
|
+
self.more.toolTip = @"更多操作";
|
|
177
|
+
[self.more setAccessibilityLabel:@"更多操作"];
|
|
178
|
+
self.more.menu.autoenablesItems = NO;
|
|
179
|
+
[self.more addItemWithTitle:@""];
|
|
180
|
+
self.more.itemArray.firstObject.image = [NSImage imageWithSystemSymbolName:@"ellipsis.circle" accessibilityDescription:@"更多操作"];
|
|
181
|
+
self.more.imagePosition = NSImageOnly;
|
|
182
|
+
self.credential = [self menuItem:@"绑定凭据…" action:@selector(credentials:)];
|
|
183
|
+
self.preview = [self menuItem:@"查看页面文本…" action:@selector(previewPage:)];
|
|
184
|
+
self.feedback = [self menuItem:@"反馈问题…" action:@selector(feedback:)];
|
|
185
|
+
[self.more.menu addItem:NSMenuItem.separatorItem];
|
|
186
|
+
self.closePageButton = [self menuItem:@"关闭标签页" action:@selector(closePage:)];
|
|
187
|
+
self.close = [self menuItem:@"关闭工作区…" action:@selector(closeWorkspace:)];
|
|
188
|
+
self.address = [[NSTextField alloc] initWithFrame:NSZeroRect];
|
|
189
|
+
self.address.placeholderString = @"输入网址";
|
|
190
|
+
self.address.bezelStyle = NSTextFieldRoundedBezel;
|
|
191
|
+
self.address.font = [NSFont systemFontOfSize:13];
|
|
192
|
+
self.address.usesSingleLineMode = YES;
|
|
193
|
+
self.address.delegate = self;
|
|
194
|
+
self.address.target = self;
|
|
195
|
+
self.address.action = @selector(submitAddress:);
|
|
196
|
+
[(NSTextFieldCell *)self.address.cell setSendsActionOnEndEditing:NO];
|
|
197
|
+
[self.address setAccessibilityLabel:@"地址栏"];
|
|
198
|
+
self.window.initialFirstResponder = self.address;
|
|
199
|
+
self.table = [[NSTableView alloc] initWithFrame:NSZeroRect];
|
|
200
|
+
NSTableColumn *column = [[NSTableColumn alloc] initWithIdentifier:@"workspace"];
|
|
201
|
+
column.resizingMask = NSTableColumnAutoresizingMask;
|
|
202
|
+
[self.table addTableColumn:column];
|
|
203
|
+
self.table.headerView = nil;
|
|
204
|
+
self.table.delegate = self;
|
|
205
|
+
self.table.dataSource = self;
|
|
206
|
+
self.table.usesAutomaticRowHeights = NO;
|
|
207
|
+
self.table.floatsGroupRows = NO;
|
|
208
|
+
self.table.columnAutoresizingStyle = NSTableViewLastColumnOnlyAutoresizingStyle;
|
|
209
|
+
self.table.style = NSTableViewStyleSourceList;
|
|
210
|
+
self.table.backgroundColor = NSColor.clearColor;
|
|
211
|
+
[self.table setAccessibilityLabel:@"工作区与页面"];
|
|
212
|
+
NSScrollView *scroll = [[NSScrollView alloc] init];
|
|
213
|
+
scroll.documentView = self.table;
|
|
214
|
+
scroll.hasVerticalScroller = YES;
|
|
215
|
+
scroll.autohidesScrollers = YES;
|
|
216
|
+
scroll.drawsBackground = NO;
|
|
217
|
+
|
|
218
|
+
NSVisualEffectView *sidebar = [[NSVisualEffectView alloc] initWithFrame:NSMakeRect(0, 0, 190, size.height)];
|
|
219
|
+
sidebar.material = NSVisualEffectMaterialSidebar;
|
|
220
|
+
NSTextField *library = Label(@"工作区", 15, NSFontWeightSemibold, NSColor.labelColor);
|
|
221
|
+
NSView *sideHeader = [NSView new];
|
|
222
|
+
[sideHeader addSubview:library];
|
|
223
|
+
[sideHeader addSubview:self.create];
|
|
224
|
+
library.translatesAutoresizingMaskIntoConstraints = NO;
|
|
225
|
+
self.create.translatesAutoresizingMaskIntoConstraints = NO;
|
|
226
|
+
NSStackView *side = Stack(@[sideHeader, scroll], YES, 8);
|
|
227
|
+
[sidebar addSubview:side];
|
|
228
|
+
[NSLayoutConstraint activateConstraints:@[
|
|
229
|
+
[sideHeader.widthAnchor constraintEqualToAnchor:side.widthAnchor],
|
|
230
|
+
[sideHeader.heightAnchor constraintEqualToConstant:27],
|
|
231
|
+
[library.leadingAnchor constraintEqualToAnchor:sideHeader.leadingAnchor constant:4],
|
|
232
|
+
[library.centerYAnchor constraintEqualToAnchor:sideHeader.centerYAnchor],
|
|
233
|
+
[self.create.trailingAnchor constraintEqualToAnchor:sideHeader.trailingAnchor],
|
|
234
|
+
[self.create.centerYAnchor constraintEqualToAnchor:sideHeader.centerYAnchor],
|
|
235
|
+
[side.leadingAnchor constraintEqualToAnchor:sidebar.leadingAnchor constant:8],
|
|
236
|
+
[side.trailingAnchor constraintEqualToAnchor:sidebar.trailingAnchor constant:-8],
|
|
237
|
+
[side.topAnchor constraintEqualToAnchor:sidebar.topAnchor constant:8],
|
|
238
|
+
[side.bottomAnchor constraintEqualToAnchor:sidebar.bottomAnchor constant:-12],
|
|
239
|
+
[scroll.widthAnchor constraintEqualToAnchor:side.widthAnchor]
|
|
240
|
+
]];
|
|
241
|
+
[scroll setContentHuggingPriority:1 forOrientation:NSLayoutConstraintOrientationVertical];
|
|
242
|
+
|
|
243
|
+
NSView *detail = [[NSView alloc] initWithFrame:NSMakeRect(0, 0, size.width - 190, size.height)];
|
|
244
|
+
self.pageView = [[OBPageView alloc] initWithCall:self.call];
|
|
245
|
+
self.pageView.translatesAutoresizingMaskIntoConstraints = NO;
|
|
246
|
+
NSView *top = [NSView new];
|
|
247
|
+
top.translatesAutoresizingMaskIntoConstraints = NO;
|
|
248
|
+
NSStackView *navigation = Stack(@[self.back, self.forward, self.reload, self.home], NO, 2);
|
|
249
|
+
NSStackView *actions = Stack(@[self.control, self.tabButton, self.splitButton, self.more], NO, 5);
|
|
250
|
+
self.address.translatesAutoresizingMaskIntoConstraints = NO;
|
|
251
|
+
[top addSubview:navigation];
|
|
252
|
+
[top addSubview:self.address];
|
|
253
|
+
[top addSubview:actions];
|
|
254
|
+
[detail addSubview:top];
|
|
255
|
+
NSBox *rule = [NSBox new];
|
|
256
|
+
rule.boxType = NSBoxSeparator;
|
|
257
|
+
rule.translatesAutoresizingMaskIntoConstraints = NO;
|
|
258
|
+
[detail addSubview:rule];
|
|
259
|
+
|
|
260
|
+
NSView *viewport = [NSView new];
|
|
261
|
+
viewport.translatesAutoresizingMaskIntoConstraints = NO;
|
|
262
|
+
[detail addSubview:viewport];
|
|
263
|
+
self.secondaryPageView = [[OBPageView alloc] initWithCall:self.call];
|
|
264
|
+
self.secondaryPageView.translatesAutoresizingMaskIntoConstraints = NO;
|
|
265
|
+
self.pageSplit = [NSSplitView new];
|
|
266
|
+
self.pageSplit.translatesAutoresizingMaskIntoConstraints = NO;
|
|
267
|
+
self.pageSplit.vertical = NO;
|
|
268
|
+
self.pageSplit.dividerStyle = NSSplitViewDividerStyleThin;
|
|
269
|
+
[self.pageSplit addArrangedSubview:self.pageView];
|
|
270
|
+
[self.pageSplit addArrangedSubview:self.secondaryPageView];
|
|
271
|
+
self.secondaryPageView.hidden = YES;
|
|
272
|
+
[viewport addSubview:self.pageSplit];
|
|
273
|
+
self.emptyTitle = Label(@"打开工作区", 24, NSFontWeightSemibold, NSColor.labelColor);
|
|
274
|
+
self.emptyAction = [self button:@"新建工作区" action:@selector(emptyAction:)];
|
|
275
|
+
NSImageView *icon = [NSImageView imageViewWithImage:NSApp.applicationIconImage];
|
|
276
|
+
[icon.widthAnchor constraintEqualToConstant:40].active = YES;
|
|
277
|
+
[icon.heightAnchor constraintEqualToConstant:40].active = YES;
|
|
278
|
+
self.emptyView = Stack(@[icon, self.emptyTitle, self.emptyAction], YES, 12);
|
|
279
|
+
self.emptyView.alignment = NSLayoutAttributeCenterX;
|
|
280
|
+
[viewport addSubview:self.emptyView];
|
|
281
|
+
|
|
282
|
+
NSBox *lowerRule = [NSBox new];
|
|
283
|
+
lowerRule.boxType = NSBoxSeparator;
|
|
284
|
+
lowerRule.translatesAutoresizingMaskIntoConstraints = NO;
|
|
285
|
+
[detail addSubview:lowerRule];
|
|
286
|
+
NSStackView *bottom = Stack(@[self.notice, self.status], YES, 3);
|
|
287
|
+
[detail addSubview:bottom];
|
|
288
|
+
|
|
289
|
+
[NSLayoutConstraint activateConstraints:@[
|
|
290
|
+
[top.leadingAnchor constraintEqualToAnchor:detail.leadingAnchor constant:8],
|
|
291
|
+
[top.trailingAnchor constraintEqualToAnchor:detail.trailingAnchor constant:-8],
|
|
292
|
+
[top.topAnchor constraintEqualToAnchor:detail.topAnchor constant:6],
|
|
293
|
+
[top.heightAnchor constraintEqualToConstant:26],
|
|
294
|
+
[navigation.leadingAnchor constraintEqualToAnchor:top.leadingAnchor],
|
|
295
|
+
[navigation.centerYAnchor constraintEqualToAnchor:top.centerYAnchor],
|
|
296
|
+
[navigation.widthAnchor constraintEqualToConstant:108],
|
|
297
|
+
[self.address.leadingAnchor constraintEqualToAnchor:navigation.trailingAnchor constant:8],
|
|
298
|
+
[self.address.trailingAnchor constraintEqualToAnchor:actions.leadingAnchor constant:-8],
|
|
299
|
+
[self.address.centerYAnchor constraintEqualToAnchor:top.centerYAnchor],
|
|
300
|
+
[self.address.heightAnchor constraintEqualToConstant:26],
|
|
301
|
+
[actions.trailingAnchor constraintEqualToAnchor:top.trailingAnchor],
|
|
302
|
+
[actions.centerYAnchor constraintEqualToAnchor:top.centerYAnchor],
|
|
303
|
+
[actions.widthAnchor constraintEqualToConstant:182],
|
|
304
|
+
[rule.topAnchor constraintEqualToAnchor:top.bottomAnchor constant:6],
|
|
305
|
+
[rule.leadingAnchor constraintEqualToAnchor:detail.leadingAnchor],
|
|
306
|
+
[rule.trailingAnchor constraintEqualToAnchor:detail.trailingAnchor],
|
|
307
|
+
[viewport.topAnchor constraintEqualToAnchor:rule.bottomAnchor],
|
|
308
|
+
[viewport.leadingAnchor constraintEqualToAnchor:detail.leadingAnchor],
|
|
309
|
+
[viewport.trailingAnchor constraintEqualToAnchor:detail.trailingAnchor],
|
|
310
|
+
[viewport.bottomAnchor constraintEqualToAnchor:lowerRule.topAnchor],
|
|
311
|
+
[self.pageSplit.topAnchor constraintEqualToAnchor:viewport.topAnchor],
|
|
312
|
+
[self.pageSplit.bottomAnchor constraintEqualToAnchor:viewport.bottomAnchor],
|
|
313
|
+
[self.pageSplit.leadingAnchor constraintEqualToAnchor:viewport.leadingAnchor],
|
|
314
|
+
[self.pageSplit.trailingAnchor constraintEqualToAnchor:viewport.trailingAnchor],
|
|
315
|
+
[self.emptyView.centerXAnchor constraintEqualToAnchor:viewport.centerXAnchor],
|
|
316
|
+
[self.emptyView.centerYAnchor constraintEqualToAnchor:viewport.centerYAnchor],
|
|
317
|
+
[lowerRule.leadingAnchor constraintEqualToAnchor:detail.leadingAnchor],
|
|
318
|
+
[lowerRule.trailingAnchor constraintEqualToAnchor:detail.trailingAnchor],
|
|
319
|
+
[bottom.topAnchor constraintEqualToAnchor:lowerRule.bottomAnchor constant:7],
|
|
320
|
+
[bottom.leadingAnchor constraintEqualToAnchor:detail.leadingAnchor constant:16],
|
|
321
|
+
[bottom.trailingAnchor constraintEqualToAnchor:detail.trailingAnchor constant:-16],
|
|
322
|
+
[bottom.bottomAnchor constraintEqualToAnchor:detail.bottomAnchor constant:-7],
|
|
323
|
+
[self.status.widthAnchor constraintEqualToAnchor:bottom.widthAnchor],
|
|
324
|
+
[self.notice.widthAnchor constraintEqualToAnchor:bottom.widthAnchor]
|
|
325
|
+
]];
|
|
326
|
+
|
|
327
|
+
NSViewController *sideController = [NSViewController new];
|
|
328
|
+
sideController.view = sidebar;
|
|
329
|
+
NSViewController *pageController = [NSViewController new];
|
|
330
|
+
pageController.view = detail;
|
|
331
|
+
NSSplitViewController *split = [NSSplitViewController new];
|
|
332
|
+
split.splitView.vertical = YES;
|
|
333
|
+
split.splitView.dividerStyle = NSSplitViewDividerStyleThin;
|
|
334
|
+
NSSplitViewItem *left = [NSSplitViewItem splitViewItemWithViewController:sideController];
|
|
335
|
+
left.minimumThickness = 148;
|
|
336
|
+
left.maximumThickness = 300;
|
|
337
|
+
left.canCollapse = NO;
|
|
338
|
+
left.holdingPriority = NSLayoutPriorityDefaultHigh;
|
|
339
|
+
[split addSplitViewItem:left];
|
|
340
|
+
NSSplitViewItem *right = [NSSplitViewItem splitViewItemWithViewController:pageController];
|
|
341
|
+
right.minimumThickness = 450;
|
|
342
|
+
[split addSplitViewItem:right];
|
|
343
|
+
self.window.contentViewController = split;
|
|
344
|
+
[self.window setContentSize:size];
|
|
345
|
+
[split.splitView setPosition:190 ofDividerAtIndex:0];
|
|
346
|
+
[self.window center];
|
|
347
|
+
[self.window setFrameAutosaveName:@"open-browser.main-window"];
|
|
348
|
+
[self displayWorkspaces:@[]];
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
- (void)emptyAction:(id)sender { [self.window makeFirstResponder:self.address]; }
|
|
352
|
+
|
|
353
|
+
- (void)show {
|
|
354
|
+
[NSApp activateIgnoringOtherApps:YES];
|
|
355
|
+
[self.window makeKeyAndOrderFront:nil];
|
|
356
|
+
[self render];
|
|
357
|
+
[self refresh];
|
|
358
|
+
if (!self.timer) {
|
|
359
|
+
__weak typeof(self) weakSelf = self;
|
|
360
|
+
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer *timer) {
|
|
361
|
+
typeof(self) owner = weakSelf;
|
|
362
|
+
if (!owner) { [timer invalidate]; return; }
|
|
363
|
+
if (owner.window.visible) [owner refresh];
|
|
364
|
+
}];
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
- (void)invoke:(NSString *)method params:(NSDictionary *)params reply:(WorkspaceReply)reply {
|
|
369
|
+
self.call(method, params, ^(id result, NSString *error) {
|
|
370
|
+
dispatch_async(dispatch_get_main_queue(), ^{ reply(result, error); });
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
- (void)message:(NSString *)message {
|
|
375
|
+
self.notice.stringValue = message ?: @"";
|
|
376
|
+
self.notice.hidden = !message.length;
|
|
377
|
+
self.notice.toolTip = message;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
- (void)refresh {
|
|
381
|
+
if (self.refreshing) return;
|
|
382
|
+
self.refreshing = YES;
|
|
383
|
+
NSUInteger revision = self.revision;
|
|
384
|
+
[self invoke:@"listWorkspaces" params:@{} reply:^(id result, NSString *error) {
|
|
385
|
+
self.refreshing = NO;
|
|
386
|
+
if (revision != self.revision) { [self refresh]; return; }
|
|
387
|
+
BOOL wasConnected = self.connected;
|
|
388
|
+
self.connected = !error && [result isKindOfClass:NSArray.class];
|
|
389
|
+
if (!self.connected) {
|
|
390
|
+
[self message:error ?: @"正在连接本地服务…"];
|
|
391
|
+
[self render];
|
|
392
|
+
return;
|
|
393
|
+
}
|
|
394
|
+
if (!wasConnected) [self message:nil];
|
|
395
|
+
[self displayWorkspaces:result];
|
|
396
|
+
}];
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
- (void)displayWorkspaces:(NSArray *)items {
|
|
400
|
+
self.workspaces = items;
|
|
401
|
+
NSSet *ids = [NSSet setWithArray:[items valueForKey:@"workspaceId"]];
|
|
402
|
+
[self.collapsed intersectSet:ids];
|
|
403
|
+
for (NSString *wid in self.drafts.allKeys) if (![ids containsObject:wid]) [self.drafts removeObjectForKey:wid];
|
|
404
|
+
if (self.pendingSelection) {
|
|
405
|
+
NSDictionary *created = Find(items, @"workspaceId", self.pendingSelection[@"workspaceId"]);
|
|
406
|
+
if (created) {
|
|
407
|
+
self.workspaceID = created[@"workspaceId"];
|
|
408
|
+
self.pageID = self.pendingSelection[@"pageId"];
|
|
409
|
+
[self.collapsed removeObject:self.workspaceID];
|
|
410
|
+
self.pendingSelection = nil;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
if (!Find(items, @"workspaceId", self.workspaceID)) {
|
|
414
|
+
self.workspaceID = items.firstObject[@"workspaceId"];
|
|
415
|
+
self.pageID = nil;
|
|
416
|
+
}
|
|
417
|
+
NSDictionary *selectedWorkspace = [self active];
|
|
418
|
+
if (![self page] && ![self draft]) {
|
|
419
|
+
self.pageID = [selectedWorkspace[@"pages"] lastObject][@"pageId"] ?: [self.drafts[self.workspaceID ?: @""] lastObject][@"pageId"];
|
|
420
|
+
}
|
|
421
|
+
NSArray *titles = @[@"待处理", @"Agent 运行", @"人工接管", @"待命"];
|
|
422
|
+
NSMutableArray *rows = [NSMutableArray array];
|
|
423
|
+
for (NSInteger i = 0; i < (NSInteger)titles.count; i++) {
|
|
424
|
+
NSMutableArray *members = [NSMutableArray array];
|
|
425
|
+
for (NSDictionary *w in items) if (Group(w) == i) [members addObject:w];
|
|
426
|
+
if (!members.count) continue;
|
|
427
|
+
[rows addObject:@{@"group":titles[i], @"count":@(members.count)}];
|
|
428
|
+
for (NSDictionary *member in members) {
|
|
429
|
+
[rows addObject:member];
|
|
430
|
+
if (![self.collapsed containsObject:member[@"workspaceId"]]) {
|
|
431
|
+
[rows addObjectsFromArray:member[@"pages"] ?: @[]];
|
|
432
|
+
[rows addObjectsFromArray:self.drafts[member[@"workspaceId"]] ?: @[]];
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
self.syncingSelection = YES;
|
|
437
|
+
if (![self.rows isEqual:rows]) {
|
|
438
|
+
self.rows = rows;
|
|
439
|
+
[self.table reloadData];
|
|
440
|
+
}
|
|
441
|
+
NSDictionary *selected = [self.collapsed containsObject:self.workspaceID ?: @""] ? selectedWorkspace : ([self page] ?: [self draft] ?: selectedWorkspace);
|
|
442
|
+
if (selected) {
|
|
443
|
+
NSUInteger index = [self.rows indexOfObject:selected];
|
|
444
|
+
if (index != NSNotFound && self.table.selectedRow != (NSInteger)index)
|
|
445
|
+
[self.table selectRowIndexes:[NSIndexSet indexSetWithIndex:index] byExtendingSelection:NO];
|
|
446
|
+
} else {
|
|
447
|
+
[self.table deselectAll:nil];
|
|
448
|
+
}
|
|
449
|
+
self.syncingSelection = NO;
|
|
450
|
+
[self render];
|
|
451
|
+
[self readLocation];
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
- (NSInteger)numberOfRowsInTableView:(NSTableView *)table { return self.rows.count; }
|
|
455
|
+
- (BOOL)tableView:(NSTableView *)table isGroupRow:(NSInteger)row { return self.rows[row][@"group"] != nil; }
|
|
456
|
+
- (BOOL)tableView:(NSTableView *)table shouldSelectRow:(NSInteger)row { return self.rows[row][@"workspaceId"] != nil; }
|
|
457
|
+
- (CGFloat)tableView:(NSTableView *)table heightOfRow:(NSInteger)row { return self.rows[row][@"group"] ? 22 : 25; }
|
|
458
|
+
|
|
459
|
+
- (NSView *)tableView:(NSTableView *)table viewForTableColumn:(NSTableColumn *)column row:(NSInteger)row {
|
|
460
|
+
NSDictionary *item = self.rows[row];
|
|
461
|
+
BOOL group = item[@"group"] != nil;
|
|
462
|
+
NSTableCellView *cell = [[NSTableCellView alloc] init];
|
|
463
|
+
BOOL page = item[@"pageId"] != nil;
|
|
464
|
+
NSString *title = String(item[@"title"]), *origin = String(item[@"origin"]);
|
|
465
|
+
NSString *workspaceName = String(item[@"name"]);
|
|
466
|
+
if (!workspaceName.length) workspaceName = @"未命名任务(请命名)";
|
|
467
|
+
NSString *name = group ? [NSString stringWithFormat:@"%@ %@", item[@"group"], item[@"count"]]
|
|
468
|
+
: page ? (title.length ? title : origin) : workspaceName;
|
|
469
|
+
NSTextField *primary = Label(name, group ? 11 : 13, group || !page ? NSFontWeightSemibold : NSFontWeightRegular, group ? NSColor.secondaryLabelColor : NSColor.labelColor);
|
|
470
|
+
cell.textField = primary;
|
|
471
|
+
BOOL workspace = !group && !page;
|
|
472
|
+
if (workspace) {
|
|
473
|
+
BOOL collapsed = [self.collapsed containsObject:item[@"workspaceId"]];
|
|
474
|
+
NSButton *toggle = [NSButton buttonWithTitle:@"" target:self action:@selector(toggleWorkspace:)];
|
|
475
|
+
toggle.bordered = NO;
|
|
476
|
+
toggle.imagePosition = NSImageOnly;
|
|
477
|
+
toggle.image = [NSImage imageWithSystemSymbolName:collapsed ? @"chevron.right" : @"chevron.down" accessibilityDescription:nil];
|
|
478
|
+
toggle.symbolConfiguration = [NSImageSymbolConfiguration configurationWithPointSize:9 weight:NSFontWeightSemibold];
|
|
479
|
+
toggle.identifier = item[@"workspaceId"];
|
|
480
|
+
toggle.toolTip = collapsed ? @"展开工作区" : @"折叠工作区";
|
|
481
|
+
[toggle setAccessibilityLabel:toggle.toolTip];
|
|
482
|
+
toggle.translatesAutoresizingMaskIntoConstraints = NO;
|
|
483
|
+
[cell addSubview:toggle];
|
|
484
|
+
[NSLayoutConstraint activateConstraints:@[
|
|
485
|
+
[toggle.leadingAnchor constraintEqualToAnchor:cell.leadingAnchor],
|
|
486
|
+
[toggle.centerYAnchor constraintEqualToAnchor:cell.centerYAnchor],
|
|
487
|
+
[toggle.widthAnchor constraintEqualToConstant:15]
|
|
488
|
+
]];
|
|
489
|
+
cell.toolTip = Status(item);
|
|
490
|
+
} else if (page) {
|
|
491
|
+
cell.toolTip = origin;
|
|
492
|
+
}
|
|
493
|
+
primary.translatesAutoresizingMaskIntoConstraints = NO;
|
|
494
|
+
[cell addSubview:primary];
|
|
495
|
+
[NSLayoutConstraint activateConstraints:@[
|
|
496
|
+
[primary.leadingAnchor constraintEqualToAnchor:cell.leadingAnchor constant:group ? 0 : page ? 22 : 16],
|
|
497
|
+
[primary.trailingAnchor constraintEqualToAnchor:cell.trailingAnchor constant:-2],
|
|
498
|
+
[primary.centerYAnchor constraintEqualToAnchor:cell.centerYAnchor]
|
|
499
|
+
]];
|
|
500
|
+
return cell;
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
- (void)toggleWorkspace:(NSButton *)sender {
|
|
504
|
+
NSString *wid = sender.identifier;
|
|
505
|
+
if ([self.collapsed containsObject:wid]) [self.collapsed removeObject:wid];
|
|
506
|
+
else [self.collapsed addObject:wid];
|
|
507
|
+
[self displayWorkspaces:self.workspaces];
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
- (void)tableViewSelectionDidChange:(NSNotification *)note {
|
|
511
|
+
NSInteger index = self.table.selectedRow;
|
|
512
|
+
if (self.syncingSelection || index < 0 || index >= (NSInteger)self.rows.count) return;
|
|
513
|
+
NSDictionary *item = self.rows[index];
|
|
514
|
+
NSString *workspaceID = item[@"workspaceId"];
|
|
515
|
+
if (!workspaceID) return;
|
|
516
|
+
BOOL same = [workspaceID isEqual:self.workspaceID];
|
|
517
|
+
self.workspaceID = workspaceID;
|
|
518
|
+
self.pageID = item[@"pageId"] ?: (same ? self.pageID : nil);
|
|
519
|
+
[self message:nil];
|
|
520
|
+
[self displayWorkspaces:self.workspaces];
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
- (NSDictionary *)active { return Find(self.workspaces, @"workspaceId", self.workspaceID); }
|
|
524
|
+
- (NSDictionary *)page { return Find([self active][@"pages"], @"pageId", self.pageID); }
|
|
525
|
+
- (NSDictionary *)draft { return Find(self.drafts[self.workspaceID ?: @""], @"pageId", self.pageID); }
|
|
526
|
+
- (NSDictionary *)handle { return [self page] ? @{@"workspaceId":self.workspaceID, @"pageId":self.pageID} : nil; }
|
|
527
|
+
- (NSDictionary *)splitCandidate {
|
|
528
|
+
for (NSDictionary *item in [self active][@"pages"])
|
|
529
|
+
if (![item[@"pageId"] isEqual:self.pageID]) return item;
|
|
530
|
+
return nil;
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
- (void)render {
|
|
534
|
+
NSDictionary *w = [self active], *activity = w[@"activity"], *page = [self page];
|
|
535
|
+
BOOL agent = [w[@"mode"] isEqual:@"agent"], human = [w[@"mode"] isEqual:@"human"];
|
|
536
|
+
BOOL ready = self.connected && !self.busy;
|
|
537
|
+
self.window.title = String(page[@"title"]).length ? page[@"title"] : @"open-browser";
|
|
538
|
+
NSString *key = self.pageID ?: self.workspaceID ?: @"";
|
|
539
|
+
if (![key isEqual:self.addressIdentity]) {
|
|
540
|
+
self.addressIdentity = key;
|
|
541
|
+
self.addressEdited = NO;
|
|
542
|
+
self.currentURL = @"";
|
|
543
|
+
self.address.stringValue = @"";
|
|
544
|
+
self.canGoBack = self.canGoForward = NO;
|
|
545
|
+
}
|
|
546
|
+
self.address.enabled = self.connected;
|
|
547
|
+
self.back.enabled = ready && self.canGoBack;
|
|
548
|
+
self.forward.enabled = ready && self.canGoForward;
|
|
549
|
+
self.reload.enabled = ready && page;
|
|
550
|
+
self.home.enabled = ready;
|
|
551
|
+
self.tabButton.enabled = ready;
|
|
552
|
+
self.splitButton.enabled = ready && page && ([self splitCandidate] || self.splitPage);
|
|
553
|
+
self.splitButton.toolTip = self.splitPage ? @"关闭拆分" : @"拆分当前工作区页面";
|
|
554
|
+
self.more.enabled = ready;
|
|
555
|
+
self.create.enabled = ready;
|
|
556
|
+
self.control.enabled = ready && (human || (agent && page));
|
|
557
|
+
self.control.title = human ? @"交回 Agent" : @"接管";
|
|
558
|
+
self.control.toolTip = human ? @"交回此工作区的控制权" : @"人工接管当前页面";
|
|
559
|
+
self.control.hidden = !w || (!page && !human);
|
|
560
|
+
self.status.stringValue = w ? Status(w) : self.connected ? @"" : @"正在连接本地服务…";
|
|
561
|
+
if ([activity[@"queued"] integerValue]) self.status.stringValue = [self.status.stringValue stringByAppendingFormat:@" · 排队 %@", activity[@"queued"]];
|
|
562
|
+
self.status.toolTip = Operation(Map(activity[@"recent"]));
|
|
563
|
+
self.credential.title = [activity[@"needsCredential"] boolValue] ? @"补充凭据…" : @"绑定凭据…";
|
|
564
|
+
self.credential.enabled = agent && page;
|
|
565
|
+
self.preview.enabled = agent && page;
|
|
566
|
+
self.feedback.enabled = self.connected;
|
|
567
|
+
self.closePageButton.enabled = page || [self draft];
|
|
568
|
+
self.close.enabled = agent;
|
|
569
|
+
|
|
570
|
+
NSDictionary *handle = self.connected && page ? [self handle] : nil;
|
|
571
|
+
self.emptyView.hidden = handle != nil;
|
|
572
|
+
self.pageView.hidden = !handle;
|
|
573
|
+
NSDictionary *second = self.splitPage;
|
|
574
|
+
if (second && (![Find([self active][@"pages"], @"pageId", second[@"pageId"]) isKindOfClass:NSDictionary.class] ||
|
|
575
|
+
[second[@"pageId"] isEqual:self.pageID])) second = nil;
|
|
576
|
+
if (!second) self.splitPage = nil;
|
|
577
|
+
NSDictionary *secondHandle = second ? @{@"workspaceId":self.workspaceID, @"pageId":second[@"pageId"]} : nil;
|
|
578
|
+
self.secondaryPageView.hidden = secondHandle == nil || handle == nil;
|
|
579
|
+
self.pageSplit.hidden = handle == nil;
|
|
580
|
+
self.emptyTitle.stringValue = !self.connected ? @"正在连接本地服务…" : @"新标签页";
|
|
581
|
+
self.emptyAction.title = @"输入网址";
|
|
582
|
+
self.emptyAction.enabled = ready;
|
|
583
|
+
if (!self.window.visible || self.window.miniaturized) handle = nil;
|
|
584
|
+
NSArray *identity = handle ? @[handle, String(w[@"mode"]), String(w[@"controlGeneration"])] : @[];
|
|
585
|
+
if (![self.displayedPage isEqual:identity]) {
|
|
586
|
+
self.displayedPage = identity;
|
|
587
|
+
[self.pageView displayPage:handle mode:String(w[@"mode"]) generation:String(w[@"controlGeneration"])];
|
|
588
|
+
}
|
|
589
|
+
[self.secondaryPageView displayPage:secondHandle mode:String(w[@"mode"]) generation:String(w[@"controlGeneration"])];
|
|
590
|
+
if ((!agent || !self.connected) && self.previewSheet) [self.window endSheet:self.previewSheet.window];
|
|
591
|
+
if ((!agent || !self.connected) && self.credentialSheet) [self.window endSheet:self.credentialSheet.window returnCode:NSModalResponseCancel];
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
- (void)windowWillClose:(NSNotification *)notification {
|
|
595
|
+
self.displayedPage = @[];
|
|
596
|
+
[self.pageView displayPage:nil mode:@"" generation:@""];
|
|
597
|
+
[self.secondaryPageView displayPage:nil mode:@"" generation:@""];
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
- (void)windowDidMiniaturize:(NSNotification *)notification { [self render]; }
|
|
601
|
+
- (void)windowDidDeminiaturize:(NSNotification *)notification { [self render]; [self refresh]; }
|
|
602
|
+
|
|
603
|
+
- (void)perform:(NSString *)method params:(NSDictionary *)params success:(void (^)(id))success {
|
|
604
|
+
if (!params || self.busy) return;
|
|
605
|
+
self.busy = YES;
|
|
606
|
+
[self message:nil];
|
|
607
|
+
[self render];
|
|
608
|
+
[self invoke:method params:params reply:^(id result, NSString *error) {
|
|
609
|
+
self.busy = NO;
|
|
610
|
+
if (error.length) {
|
|
611
|
+
NSDictionary *messages = @{@"invalid_url":@"请输入完整的 http 或 https 网址。",
|
|
612
|
+
@"origin_denied":@"该站点不在此工作区的授权范围内。",
|
|
613
|
+
@"navigation_failed":@"页面打开失败,请检查网址或网络。",
|
|
614
|
+
@"workspace_under_user_control":@"工作区正由用户控制。",
|
|
615
|
+
@"stale_control":@"控制权已变化,请重新操作。",
|
|
616
|
+
@"stale_credential_request":@"凭据请求已变化,请重新查看。"};
|
|
617
|
+
[self message:messages[error] ?: error];
|
|
618
|
+
}
|
|
619
|
+
else {
|
|
620
|
+
self.revision++;
|
|
621
|
+
if (success) success(result);
|
|
622
|
+
}
|
|
623
|
+
[self render];
|
|
624
|
+
[self refresh];
|
|
625
|
+
}];
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
- (NSAlert *)sheet:(NSString *)title message:(NSString *)message action:(NSString *)action {
|
|
629
|
+
NSAlert *sheet = [[NSAlert alloc] init];
|
|
630
|
+
sheet.messageText = title;
|
|
631
|
+
sheet.informativeText = message ?: @"";
|
|
632
|
+
[sheet addButtonWithTitle:action];
|
|
633
|
+
[sheet addButtonWithTitle:@"取消"];
|
|
634
|
+
return sheet;
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
- (NSTextField *)field:(NSString *)placeholder secure:(BOOL)secure {
|
|
638
|
+
NSTextField *field = secure ? [[NSSecureTextField alloc] initWithFrame:NSZeroRect] : [[NSTextField alloc] initWithFrame:NSZeroRect];
|
|
639
|
+
field.placeholderString = placeholder;
|
|
640
|
+
[field setAccessibilityLabel:placeholder];
|
|
641
|
+
[field.widthAnchor constraintEqualToConstant:285].active = YES;
|
|
642
|
+
return field;
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
- (void)attach:(NSArray<NSView *> *)views sheet:(NSAlert *)sheet {
|
|
646
|
+
NSStackView *stack = Stack(views, YES, 7);
|
|
647
|
+
stack.frame = NSMakeRect(0, 0, 285, stack.fittingSize.height);
|
|
648
|
+
sheet.accessoryView = stack;
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
- (void)createWorkspace:(id)sender {
|
|
652
|
+
NSAlert *sheet = [self sheet:@"新建工作区" message:@"" action:@"创建"];
|
|
653
|
+
NSTextField *name = [self field:@"工作区名称" secure:NO];
|
|
654
|
+
NSTextField *url = [self field:@"首个网址(可选)" secure:NO];
|
|
655
|
+
[self attach:@[name, url] sheet:sheet];
|
|
656
|
+
name.nextKeyView = url;
|
|
657
|
+
sheet.window.initialFirstResponder = name;
|
|
658
|
+
[sheet beginSheetModalForWindow:self.window completionHandler:^(NSModalResponse response) {
|
|
659
|
+
if (response != NSAlertFirstButtonReturn) return;
|
|
660
|
+
NSString *title = [name.stringValue stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
|
|
661
|
+
NSString *address = [url.stringValue stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
|
|
662
|
+
if (!title.length) { [self message:@"请输入工作区名称。"]; return; }
|
|
663
|
+
[self perform:@"createWorkspace" params:@{@"name":title} success:^(id result) {
|
|
664
|
+
NSString *createdID = result[@"workspaceId"];
|
|
665
|
+
self.pendingSelection = @{@"workspaceId":createdID};
|
|
666
|
+
if (address.length) [self openAddress:address workspace:createdID];
|
|
667
|
+
}];
|
|
668
|
+
}];
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
- (void)openAddress:(NSString *)address workspace:(NSString *)workspace {
|
|
672
|
+
NSDictionary *draft = [self draft];
|
|
673
|
+
[self perform:@"pageNew" params:@{@"workspaceId":workspace, @"url":address} success:^(id result) {
|
|
674
|
+
if (draft) [self.drafts[workspace] removeObject:draft];
|
|
675
|
+
self.pendingSelection = result;
|
|
676
|
+
}];
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
- (void)focusAddress:(id)sender {
|
|
680
|
+
[self.window makeFirstResponder:self.address];
|
|
681
|
+
[self.address selectText:nil];
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
- (void)newTab:(id)sender {
|
|
685
|
+
if (!self.connected || self.busy) return;
|
|
686
|
+
if (self.workspaceID) {
|
|
687
|
+
if (!self.drafts[self.workspaceID]) self.drafts[self.workspaceID] = [NSMutableArray array];
|
|
688
|
+
self.pageID = NSUUID.UUID.UUIDString;
|
|
689
|
+
[self.drafts[self.workspaceID] addObject:@{@"workspaceId":self.workspaceID, @"pageId":self.pageID, @"title":@"新标签页"}];
|
|
690
|
+
[self.collapsed removeObject:self.workspaceID];
|
|
691
|
+
[self displayWorkspaces:self.workspaces];
|
|
692
|
+
}
|
|
693
|
+
[self focusAddress:nil];
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
- (void)home:(id)sender {
|
|
697
|
+
self.splitPage = nil;
|
|
698
|
+
[self newTab:sender];
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
- (void)toggleSplit:(id)sender {
|
|
702
|
+
if (self.splitPage) {
|
|
703
|
+
self.splitPage = nil;
|
|
704
|
+
[self render];
|
|
705
|
+
return;
|
|
706
|
+
}
|
|
707
|
+
NSDictionary *candidate = [self splitCandidate];
|
|
708
|
+
if (!candidate) {
|
|
709
|
+
[self message:@"当前工作区至少需要两个页面才能拆分。"];
|
|
710
|
+
return;
|
|
711
|
+
}
|
|
712
|
+
self.splitPage = candidate;
|
|
713
|
+
[self render];
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
- (void)controlTextDidChange:(NSNotification *)note {
|
|
717
|
+
if (note.object == self.address) self.addressEdited = YES;
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
- (BOOL)control:(NSControl *)control textView:(NSTextView *)view doCommandBySelector:(SEL)selector {
|
|
721
|
+
if (control == self.address && selector == @selector(cancelOperation:)) {
|
|
722
|
+
self.addressEdited = NO;
|
|
723
|
+
self.address.stringValue = self.currentURL ?: @"";
|
|
724
|
+
[self.window makeFirstResponder:self.table];
|
|
725
|
+
return YES;
|
|
726
|
+
}
|
|
727
|
+
return NO;
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
- (void)applyLocation:(NSDictionary *)location {
|
|
731
|
+
self.currentURL = String(location[@"url"]);
|
|
732
|
+
self.canGoBack = [location[@"canGoBack"] boolValue];
|
|
733
|
+
self.canGoForward = [location[@"canGoForward"] boolValue];
|
|
734
|
+
if (!self.addressEdited && ![self.address.stringValue isEqual:self.currentURL]) self.address.stringValue = self.currentURL;
|
|
735
|
+
[self render];
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
- (void)readLocation {
|
|
739
|
+
NSDictionary *handle = [self handle];
|
|
740
|
+
if (!self.connected || !handle || [handle isEqual:self.locationPending]) return;
|
|
741
|
+
self.locationPending = handle;
|
|
742
|
+
NSUInteger revision = self.revision;
|
|
743
|
+
[self invoke:@"pageLocation" params:handle reply:^(id result, NSString *error) {
|
|
744
|
+
if (self.locationPending == handle) self.locationPending = nil;
|
|
745
|
+
if (!error && revision == self.revision && [handle isEqual:[self handle]]) [self applyLocation:result];
|
|
746
|
+
}];
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
- (void)navigate:(NSString *)action url:(NSString *)url {
|
|
750
|
+
NSDictionary *handle = [self handle];
|
|
751
|
+
if (!handle) return;
|
|
752
|
+
NSMutableDictionary *params = [handle mutableCopy];
|
|
753
|
+
params[@"action"] = action;
|
|
754
|
+
if (url) params[@"url"] = url;
|
|
755
|
+
[self perform:@"pageNavigate" params:params success:^(id result) {
|
|
756
|
+
if ([handle isEqual:[self handle]]) {
|
|
757
|
+
self.addressEdited = NO;
|
|
758
|
+
[self applyLocation:result];
|
|
759
|
+
}
|
|
760
|
+
}];
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
- (void)goBack:(id)sender { if (self.back.enabled) [self navigate:@"back" url:nil]; }
|
|
764
|
+
- (void)goForward:(id)sender { if (self.forward.enabled) [self navigate:@"forward" url:nil]; }
|
|
765
|
+
- (void)reloadPage:(id)sender { if (self.reload.enabled) [self navigate:@"reload" url:nil]; }
|
|
766
|
+
|
|
767
|
+
- (void)submitAddress:(id)sender {
|
|
768
|
+
if (!self.connected || self.busy) return;
|
|
769
|
+
NSString *address = [self.address.stringValue stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
|
|
770
|
+
if (!address.length) return;
|
|
771
|
+
if ([address rangeOfString:@"://"].location == NSNotFound) {
|
|
772
|
+
NSRange local = [address rangeOfString:@"^(localhost|127\\.0\\.0\\.1|\\[::1\\])([:/?#]|$)" options:NSRegularExpressionSearch|NSCaseInsensitiveSearch];
|
|
773
|
+
address = [(local.location == 0 ? @"http://" : @"https://") stringByAppendingString:address];
|
|
774
|
+
}
|
|
775
|
+
NSURLComponents *components = [NSURLComponents componentsWithString:address];
|
|
776
|
+
if (![@[@"http", @"https"] containsObject:components.scheme.lowercaseString] || !components.host.length || components.user || components.password) {
|
|
777
|
+
[self message:@"请输入有效的 HTTP 或 HTTPS 网址。"]; return;
|
|
778
|
+
}
|
|
779
|
+
if ([self handle]) { [self navigate:@"go" url:address]; return; }
|
|
780
|
+
if (self.workspaceID) { [self openAddress:address workspace:self.workspaceID]; return; }
|
|
781
|
+
[self perform:@"createWorkspace" params:@{@"name":components.host} success:^(id result) {
|
|
782
|
+
NSString *wid = result[@"workspaceId"];
|
|
783
|
+
self.pendingSelection = @{@"workspaceId":wid};
|
|
784
|
+
[self openAddress:address workspace:wid];
|
|
785
|
+
}];
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
- (void)changeControl:(id)sender {
|
|
789
|
+
if ([[self active][@"mode"] isEqual:@"human"]) {
|
|
790
|
+
[self perform:@"returnToAgent" params:@{@"workspaceId":self.workspaceID} success:nil];
|
|
791
|
+
} else {
|
|
792
|
+
[self perform:@"takeControl" params:[self handle] success:nil];
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
- (void)closePage:(id)sender {
|
|
797
|
+
if ([self draft]) {
|
|
798
|
+
[self.drafts[self.workspaceID] removeObject:[self draft]];
|
|
799
|
+
self.pageID = nil;
|
|
800
|
+
[self displayWorkspaces:self.workspaces];
|
|
801
|
+
} else [self perform:@"closePage" params:[self handle] success:nil];
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
- (void)feedback:(id)sender {
|
|
805
|
+
NSDictionary *workspace = [self active];
|
|
806
|
+
NSDictionary *activity = workspace[@"activity"];
|
|
807
|
+
NSString *report = [NSString stringWithFormat:@"open-browser\n工作区状态:%@\nAgent:%@\n页面标题:%@\n\n请补充复现步骤。此文本不包含网址、页面正文、凭据或 Cookie。",
|
|
808
|
+
Status(workspace), String(workspace[@"agentName"]), String([self page][@"title"])];
|
|
809
|
+
NSAlert *alert = [NSAlert new];
|
|
810
|
+
alert.messageText = @"反馈问题";
|
|
811
|
+
alert.informativeText = [NSString stringWithFormat:@"%@\n当前操作:%@", report, Operation(Map(activity[@"recent"]))];
|
|
812
|
+
[alert addButtonWithTitle:@"复制脱敏反馈"];
|
|
813
|
+
[alert addButtonWithTitle:@"关闭"];
|
|
814
|
+
if ([alert runModal] == NSAlertFirstButtonReturn) {
|
|
815
|
+
NSPasteboard *pasteboard = NSPasteboard.generalPasteboard;
|
|
816
|
+
[pasteboard clearContents];
|
|
817
|
+
[pasteboard setString:report forType:NSPasteboardTypeString];
|
|
818
|
+
[self message:@"已复制脱敏反馈,可粘贴到项目 issue 或私聊中。"];
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
|
|
822
|
+
- (void)closeWorkspace:(id)sender {
|
|
823
|
+
NSString *workspace = self.workspaceID;
|
|
824
|
+
if (!workspace) return;
|
|
825
|
+
NSAlert *sheet = [self sheet:@"关闭工作区?" message:[self active][@"name"] action:@"关闭工作区"];
|
|
826
|
+
sheet.buttons.firstObject.hasDestructiveAction = YES;
|
|
827
|
+
[sheet beginSheetModalForWindow:self.window completionHandler:^(NSModalResponse response) {
|
|
828
|
+
if (response == NSAlertFirstButtonReturn) [self perform:@"closeWorkspace" params:@{@"workspaceId":workspace} success:nil];
|
|
829
|
+
}];
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
- (void)previewPage:(id)sender {
|
|
833
|
+
NSDictionary *page = [self handle];
|
|
834
|
+
[self perform:@"previewPage" params:page success:^(id result) {
|
|
835
|
+
if (![page isEqual:[self handle]] || ![[self active][@"mode"] isEqual:@"agent"]) return;
|
|
836
|
+
NSAlert *sheet = [[NSAlert alloc] init];
|
|
837
|
+
sheet.messageText = @"页面文本";
|
|
838
|
+
sheet.informativeText = String([self page][@"origin"]);
|
|
839
|
+
[sheet addButtonWithTitle:@"完成"];
|
|
840
|
+
NSScrollView *scroll = [[NSScrollView alloc] initWithFrame:NSMakeRect(0, 0, 320, 200)];
|
|
841
|
+
scroll.hasVerticalScroller = YES;
|
|
842
|
+
scroll.borderType = NSBezelBorder;
|
|
843
|
+
NSTextView *text = [[NSTextView alloc] initWithFrame:scroll.contentView.bounds];
|
|
844
|
+
text.editable = NO;
|
|
845
|
+
text.richText = NO;
|
|
846
|
+
text.autoresizingMask = NSViewWidthSizable;
|
|
847
|
+
text.textContainerInset = NSMakeSize(6, 6);
|
|
848
|
+
text.string = String(result[@"text"]).length ? result[@"text"] : @"暂无可预览的文字。";
|
|
849
|
+
scroll.documentView = text;
|
|
850
|
+
sheet.accessoryView = scroll;
|
|
851
|
+
self.previewSheet = sheet;
|
|
852
|
+
[sheet beginSheetModalForWindow:self.window completionHandler:^(NSModalResponse __unused response) {
|
|
853
|
+
text.string = @"";
|
|
854
|
+
self.previewSheet = nil;
|
|
855
|
+
}];
|
|
856
|
+
}];
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
- (void)credentials:(id)sender {
|
|
860
|
+
NSDictionary *page = [self handle];
|
|
861
|
+
if (!page) return;
|
|
862
|
+
[self perform:@"getWorkspace" params:@{@"workspaceId":page[@"workspaceId"]} success:^(id result) {
|
|
863
|
+
if (![page isEqual:[self handle]] || ![result[@"mode"] isEqual:@"agent"]) return;
|
|
864
|
+
NSDictionary *request = Map(result[@"activity"][@"credentialRequest"]);
|
|
865
|
+
NSString *target = request ? request[@"pageId"] : page[@"pageId"];
|
|
866
|
+
NSDictionary *targetPage = Find(result[@"pages"], @"pageId", target);
|
|
867
|
+
NSString *origin = request ? String(request[@"origin"]) : String(targetPage[@"origin"]);
|
|
868
|
+
if (!targetPage || ![targetPage[@"origin"] isEqual:origin]) { [self message:@"页面已变化,请重新打开凭据面板。"]; return; }
|
|
869
|
+
[self editCredential:request page:targetPage generation:result[@"controlGeneration"] origin:origin];
|
|
870
|
+
}];
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
- (void)editCredential:(NSDictionary *)request page:(NSDictionary *)page generation:(NSString *)generation origin:(NSString *)origin {
|
|
874
|
+
NSDictionary *handle = @{@"workspaceId":page[@"workspaceId"], @"pageId":page[@"pageId"]};
|
|
875
|
+
NSAlert *sheet = [self sheet:request ? @"补充凭据" : @"绑定凭据" message:origin action:@"绑定"];
|
|
876
|
+
NSTextField *ref = [self field:@"凭据名称" secure:NO];
|
|
877
|
+
NSSecureTextField *secret = (NSSecureTextField *)[self field:@"凭据值" secure:YES];
|
|
878
|
+
NSMutableArray *fields = [NSMutableArray array];
|
|
879
|
+
if (request) {
|
|
880
|
+
ref.stringValue = String(request[@"credentialRef"]);
|
|
881
|
+
[fields addObject:Label(ref.stringValue, 13, NSFontWeightMedium, NSColor.labelColor)];
|
|
882
|
+
} else {
|
|
883
|
+
[fields addObject:ref];
|
|
884
|
+
ref.nextKeyView = secret;
|
|
885
|
+
}
|
|
886
|
+
[fields addObject:secret];
|
|
887
|
+
[self attach:fields sheet:sheet];
|
|
888
|
+
sheet.window.initialFirstResponder = request ? secret : ref;
|
|
889
|
+
self.credentialSheet = sheet;
|
|
890
|
+
self.secretField = secret;
|
|
891
|
+
[sheet beginSheetModalForWindow:self.window completionHandler:^(NSModalResponse response) {
|
|
892
|
+
self.credentialSheet = nil;
|
|
893
|
+
self.secretField = nil;
|
|
894
|
+
NSString *value = [secret.stringValue copy];
|
|
895
|
+
secret.stringValue = @"";
|
|
896
|
+
if (response != NSAlertFirstButtonReturn) return;
|
|
897
|
+
NSString *key = [ref.stringValue stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
|
|
898
|
+
if (!value.length || !key.length) { [self message:@"请填写凭据名称和值。"]; return; }
|
|
899
|
+
[self perform:@"getWorkspace" params:@{@"workspaceId":handle[@"workspaceId"]} success:^(id fresh) {
|
|
900
|
+
NSDictionary *currentRequest = Map(fresh[@"activity"][@"credentialRequest"]);
|
|
901
|
+
NSDictionary *currentPage = Find(fresh[@"pages"], @"pageId", handle[@"pageId"]);
|
|
902
|
+
if (![fresh[@"mode"] isEqual:@"agent"] || ![generation isEqual:fresh[@"controlGeneration"]] ||
|
|
903
|
+
![currentPage[@"origin"] isEqual:origin] || (request && ![request isEqual:currentRequest])) {
|
|
904
|
+
[self message:@"页面、控制权或凭据请求已变化,请重新查看。"];
|
|
905
|
+
return;
|
|
906
|
+
}
|
|
907
|
+
NSMutableDictionary *binding = [NSMutableDictionary dictionaryWithDictionary:request ?: @{}];
|
|
908
|
+
[binding addEntriesFromDictionary:@{@"workspaceId":handle[@"workspaceId"], @"origin":origin, @"credentialRef":key, @"value":value}];
|
|
909
|
+
[self perform:(request ? @"resolveCredential" : @"setCredential") params:binding success:^(id __unused bound) {
|
|
910
|
+
[self message:request ? @"已绑定凭据,调用方可以重试。" : @"已绑定凭据。"];
|
|
911
|
+
}];
|
|
912
|
+
}];
|
|
913
|
+
}];
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
@end
|