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

[WIP] Text Input #69

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ _build
*.log
*.install
default.profraw
.vscode
2 changes: 2 additions & 0 deletions renderer-macos/lib/Brisk_macos.re
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module Cocoa = {
module BriskButton = BriskButton;
module BriskImage = BriskImage;
module BriskTextView = BriskTextView;
module BriskTextInput = BriskTextInput;
module GCD = GCD;

module BriskEffectView = BriskEffectView;
Expand All @@ -24,3 +25,4 @@ module Text = Text;
module Image = Image;
module Button = Button;
module EffectView = EffectView;
module TextInput = TextInput;
52 changes: 52 additions & 0 deletions renderer-macos/lib/bindings/BriskTextInput.re
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;
};
49 changes: 49 additions & 0 deletions renderer-macos/lib/components/TextInput.re
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)));
1 change: 1 addition & 0 deletions renderer-macos/lib/dune
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
BriskButton
GCD
BriskEffectView
BriskTextInput
)
(c_flags (:standard
-Wextra -Wall -Werror -O -g -std=c99 -pedantic-errors -Wsign-compare -Wshadow
Expand Down
130 changes: 130 additions & 0 deletions renderer-macos/lib/stubs/BriskTextInput.c
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];
Copy link
Member

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 (Like NSTextField or editable NSTextView.) NSTextInputContext is just a protocol (think java interface, functor signature (kinda)) related to text input.

Copy link
Author

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 a NSTextField or NSTextView conforming to NSTextInputClient protocol?

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);
}
12 changes: 12 additions & 0 deletions renderer-macos/lib/stubs/BriskTextInput.h
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