Skip to content

Commit fdfc477

Browse files
authored
feat: Add onReceive modifier (#293)
* feat: Add onReceive modifier * improve samples
1 parent 73ab7ad commit fdfc477

File tree

7 files changed

+862
-370
lines changed

7 files changed

+862
-370
lines changed

samples/Component/CounterApp/App.fs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,12 @@ module App =
1717
let! step = State(1)
1818

1919
let timer =
20-
DispatcherTimer(
21-
TimeSpan.FromMilliseconds(1000.),
22-
DispatcherPriority.Default,
23-
(fun _ _ ->
24-
if timerOn.Current then
25-
count.Set(count.Current + step.Current))
26-
)
20+
DispatcherTimer(Interval = TimeSpan.FromSeconds(1.), IsEnabled = timerOn.Current)
2721

2822
(VStack() {
29-
TextBlock($"%d{count.Current}").centerText()
23+
TextBlock($"%d{count.Current}")
24+
.centerText()
25+
.onReceive(timer.Tick, (fun _ -> count.Set(count.Current + step.Current)))
3026

3127
Button("Increment", (fun _ -> count.Set(count.Current + step.Current)))
3228
.centerHorizontal()
@@ -37,12 +33,7 @@ module App =
3733
(HStack() {
3834
TextBlock("Timer").centerVertical()
3935

40-
ToggleSwitch(
41-
timerOn.Current,
42-
fun on ->
43-
timerOn.Set(on)
44-
if on then timer.Start() else timer.Stop()
45-
)
36+
ToggleSwitch(timerOn.Current, (fun on -> timerOn.Set(on)))
4637

4738
})
4839
.margin(20.)

samples/Component/DrawingApp/App.fs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,12 @@ module SizePicker =
6262
module Setting =
6363
let view (color, size) =
6464
Component("Setting") {
65-
View
66-
.Border(
67-
Dock(false) {
68-
ColorPicker.view(color).dock(Dock.Left)
69-
SizePicker.view(size).dock(Dock.Right)
70-
}
71-
)
65+
Border(
66+
Dock(false) {
67+
ColorPicker.view(color).dock(Dock.Left)
68+
SizePicker.view(size).dock(Dock.Right)
69+
}
70+
)
7271
.dock(Dock.Bottom)
7372
.margin(5.0)
7473
.padding(5.0)

samples/Component/MultiWindow/App.fs

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,12 @@ module FirstWindow =
1717
let! step = State(1)
1818

1919
let timer =
20-
DispatcherTimer(
21-
TimeSpan.FromMilliseconds(1000.),
22-
DispatcherPriority.Default,
23-
(fun _ _ ->
24-
if timerOn.Current then
25-
count.Set(count.Current + step.Current))
26-
)
20+
DispatcherTimer(Interval = TimeSpan.FromSeconds(1.), IsEnabled = timerOn.Current)
2721

2822
(VStack() {
29-
TextBlock($"%d{count.Current}").centerText()
23+
TextBlock($"%d{count.Current}")
24+
.centerText()
25+
.onReceive(timer.Tick, (fun _ -> count.Set(count.Current + step.Current)))
3026

3127
Button("Increment", (fun _ -> count.Set(count.Current + step.Current)))
3228
.centerHorizontal()
@@ -37,12 +33,7 @@ module FirstWindow =
3733
(HStack() {
3834
TextBlock("Timer").centerVertical()
3935

40-
ToggleSwitch(
41-
timerOn.Current,
42-
fun on ->
43-
timerOn.Set(on)
44-
if on then timer.Start() else timer.Stop()
45-
)
36+
ToggleSwitch(timerOn.Current, (fun on -> timerOn.Set(on)))
4637

4738
})
4839
.margin(20.)
@@ -82,16 +73,12 @@ module SecondWindow =
8273
let! step = State(1)
8374

8475
let timer =
85-
DispatcherTimer(
86-
TimeSpan.FromMilliseconds(1000.),
87-
DispatcherPriority.Default,
88-
(fun _ _ ->
89-
if timerOn.Current then
90-
count.Set(count.Current + step.Current))
91-
)
76+
DispatcherTimer(Interval = TimeSpan.FromSeconds(1.), IsEnabled = timerOn.Current)
9277

9378
(VStack() {
94-
TextBlock($"%d{count.Current}").centerText()
79+
TextBlock($"%d{count.Current}")
80+
.centerText()
81+
.onReceive(timer.Tick, (fun _ -> count.Set(count.Current + step.Current)))
9582

9683
Button("Increment", (fun _ -> count.Set(count.Current + step.Current)))
9784
.centerHorizontal()
@@ -102,12 +89,7 @@ module SecondWindow =
10289
(HStack() {
10390
TextBlock("Timer").centerVertical()
10491

105-
ToggleSwitch(
106-
timerOn.Current,
107-
fun on ->
108-
timerOn.Set(on)
109-
if on then timer.Start() else timer.Stop()
110-
)
92+
ToggleSwitch(timerOn.Current, (fun on -> timerOn.Set(on)))
11193

11294
})
11395
.margin(20.)

0 commit comments

Comments
 (0)