Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 260 lines (227 sloc) 9.055 kb
4dbcf248 »
2011-09-06 Clean up BookmarkListView a bit. Add ability to change font size of b…
1 //
2 // ToolJobs.m
3 // iTerm
4 //
5 // Created by George Nachman on 9/6/11.
6 // Copyright 2011 Georgetech. All rights reserved.
7 //
8
9 #import "ToolJobs.h"
10 #import "ToolWrapper.h"
11 #import "PseudoTerminal.h"
12 #import "PTYSession.h"
13 #import "PTYTask.h"
14 #import "ProcessCache.h"
15
16 static const int kMaxJobs = 20;
c8a0b488 »
2011-09-19 Fix bug where tools would become broken after becoming too short. Add…
17 static const CGFloat kButtonHeight = 23;
18 static const CGFloat kMargin = 4;
4dbcf248 »
2011-09-06 Clean up BookmarkListView a bit. Add ability to change font size of b…
19
20 @interface ToolJobs ()
21 - (void)updateTimer:(id)sender;
22 @end
23
24 @implementation ToolJobs
25
26 @synthesize hasSelection;
27
28 - (id)initWithFrame:(NSRect)frame {
29 self = [super initWithFrame:frame];
30 if (self) {
31 names_ = [[NSMutableArray alloc] init];
32 pids_ = [[NSMutableArray alloc] init];
33
34 kill_ = [[NSButton alloc] initWithFrame:NSMakeRect(0, frame.size.height - kButtonHeight, frame.size.width, kButtonHeight)];
35 [kill_ setButtonType:NSMomentaryPushInButton];
36 [kill_ setTitle:@"Send Signal"];
37 [kill_ setTarget:self];
38 [kill_ setAction:@selector(kill:)];
39 [kill_ setBezelStyle:NSSmallSquareBezelStyle];
40 [kill_ sizeToFit];
41 [kill_ setAutoresizingMask:NSViewMinYMargin];
42 [self addSubview:kill_];
43 [kill_ release];
44 [kill_ bind:@"enabled" toObject:self withKeyPath:@"hasSelection" options:nil];
45 signal_ = [[NSPopUpButton alloc] initWithFrame:NSMakeRect(kill_.frame.size.width + kMargin, frame.size.height - kButtonHeight + 1,
46 1, 22)];
47 struct { int num; NSString *name; } signals[] = {
48 { 1, @"HUP", },
49 { 2, @"INTR", },
50 { 3, @"QUIT", },
51 { 6, @"ABRT", },
52 { 9, @"KILL", },
53 { 15, @"TERM" },
54 { 0, nil }
55 };
56 for (int i = 0; signals[i].num; i++) {
57 NSMenuItem *item = [[[NSMenuItem alloc] initWithTitle:signals[i].name action:nil keyEquivalent:@""] autorelease];
58 [item setTarget:self];
59 [item setTag:signals[i].num];
60 [[signal_ menu] addItem:item];
61 if (signals[i].num == 9) {
62 [signal_ selectItem:item];
63 }
64 }
65 [[signal_ cell] setControlSize:NSSmallControlSize];
66 [[signal_ cell] setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]];
67 [signal_ setPullsDown:NO];
68 [signal_ setAutoresizingMask:NSViewMinYMargin];
69 [signal_ sizeToFit];
70 [self addSubview:signal_];
71
72 scrollView_ = [[NSScrollView alloc] initWithFrame:NSMakeRect(0, 0, frame.size.width, frame.size.height - kButtonHeight - kMargin)];
73 [scrollView_ setHasVerticalScroller:YES];
74 [scrollView_ setHasHorizontalScroller:NO];
75 NSSize contentSize = [scrollView_ contentSize];
76 [scrollView_ setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
77
78 tableView_ = [[NSTableView alloc] initWithFrame:NSMakeRect(0, 0, contentSize.width, contentSize.height)];
79 NSTableColumn *col;
80 col = [[NSTableColumn alloc] initWithIdentifier:@"name"];
81 [col setEditable:NO];
82 [tableView_ addTableColumn:col];
83 [[col headerCell] setStringValue:@"Name"];
84 NSFont *theFont = [NSFont systemFontOfSize:[NSFont smallSystemFontSize]];
85 [[col dataCell] setFont:theFont];
86 [tableView_ setRowHeight:[[[[NSLayoutManager alloc] init] autorelease] defaultLineHeightForFont:theFont]];
87
88 col = [[NSTableColumn alloc] initWithIdentifier:@"pid"];
89 [col setEditable:NO];
90 [col setWidth:75];
91 [col setMinWidth:75];
92 [col setMaxWidth:75];
93 [tableView_ addTableColumn:col];
94 [[col dataCell] setFont:theFont];
95 [[col headerCell] setStringValue:@"pid"];
96
97 [tableView_ setDataSource:self];
98 [tableView_ setDelegate:self];
99
100 [tableView_ setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
101
102
103 [scrollView_ setDocumentView:tableView_];
104 [self addSubview:scrollView_];
105
106 [tableView_ sizeToFit];
107 [tableView_ setColumnAutoresizingStyle:NSTableViewSequentialColumnAutoresizingStyle];
72404b8b »
2011-09-07 slow down jobs timer when not key window. optimize jobs timer if no c…
108
4dbcf248 »
2011-09-06 Clean up BookmarkListView a bit. Add ability to change font size of b…
109 timer_ = [NSTimer scheduledTimerWithTimeInterval:1
110 target:self
111 selector:@selector(updateTimer:)
112 userInfo:nil
72404b8b »
2011-09-07 slow down jobs timer when not key window. optimize jobs timer if no c…
113 repeats:YES];
114
115 [[NSNotificationCenter defaultCenter] addObserver:self
116 selector:@selector(setSlowTimer)
117 name:NSWindowDidResignMainNotification
118 object:nil];
119 [[NSNotificationCenter defaultCenter] addObserver:self
120 selector:@selector(setFastTimer)
121 name:NSWindowDidBecomeKeyNotification
122 object:nil];
123
4dbcf248 »
2011-09-06 Clean up BookmarkListView a bit. Add ability to change font size of b…
124 [self updateTimer:nil];
125 }
126 return self;
127 }
128
c8a0b488 »
2011-09-19 Fix bug where tools would become broken after becoming too short. Add…
129 - (void)relayout
130 {
131 NSRect frame = self.frame;
132 kill_.frame = NSMakeRect(0, frame.size.height - kButtonHeight, frame.size.width, kButtonHeight);
133 [kill_ sizeToFit];
134 signal_.frame = NSMakeRect(kill_.frame.size.width + kMargin, frame.size.height - kButtonHeight + 1,
135 1, 22);
136 [signal_ sizeToFit];
137 scrollView_.frame = NSMakeRect(0, 0, frame.size.width, frame.size.height - kButtonHeight - kMargin);
138 }
139
72404b8b »
2011-09-07 slow down jobs timer when not key window. optimize jobs timer if no c…
140 // When not key, check much less often to avoid burning the battery.
141 - (void)setSlowTimer
142 {
143 [timer_ invalidate];
144 timer_ = [NSTimer scheduledTimerWithTimeInterval:10
145 target:self
146 selector:@selector(updateTimer:)
147 userInfo:nil
148 repeats:YES];
149 }
150
151 - (void)setFastTimer
152 {
153 [self updateTimer:nil];
154 [timer_ invalidate];
155 timer_ = [NSTimer scheduledTimerWithTimeInterval:1
156 target:self
157 selector:@selector(updateTimer:)
158 userInfo:nil
159 repeats:YES];
160 }
161
4dbcf248 »
2011-09-06 Clean up BookmarkListView a bit. Add ability to change font size of b…
162 - (void)dealloc
163 {
0e3edbf6 »
2011-09-07 Remove oberserver from jobs tool to avoid a crash
164 [[NSNotificationCenter defaultCenter] removeObserver:self];
4dbcf248 »
2011-09-06 Clean up BookmarkListView a bit. Add ability to change font size of b…
165 [signal_ release];
166 [tableView_ release];
167 [scrollView_ release];
168 [timer_ invalidate];
169 [names_ release];
170 [pids_ release];
171 [super dealloc];
172 }
173
174 - (void)shutdown
175 {
371d8b01 »
2011-09-07 Fix crash when closing a window because some toolbelt tools had a not…
176 [[NSNotificationCenter defaultCenter] removeObserver:self];
d4aca591 »
2012-01-23 Don't run timers after shutdown is called--crashes when a window is c…
177 shutdown_ = YES;
4dbcf248 »
2011-09-06 Clean up BookmarkListView a bit. Add ability to change font size of b…
178 [timer_ invalidate];
179 [kill_ unbind:@"enabled"];
180 timer_ = nil;
181 }
182
183 - (void)updateTimer:(id)sender
184 {
d4aca591 »
2012-01-23 Don't run timers after shutdown is called--crashes when a window is c…
185 if (shutdown_) {
186 return;
187 }
4dbcf248 »
2011-09-06 Clean up BookmarkListView a bit. Add ability to change font size of b…
188 ToolWrapper *wrapper = (ToolWrapper *)[[self superview] superview];
189 pid_t rootPid = [[[wrapper.term currentSession] SHELL] pid];
190 NSSet *pids = [[ProcessCache sharedInstance] childrenOfPid:rootPid levelsToSkip:0];
72404b8b »
2011-09-07 slow down jobs timer when not key window. optimize jobs timer if no c…
191 if ([pids isEqualToSet:[NSSet setWithArray:pids_]]) {
192 // Nothing to do, skip the expensive step of getting names.
193 return;
194 }
4dbcf248 »
2011-09-06 Clean up BookmarkListView a bit. Add ability to change font size of b…
195 [pids_ release];
196 pids_ = [[[pids allObjects] sortedArrayUsingSelector:@selector(compare:)] retain];
197 [names_ removeAllObjects];
198 int i = 0;
199 for (NSNumber *pid in pids_) {
200 BOOL fg;
ab0f3ce5 »
2012-01-31 Fix a bug where ProcessCache getNameOfPid:isForeground: returns null …
201 NSString *pidName;
202 pidName = [[ProcessCache sharedInstance] getNameOfPid:[pid intValue]
203 isForeground:&fg];
204 if (pidName) {
205 [names_ addObject:pidName];
206 i++;
207 if (i > kMaxJobs) {
208 break;
209 }
4dbcf248 »
2011-09-06 Clean up BookmarkListView a bit. Add ability to change font size of b…
210 }
211 }
212 [tableView_ reloadData];
72404b8b »
2011-09-07 slow down jobs timer when not key window. optimize jobs timer if no c…
213
214 // Updating the table data causes the cursor to change into an arrow!
215 [self performSelector:@selector(fixCursor) withObject:nil afterDelay:0];
216 }
217
218 - (void)fixCursor
219 {
d4aca591 »
2012-01-23 Don't run timers after shutdown is called--crashes when a window is c…
220 if (!shutdown_) {
221 ToolWrapper *wrapper = (ToolWrapper *)[[self superview] superview];
222 [[[wrapper.term currentSession] TEXTVIEW] updateCursor:[[NSApplication sharedApplication] currentEvent]];
223 }
4dbcf248 »
2011-09-06 Clean up BookmarkListView a bit. Add ability to change font size of b…
224 }
225
226 - (BOOL)isFlipped
227 {
228 return YES;
229 }
230
231 - (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
232 {
233 return [names_ count];
234 }
235
236 - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
237 {
238 if ([[aTableColumn identifier] isEqualToString:@"name"]) {
239 // name
240 return [names_ objectAtIndex:rowIndex];
241 } else {
242 // pid
243 return [[pids_ objectAtIndex:rowIndex] stringValue];
244 }
245 }
246
247 - (void)tableViewSelectionDidChange:(NSNotification *)aNotification
248 {
249 self.hasSelection = ([tableView_ selectedRow] >= 0);
250 }
251
252 - (void)kill:(id)sender
253 {
254 NSNumber *pid = [pids_ objectAtIndex:[tableView_ selectedRow]];
255 pid_t p = [pid intValue];
256 kill(p, [[signal_ selectedItem] tag]);
257 }
258
c8a0b488 »
2011-09-19 Fix bug where tools would become broken after becoming too short. Add…
259 @end
Something went wrong with that request. Please try again.