Skip to content

Commit

Permalink
refactor. merge non-state functionality (rpc) into model
Browse files Browse the repository at this point in the history
  • Loading branch information
Yatao Li committed Apr 23, 2021
1 parent c275ab0 commit 4c0f090
Show file tree
Hide file tree
Showing 14 changed files with 307 additions and 300 deletions.
7 changes: 4 additions & 3 deletions Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ open Avalonia.Controls.ApplicationLifetimes
open MessagePack
open MessagePack.Resolvers

open def
open getopt
open common
open shell
Expand Down Expand Up @@ -64,7 +65,7 @@ let startMainWindow app serveropts =
let workspace = cfg.Workspace |> Array.tryFind(fun w -> w.Path = cwd)
workspace
>>= fun workspace -> workspace.Mainwin.BackgroundComposition
>>= fun comp -> states.parseBackgroundComposition(box comp)
>>= fun comp -> parseBackgroundComposition(box comp)
>>= fun comp -> states.background_composition <- comp; None
|> ignore

Expand Down Expand Up @@ -94,14 +95,14 @@ let startMainWindow app serveropts =
app <| mainwin
boundcheck()
let x, y, w, h = (int mainwinVM.X), (int mainwinVM.Y), (int mainwinVM.Width), (int mainwinVM.Height)
config.save cfg x y w h (mainwinVM.WindowState.ToString()) (states.backgroundCompositionToString states.background_composition) mainwinVM.CustomTitleBar
config.save cfg x y w h (mainwinVM.WindowState.ToString()) (backgroundCompositionToString states.background_composition) mainwinVM.CustomTitleBar
0

