mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-21 11:29:29 +08:00
Bugfix dealloc contextmenus. Create common.h. WIP: new menu handling
This commit is contained in:
parent
87e974e080
commit
5f2c437136
20
v2/internal/ffenestri/common.h
Normal file
20
v2/internal/ffenestri/common.h
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
//
|
||||||
|
// Created by Lea Anthony on 6/1/21.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef COMMON_H
|
||||||
|
#define COMMON_H
|
||||||
|
|
||||||
|
#include "hashmap.h"
|
||||||
|
|
||||||
|
void ABORT(const char *message) {
|
||||||
|
printf("%s\n", message);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int freeHashmapItem(void *const context, struct hashmap_element_s *const e) {
|
||||||
|
free(e->data);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif //ASSETS_C_COMMON_H
|
@ -2,6 +2,8 @@
|
|||||||
#ifdef FFENESTRI_DARWIN
|
#ifdef FFENESTRI_DARWIN
|
||||||
|
|
||||||
#include "ffenestri_darwin.h"
|
#include "ffenestri_darwin.h"
|
||||||
|
#include "menu_darwin.h"
|
||||||
|
|
||||||
|
|
||||||
// References to assets
|
// References to assets
|
||||||
#include "assets.h"
|
#include "assets.h"
|
||||||
@ -145,6 +147,7 @@ struct Application {
|
|||||||
int windowBackgroundIsTranslucent;
|
int windowBackgroundIsTranslucent;
|
||||||
|
|
||||||
// Menu
|
// Menu
|
||||||
|
Menu *applicationMenu;
|
||||||
const char *menuAsJSON;
|
const char *menuAsJSON;
|
||||||
JsonNode *processedMenu;
|
JsonNode *processedMenu;
|
||||||
|
|
||||||
@ -759,6 +762,7 @@ void* NewApplication(const char *title, int width, int height, int resizable, in
|
|||||||
result->delegate = NULL;
|
result->delegate = NULL;
|
||||||
|
|
||||||
// Menu
|
// Menu
|
||||||
|
result->applicationMenu = NULL;
|
||||||
result->menuAsJSON = NULL;
|
result->menuAsJSON = NULL;
|
||||||
result->processedMenu = NULL;
|
result->processedMenu = NULL;
|
||||||
|
|
||||||
@ -772,6 +776,7 @@ void* NewApplication(const char *title, int width, int height, int resizable, in
|
|||||||
|
|
||||||
// Context Menus
|
// Context Menus
|
||||||
result->contextMenusAsJSON = NULL;
|
result->contextMenusAsJSON = NULL;
|
||||||
|
result->processedContextMenus = NULL;
|
||||||
contextMenuData = NULL;
|
contextMenuData = NULL;
|
||||||
|
|
||||||
// Window Appearance
|
// Window Appearance
|
||||||
@ -783,10 +788,6 @@ void* NewApplication(const char *title, int width, int height, int resizable, in
|
|||||||
return (void*) result;
|
return (void*) result;
|
||||||
}
|
}
|
||||||
|
|
||||||
int freeHashmapItem(void *const context, struct hashmap_element_s *const e) {
|
|
||||||
free(e->data);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int releaseNSObject(void *const context, struct hashmap_element_s *const e) {
|
int releaseNSObject(void *const context, struct hashmap_element_s *const e) {
|
||||||
msg(e->data, s("release"));
|
msg(e->data, s("release"));
|
||||||
@ -935,6 +936,11 @@ void DestroyApplication(struct Application *app) {
|
|||||||
// Destroy the menu
|
// Destroy the menu
|
||||||
destroyMenu(app);
|
destroyMenu(app);
|
||||||
|
|
||||||
|
// Delete the application menu if we have one
|
||||||
|
if( app->applicationMenu != NULL ) {
|
||||||
|
DeleteMenu(app->applicationMenu);
|
||||||
|
}
|
||||||
|
|
||||||
// Destroy the tray
|
// Destroy the tray
|
||||||
destroyTray(app);
|
destroyTray(app);
|
||||||
|
|
||||||
@ -1420,6 +1426,7 @@ void SetDebug(void *applicationPointer, int flag) {
|
|||||||
// SetMenu sets the initial menu for the application
|
// SetMenu sets the initial menu for the application
|
||||||
void SetMenu(struct Application *app, const char *menuAsJSON) {
|
void SetMenu(struct Application *app, const char *menuAsJSON) {
|
||||||
app->menuAsJSON = menuAsJSON;
|
app->menuAsJSON = menuAsJSON;
|
||||||
|
app->applicationMenu = NewMenu(menuAsJSON);
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetTray sets the initial tray menu for the application
|
// SetTray sets the initial tray menu for the application
|
||||||
|
111
v2/internal/ffenestri/menu_darwin.h
Normal file
111
v2/internal/ffenestri/menu_darwin.h
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
//
|
||||||
|
// Created by Lea Anthony on 6/1/21.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef MENU_DARWIN_H
|
||||||
|
#define MENU_DARWIN_H
|
||||||
|
|
||||||
|
#include "common.h"
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
|
||||||
|
/*** Internal ***/
|
||||||
|
|
||||||
|
const char *menuAsJSON;
|
||||||
|
|
||||||
|
struct hashmap_s menuItemMap;
|
||||||
|
struct hashmap_s radioGroupMap;
|
||||||
|
|
||||||
|
} Menu;
|
||||||
|
|
||||||
|
// NewMenu creates a new Menu struct, saving the given menu structure as JSON
|
||||||
|
Menu* NewMenu(const char *menuAsJSON) {
|
||||||
|
|
||||||
|
Menu *result = malloc(sizeof(Menu));
|
||||||
|
|
||||||
|
// menuAsJSON is allocated and freed by Go
|
||||||
|
result->menuAsJSON = menuAsJSON;
|
||||||
|
|
||||||
|
// Allocate MenuItem Map
|
||||||
|
if( 0 != hashmap_create((const unsigned)16, &result->menuItemMap)) {
|
||||||
|
ABORT("[NewMenu] Not enough memory to allocate menuItemMap!");
|
||||||
|
}
|
||||||
|
// Allocate the Radio Group Map
|
||||||
|
if( 0 != hashmap_create((const unsigned)4, &result->radioGroupMap)) {
|
||||||
|
ABORT("[NewMenu] Not enough memory to allocate radioGroupMap!");
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
void DeleteMenu(Menu *menu) {
|
||||||
|
|
||||||
|
// Free menu item hashmap
|
||||||
|
hashmap_destroy(&menu->menuItemMap);
|
||||||
|
|
||||||
|
// Free radio group members
|
||||||
|
if( hashmap_num_entries(&menu->radioGroupMap) > 0 ) {
|
||||||
|
if (0 != hashmap_iterate_pairs(&menu->radioGroupMap, freeHashmapItem, NULL)) {
|
||||||
|
ABORT("[DeleteMenu] Failed to release radioGroupMap entries!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Free radio groups hashmap
|
||||||
|
hashmap_destroy(&menu->radioGroupMap);
|
||||||
|
|
||||||
|
free(menu);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Create() {
|
||||||
|
|
||||||
|
//
|
||||||
|
// // Allocate the hashmaps we need
|
||||||
|
// allocateMenuHashMaps(app);
|
||||||
|
//
|
||||||
|
// // Create a new menu bar
|
||||||
|
// id menubar = createMenu(str(""));
|
||||||
|
//
|
||||||
|
// // Parse the processed menu json
|
||||||
|
// app->processedMenu = json_decode(app->menuAsJSON);
|
||||||
|
//
|
||||||
|
// if( app->processedMenu == NULL ) {
|
||||||
|
// // Parse error!
|
||||||
|
// Fatal(app, "Unable to parse Menu JSON: %s", app->menuAsJSON);
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// // Pull out the Menu
|
||||||
|
// JsonNode *menuData = json_find_member(app->processedMenu, "Menu");
|
||||||
|
// if( menuData == NULL ) {
|
||||||
|
// // Parse error!
|
||||||
|
// Fatal(app, "Unable to find Menu data: %s", app->processedMenu);
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// parseMenu(app, menubar, menuData, &menuItemMapForApplicationMenu,
|
||||||
|
// "checkboxMenuCallbackForApplicationMenu:", "radioMenuCallbackForApplicationMenu:", "menuCallbackForApplicationMenu:");
|
||||||
|
//
|
||||||
|
// // Create the radiogroup cache
|
||||||
|
// JsonNode *radioGroups = json_find_member(app->processedMenu, "RadioGroups");
|
||||||
|
// if( radioGroups == NULL ) {
|
||||||
|
// // Parse error!
|
||||||
|
// Fatal(app, "Unable to find RadioGroups data: %s", app->processedMenu);
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // Iterate radio groups
|
||||||
|
// JsonNode *radioGroup;
|
||||||
|
// json_foreach(radioGroup, radioGroups) {
|
||||||
|
// // Get item label
|
||||||
|
// processRadioGroup(radioGroup, &menuItemMapForApplicationMenu, &radioGroupMapForApplicationMenu);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // Apply the menu bar
|
||||||
|
// msg(msg(c("NSApplication"), s("sharedApplication")), s("setMainMenu:"), menubar);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif //ASSETS_C_MENU_DARWIN_H
|
Loading…
Reference in New Issue
Block a user