Skip to content

Commit e29f861

Browse files
committed
More view props.
1 parent b90901a commit e29f861

File tree

5 files changed

+389
-99
lines changed

5 files changed

+389
-99
lines changed

ReactFSharp.Android/Views/View.fs

Lines changed: 253 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ open Android.Graphics
55
open Android.Views
66

77
open System
8+
open System.Runtime.CompilerServices
89

910
[<Struct>]
1011
type Padding =
@@ -15,48 +16,80 @@ type Padding =
1516

1617
new (start, top, end_, bottom) = { start = start; top = top; end_ = end_; bottom = bottom }
1718

19+
[<Struct>]
20+
type Pivot =
21+
val x: Single
22+
val y: Single
23+
24+
new (x, y) = { x = x; y = y }
25+
26+
type Translation =
27+
val x: Single
28+
val y: Single
29+
val z: Single
30+
31+
new (x, y, z) = { x = x; y = y; z=z }
32+
1833
type IViewProps =
1934
abstract member Alpha: float32
2035
abstract member BackgroundColor: Color
21-
abstract member BackgroundTintMode: Option<PorterDuff.Mode>
36+
abstract member BackgroundTintMode: PorterDuff.Mode
2237
abstract member Clickable: bool
2338
abstract member ContentDescription: string
2439
abstract member ContextClickable: bool
2540
abstract member LayoutParameters: ViewGroup.LayoutParams
26-
abstract member OnClick: Option<unit -> unit>
41+
abstract member OnClick: unit -> unit
42+
abstract member OnCreateContextMenu: IContextMenu * IContextMenuContextMenuInfo -> unit
43+
abstract member OnDrag: DragEvent -> bool
44+
abstract member OnGenericMotion: MotionEvent -> bool
45+
abstract member OnHover: MotionEvent -> bool
46+
abstract member OnKey: Keycode * KeyEvent -> bool
47+
abstract member OnLongClick: unit -> bool
48+
abstract member OnSystemUiVisibilityChange: StatusBarVisibility -> unit
49+
abstract member OnTouch: MotionEvent -> bool
2750
abstract member Padding: Padding
28-
abstract member PivotX: Single
29-
abstract member PivotY: Single
51+
abstract member Pivot: Pivot
3052
abstract member SoundEffectsEnabled: bool
53+
abstract member SystemUiVisibility: StatusBarVisibility
3154
abstract member TextAlignment: TextAlignment
3255
abstract member TextDirection: TextDirection
3356
abstract member TransitionName: string
34-
abstract member TranslationX: Single
35-
abstract member TranslationY: Single
36-
abstract member TranslationZ: Single
57+
abstract member Translation: Translation
58+
abstract member VerticalFadingEdgeEnabled: bool
59+
abstract member VerticalScrollBarEnabled: bool
60+
abstract member VerticalScrollbarPosition: ScrollbarPosition
3761
abstract member Visibility: ViewStates
3862

3963
type ViewProps =
4064
{
4165
// View Props
4266
alpha: float32
4367
backgroundColor: Color
44-
backgroundTintMode: Option<PorterDuff.Mode>
68+
backgroundTintMode: PorterDuff.Mode
4569
clickable: bool
4670
contentDescription: string
4771
contextClickable: bool
4872
layoutParameters: ViewGroup.LayoutParams
49-
onClick: Option<unit -> unit>
73+
onClick: unit -> unit
74+
onCreateContextMenu: IContextMenu -> IContextMenuContextMenuInfo -> unit
75+
onDrag: DragEvent -> bool
76+
onGenericMotion: MotionEvent -> bool
77+
onHover: MotionEvent -> bool
78+
onKey: Keycode -> KeyEvent -> bool
79+
onLongClick: unit -> bool
80+
onSystemUiVisibilityChange: StatusBarVisibility -> unit
81+
onTouch: MotionEvent -> bool
5082
padding: Padding
51-
pivotX: Single
52-
pivotY: Single
83+
pivot: Pivot
5384
soundEffectsEnabled: bool
85+
systemUiVisibility: StatusBarVisibility
5486
textAlignment: TextAlignment
5587
textDirection: TextDirection
5688
transitionName: string
57-
translationX: Single
58-
translationY: Single
59-
translationZ: Single
89+
translation: Translation
90+
verticalFadingEdgeEnabled: bool
91+
verticalScrollBarEnabled: bool
92+
verticalScrollbarPosition: ScrollbarPosition
6093
visibility: ViewStates
6194
}
6295

