Skip to content

Commit 1f1c9e4

Browse files
authored
Fixed a crash that would occur when viewing properties for drives and multiple items (#2345)
1 parent 0bb34ac commit 1f1c9e4

File tree

5 files changed

+100
-37
lines changed

5 files changed

+100
-37
lines changed

Files/View Models/Properties/CombinedProperties.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public CombinedProperties(SelectedItemsPropertiesViewModel viewModel, Cancellati
2727
List = listedItems;
2828
AppInstance = instance;
2929
GetBaseProperties();
30+
ViewModel.PropertyChanged += ViewModel_PropertyChanged;
3031
}
3132

3233
public override void GetBaseProperties()
@@ -49,6 +50,12 @@ public override void GetBaseProperties()
4950

5051
public override async void GetSpecialProperties()
5152
{
53+
if (List.All(x => x.PrimaryItemAttribute == StorageItemTypes.File))
54+
{
55+
ViewModel.IsReadOnly = List.All(x => NativeFileOperationsHelper.HasFileAttribute(x.ItemPath, System.IO.FileAttributes.ReadOnly));
56+
}
57+
ViewModel.IsHidden = List.All(x => NativeFileOperationsHelper.HasFileAttribute(x.ItemPath, System.IO.FileAttributes.Hidden));
58+
5259
ViewModel.LastSeparatorVisibility = Visibility.Collapsed;
5360
ViewModel.ItemSizeVisibility = Visibility.Visible;
5461

@@ -86,5 +93,36 @@ public override async void GetSpecialProperties()
8693
+ " (" + ByteSize.FromBytes(totalSize).Bytes.ToString("#,##0") + " " + "ItemSizeBytes".GetLocalized() + ")";
8794
SetItemsCountString();
8895
}
96+
97+
private void ViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
98+
{
99+
switch (e.PropertyName)
100+
{
101+
case "IsReadOnly":
102+
if (ViewModel.IsReadOnly)
103+
{
104+
List.ForEach(x => NativeFileOperationsHelper.SetFileAttribute(
105+
x.ItemPath, System.IO.FileAttributes.ReadOnly));
106+
}
107+
else
108+
{
109+
List.ForEach(x => NativeFileOperationsHelper.UnsetFileAttribute(
110+
x.ItemPath, System.IO.FileAttributes.ReadOnly));
111+
}
112+
break;
113+
case "IsHidden":
114+
if (ViewModel.IsHidden)
115+
{
116+
List.ForEach(x => NativeFileOperationsHelper.SetFileAttribute(
117+
x.ItemPath, System.IO.FileAttributes.Hidden));
118+
}
119+
else
120+
{
121+
List.ForEach(x => NativeFileOperationsHelper.UnsetFileAttribute(
122+
x.ItemPath, System.IO.FileAttributes.Hidden));
123+
}
124+
break;
125+
}
126+
}
89127
}
90128
}

Files/View Models/Properties/FileProperties.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,11 @@ public override void GetBaseProperties()
218218

