◸code

背景

实现

代码

还是老规矩,直接放代码

// Method define
NAN_METHOD(MapTrafficButton) {
  void* buffer = node::Buffer::Data(info[0]->ToObject());
  if (!buffer) {
    ThrowError("MapTrafficButton invalid buffer handle !");
    return;
  }
  unsigned long handle = *reinterpret_cast<unsigned long *>(buffer);
  NSView* view = (NSView *)handle;
  // NSWindow handle
  NSWindow* win = [view window];
  // Traffic button handle
  NSButton* closeButton = [win standardWindowButton:NSWindowCloseButton];
  NSButton* minButton = [win standardWindowButton:NSWindowMiniaturizeButton];
  NSButton* zoomButton = [win standardWindowButton:NSWindowZoomButton];
  // TitleBar handle
  NSView* titlebarView = closeButton.superview;
  // Make NSWindow titlebar hight from 22 to 38
  NSToolbar* toolbar = [[NSToolbar alloc] initWithIdentifier:@"window-titlebar"];
  toolbar.showsBaselineSeparator = NO;
  [win setTitleVisibility:NSWindowTitleHidden];
  [win setToolbar:toolbar];
  // Traffic layout
  closeButton.translatesAutoresizingMaskIntoConstraints = NO;
  [titlebarView addConstraints:@[
    [NSLayoutConstraint constraintWithItem:closeButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:titlebarView attribute:NSLayoutAttributeTop multiplier:1 constant:15],
    [NSLayoutConstraint constraintWithItem:closeButton attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:titlebarView attribute:NSLayoutAttributeLeft multiplier:1 constant:6]
  ]];
  minButton.translatesAutoresizingMaskIntoConstraints = NO;
  [titlebarView addConstraints:@[
    [NSLayoutConstraint constraintWithItem:minButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:titlebarView attribute:NSLayoutAttributeTop multiplier:1 constant:15],
    [NSLayoutConstraint constraintWithItem:minButton attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:titlebarView attribute:NSLayoutAttributeLeft multiplier:1 constant:23]
  ]];
  zoomButton.translatesAutoresizingMaskIntoConstraints = NO;
  [titlebarView addConstraints:@[
    [NSLayoutConstraint constraintWithItem:zoomButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:titlebarView attribute:NSLayoutAttributeTop multiplier:1 constant:15],
    [NSLayoutConstraint constraintWithItem:zoomButton attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:titlebarView attribute:NSLayoutAttributeLeft multiplier:1 constant:40]
  ]];
}

// Export method
Set(target, New<v8::String>("mapTrafficButton").ToLocalChecked(), New<v8::FunctionTemplate>(MapTrafficButton)->GetFunction());