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 @end
@interface _sapp_macos_window_delegate : NSObject<NSWindowDelegate> @interface _sapp_macos_window_delegate : NSObject<NSWindowDelegate>
@end @end
#if defined(SOKOL_METAL) #if defined(SOKOL_METAL)
@interface _sapp_macos_view : MTKView @interface _sapp_macos_view : MTKView
@end @end
@ -5452,7 +5453,7 @@ _SOKOL_PRIVATE LRESULT CALLBACK _sapp_win32_wndproc(HWND hWnd, UINT uMsg, WPARAM
_sapp_wgl_swap_buffers(); _sapp_wgl_swap_buffers();
#endif #endif
/* NOTE: resizing the swap-chain during resize leads to a substantial /* 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 (_sapp_win32_update_dimensions()) {
#if defined(SOKOL_D3D11) #if defined(SOKOL_D3D11)

View File

@ -3,6 +3,9 @@ module clipboard
#include <libkern/OSAtomic.h> #include <libkern/OSAtomic.h>
#include <Cocoa/Cocoa.h> #include <Cocoa/Cocoa.h>
#flag -framework Cocoa #flag -framework Cocoa
#include "@VROOT/vlib/clipboard/clipboard_darwin.m"
pub struct Clipboard { pub struct Clipboard {
pb voidptr pb voidptr
last_cb_serial i64 last_cb_serial i64
@ -10,11 +13,12 @@ mut:
foo int // TODO remove, for mut hack foo int // TODO remove, for mut hack
} }
fn C.darwin_new_pasteboard() voidptr
fn C.darwin_get_pasteboard_text() byteptr
fn new_clipboard() &Clipboard { fn new_clipboard() &Clipboard {
pb := voidptr(0)
#pb = [NSPasteboard generalPasteboard];
cb := &Clipboard{ cb := &Clipboard{
pb: pb pb: C.darwin_new_pasteboard()// pb
} }
return cb return cb
} }
@ -59,13 +63,11 @@ fn (mut cb Clipboard) set_text(text string) bool {
fn (mut cb Clipboard) get_text() string { fn (mut cb Clipboard) get_text() string {
cb.foo = 0 cb.foo = 0
#NSString *ns_clip; if isnil(cb.pb) {
utf8_clip := byteptr(0) return ''
#ns_clip = [cb->pb stringForType:NSStringPboardType]; //NSPasteboardTypeString }
#if (ns_clip == nil) {
#return tos3(""); //in case clipboard is empty utf8_clip := C.darwin_get_pasteboard_text(cb.pb)
#}
#utf8_clip = [ns_clip UTF8String];
return unsafe {utf8_clip.vstring()} 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 // text := text_.trim_space() // TODO remove/optimize
mut text := text_ mut text := text_
if text.contains('\t') { //if text.contains('\t') {
text = text.replace('\t', ' ') //text = text.replace('\t', ' ')
} //}
ctx.set_cfg(cfg) ctx.set_cfg(cfg)
scale := if ctx.ft.scale == 0 { f32(1) } else { ctx.ft.scale } 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 C.fonsDrawText(ctx.ft.fons, x * scale, y * scale, text.str, 0) // TODO: check offsets/alignment