219219
public override async void GetSpecialProperties()
220220
{
221+
ViewModel.IsReadOnly = NativeFileOperationsHelper.HasFileAttribute(
222+
Item.ItemPath, System.IO.FileAttributes.ReadOnly);
223+
ViewModel.IsHidden = NativeFileOperationsHelper.HasFileAttribute(
224+
Item.ItemPath, System.IO.FileAttributes.Hidden);
225+
221226
ViewModel.ItemSizeVisibility = Visibility.Visible;
222227
ViewModel.ItemSize = ByteSize.FromBytes(Item.FileSizeBytes).ToBinaryString().ConvertSizeAbbreviation()
223228
+ " (" + ByteSize.FromBytes(Item.FileSizeBytes).Bytes.ToString("#,##0") + " " + "ItemSizeBytes".GetLocalized() + ")";
@@ -463,6 +468,30 @@ private async void ViewModel_PropertyChanged(object sender, System.ComponentMode
463468
{
464469
switch (e.PropertyName)
465470
{
471+
case "IsReadOnly":
472+
if (ViewModel.IsReadOnly)
473+
{
474+
NativeFileOperationsHelper.SetFileAttribute(
475+
Item.ItemPath, System.IO.FileAttributes.ReadOnly);
476+
}
477+
else
478+
{
479+
NativeFileOperationsHelper.UnsetFileAttribute(
480+
Item.ItemPath, System.IO.FileAttributes.ReadOnly);
481+
}
482+
break;
483+
case "IsHidden":
484+
if (ViewModel.IsHidden)
485+
{
486+
NativeFileOperationsHelper.SetFileAttribute(
487+
Item.ItemPath, System.IO.FileAttributes.Hidden);
488+
}
489+
else
490+
{
491+
NativeFileOperationsHelper.UnsetFileAttribute(
492+
Item.ItemPath, System.IO.FileAttributes.Hidden);
493+
}
494+
break;
466495
case "ShortcutItemPath":
467496
case "ShortcutItemWorkingDir":
468497
case "ShortcutItemArguments":

Files/View Models/Properties/FolderProperties.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ public override void GetBaseProperties()
7474

7575
public async override void GetSpecialProperties()
7676
{
77+
ViewModel.IsHidden = NativeFileOperationsHelper.HasFileAttribute(
78+
Item.ItemPath, System.IO.FileAttributes.Hidden);
79+
7780
if (Item.IsShortcutItem)
7881
{
7982
ViewModel.ItemSizeVisibility = Visibility.Visible;
@@ -227,6 +230,18 @@ private async void ViewModel_PropertyChanged(object sender, System.ComponentMode
227230
{
228231
switch (e.PropertyName)
229232
{
233+
case "IsHidden":
234+
if (ViewModel.IsHidden)
235+
{
236+
NativeFileOperationsHelper.SetFileAttribute(
237+
Item.ItemPath, System.IO.FileAttributes.Hidden);
238+
}
239+
else
240+
{
241+
NativeFileOperationsHelper.UnsetFileAttribute(
242+
Item.ItemPath, System.IO.FileAttributes.Hidden);
243+
}
244+
break;
230245
case "ShortcutItemPath":
231246
case "ShortcutItemWorkingDir":
232247
case "ShortcutItemArguments":

Files/View Models/SelectedItemsPropertiesViewModel.cs

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -768,52 +768,32 @@ public Visibility DetailsSectionVisibility_Media
768768
set => SetProperty(ref detailsSectionVisibility_Media, value);
769769
}
770770

771+
private bool isReadOnly;
772+
771773
public bool IsReadOnly
772774
{
773-
get
774-
{
775-
return NativeFileOperationsHelper.HasFileAttribute(
776-
Path.Combine(ItemPath, ItemName), FileAttributes.ReadOnly);
777-
}
778-
775+
get => isReadOnly;
779776
set
780777
{
781-
if (value)
782-
{
783-
NativeFileOperationsHelper.SetFileAttribute(
784-
Path.Combine(ItemPath, ItemName), FileAttributes.ReadOnly);
785-
}
786-
else
787-
{
788-
NativeFileOperationsHelper.UnsetFileAttribute(
789-
Path.Combine(ItemPath, ItemName), FileAttributes.ReadOnly);
790-
}
791-
base.OnPropertyChanged(nameof(IsReadOnly));
778+
IsReadOnlyEnabled = true;
779+
SetProperty(ref isReadOnly, value);
792780
}
793781
}
794782

795-
public bool IsHidden
783+
private bool isReadOnlyEnabled;
784+
785+
public bool IsReadOnlyEnabled
796786
{
797-
get
798-
{
799-
return NativeFileOperationsHelper.HasFileAttribute(
800-
Path.Combine(ItemPath, ItemName), FileAttributes.Hidden);
801-
}
787+
get => isReadOnlyEnabled;
788+
set => SetProperty(ref isReadOnlyEnabled, value);
789+
}
802790

803-
set
804-
{
805-
if (value)
806-
{
807-
NativeFileOperationsHelper.SetFileAttribute(
808-
Path.Combine(ItemPath, ItemName), FileAttributes.Hidden);
809-
}
810-
else
811-
{
812-
NativeFileOperationsHelper.UnsetFileAttribute(
813-
Path.Combine(ItemPath, ItemName), FileAttributes.Hidden);
814-
}
815-
base.OnPropertyChanged(nameof(IsHidden));
816-
}
791+
private bool isHidden;
792+
793+
public bool IsHidden
794+
{
795+
get => isHidden;
796+
set => SetProperty(ref isHidden, value);
817797
}
818798
}
819799
}

Files/Views/Pages/PropertiesGeneral.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@
362362
<CheckBox
363363
x:Uid="PropertiesDialogReadOnly"
364364
Content="Read-only"
365+
IsEnabled="{x:Bind ViewModel.IsReadOnlyEnabled, Mode=OneWay}"
365366
IsChecked="{x:Bind ViewModel.IsReadOnly, Mode=TwoWay}"/>
366367
<CheckBox
367368
x:Uid="PropertiesDialogHidden"

0 commit comments

Comments
 (0)