This repository has been archived by the owner on Jun 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shell.fs
188 lines (163 loc) · 8.28 KB
/
Shell.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
namespace WinRTFs.Demo
/// This is the main module of your application
/// here you handle all of your child pages as well as their
/// messages and their updates, useful to update multiple parts
/// of your application, Please refer to the `view` function
/// to see how to handle different kinds of "*child*" controls
module Shell =
open System
open Elmish
open Avalonia
open Avalonia.Controls
open Avalonia.Input
open Avalonia.FuncUI.DSL
open Avalonia.FuncUI
open Avalonia.FuncUI.Builder
open Avalonia.FuncUI.Components.Hosts
open Avalonia.FuncUI.Elmish
open Windows.Networking.Connectivity
open Windows.System.Power
open WinRTFs.Core.Network
open WinRTFs.Core.Power
type State =
{ /// store the child state in your main state
AboutState: About.State
CounterState: Counter.State
NetworkConnectivityLevel: NetworkConnectivityLevel
PowerInformation: PowerInformation }
type Msg =
| AboutMsg of About.Msg
| CounterMsg of Counter.Msg
| NetworkChanged of ConnectionProfile
| BatteryStatusChanged of BatteryStatus
| PowerSupplyStatusChanged of PowerSupplyStatus
| EnergySaverStatusChanged of EnergySaverStatus
| RemainingChargePercentChanged of int32
| RemainingDischargeTimeChanged of TimeSpan
let init networkLevel powerinfo =
let aboutState, aboutCmd = About.init
let counterState = Counter.init
{ AboutState = aboutState
CounterState = counterState
NetworkConnectivityLevel = networkLevel
PowerInformation = powerinfo }, Cmd.batch [ aboutCmd ]
/// If your children controls don't emit any commands
/// in the init function, you can just return Cmd.none
/// otherwise, you can use a batch operation on all of them
/// you can add more init commands as you need
let update (msg: Msg) (state: State): State * Cmd<_> =
match msg with
| AboutMsg bpmsg ->
let aboutState, cmd = About.update bpmsg state.AboutState
{ state with AboutState = aboutState }, Cmd.map AboutMsg cmd
| CounterMsg countermsg ->
/// map the message to the kind of message
/// your child control needs to handle
let counterMsg = Counter.update countermsg state.CounterState
{ state with CounterState = counterMsg }, Cmd.none
| NetworkChanged profile -> { state with NetworkConnectivityLevel = getNetworkLevel profile }, Cmd.none
| BatteryStatusChanged status ->
{ state with PowerInformation = { state.PowerInformation with BatteryStatus = status } }, Cmd.none
| PowerSupplyStatusChanged status ->
{ state with PowerInformation = { state.PowerInformation with PowerSupplyStatus = status } }, Cmd.none
| EnergySaverStatusChanged status ->
{ state with PowerInformation = { state.PowerInformation with EnergySaverStatus = status } }, Cmd.none
| RemainingChargePercentChanged charge ->
{ state with PowerInformation = { state.PowerInformation with RemainingCharge = charge } }, Cmd.none
| RemainingDischargeTimeChanged timeLeft ->
{ state with PowerInformation = { state.PowerInformation with RemainingRuntime = timeLeft } }, Cmd.none
/// map the message to the kind of message
/// your child control needs to handle
let private networkMenu state =
Menu.create
[ Menu.dock Dock.Bottom
Menu.viewItems
[ MenuItem.create [ MenuItem.header (stringifyConnectivity state.NetworkConnectivityLevel) ] ] ]
let private powerMenu state =
Menu.create
[ Menu.dock Dock.Bottom
Menu.viewItems
[ MenuItem.create
[ MenuItem.header (stringifyEnergySaverStatus state.PowerInformation.EnergySaverStatus) ]
MenuItem.create
[ MenuItem.header (stringifyRemainingCharge state.PowerInformation.RemainingCharge) ]
MenuItem.create
[ MenuItem.header (stringifyRemainingTime state.PowerInformation.RemainingRuntime) ]
MenuItem.create [ MenuItem.header (stringifyBatteryStatus state.PowerInformation.BatteryStatus) ]
MenuItem.create
[ MenuItem.header (stringifyPowerSupplyStatus state.PowerInformation.PowerSupplyStatus) ] ] ]
let view (state: State) (dispatch) =
DockPanel.create
[ DockPanel.children
[ powerMenu state
networkMenu state
TabControl.create
[ TabControl.tabStripPlacement Dock.Top
TabControl.dock Dock.Top
TabControl.viewItems
[ TabItem.create
[ TabItem.header "Counter Sample"
TabItem.content (Counter.view state.CounterState (CounterMsg >> dispatch)) ]
TabItem.create
[ TabItem.header "Media APIs"
TabItem.content (ViewBuilder.Create<Media.Host>([])) ]
TabItem.create
[ TabItem.header "About"
TabItem.content (About.view state.AboutState (AboutMsg >> dispatch)) ] ] ] ] ]
/// This is the main window of your application
/// you can do all sort of useful things here like setting heights and widths
/// as well as attaching your dev tools that can be super useful when developing with
/// Avalonia
type MainWindow() as this =
inherit HostWindow()
do
base.Title <- "Full App"
base.Width <- 800.0
base.Height <- 600.0
base.MinWidth <- 800.0
base.MinHeight <- 600.0
let networkStatus state =
let sub dispatch =
NetworkInformation.NetworkStatusChanged.AddHandler
(fun _ -> dispatch (NetworkChanged(NetworkInformation.GetInternetConnectionProfile())))
|> ignore
Cmd.ofSub sub
let batteryStatus state =
let sub dispatch =
PowerManager.BatteryStatusChanged.Subscribe
(fun _ -> dispatch (BatteryStatusChanged PowerManager.BatteryStatus)) |> ignore
Cmd.ofSub sub
let powerSupplyStatus state =
let sub dispatch =
PowerManager.PowerSupplyStatusChanged.Subscribe
(fun _ -> dispatch (PowerSupplyStatusChanged PowerManager.PowerSupplyStatus)) |> ignore
Cmd.ofSub sub
let energySaverStatus state =
let sub dispatch =
PowerManager.EnergySaverStatusChanged.Subscribe
(fun _ -> dispatch (EnergySaverStatusChanged PowerManager.EnergySaverStatus)) |> ignore
Cmd.ofSub sub
let chargeRemaining state =
let sub dispatch =
PowerManager.RemainingChargePercentChanged.Subscribe
(fun _ -> dispatch (RemainingChargePercentChanged PowerManager.RemainingChargePercent))
|> ignore
Cmd.ofSub sub
let runtimeRemaining state =
let sub dispatch =
PowerManager.RemainingDischargeTimeChanged.Subscribe
(fun _ -> dispatch (RemainingDischargeTimeChanged PowerManager.RemainingDischargeTime))
|> ignore
Cmd.ofSub sub
let networklevel = getNetworkLevel (NetworkInformation.GetInternetConnectionProfile())
let powerinfo = getPowerInfo()
printProfiles()
Program.mkProgram (fun () -> init networklevel powerinfo) update view
|> Program.withHost this
|> Program.withSubscription networkStatus
|> Program.withSubscription batteryStatus
|> Program.withSubscription powerSupplyStatus
|> Program.withSubscription energySaverStatus
|> Program.withSubscription chargeRemaining
|> Program.withSubscription runtimeRemaining
|> Program.run