5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-05 05:52:33 +08:00

[v3 mac] ANSI systray label [WIP]

This commit is contained in:
Lea Anthony 2023-08-05 14:04:49 +10:00
parent 5e76a5e76a
commit ecf970d6fe
6 changed files with 105 additions and 2 deletions

View File

@ -35,6 +35,8 @@ func main() {
systemTray := app.NewSystemTray()
if runtime.GOOS == "darwin" {
systemTray.SetTemplateIcon(icons.SystrayMacTemplate)
systemTray.SetLabel("\u001B[1;31mWails\u001B[0m")
}
myMenu := app.NewMenu()

View File

@ -14,6 +14,7 @@ require (
github.com/jackmordaunt/icns/v2 v2.2.1
github.com/json-iterator/go v1.1.12
github.com/leaanthony/clir v1.6.0
github.com/leaanthony/go-ansi-parser v1.6.1
github.com/leaanthony/gosod v1.0.3
github.com/leaanthony/winicon v1.0.0
github.com/markbates/goth v1.77.0

View File

@ -189,6 +189,8 @@ github.com/leaanthony/clir v1.6.0 h1:mLV9thGkmqFqJU7ozmqlER8sBtGdZlz6H3gKsfIiB3o
github.com/leaanthony/clir v1.6.0/go.mod h1:k/RBkdkFl18xkkACMCLt09bhiZnrGORoxmomeMvDpE0=
github.com/leaanthony/debme v1.2.1 h1:9Tgwf+kjcrbMQ4WnPcEIUcQuIZYqdWftzZkBr+i/oOc=
github.com/leaanthony/debme v1.2.1/go.mod h1:3V+sCm5tYAgQymvSOfYQ5Xx2JCr+OXiD9Jkw3otUjiA=
github.com/leaanthony/go-ansi-parser v1.6.1 h1:xd8bzARK3dErqkPFtoF9F3/HgN8UQk0ed1YDKpEz01A=
github.com/leaanthony/go-ansi-parser v1.6.1/go.mod h1:+vva/2y4alzVmmIEpk9QDhA7vLC5zKDTRwfZGOp3IWU=
github.com/leaanthony/gosod v1.0.3 h1:Fnt+/B6NjQOVuCWOKYRREZnjGyvg+mEhd1nkkA04aTQ=
github.com/leaanthony/gosod v1.0.3/go.mod h1:BJ2J+oHsQIyIQpnLPjnqFGTMnOZXDbvWtRCSG7jGxs4=
github.com/leaanthony/slicer v1.5.0 h1:aHYTN8xbCCLxJmkNKiLB6tgcMARl4eWmH9/F+S/0HtY=

View File

@ -12,6 +12,7 @@ package application
*/
import "C"
import (
"github.com/leaanthony/go-ansi-parser"
"unsafe"
)
@ -123,7 +124,7 @@ func (s *macosSystemTray) run() {
s.nsStatusItem = unsafe.Pointer(C.systemTrayNew(C.long(s.id)))
if s.label != "" {
C.systemTraySetLabel(s.nsStatusItem, C.CString(s.label))
s.setLabel(s.label)
}
if s.icon != nil {
s.nsImage = unsafe.Pointer(C.imageFromBytes((*C.uchar)(&s.icon[0]), C.int(len(s.icon))))
@ -181,7 +182,27 @@ func newSystemTrayImpl(s *SystemTray) systemTrayImpl {
func (s *macosSystemTray) setLabel(label string) {
s.label = label
if !ansi.HasEscapeCodes(label) {
C.systemTraySetLabel(s.nsStatusItem, C.CString(label))
} else {
parsed, err := ansi.Parse(label)
if err != nil {
C.systemTraySetLabel(s.nsStatusItem, C.CString(label))
return
}
//TODO: Support more than one colour
newLabel := parsed[0].Label
var FG string
if parsed[0].FgCol != nil {
FG = parsed[0].FgCol.Hex
}
var BG string
if parsed[0].BgCol != nil {
BG = parsed[0].BgCol.Hex
}
C.systemTraySetANSILabel(s.nsStatusItem, C.CString(newLabel), C.CString(FG), C.CString(BG))
}
}
func (s *macosSystemTray) destroy() {

View File

@ -7,6 +7,8 @@
void* systemTrayNew(long id);
void systemTraySetLabel(void* nsStatusItem, char *label);
void systemTraySetANSILabel(void* nsStatusItem, char *label, char *FG, char *BG);
NSMutableAttributedString* createAttributedString(char *title, char *FG, char *BG);
NSImage* imageFromBytes(const unsigned char *bytes, int length);
void systemTraySetIcon(void* nsStatusItem, void* nsImage, int position, bool isTemplate);
void systemTraySetMenu(void* nsStatusItem, void* nsMenu);

View File

@ -40,6 +40,81 @@ void systemTraySetLabel(void* nsStatusItem, char *label) {
});
}
void systemTraySetANSILabel(void* nsStatusItem, char *label, char *FG, char *BG) {
if( label == NULL ) {
return;
}
NSLog(@"ANSI Label: %s\n", label);
//call createAttributedString
NSMutableAttributedString* attributedString = createAttributedString(label, FG, BG);
printf("ANSI Label 2: %s\n", label);
// Set the label
NSStatusItem *statusItem = (NSStatusItem *)nsStatusItem;
[statusItem setAttributedTitle:attributedString];
// [attributedString release];
// Free memory
free(label);
free(FG);
free(BG);
}
NSMutableAttributedString* createAttributedString(char *title, char *FG, char *BG) {
NSMutableDictionary *dictionary = [NSMutableDictionary new];
printf("1\n");
// RGBA
if(strlen(FG) > 0) {
printf("2\n");
unsigned short r, g, b, a;
// white by default
r = g = b = a = 255;
int count = sscanf(FG, "#%02hx%02hx%02hx%02hx", &r, &g, &b, &a);
printf("count: %d\n", count);
printf("r, g, b, a: %d, %d, %d, %d\n", r, g, b, a);
if (count > 0) {
NSColor *colour = [NSColor colorWithCalibratedRed:(CGFloat)r / 255.0
green:(CGFloat)g / 255.0
blue:(CGFloat)b / 255.0
alpha:(CGFloat)a / 255.0];
printf("here\n");
dictionary[NSForegroundColorAttributeName] = colour;
printf("here\n");
}
}
// Calculate BG colour
if(strlen(BG) > 0) {
printf("here2\n");
unsigned short r, g, b, a;
// white by default
r = g = b = a = 255;
int count = sscanf(BG, "#%02hx%02hx%02hx%02hx", &r, &g, &b, &a);
if (count > 0) {
NSColor *colour = [NSColor colorWithCalibratedRed:(CGFloat)r / 255.0
green:(CGFloat)g / 255.0
blue:(CGFloat)b / 255.0
alpha:(CGFloat)a / 255.0];
dictionary[NSBackgroundColorAttributeName] = colour;
}
}
printf("here6\n");
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithUTF8String:title] attributes:dictionary];
printf("eof\n");
return attributedString;
}
// Create an nsimage from a byte array
NSImage* imageFromBytes(const unsigned char *bytes, int length) {
NSData *data = [NSData dataWithBytes:bytes length:length];