Skip to content

Commit

Permalink
remember last ui status and restore on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
Yatao Li committed Nov 1, 2019
1 parent 05d6721 commit f4e1ed5
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 30 deletions.
9 changes: 8 additions & 1 deletion Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ open System
open System.IO
open getopt
open Shell
open common
open MessagePack.Formatters

// Avalonia configuration, don't remove; also used by visual designer.
Expand Down Expand Up @@ -105,11 +106,17 @@ let main(args: string[]) =
let cfg = config.load()
let cwd = Environment.CurrentDirectory |> Path.GetFullPath
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 -> States.background_composition <- comp; None
|> ignore

let mainwin = new MainWindowViewModel(workspace)
lifetime.MainWindow <- MainWindow(DataContext = mainwin)
let ret = lifetime.Start(args)

config.save cfg (int mainwin.X) (int mainwin.Y) mainwin.Width mainwin.Height mainwin.WindowState
config.save cfg (int mainwin.X) (int mainwin.Y) (mainwin.Width) (mainwin.Height) (mainwin.WindowState) (States.backgroundCompositionToString States.background_composition) mainwin.CustomTitleBar
ret
| Error ex ->
let crash = new CrashReportViewModel(ex)
Expand Down
3 changes: 3 additions & 0 deletions ViewModels/MainWindowViewModel.fs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ type MainWindowViewModel(cfg: config.ConfigObject.Workspace option, ?_maingrid:
match Enum.TryParse<WindowState>(cfg.Mainwin.State) with
| true, v -> this.WindowState <- v
| _ -> ()
match cfg.Mainwin.CustomTitleBar with
| Some true -> m_customTitleBar <- true
| _ -> ()
| None -> ()
this.Watch [
States.Register.Notify "ToggleFullScreen" (fun [| Integer32(gridid) |] -> toggleFullScreen gridid )
Expand Down
33 changes: 10 additions & 23 deletions Views/MainWindow.xaml.fs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,17 @@ type MainWindow() as this =

let mutable m_bgcolor: Color = Color()
let mutable m_bgopacity: float = 1.0
let mutable m_bgblur = false
let mutable m_bgcomp = States.NoComposition
let mutable m_bgacrylic = false

let configBackground() =
m_bgcomp <- States.background_composition
m_bgopacity <- States.background_opacity
let comp =
if m_bgacrylic then ui.AdvancedBlur(m_bgopacity, m_bgcolor)
elif m_bgblur then ui.GaussianBlur(m_bgopacity, m_bgcolor)
else ui.SolidBackground m_bgcolor
match m_bgcomp with
| States.Acrylic -> ui.AdvancedBlur(m_bgopacity, m_bgcolor)
| States.Blur -> ui.GaussianBlur(m_bgopacity, m_bgcolor)
| _ -> ui.SolidBackground m_bgcolor
trace "mainwindow" "configBackground: %A" comp
ui.SetWindowBackgroundComposition this comp

Expand Down Expand Up @@ -130,25 +133,8 @@ type MainWindow() as this =
this.Bind(XProp, Binding("X", BindingMode.TwoWay))
this.Bind(YProp, Binding("Y", BindingMode.TwoWay))

States.Register.Watch "background.composition" (fun () ->
match States.background_composition.ToLower() with
| "blur" ->
m_bgblur <- true
m_bgacrylic <- false
| "acrylic" ->
m_bgblur <- false
m_bgacrylic <- true
| "none" | _ ->
m_bgblur <- false
m_bgacrylic <- false
configBackground()
)

States.Register.Watch "background.opacity" (fun () ->
m_bgopacity <- States.background_opacity
configBackground()
)

States.Register.Watch "background.composition" configBackground
States.Register.Watch "background.opacity" configBackground
States.Register.Notify "DrawFPS" (fun [| Bool(v) |] ->
trace "mainwindow" "DrawFPS: %A" v
this.Renderer.DrawFps <- v)
Expand Down Expand Up @@ -205,6 +191,7 @@ type MainWindow() as this =
let mutable deltaX = 0
let mutable deltaY = 0
this.ViewModel <- ctx
toggleTitleBar ctx.CustomTitleBar

trace "mainwindow" "set position: %d, %d" pos.X pos.Y
this.Position <- pos
Expand Down
16 changes: 14 additions & 2 deletions config.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ let sample_config = """
[
{
"workspace": [
{
"path": "C:\foo\bar",
"mainwin": {
"x": 1030,
"y": 200,
"w": 800.00,
"h": 600.00,
"state": "Normal",
"BackgroundComposition": "acrylic",
"CustomTitleBar": false
}
},
{
"path": "C:\foo\bar",
"mainwin": {
Expand Down Expand Up @@ -56,10 +68,10 @@ let load() =
cfg
with _ -> ConfigObject.Parse("{}")

let save (cfg: ConfigObject.Root) (x: int) (y: int) (w: float) (h: float) (state: WindowState) =
let save (cfg: ConfigObject.Root) (x: int) (y: int) (w: float) (h: float) (state: WindowState) (composition: string) (customTitleBar: bool) =
let dict = cfg.Workspace |> Array.map (fun ws -> (ws.Path, ws)) |> Map.ofArray
let cwd = Environment.CurrentDirectory |> Path.GetFullPath
let ws = ConfigObject.Workspace(cwd, ConfigObject.Mainwin(x, y, int w, int h, state.ToString()))
let ws = ConfigObject.Workspace(cwd, ConfigObject.Mainwin(x, y, int w, int h, state.ToString(), Some composition, Some customTitleBar))
let dict = dict.Add(cwd, ws)
let cfg = ConfigObject.Root(dict |> Map.toArray |> Array.map snd, cfg.Logging)
try File.WriteAllText(configfile, cfg.ToString())
Expand Down
2 changes: 1 addition & 1 deletion model.fs
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ let Start opts =
States.Register.Bool "ui.termcolors"
States.Register.Bool "ui.hlstate"

States.Register.String "background.composition"
States.Register.Prop<States.BackgroundComposition> States.parseBackgroundComposition "background.composition"
States.Register.Float "background.opacity"
States.Register.Float "background.altopacity"

Expand Down
24 changes: 22 additions & 2 deletions states.fs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,13 @@ let mutable ui_messages = false
let mutable ui_termcolors = false
let mutable ui_hlstate = false

type BackgroundComposition =
| NoComposition
| Blur
| Acrylic

// background
let mutable background_composition = ""
let mutable background_composition = NoComposition
let mutable background_opacity = 1.0
let mutable background_altopacity = 1.0

Expand Down Expand Up @@ -144,7 +149,6 @@ module private Helper =
error "states" "The property %s is not found" name
None


let parseHintLevel (v: obj) =
match v with
| String v ->
Expand Down Expand Up @@ -176,6 +180,22 @@ let parseLineHeightOption (v: obj) =
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
| _ -> None
| _ -> None

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

let Shutdown code = _appLifetime.Shutdown code

let msg_dispatch =
Expand Down
2 changes: 1 addition & 1 deletion theme.fs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ let GetDrawAttrs hlid =
fg <- bg
bg <- tmp

if (States.background_composition = "acrylic" || States.background_composition = "blur") then
if (States.background_composition <> States.BackgroundComposition.NoComposition) then
let alpha =
if bg = default_bg then 0uy
else byte(States.background_altopacity * 255.0)
Expand Down

0 comments on commit f4e1ed5

Please sign in to comment.