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)
include(CTest)
enable_testing()
add_library(tray tray.cpp)
add_library(tray src/tray.cpp src/raw_tray.c )
target_include_directories(tray
PUBLIC
include
......@@ -14,6 +14,9 @@ if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
add_executable(tray_test example/example.c)
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(
TARGET tray_test
POST_BUILD
......
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined (_WIN32) || defined (_WIN64)
......@@ -9,7 +10,7 @@
#define TRAY_APPKIT 1
#endif
#include "tray/tray.h"
#include "tray/tray_raw.h"
#if TRAY_APPINDICATOR
#define TRAY_ICON1 "indicator-messages"
......@@ -33,6 +34,14 @@ static void toggle_cb(struct tray_menu *item) {
static void hello_cb(struct tray_menu *item) {
(void)item;
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) {
tray.icon = TRAY_ICON2;
} else {
......@@ -58,7 +67,7 @@ static struct tray tray = {
.icon = TRAY_ICON1,
.menu =
(struct tray_menu[]){
{.text = "Hello", .cb = hello_cb},
{.text = "Hello", .cb = hello_cb },
{.text = "Checked", .checked = 1, .cb = toggle_cb},
{.text = "Disabled", .disabled = 1},
{.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,8 +26,8 @@ static LRESULT CALLBACK _tray_wnd_proc(HWND hwnd, UINT msg, WPARAM wparam,
POINT p;
GetCursorPos(&p);
SetForegroundWindow(hwnd);
WORD cmd = TrackPopupMenu(hmenu, TPM_LEFTALIGN | TPM_RIGHTBUTTON |
TPM_RETURNCMD | TPM_NONOTIFY,
WORD cmd = TrackPopupMenu(
hmenu, TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD | TPM_NONOTIFY,
p.x, p.y, 0, hwnd, NULL);
SendMessage(hwnd, WM_COMMAND, cmd, 0);
return 0;
......@@ -36,7 +36,8 @@ static LRESULT CALLBACK _tray_wnd_proc(HWND hwnd, UINT msg, WPARAM wparam,
case WM_COMMAND:
if (wparam >= ID_TRAY_FIRST) {
MENUITEMINFO item = {
.cbSize = sizeof(MENUITEMINFO), .fMask = MIIM_ID | MIIM_DATA,
.cbSize = sizeof(MENUITEMINFO),
.fMask = MIIM_ID | MIIM_DATA,
};
if (GetMenuItemInfo(hmenu, wparam, FALSE, &item)) {
struct tray_menu *menu = (struct tray_menu *)item.dwItemData;
......@@ -83,7 +84,7 @@ static HMENU _tray_menu(struct tray_menu *m, UINT *id) {
return hmenu;
}
static int tray_init(struct tray *tray) {
int tray_init(struct tray *tray) {
memset(&wc, 0, sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = _tray_wnd_proc;
......@@ -111,7 +112,7 @@ static int tray_init(struct tray *tray) {
return 0;
}
static int tray_loop(int blocking) {
int tray_loop(int blocking) {
MSG msg;
if (blocking) {
GetMessage(&msg, NULL, 0, 0);
......@@ -126,7 +127,7 @@ static int tray_loop(int blocking) {
return 0;
}
static void tray_update(struct tray *tray) {
void tray_update(struct tray *tray) {
HMENU prevmenu = hmenu;
UINT id = ID_TRAY_FIRST;
hmenu = _tray_menu(tray->menu, &id);
......@@ -144,7 +145,7 @@ static void tray_update(struct tray *tray) {
}
}
static void tray_exit() {
void tray_exit() {
Shell_NotifyIcon(NIM_DELETE, &nid);
if (nid.hIcon != 0) {
DestroyIcon(nid.hIcon);
......
#ifndef TRAY_H
#define TRAY_H
#if defined(__cplusplus)
extern "C" {
#endif
struct tray_menu;
struct tray {
char *icon;
struct tray_menu *menu;
#pragma once
#include <functional>
#include <memory>
#include <string>
#include <vector>
#include "tray_raw.h"
namespace trays {
class Menu {
public:
std::string text_;
bool check_able_{};
bool disabled_{};
std::function<void(Menu* self)> on_click_;
};
struct tray_menu {
char *text;
int disabled;
int checked;
void (*cb)(struct tray_menu *);
void *context;
struct tray_menu *submenu;
class Tray {
public:
Tray(std::string icon_path, std::vector<Menu> menus = {});
void run();
void add(Menu menu);
private:
std::vector<std::unique_ptr<Menu>> menus_;
std::vector<tray_menu> raw_menus_;
std::string icon_ = "icon.ico";
struct tray tray_ {};
bool init_finished_{};
};
static void tray_update(struct tray *tray);
#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 */
} // namespace trays
\ No newline at end of file
#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