mirror of
https://github.com/wavetermdev/wails.git
synced 2026-08-05 13:53:43 -07:00
[v3] Support ANSI label on Mac. Update example.
This commit is contained in:
@@ -36,7 +36,7 @@ func main() {
|
||||
systemTray := app.NewSystemTray()
|
||||
if runtime.GOOS == "darwin" {
|
||||
systemTray.SetTemplateIcon(icons.SystrayMacTemplate)
|
||||
systemTray.SetLabel("\u001B[1;31mWails\u001B[0m")
|
||||
systemTray.SetLabel("\u001B[1;31mW\u001B[1;32ma\u001B[1;33mi\u001B[1;34ml\u001B[1;35ms\u001B[0m")
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -3,11 +3,12 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"github.com/lmittmann/tint"
|
||||
"github.com/mattn/go-isatty"
|
||||
"log/slog"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/lmittmann/tint"
|
||||
"github.com/mattn/go-isatty"
|
||||
)
|
||||
|
||||
func DefaultLogger() *slog.Logger {
|
||||
|
||||
@@ -12,8 +12,9 @@ package application
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
"github.com/leaanthony/go-ansi-parser"
|
||||
"unsafe"
|
||||
|
||||
"github.com/leaanthony/go-ansi-parser"
|
||||
)
|
||||
|
||||
type macosSystemTray struct {
|
||||
@@ -180,6 +181,17 @@ func newSystemTrayImpl(s *SystemTray) systemTrayImpl {
|
||||
return result
|
||||
}
|
||||
|
||||
func extractAnsiTextParts(text *ansi.StyledText) (label *C.char, fg *C.char, bg *C.char) {
|
||||
label = C.CString(text.Label)
|
||||
if text.FgCol != nil {
|
||||
fg = C.CString(text.FgCol.Hex)
|
||||
}
|
||||
if text.BgCol != nil {
|
||||
bg = C.CString(text.BgCol.Hex)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (s *macosSystemTray) setLabel(label string) {
|
||||
s.label = label
|
||||
if !ansi.HasEscapeCodes(label) {
|
||||
@@ -190,18 +202,19 @@ func (s *macosSystemTray) setLabel(label string) {
|
||||
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
|
||||
if len(parsed) == 0 {
|
||||
return
|
||||
}
|
||||
var BG string
|
||||
if parsed[0].BgCol != nil {
|
||||
BG = parsed[0].BgCol.Hex
|
||||
label, fg, bg := extractAnsiTextParts(parsed[0])
|
||||
var attributedString = C.createAttributedString(label, fg, bg)
|
||||
if len(parsed) > 1 {
|
||||
for _, parsedPart := range parsed[1:] {
|
||||
label, fg, bg = extractAnsiTextParts(parsedPart)
|
||||
attributedString = C.appendAttributedString(attributedString, label, fg, bg)
|
||||
}
|
||||
}
|
||||
|
||||
C.systemTraySetANSILabel(s.nsStatusItem, C.CString(newLabel), C.CString(FG), C.CString(BG))
|
||||
C.systemTraySetANSILabel(s.nsStatusItem, attributedString)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,8 +7,9 @@
|
||||
|
||||
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);
|
||||
void systemTraySetANSILabel(void* nsStatusItem, void* attributedString);
|
||||
void* createAttributedString(char *title, char *FG, char *BG);
|
||||
void* appendAttributedString(void* original, char* label, 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);
|
||||
|
||||
@@ -40,61 +40,54 @@ void systemTraySetLabel(void* nsStatusItem, char *label) {
|
||||
});
|
||||
}
|
||||
|
||||
void systemTraySetANSILabel(void* nsStatusItem, char *label, char *FG, char *BG) {
|
||||
void systemTraySetANSILabel(void* nsStatusItem, void* label) {
|
||||
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);
|
||||
NSMutableAttributedString* attributedString = (NSMutableAttributedString*) 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) {
|
||||
void* appendAttributedString(void *currentString, char *title, char *FG, char *BG) {
|
||||
|
||||
NSMutableAttributedString* newString = createAttributedString(title, FG, BG);
|
||||
if( currentString != NULL ) {
|
||||
NSMutableAttributedString* current = (NSMutableAttributedString*)currentString;
|
||||
[current appendAttributedString:newString];
|
||||
newString = current;
|
||||
}
|
||||
|
||||
return (void*)newString;
|
||||
}
|
||||
|
||||
void* createAttributedString(char *title, char *FG, char *BG) {
|
||||
|
||||
NSMutableDictionary *dictionary = [NSMutableDictionary new];
|
||||
printf("1\n");
|
||||
|
||||
// RGBA
|
||||
if(strlen(FG) > 0) {
|
||||
printf("2\n");
|
||||
if(FG != NULL && strlen(FG) > 0) {
|
||||
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");
|
||||
|
||||
if(BG != NULL && strlen(BG) > 0) {
|
||||
unsigned short r, g, b, a;
|
||||
|
||||
// white by default
|
||||
@@ -108,11 +101,8 @@ NSMutableAttributedString* createAttributedString(char *title, char *FG, char *B
|
||||
dictionary[NSBackgroundColorAttributeName] = colour;
|
||||
}
|
||||
}
|
||||
printf("here6\n");
|
||||
|
||||
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithUTF8String:title] attributes:dictionary];
|
||||
printf("eof\n");
|
||||
return attributedString;
|
||||
return (void*)attributedString;
|
||||
}
|
||||
|
||||
// Create an nsimage from a byte array
|
||||
|
||||
Reference in New Issue
Block a user