Skip to content
Merged
87 changes: 21 additions & 66 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,61 +37,18 @@ module main

import ouri028.vtray

enum MenuItems {
edit = 1
copy = 2
quit = 3
}

fn main() {
icon := $if macos {
'${@VMODROOT}/assets/icon.png'
} $else {
'${@VMODROOT}/assets/icon.ico'
}
mut systray := &vtray.VTrayApp{
identifier: 'VTray!'
tooltip: 'VTray Demo!'
icon: icon
items: [
&vtray.VTrayMenuItem{
id: int(MenuItems.edit)
text: 'Edit'
checkable: true
},
&vtray.VTrayMenuItem{
id: int(MenuItems.copy)
text: 'Copy'
disabled: true
},
&vtray.VTrayMenuItem{
id: int(MenuItems.quit)
text: 'Quit'
},
]
}
on_click := fn [systray] (mut menu_item vtray.VTrayMenuItem) {
println(menu_item)
if menu_item.id == int(MenuItems.quit) {
systray.destroy()
}
}
systray.on_click = on_click
systray.vtray_init()
systray.run()
systray.destroy()
}
```

Edit v.mod

```v
Module {
name: 'myapp'
description: ''
version: '0.0.1'
license: 'MIT'
dependencies: ['ouri028.vtray']
mut tray := vtray.create(icon, tooltip: 'VTray Demo!')
tray.add_item('Edit', checkable: true)
tray.add_item('Copy', disabled: true)
tray.add_item('Quit', on_click: tray.destroy)
tray.run()
tray.destroy()
}
```

Expand All @@ -113,32 +70,30 @@ Module {

![image5.png](assets%2Fimage5.png)

## Struct Definitions
## Definitions

```v
module vtray

