Skip to content

Commit e9d7fe6

Browse files
yaira20x5bfa
authored andcommitted
Init
1 parent b8f9029 commit e9d7fe6

File tree

11 files changed

+107
-57
lines changed

11 files changed

+107
-57
lines changed
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
// Copyright (c) 2024 Files Community
22
// Licensed under the MIT License. See the LICENSE.
33

4-
using System.ComponentModel;
5-
64
namespace Files.App.Data.Contexts
75
{
86
public interface IWindowContext : INotifyPropertyChanged
97
{
108
bool IsCompactOverlay { get; }
9+
10+
/// <inheritdoc cref="IWindowsSecurityService.IsAppElevated"/>
11+
bool IsRunningAsAdmin { get; }
12+
13+
/// <inheritdoc cref="IWindowsSecurityService.CanDragAndDrop"/>
14+
bool CanDragAndDrop { get; }
1115
}
1216
}

src/Files.App/Data/Contexts/Window/WindowContext.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
11
// Copyright (c) 2024 Files Community
22
// Licensed under the MIT License. See the LICENSE.
33

4-
using CommunityToolkit.Mvvm.ComponentModel;
54
using Microsoft.UI.Windowing;
65

76
namespace Files.App.Data.Contexts
87
{
8+
/// <inheritdoc cref="IWindowContext"/>
99
internal sealed class WindowContext : ObservableObject, IWindowContext
1010
{
11+
private IWindowsSecurityService WindowsSecurityService = Ioc.Default.GetRequiredService<IWindowsSecurityService>();
12+
1113
private bool isCompactOverlay;
14+
/// <inheritdoc/>
1215
public bool IsCompactOverlay => isCompactOverlay;
1316

17+
/// <inheritdoc/>
18+
public bool IsRunningAsAdmin { get; private set; }
19+
20+
/// <inheritdoc/>
21+
public bool CanDragAndDrop { get; private set; }
22+
1423
public WindowContext()
1524
{
25+
IsRunningAsAdmin = WindowsSecurityService.IsAppElevated();
26+
CanDragAndDrop = WindowsSecurityService.CanDragAndDrop();
27+
1628
MainWindow.Instance.PresenterChanged += Window_PresenterChanged;
1729
}
1830

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) 2024 Files Community
2+
// Licensed under the MIT License. See the LICENSE.
3+
4+
namespace Files.App.Data.Contracts
5+
{
6+
/// <summary>
7+
/// Provides service for security APIs on Windows.
8+
/// </summary>
9+
public interface IWindowsSecurityService
10+
{
11+
/// <summary>
12+
/// Gets a value that indicates whether the application is elevated.
13+
/// </summary>
14+
/// <returns>Returns true if the application is elevated; otherwise, false.</returns>
15+
bool IsAppElevated();
16+
17+
/// <summary>
18+
/// Gets a value that indicates whether the application can drag &amp; drop.
19+
/// </summary>
20+
/// <remarks>
21+
/// Drag &amp; drop onto an elevated app is not allowed (just crashes) due to UIPI.
22+
/// <br/>
23+
/// <br/>
24+
/// For more info, visit:
25+
/// <br/>
26+
/// <a href="https://github.com/files-community/Files/issues/12390"/>
27+
/// <br/>
28+
/// <a href="https://github.com/microsoft/terminal/issues/12017#issuecomment-1004129669"/>
29+
/// </remarks>
30+
/// <returns>Returns true if the application can drag &amp; drop; otherwise, false.</returns>
31+
bool CanDragAndDrop();
32+
33+
/// <summary>
34+
/// Gets a value that indicates whether the application needs to be elevated for some operations.
35+
/// </summary>
36+
/// <param name="path"></param>
37+
/// <returns>True if the application needs to be elevated for some operations; otherwise, false.</returns>
38+
bool IsElevationRequired(string path);
39+
}
40+
}

src/Files.App/Helpers/Application/AppLifecycleHelper.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ public static IHost ConfigureHost()
166166
// Services
167167
.AddSingleton<IWindowsIniService, WindowsIniService>()
168168
.AddSingleton<IWindowsWallpaperService, WindowsWallpaperService>()
169+
.AddSingleton<IWindowsSecurityService, WindowsSecurityService>()
169170
.AddSingleton<IAppThemeModeService, AppThemeModeService>()
170171
.AddSingleton<IDialogService, DialogService>()
171172
.AddSingleton<ICommonDialogService, CommonDialogService>()

src/Files.App/Helpers/Environment/ElevationHelpers.cs

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) 2024 Files Community
2+
// Licensed under the MIT License. See the LICENSE.
3+
4+
namespace Files.App.Services
5+
{
6+
/// <inheritdoc cref="IWindowsSecurityService"/>
7+
public sealed class WindowsSecurityService : IWindowsSecurityService
8+
{
9+
/// <inheritdoc/>
10+
public unsafe bool IsAppElevated()
11+
{
12+
var identity = System.Security.Principal.WindowsIdentity.GetCurrent();
13+
var principal = new System.Security.Principal.WindowsPrincipal(identity);
14+
return principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator);
15+
}
16+
17+
/// <inheritdoc/>
18+
public unsafe bool CanDragAndDrop()
19+
{
20+
return !IsAppElevated();
21+
}
22+
23+
/// <inheritdoc/>
24+
public bool IsElevationRequired(string path)
25+
{
26+
if (string.IsNullOrEmpty(path))
27+
return false;
28+
29+
return Win32PInvoke.IsElevationRequired(path);
30+
}
31+
}
32+
}

