module ParasitemiaUI.GUI open System open System.IO open System.Linq open System.Windows open System.Windows.Media open System.Windows.Markup open System.Windows.Shapes open System.Windows.Controls open System.Diagnostics open Microsoft.Win32 // For the common dialogs. open Emgu.CV.WPF open Logger open ParasitemiaCore.Config open ParasitemiaCore.Utils open Types let run (defaultConfig: Config) (fileToOpen: string option) = let app = new Application() let win = Views.MainWindow() let state = State.State(defaultConfig) let mutable currentScale = 1. let mutable displayHealthy = false let warningBelowNumberOfRBC = 1000 // Initializations. let canvasCurrentImageColor = win.canvasCurrentImage.Background win.menuHightlightRBC.IsChecked <- displayHealthy // Utils. let extractRBCPreview (img: Emgu.CV.Image) (rbc: RBC) : Emgu.CV.Image = let rbcWidth = rbc.size.Width let rbcHeight = rbc.size.Height let x = rbc.center.X - rbcWidth / 2. |> roundInt let y = rbc.center.Y - rbcHeight / 2. |> roundInt let w = roundInt rbcWidth let h = roundInt rbcHeight img.GetSubRect(System.Drawing.Rectangle(System.Drawing.Point((if x < 0 then 0 else x), (if y < 0 then 0 else y)), System.Drawing.Size((if x + w >= img.Width then img.Width - x else w), (if y + h >= img.Height then img.Height - y else h)))) let setRBCFrameStyle (srcImg: SourceImage) (rbc: RBC) (frame: Views.RBCFrame) = frame.Opacity <- if displayHealthy || rbc.setManually || rbc.infected then 1. else 0. let color = if rbc.infected then srcImg.InfectedRBCColor else srcImg.HealthyRBCColor frame.manuallyAdded.Visibility <- if rbc.setManually then Visibility.Visible else Visibility.Hidden frame.manuallyAdded.Fill <- color frame.border.Stroke <- color let RBCFrameFromExisting (srcImg: SourceImage) (rbc: RBC) (frame: Views.RBCFrame) : Views.RBCFrame = frame.Visibility <- Visibility.Visible frame.Height <- rbc.size.Height frame.Width <- rbc.size.Width frame.Tag <- rbc setRBCFrameStyle srcImg rbc frame frame.border.StrokeThickness <- 1. frame.txtRBCNumber.Text <- rbc.num.ToString() frame let updateDocumentStatus () = win.txtDocumentStatus.Text <- if state.FilePath = "" then "" else state.FilePath let statusMessageTimer = Threading.DispatcherTimer() statusMessageTimer.Tick.AddHandler(fun obj args -> statusMessageTimer.Stop(); win.txtMessageStatus.Text <- "") statusMessageTimer.Interval <- TimeSpan(0, 0, 2) let displayStatusMessage (message: string) = win.txtMessageStatus.Text <- message statusMessageTimer.Stop() statusMessageTimer.Start() let highlightRBCFrame (frame: Views.RBCFrame) (highlight: bool) = let rbc = frame.Tag :?> RBC if highlight then frame.border.StrokeThickness <- 3. if not rbc.infected && not rbc.setManually && not displayHealthy then frame.Opacity <- 1. else frame.border.StrokeThickness <- 1. if not rbc.infected && not rbc.setManually && not displayHealthy then frame.Opacity <- 0. let zoomToRBC (rbc: RBC) = win.scrollViewCurrentImage.ScrollToHorizontalOffset(rbc.center.X * currentScale - win.scrollViewCurrentImage.ViewportWidth / 2. + win.borderCurrentImage.BorderThickness.Left) win.scrollViewCurrentImage.ScrollToVerticalOffset(rbc.center.Y * currentScale - win.scrollViewCurrentImage.ViewportHeight / 2. + win.borderCurrentImage.BorderThickness.Top) let txtImageName_TextChanged = TextChangedEventHandler(fun obj args -> state.CurrentImage |> Option.iter(fun srcImg -> state.SetName srcImg win.txtImageName.Text)) let updateCurrentImageInformation () = win.txtImageName.TextChanged.RemoveHandler(txtImageName_TextChanged) win.txtImageInformation1.Inlines.Clear() win.txtImageInformation2.Inlines.Clear() win.txtImageName.Text <- "" match state.CurrentImage with | Some srcImg -> win.gridImageInformation.Visibility <- Visibility.Visible win.txtImageName.Text <- srcImg.name win.txtImageName.TextChanged.AddHandler(txtImageName_TextChanged) // The left part. let parasitemiaStr = Utils.percentText (state.ImageParasitemia srcImg) win.txtImageInformation1.Inlines.Add(Documents.Run("Parasitemia: ", FontWeight = FontWeights.Bold)) win.txtImageInformation1.Inlines.Add(parasitemiaStr) win.txtImageInformation1.Inlines.Add(Documents.LineBreak()) win.txtImageInformation1.Inlines.Add(Documents.Run("Last analysis: ", FontWeight = FontWeights.Bold)) win.txtImageInformation1.Inlines.Add(Documents.Run(if srcImg.dateLastAnalysis.Ticks = 0L then "" else srcImg.dateLastAnalysis.ToLocalTime().ToString())) // The right part part. win.txtImageInformation2.Inlines.Add(Documents.Run("Added infected erythrocyte: ", FontWeight = FontWeights.Bold)) win.txtImageInformation2.Inlines.Add(Documents.Run((state.ImageNbManuallyChangedRBCStr srcImg true) + " " + (state.ImageManuallyChangedRBCStr srcImg true))) win.txtImageInformation2.Inlines.Add(Documents.LineBreak()) win.txtImageInformation2.Inlines.Add(Documents.Run("Removed infected erythrocyte: ", FontWeight = FontWeights.Bold)) win.txtImageInformation2.Inlines.Add(Documents.Run((state.ImageNbManuallyChangedRBCStr srcImg false) + " " + (state.ImageManuallyChangedRBCStr srcImg false))) | _ -> win.gridImageInformation.Visibility <- Visibility.Hidden let updateGlobalParasitemia () = win.txtGlobalParasitemia.Inlines.Clear() let total, infected = state.GlobalParasitemia win.txtGlobalParasitemia.Inlines.Add(Documents.Run(Utils.percentText (total, infected), FontWeight = FontWeights.Bold)) if total > 0 && total < warningBelowNumberOfRBC then win.txtGlobalParasitemia.Inlines.Add( Documents.Run( sprintf " Warning: the number of erythrocytes should be above %d" warningBelowNumberOfRBC, FontWeight = FontWeights.Bold, Foreground = Brushes.Red)) let updateViewportPreview () = for preview in win.stackPreviews.Children |> Seq.cast do let srcImg = preview.Tag :?> SourceImage if Some srcImg = state.CurrentImage then preview.viewport.Visibility <- Visibility.Visible let canvasWidth = win.canvasCurrentImage.ActualWidth * currentScale let canvasHeight = win.canvasCurrentImage.ActualHeight * currentScale let previewWidth = (preview.ActualWidth - preview.BorderThickness.Left - preview.BorderThickness.Right) let previewHeight = (preview.ActualHeight - preview.BorderThickness.Top - preview.BorderThickness.Bottom) let marginLeft = previewWidth * (win.scrollViewCurrentImage.HorizontalOffset - win.borderCurrentImage.BorderThickness.Left) / canvasWidth - 2. let marginRight = previewWidth * (canvasWidth - (win.scrollViewCurrentImage.HorizontalOffset - win.borderCurrentImage.BorderThickness.Right) - win.scrollViewCurrentImage.ViewportWidth) / canvasWidth - 2. let marginTop = previewHeight * (win.scrollViewCurrentImage.VerticalOffset - win.borderCurrentImage.BorderThickness.Top) / canvasHeight - 2. let marginBottom = previewHeight * (canvasHeight - (win.scrollViewCurrentImage.VerticalOffset - win.borderCurrentImage.BorderThickness.Bottom) - win.scrollViewCurrentImage.ViewportHeight) / canvasHeight - 2. preview.viewport.Margin <- Thickness( marginLeft, marginTop, marginRight, marginBottom) else preview.viewport.Visibility <- Visibility.Hidden let rec setAsInfected (srcImg: SourceImage) (rbc: RBC) (infected: bool) = state.SetAsInfected rbc infected win.canvasCurrentImage.Children |> Seq.cast |> Seq.iter (fun frame -> if (frame.Tag :?> RBC) = rbc then setRBCFrameStyle srcImg rbc frame) updateRBCFramesPreview () updateCurrentImageInformation () updateGlobalParasitemia () and RBCFrame (srcImg: SourceImage) (rbc: RBC) : Views.RBCFrame = let frame = RBCFrameFromExisting srcImg rbc (Views.RBCFrame()) frame.SetValue(Panel.ZIndexProperty, Int32.MaxValue - rbc.num) // To be sure the frame.menuRBCSetAsHealthy.Click.AddHandler(fun obj args -> setAsInfected srcImg (frame.Tag :?> RBC) false) frame.menuRBCSetAsInfected.Click.AddHandler(fun obj args -> setAsInfected srcImg (frame.Tag :?> RBC) true) frame.ContextMenuOpening.AddHandler( fun obj args -> if (frame.Tag :?> RBC).infected then frame.menuRBCSetAsHealthy.Visibility <- Visibility.Visible frame.menuRBCSetAsInfected.Visibility <- Visibility.Collapsed else frame.menuRBCSetAsHealthy.Visibility <- Visibility.Collapsed frame.menuRBCSetAsInfected.Visibility <- Visibility.Visible) frame.ContextMenuClosing.AddHandler(fun obj args -> if not frame.IsMouseOver then highlightRBCFrame frame false ) frame.MouseEnter.AddHandler(fun obj args -> highlightRBCFrame frame true) frame.MouseLeave.AddHandler(fun obj args -> if not frame.grid.ContextMenu.IsOpen then highlightRBCFrame frame false) frame and updateRBCFramesPreview () = match state.CurrentImage with | Some srcImg -> let mutable currentPreview = 0 for rbc in srcImg.rbcs |> List.filter (fun rbc -> displayHealthy || rbc.infected) do let previewInfected = if currentPreview < win.stackRBC.Children.Count then RBCFrameFromExisting srcImg rbc (win.stackRBC.Children.[currentPreview] :?> Views.RBCFrame) else let f = RBCFrame srcImg rbc f.MouseLeftButtonUp.AddHandler(fun obj args -> zoomToRBC (f.Tag :?> RBC)) win.stackRBC.Children.Add(f) |> ignore f currentPreview <- currentPreview + 1 previewInfected.Height <- win.stackRBC.ActualHeight previewInfected.Width <- win.stackRBC.ActualHeight * rbc.size.Width / rbc.size.Height previewInfected.border.Fill <- ImageBrush(BitmapSourceConvert.ToBitmapSource(extractRBCPreview srcImg.img rbc)) win.stackRBC.Children.RemoveRange(currentPreview, win.stackRBC.Children.Count - currentPreview) | _ -> () updateViewportPreview () let updateRBCFramesCurrent () = match state.CurrentImage with | Some srcImg -> let mutable currentCanvas = 0 for rbc in srcImg.rbcs do let frame = if currentCanvas < win.canvasCurrentImage.Children.Count then RBCFrameFromExisting srcImg rbc (win.canvasCurrentImage.Children.[currentCanvas] :?> Views.RBCFrame) else let f = RBCFrame srcImg rbc f.Root.Opacity <- 0.7 win.canvasCurrentImage.Children.Add(f) |> ignore f currentCanvas <- currentCanvas + 1 Canvas.SetLeft(frame, rbc.center.X - rbc.size.Width / 2.) Canvas.SetTop(frame, rbc.center.Y - rbc.size.Height / 2.) for i in currentCanvas .. win.canvasCurrentImage.Children.Count - 1 do win.canvasCurrentImage.Children.[i].Visibility <- Visibility.Hidden | _ -> () let askDocumentPathToSave () : string option = let dialog = SaveFileDialog(AddExtension = true, DefaultExt = PiaZ.extension, Filter = PiaZ.filter) if state.FilePath <> "" then dialog.FileName <- FileInfo(state.FilePath).Name elif state.PatientID <> "" then dialog.FileName <- state.PatientID + PiaZ.extension let res = dialog.ShowDialog() if res.HasValue && res.Value then Some dialog.FileName else None let saveCurrentDocument () = try if state.FilePath = "" then match askDocumentPathToSave () with | Some filepath -> state.FilePath <- filepath state.Save() | _ -> () else state.Save() updateDocumentStatus () displayStatusMessage "Document saved" with | :? IOException as ex -> Log.Error(ex.ToString()) MessageBox.Show(sprintf "The document cannot be save in \"%s\"" state.FilePath, "Error saving the document", MessageBoxButton.OK, MessageBoxImage.Error) |> ignore let saveCurrentDocumentAsNewFile () = match askDocumentPathToSave () with | Some filepath -> state.FilePath <- filepath saveCurrentDocument () | _ -> () // Ask the use to save the current document if neccessary. let askSaveCurrent () = if state.AlteredSinceLastSave then match MessageBox.Show("Would you like to save the current document?", "Saving the current document", MessageBoxButton.YesNo, MessageBoxImage.Question) with | MessageBoxResult.Yes -> saveCurrentDocument () | _ -> () let updateCurrentImage () = match state.CurrentImage with | Some srcImg -> win.imgLogos.Visibility <- Visibility.Collapsed // Highlight the preview. win.stackPreviews.Children |> Seq.cast |> Seq.iter (fun preview -> preview.border.BorderThickness <- Thickness(if preview.Tag = (srcImg :> Object) then 3. else 0.)) win.canvasCurrentImage.Height <- float srcImg.img.Height win.canvasCurrentImage.Width <- float srcImg.img.Width win.canvasCurrentImage.Background <- ImageBrush(BitmapSourceConvert.ToBitmapSource(srcImg.img)) updateRBCFramesCurrent () updateRBCFramesPreview () | None -> win.imgLogos.Visibility <- Visibility.Visible win.stackRBC.Children.Clear() win.canvasCurrentImage.Children.Clear() win.canvasCurrentImage.Background <- canvasCurrentImageColor updateCurrentImageInformation () let setCurrentImage (srcImg: SourceImage) = if state.CurrentImage.IsNone || state.CurrentImage.Value <> srcImg then state.CurrentImage <- Some srcImg updateCurrentImage () let addPreview (srcImg: SourceImage) = let imgCtrl = Views.ImageSourcePreview(Margin = Thickness(3.)) imgCtrl.menuRemoveImage.Click.AddHandler(fun obj args -> win.stackPreviews.Children.Remove(imgCtrl) let srcImg = imgCtrl.Tag :?> SourceImage let currentRemoved = Some srcImg = state.CurrentImage state.RemoveSourceImage srcImg if currentRemoved then updateCurrentImage() updateGlobalParasitemia() // Update image numbers. win.stackPreviews.Children |> Seq.cast |> Seq.iter (fun imgPreview -> imgPreview.txtImageNumber.Text <- (imgPreview.Tag :?> SourceImage).num.ToString())) imgCtrl.Tag <- srcImg imgCtrl.txtImageNumber.Text <- srcImg.num.ToString() let width = 200 let height = srcImg.img.Height * width / srcImg.img.Width imgCtrl.imagePreview.Source <- BitmapSourceConvert.ToBitmapSource(srcImg.img.Resize(width, height, Emgu.CV.CvEnum.Inter.Cubic)) win.stackPreviews.Children.Add(imgCtrl) |> ignore // Zoom to a mouse position into the control 'imgCtrl'. let zoomTo (mousePos: Point) = let canvasW = win.canvasCurrentImage.ActualWidth * currentScale let canvasH = win.canvasCurrentImage.ActualHeight * currentScale let centerX = (mousePos.X - imgCtrl.BorderThickness.Left) / (imgCtrl.ActualWidth - imgCtrl.BorderThickness.Left) * canvasW let centerY = (mousePos.Y - imgCtrl.BorderThickness.Top) / (imgCtrl.ActualHeight - imgCtrl.BorderThickness.Top) * canvasH win.scrollViewCurrentImage.ScrollToHorizontalOffset(centerX - win.scrollViewCurrentImage.ViewportWidth / 2. + win.borderCurrentImage.BorderThickness.Left) win.scrollViewCurrentImage.ScrollToVerticalOffset(centerY - win.scrollViewCurrentImage.ViewportHeight / 2. + win.borderCurrentImage.BorderThickness.Top) imgCtrl.MouseLeftButtonDown.AddHandler(fun obj args -> setCurrentImage (state.SourceImages |> Seq.find (fun srcImg -> (srcImg :> Object) = imgCtrl.Tag)) imgCtrl.UpdateLayout() zoomTo (args.GetPosition(imgCtrl)) imgCtrl.CaptureMouse() |> ignore) imgCtrl.MouseMove.AddHandler(fun obj args -> if imgCtrl.IsMouseCaptured then zoomTo (args.GetPosition(imgCtrl))) imgCtrl.MouseLeftButtonUp.AddHandler(fun obj args -> if imgCtrl.IsMouseCaptured then imgCtrl.ReleaseMouseCapture()) let updatePreviews () = win.stackPreviews.Children.Clear () for srcImg in state.SourceImages do addPreview srcImg updateCurrentImage () let updateGUI () = win.txtPatient.Text <- state.PatientID updatePreviews () updateGlobalParasitemia () updateDocumentStatus () let loadFile (filepath: string) = askSaveCurrent () let previousFilePath = state.FilePath try state.FilePath <- filepath state.Load() updateGUI () with | :? IOException as ex -> Log.Error(ex.ToString()) state.FilePath <- previousFilePath MessageBox.Show(sprintf "The document cannot be loaded from \"%s\"" filepath, "Error loading the document", MessageBoxButton.OK, MessageBoxImage.Error) |> ignore let askLoadFile () = let dialog = OpenFileDialog(Filter = PiaZ.filter) let res = dialog.ShowDialog() if res.HasValue && res.Value then loadFile dialog.FileName let newFile () = askSaveCurrent () state.Reset() updateGUI() let exportResults () = let extension = ".txt" let dialog = SaveFileDialog(AddExtension = true, DefaultExt = extension) if state.FilePath <> "" then dialog.FileName <- Path.GetFileNameWithoutExtension(state.FilePath) + extension elif state.PatientID <> "" then dialog.FileName <- state.PatientID + extension let res = dialog.ShowDialog() if res.HasValue && res.Value then try Export.exportResults state dialog.FileName with | :? IOException as ex -> Log.Error(ex.ToString()) MessageBox.Show(sprintf "The results cannot be exported in \"%s\"" state.FilePath, "Error exporting the files", MessageBoxButton.OK, MessageBoxImage.Error) |> ignore win.txtPatient.TextChanged.AddHandler(fun obj args -> state.PatientID <- win.txtPatient.Text) win.menuExit.Click.AddHandler(fun obj args -> win.Root.Close()) win.menuSave.Click.AddHandler(fun obj args -> saveCurrentDocument ()) win.menuSaveAs.Click.AddHandler(fun obj args -> saveCurrentDocumentAsNewFile ()) win.menuOpen.Click.AddHandler(fun obj args -> askLoadFile ()) win.menuNew.Click.AddHandler(fun obj args -> newFile ()) win.menuExportResults.Click.AddHandler(fun obj args -> exportResults ()) win.menuAddSourceImage.Click.AddHandler(fun obj args -> let dialog = OpenFileDialog(Filter = "Image Files|*.png;*.jpg;*.tif;*.tiff", Multiselect = true) let res = dialog.ShowDialog() if res.HasValue && res.Value then let noSourceImage = state.SourceImages.Count() = 0 for filename in dialog.FileNames do try let srcImg = state.AddSourceImage filename defaultConfig addPreview srcImg with | _ as ex -> Log.Error(ex.ToString()) MessageBox.Show(sprintf "Unable to read the image from \"%s\"" filename, "Error adding an image", MessageBoxButton.OK, MessageBoxImage.Error) |> ignore updateGlobalParasitemia () if noSourceImage then updateCurrentImage ()) win.menuAnalysis.SubmenuOpened.AddHandler(fun obj args -> win.menuStartAnalysis.IsEnabled <- state.SourceImages.Count() > 0) win.menuStartAnalysis.Click.AddHandler(fun obj args -> if Analysis.showWindow win.Root state then updateGlobalParasitemia () updateCurrentImage ()) win.menuHightlightRBC.Click.AddHandler(fun obj args -> displayHealthy <- win.menuHightlightRBC.IsChecked updateRBCFramesPreview () updateRBCFramesCurrent ()) win.menuAbout.Click.AddHandler(fun obj args -> About.showWindow win.Root) win.Root.Closing.AddHandler(fun obj args -> askSaveCurrent ()) // Zoom on the current image. let adjustCurrentImageBorders (deltaX: float) (deltaY: float) = win.borderCurrentImage.BorderThickness <- Thickness( (win.scrollViewCurrentImage.ViewportWidth + deltaX) / 2., (win.scrollViewCurrentImage.ViewportHeight + deltaY) / 2., (win.scrollViewCurrentImage.ViewportWidth + deltaX) / 2., (win.scrollViewCurrentImage.ViewportHeight + deltaY) / 2.) win.canvasCurrentImage.SizeChanged.AddHandler(fun obj args -> let deltaX = args.NewSize.Width - args.PreviousSize.Width let deltaY = args.NewSize.Height - args.PreviousSize.Height if deltaX > 0.5 || deltaY > 0.5 then adjustCurrentImageBorders 0.0 0.0 // Center the view at the center of the image initialy. win.scrollViewCurrentImage.UpdateLayout() win.scrollViewCurrentImage.ScrollToHorizontalOffset(win.borderCurrentImage.ActualWidth / 2. - win.scrollViewCurrentImage.ViewportWidth / 2.) win.scrollViewCurrentImage.ScrollToVerticalOffset(win.borderCurrentImage.ActualHeight / 2. - win.scrollViewCurrentImage.ViewportHeight / 2.)) win.scrollViewCurrentImage.SizeChanged.AddHandler(fun obj args -> let deltaX = args.NewSize.Width - args.PreviousSize.Width let deltaY = args.NewSize.Height - args.PreviousSize.Height adjustCurrentImageBorders deltaX deltaY win.scrollViewCurrentImage.ScrollToHorizontalOffset(win.scrollViewCurrentImage.HorizontalOffset + deltaX / 8.) win.scrollViewCurrentImage.ScrollToVerticalOffset(win.scrollViewCurrentImage.VerticalOffset + deltaY / 8.)) let mutable maxScale = 4. let mutable minScale = 0.25 let currentImageScaleTransform = ScaleTransform() win.canvasCurrentImage.LayoutTransform <- currentImageScaleTransform win.borderCurrentImage.PreviewMouseWheel.AddHandler(fun obj args -> let scaleFactor = if args.Delta > 0 then 2.0 else 0.5 if scaleFactor > 1. && currentScale < maxScale || scaleFactor < 1. && currentScale > minScale then let previousScale = currentScale currentScale <- let newScale = currentScale * scaleFactor if newScale > maxScale then maxScale elif newScale < minScale then minScale else newScale let realScaleFactor = currentScale / previousScale let centerX = win.scrollViewCurrentImage.HorizontalOffset + win.scrollViewCurrentImage.ViewportWidth / 2. - win.borderCurrentImage.BorderThickness.Left let centerY = win.scrollViewCurrentImage.VerticalOffset + win.scrollViewCurrentImage.ViewportHeight / 2. - win.borderCurrentImage.BorderThickness.Top currentImageScaleTransform.ScaleX <- currentScale currentImageScaleTransform.ScaleY <- currentScale win.scrollViewCurrentImage.ScrollToHorizontalOffset(centerX * realScaleFactor - win.scrollViewCurrentImage.ViewportWidth / 2. + win.borderCurrentImage.BorderThickness.Left) win.scrollViewCurrentImage.ScrollToVerticalOffset(centerY * realScaleFactor - win.scrollViewCurrentImage.ViewportHeight / 2. + win.borderCurrentImage.BorderThickness.Top) args.Handled <- true) // Pan on the current image. let mutable scrollStartPosition = Point(0., 0.) let mutable scrollStartOffsetX = 0. let mutable scrollStartOffsetY = 0. win.borderCurrentImage.PreviewMouseLeftButtonDown.AddHandler(fun obj args -> scrollStartPosition <- args.GetPosition(win.scrollViewCurrentImage) scrollStartOffsetX <- win.scrollViewCurrentImage.HorizontalOffset scrollStartOffsetY <- win.scrollViewCurrentImage.VerticalOffset win.borderCurrentImage.Cursor <- Input.Cursors.ScrollAll win.borderCurrentImage.CaptureMouse() |> ignore args.Handled <- true) win.borderCurrentImage.PreviewMouseMove.AddHandler(fun obj args -> if win.borderCurrentImage.IsMouseCaptured then let position = args.GetPosition(win.scrollViewCurrentImage) let deltaX = scrollStartPosition.X - position.X let deltaY = scrollStartPosition.Y - position.Y win.scrollViewCurrentImage.ScrollToHorizontalOffset(deltaX + scrollStartOffsetX) win.scrollViewCurrentImage.ScrollToVerticalOffset(deltaY + scrollStartOffsetY) args.Handled <- true) win.borderCurrentImage.PreviewMouseLeftButtonUp.AddHandler(fun obj args -> if win.borderCurrentImage.IsMouseCaptured then win.borderCurrentImage.Cursor <- Input.Cursors.Arrow win.borderCurrentImage.ReleaseMouseCapture() args.Handled <- true) // Shortcuts. // Save. win.Root.InputBindings.Add( Input.KeyBinding( FSharp.ViewModule.FunCommand((fun obj -> saveCurrentDocument ()), (fun obj -> true)), Input.KeyGesture(Input.Key.S, Input.ModifierKeys.Control))) |> ignore // Save as. win.Root.InputBindings.Add( Input.KeyBinding( FSharp.ViewModule.FunCommand((fun obj -> saveCurrentDocumentAsNewFile ()), (fun obj -> true)), Input.KeyGesture(Input.Key.S, Input.ModifierKeys.Control ||| Input.ModifierKeys.Shift))) |> ignore // Open. win.Root.InputBindings.Add( Input.KeyBinding( FSharp.ViewModule.FunCommand((fun obj -> askLoadFile ()), (fun obj -> true)), Input.KeyGesture(Input.Key.O, Input.ModifierKeys.Control))) |> ignore // New file. win.Root.InputBindings.Add( Input.KeyBinding( FSharp.ViewModule.FunCommand((fun obj -> newFile ()), (fun obj -> true)), Input.KeyGesture(Input.Key.N, Input.ModifierKeys.Control))) |> ignore // Export results. win.Root.InputBindings.Add( Input.KeyBinding( FSharp.ViewModule.FunCommand((fun obj -> exportResults ()), (fun obj -> true)), Input.KeyGesture(Input.Key.E, Input.ModifierKeys.Control))) |> ignore // Viewport preview. win.scrollViewCurrentImage.ScrollChanged.AddHandler(fun obj args -> updateViewportPreview ()) updateDocumentStatus () win.gridImageInformation.Visibility <- Visibility.Hidden win.Root.Show() match fileToOpen with | Some filepath -> loadFile filepath | None -> () app.Run()