let startCrashReportWindow app ex =
let app = app()
trace "displaying crash dialog"
trace "exception: %O" ex
let code, msgs = states.get_crash_info()
let code, msgs = model.get_crash_info()
let crash = new CrashReportViewModel(ex, code, msgs)
let win = new CrashReport(DataContext = crash)
// there may be messages already posted into the sync context,
Expand Down
3 changes: 1 addition & 2 deletions Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"profiles": {
"fvim": {
"commandName": "Project",
"commandLineArgs": "--debug-multigrid"
"commandName": "Project"
},
"multigrid": {
"commandName": "Project",
Expand Down
9 changes: 5 additions & 4 deletions ViewModels/FrameViewModel.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

open log
open common
open ui
open model

open ReactiveUI
open Avalonia
open Avalonia.Controls
open System
open System.IO
open ui
open Avalonia.Media.Imaging
open Avalonia.Media
open Avalonia.Layout
Expand Down Expand Up @@ -94,9 +95,9 @@ type FrameViewModel(cfg: config.ConfigObject.Workspace option, ?_maingrid: GridV
| _ -> ()
| None -> ()
this.Watch [
states.register.notify "ToggleFullScreen" (fun [| Integer32(gridid) |] -> toggleFullScreen gridid )
states.register.notify "CustomTitleBar" (fun [| Bool(v) |] -> this.CustomTitleBar <- v )
states.register.watch "background.image" (fun _ -> updateBackgroundImage())
rpc.register.notify "ToggleFullScreen" (fun [| Integer32(gridid) |] -> toggleFullScreen gridid )
rpc.register.notify "CustomTitleBar" (fun [| Bool(v) |] -> this.CustomTitleBar <- v )
rpc.register.watch "background.image" (fun _ -> updateBackgroundImage())
]
model.OnFrameReady this

Expand Down
3 changes: 2 additions & 1 deletion ViewModels/GridViewModel.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ open FSharp.Control.Reactive

open System
open System.Collections.ObjectModel
open model

#nowarn "0025"

Expand Down Expand Up @@ -429,7 +430,7 @@ type GridViewModel(_gridid: int, ?parent: GridViewModel, ?_gridsize: GridSize, ?
theme.cursoren_ev.Publish
|> Observable.subscribe setCursorEnabled

states.register.watch "font" fontConfig
rpc.register.watch "font" fontConfig

this.ObservableForProperty(fun x -> x.IsFocused)
|> Observable.subscribe (fun x ->
Expand Down
3 changes: 2 additions & 1 deletion Views/Cursor.xaml.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ open log
open ui
open common
open def
open model

open Avalonia
open Avalonia.Animation
Expand Down Expand Up @@ -138,7 +139,7 @@ type Cursor() as this =
|> Observable.subscribe(fun _ ->
setCursorAnimation()
this.InvalidateVisual())
states.register.watch "cursor" setCursorAnimation
rpc.register.watch "cursor" setCursorAnimation
]
cursor_timer.Start()
AvaloniaXamlLoader.Load(this)
Expand Down
15 changes: 8 additions & 7 deletions Views/Frame.xaml.fs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ open Avalonia.Interactivity
open Avalonia.VisualTree

open Avalonia.Diagnostics
open model

#nowarn "0025"

Expand All @@ -33,16 +34,16 @@ type Frame() as this =

let mutable m_bgcolor: Color = Color()
let mutable m_bgopacity: float = 1.0
let mutable m_bgcomp = states.NoComposition
let mutable m_bgcomp = NoComposition

let configBackground() =
m_bgcomp <- states.background_composition
m_bgopacity <- states.background_opacity
let comp =
match m_bgcomp with
| states.Acrylic -> ui.AdvancedBlur(m_bgopacity, m_bgcolor)
| states.Blur -> ui.GaussianBlur(m_bgopacity, m_bgcolor)
| states.Transparent -> ui.TransparentBackground(m_bgopacity, m_bgcolor)
| Acrylic -> ui.AdvancedBlur(m_bgopacity, m_bgcolor)
| Blur -> ui.GaussianBlur(m_bgopacity, m_bgcolor)
| Transparent -> ui.TransparentBackground(m_bgopacity, m_bgcolor)
| _ -> ui.SolidBackground(m_bgopacity, m_bgcolor)
trace "frame" "configBackground: %A" comp
ui.SetWindowBackgroundComposition this comp
Expand Down Expand Up @@ -135,9 +136,9 @@ type Frame() as this =
this.GotFocus.Subscribe (fun _ -> model.OnFocusGained())
this.LostFocus.Subscribe (fun _ -> model.OnFocusLost())

states.register.watch "background.composition" configBackground
states.register.watch "background.opacity" configBackground
states.register.notify "DrawFPS" (fun [| Bool(v) |] ->
rpc.register.watch "background.composition" configBackground
rpc.register.watch "background.opacity" configBackground
rpc.register.notify "DrawFPS" (fun [| Bool(v) |] ->
trace "mainwindow" "DrawFPS: %A" v
this.Renderer.DrawFps <- v)

Expand Down
3 changes: 2 additions & 1 deletion Views/Grid.xaml.fs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module private GridHelper =
FVim.log.trace ("editor #" + nr) fmt

open GridHelper
open model

type Grid() as this =
inherit Canvas()
Expand Down Expand Up @@ -339,7 +340,7 @@ type Grid() as this =
this.Bind(Canvas.LeftProperty, Binding("X"))
this.Bind(Canvas.TopProperty, Binding("Y"))

states.register.watch "font" (fun () ->
rpc.register.watch "font" (fun () ->
if grid_vm <> Unchecked.defaultof<_> then
grid_vm.MarkAllDirty()
this.InvalidateVisual())
Expand Down
100 changes: 100 additions & 0 deletions def.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ open common
open Avalonia.Media
open System
open System.Threading.Tasks
open SkiaSharp
open Avalonia.Layout

let inline private trace fmt = trace "def" fmt

Expand Down Expand Up @@ -687,3 +689,101 @@ let parse_redrawcmd (x: obj) =
//| C("suspend", _) ->
//| C("update_menu", _) ->


// FVim-specific types

type LineHeightOption =
| Absolute of float
| Add of float
| Default

type BackgroundComposition =
| NoComposition
| Transparent
| Blur
| Acrylic


let parseHintLevel (v: obj) =
match v with
| String v ->
match v.ToLower() with
| "none" -> Some SKPaintHinting.NoHinting
| "slight" -> Some SKPaintHinting.Slight
| "normal" -> Some SKPaintHinting.Normal
| "full" -> Some SKPaintHinting.Full
| _ -> None
| _ -> None

let parseFontWeight (v: obj) =
match v with
| Integer32 v -> Some(LanguagePrimitives.EnumOfValue v)
| _ -> None

let parseLineHeightOption (v: obj) =
match v with
| String v ->
if v.StartsWith("+") then
Some(Add(float v.[1..]))
elif v.StartsWith("-") then
Some(Add(-float v.[1..]))
elif v.ToLowerInvariant() = "default" then
Some Default
else
let v = float v
if v > 0.0 then Some(Absolute v)
else None
| _ -> None

let parseBackgroundComposition (v: obj) =
match v with
| String v ->
match v.ToLower() with
| "none" -> Some NoComposition
| "blur" -> Some Blur
| "acrylic" -> Some Acrylic
| "transparent" -> Some Transparent
| _ -> None
| _ -> None

let parseStretch (v: obj) =
match v with
| String v ->
match v.ToLower() with
| "none" -> Some Stretch.None
| "fill" -> Some Stretch.Fill
| "uniform" -> Some Stretch.Uniform
| "uniformfill" -> Some Stretch.UniformToFill
| _ -> None
| _ -> None

let parseHorizontalAlignment (v: obj) =
match v with
| String v ->
match v.ToLower() with
| "left" -> Some HorizontalAlignment.Left
| "center" -> Some HorizontalAlignment.Center
| "right" -> Some HorizontalAlignment.Right
| "stretch" -> Some HorizontalAlignment.Stretch
| _ -> None
| _ -> None

let parseVerticalAlignment (v: obj) =
match v with
| String v ->
match v.ToLower() with
| "top" -> Some VerticalAlignment.Top
| "center" -> Some VerticalAlignment.Center
| "bottom" -> Some VerticalAlignment.Bottom
| "stretch" -> Some VerticalAlignment.Stretch
| _ -> None
| _ -> None

let backgroundCompositionToString =
function
| NoComposition -> "none"
| Blur -> "blur"
| Acrylic -> "acrylic"
| Transparent -> "transparent"


2 changes: 1 addition & 1 deletion fvim.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
<Compile Include="daemon.fs" />
<Compile Include="shell.fs" />
<Compile Include="states.fs" />
<Compile Include="neovim.fs" />
<Compile Include="ui.fs" />
<Compile Include="theme.fs" />
<Compile Include="neovim.fs" />
<Compile Include="input.fs" />
<Compile Include="model.fs" />
<Compile Include="ViewModels\ViewModelBase.fs" />
Expand Down
Loading

0 comments on commit 4c0f090

Please sign in to comment.