This repository has been archived by the owner on Jul 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.axaml.fs
199 lines (158 loc) · 7.25 KB
/
MainWindow.axaml.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
189
190
191
192
193
194
195
196
197
198
199
namespace ImageProcessingApp
open Avalonia
open Avalonia.Controls
open Brahma.FSharp
open ImageProcessing.MyImage
open ImageProcessing.Process
open Avalonia.Markup.Xaml
open Avalonia.Interactivity
open Avalonia.Styling
open System.IO
open MsBox.Avalonia
open Avalonia.Media.Imaging
type MainWindow() as this =
inherit Window()
let mutable temporaryImagePath = __SOURCE_DIRECTORY__ + "/Assets/Temporary/tmp.png"
let mutable myImage = load temporaryImagePath
let device = ClDevice.GetFirstAppropriateDevice()
let clContext = ClContext(device)
let transformationsParserGPU = transformationsParserGPU clContext 64
let processImage currentTheme (imageContainer: Image) (transformation: Transformations) =
match currentTheme with
| "Light" ->
myImage <- myImage |> transformationsParserCPU transformation
save myImage temporaryImagePath
imageContainer.Source <- new Bitmap(temporaryImagePath)
| "Dark" ->
myImage <- myImage |> transformationsParserGPU transformation
save myImage temporaryImagePath
imageContainer.Source <- new Bitmap(temporaryImagePath)
|> ignore
do this.InitializeComponent()
member this.SwitchThemes(source: obj, args: RoutedEventArgs) =
match this.ActualThemeVariant.ToString() with
| "Light" ->
this.SetValue<ThemeVariant>(Application.RequestedThemeVariantProperty, ThemeVariant.Dark)
let gpuModeOnBox =
MessageBoxManager.GetMessageBoxStandard(
"GPUNotification",
"GPU mode on",
MsBox.Avalonia.Enums.ButtonEnum.Ok
)
gpuModeOnBox.ShowAsPopupAsync(this) |> ignore
| "Dark" ->
this.SetValue<ThemeVariant>(Application.RequestedThemeVariantProperty, ThemeVariant.Light)
let gpuModeOffBox =
MessageBoxManager.GetMessageBoxStandard(
"GPUNotification",
"GPU mode off",
MsBox.Avalonia.Enums.ButtonEnum.Ok
)
gpuModeOffBox.ShowAsPopupAsync(this) |> ignore
|> ignore
member this.ButtonWithInputBorderClick(source: obj, args: RoutedEventArgs) =
let saveBorder = this.FindControl<Border>("SaveBorder")
let importBorder = this.FindControl<Border>("ImportBorder")
let _inner (currentBorder: Border) (anotherBorder: Border) =
currentBorder.IsVisible <- not currentBorder.IsVisible
anotherBorder.IsVisible <- false
match source with
| :? Button as button ->
match button.Name with
| "ImportButton" -> _inner importBorder saveBorder
| "SaveButton" -> _inner saveBorder importBorder
member this.ClickOnBorder(source: obj, args: Avalonia.Input.TappedEventArgs) =
let importBorder = this.FindControl<Border>("ImportBorder")
let saveBorder = this.FindControl<Border>("SaveBorder")
importBorder.IsVisible <- false
saveBorder.IsVisible <- false
member this.ClickOnAnotherButton(source: obj, args: RoutedEventArgs) =
let importBorder = this.FindControl<Border>("ImportBorder")
let saveBorder = this.FindControl<Border>("SaveBorder")
importBorder.IsVisible <- false
saveBorder.IsVisible <- false
member this.ImportImage(source: obj, args: RoutedEventArgs) =
let imageContainer = this.FindControl<Image>("Image")
let pathToImportImage = this.FindControl<TextBox>("PathToImport").Text
if File.Exists pathToImportImage then
try
myImage <- load pathToImportImage
temporaryImagePath <-
__SOURCE_DIRECTORY__
+ "/Assets/Temporary/tmp"
+ Path.GetExtension pathToImportImage
save myImage temporaryImagePath
imageContainer.Source <- new Bitmap(pathToImportImage)
let importingWasSucceedBox =
MessageBoxManager.GetMessageBoxStandard(
"ImportNotification",
"Importing was successful",
MsBox.Avalonia.Enums.ButtonEnum.Ok
)
importingWasSucceedBox.ShowAsPopupAsync(this) |> ignore
with ex ->
let importingWasFailedBox =
MessageBoxManager.GetMessageBoxStandard(
"ImportNotification",
ex.Message,
MsBox.Avalonia.Enums.ButtonEnum.Ok
)
importingWasFailedBox.ShowAsPopupAsync(this) |> ignore
else
let incorrectPathBox =
MessageBoxManager.GetMessageBoxStandard(
"ImportNotification",
"Incorrect path to import image",
MsBox.Avalonia.Enums.ButtonEnum.Ok
)
incorrectPathBox.ShowAsPopupAsync(this) |> ignore
member this.Process(source: obj, args: RoutedEventArgs) =
let imageContainer = this.FindControl<Image>("Image")
let processImage = processImage (this.ActualThemeVariant.ToString()) imageContainer
match source with
| :? MenuItem as item ->
match item.Name with
| "GaussianBlur" -> processImage Gauss
| "Edges" -> processImage Edges
| "Darken" -> processImage Darken
| "Lighten" -> processImage Lighten
| "Sharpen" -> processImage Sharpen
| "VerticalFlip" -> processImage FlipV
| "HorizontalFlip" -> processImage FlipH
| "ClockwiseRotate" -> processImage RotationR
| "CounterclockwiseRotate" -> processImage RotationL
member this.SaveImage(source: obj, args: RoutedEventArgs) =
let pathToSaveImage = this.FindControl<TextBox>("PathToSave").Text
if Directory.Exists(Path.GetDirectoryName pathToSaveImage) then
try
File.Copy(temporaryImagePath, pathToSaveImage)
let savingWasSucceedBox =
MessageBoxManager.GetMessageBoxStandard(
"SavingNotification",
"Saving was successful",
MsBox.Avalonia.Enums.ButtonEnum.Ok
)
savingWasSucceedBox.ShowAsPopupAsync(this) |> ignore
with :? System.Exception as ex ->
let savingWasFailedBox =
MessageBoxManager.GetMessageBoxStandard(
"SavingNotification",
ex.Message,
MsBox.Avalonia.Enums.ButtonEnum.Ok
)
savingWasFailedBox.ShowAsPopupAsync(this) |> ignore
else
let pathWasIncorrectBox =
MessageBoxManager.GetMessageBoxStandard(
"SavingNotification",
"Incorrect path to save image",
MsBox.Avalonia.Enums.ButtonEnum.Ok
)
pathWasIncorrectBox.ShowAsPopupAsync(this) |> ignore
member private this.InitializeComponent() =
#if DEBUG
this.AttachDevTools()
#endif
this.Width <- 1500
this.Height <- 920
AvaloniaXamlLoader.Load(this)