Commit f6f5fc7c authored by kimidaisuki22's avatar kimidaisuki22

wrap it to cpp class.(only finished on windows.)

parent 6836f3cf
...@@ -4,7 +4,7 @@ project(tray VERSION 0.1.0 LANGUAGES C CXX) ...@@ -4,7 +4,7 @@ project(tray VERSION 0.1.0 LANGUAGES C CXX)
include(CTest) include(CTest)
enable_testing() enable_testing()
add_library(tray tray.cpp) add_library(tray src/tray.cpp src/raw_tray.c )
target_include_directories(tray target_include_directories(tray
PUBLIC PUBLIC
include include
...@@ -14,6 +14,9 @@ if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) ...@@ -14,6 +14,9 @@ if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
add_executable(tray_test example/example.c) add_executable(tray_test example/example.c)
target_link_libraries(tray_test PRIVATE tray) target_link_libraries(tray_test PRIVATE tray)
add_executable(traypp_test example/example_pp.cpp)
target_link_libraries(traypp_test PRIVATE tray)
add_custom_command( add_custom_command(
TARGET tray_test TARGET tray_test
POST_BUILD POST_BUILD
......
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#if defined (_WIN32) || defined (_WIN64) #if defined (_WIN32) || defined (_WIN64)
...@@ -9,7 +10,7 @@ ...@@ -9,7 +10,7 @@
#define TRAY_APPKIT 1 #define TRAY_APPKIT 1
#endif #endif
#include "tray/tray.h" #include "tray/tray_raw.h"
#if TRAY_APPINDICATOR #if TRAY_APPINDICATOR
#define TRAY_ICON1 "indicator-messages" #define TRAY_ICON1 "indicator-messages"
...@@ -33,6 +34,14 @@ static void toggle_cb(struct tray_menu *item) { ...@@ -33,6 +34,14 @@ static void toggle_cb(struct tray_menu *item) {
static void hello_cb(struct tray_menu *item) { static void hello_cb(struct tray_menu *item) {
(void)item; (void)item;
printf("hello cb\n"); printf("hello cb\n");
if(item->context ==NULL){
item->context = malloc(4);
memset(item->context, 0, 4);
}else{
int * number = (int*)item->context;
(*number)++;
printf("hello conut: %d\n",*number);
}
if (strcmp(tray.icon, TRAY_ICON1) == 0) { if (strcmp(tray.icon, TRAY_ICON1) == 0) {
tray.icon = TRAY_ICON2; tray.icon = TRAY_ICON2;
} else { } else {
...@@ -58,7 +67,7 @@ static struct tray tray = { ...@@ -58,7 +67,7 @@ static struct tray tray = {
.icon = TRAY_ICON1, .icon = TRAY_ICON1,
.menu = .menu =
(struct tray_menu[]){ (struct tray_menu[]){
{.text = "Hello", .cb = hello_cb}, {.text = "Hello", .cb = hello_cb },
{.text = "Checked", .checked = 1, .cb = toggle_cb}, {.text = "Checked", .checked = 1, .cb = toggle_cb},
{.text = "Disabled", .disabled = 1}, {.text = "Disabled", .disabled = 1},
{.text = "-"}, {.text = "-"},
......
#include "tray/tray.h"
#include <stdlib.h>
int main() {
trays::Menu m;
m.text_ = "Open work dir";
m.on_click_ = [](auto) { system("start ."); };
trays::Tray t{"icon.ico", {m}};
// t.add(m);
t.run();
}
\ No newline at end of file
#include "tray/tray_raw.h"
#if defined(TRAY_APPINDICATOR)
#include "linux.h"
#elif defined(TRAY_APPKIT)
#include "macos.h"
#elif defined(_WIN32)
#include "windows.h"
#else
static_assert(false, "unknow platform");
#endif
\ No newline at end of file
...@@ -26,9 +26,9 @@ static LRESULT CALLBACK _tray_wnd_proc(HWND hwnd, UINT msg, WPARAM wparam, ...@@ -26,9 +26,9 @@ static LRESULT CALLBACK _tray_wnd_proc(HWND hwnd, UINT msg, WPARAM wparam,
POINT p; POINT p;
GetCursorPos(&p); GetCursorPos(&p);
SetForegroundWindow(hwnd); SetForegroundWindow(hwnd);
WORD cmd = TrackPopupMenu(hmenu, TPM_LEFTALIGN | TPM_RIGHTBUTTON | WORD cmd = TrackPopupMenu(
TPM_RETURNCMD | TPM_NONOTIFY, hmenu, TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD | TPM_NONOTIFY,
p.x, p.y, 0, hwnd, NULL); p.x, p.y, 0, hwnd, NULL);
SendMessage(hwnd, WM_COMMAND, cmd, 0); SendMessage(hwnd, WM_COMMAND, cmd, 0);
return 0; return 0;
} }
...@@ -36,7 +36,8 @@ static LRESULT CALLBACK _tray_wnd_proc(HWND hwnd, UINT msg, WPARAM wparam, ...@@ -36,7 +36,8 @@ static LRESULT CALLBACK _tray_wnd_proc(HWND hwnd, UINT msg, WPARAM wparam,
case WM_COMMAND: case WM_COMMAND:
if (wparam >= ID_TRAY_FIRST) { if (wparam >= ID_TRAY_FIRST) {
MENUITEMINFO item = { MENUITEMINFO item = {
.cbSize = sizeof(MENUITEMINFO), .fMask = MIIM_ID | MIIM_DATA, .cbSize = sizeof(MENUITEMINFO),
.fMask = MIIM_ID | MIIM_DATA,
}; };
if (GetMenuItemInfo(hmenu, wparam, FALSE, &item)) { if (GetMenuItemInfo(hmenu, wparam, FALSE, &item)) {
struct tray_menu *menu = (struct tray_menu *)item.dwItemData; struct tray_menu *menu = (struct tray_menu *)item.dwItemData;
...@@ -83,7 +84,7 @@ static HMENU _tray_menu(struct tray_menu *m, UINT *id) { ...@@ -83,7 +84,7 @@ static HMENU _tray_menu(struct tray_menu *m, UINT *id) {
return hmenu; return hmenu;
} }
static int tray_init(struct tray *tray) { int tray_init(struct tray *tray) {
memset(&wc, 0, sizeof(wc)); memset(&wc, 0, sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX); wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = _tray_wnd_proc; wc.lpfnWndProc = _tray_wnd_proc;
...@@ -111,7 +112,7 @@ static int tray_init(struct tray *tray) { ...@@ -111,7 +112,7 @@ static int tray_init(struct tray *tray) {
return 0; return 0;
} }
static int tray_loop(int blocking) { int tray_loop(int blocking) {
MSG msg; MSG msg;
if (blocking) { if (blocking) {
GetMessage(&msg, NULL, 0, 0); GetMessage(&msg, NULL, 0, 0);
...@@ -126,7 +127,7 @@ static int tray_loop(int blocking) { ...@@ -126,7 +127,7 @@ static int tray_loop(int blocking) {
return 0; return 0;
} }
static void tray_update(struct tray *tray) { void tray_update(struct tray *tray) {
HMENU prevmenu = hmenu; HMENU prevmenu = hmenu;
UINT id = ID_TRAY_FIRST; UINT id = ID_TRAY_FIRST;
hmenu = _tray_menu(tray->menu, &id); hmenu = _tray_menu(tray->menu, &id);
...@@ -144,7 +145,7 @@ static void tray_update(struct tray *tray) { ...@@ -144,7 +145,7 @@ static void tray_update(struct tray *tray) {
} }
} }
static void tray_exit() { void tray_exit() {
Shell_NotifyIcon(NIM_DELETE, &nid); Shell_NotifyIcon(NIM_DELETE, &nid);
if (nid.hIcon != 0) { if (nid.hIcon != 0) {
DestroyIcon(nid.hIcon); DestroyIcon(nid.hIcon);
......
#ifndef TRAY_H #pragma once
#define TRAY_H #include <functional>
#if defined(__cplusplus) #include <memory>
extern "C" { #include <string>
#endif #include <vector>
struct tray_menu;
#include "tray_raw.h"
struct tray { namespace trays {
char *icon; class Menu {
struct tray_menu *menu; public:
std::string text_;
bool check_able_{};
bool disabled_{};
std::function<void(Menu* self)> on_click_;
}; };
class Tray {
struct tray_menu { public:
char *text; Tray(std::string icon_path, std::vector<Menu> menus = {});
int disabled; void run();
int checked;
void add(Menu menu);
void (*cb)(struct tray_menu *);
void *context; private:
std::vector<std::unique_ptr<Menu>> menus_;
struct tray_menu *submenu; std::vector<tray_menu> raw_menus_;
std::string icon_ = "icon.ico";
struct tray tray_ {};
bool init_finished_{};
}; };
} // namespace trays
static void tray_update(struct tray *tray); \ No newline at end of file
#if defined(TRAY_APPINDICATOR)
#include "platform/linux.h"
#elif defined(TRAY_APPKIT)
#include "platform/macos.h"
#elif defined(_WIN32)
#include "platform/windows.h"
#else
static_assert(false,"unknow platform");
#endif
#ifdef __cplusplus
}
#endif
#endif /* TRAY_H */
#ifndef TRAY_H
#define TRAY_H
#if defined(__cplusplus)
extern "C" {
#endif
struct tray_menu;
struct tray {
char *icon;
struct tray_menu *menu;
};
struct tray_menu {
char *text;
int disabled;
int checked;
void (*cb)(struct tray_menu *);
void *context;
struct tray_menu *submenu;
};
void tray_update(struct tray *tray);
int tray_init(struct tray *tray);
int tray_loop(int blocking);
void tray_update(struct tray *tray);
void tray_exit();
#ifdef __cplusplus
}
#endif
#endif /* TRAY_H */
#include "tray/platform//tray_impl.h"
// just impl functions.
\ No newline at end of file
#include "tray/tray.h"
trays::Tray::Tray(std::string icon_path, std::vector<Menu> menus)
: icon_(icon_path) {
for (auto m : menus) {
add(m);
}
Menu exit;
exit.on_click_ = [](Menu *) { tray_exit(); };
exit.text_ = "exit";
add(exit);
tray_.icon = &icon_path[0];
tray_init(&tray_);
}
void trays::Tray::run() {
while (!tray_loop(1)) {
printf("Running\n");
}
}
void trays::Tray::add(Menu menu) {
if (!raw_menus_.empty()) {
raw_menus_.pop_back();
}
auto ptr = std::make_unique<Menu>(menu);
tray_menu me{};
me.context = ptr.get();
me.checked = menu.check_able_;
me.disabled = menu.disabled_;
me.text = &ptr->text_[0];
me.cb = [](tray_menu *mem) {
auto menu_pp = reinterpret_cast<Menu *>(mem->context);
menu_pp->on_click_(menu_pp);
};
menus_.push_back(std::move(ptr));
raw_menus_.push_back(me);
raw_menus_.push_back({});
tray_.menu = raw_menus_.data();
if (init_finished_) {
tray_update(&tray_);
}
}
#include <iostream>
void say_hello(){
std::cout << "Hello, from tray!\n";
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment