Skip to content

Commit

Permalink
add recent projects to no project menu
Browse files Browse the repository at this point in the history
  • Loading branch information
tomcl committed Apr 22, 2022
1 parent bfb27a7 commit fbbb4c6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 17 deletions.
65 changes: 48 additions & 17 deletions src/Renderer/UI/FileMenuView.fs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ open DrawModelType
open Sheet.SheetInterface

open System

module Constants =
let numberOfRecentProjects: int = 5
let maxDisplayedPathLengthInRecentProjects: int = 60
//--------------------------------------------------------------------------------------------//
//--------------------------------------------------------------------------------------------//
//---------------------Code for CanvasState comparison and FILE BACKUP------------------------//
Expand Down Expand Up @@ -722,7 +726,35 @@ let rec resolveComponentOpenPopup
resolveComponentOpenPopup pPath (autoComp::components) rLst model dispatch
| OkComp comp ::rLst ->
resolveComponentOpenPopup pPath (comp::components) rLst model dispatch


let addToRecents path recents =
recents
|> Option.defaultValue []
|> List.filter ((<>) path)
|> List.truncate Constants.numberOfRecentProjects
|> List.insertAt 0 path
|> Some

/// open an rxisting porject from its path
let openProjectFromPath (path:string) model dispatch =
dispatch <| SetUserData {
model.UserData with
LastUsedDirectory = Some path;
RecentProjects = addToRecents path model.UserData.RecentProjects
}
dispatch (ExecFuncAsynch <| fun () ->
traceIf "project" (fun () -> "loading files")
match loadAllComponentFiles path with
| Error err ->
log err
displayFileErrorNotification err dispatch
| Ok componentsToResolve ->
traceIf "project" (fun () -> "resolving popups...")

resolveComponentOpenPopup path [] componentsToResolve model dispatch
traceIf "project" (fun () -> "project successfully opened.")

Elmish.Cmd.none)

/// open an existing project
let private openProject model dispatch =
Expand All @@ -735,21 +767,9 @@ let private openProject model dispatch =
| _ -> model.UserData.LastUsedDirectory
match askForExistingProjectPath dirName with
| None -> () // User gave no path.
| Some path ->
dispatch <| SetUserData {model.UserData with LastUsedDirectory = Some path}
dispatch (ExecFuncAsynch <| fun () ->
traceIf "project" (fun () -> "loading files")
match loadAllComponentFiles path with
| Error err ->
log err
displayFileErrorNotification err dispatch
| Ok componentsToResolve ->
traceIf "project" (fun () -> "resolving popups...")

resolveComponentOpenPopup path [] componentsToResolve model dispatch
traceIf "project" (fun () -> "project successfully opened.")
| Some path -> openProjectFromPath path model dispatch


Elmish.Cmd.none)



Expand All @@ -761,12 +781,23 @@ let viewNoProjectMenu model dispatch =
Menu.Item.li
[ Menu.Item.IsActive false
Menu.Item.OnClick action ] [ str label ]

let recentsList =
model.UserData
|> (fun ud -> ud.RecentProjects)
|> Option.defaultValue []
|> List.map (fun path ->
menuItem
(cropToLength Constants.maxDisplayedPathLengthInRecentProjects false path)
(fun _ -> openProjectFromPath path model dispatch))

let initialMenu =
Menu.menu []
[ Menu.list []
[ menuItem "New project" (fun _ -> newProject model dispatch)
menuItem "Open project" (fun _ -> openProject model dispatch) ]
([ menuItem "New project" (fun _ -> newProject model dispatch)
menuItem "Open project" (fun _ -> openProject model dispatch)]
@ (if recentsList <> [] then [hr []] else [])
@ recentsList)
]

match model.CurrentProj with
Expand Down
1 change: 1 addition & 0 deletions src/Renderer/UI/MainView.fs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ let init() = {
ArrowDisplay = true
UserAppDir = None
LastUsedDirectory = None
RecentProjects = None
}
LastChangeCheckTime = 0.
// Diagram = new Draw2dWrapper()
Expand Down
1 change: 1 addition & 0 deletions src/Renderer/UI/ModelType.fs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ type UserData = {
/// Where to save the persistent app data
UserAppDir : string option
LastUsedDirectory: string option
RecentProjects: string list option
ArrowDisplay: bool
WireType: DrawModelType.BusWireT.WireType
}
Expand Down

0 comments on commit fbbb4c6

Please sign in to comment.