Skip to content

Commit 6054f1f

Browse files
authored
textfield: Add OnChange(fn func(newValue string)) (#3)
1 parent 1c9b795 commit 6054f1f

File tree

5 files changed

+44
-1
lines changed

5 files changed

+44
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
>
77
> #### Changes to original
88
>
9+
> - 2024-05-22 [#3](https://github.com/roblillack/gocoa/pull/3): Add support for `OnChange` callbacks for textfields
910
> - 2024-05-22 [#1](https://github.com/roblillack/gocoa/pull/1): Add support for removing controls (based on work by @phaus)
1011
> - 2024-05-22 [#2](https://github.com/roblillack/gocoa/pull/2): Update deprecated NSButton enums (by @dim13)
1112
> - 2024-05-14 [e85e4be](https://github.com/roblillack/gocoa/commit/e85e4be) progressindicator: Don't hide by default as this would increase time to create widgets in the first place

textfield.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
// TextField -Button represents a button control that can trigger actions.
1313
type TextField struct {
1414
textFieldPtr C.TextFieldPtr
15-
callback func()
15+
callback func(value string)
1616
}
1717

1818
var textfields []*TextField
@@ -126,3 +126,16 @@ func (textField *TextField) SetSelectable(selectable bool) {
126126
C.TextField_SetSelectable(textField.textFieldPtr, C.int(0))
127127
}
128128
}
129+
130+
func (textField *TextField) OnChange(fn func(value string)) {
131+
textField.callback = fn
132+
}
133+
134+
//export onTextFieldDidChange
135+
func onTextFieldDidChange(id C.int) {
136+
textFieldID := int(id)
137+
if textFieldID < len(textfields) && textfields[textFieldID].callback != nil {
138+
tf := textfields[textFieldID]
139+
tf.callback(tf.StringValue())
140+
}
141+
}

textfield.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#import "textfield.h"
2+
#import "textfielddelegate.h"
23
#include "_cgo_export.h"
34

45
@implementation TextFieldHandler
@@ -8,6 +9,10 @@ TextFieldPtr TextField_New(int goTextFieldId, int x, int y, int w, int h) {
89
/* create the NSTextField and add it to the window */
910
id nsTextField = [[[NSTextField alloc] initWithFrame:NSMakeRect(x, y, w, h)] autorelease];
1011

12+
id d = [[TextFieldDelegate alloc] init];
13+
[d setGoTextFieldId:goTextFieldId];
14+
[nsTextField setDelegate:d];
15+
1116
return (TextFieldPtr)nsTextField;
1217
}
1318

textfielddelegate.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#import <Cocoa/Cocoa.h>
2+
3+
@interface TextFieldDelegate : NSObject <NSTextFieldDelegate>
4+
5+
@property(assign) int goTextFieldId;
6+
7+
@end

textfielddelegate.m

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#import "textfielddelegate.h"
2+
#include "_cgo_export.h"
3+
4+
@implementation TextFieldDelegate
5+
6+
- (void)dealloc {
7+
[super dealloc];
8+
}
9+
10+
- (void)controlTextDidChange:(NSNotification *)aNotification {
11+
// NSTextField *textField = [aNotification object];
12+
// const char *text = [[textField stringValue]
13+
// cStringUsingEncoding:NSUTF8StringEncoding];
14+
onTextFieldDidChange([self goTextFieldId]);
15+
}
16+
17+
@end

0 commit comments

Comments
 (0)