@@ -23,73 +23,121 @@ public static void Start(MainViewModel vm, bool settingsExists, IClassicDesktopS
23
23
24
24
if ( ! settingsExists )
25
25
{
26
- // Fixes incorrect window
27
- w . Height = w . MinHeight ;
28
- w . Width = w . MinWidth ;
29
-
30
- WindowHelper . CenterWindowOnScreen ( ) ;
31
- vm . CanResize = true ;
32
- vm . IsAutoFit = false ;
26
+ InitializeWindowForNoSettings ( w , vm ) ;
33
27
}
34
28
else
35
29
{
36
- if ( SettingsHelper . Settings . UIProperties . OpenInSameWindow )
30
+ HandleWindowStartupSettings ( vm , desktop , w , args ) ;
31
+ }
32
+
33
+ w . Show ( ) ;
34
+ InitializePostWindowStartup ( vm , desktop , w , args , settingsExists ) ;
35
+ }
36
+
37
+ private static void InitializeWindowForNoSettings ( Window w , MainViewModel vm )
38
+ {
39
+ w . Height = w . MinHeight ;
40
+ w . Width = w . MinWidth ;
41
+ WindowHelper . CenterWindowOnScreen ( ) ;
42
+ vm . CanResize = true ;
43
+ vm . IsAutoFit = false ;
44
+ }
45
+
46
+ private static void HandleWindowStartupSettings ( MainViewModel vm , IClassicDesktopStyleApplicationLifetime desktop , Window w , string [ ] args )
47
+ {
48
+ if ( SettingsHelper . Settings . UIProperties . OpenInSameWindow && ProcessHelper . CheckIfAnotherInstanceIsRunning ( ) )
49
+ {
50
+ HandleMultipleInstances ( args ) ;
51
+ }
52
+
53
+ ApplyWindowSizeAndPosition ( vm , desktop , w ) ;
54
+
55
+ if ( SettingsHelper . Settings . UIProperties . ShowInterface )
56
+ {
57
+ vm . IsTopToolbarShown = true ;
58
+ vm . IsBottomToolbarShown = SettingsHelper . Settings . UIProperties . ShowBottomNavBar ;
59
+ }
60
+ }
61
+
62
+ private static void HandleMultipleInstances ( string [ ] args )
63
+ {
64
+ if ( args . Length > 1 )
65
+ {
66
+ Task . Run ( async ( ) =>
37
67
{
38
- var pipeName = "PicViewPipe" ;
39
-
40
- if ( ProcessHelper . CheckIfAnotherInstanceIsRunning ( ) )
68
+ var retries = 0 ;
69
+ while ( ! await IPC . SendArgumentToRunningInstance ( args [ 1 ] , IPC . PipeName ) )
41
70
{
42
- if ( args . Length > 1 )
71
+ await Task . Delay ( 1000 ) ;
72
+ if ( ++ retries > 20 )
43
73
{
44
- // Another instance is running, send arguments and exit
45
- _ = IPC . SendArgumentToRunningInstance ( args [ 1 ] , pipeName ) ;
46
- Environment . Exit ( 0 ) ;
74
+ break ;
47
75
}
48
76
}
49
- }
50
- if ( SettingsHelper . Settings . WindowProperties . Fullscreen )
51
- {
52
- WindowHelper . Fullscreen ( vm , desktop ) ;
53
- }
54
- else if ( SettingsHelper . Settings . WindowProperties . Maximized )
55
- {
56
- WindowHelper . Maximize ( ) ;
57
- }
58
- else if ( SettingsHelper . Settings . WindowProperties . AutoFit )
59
- {
60
- desktop . MainWindow . WindowStartupLocation = WindowStartupLocation . CenterScreen ;
61
- vm . SizeToContent = SizeToContent . WidthAndHeight ;
62
- vm . CanResize = false ;
63
- vm . IsAutoFit = true ;
64
- if ( SettingsHelper . Settings . UIProperties . ShowInterface )
65
- {
66
- vm . IsTopToolbarShown = true ;
67
- vm . IsBottomToolbarShown = SettingsHelper . Settings . UIProperties . ShowBottomNavBar ;
68
- }
69
- }
70
- else
71
- {
72
- vm . CanResize = true ;
73
- vm . IsAutoFit = false ;
74
- WindowHelper . InitializeWindowSizeAndPosition ( w ) ;
75
- if ( SettingsHelper . Settings . UIProperties . ShowInterface )
76
- {
77
- vm . IsTopToolbarShown = true ;
78
- vm . IsBottomToolbarShown = SettingsHelper . Settings . UIProperties . ShowBottomNavBar ;
79
- }
80
- }
77
+ Environment . Exit ( 0 ) ;
78
+ } ) ;
81
79
}
80
+ }
82
81
83
- w . Show ( ) ;
82
+ private static void ApplyWindowSizeAndPosition ( MainViewModel vm , IClassicDesktopStyleApplicationLifetime desktop , Window w )
83
+ {
84
+ if ( SettingsHelper . Settings . WindowProperties . Fullscreen )
85
+ {
86
+ WindowHelper . Fullscreen ( vm , desktop ) ;
87
+ }
88
+ else if ( SettingsHelper . Settings . WindowProperties . Maximized )
89
+ {
90
+ WindowHelper . Maximize ( ) ;
91
+ }
92
+ else if ( SettingsHelper . Settings . WindowProperties . AutoFit )
93
+ {
94
+ desktop . MainWindow . WindowStartupLocation = WindowStartupLocation . CenterScreen ;
95
+ vm . SizeToContent = SizeToContent . WidthAndHeight ;
96
+ vm . CanResize = false ;
97
+ vm . IsAutoFit = true ;
98
+ }
99
+ else
100
+ {
101
+ vm . CanResize = true ;
102
+ vm . IsAutoFit = false ;
103
+ WindowHelper . InitializeWindowSizeAndPosition ( w ) ;
104
+ }
105
+ }
106
+
107
+ private static void InitializePostWindowStartup ( MainViewModel vm , IClassicDesktopStyleApplicationLifetime desktop , Window w , string [ ] args , bool settingsExists )
108
+ {
84
109
vm . IsLoading = true ;
85
110
ScreenHelper . UpdateScreenSize ( w ) ;
111
+ ApplyThemeAndUISettings ( vm , desktop ) ;
112
+
113
+ LoadInitialContent ( vm , args ) ;
114
+
115
+ ValidateGallerySettings ( vm , settingsExists ) ;
116
+
117
+ BackgroundManager . SetBackground ( vm ) ;
118
+ ColorManager . UpdateAccentColors ( SettingsHelper . Settings . Theme . ColorTheme ) ;
119
+
120
+ Task . Run ( KeybindingsHelper . LoadKeybindings ) ;
121
+ UIHelper . AddMenus ( ) ;
122
+ SetWindowEventHandlers ( w ) ;
123
+ Application . Current . Name = "PicView" ;
124
+
125
+ if ( SettingsHelper . Settings . UIProperties . OpenInSameWindow )
126
+ {
127
+ _ = IPC . StartListeningForArguments ( IPC . PipeName , vm ) ;
128
+ }
129
+ }
130
+
131
+ private static void ApplyThemeAndUISettings ( MainViewModel vm , IClassicDesktopStyleApplicationLifetime desktop )
132
+ {
86
133
if ( SettingsHelper . Settings . Theme . GlassTheme )
87
134
{
88
135
ThemeManager . GlassThemeUpdates ( ) ;
89
136
}
137
+
90
138
UIHelper . SetControls ( desktop ) ;
91
139
LanguageUpdater . UpdateLanguage ( vm ) ;
92
-
140
+
93
141
if ( SettingsHelper . Settings . Zoom . ScrollEnabled )
94
142
{
95
143
vm . ToggleScrollBarVisibility = ScrollBarVisibility . Visible ;
@@ -105,9 +153,12 @@ public static void Start(MainViewModel vm, bool settingsExists, IClassicDesktopS
105
153
{
106
154
desktop . MainWindow . Topmost = true ;
107
155
}
156
+ }
108
157
158
+ private static void LoadInitialContent ( MainViewModel vm , string [ ] args )
159
+ {
109
160
vm . ImageViewer = new ImageViewer ( ) ;
110
-
161
+
111
162
if ( args . Length > 1 )
112
163
{
113
164
vm . CurrentView = vm . ImageViewer ;
@@ -131,54 +182,49 @@ public static void Start(MainViewModel vm, bool settingsExists, IClassicDesktopS
131
182
vm . CurrentView = new StartUpMenu ( ) ;
132
183
vm . IsLoading = false ;
133
184
}
185
+ }
134
186
187
+ private static void ValidateGallerySettings ( MainViewModel vm , bool settingsExists )
188
+ {
135
189
if ( ! settingsExists )
136
190
{
137
191
vm . GetBottomGalleryItemHeight = GalleryDefaults . DefaultBottomGalleryHeight ;
138
192
vm . GetFullGalleryItemHeight = GalleryDefaults . DefaultFullGalleryHeight ;
139
193
}
194
+
140
195
// Set default gallery sizes if they are out of range or upgrading from an old version
141
196
if ( vm . GetBottomGalleryItemHeight < vm . MinBottomGalleryItemHeight ||
142
197
vm . GetBottomGalleryItemHeight > vm . MaxBottomGalleryItemHeight )
143
198
{
144
199
vm . GetBottomGalleryItemHeight = GalleryDefaults . DefaultBottomGalleryHeight ;
145
200
}
201
+
146
202
if ( vm . GetFullGalleryItemHeight < vm . MinFullGalleryItemHeight ||
147
203
vm . GetFullGalleryItemHeight > vm . MaxFullGalleryItemHeight )
148
204
{
149
205
vm . GetFullGalleryItemHeight = GalleryDefaults . DefaultFullGalleryHeight ;
150
206
}
151
207
152
- if ( ! settingsExists )
208
+ if ( settingsExists )
153
209
{
154
- if ( string . IsNullOrWhiteSpace ( SettingsHelper . Settings . Gallery . BottomGalleryStretchMode ) )
155
- {
156
- SettingsHelper . Settings . Gallery . BottomGalleryStretchMode = "UniformToFill" ;
157
- }
210
+ return ;
211
+ }
158
212
159
- if ( string . IsNullOrWhiteSpace ( SettingsHelper . Settings . Gallery . FullGalleryStretchMode ) )
160
- {
161
- SettingsHelper . Settings . Gallery . FullGalleryStretchMode = "UniformToFill" ;
162
- }
213
+ if ( string . IsNullOrWhiteSpace ( SettingsHelper . Settings . Gallery . BottomGalleryStretchMode ) )
214
+ {
215
+ SettingsHelper . Settings . Gallery . BottomGalleryStretchMode = "UniformToFill" ;
163
216
}
164
-
165
- BackgroundManager . SetBackground ( vm ) ;
166
- ColorManager . UpdateAccentColors ( SettingsHelper . Settings . Theme . ColorTheme ) ;
167
-
168
- Task . Run ( KeybindingsHelper . LoadKeybindings ) ;
169
-
170
- UIHelper . AddMenus ( ) ;
171
217
218
+ if ( string . IsNullOrWhiteSpace ( SettingsHelper . Settings . Gallery . FullGalleryStretchMode ) )
219
+ {
220
+ SettingsHelper . Settings . Gallery . FullGalleryStretchMode = "UniformToFill" ;
221
+ }
222
+ }
223
+
224
+ private static void SetWindowEventHandlers ( Window w )
225
+ {
172
226
w . KeyDown += async ( _ , e ) => await MainKeyboardShortcuts . MainWindow_KeysDownAsync ( e ) . ConfigureAwait ( false ) ;
173
227
w . KeyUp += ( _ , e ) => MainKeyboardShortcuts . MainWindow_KeysUp ( e ) ;
174
228
w . PointerPressed += async ( _ , e ) => await MouseShortcuts . MainWindow_PointerPressed ( e ) . ConfigureAwait ( false ) ;
175
-
176
- Application . Current . Name = "PicView" ;
177
-
178
- if ( SettingsHelper . Settings . UIProperties . OpenInSameWindow )
179
- {
180
- // No other instance is running, create named pipe server
181
- _ = IPC . StartListeningForArguments ( "PicViewPipe" , vm ) ;
182
- }
183
229
}
184
- }
230
+ }
0 commit comments