@@ -69,84 +102,260 @@ type ViewProps =
69102
member this.ContentDescription = this.contentDescription
70103
member this.ContextClickable = this.contextClickable
71104
member this.LayoutParameters = this.layoutParameters
72-
member this.OnClick = this.onClick
105+
member this.OnClick () = this.onClick ()
106+
member this.OnCreateContextMenu (menu, info) = this.onCreateContextMenu menu info
107+
member this.OnDrag de = this.onDrag de
108+
member this.OnGenericMotion me = this.onGenericMotion me
109+
member this.OnHover me = this.onHover me
110+
member this.OnKey (keyCode, keyEvent) = this.onKey keyCode keyEvent
111+
member this.OnLongClick () = this.onLongClick ()
112+
member this.OnSystemUiVisibilityChange sbv = this.onSystemUiVisibilityChange sbv
113+
member this.OnTouch me = this.onTouch me
73114
member this.Padding = this.padding
74-
member this.PivotX = this.pivotX
75-
member this.PivotY = this.pivotY
115+
member this.Pivot = this.pivot
76116
member this.SoundEffectsEnabled = this.soundEffectsEnabled
117+
member this.SystemUiVisibility = this.systemUiVisibility
77118
member this.TextAlignment = this.textAlignment
78119
member this.TextDirection = this.textDirection
79120
member this.TransitionName = this.transitionName
80-
member this.TranslationX = this.translationX
81-
member this.TranslationY = this.translationY
82-
member this.TranslationZ = this.translationZ
121+
member this.Translation = this.translation
122+
member this.VerticalFadingEdgeEnabled = this.verticalFadingEdgeEnabled
123+
member this.VerticalScrollBarEnabled = this.verticalScrollBarEnabled
124+
member this.VerticalScrollbarPosition = this.verticalScrollbarPosition
83125
member this.Visibility = this.visibility
84126

85127
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
86128
module View =
87129
type private OnClickListener (onClick) =
88130
inherit Java.Lang.Object ()
89131

