@@ -126,7 +126,7 @@ private void Initialize()
126126 CheckForNewVersion ( ) ;
127127 InitializeDisplayManager ( ) ;
128128 InitializeAudioManager ( ) ;
129- Globals . Instance . SaveSettings ( ) ;
129+ Globals . Instance . SaveSettings ( ) ;
130130 InitializeTrayMenuHelper ( ) ;
131131 CreateRelayCommands ( ) ;
132132 ShowView = ! Settings . StartMinimizedToTray ;
@@ -169,8 +169,8 @@ private void UpdateCurrentProfile(ApplicationItem application, ApplicationChange
169169
170170 if ( profile == null )
171171 return ;
172- bool profileChanged = Equals ( profile , CurrentProfile ) ;
173-
172+ bool profileChanged = Equals ( profile , CurrentProfile ) ;
173+
174174 CurrentProfile = profile ;
175175 if ( profileChanged )
176176 Globals . Logs . Add ( $ "Profile changed to { profile . Name } ", false ) ;
@@ -206,7 +206,7 @@ private void UpdateCurrentProfile(ApplicationItem application, ApplicationChange
206206 CurrentProfile = null ;
207207 }
208208 }
209-
209+
210210 private void ActionLog ( object sender , LogEntry entry )
211211 {
212212 Globals . Logs . AppendLogEntry ( entry ) ;
@@ -262,7 +262,7 @@ private void CreateRelayCommands()
262262 ActivateHDRCommand = new RelayCommand ( DisplayManager . Instance . ActivateHDR ) ;
263263 DeactivateHDRCommand = new RelayCommand ( DisplayManager . Instance . DeactivateHDR ) ;
264264 AddAssignmentCommand = new RelayCommand ( AddAssignment ) ;
265- EditApplicationCommand = new RelayCommand < ApplicationProfileAssignment > ( EditApplication ) ;
265+ EditApplicationCommand = new RelayCommand < ApplicationProfileAssignment > ( EditApplication ) ;
266266 RemoveAssignmentCommand = new RelayCommand < ApplicationProfileAssignment > ( RemoveAssignment ) ;
267267
268268 MoveAssignmentUpCommand = new RelayCommand < ApplicationProfileAssignment > ( MoveAssignmentUp ) ;
@@ -307,6 +307,7 @@ private void LoadSettings()
307307 {
308308 Globals . Instance . LoadSettings ( ) ;
309309 FixAssignments ( ) ;
310+ Globals . Instance . SaveSettings ( ) ;
310311 Settings . ApplicationProfileAssignments . Sort ( a => a . Position , ListSortDirection . Ascending ) ;
311312 Settings . ApplicationProfileAssignments . CollectionChanged += ApplicationProfileAssigments_CollectionChanged ;
312313 Settings . ApplicationProfiles . CollectionChanged += ApplicationProfiles_CollectionChanged ;
@@ -317,9 +318,9 @@ private void LoadSettings()
317318 ApplicationProfileAssigments_CollectionChanged (
318319 Settings . ApplicationProfileAssignments , new NotifyCollectionChangedEventArgs ( NotifyCollectionChangedAction . Add , Settings . ApplicationProfileAssignments . ToList ( ) ) ) ;
319320
320- ApplicationProfiles_CollectionChanged ( Settings . ApplicationProfiles , new NotifyCollectionChangedEventArgs ( NotifyCollectionChangedAction . Add , Settings . ApplicationProfiles . ToList ( ) ) ) ;
321+ ApplicationProfiles_CollectionChanged ( Settings . ApplicationProfiles , new NotifyCollectionChangedEventArgs ( NotifyCollectionChangedAction . Add , Settings . ApplicationProfiles . ToList ( ) ) ) ;
321322
322- Monitors_CollectionChanged ( Settings . Monitors , new NotifyCollectionChangedEventArgs ( NotifyCollectionChangedAction . Add , Settings . Monitors . ToList ( ) ) ) ;
323+ Monitors_CollectionChanged ( Settings . Monitors , new NotifyCollectionChangedEventArgs ( NotifyCollectionChangedAction . Add , Settings . Monitors . ToList ( ) ) ) ;
323324
324325
325326 Globals . Logs . LogFileEnabled = Settings . CreateLogFile ;
@@ -353,7 +354,7 @@ private void FixAssignments()
353354 do
354355 {
355356 assignment . Position = assignment . Position - 1 ;
356- } while ( Settings . ApplicationProfileAssignments . Count ( a => a . Position == assignment . Position ) > 1 ) ;
357+ } while ( Settings . ApplicationProfileAssignments . Count ( a => a . Position == assignment . Position ) > 1 ) ;
357358 }
358359 }
359360
@@ -374,7 +375,7 @@ private void StartApplication(ApplicationItem application)
374375 DisplayManager . Instance . ActivateHDR ( ) ;
375376 System . Threading . Thread . Sleep ( 2500 ) ;
376377 application . StartApplication ( ) ;
377-
378+
378379 }
379380 catch ( Exception ex )
380381 {
@@ -419,7 +420,7 @@ public void Start()
419420 DisplayManager . Instance . StartMonitoring ( ) ;
420421 Globals . Logs . Add ( $ "HDR Monitoring started", false ) ;
421422 Started = true ;
422- // UpdateHDRModeBasedOnCurrentApplication();
423+ // UpdateHDRModeBasedOnCurrentApplication();
423424 }
424425 }
425426
@@ -451,7 +452,9 @@ private void AddAssignment()
451452 {
452453 if ( ! Settings . ApplicationProfileAssignments . Any ( pi => pi . Application . ApplicationFilePath == adder . ApplicationItem . ApplicationFilePath ) )
453454 {
454- ApplicationProfileAssignment . NewAssigment ( adder . ApplicationItem ) ;
455+ var assignment = ApplicationProfileAssignment . NewAssigment ( adder . ApplicationItem ) ;
456+ if ( Settings . DefaultProfile != null )
457+ assignment . Profile = Settings . DefaultProfile ;
455458 }
456459 Settings . ApplicationProfileAssignments . Sort ( x => x . Position , System . ComponentModel . ListSortDirection . Ascending ) ;
457460
@@ -615,76 +618,86 @@ private void ProfileActions_CollectionChanged(object sender, NotifyCollectionCha
615618 Globals . Instance . SaveSettings ( ) ;
616619 }
617620
621+ readonly object _lockAssignments = new object ( ) ;
622+
618623 private void ApplicationProfileAssigments_CollectionChanged ( object sender , NotifyCollectionChangedEventArgs e )
619624 {
620- SortableObservableCollection < ApplicationProfileAssignment > collection = ( SortableObservableCollection < ApplicationProfileAssignment > ) sender ;
621- switch ( e . Action )
625+ bool taken = Monitor . TryEnter ( _lockAssignments ) ;
626+ if ( ! taken )
627+ return ;
628+ try
622629 {
623- case NotifyCollectionChangedAction . Add :
624-
625- foreach ( ApplicationProfileAssignment assignment in e . NewItems )
626- {
630+ SortableObservableCollection < ApplicationProfileAssignment > collection = ( SortableObservableCollection < ApplicationProfileAssignment > ) sender ;
631+ switch ( e . Action )
632+ {
633+ case NotifyCollectionChangedAction . Add :
627634
635+ foreach ( ApplicationProfileAssignment assignment in e . NewItems )
636+ {
628637
629- Globals . Logs . Add ( $ "Application added: { assignment . Application . ApplicationName } ", false ) ;
630- assignment . PropertyChanged += SaveSettingsOnPropertyChanged ;
631- ApplicationWatcher . AddProcess ( assignment . Application ) ;
632- assignment . Application . PropertyChanged += SaveSettingsOnPropertyChanged ;
633- }
634-
635- break ;
636- case NotifyCollectionChangedAction . Remove :
637- foreach ( ApplicationProfileAssignment assignment in e . OldItems )
638- {
639- Globals . Logs . Add ( $ "Application removed: { assignment . Application . ApplicationName } ", false ) ;
640- assignment . PropertyChanged -= SaveSettingsOnPropertyChanged ;
641638
639+ Globals . Logs . Add ( $ "Application added: { assignment . Application . ApplicationName } ", false ) ;
640+ assignment . PropertyChanged += SaveSettingsOnPropertyChanged ;
641+ ApplicationWatcher . AddProcess ( assignment . Application ) ;
642+ assignment . Application . PropertyChanged += SaveSettingsOnPropertyChanged ;
643+ }
642644
643- int removedPosition = assignment . Position ;
644- foreach ( ApplicationProfileAssignment a in collection )
645+ break ;
646+ case NotifyCollectionChangedAction . Remove :
647+ foreach ( ApplicationProfileAssignment assignment in e . OldItems )
645648 {
646- if ( a . Position >= removedPosition )
647- a . Position = a . Position - 1 ;
649+ Globals . Logs . Add ( $ "Application removed: { assignment . Application . ApplicationName } ", false ) ;
650+ assignment . PropertyChanged -= SaveSettingsOnPropertyChanged ;
651+
652+
653+ int removedPosition = assignment . Position ;
654+ foreach ( ApplicationProfileAssignment a in collection )
655+ {
656+ if ( a . Position >= removedPosition )
657+ a . Position = a . Position - 1 ;
658+ }
659+ ApplicationWatcher . RemoveProcess ( assignment . Application ) ;
660+ assignment . Application . PropertyChanged -= SaveSettingsOnPropertyChanged ;
648661 }
649- ApplicationWatcher . RemoveProcess ( assignment . Application ) ;
650- assignment . Application . PropertyChanged -= SaveSettingsOnPropertyChanged ;
651- }
652662
653- break ;
654- case NotifyCollectionChangedAction . Move :
663+ break ;
664+ case NotifyCollectionChangedAction . Move :
665+ int downFrom = e . NewStartingIndex ;
666+ int upFrom = e . OldStartingIndex ;
655667
656- int up , down , delta ;
668+ if ( e . OldStartingIndex == e . NewStartingIndex )
669+ break ;
657670
658- if ( e . OldStartingIndex < e . NewStartingIndex )
659- {
660- up = e . OldStartingIndex + 1 ;
661- down = e . NewStartingIndex ;
662- delta = - 1 ;
663- }
664- else
665- {
666- up = e . NewStartingIndex ;
667- down = e . OldStartingIndex - 1 ;
668- delta = 1 ;
669- }
670671
671- foreach ( ApplicationProfileAssignment assingment in collection )
672- {
673- int position = assingment . Position ;
674- if ( position == e . OldStartingIndex )
675- {
676- assingment . Position = e . NewStartingIndex ;
677- }
678- else if ( down <= position && position <= up )
672+
673+ foreach ( ApplicationProfileAssignment assingment in collection )
679674 {
680- assingment . Position = position + delta ;
675+ int position = assingment . Position ;
676+ if ( position == e . OldStartingIndex )
677+ {
678+ assingment . Position = e . NewStartingIndex ;
679+ }
680+ else if ( e . OldStartingIndex > e . NewStartingIndex && position < e . OldStartingIndex && position >= e . NewStartingIndex )
681+ {
682+ assingment . Position = position + 1 ;
683+ }
684+ else if ( e . OldStartingIndex < e . NewStartingIndex && position > e . OldStartingIndex && position <= e . NewStartingIndex )
685+ {
686+ assingment . Position = position - 1 ;
687+ }
681688 }
682- }
683- break ;
689+
690+ break ;
691+ }
692+ Globals . Instance . SaveSettings ( ) ;
693+ }
694+ finally
695+ {
696+ if ( taken )
697+ Monitor . Exit ( _lockAssignments ) ;
684698 }
685- Globals . Instance . SaveSettings ( ) ;
686699 }
687-
700+
688701
689702
690703 private void SaveSettingsOnPropertyChanged ( object sender , PropertyChangedEventArgs e )
0 commit comments