Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
T
tray
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Vladislav
tray
Commits
07fd3b25
Commit
07fd3b25
authored
Jul 23, 2018
by
Serge Zaitsev
Committed by
GitHub
Jul 23, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #4 from jslegendre/master
macOS version re-written with objc-runtime
parents
3b6520f6
5e5bd660
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
92 additions
and
66 deletions
+92
-66
Makefile
Makefile
+1
-1
README.md
README.md
+1
-1
tray.h
tray.h
+90
-64
No files found.
Makefile
View file @
07fd3b25
...
@@ -5,7 +5,7 @@ else ifeq ($(shell uname -s),Linux)
...
@@ -5,7 +5,7 @@ else ifeq ($(shell uname -s),Linux)
TRAY_CFLAGS
:=
-DTRAY_APPINDICATOR
=
1
$(
shell
pkg-config
--cflags
appindicator3-0.1
)
TRAY_CFLAGS
:=
-DTRAY_APPINDICATOR
=
1
$(
shell
pkg-config
--cflags
appindicator3-0.1
)
TRAY_LDFLAGS
:=
$(
shell
pkg-config
--libs
appindicator3-0.1
)
TRAY_LDFLAGS
:=
$(
shell
pkg-config
--libs
appindicator3-0.1
)
else
ifeq
($(shell
uname
-s),Darwin)
else
ifeq
($(shell
uname
-s),Darwin)
TRAY_CFLAGS
:=
-DTRAY_APPKIT
=
1
-x
objective-c
TRAY_CFLAGS
:=
-DTRAY_APPKIT
=
1
TRAY_LDFLAGS
:=
-framework
Cocoa
TRAY_LDFLAGS
:=
-framework
Cocoa
endif
endif
...
...
README.md
View file @
07fd3b25
...
@@ -136,7 +136,7 @@ array must have text field set to NULL.
...
@@ -136,7 +136,7 @@ array must have text field set to NULL.
*
[
x
]
Checked/unchecked menu items
*
[
x
]
Checked/unchecked menu items
*
[
x
]
Nested menus
*
[
x
]
Nested menus
*
[
]
Icons for menu items
*
[
]
Icons for menu items
*
[
]
Rewrite ObjC code in C using ObjC Runtime (now ObjC code breaks many linters and static analyzers)
*
[
x
]
Rewrite ObjC code in C using ObjC Runtime (now ObjC code breaks many linters and static analyzers)
*
[
]
Call GTK code using dlopen/dlsym (to make binaries run safely if Gtk libraries are not available)
*
[
]
Call GTK code using dlopen/dlsym (to make binaries run safely if Gtk libraries are not available)
## License
## License
...
...
tray.h
View file @
07fd3b25
...
@@ -90,92 +90,118 @@ static void tray_exit() { loop_result = -1; }
...
@@ -90,92 +90,118 @@ static void tray_exit() { loop_result = -1; }
#elif defined(TRAY_APPKIT)
#elif defined(TRAY_APPKIT)
#import <Cocoa/Cocoa.h>
#include <objc/objc-runtime.h>
#include <limits.h>
static
NSAutoreleasePool
*
pool
;
static
id
app
;
static
NSStatusBar
*
statusBar
;
static
id
pool
;
static
id
statusBar
;
static
id
statusItem
;
static
id
statusItem
;
static
id
statusBarButton
;
static
id
statusBarButton
;
@interface
Tray
:
NSObject
<
NSApplicationDelegate
>
static
id
_tray_menu
(
struct
tray_menu
*
m
)
{
-
(
void
)
menuCallback
:
(
id
)
sender
;
id
menu
=
objc_msgSend
((
id
)
objc_getClass
(
"NSMenu"
),
sel_registerName
(
"new"
));
@end
objc_msgSend
(
menu
,
sel_registerName
(
"autorelease"
));
@implementation
Tray
objc_msgSend
(
menu
,
sel_registerName
(
"setAutoenablesItems:"
),
false
);
-
(
void
)
menuCallback
:(
id
)
sender
{
struct
tray_menu
*
m
=
(
struct
tray_menu
*
)[[
sender
representedObject
]
pointerValue
];
if
(
m
!=
NULL
&&
m
->
cb
!=
NULL
)
{
m
->
cb
(
m
);
}
}
@end
static
NSMenu
*
_tray_menu
(
struct
tray_menu
*
m
)
{
NSMenu
*
menu
=
[
NSMenu
new
];
[
menu
autorelease
];
[
menu
setAutoenablesItems
:
NO
];
for
(;
m
!=
NULL
&&
m
->
text
!=
NULL
;
m
++
)
{
if
(
strcmp
(
m
->
text
,
"-"
)
==
0
)
{
[
menu
addItem
:[
NSMenuItem
separatorItem
]];
}
else
{
NSMenuItem
*
menuItem
=
[
NSMenuItem
alloc
];
[
menuItem
autorelease
];
[
menuItem
initWithTitle
:[
NSString
stringWithUTF8String
:
m
->
text
]
action
:
@selector
(
menuCallback
:
)
keyEquivalent
:
@""
];
[
menuItem
setEnabled
:(
m
->
disabled
?
NO
:
YES
)];
[
menuItem
setState
:(
m
->
checked
?
NSOnState
:
NSOffState
)];
[
menuItem
setRepresentedObject
:[
NSValue
valueWithPointer
:
m
]];
[
menu
addItem
:
menuItem
];
if
(
m
->
submenu
!=
NULL
)
{
for
(;
m
!=
NULL
&&
m
->
text
!=
NULL
;
m
++
)
{
[
menu
setSubmenu
:
_tray_menu
(
m
->
submenu
)
forItem
:
menuItem
];
if
(
strcmp
(
m
->
text
,
"-"
)
==
0
)
{
objc_msgSend
(
menu
,
sel_registerName
(
"addItem:"
),
objc_msgSend
((
id
)
objc_getClass
(
"NSMenuItem"
),
sel_registerName
(
"separatorItem"
)));
}
else
{
id
menuItem
=
objc_msgSend
((
id
)
objc_getClass
(
"NSMenuItem"
),
sel_registerName
(
"alloc"
));
objc_msgSend
(
menuItem
,
sel_registerName
(
"autorelease"
));
objc_msgSend
(
menuItem
,
sel_registerName
(
"initWithTitle:action:keyEquivalent:"
),
objc_msgSend
((
id
)
objc_getClass
(
"NSString"
),
sel_registerName
(
"stringWithUTF8String:"
),
m
->
text
),
sel_registerName
(
"menuCallback:"
),
objc_msgSend
((
id
)
objc_getClass
(
"NSString"
),
sel_registerName
(
"stringWithUTF8String:"
),
""
));
objc_msgSend
(
menuItem
,
sel_registerName
(
"setEnabled:"
),
(
m
->
disabled
?
false
:
true
));
objc_msgSend
(
menuItem
,
sel_registerName
(
"setState:"
),
(
m
->
checked
?
1
:
0
));
objc_msgSend
(
menuItem
,
sel_registerName
(
"setRepresentedObject:"
),
objc_msgSend
((
id
)
objc_getClass
(
"NSValue"
),
sel_registerName
(
"valueWithPointer:"
),
m
));
objc_msgSend
(
menu
,
sel_registerName
(
"addItem:"
),
menuItem
);
if
(
m
->
submenu
!=
NULL
)
{
objc_msgSend
(
menu
,
sel_registerName
(
"setSubmenu:forItem:"
),
_tray_menu
(
m
->
submenu
),
menuItem
);
}
}
}
}
}
}
return
menu
;
return
menu
;
}
}
static
int
tray_init
(
struct
tray
*
tray
)
{
static
void
menu_callback
(
id
self
,
SEL
cmd
,
id
sender
)
{
pool
=
[
NSAutoreleasePool
new
];
struct
tray_menu
*
m
=
[
NSApplication
sharedApplication
];
(
struct
tray_menu
*
)
objc_msgSend
(
objc_msgSend
(
sender
,
sel_registerName
(
"representedObject"
)),
sel_registerName
(
"pointerValue"
));
Tray
*
trayDelegate
=
[
Tray
new
];
[
NSApp
setDelegate
:
trayDelegate
];
statusBar
=
[
NSStatusBar
systemStatusBar
];
if
(
m
!=
NULL
&&
m
->
cb
!=
NULL
)
{
statusItem
=
[
statusBar
statusItemWithLength
:
NSVariableStatusItemLength
];
m
->
cb
(
m
);
[
statusItem
retain
];
}
[
statusItem
setHighlightMode
:
YES
];
}
statusBarButton
=
[
statusItem
button
];
tray_update
(
tray
);
static
int
tray_init
(
struct
tray
*
tray
)
{
[
NSApp
activateIgnoringOtherApps
:
YES
];
pool
=
objc_msgSend
((
id
)
objc_getClass
(
"NSAutoreleasePool"
),
return
0
;
sel_registerName
(
"new"
));
objc_msgSend
((
id
)
objc_getClass
(
"NSApplication"
),
sel_registerName
(
"sharedApplication"
));
Class
trayDelegateClass
=
objc_allocateClassPair
(
objc_getClass
(
"NSObject"
),
"Tray"
,
0
);
class_addProtocol
(
trayDelegateClass
,
objc_getProtocol
(
"NSApplicationDelegate"
));
class_addMethod
(
trayDelegateClass
,
sel_registerName
(
"menuCallback:"
),
(
IMP
)
menu_callback
,
"v@:@"
);
objc_registerClassPair
(
trayDelegateClass
);
id
trayDelegate
=
objc_msgSend
((
id
)
trayDelegateClass
,
sel_registerName
(
"new"
));
app
=
objc_msgSend
((
id
)
objc_getClass
(
"NSApplication"
),
sel_registerName
(
"sharedApplication"
));
objc_msgSend
(
app
,
sel_registerName
(
"setDelegate:"
),
trayDelegate
);
statusBar
=
objc_msgSend
((
id
)
objc_getClass
(
"NSStatusBar"
),
sel_registerName
(
"systemStatusBar"
));
statusItem
=
objc_msgSend
(
statusBar
,
sel_registerName
(
"statusItemWithLength:"
),
-
1
.
0
);
objc_msgSend
(
statusItem
,
sel_registerName
(
"retain"
));
objc_msgSend
(
statusItem
,
sel_registerName
(
"setHighlightMode:"
),
true
);
statusBarButton
=
objc_msgSend
(
statusItem
,
sel_registerName
(
"button"
));
tray_update
(
tray
);
objc_msgSend
(
app
,
sel_registerName
(
"activateIgnoringOtherApps:"
),
true
);
return
0
;
}
}
static
int
tray_loop
(
int
blocking
)
{
static
int
tray_loop
(
int
blocking
)
{
NSEvent
*
event
;
id
until
=
(
blocking
?
NSDate
*
until
=
(
blocking
?
[
NSDate
distantFuture
]
:
[
NSDate
distantPast
]);
objc_msgSend
((
id
)
objc_getClass
(
"NSDate"
),
sel_registerName
(
"distantFuture"
))
:
event
=
[
NSApp
nextEventMatchingMask
:
NSAnyEventMask
objc_msgSend
((
id
)
objc_getClass
(
"NSDate"
),
sel_registerName
(
"distantPast"
)));
untilDate
:
until
inMode
:
NSDefaultRunLoopMode
id
event
=
objc_msgSend
(
app
,
sel_registerName
(
"nextEventMatchingMask:untilDate:inMode:dequeue:"
),
dequeue
:
YES
];
ULONG_MAX
,
if
(
event
)
{
until
,
[
NSApp
sendEvent
:
event
];
objc_msgSend
((
id
)
objc_getClass
(
"NSString"
),
}
sel_registerName
(
"stringWithUTF8String:"
),
return
0
;
"kCFRunLoopDefaultMode"
),
true
);
if
(
event
)
{
objc_msgSend
(
app
,
sel_registerName
(
"sendEvent:"
),
event
);
}
return
0
;
}
}
static
void
tray_update
(
struct
tray
*
tray
)
{
static
void
tray_update
(
struct
tray
*
tray
)
{
[
statusBarButton
objc_msgSend
(
statusBarButton
,
sel_registerName
(
"setImage:"
),
setImage
:[
NSImage
imageNamed
:[
NSString
stringWithUTF8String
:
tray
->
icon
]]];
objc_msgSend
((
id
)
objc_getClass
(
"NSImage"
),
sel_registerName
(
"imageNamed:"
),
objc_msgSend
((
id
)
objc_getClass
(
"NSString"
),
sel_registerName
(
"stringWithUTF8String:"
),
tray
->
icon
)));
[
statusItem
setMenu
:
_tray_menu
(
tray
->
menu
)]
;
objc_msgSend
(
statusItem
,
sel_registerName
(
"setMenu:"
),
_tray_menu
(
tray
->
menu
))
;
}
}
static
void
tray_exit
()
{
[
NSApp
terminate
:
NSApp
]
;
}
static
void
tray_exit
()
{
objc_msgSend
(
app
,
sel_registerName
(
"terminate:"
),
app
)
;
}
#elif defined(TRAY_WINAPI)
#elif defined(TRAY_WINAPI)
#include <windows.h>
#include <windows.h>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment