clipboard: fix get_text() on macos

pull/6882/head
Alexander Medvednikov 2020-11-20 01:08:39 +01:00
parent 925b40e2c0
commit d50c919879
4 changed files with 29 additions and 14 deletions

View File

@ -1360,6 +1360,7 @@ inline int sapp_run(const sapp_desc& desc) { return sapp_run(&desc); }
@end
@interface _sapp_macos_window_delegate : NSObject<NSWindowDelegate>
@end
#if defined(SOKOL_METAL)
@interface _sapp_macos_view : MTKView
@end
@ -5452,7 +5453,7 @@ _SOKOL_PRIVATE LRESULT CALLBACK _sapp_win32_wndproc(HWND hWnd, UINT uMsg, WPARAM
_sapp_wgl_swap_buffers();
#endif
/* NOTE: resizing the swap-chain during resize leads to a substantial
memory spike (hundreds of megabytes for a few seconds).
memory spike (hundreds of megabytes for a few seconds).
if (_sapp_win32_update_dimensions()) {
#if defined(SOKOL_D3D11)

View File

@ -3,6 +3,9 @@ module clipboard
#include <libkern/OSAtomic.h>
#include <Cocoa/Cocoa.h>
#flag -framework Cocoa
#include "@VROOT/vlib/clipboard/clipboard_darwin.m"
pub struct Clipboard {
pb voidptr
last_cb_serial i64
@ -10,11 +13,12 @@ mut:
foo int // TODO remove, for mut hack
}
fn C.darwin_new_pasteboard() voidptr
fn C.darwin_get_pasteboard_text() byteptr
fn new_clipboard() &Clipboard {
pb := voidptr(0)
#pb = [NSPasteboard generalPasteboard];
cb := &Clipboard{
pb: pb
pb: C.darwin_new_pasteboard()// pb
}
return cb
}
@ -59,13 +63,11 @@ fn (mut cb Clipboard) set_text(text string) bool {
fn (mut cb Clipboard) get_text() string {
cb.foo = 0
#NSString *ns_clip;
utf8_clip := byteptr(0)
#ns_clip = [cb->pb stringForType:NSStringPboardType]; //NSPasteboardTypeString
#if (ns_clip == nil) {
#return tos3(""); //in case clipboard is empty
#}
#utf8_clip = [ns_clip UTF8String];
if isnil(cb.pb) {
return ''
}
utf8_clip := C.darwin_get_pasteboard_text(cb.pb)
return unsafe {utf8_clip.vstring()}
}

View File

@ -0,0 +1,12 @@
//NSPasteboard* darwin_new_pasteboard() {
void* darwin_new_pasteboard() {
return (__bridge void*) [NSPasteboard generalPasteboard];
}
char* darwin_get_pasteboard_text(void* pb) {
NSString *ns_clip = [((__bridge NSPasteboard*)pb) stringForType:NSStringPboardType]; //NSPasteboardTypeString
if (ns_clip == nil) {
return "";
}
return [ns_clip UTF8String];
}

View File

@ -114,9 +114,9 @@ pub fn (ctx &Context) draw_text(x int, y int, text_ string, cfg gx.TextCfg) {
}
// text := text_.trim_space() // TODO remove/optimize
mut text := text_
if text.contains('\t') {
text = text.replace('\t', ' ')
}
//if text.contains('\t') {
//text = text.replace('\t', ' ')
//}
ctx.set_cfg(cfg)
scale := if ctx.ft.scale == 0 { f32(1) } else { ctx.ft.scale }
C.fonsDrawText(ctx.ft.fons, x * scale, y * scale, text.str, 0) // TODO: check offsets/alignment