[WIP] Add tray menu store + refactor

This commit is contained in:
Lea Anthony
2021-01-12 16:55:30 +11:00
parent db6dde3e50
commit 1a7507f524
8 changed files with 168 additions and 61 deletions

View File

@@ -0,0 +1,65 @@
//
// Created by Lea Anthony on 6/1/21.
//
#include "common.h"
// Credit: https://stackoverflow.com/a/8465083
char* concat(const char *string1, const char *string2)
{
const size_t len1 = strlen(string1);
const size_t len2 = strlen(string2);
char *result = malloc(len1 + len2 + 1);
strcpy(result, string1);
memcpy(result + len1, string2, len2 + 1);
return result;
}
// 10k is more than enough for a log message
#define MAXMESSAGE 1024*10
char abortbuffer[MAXMESSAGE];
void ABORT(const char *message, ...) {
const char *temp = concat("FATAL: ", message);
va_list args;
va_start(args, message);
vsnprintf(abortbuffer, MAXMESSAGE, temp, args);
printf("%s\n", &abortbuffer[0]);
MEMFREE(temp);
va_end(args);
exit(1);
}
int freeHashmapItem(void *const context, struct hashmap_element_s *const e) {
free(e->data);
return -1;
}
const char* getJSONString(JsonNode *item, const char* key) {
// Get key
JsonNode *node = json_find_member(item, key);
const char *result = "";
if ( node != NULL && node->tag == JSON_STRING) {
result = node->string_;
}
return result;
}
bool getJSONBool(JsonNode *item, const char* key, bool *result) {
JsonNode *node = json_find_member(item, key);
if ( node != NULL && node->tag == JSON_BOOL) {
*result = node->bool_;
return true;
}
return false;
}
bool getJSONInt(JsonNode *item, const char* key, int *result) {
JsonNode *node = json_find_member(item, key);
if ( node != NULL && node->tag == JSON_NUMBER) {
*result = (int) node->number_;
return true;
}
return false;
}

View File

@@ -5,8 +5,16 @@
#ifndef COMMON_H
#define COMMON_H
#define OBJC_OLD_DISPATCH_PROTOTYPES 1
#include <objc/objc-runtime.h>
#include <CoreGraphics/CoreGraphics.h>
#include <stdio.h>
#include <stdarg.h>
#include "string.h"
#include "hashmap.h"
#include "vec.h"
#include "json.h"
#define STREQ(a,b) strcmp(a, b) == 0
#define STRCOPY(a) concat(a, "")
@@ -15,63 +23,11 @@
#define FREE_AND_SET(variable, value) if( variable != NULL ) { MEMFREE(variable); } variable = value
// Credit: https://stackoverflow.com/a/8465083
char* concat(const char *string1, const char *string2)
{
const size_t len1 = strlen(string1);
const size_t len2 = strlen(string2);
char *result = malloc(len1 + len2 + 1);
strcpy(result, string1);
memcpy(result + len1, string2, len2 + 1);
return result;
}
// 10k is more than enough for a log message
#define MAXMESSAGE 1024*10
char logbuffer[MAXMESSAGE];
void ABORT(const char *message, ...) {
const char *temp = concat("FATAL: ", message);
va_list args;
va_start(args, message);
vsnprintf(logbuffer, MAXMESSAGE, temp, args);
printf("%s\n", &logbuffer[0]);
MEMFREE(temp);
va_end(args);
exit(1);
}
int freeHashmapItem(void *const context, struct hashmap_element_s *const e) {
free(e->data);
return -1;
}
const char* getJSONString(JsonNode *item, const char* key) {
// Get key
JsonNode *node = json_find_member(item, key);
const char *result = "";
if ( node != NULL && node->tag == JSON_STRING) {
result = node->string_;
}
return result;
}
bool getJSONBool(JsonNode *item, const char* key, bool *result) {
JsonNode *node = json_find_member(item, key);
if ( node != NULL && node->tag == JSON_BOOL) {
*result = node->bool_;
return true;
}
return false;
}
bool getJSONInt(JsonNode *item, const char* key, int *result) {
JsonNode *node = json_find_member(item, key);
if ( node != NULL && node->tag == JSON_NUMBER) {
*result = (int) node->number_;
return true;
}
return false;
}
char* concat(const char *string1, const char *string2);
void ABORT(const char *message, ...);
int freeHashmapItem(void *const context, struct hashmap_element_s *const e);
const char* getJSONString(JsonNode *item, const char* key);
bool getJSONBool(JsonNode *item, const char* key, bool *result);
bool getJSONInt(JsonNode *item, const char* key, int *result);
#endif //ASSETS_C_COMMON_H

View File

@@ -38,5 +38,6 @@ extern void UpdateTray(void *app, char *menuAsJSON);
extern void UpdateContextMenus(void *app, char *contextMenusAsJSON);
extern void UpdateTrayLabel(void *app, const char *label);
extern void UpdateTrayIcon(void *app, const char *label);
extern void AddTrayMenu(void *app, const char *trayAsJSON);
#endif

