Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Cocoa TextView OnChange callback #20

Merged
merged 3 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion ui/internal/cocoa/textview.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import "unsafe"
// TextView - represents a textView control that can trigger actions.
type TextView struct {
ptr C.TextViewPtr
callback func()
callback func(string)
}

var textviews []*TextView
Expand All @@ -26,6 +26,11 @@ func NewTextView(x int, y int, width int, height int) *TextView {
return tv
}

// GetText gets the text of the text view
func (textview *TextView) GetText() string {
return C.GoString(C.TextView_Text(textview.ptr))
}

// SetText sets the text of the text view
func (textview *TextView) SetText(text string) {
cText := C.CString(text)
Expand All @@ -50,3 +55,16 @@ func (c *TextView) SetEditable(editable bool) {
C.TextView_SetEditable(c.ptr, 0)
}
}

func (c *TextView) OnChange(fn func(value string)) {
c.callback = fn
}

//export onTextViewDidChange
func onTextViewDidChange(id C.int) {
textViewId := int(id)
if textViewId < len(textviews) && textviews[textViewId].callback != nil {
tf := textviews[textViewId]
tf.callback(tf.GetText())
}
}
1 change: 1 addition & 0 deletions ui/internal/cocoa/textview.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
typedef void *TextViewPtr;

TextViewPtr TextView_New(int goTextViewId, int x, int y, int w, int h);
const char *TextView_Text(TextViewPtr textViewPtr);
void TextView_SetText(TextViewPtr textViewPtr, const char *text);
void TextView_Remove(TextViewPtr textViewPtr);
void TextView_SetFontSize(TextViewPtr textViewPtr, int size);
Expand Down
11 changes: 11 additions & 0 deletions ui/internal/cocoa/textview.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#import "textview.h"
#include "_cgo_export.h"
#import "textviewdelegate.h"

@implementation TextViewHandler
@end
Expand All @@ -9,6 +10,10 @@ TextViewPtr TextView_New(int goTextViewId, int x, int y, int w, int h) {
NSTextView *textView =
[[[NSTextView alloc] initWithFrame:NSMakeRect(x, y, w, h)] autorelease];

id d = [[TextViewDelegate alloc] init];
[d setGoTextViewId:goTextViewId];
[textView setDelegate:d];

NSScrollView *scrollView =
[[[NSScrollView alloc] initWithFrame:NSMakeRect(x, y, w, h)] autorelease];
[scrollView setDocumentView:textView];
Expand All @@ -20,6 +25,12 @@ TextViewPtr TextView_New(int goTextViewId, int x, int y, int w, int h) {
return (TextViewPtr)scrollView;
}

const char *TextView_Text(TextViewPtr ptr) {
NSTextView *c = ((NSScrollView *)ptr).documentView;
return
[[[c textStorage] string] cStringUsingEncoding:NSISOLatin1StringEncoding];
}

void TextView_SetText(TextViewPtr ptr, const char *text) {
NSTextView *c = ((NSScrollView *)ptr).documentView;
[c setString:[NSString stringWithUTF8String:text]];
Expand Down
7 changes: 7 additions & 0 deletions ui/internal/cocoa/textviewdelegate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#import <Cocoa/Cocoa.h>

@interface TextViewDelegate : NSObject <NSTextViewDelegate>

@property(assign) int goTextViewId;

@end
17 changes: 17 additions & 0 deletions ui/internal/cocoa/textviewdelegate.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#import "textviewdelegate.h"
#include "_cgo_export.h"

@implementation TextViewDelegate

- (void)dealloc {
[super dealloc];
}

- (void)textDidChange:(NSNotification *)aNotification {
// NSTextField *textField = [aNotification object];
// const char *text = [[textField stringValue]
// cStringUsingEncoding:NSUTF8StringEncoding];
onTextViewDidChange([self goTextViewId]);
}

@end
3 changes: 3 additions & 0 deletions ui/texteditor_cocoa.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ func (w *TextEditor) Update(nextComponent spot.Control) bool {
w.ref.SetFontSize(w.FontSize)
}

w.ref.OnChange(next.OnChange)

return true
}

Expand All @@ -41,6 +43,7 @@ func (w *TextEditor) Mount(parent spot.Control) any {
if w.FontSize > 0 {
w.ref.SetFontSize(w.FontSize)
}
w.ref.OnChange(w.OnChange)

if window, ok := parent.(*Window); ok && window != nil && window.ref != nil {
window.ref.AddTextView(w.ref)
Expand Down