132+
static let cache =
133+
new ConditionalWeakTable<unit -> unit, Android.Views.View.IOnClickListener>()
134+
135+
static member Create(onClick) =
136+
cache.GetValue(
137+
onClick,
138+
fun onClick -> (new OnClickListener(onClick)) :> View.IOnClickListener
139+
)
140+
90141
interface View.IOnClickListener with
91-
member this.OnClick view = onClick ()
142+
member this.OnClick view = onClick ()
143+
144+
type private OnCreateContextMenuListener (onCreateContextMenu) =
145+
inherit Java.Lang.Object ()
146+
147+
static let cache =
148+
new ConditionalWeakTable<
149+
IContextMenu * IContextMenuContextMenuInfo -> unit,
150+
Android.Views.View.IOnCreateContextMenuListener
151+
>()
152+
153+
static member Create(onCreateContextMenu) =
154+
cache.GetValue(
155+
onCreateContextMenu,
156+
fun onClick -> (new OnCreateContextMenuListener(onCreateContextMenu)) :> View.IOnCreateContextMenuListener
157+
)
158+
159+
interface View.IOnCreateContextMenuListener with
160+
member this.OnCreateContextMenu (menu, view, info) = onCreateContextMenu (menu, info)
161+
162+
type private OnDragListener (onDrag) =
163+
inherit Java.Lang.Object ()
164+
165+
static let cache =
166+
new ConditionalWeakTable<DragEvent -> bool, View.IOnDragListener>()
167+
168+
static member Create(onDrag) =
169+
cache.GetValue(
170+
onDrag,
171+
fun onDrag ->
172+
(new OnDragListener(onDrag)) :> View.IOnDragListener
173+
)
174+
175+
interface Android.Views.View.IOnDragListener with
176+
member this.OnDrag (view, motionEvent) = onDrag(motionEvent)
177+
178+
type private OnHoverListener (onHover) =
179+
inherit Java.Lang.Object ()
180+
181+
static let cache =
182+
new ConditionalWeakTable<MotionEvent -> bool, View.IOnHoverListener>()
183+
184+
static member Create(onHover) =
185+
cache.GetValue(
186+
onHover,
187+
fun onHover ->
188+
(new OnHoverListener(onHover)) :> View.IOnHoverListener
189+
)
190+
191+
interface Android.Views.View.IOnHoverListener with
192+
member this.OnHover (view, motionEvent) = onHover(motionEvent)
193+
194+
type private OnGenericMotionListener (onGenericMotion) =
195+
inherit Java.Lang.Object ()
196+
197+
static let cache =
198+
new ConditionalWeakTable<MotionEvent -> bool, View.IOnGenericMotionListener>()
199+
200+
static member Create(onGenericMotion) =
201+
cache.GetValue(
202+
onGenericMotion,
203+
fun onGenericMotion ->
204+
(new OnGenericMotionListener(onGenericMotion)) :> View.IOnGenericMotionListener
205+
)
206+
207+
interface Android.Views.View.IOnGenericMotionListener with
208+
member this.OnGenericMotion (view, motionEvent) = onGenericMotion(motionEvent)
209+
210+
type private OnKeyListener (onKey) =
211+
inherit Java.Lang.Object ()
212+
213+
static let cache =
214+
new ConditionalWeakTable<Keycode * KeyEvent -> bool, View.IOnKeyListener>()
215+
216+
static member Create(onKey) =
217+
cache.GetValue(
218+
onKey,
219+
fun onKey -> (new OnKeyListener(onKey)) :> View.IOnKeyListener
220+
)
221+
222+
interface View.IOnKeyListener with
223+
member this.OnKey (view, keyCode, keyEvent) = onKey (keyCode, keyEvent)
224+
225+
type private OnLongClickListener (onLongClick) =
226+
inherit Java.Lang.Object ()
227+
228+
static let cache =
229+
new ConditionalWeakTable<unit -> bool, Android.Views.View.IOnLongClickListener>()
230+
231+
static member Create(onClick) =
232+
cache.GetValue(
233+
onClick,
234+
fun onClick -> (new OnLongClickListener(onClick)) :> View.IOnLongClickListener
235+
)
236+
237+
interface View.IOnLongClickListener with
238+
member this.OnLongClick view = onLongClick ()
239+
240+
type private OnSystemUiVisibilityChangeListener (onSystemUiVisibilityChange) =
241+
inherit Java.Lang.Object ()
242+
243+
static let cache =
244+
new ConditionalWeakTable<StatusBarVisibility -> unit, View.IOnSystemUiVisibilityChangeListener>()
245+
246+
static member Create(onSystemUiVisibilityChange) =
247+
cache.GetValue(
248+
onSystemUiVisibilityChange,
249+
fun onSystemUiVisibilityChange ->
250+
(new OnSystemUiVisibilityChangeListener(onSystemUiVisibilityChange)) :> View.IOnSystemUiVisibilityChangeListener
251+
)
252+
253+
interface View.IOnSystemUiVisibilityChangeListener with
254+
member this.OnSystemUiVisibilityChange(sbv) = onSystemUiVisibilityChange sbv
255+
256+
type private OnTouchListener (onTouch) =
257+
inherit Java.Lang.Object ()
258+
259+
static let cache =
260+
new ConditionalWeakTable<MotionEvent -> bool, View.IOnTouchListener>()
261+
262+
static member Create(onTouch) =
263+
cache.GetValue(
264+
onTouch,
265+
fun onTcouh ->
266+
(new OnTouchListener(onTouch)) :> View.IOnTouchListener
267+
)
268+
269+
interface Android.Views.View.IOnTouchListener with
270+
member this.OnTouch (view, motionEvent) = onTouch(motionEvent)
92271

93272
let defaultProps = {
94273
alpha = 1.0f
95274
backgroundColor = Color.White
96-
backgroundTintMode = None
275+
backgroundTintMode = PorterDuff.Mode.SrcIn
97276
clickable = true
98277
contentDescription = ""
99278
contextClickable = true
100279
layoutParameters = new ViewGroup.LayoutParams(-2, -2)
101-
onClick = None
280+
onClick = fun () -> ()
281+
onCreateContextMenu = fun _ _ -> ()
282+
onDrag = fun _ -> false
283+
onGenericMotion = fun _ -> false
284+
onHover = fun _ -> false
285+
onKey = fun _ _ -> false
286+
onLongClick = fun () -> false
287+
onSystemUiVisibilityChange = fun _ -> ()
288+
onTouch = fun _ -> false
102289
padding = Unchecked.defaultof<Padding>
103-
pivotX = 0.0f
104-
pivotY = 0.0f
290+
pivot = Pivot(0.0f, 0.0f)
105291
soundEffectsEnabled = true
292+
systemUiVisibility = StatusBarVisibility.Visible
106293
textAlignment = TextAlignment.Inherit
107294
textDirection = TextDirection.Inherit
108295
transitionName = ""
109-
translationX = 0.0f
110-
translationY = 0.0f
111-
translationZ = 0.0f
296+
translation = Translation(0.0f, 0.0f, 0.0f)
297+
verticalFadingEdgeEnabled = false
298+
verticalScrollBarEnabled = false
299+
verticalScrollbarPosition = ScrollbarPosition.Default
112300
visibility = ViewStates.Visible
113301
}
114302

115303
let dispose (view: View) =
116-
view.SetOnKeyListener null
117-
view.SetOnDragListener null
118-
view.SetOnTouchListener null
119304
view.SetOnClickListener null
305+
view.SetOnCreateContextMenuListener null
306+
view.SetOnDragListener null
307+
view.SetOnGenericMotionListener null
120308
view.SetOnHoverListener null
309+
view.SetOnKeyListener null
121310
view.SetOnLongClickListener null
122-
view.SetOnContextClickListener null
123-
view.SetOnGenericMotionListener null
124-
view.SetOnCreateContextMenuListener null
125-
view.SetOnApplyWindowInsetsListener null
126311
view.SetOnSystemUiVisibilityChangeListener null
312+
view.SetOnTouchListener null
313+
314+
//view.SetOnContextClickListener null
315+
//view.SetOnApplyWindowInsetsListener null
316+
127317
()
128318

129319
let setProps (view: View) (props: IViewProps) =
320+
view.SetOnClickListener (OnClickListener.Create props.OnClick)
321+
view.SetOnCreateContextMenuListener (OnCreateContextMenuListener.Create props.OnCreateContextMenu)
322+
view.SetOnDragListener (OnDragListener.Create props.OnDrag)
323+
view.SetOnGenericMotionListener (OnGenericMotionListener.Create props.OnGenericMotion)
324+
view.SetOnHoverListener (OnHoverListener.Create props.OnHover)
325+
view.SetOnKeyListener (OnKeyListener.Create props.OnKey)
326+
view.SetOnLongClickListener (OnLongClickListener.Create props.OnLongClick)
327+
view.SetOnSystemUiVisibilityChangeListener (
328+
OnSystemUiVisibilityChangeListener.Create props.OnSystemUiVisibilityChange
329+
)
330+
view.SetOnTouchListener(OnTouchListener.Create props.OnTouch)
331+
332+
// FIXME: Might make sense to take a dependency on the compat package
333+
// and hide a lot of the underyling platform differences.
334+
//view.SetOnContextClickListener null
335+
//view.SetOnApplyWindowInsetsListener null
336+
130337
view.Alpha <- props.Alpha
131338
view.SetBackgroundColor props.BackgroundColor
132-
props.BackgroundTintMode |> Option.map (fun tint -> view.BackgroundTintMode <- tint) |> ignore
339+
view.BackgroundTintMode <- props.BackgroundTintMode
133340
view.Clickable <- props.Clickable
134341
view.ContentDescription <- props.ContentDescription
135342
view.ContextClickable <- props.ContextClickable
136343
view.LayoutParameters <- props.LayoutParameters
137344
view.SetPaddingRelative(props.Padding.start, props.Padding.top, props.Padding.end_, props.Padding.bottom)
138-
view.PivotX <- props.PivotX
139-
view.PivotY <- props.PivotY
345+
view.PivotX <- props.Pivot.x
346+
view.PivotY <- props.Pivot.y
140347
view.SoundEffectsEnabled <- props.SoundEffectsEnabled
348+
view.SystemUiVisibility <- props.SystemUiVisibility
141349
view.TextAlignment <- props.TextAlignment
142350
view.TextDirection <- props.TextDirection
143351
view.TransitionName <- props.TransitionName
144-
view.TranslationX <- props.TranslationX
145-
view.TranslationY <- props.TranslationY
146-
view.TranslationZ <- props.TranslationZ
352+
view.TranslationX <- props.Translation.x
353+
view.TranslationY <- props.Translation.y
354+
view.TranslationZ <- props.Translation.z
355+
view.VerticalFadingEdgeEnabled <- props.VerticalFadingEdgeEnabled
356+
view.VerticalScrollBarEnabled <- props.VerticalScrollBarEnabled
357+
view.VerticalScrollbarPosition <- props.VerticalScrollbarPosition
147358
view.Visibility <- props.Visibility
148359

149-
match props.OnClick with
150-
| Some onClick -> view.SetOnClickListener (new OnClickListener (onClick))
151-
| None -> view.SetOnClickListener null
360+
152361

0 commit comments

Comments
 (0)