Repository navigation
Expand file tree
/
Copy pathSCWeekGridView.m
More file actions
544 lines (438 loc) · 18.7 KB
/
Copy pathSCWeekGridView.m
File metadata and controls
544 lines (438 loc) · 18.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
//
// SCWeekGridView.m
// SelfControl
//
#import "SCWeekGridView.h"
#import "Block Management/SCScheduleManager.h"
// Layout constants
static const CGFloat kRowHeight = 60.0;
static const CGFloat kHeaderHeight = 30.0;
static const CGFloat kBundleLabelWidth = 120.0;
static const CGFloat kCellPadding = 4.0;
static const CGFloat kTimelineHeight = 48.0;
@interface SCWeekGridView ()
@property (nonatomic, strong, nullable) NSString *highlightedBundleID;
@property (nonatomic, assign) SCDayOfWeek highlightedDay;
@property (nonatomic, strong) NSTrackingArea *trackingArea;
@property (nonatomic, assign) NSPoint lastMousePoint;
// Hover tracking
@property (nonatomic, assign) NSInteger hoveredBundleIndex;
@property (nonatomic, assign) NSInteger hoveredDayIndex;
@property (nonatomic, assign) BOOL isHoveringBundleLabel;
@end
@implementation SCWeekGridView
- (instancetype)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self commonInit];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)coder {
self = [super initWithCoder:coder];
if (self) {
[self commonInit];
}
return self;
}
- (void)commonInit {
_showOnlyRemainingDays = YES;
_isCommitted = NO;
_highlightedDay = -1;
_hoveredBundleIndex = -1;
_hoveredDayIndex = -1;
_isHoveringBundleLabel = NO;
}
- (void)updateTrackingAreas {
[super updateTrackingAreas];
if (self.trackingArea) {
[self removeTrackingArea:self.trackingArea];
}
self.trackingArea = [[NSTrackingArea alloc] initWithRect:self.bounds
options:(NSTrackingMouseMoved |
NSTrackingMouseEnteredAndExited |
NSTrackingActiveInKeyWindow)
owner:self
userInfo:nil];
[self addTrackingArea:self.trackingArea];
}
#pragma mark - Data
- (void)reloadData {
[self setNeedsDisplay:YES];
}
- (nullable SCWeeklySchedule *)scheduleForBundle:(SCBlockBundle *)bundle {
for (SCWeeklySchedule *schedule in self.schedules) {
if ([schedule.bundleID isEqualToString:bundle.bundleID]) {
return schedule;
}
}
return nil;
}
- (NSArray<NSNumber *> *)daysToShow {
if (self.showOnlyRemainingDays) {
return [SCWeeklySchedule remainingDaysInWeekStartingMonday:YES];
}
return [SCWeeklySchedule allDaysStartingMonday:YES];
}
#pragma mark - Layout Calculations
- (CGFloat)cellWidthForDayCount:(NSUInteger)dayCount {
CGFloat availableWidth = self.bounds.size.width - kBundleLabelWidth;
return availableWidth / dayCount;
}
- (NSRect)rectForBundleLabel:(NSUInteger)bundleIndex {
CGFloat y = self.bounds.size.height - kHeaderHeight - (bundleIndex + 1) * kRowHeight;
return NSMakeRect(0, y, kBundleLabelWidth, kRowHeight);
}
- (NSRect)rectForCell:(NSUInteger)bundleIndex dayIndex:(NSUInteger)dayIndex dayCount:(NSUInteger)dayCount {
CGFloat cellWidth = [self cellWidthForDayCount:dayCount];
CGFloat x = kBundleLabelWidth + dayIndex * cellWidth;
CGFloat y = self.bounds.size.height - kHeaderHeight - (bundleIndex + 1) * kRowHeight;
return NSMakeRect(x, y, cellWidth, kRowHeight);
}
- (NSRect)rectForDayHeader:(NSUInteger)dayIndex dayCount:(NSUInteger)dayCount {
CGFloat cellWidth = [self cellWidthForDayCount:dayCount];
CGFloat x = kBundleLabelWidth + dayIndex * cellWidth;
CGFloat y = self.bounds.size.height - kHeaderHeight;
return NSMakeRect(x, y, cellWidth, kHeaderHeight);
}
#pragma mark - Drawing
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
// Background
[[NSColor windowBackgroundColor] setFill];
NSRectFill(dirtyRect);
NSArray<NSNumber *> *days = [self daysToShow];
NSUInteger dayCount = days.count;
if (dayCount == 0) return;
// Draw day headers
[self drawDayHeaders:days];
// Draw bundle rows
for (NSUInteger i = 0; i < self.bundles.count; i++) {
SCBlockBundle *bundle = self.bundles[i];
SCWeeklySchedule *schedule = [self scheduleForBundle:bundle];
// Draw bundle label
[self drawBundleLabel:bundle atIndex:i];
// Draw cells for each day
for (NSUInteger j = 0; j < dayCount; j++) {
SCDayOfWeek day = [days[j] integerValue];
[self drawCellForBundle:bundle
schedule:schedule
day:day
bundleIndex:i
dayIndex:j
dayCount:dayCount];
}
}
// Draw grid lines
[self drawGridLines:days];
// Draw continuous NOW line across all bundles for today
[self drawNowLine:days];
}
- (void)drawDayHeaders:(NSArray<NSNumber *> *)days {
NSDictionary *attrs = @{
NSFontAttributeName: [NSFont systemFontOfSize:12 weight:NSFontWeightMedium],
NSForegroundColorAttributeName: [NSColor secondaryLabelColor]
};
SCDayOfWeek today = [SCWeeklySchedule today];
BOOL isCurrentWeek = (self.weekOffset == 0);
for (NSUInteger i = 0; i < days.count; i++) {
SCDayOfWeek day = [days[i] integerValue];
NSRect headerRect = [self rectForDayHeader:i dayCount:days.count];
// Highlight today (only for current week)
if (isCurrentWeek && day == today) {
[[NSColor controlAccentColor] setFill];
NSRect highlightRect = NSInsetRect(headerRect, 2, 2);
highlightRect.size.height -= 2;
NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:highlightRect xRadius:4 yRadius:4];
[path fill];
attrs = @{
NSFontAttributeName: [NSFont systemFontOfSize:12 weight:NSFontWeightBold],
NSForegroundColorAttributeName: [NSColor whiteColor]
};
} else {
attrs = @{
NSFontAttributeName: [NSFont systemFontOfSize:12 weight:NSFontWeightMedium],
NSForegroundColorAttributeName: [NSColor secondaryLabelColor]
};
}
NSString *dayName = [SCWeeklySchedule shortNameForDay:day];
NSSize textSize = [dayName sizeWithAttributes:attrs];
NSPoint textPoint = NSMakePoint(
headerRect.origin.x + (headerRect.size.width - textSize.width) / 2,
headerRect.origin.y + (headerRect.size.height - textSize.height) / 2
);
[dayName drawAtPoint:textPoint withAttributes:attrs];
}
}
- (void)drawBundleLabel:(SCBlockBundle *)bundle atIndex:(NSUInteger)index {
NSRect labelRect = [self rectForBundleLabel:index];
// Check if this label is hovered
BOOL isHovered = (self.hoveredBundleIndex == (NSInteger)index && self.isHoveringBundleLabel);
// Draw hover background
if (isHovered) {
NSRect hoverRect = NSInsetRect(labelRect, 4, 4);
[[[NSColor controlBackgroundColor] blendedColorWithFraction:0.15 ofColor:[NSColor blackColor]] setFill];
NSBezierPath *hoverPath = [NSBezierPath bezierPathWithRoundedRect:hoverRect xRadius:4 yRadius:4];
[hoverPath fill];
}
// Color indicator
NSRect colorRect = NSMakeRect(labelRect.origin.x + 8,
labelRect.origin.y + (labelRect.size.height - 12) / 2,
12, 12);
[bundle.color setFill];
NSBezierPath *colorPath = [NSBezierPath bezierPathWithOvalInRect:colorRect];
[colorPath fill];
// Bundle name
NSDictionary *attrs = @{
NSFontAttributeName: [NSFont systemFontOfSize:12 weight:NSFontWeightMedium],
NSForegroundColorAttributeName: [NSColor labelColor]
};
NSRect textRect = NSMakeRect(colorRect.origin.x + 18,
labelRect.origin.y,
kBundleLabelWidth - 30,
labelRect.size.height);
NSMutableParagraphStyle *para = [[NSMutableParagraphStyle alloc] init];
para.lineBreakMode = NSLineBreakByTruncatingTail;
NSMutableDictionary *textAttrs = [attrs mutableCopy];
textAttrs[NSParagraphStyleAttributeName] = para;
NSSize textSize = [bundle.name sizeWithAttributes:textAttrs];
NSPoint textPoint = NSMakePoint(textRect.origin.x,
textRect.origin.y + (textRect.size.height - textSize.height) / 2);
[bundle.name drawAtPoint:textPoint withAttributes:textAttrs];
}
- (void)drawCellForBundle:(SCBlockBundle *)bundle
schedule:(SCWeeklySchedule *)schedule
day:(SCDayOfWeek)day
bundleIndex:(NSUInteger)bundleIndex
dayIndex:(NSUInteger)dayIndex
dayCount:(NSUInteger)dayCount {
NSRect cellRect = [self rectForCell:bundleIndex dayIndex:dayIndex dayCount:dayCount];
NSRect innerRect = NSInsetRect(cellRect, kCellPadding, kCellPadding);
// Check if this cell is highlighted (for copy/paste)
BOOL isHighlighted = [self.highlightedBundleID isEqualToString:bundle.bundleID] &&
self.highlightedDay == day;
// Check if this cell is hovered
BOOL isHovered = (self.hoveredBundleIndex == (NSInteger)bundleIndex &&
self.hoveredDayIndex == (NSInteger)dayIndex &&
!self.isHoveringBundleLabel);
// Cell background
if (isHighlighted) {
[[NSColor selectedContentBackgroundColor] setFill];
} else if (isHovered) {
// Darker background on hover
[[[NSColor controlBackgroundColor] blendedColorWithFraction:0.15 ofColor:[NSColor blackColor]] setFill];
} else {
[[NSColor controlBackgroundColor] setFill];
}
NSBezierPath *bgPath = [NSBezierPath bezierPathWithRoundedRect:innerRect xRadius:6 yRadius:6];
[bgPath fill];
// Draw timeline
[self drawTimelineInRect:innerRect schedule:schedule day:day bundleColor:bundle.color];
}
- (void)drawTimelineInRect:(NSRect)rect
schedule:(SCWeeklySchedule *)schedule
day:(SCDayOfWeek)day
bundleColor:(NSColor *)color {
// Timeline bar area
CGFloat timelineY = rect.origin.y + (rect.size.height - kTimelineHeight) / 2;
NSRect timelineRect = NSMakeRect(rect.origin.x + 4, timelineY, rect.size.width - 8, kTimelineHeight);
// Background (blocked = gray)
[[NSColor tertiaryLabelColor] setFill];
NSBezierPath *bgPath = [NSBezierPath bezierPathWithRoundedRect:timelineRect xRadius:4 yRadius:4];
[bgPath fill];
if (!schedule) return;
// Draw allowed windows as colored bars
NSArray<SCTimeRange *> *windows = [schedule allowedWindowsForDay:day];
for (SCTimeRange *window in windows) {
CGFloat startPercent = [window startMinutes] / (24.0 * 60.0);
CGFloat endPercent = [window endMinutes] / (24.0 * 60.0);
CGFloat startX = timelineRect.origin.x + startPercent * timelineRect.size.width;
CGFloat endX = timelineRect.origin.x + endPercent * timelineRect.size.width;
NSRect windowRect = NSMakeRect(startX, timelineRect.origin.y,
endX - startX, timelineRect.size.height);
// Use bundle color with some transparency
[[color colorWithAlphaComponent:0.8] setFill];
NSBezierPath *windowPath = [NSBezierPath bezierPathWithRoundedRect:windowRect xRadius:2 yRadius:2];
[windowPath fill];
}
// Draw time markers (6am, 12pm, 6pm)
[[NSColor quaternaryLabelColor] setStroke];
NSBezierPath *markerPath = [NSBezierPath bezierPath];
CGFloat markers[] = {0.25, 0.5, 0.75}; // 6am, 12pm, 6pm
for (int i = 0; i < 3; i++) {
CGFloat x = timelineRect.origin.x + markers[i] * timelineRect.size.width;
[markerPath moveToPoint:NSMakePoint(x, timelineRect.origin.y)];
[markerPath lineToPoint:NSMakePoint(x, timelineRect.origin.y + timelineRect.size.height)];
}
[markerPath setLineWidth:0.5];
[markerPath stroke];
// NOW line is drawn separately at grid level for continuity across rows
}
- (void)drawNowLine:(NSArray<NSNumber *> *)days {
// Only draw the NOW line for current week
if (self.weekOffset != 0) return;
// Find if today is visible
SCDayOfWeek today = [SCWeeklySchedule today];
NSInteger todayIndex = -1;
for (NSUInteger i = 0; i < days.count; i++) {
if ([days[i] integerValue] == today) {
todayIndex = i;
break;
}
}
if (todayIndex < 0 || self.bundles.count == 0) return;
// Calculate current time as percentage of day
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comps = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:[NSDate date]];
CGFloat nowPercent = (comps.hour * 60 + comps.minute) / (24.0 * 60.0);
// Get the X position within today's column
NSUInteger dayCount = days.count;
CGFloat cellWidth = [self cellWidthForDayCount:dayCount];
CGFloat cellX = kBundleLabelWidth + todayIndex * cellWidth;
// The timeline inside each cell has padding
CGFloat timelineLeft = cellX + kCellPadding + 4;
CGFloat timelineWidth = cellWidth - kCellPadding * 2 - 8;
CGFloat nowX = timelineLeft + nowPercent * timelineWidth;
// Calculate Y range: from first bundle row to last
CGFloat topY = self.bounds.size.height - kHeaderHeight;
CGFloat bottomY = self.bounds.size.height - kHeaderHeight - self.bundles.count * kRowHeight;
// Draw the continuous red line
[[NSColor systemRedColor] setStroke];
NSBezierPath *nowPath = [NSBezierPath bezierPath];
[nowPath setLineWidth:2.0];
[nowPath moveToPoint:NSMakePoint(nowX, bottomY)];
[nowPath lineToPoint:NSMakePoint(nowX, topY)];
[nowPath stroke];
}
- (void)drawGridLines:(NSArray<NSNumber *> *)days {
[[NSColor separatorColor] setStroke];
NSBezierPath *gridPath = [NSBezierPath bezierPath];
// Horizontal lines
for (NSUInteger i = 0; i <= self.bundles.count; i++) {
CGFloat y = self.bounds.size.height - kHeaderHeight - i * kRowHeight;
[gridPath moveToPoint:NSMakePoint(0, y)];
[gridPath lineToPoint:NSMakePoint(self.bounds.size.width, y)];
}
// Vertical line after bundle labels
[gridPath moveToPoint:NSMakePoint(kBundleLabelWidth, 0)];
[gridPath lineToPoint:NSMakePoint(kBundleLabelWidth, self.bounds.size.height)];
[gridPath setLineWidth:0.5];
[gridPath stroke];
}
#pragma mark - Mouse Handling
- (void)mouseMoved:(NSEvent *)event {
NSPoint point = [self convertPoint:event.locationInWindow fromView:nil];
[self updateHoverStateForPoint:point];
}
- (void)mouseExited:(NSEvent *)event {
self.hoveredBundleIndex = -1;
self.hoveredDayIndex = -1;
self.isHoveringBundleLabel = NO;
[[NSCursor arrowCursor] set];
[self setNeedsDisplay:YES];
}
- (void)updateHoverStateForPoint:(NSPoint)point {
NSArray<NSNumber *> *days = [self daysToShow];
NSUInteger dayCount = days.count;
NSInteger newBundleIndex = -1;
NSInteger newDayIndex = -1;
BOOL newIsHoveringLabel = NO;
// Check which element is under the mouse
for (NSUInteger i = 0; i < self.bundles.count; i++) {
// Check bundle label
NSRect labelRect = [self rectForBundleLabel:i];
if (NSPointInRect(point, labelRect)) {
newBundleIndex = i;
newIsHoveringLabel = YES;
break;
}
// Check day cells
for (NSUInteger j = 0; j < dayCount; j++) {
NSRect cellRect = [self rectForCell:i dayIndex:j dayCount:dayCount];
if (NSPointInRect(point, cellRect)) {
newBundleIndex = i;
newDayIndex = j;
break;
}
}
if (newBundleIndex >= 0) break;
}
// Update state if changed
if (newBundleIndex != self.hoveredBundleIndex ||
newDayIndex != self.hoveredDayIndex ||
newIsHoveringLabel != self.isHoveringBundleLabel) {
self.hoveredBundleIndex = newBundleIndex;
self.hoveredDayIndex = newDayIndex;
self.isHoveringBundleLabel = newIsHoveringLabel;
// Update cursor
if (newBundleIndex >= 0) {
[[NSCursor pointingHandCursor] set];
} else {
[[NSCursor arrowCursor] set];
}
[self setNeedsDisplay:YES];
}
}
- (void)mouseDown:(NSEvent *)event {
NSPoint point = [self convertPoint:event.locationInWindow fromView:nil];
NSArray<NSNumber *> *days = [self daysToShow];
NSUInteger dayCount = days.count;
// Check which cell was clicked
for (NSUInteger i = 0; i < self.bundles.count; i++) {
SCBlockBundle *bundle = self.bundles[i];
// Check bundle label (for editing bundle)
NSRect labelRect = [self rectForBundleLabel:i];
if (NSPointInRect(point, labelRect)) {
[self.delegate weekGridView:self didRequestEditBundle:bundle];
return;
}
// Check day cells
for (NSUInteger j = 0; j < dayCount; j++) {
NSRect cellRect = [self rectForCell:i dayIndex:j dayCount:dayCount];
if (NSPointInRect(point, cellRect)) {
SCDayOfWeek day = [days[j] integerValue];
[self.delegate weekGridView:self didSelectBundle:bundle forDay:day];
return;
}
}
}
}
- (void)rightMouseDown:(NSEvent *)event {
NSPoint point = [self convertPoint:event.locationInWindow fromView:nil];
NSArray<NSNumber *> *days = [self daysToShow];
NSUInteger dayCount = days.count;
// Check which cell was right-clicked
for (NSUInteger i = 0; i < self.bundles.count; i++) {
SCBlockBundle *bundle = self.bundles[i];
for (NSUInteger j = 0; j < dayCount; j++) {
NSRect cellRect = [self rectForCell:i dayIndex:j dayCount:dayCount];
if (NSPointInRect(point, cellRect)) {
SCDayOfWeek day = [days[j] integerValue];
if ([self.delegate respondsToSelector:@selector(weekGridView:didRightClickBundle:forDay:atPoint:)]) {
[self.delegate weekGridView:self didRightClickBundle:bundle forDay:day atPoint:point];
}
return;
}
}
}
}
#pragma mark - Highlighting
- (void)highlightCellForBundle:(NSString *)bundleID day:(SCDayOfWeek)day {
self.highlightedBundleID = bundleID;
self.highlightedDay = day;
[self setNeedsDisplay:YES];
}
- (void)clearCellHighlight {
self.highlightedBundleID = nil;
self.highlightedDay = -1;
[self setNeedsDisplay:YES];
}
#pragma mark - Intrinsic Size
- (NSSize)intrinsicContentSize {
CGFloat height = kHeaderHeight + self.bundles.count * kRowHeight;
return NSMakeSize(NSViewNoIntrinsicMetric, height);
}
- (BOOL)isFlipped {
return NO; // Use standard coordinate system (origin at bottom-left)
}
@end