Skip to content

Commit 93d05ed

Browse files
committed
Remove PBRefMenuItem subclass
1 parent ad8e5ba commit 93d05ed

File tree

3 files changed

+58
-68
lines changed

3 files changed

+58
-68
lines changed

Classes/Controllers/PBRefController.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ - (void)awakeFromNib
3030

3131
- (NSArray<NSMenuItem *> *) menuItemsForRef:(PBGitRef *)ref
3232
{
33-
return [PBRefMenuItem defaultMenuItemsForRef:ref inRepository:historyController.repository target:nil];
33+
return [NSMenuItem pb_defaultMenuItemsForRef:ref inRepository:historyController.repository];
3434
}
3535

3636
- (NSArray<NSMenuItem *> *) menuItemsForCommits:(NSArray<PBGitCommit *> *)commits
3737
{
38-
return [PBRefMenuItem defaultMenuItemsForCommits:commits target:nil];
38+
return [NSMenuItem pb_defaultMenuItemsForCommits:commits];
3939
}
4040

4141
- (NSArray<NSMenuItem *> *)menuItemsForRow:(NSInteger)rowIndex

Classes/Views/PBRefMenuItem.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,9 @@
1010
#import "PBGitRef.h"
1111
#import "PBGitCommit.h"
1212

13-
@interface PBRefMenuItem : NSMenuItem {
14-
}
13+
@interface NSMenuItem (PBRefMenuItem)
1514

16-
17-
+ (PBRefMenuItem *) separatorItem;
18-
+ (NSArray *) defaultMenuItemsForRef:(PBGitRef *)refs inRepository:(PBGitRepository *)repo target:(id)target;
19-
+ (NSArray *) defaultMenuItemsForCommits:(NSArray<PBGitCommit *> *)commits target:(id)target;
15+
+ (NSArray *)pb_defaultMenuItemsForRef:(PBGitRef *)refs inRepository:(PBGitRepository *)repo;
16+
+ (NSArray *)pb_defaultMenuItemsForCommits:(NSArray<PBGitCommit *> *)commits;
2017

2118
@end

Classes/Views/PBRefMenuItem.m

Lines changed: 53 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -10,52 +10,48 @@
1010
#import "PBGitRepository.h"
1111
#import "PBGitRevSpecifier.h"
1212

13-
@implementation PBRefMenuItem
13+
/*
14+
* TODO: This is kept for simplicity reasons. A "more correct" way of handling
15+
* menus would be to have them in NIBs, and handle them using NSMenuValidation.
16+
*/
1417

15-
+ (PBRefMenuItem *) itemWithTitle:(NSString *)title action:(SEL)selector enabled:(BOOL)isEnabled
18+
@implementation NSMenuItem (PBRefMenuItem)
19+
20+
+ (NSMenuItem *)pb_itemWithTitle:(NSString *)title action:(SEL)selector enabled:(BOOL)isEnabled
1621
{
1722
if (!isEnabled)
1823
selector = nil;
1924

20-
PBRefMenuItem *item = [[PBRefMenuItem alloc] initWithTitle:title action:selector keyEquivalent:@""];
25+
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:title action:selector keyEquivalent:@""];
2126
[item setEnabled:isEnabled];
2227
return item;
2328
}
2429