View File

@@ -4,6 +4,7 @@
#include "ffenestri_darwin.h"
#include "menu_darwin.h"
#include "contextmenus_darwin.h"
#include "traymenustore_darwin.h"
// References to assets
@@ -124,6 +125,8 @@ struct Application {
Menu *applicationMenu;
// Tray
TrayMenuStore* trayMenuStore;
const char *trayMenuAsJSON;
const char *trayLabel;
const char *trayIconName;
@@ -147,6 +150,8 @@ struct Application {
// Debug works like sprintf but mutes if the global debug flag is true
// Credit: https://stackoverflow.com/a/20639708
#define MAXMESSAGE 1024*10
char logbuffer[MAXMESSAGE];
void Debug(struct Application *app, const char *message, ... ) {
if ( debug ) {
@@ -578,6 +583,8 @@ void DestroyApplication(struct Application *app) {
// Destroy the tray
destroyTray(app);
DeleteTrayMenuStore(app->trayMenuStore);
// Destroy the context menus
destroyContextMenus(app);
@@ -1073,6 +1080,11 @@ void SetContextMenus(struct Application *app, const char *contextMenusAsJSON) {
app->contextMenuStore = NewContextMenuStore(contextMenusAsJSON);
}
void AddTrayMenu(struct Application *app, const char *trayJSON) {
AddTrayMenuToStore(app->trayMenuStore, trayJSON);
}
void SetBindings(struct Application *app, const char *bindings) {
const char* temp = concat("window.wailsbindings = \"", bindings);
const char* jscall = concat(temp, "\";");
@@ -1666,6 +1678,7 @@ void processTrayIconData(struct Application *app) {
}
}
void parseTrayData(struct Application *app) {
// Allocate the hashmaps we need
@@ -2050,6 +2063,7 @@ void* NewApplication(const char *title, int width, int height, int resizable, in
result->applicationMenu = NULL;
// Tray
result->trayMenuStore = NewTrayMenuStore();
result->trayMenuAsJSON = NULL;
result->trayLabel = NULL;
result->trayIconName = NULL;

View File

@@ -19,12 +19,12 @@ extern void WebviewIsTransparent(void *);
extern void WindowBackgroundIsTranslucent(void *);
extern void SetTray(void *, const char *, const char *, const char *);
extern void SetContextMenus(void *, const char *);
extern void AddTrayMenu(void *, const char *);
*/
import "C"
import (
"github.com/wailsapp/wails/v2/pkg/options"
"os"
)
func (a *Application) processPlatformSettings() error {
@@ -90,10 +90,9 @@ func (a *Application) processPlatformSettings() error {
if trays != nil {
for _, tray := range trays {
println("Adding tray menu: " + tray)
//C.AddTray(a.app, a.string2CString(tray))
C.AddTrayMenu(a.app, a.string2CString(tray))
}
}
os.Exit(1)
// Process context menus
contextMenus := options.GetContextMenus(a.config)

View File

@@ -0,0 +1,20 @@
//
// Created by Lea Anthony on 12/1/21.
//
#ifndef TRAYMENU_DARWIN_H
#define TRAYMENU_DARWIN_H
#include "menu_darwin.h"
typedef struct {
const char *label;
const char *icon;
const char *trayID;
Menu* menu;
} TrayMenu;
#endif //TRAYMENU_DARWIN_H

View File

@@ -0,0 +1,28 @@
//
// Created by Lea Anthony on 12/1/21.
//
#include "common.h"
#include "traymenustore_darwin.h"
TrayMenuStore* NewTrayMenuStore() {
TrayMenuStore* result = malloc(sizeof(TrayMenuStore));
// Allocate Context Menu Store
if( 0 != hashmap_create((const unsigned)4, &result->trayMenuMap)) {
ABORT("[NewTrayMenuStore] Not enough memory to allocate trayMenuMap!");
}
return result;
}
void AddTrayMenuToStore(TrayMenuStore* store, const char* menuJSON) {
}
void DeleteTrayMenuStore(TrayMenuStore *trayMenuStore) {
// Destroy tray menu map
hashmap_destroy(&trayMenuStore->trayMenuMap);
}

View File

@@ -0,0 +1,24 @@
//
// Created by Lea Anthony on 7/1/21.
//
#ifndef TRAYMENUSTORE_DARWIN_H
#define TRAYMENUSTORE_DARWIN_H
typedef struct {
int dummy;
// This is our tray menu map
// It maps tray IDs to TrayMenu*
struct hashmap_s trayMenuMap;
} TrayMenuStore;
TrayMenuStore* NewTrayMenuStore();
void AddTrayMenuToStore(TrayMenuStore* store, const char* menuJSON);
void DeleteTrayMenuStore(TrayMenuStore* store);
#endif //TRAYMENUSTORE_DARWIN_H