src/Files.App/UserControls/TabBar/TabBar.xaml.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public sealed partial class TabBar : BaseTabBar, INotifyPropertyChanged
1818

1919
private readonly ICommandManager Commands = Ioc.Default.GetRequiredService<ICommandManager>();
2020
private readonly IAppearanceSettingsService AppearanceSettingsService = Ioc.Default.GetRequiredService<IAppearanceSettingsService>();
21+
private readonly IWindowContext WindowContext = Ioc.Default.GetRequiredService<IWindowContext>();
2122

2223
// Fields
2324

@@ -44,12 +45,8 @@ public sealed partial class TabBar : BaseTabBar, INotifyPropertyChanged
4445
public bool ShowTabActionsButton
4546
=> AppearanceSettingsService.ShowTabActions;
4647

47-
// Dragging makes the app crash when run as admin.
48-
// For more information:
49-
// - https://github.com/files-community/Files/issues/12390
50-
// - https://github.com/microsoft/terminal/issues/12017#issuecomment-1004129669
5148
public bool AllowTabsDrag
52-
=> !ElevationHelpers.IsAppRunAsAdmin();
49+
=> WindowContext.CanDragAndDrop;
5350

5451
public Rectangle DragArea
5552
=> DragAreaRectangle;
@@ -172,7 +169,7 @@ private void TabView_TabStripDragOver(object sender, DragEventArgs e)
172169
{
173170
if (e.DataView.Properties.ContainsKey(TabPathIdentifier))
174171
{
175-
HorizontalTabView.CanReorderTabs = true && !ElevationHelpers.IsAppRunAsAdmin();
172+
HorizontalTabView.CanReorderTabs = WindowContext.CanDragAndDrop;
176173

177174
e.AcceptedOperation = DataPackageOperation.Move;
178175
e.DragUIOverride.Caption = "TabStripDragAndDropUIOverrideCaption".GetLocalizedResource();
@@ -187,12 +184,12 @@ private void TabView_TabStripDragOver(object sender, DragEventArgs e)
187184

188185
private void TabView_DragLeave(object sender, DragEventArgs e)
189186
{
190-
HorizontalTabView.CanReorderTabs = true && !ElevationHelpers.IsAppRunAsAdmin();
187+
HorizontalTabView.CanReorderTabs = WindowContext.CanDragAndDrop;
191188
}
192189

193190
private async void TabView_TabStripDrop(object sender, DragEventArgs e)
194191
{
195-
HorizontalTabView.CanReorderTabs = true && !ElevationHelpers.IsAppRunAsAdmin();
192+
HorizontalTabView.CanReorderTabs = WindowContext.CanDragAndDrop;
196193

197194
if (!(sender is TabView tabStrip))
198195
return;

src/Files.App/ViewModels/ShellViewModel.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public sealed class ShellViewModel : ObservableObject, IDisposable
5555
private readonly IFileTagsSettingsService fileTagsSettingsService = Ioc.Default.GetRequiredService<IFileTagsSettingsService>();
5656
private readonly ISizeProvider folderSizeProvider = Ioc.Default.GetRequiredService<ISizeProvider>();
5757
private readonly IStorageCacheService fileListCache = Ioc.Default.GetRequiredService<IStorageCacheService>();
58+
private readonly IWindowsSecurityService WindowsSecurityService = Ioc.Default.GetRequiredService<IWindowsSecurityService>();
5859

5960
// Only used for Binding and ApplyFilesAndFoldersChangesAsync, don't manipulate on this!
6061
public BulkConcurrentObservableCollection<ListedItem> FilesAndFolders { get; }
@@ -1268,9 +1269,7 @@ private bool CheckElevationRights(ListedItem item)
12681269
if (item.SyncStatusUI.LoadSyncStatus)
12691270
return false;
12701271

1271-
return item.IsShortcut
1272-
? ElevationHelpers.IsElevationRequired(((ShortcutItem)item).TargetPath)
1273-
: ElevationHelpers.IsElevationRequired(item.ItemPath);
1272+
return WindowsSecurityService.IsElevationRequired(item.IsShortcut ? ((ShortcutItem)item).TargetPath : item.ItemPath);
12741273
}
12751274

12761275
public async Task LoadGitPropertiesAsync(GitItem gitItem)

src/Files.App/Views/Layouts/BaseLayoutPage.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public abstract class BaseLayoutPage : Page, IBaseLayoutPage, INotifyPropertyCha
3939
protected IUserSettingsService UserSettingsService { get; } = Ioc.Default.GetService<IUserSettingsService>()!;
4040
protected ICommandManager Commands { get; } = Ioc.Default.GetRequiredService<ICommandManager>();
4141
public InfoPaneViewModel InfoPaneViewModel { get; } = Ioc.Default.GetRequiredService<InfoPaneViewModel>();
42+
protected readonly IWindowContext WindowContext = Ioc.Default.GetRequiredService<IWindowContext>();
4243

4344
// ViewModels
4445

@@ -82,10 +83,8 @@ public CurrentInstanceViewModel? InstanceViewModel
8283
public static AppModel AppModel
8384
=> App.AppModel;
8485

85-
// NOTE: Dragging makes the app crash when run as admin. (#12390)
86-
// For more information, visit https://github.com/microsoft/terminal/issues/12017#issuecomment-1004129669
8786
public bool AllowItemDrag
88-
=> !ElevationHelpers.IsAppRunAsAdmin();
87+
=> WindowContext.CanDragAndDrop;
8988

9089
public CommandBarFlyout ItemContextMenuFlyout { get; set; } = new()
9190
{

src/Files.App/Views/MainPage.xaml.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@
1212
using Microsoft.UI.Xaml.Controls;
1313
using Microsoft.UI.Xaml.Input;
1414
using Microsoft.UI.Xaml.Navigation;
15-
using Sentry;
16-
using System.Data;
1715
using Windows.ApplicationModel;
18-
using Windows.ApplicationModel.DataTransfer;
1916
using Windows.Foundation.Metadata;
2017
using Windows.Graphics;
2118
using Windows.Services.Store;
@@ -28,24 +25,17 @@ public sealed partial class MainPage : Page
2825
{
2926
private IGeneralSettingsService generalSettingsService { get; } = Ioc.Default.GetRequiredService<IGeneralSettingsService>();
3027
public IUserSettingsService UserSettingsService { get; }
31-
28+
private readonly IWindowContext WindowContext = Ioc.Default.GetRequiredService<IWindowContext>();
3229
public ICommandManager Commands { get; }
33-
34-
public IWindowContext WindowContext { get; }
35-
3630
public SidebarViewModel SidebarAdaptiveViewModel { get; }
37-
3831
public MainPageViewModel ViewModel { get; }
39-
4032
public StatusCenterViewModel OngoingTasksViewModel { get; }
4133

4234
public static AppModel AppModel
4335
=> App.AppModel;
4436

4537
private bool keyReleased = true;
4638

47-
private bool isAppRunningAsAdmin => ElevationHelpers.IsAppRunAsAdmin();
48-
4939
private DispatcherQueueTimer _updateDateDisplayTimer;
5040

5141
public MainPage()
@@ -55,7 +45,6 @@ public MainPage()
5545
// Dependency Injection
5646
UserSettingsService = Ioc.Default.GetRequiredService<IUserSettingsService>();
5747
Commands = Ioc.Default.GetRequiredService<ICommandManager>();
58-
WindowContext = Ioc.Default.GetRequiredService<IWindowContext>();
5948
SidebarAdaptiveViewModel = Ioc.Default.GetRequiredService<SidebarViewModel>();
6049
SidebarAdaptiveViewModel.PaneFlyout = (MenuFlyout)Resources["SidebarContextMenu"];
6150
ViewModel = Ioc.Default.GetRequiredService<MainPageViewModel>();
@@ -307,7 +296,7 @@ private void Page_Loaded(object sender, RoutedEventArgs e)
307296
if
308297
(
309298
AppLifecycleHelper.AppEnvironment is not AppEnvironment.Dev &&
310-
isAppRunningAsAdmin &&
299+
WindowContext.IsRunningAsAdmin &&
311300
UserSettingsService.ApplicationSettingsService.ShowRunningAsAdminPrompt
312301
)
313302
{

src/Files.App/Views/Settings/TagsPage.xaml.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,17 @@ namespace Files.App.Views.Settings
1212
{
1313
public sealed partial class TagsPage : Page
1414
{
15+
private readonly IWindowContext WindowContext = Ioc.Default.GetRequiredService<IWindowContext>();
16+
1517
private string oldTagName = string.Empty;
1618

1719
// Will be null unless the user has edited any tag
1820
private ListedTagViewModel? editingTag;
1921

2022
private FlyoutBase? deleteItemFlyout;
2123

22-
// See issue #12390 on Github. Dragging makes the app crash when run as admin.
23-
// Further reading: https://github.com/microsoft/terminal/issues/12017#issuecomment-1004129669
24-
public bool AllowItemsDrag => !ElevationHelpers.IsAppRunAsAdmin();
24+
public bool AllowItemsDrag
25+
=> WindowContext.CanDragAndDrop;
2526

2627
public TagsPage()
2728
{

0 commit comments

Comments
 (0)