File: utility.cpp

package info (click to toggle)
ares 126-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 32,600 kB
  • sloc: cpp: 356,508; ansic: 20,394; makefile: 16; sh: 2
file content (84 lines) | stat: -rw-r--r-- 3,682 bytes parent folder | download | duplicates (2)
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
auto NSMakeColor(const hiro::Color& color) -> NSColor* {
  return [NSColor colorWithRed:(color.red() / 255.0) green:(color.green() / 255.0) blue:(color.blue() / 255.0) alpha:(color.alpha() / 255.0)];
}

auto NSMakeColor(const hiro::SystemColor color) -> NSColor* {
  switch(color) {
    case hiro::SystemColor::Text: return [NSColor textColor];
    case hiro::SystemColor::Label:
      if([NSColor respondsToSelector:@selector(labelColor)]) return [NSColor labelColor];
      return [NSColor textColor];
    case hiro::SystemColor::Sublabel:
      if([NSColor respondsToSelector:@selector(secondaryLabelColor)]) return [NSColor secondaryLabelColor];
      return [NSColor colorWithWhite:2/3.0 alpha:1.0];
    case hiro::SystemColor::Link:
      if([NSColor respondsToSelector:@selector(linkColor)]) return [NSColor linkColor];
      return [NSColor colorWithRed:0.21 green:0.48 blue:1.0 alpha:1.0];
    case hiro::SystemColor::PlaceholderText:
      if([NSColor respondsToSelector:@selector(placeholderTextColor)]) return [NSColor placeholderTextColor];
      return [NSColor colorWithWhite:0.5 alpha:1.0];
  }
  return nil;
}

auto NSMakeCursor(const hiro::MouseCursor& mouseCursor) -> NSCursor* {
  if(mouseCursor == hiro::MouseCursor::Hand) return [NSCursor pointingHandCursor];
  if(mouseCursor == hiro::MouseCursor::HorizontalResize) return [NSCursor resizeLeftRightCursor];
  if(mouseCursor == hiro::MouseCursor::VerticalResize) return [NSCursor resizeUpDownCursor];
  return nil;
}

auto NSMakeImage(image icon, u32 scaleWidth = 0, u32 scaleHeight = 0) -> NSImage* {
  if(!icon) return nil;

  if(scaleWidth && scaleHeight) icon.scale(scaleWidth, scaleHeight);
  icon.transform(0, 32, 255u << 24, 255u << 0, 255u << 8, 255u << 16);  //Cocoa stores images in ABGR format

  //create NSImage from memory
  NSImage* cocoaImage = [[NSImage alloc] initWithSize:NSMakeSize(icon.width(), icon.height())];
  NSBitmapImageRep* bitmap = [[NSBitmapImageRep alloc]
    initWithBitmapDataPlanes:nil
    pixelsWide:icon.width() pixelsHigh:icon.height()
    bitsPerSample:8 samplesPerPixel:4 hasAlpha:YES
    isPlanar:NO colorSpaceName:NSDeviceRGBColorSpace
    bitmapFormat:NSAlphaNonpremultipliedBitmapFormat
    bytesPerRow:(4 * icon.width()) bitsPerPixel:32
  ];
  memory::copy<u32>([bitmap bitmapData], icon.data(), icon.width() * icon.height());
  [cocoaImage addRepresentation:bitmap];
  return cocoaImage;
}

auto NSMakeImage(multiFactorImage icon, u32 scaleWidth = 0, u32 scaleHeight = 0) -> NSImage* {
  if(!icon) return nil;
  NSImage* cocoaImage = NSMakeImage(icon.lowDPI(), scaleWidth, scaleHeight);
  if(!icon.highDPI()) return cocoaImage;
  NSImage* retinaCocoaImage = NSMakeImage(icon.highDPI(), scaleWidth * 2, scaleHeight * 2);
  [cocoaImage addRepresentation:retinaCocoaImage.representations[0]];
  
  return cocoaImage;
}

auto DropPathsOperation(id<NSDraggingInfo> sender) -> NSDragOperation {
  NSPasteboard* pboard = [sender draggingPasteboard];
  if([[pboard types] containsObject:NSFilenamesPboardType]) {
    if([sender draggingSourceOperationMask] & NSDragOperationGeneric) {
      return NSDragOperationGeneric;
    }
  }
  return NSDragOperationNone;
}

auto DropPaths(id<NSDraggingInfo> sender) -> vector<string> {
  vector<string> paths;
  NSPasteboard* pboard = [sender draggingPasteboard];
  if([[pboard types] containsObject:NSFilenamesPboardType]) {
    NSArray* files = [pboard propertyListForType:NSFilenamesPboardType];
    for(u32 n = 0; n < [files count]; n++) {
      string path = [[files objectAtIndex:n] UTF8String];
      if(directory::exists(path) && !path.endsWith("/")) path.append("/");
      paths.append(path);
    }
  }
  return paths;
}