1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/clipboard/clipboard_darwin.c.v

85 lines
2.1 KiB
V
Raw Normal View History

module clipboard
#include <libkern/OSAtomic.h>
#include <Cocoa/Cocoa.h>
#flag -framework Cocoa
#include "@VEXEROOT/vlib/clipboard/clipboard_darwin.m"
// Clipboard represents a system clipboard.
//
// System "copy" and "paste" actions utilize the clipboard for temporary storage.
pub struct Clipboard {
pb voidptr
last_cb_serial i64
mut:
foo int // TODO remove, for mut hack
}
2020-11-20 03:08:39 +03:00
fn C.darwin_new_pasteboard() voidptr
2022-04-15 18:25:45 +03:00
fn C.darwin_get_pasteboard_text(voidptr) &u8
2021-01-31 15:57:06 +03:00
fn C.darwin_set_pasteboard_text(voidptr, string) bool
2020-11-20 03:08:39 +03:00
fn new_clipboard() &Clipboard {
cb := &Clipboard{
pb: C.darwin_new_pasteboard() // pb
}
return cb
}
// check_availability returns true if the clipboard is ready to be used.
pub fn (cb &Clipboard) check_availability() bool {
return cb.pb != C.NULL
}
// clear empties the clipboard contents.
pub fn (mut cb Clipboard) clear() {
cb.foo = 0
cb.set_text('')
2020-11-20 13:58:53 +03:00
//#[cb->pb clearContents];
}
// free releases all memory associated with the clipboard
// instance.
pub fn (mut cb Clipboard) free() {
cb.foo = 0
// nothing to free
}
// has_ownership returns true if the contents of
// the clipboard were created by this clipboard instance.
pub fn (cb &Clipboard) has_ownership() bool {
if cb.last_cb_serial == 0 {
return false
}
2020-11-21 01:49:52 +03:00
//#return [cb->pb changeCount] == cb->last_cb_serial;
return false
}
2019-11-24 06:27:02 +03:00
fn C.OSAtomicCompareAndSwapLong()
// set_text transfers `text` to the system clipboard.
// This is often associated with a *copy* action (`Cmd` + `C`).
pub fn (mut cb Clipboard) set_text(text string) bool {
2020-11-20 05:28:28 +03:00
return C.darwin_set_pasteboard_text(cb.pb, text)
}
// get_text retrieves the contents of the system clipboard
// as a `string`.
// This is often associated with a *paste* action (`Cmd` + `V`).
pub fn (mut cb Clipboard) get_text() string {
cb.foo = 0
2020-11-20 03:08:39 +03:00
if isnil(cb.pb) {
return ''
}
utf8_clip := C.darwin_get_pasteboard_text(cb.pb)
2022-04-15 14:58:56 +03:00
return unsafe { tos_clone(&u8(utf8_clip)) }
2019-11-18 13:10:31 +03:00
}
// new_primary returns a new X11 `PRIMARY` type `Clipboard` instance allocated on the heap.
// Please note: new_primary only works on X11 based systems.
pub fn new_primary() &Clipboard {
panic('Primary clipboard is not supported on non-Linux systems.')
}