25-
26-
+ (PBRefMenuItem *) separatorItem
27-
{
28-
PBRefMenuItem *item = (PBRefMenuItem *)[super separatorItem];
29-
return item;
30-
}
31-
32-
33-
+ (NSArray<NSMenuItem *> *) defaultMenuItemsForStashRef:(PBGitRef *)ref inRepository:(PBGitRepository *)repo target:(id)target
30+
+ (NSArray<NSMenuItem *> *)pb_defaultMenuItemsForStashRef:(PBGitRef *)ref
3431
{
3532
NSMutableArray *items = [NSMutableArray array];
3633
NSString *targetRefName = [ref shortName];
3734
BOOL isCleanWorkingCopy = YES;
38-
35+
3936
// pop
4037
NSString *stashPopTitle = [NSString stringWithFormat:NSLocalizedString(@"Pop %@", @"Contextual Menu Item to pop the selected stash ref"), targetRefName];
41-
[items addObject:[PBRefMenuItem itemWithTitle:stashPopTitle action:@selector(stashPop:) enabled:isCleanWorkingCopy]];
38+
[items addObject:[NSMenuItem pb_itemWithTitle:stashPopTitle action:@selector(stashPop:) enabled:isCleanWorkingCopy]];
4239

4340
// apply
4441
NSString *stashApplyTitle = [NSString stringWithFormat:NSLocalizedString(@"Apply %@", @"Contextual Menu Item to apply the selected stash ref"), targetRefName];
45-
[items addObject:[PBRefMenuItem itemWithTitle:stashApplyTitle action:@selector(stashApply:) enabled:YES]];
42+
[items addObject:[NSMenuItem pb_itemWithTitle:stashApplyTitle action:@selector(stashApply:) enabled:YES]];
4643

4744
// view diff
4845
NSString *stashDiffTitle = @"View Diff";
49-
[items addObject:[PBRefMenuItem itemWithTitle:stashDiffTitle action:@selector(stashViewDiff:) enabled:YES]];
46+
[items addObject:[NSMenuItem pb_itemWithTitle:stashDiffTitle action:@selector(stashViewDiff:) enabled:YES]];
5047

51-
[items addObject:[PBRefMenuItem separatorItem]];
48+
[items addObject:[NSMenuItem separatorItem]];
5249

5350
// drop
5451
NSString *stashDropTitle = [NSString stringWithFormat:NSLocalizedString(@"Drop %@", @"Contextual Menu Item to drop the selected stash ref"), targetRefName];
55-
[items addObject:[PBRefMenuItem itemWithTitle:stashDropTitle action:@selector(stashDrop:) enabled:YES]];
52+
[items addObject:[NSMenuItem pb_itemWithTitle:stashDropTitle action:@selector(stashDrop:) enabled:YES]];
5653

57-
for (PBRefMenuItem *item in items) {
58-
item.target = target;
54+
for (NSMenuItem *item in items) {
5955
if (!item.representedObject) {
6056
item.representedObject = ref;
6157
}
@@ -65,14 +61,14 @@ + (PBRefMenuItem *) separatorItem
6561
}
6662

6763

68-
+ (NSArray<NSMenuItem *> *) defaultMenuItemsForRef:(PBGitRef *)ref inRepository:(PBGitRepository *)repo target:(id)target
64+
+ (NSArray<NSMenuItem *> *)pb_defaultMenuItemsForRef:(PBGitRef *)ref inRepository:(PBGitRepository *)repo
6965
{
7066
if (!ref || !repo) {
7167
return nil;
7268
}
7369

7470
if (ref.isStash) {
75-
return [self defaultMenuItemsForStashRef:ref inRepository:repo target:target];
71+
return [self pb_defaultMenuItemsForStashRef:ref];
7672
}
7773

7874
NSString *refName = ref.shortName;
@@ -95,82 +91,81 @@ + (PBRefMenuItem *) separatorItem
9591
if (!isRemote) {
9692
// checkout ref
9793
NSString *checkoutTitle = [NSString stringWithFormat:NSLocalizedString(@"Checkout “%@", @"Contextual Menu Item to check out the selected ref"), refName];
98-
[items addObject:[PBRefMenuItem itemWithTitle:checkoutTitle action:@selector(checkout:) enabled:!isHead]];
99-
[items addObject:[PBRefMenuItem separatorItem]];
94+
[items addObject:[NSMenuItem pb_itemWithTitle:checkoutTitle action:@selector(checkout:) enabled:!isHead]];
95+
[items addObject:[NSMenuItem separatorItem]];
10096

10197
// create branch
10298
NSString *createBranchTitle = ref.isRemoteBranch
10399
? [NSString stringWithFormat:NSLocalizedString(@"Create Branch tracking “%@”…", @"Contextual Menu Item to create a branch tracking the selected remote branch"), refName]
104100
: NSLocalizedString(@"Create Branch…", @"Contextual Menu Item to create a new branch at the selected ref");
105-
[items addObject:[PBRefMenuItem itemWithTitle:createBranchTitle action:@selector(createBranch:) enabled:YES]];
101+
[items addObject:[NSMenuItem pb_itemWithTitle:createBranchTitle action:@selector(createBranch:) enabled:YES]];
106102

107103
// create tag
108-
[items addObject:[PBRefMenuItem itemWithTitle:NSLocalizedString(@"Create Tag…", @"Contextual Menu Item to create a tag at the selected ref") action:@selector(createTag:) enabled:YES]];
104+
[items addObject:[NSMenuItem pb_itemWithTitle:NSLocalizedString(@"Create Tag…", @"Contextual Menu Item to create a tag at the selected ref") action:@selector(createTag:) enabled:YES]];
109105

110106
// view tag info
111107
if (ref.isTag) {
112-
[items addObject:[PBRefMenuItem itemWithTitle:NSLocalizedString(@"View Tag Info…", @"Contextual Menu Item to view Information about the selected tag") action:@selector(showTagInfoSheet:) enabled:YES]];
108+
[items addObject:[NSMenuItem pb_itemWithTitle:NSLocalizedString(@"View Tag Info…", @"Contextual Menu Item to view Information about the selected tag") action:@selector(showTagInfoSheet:) enabled:YES]];
113109
}
114110

115111
// Diff
116112
NSString *diffTitle = [NSString stringWithFormat:NSLocalizedString(@"Diff with “%@", @"Contextual Menu Item to view a diff between the selected ref and HEAD"), headRefName];
117-
[items addObject:[PBRefMenuItem itemWithTitle:diffTitle action:@selector(diffWithHEAD:) enabled:!isHead]];
118-
[items addObject:[PBRefMenuItem separatorItem]];
113+
[items addObject:[NSMenuItem pb_itemWithTitle:diffTitle action:@selector(diffWithHEAD:) enabled:!isHead]];
114+
[items addObject:[NSMenuItem separatorItem]];
119115

120116
// merge ref
121117
NSString *mergeTitle = isOnHeadBranch
122118
? NSLocalizedString(@"Merge", @"Inactive Contextual Menu Item for merging")
123119
: [NSString stringWithFormat:@"Merge %@ into %@", refName, headRefName];
124-
[items addObject:[PBRefMenuItem itemWithTitle:mergeTitle action:@selector(merge:) enabled:!isOnHeadBranch]];
120+
[items addObject:[NSMenuItem pb_itemWithTitle:mergeTitle action:@selector(merge:) enabled:!isOnHeadBranch]];
125121

126122
// rebase
127123
NSString *rebaseTitle = isOnHeadBranch
128124
? NSLocalizedString(@"Rebase", @"Inactive Contextual Menu Item for rebasing")
129125
: [NSString stringWithFormat:NSLocalizedString(@"Rebase ”%@“ onto “%@", @"Contextual Menu Item to rebase HEAD onto the selected ref"), headRefName, refName];
130-
[items addObject:[PBRefMenuItem itemWithTitle:rebaseTitle action:@selector(rebaseHeadBranch:) enabled:!isOnHeadBranch]];
126+
[items addObject:[NSMenuItem pb_itemWithTitle:rebaseTitle action:@selector(rebaseHeadBranch:) enabled:!isOnHeadBranch]];
131127

132-
[items addObject:[PBRefMenuItem separatorItem]];
128+
[items addObject:[NSMenuItem separatorItem]];
133129
}
134130

135131
// fetch
136132
NSString *fetchTitle = hasRemote
137133
? [NSString stringWithFormat:NSLocalizedString(@"Fetch “%@", @"Contextual Menu Item to fetch the selected remote"), remoteName]
138134
: NSLocalizedString(@"Fetch", @"Inactive Contextual Menu Item for fetching");
139-
[items addObject:[PBRefMenuItem itemWithTitle:fetchTitle action:@selector(fetchRemote:) enabled:hasRemote]];
135+
[items addObject:[NSMenuItem pb_itemWithTitle:fetchTitle action:@selector(fetchRemote:) enabled:hasRemote]];
140136

141137
// pull
142138
NSString *pullTitle = hasRemote
143139
? [NSString stringWithFormat:NSLocalizedString(@"Pull “%@” and Update “%@", @"Contextual Menu Item to pull the remote and update the selected branch"), remoteName, headRefName]
144140
: NSLocalizedString(@"Pull", @"Inactive Contextual Menu Item for pulling");
145-
[items addObject:[PBRefMenuItem itemWithTitle:pullTitle action:@selector(pullRemote:) enabled:hasRemote]];
141+
[items addObject:[NSMenuItem pb_itemWithTitle:pullTitle action:@selector(pullRemote:) enabled:hasRemote]];
146142

147143
// push
148144
if (isRemote || ref.isRemoteBranch) {
149145
// push updates to remote
150146
NSString *pushTitle = [NSString stringWithFormat:NSLocalizedString(@"Push Updates to “%@", @"Contextual Menu Item to push updates of the selected ref to he named remote"), remoteName];
151-
[items addObject:[PBRefMenuItem itemWithTitle:pushTitle action:@selector(pushUpdatesToRemote:) enabled:YES]];
147+
[items addObject:[NSMenuItem pb_itemWithTitle:pushTitle action:@selector(pushUpdatesToRemote:) enabled:YES]];
152148
}
153149
else if (isDetachedHead) {
154-
[items addObject:[PBRefMenuItem itemWithTitle:NSLocalizedString(@"Push", @"Inactive Contextual Menu Item for pushing") action:nil enabled:NO]];
150+
[items addObject:[NSMenuItem pb_itemWithTitle:NSLocalizedString(@"Push", @"Inactive Contextual Menu Item for pushing") action:nil enabled:NO]];
155151
}
156152
else {
157153
// push to default remote
158154
BOOL hasDefaultRemote = NO;
159155
if (!ref.isTag && hasRemote) {
160156
hasDefaultRemote = YES;
161157
NSString *pushTitle = [NSString stringWithFormat:NSLocalizedString(@"Push “%@” to “%@", @"Contextual Menu Item to push a ref to a specific remote"), refName, remoteName];
162-
[items addObject:[PBRefMenuItem itemWithTitle:pushTitle action:@selector(pushDefaultRemoteForRef:) enabled:YES]];
158+
[items addObject:[NSMenuItem pb_itemWithTitle:pushTitle action:@selector(pushDefaultRemoteForRef:) enabled:YES]];
163159
}
164160

165161
// push to remotes submenu
166162
NSArray *remoteNames = [repo remotes];
167163
if ([remoteNames count] && !(hasDefaultRemote && ([remoteNames count] == 1))) {
168164
NSString *pushToTitle = [NSString stringWithFormat:NSLocalizedString(@"Push “%@” to", @"Contextual Menu Submenu Item containing the remotes the selected ref can be pushed to"), refName];
169-
PBRefMenuItem *pushToItem = [PBRefMenuItem itemWithTitle:pushToTitle action:nil enabled:YES];
165+
NSMenuItem *pushToItem = [NSMenuItem pb_itemWithTitle:pushToTitle action:nil enabled:YES];
170166
NSMenu *remotesMenu = [[NSMenu alloc] initWithTitle:NSLocalizedString(@"Remotes Menu", @"Menu listing the repository’s remotes")];
171167
for (NSString *remote in remoteNames) {
172-
PBRefMenuItem *remoteItem = [PBRefMenuItem itemWithTitle:remote action:@selector(pushToRemote:) enabled:YES];
173-
remoteItem.target = target;
168+
NSMenuItem *remoteItem = [NSMenuItem pb_itemWithTitle:remote action:@selector(pushToRemote:) enabled:YES];
174169
remoteItem.representedObject = remote;
175170
[remotesMenu addItem:remoteItem];
176171
}
@@ -181,20 +176,19 @@ + (PBRefMenuItem *) separatorItem
181176
}
182177

183178
// delete ref
184-
[items addObject:[PBRefMenuItem separatorItem]];
179+
[items addObject:[NSMenuItem separatorItem]];
185180
BOOL isStash = [[ref ref] hasPrefix:@"refs/stash"];
186181
BOOL isDeleteEnabled = !(isDetachedHead || isHead || isStash);
187182
if (isDeleteEnabled) {
188183
NSString *deleteFormat = ref.isRemote
189184
? NSLocalizedString(@"Delete “%@”…", @"Contextual Menu Item to delete a local ref (e.g. branch)")
190185
: NSLocalizedString(@"Remove “%@”…", @"Contextual Menu Item to remove a remote");
191186
NSString *deleteItemTitle = [NSString stringWithFormat:deleteFormat, refName];
192-
PBRefMenuItem *deleteItem = [PBRefMenuItem itemWithTitle:deleteItemTitle action:@selector(showDeleteRefSheet:) enabled:YES];
187+
NSMenuItem *deleteItem = [NSMenuItem pb_itemWithTitle:deleteItemTitle action:@selector(showDeleteRefSheet:) enabled:YES];
193188
[items addObject:deleteItem];
194189
}
195190

196-
for (PBRefMenuItem *item in items) {
197-
item.target = target;
191+
for (NSMenuItem *item in items) {
198192
if (!item.representedObject) {
199193
item.representedObject = ref;
200194
}
@@ -204,7 +198,7 @@ + (PBRefMenuItem *) separatorItem
204198
}
205199

206200

207-
+ (NSArray<NSMenuItem *> *) defaultMenuItemsForCommits:(NSArray<PBGitCommit *> *)commits target:(id)target
201+
+ (NSArray<NSMenuItem *> *)pb_defaultMenuItemsForCommits:(NSArray<PBGitCommit *> *)commits
208202
{
209203
NSMutableArray *items = [NSMutableArray array];
210204

@@ -216,44 +210,43 @@ + (PBRefMenuItem *) separatorItem
216210
BOOL isHead = [firstCommit.OID isEqual:firstCommit.repository.headOID];
217211

218212
if (isSingleCommitSelection) {
219-
[items addObject:[PBRefMenuItem itemWithTitle:NSLocalizedString(@"Checkout Commit", @"Contextual Menu Item to check out the selected commit") action:@selector(checkout:) enabled:YES]];
220-
[items addObject:[PBRefMenuItem separatorItem]];
213+
[items addObject:[NSMenuItem pb_itemWithTitle:NSLocalizedString(@"Checkout Commit", @"Contextual Menu Item to check out the selected commit") action:@selector(checkout:) enabled:YES]];
214+
[items addObject:[NSMenuItem separatorItem]];
221215

222-
[items addObject:[PBRefMenuItem itemWithTitle:NSLocalizedString(@"Create Branch…", @"Contextual Menu Item to create a branch at the selected commit") action:@selector(createBranch:) enabled:YES]];
223-
[items addObject:[PBRefMenuItem itemWithTitle:NSLocalizedString(@"Create Tag…", @"Contextual Menu Item to create a tag at the selected commit") action:@selector(createTag:) enabled:YES]];
224-
[items addObject:[PBRefMenuItem separatorItem]];
216+
[items addObject:[NSMenuItem pb_itemWithTitle:NSLocalizedString(@"Create Branch…", @"Contextual Menu Item to create a branch at the selected commit") action:@selector(createBranch:) enabled:YES]];
217+
[items addObject:[NSMenuItem pb_itemWithTitle:NSLocalizedString(@"Create Tag…", @"Contextual Menu Item to create a tag at the selected commit") action:@selector(createTag:) enabled:YES]];
218+
[items addObject:[NSMenuItem separatorItem]];
225219
}
226220

227-
[items addObject:[PBRefMenuItem itemWithTitle:NSLocalizedString(@"Copy SHA", @"Contextual Menu Item to copy the selected commits’ full SHA(s)") action:@selector(copySHA:) enabled:YES]];
228-
[items addObject:[PBRefMenuItem itemWithTitle:NSLocalizedString(@"Copy short SHA", @"Contextual Menu Item to copy the selected commits’ short SHA(s)") action:@selector(copyShortSHA:) enabled:YES]];
229-
[items addObject:[PBRefMenuItem itemWithTitle:NSLocalizedString(@"Copy Patch", @"Contextual Menu Item to copy the selected commits as patch(es)") action:@selector(copyPatch:) enabled:YES]];
221+
[items addObject:[NSMenuItem pb_itemWithTitle:NSLocalizedString(@"Copy SHA", @"Contextual Menu Item to copy the selected commits’ full SHA(s)") action:@selector(copySHA:) enabled:YES]];
222+
[items addObject:[NSMenuItem pb_itemWithTitle:NSLocalizedString(@"Copy short SHA", @"Contextual Menu Item to copy the selected commits’ short SHA(s)") action:@selector(copyShortSHA:) enabled:YES]];
223+
[items addObject:[NSMenuItem pb_itemWithTitle:NSLocalizedString(@"Copy Patch", @"Contextual Menu Item to copy the selected commits as patch(es)") action:@selector(copyPatch:) enabled:YES]];
230224

231225
if (isSingleCommitSelection) {
232226
NSString *diffTitle = [NSString stringWithFormat:NSLocalizedString(@"Diff with “%@", @"Contextual Menu Item to view a diff between the selected commit and HEAD"), headBranchName];
233-
[items addObject:[PBRefMenuItem itemWithTitle:diffTitle action:@selector(diffWithHEAD:) enabled:!isHead]];
234-
[items addObject:[PBRefMenuItem separatorItem]];
227+
[items addObject:[NSMenuItem pb_itemWithTitle:diffTitle action:@selector(diffWithHEAD:) enabled:!isHead]];
228+
[items addObject:[NSMenuItem separatorItem]];
235229

236230
// merge commit
237231
NSString *mergeTitle = isOnHeadBranch
238232
? NSLocalizedString(@"Merge Commit", @"Inactive Contextual Menu Item for merging commits")
239233
: [NSString stringWithFormat:NSLocalizedString(@"Merge Commit into “%@", @"Contextual Menu Item to merge the selected commit into HEAD"), headBranchName];
240-
[items addObject:[PBRefMenuItem itemWithTitle:mergeTitle action:@selector(merge:) enabled:!isOnHeadBranch]];
234+
[items addObject:[NSMenuItem pb_itemWithTitle:mergeTitle action:@selector(merge:) enabled:!isOnHeadBranch]];
241235

242236
// cherry pick
243237
NSString *cherryPickTitle = isOnHeadBranch
244238
? NSLocalizedString(@"Cherry Pick Commit", @"Inactive Contextual Menu Item for cherry-picking commits")
245239
: [NSString stringWithFormat:NSLocalizedString(@"Cherry Pick Commit to “%@", @"Contextual Menu Item to cherry-pick the selected commit on top of HEAD"), headBranchName];
246-
[items addObject:[PBRefMenuItem itemWithTitle:cherryPickTitle action:@selector(cherryPick:) enabled:!isOnHeadBranch]];
240+
[items addObject:[NSMenuItem pb_itemWithTitle:cherryPickTitle action:@selector(cherryPick:) enabled:!isOnHeadBranch]];
247241

248242
// rebase
249243
NSString *rebaseTitle = isOnHeadBranch
250244
? NSLocalizedString(@"Rebase Commit", @"Inactive Contextual Menu Item for rebasing onto commits")
251245
: [NSString stringWithFormat:NSLocalizedString(@"Rebase “%@” onto Commit", @"Contextual Menu Item to rebase the HEAD branch onto the selected commit"), headBranchName];
252-
[items addObject:[PBRefMenuItem itemWithTitle:rebaseTitle action:@selector(rebaseHeadBranch:) enabled:!isOnHeadBranch]];
246+
[items addObject:[NSMenuItem pb_itemWithTitle:rebaseTitle action:@selector(rebaseHeadBranch:) enabled:!isOnHeadBranch]];
253247
}
254248

255-
for (PBRefMenuItem *item in items) {
256-
item.target = target;
249+
for (NSMenuItem *item in items) {
257250
if (!item.representedObject) {
258251
item.representedObject = isSingleCommitSelection ? firstCommit : commits;
259252
}

0 commit comments

Comments
 (0)