-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathViews.fs
222 lines (192 loc) Β· 6.73 KB
/
Views.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
module SharpPoint.Views
open System.IO
open System.Net.Http
open Avalonia
open Avalonia.Controls
open Avalonia.Controls.ApplicationLifetimes
open Avalonia.FuncUI
open Avalonia.FuncUI.DSL
open Avalonia.FuncUI.Hosts
open Avalonia.FuncUI.Types
open Avalonia.Input
open Avalonia.Layout
open Avalonia.Media
open Avalonia.Media.Imaging
open Avalonia.Themes.Fluent
open SharpPoint.Domain
(* --- Global state --- *)
type private CurrentSlide =
| Initial
| Slide of index: int
type private GlobalState = { CurrentSlide: IWritable<CurrentSlide> }
let private state = { CurrentSlide = new State<CurrentSlide>(Initial) }
(* --- Custom hooks --- *)
type Deferred<'t> =
| NotStartedYet
| Pending
| Resolved of 't
| Failed of exn
type IComponentContext with
member this.useAsync<'signal>(init: Async<'signal>) : IWritable<Deferred<'signal>> =
let state = this.useState (Deferred.NotStartedYet, true)
this.useEffect (
handler = (fun _ ->
match state.Current with
| Deferred.NotStartedYet ->
state.Set Deferred.Pending
Async.StartImmediate (
async {
let! result = Async.Catch init
match result with
| Choice1Of2 value -> state.Set (Deferred.Resolved value)
| Choice2Of2 exn -> state.Set (Deferred.Failed exn)
}
)
| _ ->
()
),
triggers = [ EffectTrigger.AfterInit ]
)
state
(* --- Utils --- *)
let loadImage (url: string) =
async {
use httpClient = new HttpClient()
let! bytes =
url
|> httpClient.GetByteArrayAsync
|> Async.AwaitTask
use stream = new MemoryStream(bytes)
return new Bitmap(stream)
}
(* --- Views --- *)
let private initialSlide deck =
Component.create (
"initial-slide",
fun _ ->
StackPanel.create [
StackPanel.horizontalAlignment HorizontalAlignment.Center
StackPanel.verticalAlignment VerticalAlignment.Center
StackPanel.children [
TextBlock.create [
TextBlock.fontSize 72
TextBlock.fontWeight FontWeight.Bold
TextBlock.text deck.Title
TextBlock.textWrapping TextWrapping.Wrap
]
]
]
) :> IView
let private image url =
Component.create (
$"image-{url}",
fun ctx ->
let image =
loadImage url
|> ctx.useAsync
match image.Current with
| Deferred.Resolved bitmap ->
Image.create [
Image.source bitmap
Image.maxHeight 300
]
| Deferred.Failed e ->
TextBlock.create [
TextBlock.text $"{e.Message}"
TextBlock.foreground Brushes.Red
]
| Deferred.Pending | Deferred.NotStartedYet ->
ProgressBar.create [
ProgressBar.isEnabled true
ProgressBar.isIndeterminate true
]
)
let private slide (index: int) slide =
Component.create(
$"slide-{index}",
fun _ ->
StackPanel.create [
StackPanel.children [
if System.String.IsNullOrEmpty slide.Header |> not then
yield TextBlock.create [
TextBlock.fontSize 48
TextBlock.fontWeight FontWeight.Bold
TextBlock.text slide.Header
]
yield!
slide.Content
|> List.map (fun content ->
match content with
| Text text ->
TextBlock.create [
TextBlock.fontSize 24
TextBlock.text text
TextBlock.textWrapping TextWrapping.Wrap
] :> IView
| Image url ->
image url
)
]
]
) :> IView
let root deck =
Component(fun ctx ->
let currentSlide = ctx.usePassedRead state.CurrentSlide
match currentSlide.Current with
| Initial -> initialSlide deck
| Slide idx ->
deck.Slides
|> List.item idx
|> slide idx
)
(* --- Entrypoint --- *)
let hasSlideAvailableIn idx slides =
slides
|> List.tryItem idx
|> Option.isSome
type MainWindow(deck: Deck) as this =
inherit HostWindow()
do
base.Title <- "SharpPoint"
base.MinWidth <- 1280.0
base.MinHeight <- 720.0
this.Padding <- Thickness(10, 30, 10, 0)
this.Content <- root deck
this.ExtendClientAreaToDecorationsHint <- true
#if DEBUG
this.AttachDevTools()
#endif
override this.OnKeyDown event =
let current = state.CurrentSlide.Current
match current, event.Key with
| Slide 0, Key.Left ->
(* Go back to the initial slide *)
Initial
| Initial, Key.Right when List.isEmpty deck.Slides |> not ->
(* Go to the first slide (if any) *)
Slide 0
| Slide index, Key.Left when deck.Slides |> hasSlideAvailableIn (index - 1) ->
(* Go to the previous slide (if any) *)
index - 1 |> Slide
| Slide index, Key.Right when deck.Slides |> hasSlideAvailableIn (index + 1) ->
(* Go to the next slide (if any) *)
index + 1 |> Slide
| _ -> current
|> state.CurrentSlide.Set
type App(deck: Deck) =
inherit Application()
override this.Initialize() =
this.Styles.Add(FluentTheme(baseUri = null, Mode = FluentThemeMode.Dark))
override this.OnFrameworkInitializationCompleted() =
match this.ApplicationLifetime with
| :? IClassicDesktopStyleApplicationLifetime as desktopLifetime ->
let mainWindow = MainWindow(deck)
desktopLifetime.MainWindow <- mainWindow
| _ -> ()
let showPresentation deck =
AppBuilder
.Configure(fun _ -> App(deck))
.UsePlatformDetect()
.UseSkia()
.StartWithClassicDesktopLifetime([||])
|> ignore