struct VTrayMenuItem {
struct MenuItem {
pub mut:
id int
text string
checked bool
checkable bool
disabled bool
text string
checked bool
checkable bool
disabled bool
}
struct VTrayApp {
struct Tray {
mut:
tray &VTray = unsafe { nil }
tray &VTray = unsafe { nil }
pub mut:
identifier string
tooltip string
icon string
items []&VTrayMenuItem
on_click fn (menu_item &VTrayMenuItem) = unsafe { nil }
identifier string
tooltip string
icon string
items []&MenuItem
}
fn (mut v VTrayApp) vtray_init()
fn (v &VTrayApp) run()
fn (v &VTrayApp) destroy()
fn (mut v Tray) vtray_init()
fn (v &Tray) run()
fn (v &Tray) destroy()
```

## License
Expand Down
14 changes: 7 additions & 7 deletions c/linux/tray.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ void *GLOBAL_TRAY = NULL;

static void on_menu_item_clicked(GtkMenuItem *menu_item, gpointer user_data)
{
struct VTrayMenuItem *item = (struct VTrayMenuItem *)user_data;
struct MenuItem *item = (struct MenuItem *)user_data;
struct VTray *tray = (struct VTray *)get_global_vtray();
if (tray != NULL)
{
Expand All @@ -19,7 +19,7 @@ static void on_menu_item_clicked(GtkMenuItem *menu_item, gpointer user_data)

static void on_toggle_menu_item_clicked(GtkCheckMenuItem *menu_item, gpointer user_data)
{
struct VTrayMenuItem *item = (struct VTrayMenuItem *)user_data;
struct MenuItem *item = (struct MenuItem *)user_data;
struct VTray *tray = (struct VTray *)get_global_vtray();
if (tray != NULL)
{
Expand All @@ -31,7 +31,7 @@ static void on_toggle_menu_item_clicked(GtkCheckMenuItem *menu_item, gpointer us
}
}

struct VTray *vtray_init(VTrayParams *params, size_t num_items, struct VTrayMenuItem *items[])
struct VTray *vtray_init(VTrayParams *params, size_t num_items, struct MenuItem *items[])
{
struct VTray *tray = (struct VTray *)malloc(sizeof(struct VTray));
if (!tray)
Expand Down Expand Up @@ -79,7 +79,7 @@ void vtray_construct(struct VTray *parent)

for (size_t i = 0; i < parent->num_items; i++)
{
struct VTrayMenuItem *item = parent->items[i];
struct MenuItem *item = parent->items[i];
GtkMenuItem *menu_item;

if (item->checkable)
Expand Down Expand Up @@ -114,7 +114,7 @@ void vtray_construct(struct VTray *parent)

void vtray_update_menu_item(struct VTray *tray, int menu_id)
{
VTrayMenuItem *item = get_vmenu_item_by_id(menu_id, tray);
MenuItem *item = get_vmenu_item_by_id(menu_id, tray);
GtkMenuItem *menu_item = get_menu_item_by_label(tray, string_to_char(item->text));
if (item == NULL)
{
Expand Down Expand Up @@ -154,7 +154,7 @@ GtkMenuItem *get_menu_item_by_label(struct VTray *tray, char *label)
return NULL;
}

VTrayMenuItem *get_vmenu_item_by_id(int menu_id, struct VTray *tray)
MenuItem *get_vmenu_item_by_id(int menu_id, struct VTray *tray)
{
for (size_t i = 0; i < tray->num_items; i++)
{
Expand All @@ -163,7 +163,7 @@ VTrayMenuItem *get_vmenu_item_by_id(int menu_id, struct VTray *tray)
return tray->items[i];
}
}
return (VTrayMenuItem *){0};
return (MenuItem *){0};
}

void vtray_exit(struct VTray *tray)
Expand Down
12 changes: 6 additions & 6 deletions c/linux/tray.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
#include "utils.h"

typedef struct VTrayParams VTrayParams;
typedef struct VTrayMenuItem VTrayMenuItem;
typedef void (*CallbackFunction)(VTrayMenuItem *menu_item);
typedef struct MenuItem MenuItem;
typedef void (*CallbackFunction)(MenuItem *menu_item);

struct VTray
{
AppIndicator *indicator;
GtkWidget *menu;
CallbackFunction on_click;
struct VTrayMenuItem **items;
struct MenuItem **items;
size_t num_items;
size_t num_menus;
GtkMenuItem **menus;
Expand All @@ -30,7 +30,7 @@ struct VTrayParams
CallbackFunction on_click;
};

struct VTrayMenuItem
struct MenuItem
{
int id;
String text;
Expand All @@ -39,7 +39,7 @@ struct VTrayMenuItem
bool disabled;
};

struct VTray *vtray_init(VTrayParams *params, size_t num_items, struct VTrayMenuItem *items[]);
struct VTray *vtray_init(VTrayParams *params, size_t num_items, struct MenuItem *items[]);

void vtray_run(struct VTray *tray);

Expand All @@ -53,7 +53,7 @@ void vtray_update_menu_item(struct VTray *tray, int menu_id);

GtkMenuItem *get_menu_item_by_label(struct VTray *tray, char *label);

VTrayMenuItem *get_vmenu_item_by_id(int menu_id, struct VTray *tray);
MenuItem *get_vmenu_item_by_id(int menu_id, struct VTray *tray);

void set_global_vtray(void *ptr);

Expand Down
10 changes: 5 additions & 5 deletions c/macos/tray.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ @interface AppDelegate : NSObject <NSApplicationDelegate> {
NSStatusItem *statusItem; // our button
int num_items;
vtray__VTrayParams *trayParams; // VTrayParams is defined in tray.v
vtray__VTrayMenuItem **menuItems;
vtray__MenuItem **menuItems;
}
@end

@implementation AppDelegate
- (AppDelegate *)initWithParams:(vtray__VTrayParams *)params
itemsArray:(vtray__VTrayMenuItem *[])itemsArray
itemsArray:(vtray__MenuItem *[])itemsArray
numItems:(int)numItems {
if (self = [super init]) {
trayParams = params;
Expand All @@ -69,8 +69,8 @@ - (AppDelegate *)initWithParams:(vtray__VTrayParams *)params
// Called when NSMenuItem is clicked.
- (void)onAction:(id)sender {
NSMenuItem *menuItem = (NSMenuItem *)sender;
struct vtray__VTrayMenuItem *item =
(struct vtray__VTrayMenuItem *)[[sender representedObject] pointerValue];
struct vtray__MenuItem *item =
(struct vtray__MenuItem *)[[sender representedObject] pointerValue];

if (item && item->checkable) {
item->checked = !item->checked; // Toggle the checked state
Expand Down Expand Up @@ -144,7 +144,7 @@ - (NSApplicationTerminateReply)applicationShouldTerminate:
@end

// Initializes NSApplication and NSStatusItem, aka system tray menu item.
vtray__VTray *vtray_init(vtray__VTrayParams *params, int numItems, vtray__VTrayMenuItem *itemsArray[]) {
vtray__VTray *vtray_init(vtray__VTrayParams *params, int numItems, vtray__MenuItem *itemsArray[]) {
NSApplication *app = [NSApplication sharedApplication];
AppDelegate *appDelegate = [[AppDelegate alloc] initWithParams:params itemsArray:itemsArray numItems:numItems];

Expand Down
10 changes: 5 additions & 5 deletions c/windows/tray.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ LRESULT CALLBACK vtray_wndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam
return 0;
}

struct VTray *vtray_init(VTrayParams *params, size_t num_items, struct VTrayMenuItem *items[])
struct VTray *vtray_init(VTrayParams *params, size_t num_items, struct MenuItem *items[])
{
struct VTray *tray = (struct VTray *)malloc(sizeof(struct VTray));
if (!tray)
Expand Down Expand Up @@ -145,7 +145,7 @@ void vtray_construct(struct VTray *parent)
{
for (size_t i = 0; i < parent->num_items; i++)
{
struct VTrayMenuItem *item = parent->items[i];
struct MenuItem *item = parent->items[i];
MENUITEMINFO menuItem;
memset(&menuItem, 0, sizeof(MENUITEMINFO));
menuItem.cbSize = sizeof(MENUITEMINFO);
Expand Down Expand Up @@ -201,7 +201,7 @@ void vtray_update_menu_item(struct VTray *tray, int menu_id, bool checked)
return;
}
menuItemInfo.fMask = MIIM_STATE;
VTrayMenuItem *item = get_vmenu_item_by_id(menu_id, tray);
MenuItem *item = get_vmenu_item_by_id(menu_id, tray);
if (item == NULL)
{
fprintf(stderr, "Failed to find menu item with ID %d\n", menu_id);
Expand Down Expand Up @@ -240,7 +240,7 @@ MENUITEMINFO get_menu_item_by_id(HMENU menu, UINT menu_id)
return menuItemInfo;
}

VTrayMenuItem *get_vmenu_item_by_id(int menu_id, struct VTray *tray)
MenuItem *get_vmenu_item_by_id(int menu_id, struct VTray *tray)
{
for (size_t i = 0; i < tray->num_items; i++)
{
Expand All @@ -249,7 +249,7 @@ VTrayMenuItem *get_vmenu_item_by_id(int menu_id, struct VTray *tray)
return tray->items[i];
}
}
return (VTrayMenuItem *){0};
return (MenuItem *){0};
}

void vtray_run(struct VTray *tray)
Expand Down
12 changes: 6 additions & 6 deletions c/windows/tray.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
#define ID_TRAYICON 100

typedef struct VTrayParams VTrayParams;
typedef struct VTrayMenuItem VTrayMenuItem;
typedef struct MenuItem MenuItem;

typedef void (*CallbackFunction)(VTrayMenuItem *menu_item);
typedef void (*CallbackFunction)(MenuItem *menu_item);

struct VTrayParams
{
Expand All @@ -24,7 +24,7 @@ struct VTrayParams
CallbackFunction on_click;
};

struct VTrayMenuItem
struct MenuItem
{
int id;
String text;
Expand All @@ -43,11 +43,11 @@ struct VTray
HWND hwnd;
wchar_t *tooltip;
CallbackFunction on_click;
struct VTrayMenuItem **items;
struct MenuItem **items;
size_t num_items;
};

struct VTray *vtray_init(VTrayParams *params, size_t num_items, struct VTrayMenuItem *items[]);
struct VTray *vtray_init(VTrayParams *params, size_t num_items, struct MenuItem *items[]);

void vtray_exit(struct VTray *tray);

Expand All @@ -63,7 +63,7 @@ BOOL is_menu_item_checked(HMENU menu, UINT menuId);

MENUITEMINFO get_menu_item_by_id(HMENU menu, UINT menu_id);

VTrayMenuItem *get_vmenu_item_by_id(int menu_id, struct VTray *tray);
MenuItem *get_vmenu_item_by_id(int menu_id, struct VTray *tray);

void vtray_run(struct VTray *tray);

Expand Down
Loading