Added NSStatusBar (systray) support

This commit is contained in:
Armando Rivera 2021-08-15 19:13:08 -04:00
parent b5d3ed13d8
commit b00a02b22e
4 changed files with 133 additions and 2 deletions

View File

@ -57,6 +57,9 @@ proc newSlider*(parent: ID; left, top, width, height: int, `func`: ACTION): ID {
proc newTableview*(parent: ID, left, top, width, height: cint): ID {.cdecl, importc: "createTableView".} proc newTableview*(parent: ID, left, top, width, height: cint): ID {.cdecl, importc: "createTableView".}
proc newTextEdit*(parent: ID; txt: cstring, left, top, width, height: int): ID {.cdecl, importc: "createTextEdit".} proc newTextEdit*(parent: ID; txt: cstring, left, top, width, height: int): ID {.cdecl, importc: "createTextEdit".}
proc newTextField*(parent: ID; txt: cstring; left: cint; top: cint; width: cint; height: cint): ID {.cdecl, importc: "createTextField".} proc newTextField*(parent: ID; txt: cstring; left: cint; top: cint; width: cint; height: cint): ID {.cdecl, importc: "createTextField".}
proc newStatusBar*(resource: cstring): ID {.cdecl, importc: "createStatusBar".}
proc newStatusMenu*(): ID {.cdecl, importc: "createStatusMenu".}
proc newStatusMenuItem*(parent: ID, tag: int, title: cstring, callback: ACTION) {.cdecl, importc: "createStatusItem".}
# GUI Containers # GUI Containers
@ -113,6 +116,15 @@ proc loadTableView*(tview: ID, path: cstring) {.cdecl, importc: "tableviewLoadFr
proc addTab*(parent: ID, label: cstring) {.cdecl, importc: "addTab".} proc addTab*(parent: ID, label: cstring) {.cdecl, importc: "addTab".}
proc getTab*(parent: ID, label: cstring): ID {.cdecl, importc: "getTab".} proc getTab*(parent: ID, label: cstring): ID {.cdecl, importc: "getTab".}
# StatusBar Functions
proc separator*(parent: ID) {.cdecl, importc: "addSeparator".}
proc setStatusMenu*(parent, menu: ID) {.cdecl,importc: "setMenu".}
proc tag*(widget: ID): int {.cdecl, importc: "tag".}
proc Status_Init*() {.cdecl, importc: "Status_Init".}
proc `menu=`*(parent,widget: ID ) =
parent.setStatusMenu(widget)
# *********** # ***********
# ** TO-DO ** # ** TO-DO **
# *********** # ***********

15
cocoa/NSStatusBar.nim Normal file
View File

@ -0,0 +1,15 @@
{.compile: "widgets/statusbar.m".}
import NSFunctions
proc newStatusBar*(resource: cstring): ID {.cdecl, importc: "createStatusBar".}
proc newStatusMenu*(): ID {.cdecl, importc: "createStatusMenu".}
proc newStatusMenuItem*(parent: ID, tag: int, title: cstring, callback: ACTION) {.cdecl, importc: "createStatusItem".}
proc separator*(parent: ID) {.cdecl, importc: "addSeparator".}
proc setStatusMenu*(parent, menu: ID) {.cdecl,importc: "setMenu".}
proc tag*(widget: ID): int {.cdecl, importc: "tag".}
proc Status_Init*() {.cdecl, importc: "Status_Init".}
proc `menu=`*(parent,widget: ID ) =
parent.setStatusMenu(widget)

102
cocoa/widgets/statusbar.m Normal file
View File

@ -0,0 +1,102 @@
#import <Cocoa/Cocoa.h>
#import <SystemConfiguration/SystemConfiguration.h>
#import <mach-o/getsect.h>
#import <mach-o/ldsyms.h>
#include <stdio.h>
typedef void(*ACTION)(id);
// SUBCLASSED NSMENUITEM ENABLING C-STYLE CALLBACKS
@interface StatusMenuItem: NSMenuItem{
ACTION menuAction;
}
@property ACTION menuAction;
- (void) click:(id)sender;
@end
@implementation StatusMenuItem
@synthesize menuAction;
- (void) click:(id)sender {
menuAction(sender);
}
@end
NSData *getResource(NSString *sectionName, NSString *itemName) {
unsigned long size;
void *ptr = getsectiondata(&_mh_execute_header, sectionName.UTF8String, itemName.UTF8String, &size);
NSData *resourceData = [NSData dataWithBytesNoCopy:ptr length:size freeWhenDone:NO];
return resourceData;
}
id createStatusBar(const char *resource) {
NSString *tmpStr = [NSString stringWithUTF8String: resource];
NSStatusItem *self;
NSStatusBar *statusBar = [NSStatusBar systemStatusBar];
self = [statusBar statusItemWithLength:NSSquareStatusItemLength];
NSImage *icon = [[NSImage alloc] initWithContentsOfFile: tmpStr];
icon.template = YES;
self.button.image = icon;
return self;
}
void setMenu(id parent, id menu) {
// ((NSStatusItem *)parent).menu = menu;
[(NSStatusItem *)parent setMenu: menu];
}
// FUNCTION TO CREATE MENU ITEMS/ENTRIES
void createStatusItem(id parent, int tag, const char *title, ACTION func) {
StatusMenuItem *self = [StatusMenuItem new];
self.title = [NSString stringWithUTF8String: title];
self.target = self;
if (func)
{
self.menuAction = func;
self.action = @selector(click:);
}
if (tag) {self.tag = tag;}
[parent addItem: self];
// return self;
}
// FUNCTION THAT RETURNS A MENU OBJECT
NSMenu *createStatusMenu() {
return NSMenu.new;
}
void setTag(id widget, int number) {
((NSMenuItem*)widget).tag = number;
}
int tag(id widget) {
return ((NSMenuItem*)widget).tag;
}
// FUNCTION TO ADD A SEPARATOR TO A MENU
void addSeparator(id parent) {
[parent addItem: [NSMenuItem separatorItem]];
}
void Status_Init() {
[NSApplication sharedApplication];
[NSApp setActivationPolicy:NSApplicationActivationPolicyAccessory];
}
// FUNCTION THAT PASSES CONTROL TO COCOA SUBSYSTEM
void Status_Run(id obj) {
[NSApp setDelegate: obj];
[NSApp run];
}

View File

@ -20,7 +20,8 @@ import Cocoa / [NSButton,
NSTableview, NSTableview,
NSTextedit, NSTextedit,
NSTextfield, NSTextfield,
NSWindow NSWindow,
NSStatusBar
] ]
export NSButton, export NSButton,
@ -43,4 +44,5 @@ export NSButton,
NSTableview, NSTableview,
NSTextedit, NSTextedit,
NSTextfield, NSTextfield,
NSWindow NSWindow,
NSStatusBar