Initial implementation of JNA for the native macosx system tray
This commit is contained in:
parent
b861cf6314
commit
c45225fc54
26
src/dorkbox/util/jna/macos/cocoa/NSCellStateValue.java
Executable file
26
src/dorkbox/util/jna/macos/cocoa/NSCellStateValue.java
Executable file
@ -0,0 +1,26 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2018 dorkbox, llc
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package dorkbox.util.jna.macos.cocoa;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* https://developer.apple.com/documentation/appkit/nscell?language=objc
|
||||||
|
*/
|
||||||
|
public
|
||||||
|
interface NSCellStateValue {
|
||||||
|
int NSMixedState = -1;
|
||||||
|
int NSOffState = 0;
|
||||||
|
int NSOnState = 1;
|
||||||
|
}
|
36
src/dorkbox/util/jna/macos/cocoa/NSData.java
Executable file
36
src/dorkbox/util/jna/macos/cocoa/NSData.java
Executable file
@ -0,0 +1,36 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2018 dorkbox, llc
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package dorkbox.util.jna.macos.cocoa;
|
||||||
|
|
||||||
|
import com.sun.jna.Pointer;
|
||||||
|
|
||||||
|
import dorkbox.util.jna.macos.foundation.ObjectiveC;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* https://developer.apple.com/documentation/foundation/nsdata?language=objc
|
||||||
|
*/
|
||||||
|
public
|
||||||
|
class NSData extends NSObject {
|
||||||
|
|
||||||
|
private static final Pointer objectClass = ObjectiveC.objc_lookUpClass("NSData");
|
||||||
|
|
||||||
|
private static final Pointer dataWithBytesLength = ObjectiveC.sel_getUid("dataWithBytes:length:");
|
||||||
|
|
||||||
|
public
|
||||||
|
NSData(byte[] data) {
|
||||||
|
super(ObjectiveC.objc_msgSend(objectClass, dataWithBytesLength, data, data.length));
|
||||||
|
}
|
||||||
|
}
|
60
src/dorkbox/util/jna/macos/cocoa/NSImage.java
Executable file
60
src/dorkbox/util/jna/macos/cocoa/NSImage.java
Executable file
@ -0,0 +1,60 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2018 dorkbox, llc
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package dorkbox.util.jna.macos.cocoa;
|
||||||
|
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import com.sun.jna.Pointer;
|
||||||
|
|
||||||
|
import dorkbox.util.ImageUtil;
|
||||||
|
import dorkbox.util.jna.macos.foundation.ObjectiveC;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* https://developer.apple.com/documentation/appkit/nsimage?language=objc
|
||||||
|
*/
|
||||||
|
public
|
||||||
|
class NSImage extends NSObject {
|
||||||
|
|
||||||
|
private static final Pointer objectClass = ObjectiveC.objc_lookUpClass("NSImage");
|
||||||
|
|
||||||
|
private static final Pointer initWithContentsOfFile = ObjectiveC.sel_getUid("initWithContentsOfFile:");
|
||||||
|
private static final Pointer initWithData = ObjectiveC.sel_getUid("initWithData:");
|
||||||
|
|
||||||
|
public
|
||||||
|
NSImage(NSData data) {
|
||||||
|
super(ObjectiveC.class_createInstance(objectClass, 0));
|
||||||
|
ObjectiveC.objc_msgSend(this, initWithData, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public
|
||||||
|
NSImage(BufferedImage image) throws IOException {
|
||||||
|
this(ImageUtil.toBytes(image));
|
||||||
|
}
|
||||||
|
|
||||||
|
public
|
||||||
|
NSImage(byte[] imageBytes) {
|
||||||
|
this(new NSData(imageBytes));
|
||||||
|
}
|
||||||
|
|
||||||
|
public
|
||||||
|
NSImage(final File file) {
|
||||||
|
super(ObjectiveC.class_createInstance(objectClass, 0));
|
||||||
|
ObjectiveC.objc_msgSend(this, initWithContentsOfFile, new NSString(file.getAbsolutePath()));
|
||||||
|
}
|
||||||
|
}
|
34
src/dorkbox/util/jna/macos/cocoa/NSInteger.java
Normal file
34
src/dorkbox/util/jna/macos/cocoa/NSInteger.java
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2018 dorkbox, llc
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package dorkbox.util.jna.macos.cocoa;
|
||||||
|
|
||||||
|
import com.sun.jna.NativeLong;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* https://developer.apple.com/documentation/foundation/nsnumber?language=objc
|
||||||
|
*/
|
||||||
|
public
|
||||||
|
class NSInteger extends NativeLong {
|
||||||
|
|
||||||
|
public
|
||||||
|
NSInteger() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public
|
||||||
|
NSInteger(long value) {
|
||||||
|
super(value);
|
||||||
|
}
|
||||||
|
}
|
114
src/dorkbox/util/jna/macos/cocoa/NSMenu.java
Executable file
114
src/dorkbox/util/jna/macos/cocoa/NSMenu.java
Executable file
@ -0,0 +1,114 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2018 dorkbox, llc
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package dorkbox.util.jna.macos.cocoa;
|
||||||
|
|
||||||
|
import com.sun.jna.Pointer;
|
||||||
|
|
||||||
|
import dorkbox.util.jna.macos.foundation.ObjectiveC;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* https://developer.apple.com/documentation/appkit/nsmenu?language=objc
|
||||||
|
*/
|
||||||
|
public
|
||||||
|
class NSMenu extends NSObject {
|
||||||
|
|
||||||
|
private static final Pointer objectClass = ObjectiveC.objc_lookUpClass("NSMenu");
|
||||||
|
|
||||||
|
private static final Pointer addItem = ObjectiveC.sel_getUid("addItem:");
|
||||||
|
private static final Pointer removeItem = ObjectiveC.sel_getUid("removeItem:");
|
||||||
|
private static final Pointer setAutoenablesItems = ObjectiveC.sel_getUid("setAutoenablesItems:");
|
||||||
|
private static final Pointer itemAtIndex = ObjectiveC.sel_getUid("itemAtIndex:");
|
||||||
|
private static final Pointer insertItemAtIndex = ObjectiveC.sel_getUid("insertItem:atIndex:");
|
||||||
|
|
||||||
|
public
|
||||||
|
NSMenu() {
|
||||||
|
super(ObjectiveC.class_createInstance(objectClass, 0));
|
||||||
|
|
||||||
|
// make this part of the constructor. By default, we want to have items automatically enabled
|
||||||
|
setAutoEnablesItems(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a menu item to the end of the menu.
|
||||||
|
* <p>
|
||||||
|
* NOTE: This method invokes insertItem(_:at:). Thus, the menu does not accept the
|
||||||
|
* menu item if it already belongs to another menu. After adding the menu item, the menu updates itself.
|
||||||
|
*
|
||||||
|
* @param newItem The menu item (an object conforming to the NSMenuItem protocol) to add to the menu.
|
||||||
|
*/
|
||||||
|
public
|
||||||
|
void addItem(NSMenuItem newItem) {
|
||||||
|
ObjectiveC.objc_msgSend(this, addItem, newItem);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inserts a menu item into the menu at a specific location.
|
||||||
|
* <p>
|
||||||
|
* NOTE: This method posts an NSMenuDidAddItemNotification, allowing
|
||||||
|
* interested observers to update as appropriate. This method is a primitive
|
||||||
|
* method. All item-addition methods end up calling this method, so this is
|
||||||
|
* where you should implement custom behavior on adding new items to a menu
|
||||||
|
* in a custom subclass. If the menu item already exists in another menu, it
|
||||||
|
* is not inserted and the method raises an exception of type
|
||||||
|
* NSInternalInconsistencyException.
|
||||||
|
*
|
||||||
|
* @param newItem An object conforming to the NSMenuItem protocol that represents a menu item.
|
||||||
|
* @param index An integer index identifying the location of the menu item in the menu.
|
||||||
|
*/
|
||||||
|
public
|
||||||
|
void insertItemAtIndex(NSMenuItem newItem, int index) {
|
||||||
|
ObjectiveC.objc_msgSend(this, insertItemAtIndex, newItem, index);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the menu item at a specific location of the menu.
|
||||||
|
*
|
||||||
|
* @param index An integer index locating a menu item in a menu.
|
||||||
|
*
|
||||||
|
* @return The found menu item (an object conforming to the NSMenuItem protocol) or nil if the object couldn't be found.
|
||||||
|
*/
|
||||||
|
public
|
||||||
|
NSMenuItem itemAtIndex(int index) {
|
||||||
|
return new NSMenuItem(ObjectiveC.objc_msgSend(this, itemAtIndex, index));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Indicates whether the menu automatically enables and disables its menu items.
|
||||||
|
* <p>
|
||||||
|
* This property contains a Boolean value, indicating whether the menu automatically
|
||||||
|
* enables and disables its menu items. If set to true, menu items of the menu are automatically
|
||||||
|
* enabled and disabled according to rules computed by the NSMenuValidation informal protocol.
|
||||||
|
* By default, NSMenu objects autoenable their menu items.
|
||||||
|
*
|
||||||
|
* @param enable true to enable the items by default, false to disable items by default
|
||||||
|
*/
|
||||||
|
public
|
||||||
|
void setAutoEnablesItems(boolean enable) {
|
||||||
|
// WEIRDLY enough, the logic is backwards...
|
||||||
|
ObjectiveC.objc_msgSend(this, setAutoenablesItems, !enable);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes a menu item from the menu.
|
||||||
|
*
|
||||||
|
* @param anItem The menu item to remove.
|
||||||
|
*/
|
||||||
|
public
|
||||||
|
void removeItem(final NSMenuItem anItem) {
|
||||||
|
ObjectiveC.objc_msgSend(this, removeItem, anItem);
|
||||||
|
}
|
||||||
|
}
|
163
src/dorkbox/util/jna/macos/cocoa/NSMenuItem.java
Executable file
163
src/dorkbox/util/jna/macos/cocoa/NSMenuItem.java
Executable file
@ -0,0 +1,163 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2018 dorkbox, llc
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package dorkbox.util.jna.macos.cocoa;
|
||||||
|
|
||||||
|
import com.sun.jna.Pointer;
|
||||||
|
|
||||||
|
import dorkbox.util.jna.macos.foundation.ObjectiveC;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* https://developer.apple.com/documentation/appkit/nsmenuitem?language=objc
|
||||||
|
*/
|
||||||
|
public
|
||||||
|
class NSMenuItem extends NSObject {
|
||||||
|
|
||||||
|
private static final Pointer objectClass = ObjectiveC.objc_lookUpClass("NSMenuItem");
|
||||||
|
|
||||||
|
private static final Pointer setTitle = ObjectiveC.sel_getUid("setTitle:");
|
||||||
|
private static final Pointer toolTip = ObjectiveC.sel_getUid("toolTip");
|
||||||
|
private static final Pointer setKeyEquivalent = ObjectiveC.sel_getUid("setKeyEquivalent:");
|
||||||
|
|
||||||
|
private static final Pointer setIndentationLevel = ObjectiveC.sel_getUid("setIndentationLevel:");
|
||||||
|
|
||||||
|
private static final Pointer setImage = ObjectiveC.sel_getUid("setImage:");
|
||||||
|
private static final Pointer setOnStateImage = ObjectiveC.sel_getUid("setOnStateImage:");
|
||||||
|
private static final Pointer setEnabled = ObjectiveC.sel_getUid("setEnabled:");
|
||||||
|
private static final Pointer separatorItem = ObjectiveC.sel_getUid("separatorItem");
|
||||||
|
|
||||||
|
private static final Pointer setSubmenu = ObjectiveC.sel_getUid("setSubmenu:");
|
||||||
|
|
||||||
|
private static final Pointer setState = ObjectiveC.sel_getUid("setState:");
|
||||||
|
private static final Pointer setTarget = ObjectiveC.sel_getUid("setTarget:");
|
||||||
|
private static final Pointer setAction = ObjectiveC.sel_getUid("setAction:");
|
||||||
|
|
||||||
|
public
|
||||||
|
NSMenuItem() {
|
||||||
|
super(ObjectiveC.class_createInstance(objectClass, 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
public
|
||||||
|
NSMenuItem(long peer) {
|
||||||
|
super(peer);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A menu item that is used to separate logical groups of menu commands.
|
||||||
|
* <p>
|
||||||
|
* NOTE: This menu item is disabled. The default separator item is blank space.
|
||||||
|
*/
|
||||||
|
public static
|
||||||
|
NSMenuItem separatorItem() {
|
||||||
|
return new NSMenuItem(ObjectiveC.objc_msgSend(objectClass, separatorItem));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The menu item's title.
|
||||||
|
*/
|
||||||
|
public
|
||||||
|
void setTitle(NSString title) {
|
||||||
|
ObjectiveC.objc_msgSend(this, setTitle, title);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the menu item's title.
|
||||||
|
*/
|
||||||
|
public
|
||||||
|
NSString setToolTip(NSString tooltip) {
|
||||||
|
return new NSString(ObjectiveC.objc_msgSend(this, toolTip, tooltip));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the menu item indentation level for the menu item.
|
||||||
|
*
|
||||||
|
* @param indentationLevel Value is from 0 to 15. The default indentation level is 0.
|
||||||
|
*/
|
||||||
|
public
|
||||||
|
void setIndentationLevel(NSInteger indentationLevel) {
|
||||||
|
ObjectiveC.objc_msgSend(this, NSMenuItem.setIndentationLevel, indentationLevel);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The menu item's shortcut key, if it's a capital letter, then it's a capital letter required for the shortcut
|
||||||
|
* <p>
|
||||||
|
* https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Button/Tasks/SettingButtonKeyEquiv.html
|
||||||
|
*/
|
||||||
|
public
|
||||||
|
void setKeyEquivalent(NSString characters) {
|
||||||
|
ObjectiveC.objc_msgSend(this, setKeyEquivalent, characters);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The menu item’s image.
|
||||||
|
*/
|
||||||
|
public
|
||||||
|
void setImage(NSImage image) {
|
||||||
|
ObjectiveC.objc_msgSend(this, setImage, image);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The menu item’s image.
|
||||||
|
*/
|
||||||
|
public
|
||||||
|
void setOnStateImage(NSImage image) {
|
||||||
|
ObjectiveC.objc_msgSend(this, setOnStateImage, image);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Boolean value that indicates whether the menu item is enabled.
|
||||||
|
*/
|
||||||
|
public
|
||||||
|
void setEnabled(boolean enabled) {
|
||||||
|
ObjectiveC.objc_msgSend(this, setEnabled, enabled);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The submenu of the menu item.
|
||||||
|
*/
|
||||||
|
public
|
||||||
|
void setSubmenu(NSMenu submenu) {
|
||||||
|
ObjectiveC.objc_msgSend(this, setSubmenu, submenu);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The state of the menu item.
|
||||||
|
* <p>
|
||||||
|
* NOTE: The image associated with the new state is displayed to the left of the menu item.
|
||||||
|
*/
|
||||||
|
public
|
||||||
|
void setState(int state) {
|
||||||
|
ObjectiveC.objc_msgSend(this, setState, state);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The menu item's target.
|
||||||
|
* <p>
|
||||||
|
* NOTE: To ensure that a menu item’s target can receive commands while a
|
||||||
|
* modal dialog is open, the target object should return YES in worksWhenModal.
|
||||||
|
*/
|
||||||
|
public
|
||||||
|
void setTarget(NSObject target) {
|
||||||
|
ObjectiveC.objc_msgSend(this, setTarget, target);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The menu item's action-method selector.
|
||||||
|
*/
|
||||||
|
public
|
||||||
|
void setAction(Pointer pointer) {
|
||||||
|
ObjectiveC.objc_msgSend(this, setAction, pointer);
|
||||||
|
}
|
||||||
|
}
|
73
src/dorkbox/util/jna/macos/cocoa/NSObject.java
Executable file
73
src/dorkbox/util/jna/macos/cocoa/NSObject.java
Executable file
@ -0,0 +1,73 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2018 dorkbox, llc
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package dorkbox.util.jna.macos.cocoa;
|
||||||
|
|
||||||
|
import com.sun.jna.Pointer;
|
||||||
|
|
||||||
|
import dorkbox.util.jna.macos.foundation.ObjectiveC;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* https://developer.apple.com/documentation/objectivec/nsobject?language=objc
|
||||||
|
*/
|
||||||
|
public
|
||||||
|
class NSObject extends Pointer {
|
||||||
|
public static final Pointer objectClass = ObjectiveC.objc_lookUpClass("NSObject");
|
||||||
|
|
||||||
|
private static final Pointer alloc = ObjectiveC.sel_getUid("alloc");
|
||||||
|
private static final Pointer retain = ObjectiveC.sel_getUid("retain");
|
||||||
|
private static final Pointer release = ObjectiveC.sel_getUid("release");
|
||||||
|
|
||||||
|
public
|
||||||
|
NSObject(long peer) {
|
||||||
|
super(peer);
|
||||||
|
|
||||||
|
retain();
|
||||||
|
}
|
||||||
|
|
||||||
|
public
|
||||||
|
NSObject(Pointer pointer) {
|
||||||
|
this(Pointer.nativeValue(pointer));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected
|
||||||
|
void finalize() throws Throwable {
|
||||||
|
release();
|
||||||
|
|
||||||
|
super.finalize();
|
||||||
|
}
|
||||||
|
|
||||||
|
public
|
||||||
|
void alloc() {
|
||||||
|
ObjectiveC.objc_msgSend(this, alloc);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public
|
||||||
|
void retain() {
|
||||||
|
ObjectiveC.objc_msgSend(this, retain);
|
||||||
|
}
|
||||||
|
|
||||||
|
public
|
||||||
|
void release() {
|
||||||
|
ObjectiveC.objc_msgSend(this, release);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("WeakerAccess")
|
||||||
|
public
|
||||||
|
long asPointer() {
|
||||||
|
return Pointer.nativeValue(this);
|
||||||
|
}
|
||||||
|
}
|
56
src/dorkbox/util/jna/macos/cocoa/NSStatusBar.java
Executable file
56
src/dorkbox/util/jna/macos/cocoa/NSStatusBar.java
Executable file
@ -0,0 +1,56 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2018 dorkbox, llc
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package dorkbox.util.jna.macos.cocoa;
|
||||||
|
|
||||||
|
import com.sun.jna.Pointer;
|
||||||
|
|
||||||
|
import dorkbox.util.jna.macos.foundation.ObjectiveC;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* https://developer.apple.com/documentation/appkit/nsstatusbar?language=objc
|
||||||
|
*/
|
||||||
|
public
|
||||||
|
class NSStatusBar extends NSObject {
|
||||||
|
|
||||||
|
public final static double NSVariableStatusItemLength = -1; // can specify exactly how wide to make the space
|
||||||
|
public final static double NSSquareStatusItemLength = -2; // the space will be a square
|
||||||
|
|
||||||
|
static Pointer objectClass = ObjectiveC.objc_lookUpClass("NSStatusBar");
|
||||||
|
|
||||||
|
static Pointer systemStatusBar = ObjectiveC.sel_getUid("systemStatusBar");
|
||||||
|
static Pointer statusItemWithLength = ObjectiveC.sel_getUid("statusItemWithLength:");
|
||||||
|
static Pointer removeStatusItem = ObjectiveC.sel_getUid("removeStatusItem:");
|
||||||
|
|
||||||
|
public
|
||||||
|
NSStatusBar(long peer) {
|
||||||
|
super(peer);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static
|
||||||
|
NSStatusBar systemStatusBar() {
|
||||||
|
return new NSStatusBar(ObjectiveC.objc_msgSend(objectClass, systemStatusBar));
|
||||||
|
}
|
||||||
|
|
||||||
|
public
|
||||||
|
NSStatusItem newStatusItem() {
|
||||||
|
return new NSStatusItem(ObjectiveC.objc_msgSend(this, statusItemWithLength, NSStatusBar.NSSquareStatusItemLength));
|
||||||
|
}
|
||||||
|
|
||||||
|
public
|
||||||
|
void removeStatusItem(NSStatusItem statusItem) {
|
||||||
|
ObjectiveC.objc_msgSend(this, removeStatusItem, statusItem);
|
||||||
|
}
|
||||||
|
}
|
57
src/dorkbox/util/jna/macos/cocoa/NSStatusItem.java
Executable file
57
src/dorkbox/util/jna/macos/cocoa/NSStatusItem.java
Executable file
@ -0,0 +1,57 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2018 dorkbox, llc
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package dorkbox.util.jna.macos.cocoa;
|
||||||
|
|
||||||
|
import com.sun.jna.Pointer;
|
||||||
|
|
||||||
|
import dorkbox.util.jna.macos.foundation.ObjectiveC;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* https://developer.apple.com/documentation/appkit/nsstatusitem?language=objc
|
||||||
|
*/
|
||||||
|
public
|
||||||
|
class NSStatusItem extends NSObject {
|
||||||
|
|
||||||
|
private static final Pointer setHighlightMode = ObjectiveC.sel_getUid("setHighlightMode:");
|
||||||
|
private static final Pointer setToolTip = ObjectiveC.sel_getUid("setToolTip:");
|
||||||
|
private static final Pointer setMenu = ObjectiveC.sel_getUid("setMenu:");
|
||||||
|
private static final Pointer setImage = ObjectiveC.sel_getUid("setImage:");
|
||||||
|
|
||||||
|
public
|
||||||
|
NSStatusItem(long peer) {
|
||||||
|
super(peer);
|
||||||
|
}
|
||||||
|
|
||||||
|
public
|
||||||
|
void setHighlightMode(boolean highlight) {
|
||||||
|
ObjectiveC.objc_msgSend(this, setHighlightMode, highlight);
|
||||||
|
}
|
||||||
|
|
||||||
|
public
|
||||||
|
void setImage(NSImage image) {
|
||||||
|
ObjectiveC.objc_msgSend(this, setImage, image);
|
||||||
|
}
|
||||||
|
|
||||||
|
public
|
||||||
|
void setMenu(NSMenu menu) {
|
||||||
|
ObjectiveC.objc_msgSend(this, setMenu, menu);
|
||||||
|
}
|
||||||
|
|
||||||
|
public
|
||||||
|
void setToolTip(NSString tooltip) {
|
||||||
|
ObjectiveC.objc_msgSend(this, setToolTip, tooltip);
|
||||||
|
}
|
||||||
|
}
|
57
src/dorkbox/util/jna/macos/cocoa/NSString.java
Executable file
57
src/dorkbox/util/jna/macos/cocoa/NSString.java
Executable file
@ -0,0 +1,57 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2018 dorkbox, llc
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package dorkbox.util.jna.macos.cocoa;
|
||||||
|
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
|
||||||
|
import com.sun.jna.Pointer;
|
||||||
|
|
||||||
|
import dorkbox.util.jna.macos.foundation.ObjectiveC;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* https://developer.apple.com/documentation/foundation/nsstring?language=objc
|
||||||
|
*/
|
||||||
|
public
|
||||||
|
class NSString extends NSObject {
|
||||||
|
private static final Charset UTF8 = Charset.forName("UTF-8");
|
||||||
|
|
||||||
|
static
|
||||||
|
Object asBytes(String string) {
|
||||||
|
return (string + "\0").getBytes(UTF8);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static final Pointer objectClass = ObjectiveC.objc_lookUpClass("NSString");
|
||||||
|
|
||||||
|
private static final Pointer stringWithUTF8String = ObjectiveC.sel_getUid("stringWithUTF8String:");
|
||||||
|
private static final Pointer UTF8String = ObjectiveC.sel_getUid("UTF8String");
|
||||||
|
|
||||||
|
public
|
||||||
|
NSString(long peer) {
|
||||||
|
super(peer);
|
||||||
|
}
|
||||||
|
|
||||||
|
public
|
||||||
|
NSString(String string) {
|
||||||
|
this(ObjectiveC.objc_msgSend(objectClass, stringWithUTF8String, asBytes(string)));
|
||||||
|
}
|
||||||
|
|
||||||
|
public
|
||||||
|
String toString() {
|
||||||
|
long pointerReference = ObjectiveC.objc_msgSend(this, UTF8String);
|
||||||
|
return new Pointer(pointerReference).getString(0);
|
||||||
|
}
|
||||||
|
}
|
24
src/dorkbox/util/jna/macos/cocoa/OsxClickCallback.java
Normal file
24
src/dorkbox/util/jna/macos/cocoa/OsxClickCallback.java
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2018 dorkbox, llc
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package dorkbox.util.jna.macos.cocoa;
|
||||||
|
|
||||||
|
import dorkbox.util.Keep;
|
||||||
|
|
||||||
|
@Keep
|
||||||
|
public
|
||||||
|
interface OsxClickCallback {
|
||||||
|
void click();
|
||||||
|
}
|
157
src/dorkbox/util/jna/macos/foundation/ObjectiveC.java
Executable file
157
src/dorkbox/util/jna/macos/foundation/ObjectiveC.java
Executable file
@ -0,0 +1,157 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2018 dorkbox, llc
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package dorkbox.util.jna.macos.foundation;
|
||||||
|
|
||||||
|
import com.sun.jna.Callback;
|
||||||
|
import com.sun.jna.Library;
|
||||||
|
import com.sun.jna.Native;
|
||||||
|
import com.sun.jna.Pointer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* "how to" from: https://gist.github.com/3974488
|
||||||
|
* <p>
|
||||||
|
* https://developer.apple.com/documentation/objectivec/objective-c_runtime
|
||||||
|
* <p>
|
||||||
|
* We cannot use JNA "direct mapping", because direct mapping DOES NOT support var-args (which `objc_msgSend` requires)
|
||||||
|
*/
|
||||||
|
public
|
||||||
|
class ObjectiveC {
|
||||||
|
interface Objc extends Library {
|
||||||
|
Pointer objc_lookUpClass(String name);
|
||||||
|
|
||||||
|
long objc_msgSend(Pointer theReceiver, Pointer theSelector, Object... string);
|
||||||
|
|
||||||
|
Pointer objc_allocateClassPair(Pointer superClass, String name, long extraBytes);
|
||||||
|
|
||||||
|
void objc_registerClassPair(Pointer clazz);
|
||||||
|
|
||||||
|
Pointer class_createInstance(Pointer clazz, int extraBytes);
|
||||||
|
|
||||||
|
boolean class_addMethod(Pointer clazz, Pointer selector, Callback callback, String types);
|
||||||
|
|
||||||
|
Pointer sel_getUid(String name);
|
||||||
|
|
||||||
|
Pointer sel_registerName(String name);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final Objc INSTANCE = Native.loadLibrary("objc", Objc.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the class definition of a specified class.
|
||||||
|
*
|
||||||
|
* @param name The name of the class to look up.
|
||||||
|
*
|
||||||
|
* @return The Class object for the named class, or nil if the class is not registered with the Objective-C runtime.
|
||||||
|
*/
|
||||||
|
public static
|
||||||
|
Pointer objc_lookUpClass(String name) {
|
||||||
|
return INSTANCE.objc_lookUpClass(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends a message with a simple return value to an instance of a class.
|
||||||
|
*
|
||||||
|
* @param self A pointer that points to the instance of the class that is to receive the message.
|
||||||
|
* @param op The selector of the method that handles the message.
|
||||||
|
* @param args A variable argument list containing the arguments to the method.
|
||||||
|
*
|
||||||
|
* @return The return value of the method.
|
||||||
|
*/
|
||||||
|
public static
|
||||||
|
long objc_msgSend(Pointer self, Pointer op, Object... args) {
|
||||||
|
return INSTANCE.objc_msgSend(self, op, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new class and metaclass.
|
||||||
|
*
|
||||||
|
* @param superClass The class to use as the new class's superclass, or Nil to create a new root class.
|
||||||
|
* @param name The string to use as the new class's name. The string will be copied.
|
||||||
|
* @param extraBytes The number of bytes to allocate for indexed ivars at the end of the class and metaclass objects. This should usually be 0.
|
||||||
|
*
|
||||||
|
* @return The new class, or Nil if the class could not be created (for example, the desired name is already in use).
|
||||||
|
*/
|
||||||
|
public static
|
||||||
|
Pointer objc_allocateClassPair(Pointer superClass, String name, long extraBytes) {
|
||||||
|
return INSTANCE.objc_allocateClassPair(superClass, name, extraBytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers a class that was allocated using objc_allocateClassPair.
|
||||||
|
*
|
||||||
|
* @param cls The class you want to register.
|
||||||
|
*/
|
||||||
|
public static
|
||||||
|
void objc_registerClassPair(Pointer cls) {
|
||||||
|
INSTANCE.objc_registerClassPair(cls);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an instance of a class, allocating memory for the class in the default malloc memory zone.
|
||||||
|
*
|
||||||
|
* @param cls The class that you want to allocate an instance of.
|
||||||
|
* @param extraBytes An integer indicating the number of extra bytes to allocate. The additional bytes can be used to store additional instance variables beyond those defined in the class definition.
|
||||||
|
*
|
||||||
|
* @return An instance of the class cls.
|
||||||
|
*/
|
||||||
|
public static
|
||||||
|
Pointer class_createInstance(Pointer cls, int extraBytes) {
|
||||||
|
return INSTANCE.class_createInstance(cls, extraBytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a new method to a class with a given name and implementation.
|
||||||
|
* NOTE: class_addMethod will add an override of a superclass's implementation, but will not replace an existing implementation in this class. To change an existing implementation, use method_setImplementation.
|
||||||
|
*
|
||||||
|
* @param cls The class to which to add a method.
|
||||||
|
* @param name A selector that specifies the name of the method being added.
|
||||||
|
* @param imp A function which is the implementation of the new method. The function must take at least two arguments—self and _cmd.
|
||||||
|
* @param types An array of characters that describe the types of the arguments to the method. For possible values, see Objective-C Runtime Programming Guide > Type Encodings. Since the function must take at least two arguments—self and _cmd, the second and third characters must be “@:” (the first character is the return type).
|
||||||
|
*
|
||||||
|
* @return YES if the method was added successfully, otherwise NO (for example, the class already contains a method implementation with that name).
|
||||||
|
*/
|
||||||
|
public static
|
||||||
|
boolean class_addMethod(Pointer cls, Pointer name, Callback imp, String types) {
|
||||||
|
return INSTANCE.class_addMethod(cls, name, imp, types);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers a method name with the Objective-C runtime system.
|
||||||
|
* NOTE: The implementation of this method is identical to the implementation of sel_registerName.
|
||||||
|
*
|
||||||
|
* @param str A pointer to a C string. Pass the name of the method you wish to register.
|
||||||
|
*
|
||||||
|
* @return A pointer of type SEL specifying the selector for the named method.
|
||||||
|
*/
|
||||||
|
public static
|
||||||
|
Pointer sel_getUid(String str) {
|
||||||
|
return INSTANCE.sel_getUid(str);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers a method with the Objective-C runtime system, maps the method name to a selector, and returns the selector value.
|
||||||
|
* NOTE: You must register a method name with the Objective-C runtime system to obtain the method’s selector before you can add the method to a class definition. If the method name has already been registered, this function simply returns the selector.
|
||||||
|
*
|
||||||
|
* @param str A pointer to a C string. Pass the name of the method you wish to register.
|
||||||
|
*
|
||||||
|
* @return A pointer of type SEL specifying the selector for the named method.
|
||||||
|
*/
|
||||||
|
public static
|
||||||
|
Pointer sel_registerName(String str) {
|
||||||
|
return INSTANCE.sel_registerName(str);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user