-
Notifications
You must be signed in to change notification settings - Fork 9
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
[WIP] Text Input #69
Open
schinns
wants to merge
6
commits into
briskml:master
Choose a base branch
from
schinns:feature/text-input
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[WIP] Text Input #69
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d3ad852
ignoring .vscode
schinns 806a20b
text input work in progress
schinns c3e1545
added BriskTextInput
schinns 5ffa7de
required methods in implementation
schinns 59ec718
cleaned up stub
schinns 73c7d5b
just trying to et BriskTextInput to render
schinns File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ _build | |
*.log | ||
*.install | ||
default.profraw | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
type t = CocoaTypes.view; | ||
|
||
[@noalloc] external make: unit => t = "ml_BriskTextInput_make"; | ||
|
||
[@noalloc] | ||
external getTextWidth: t => [@unboxed] float = | ||
"ml_BriskTextInput_getTextWidth_bc" "ml_BriskTextInput_getTextWidth"; | ||
|
||
[@noalloc] | ||
external getTextHeight: t => [@unboxed] float = | ||
"ml_BriskTextInput_getTextHeight_bc" "ml_BriskTextInput_getTextHeight"; | ||
|
||
[@noalloc] | ||
external setStringValue: (t, string) => unit = | ||
"ml_BriskTextInput_setStringValue"; | ||
|
||
[@noalloc] | ||
external setPlaceholder: (t, string) => unit = | ||
"ml_BriskTextInput_setPlaceholder"; | ||
|
||
[@noalloc] | ||
external setBackgroundColor: | ||
( | ||
t, | ||
[@unboxed] float, | ||
[@unboxed] float, | ||
[@unboxed] float, | ||
[@unboxed] float | ||
) => | ||
unit = | ||
"ml_BriskTextInput_setBackgroundColor_bc" | ||
"ml_BriskTextInput_setBackgroundColor"; | ||
|
||
[@noalloc] | ||
external setPadding: | ||
( | ||
t, | ||
[@unboxed] float, | ||
[@unboxed] float, | ||
[@unboxed] float, | ||
[@unboxed] float | ||
) => | ||
unit = | ||
"ml_BriskTextInput_setPadding_bc" "ml_BriskTextInput_setPadding"; | ||
|
||
let make = (value, placeholder) => { | ||
let input = make(); | ||
|
||
setStringValue(input, value); | ||
setPlaceholder(input, placeholder); | ||
input; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
open Brisk; | ||
|
||
type attribute = [ Layout.style | Styles.textStyle | Styles.viewStyle]; | ||
|
||
type style = list(attribute); | ||
|
||
let component = nativeComponent("Text"); | ||
|
||
let measure = (node, _, _, _, _) => { | ||
open Layout.FlexLayout.LayoutSupport.LayoutTypes; | ||
|
||
let {context: input}: node = node; | ||
|
||
let width = BriskTextInput.getTextWidth(input) |> int_of_float; | ||
let height = BriskTextInput.getTextHeight(input) |> int_of_float; | ||
|
||
{width, height}; | ||
}; | ||
|
||
let make = (~style=[], ~value, ~placeholder, children) => | ||
component((_: Hooks.empty) => | ||
{ | ||
make: () => { | ||
let view = BriskTextInput.make(value, placeholder); | ||
{view, layoutNode: Layout.Node.make(~measure, ~style, view)}; | ||
}, | ||
configureInstance: (~isFirstRender as _, {view} as node) => { | ||
open Layout; | ||
style | ||
|> List.iter(attribute => | ||
switch (attribute) { | ||
| `Padding({left, top, right, bottom}) => | ||
BriskTextInput.setPadding(view, left, top, right, bottom) | ||
| `Background(({r, g, b, a}: Color.t)) => | ||
BriskTextInput.setBackgroundColor(view, r, g, b, a) | ||
| #Styles.textStyle => Styles.setTextStyle(view, attribute) | ||
| #Styles.viewStyle => Styles.setViewStyle(view, attribute) | ||
| #Layout.style => () | ||
} | ||
); | ||
Styles.commitTextStyle(view); | ||
node; | ||
}, | ||
children, | ||
} | ||
); | ||
|
||
let createElement = (~style=[], ~value, ~placeholder, ~children, ()) => | ||
element(make(~style, ~value, ~placeholder, listToElement(children))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
#import "BriskTextInput.h" | ||
|
||
@interface BriskTextInput : NSTextView <BriskTextInputProtocol> | ||
|
||
@end | ||
|
||
@implementation BriskTextInput | ||
|
||
@synthesize attributedString; | ||
@synthesize attributedProps; | ||
@synthesize paragraphStyle; | ||
@synthesize placeholder; | ||
|
||
- (id)init { | ||
self = [super init]; | ||
if (self) { | ||
self.attributedString = [[NSMutableAttributedString alloc] init]; | ||
self.attributedProps = [NSMutableDictionary dictionary]; | ||
self.paragraphStyle = | ||
[[NSParagraphStyle defaultParagraphStyle] mutableCopy]; | ||
|
||
self.attributedProps[NSParagraphStyleAttributeName] = self.paragraphStyle; | ||
self.placeholder = [[NSMutableAttributedString alloc] init]; | ||
} | ||
return self; | ||
} | ||
|
||
- (BOOL)isEditable { | ||
return YES; | ||
} | ||
|
||
- (BOOL)isSelectable { | ||
return YES; | ||
} | ||
|
||
- (void)applyTextStyle { | ||
NSRange range = NSMakeRange(0, self.attributedString.length); | ||
|
||
[self.attributedString setAttributes:self.attributedProps range:range]; | ||
[[self textStorage] setAttributedString:self.attributedString]; | ||
} | ||
|
||
@end | ||
|
||
BriskTextInput *ml_BriskTextInput_make() { | ||
BriskTextInput *input = [BriskTextInput new]; | ||
retainView(input); | ||
|
||
return input; | ||
} | ||
|
||
double ml_BriskTextInput_getTextWidth(BriskTextInput *input) { | ||
return (double)[input.attributedString size].width; | ||
} | ||
|
||
double ml_BriskTextInput_getTextHeight(BriskTextInput *input) { | ||
return (double)[input.attributedString size].height; | ||
} | ||
|
||
CAMLprim value ml_BriskTextInput_getTextWidth_bc(BriskTextInput *input) { | ||
CAMLparam0(); | ||
CAMLreturn(caml_copy_double(ml_BriskTextInput_getTextWidth(input))); | ||
} | ||
|
||
CAMLprim value ml_BriskTextInput_getTextHeight_bc(BriskTextInput *input) { | ||
CAMLparam0(); | ||
CAMLreturn(caml_copy_double(ml_BriskTextInput_getTextHeight(input))); | ||
} | ||
|
||
CAMLprim value ml_BriskTextInput_setStringValue(BriskTextInput *input, | ||
value str_v) { | ||
CAMLparam1(str_v); | ||
|
||
NSString *str = [NSString stringWithUTF8String:String_val(str_v)]; | ||
|
||
[input.attributedString.mutableString setString:str]; | ||
[input applyTextStyle]; | ||
|
||
CAMLreturn(Val_unit); | ||
} | ||
|
||
CAMLprim value ml_BriskTextInput_setPlaceholder(BriskTextInput *input, | ||
value str_v) { | ||
CAMLparam1(str_v); | ||
|
||
NSString *str = [NSString stringWithUTF8String:String_val(str_v)]; | ||
|
||
[input.placeholder.mutableString setString:str]; | ||
[input applyTextStyle]; | ||
|
||
CAMLreturn(Val_unit); | ||
} | ||
|
||
void ml_BriskTextInput_setBackgroundColor(BriskTextInput *input, double red, | ||
double green, double blue, | ||
double alpha) { | ||
NSColor *color = [NSColor colorWithRed:red green:green blue:blue alpha:alpha]; | ||
[input setBackgroundColor:color]; | ||
} | ||
|
||
CAMLprim value ml_BriskTextInput_setBackgroundColor_bc(BriskTextInput *input, | ||
value red_v, | ||
value green_v, | ||
value blue_v, | ||
value alpha_v) { | ||
CAMLparam4(red_v, blue_v, green_v, alpha_v); | ||
|
||
ml_BriskTextInput_setBackgroundColor(input, Double_val(red_v), | ||
Double_val(blue_v), Double_val(green_v), | ||
Double_val(alpha_v)); | ||
|
||
CAMLreturn(Val_unit); | ||
} | ||
|
||
void ml_BriskTextInput_setPadding(BriskTextInput *input, double left, double top, | ||
__unused double right, | ||
__unused double bottom) { | ||
input.textContainerInset = CGSizeMake(left, top); | ||
} | ||
|
||
CAMLprim value ml_BriskTextInput_setPadding_bc(BriskTextInput *input, value left_v, | ||
value top_v, value right_v, | ||
value bottom_v) { | ||
CAMLparam4(left_v, top_v, right_v, bottom_v); | ||
|
||
ml_BriskTextInput_setPadding(input, Double_val(left_v), Double_val(top_v), | ||
Double_val(right_v), Double_val(bottom_v)); | ||
|
||
CAMLreturn(Val_unit); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#import "BriskCocoa.h" | ||
|
||
@protocol BriskTextInputProtocol <NSTextInput> | ||
|
||
@property(nonatomic, retain) NSMutableAttributedString *attributedString; | ||
@property(nonatomic, retain) NSMutableDictionary *attributedProps; | ||
@property(nonatomic, retain) NSMutableParagraphStyle *paragraphStyle; | ||
@property(nonatomic, retain) NSMutableAttributedString *placeholder; | ||
|
||
- (void)applyTextStyle; | ||
|
||
@end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so
ml_BriskTextInput_make
should return a view (LikeNSTextField
or editableNSTextView
.)NSTextInputContext
is just a protocol (think java interface, functor signature (kinda)) related to text input.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So NSTextInputContext is actually a class and NSTextInputClient is a protocol. Should
ml_BriskTextInput_make
then return aNSTextField
orNSTextView
conforming toNSTextInputClient
protocol?