-
Notifications
You must be signed in to change notification settings - Fork 12
feature: choose custom install location #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AlvinSchiller
wants to merge
16
commits into
MarcellVokk:master
Choose a base branch
from
AlvinSchiller:feature/installLocationChooser
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
001b7ca
feat: added FolderPicker for installation popup to choose installatio…
AlvinSchiller ad30719
fix: Move FolderPicker
AlvinSchiller 8972c6c
fix: refactored code to DriveUtils class
AlvinSchiller 6191e6e
fix: update folderpicker
AlvinSchiller d754015
feat: Location selektion updates.
AlvinSchiller 58b32f0
feat: update layout and simplify error handling
AlvinSchiller b0d0392
fix: cleanup
AlvinSchiller b8f5d6c
fix: cleanup
AlvinSchiller 271d6a4
fix: Revert: introduce in another pr
AlvinSchiller 6015819
fix: use existing toggleable element. align style to system settings …
AlvinSchiller dc160c8
fix: use Win32 FolderDialog
AlvinSchiller aaeb4be
fix: update variable names
AlvinSchiller 4c5d8da
fix: update subelement style
AlvinSchiller 36c3fe1
fix: set HiglightButton style for folderselection button
AlvinSchiller b6fd1be
fix: Update advanced folder selection ui
AlvinSchiller 0490cce
fix: Update advanced folder selection ui. refactor to single button.
AlvinSchiller File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
110
src/BfmeFoundationProject_AllInOneLauncher/Elements/Native/FolderPicker.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| using System; | ||
| using System.Runtime.InteropServices; | ||
| using System.Text; | ||
| using System.Windows; | ||
|
|
||
| namespace BfmeFoundationProject.AllInOneLauncher.Elements.Native; | ||
|
|
||
| internal static class FolderPicker | ||
| { | ||
| public static string? ShowDialog(Window owner, string title = "", string? initialPath = null) | ||
| { | ||
| IntPtr ownerHandle = new System.Windows.Interop.WindowInteropHelper(owner).Handle; | ||
| return InternalShowDialog(ownerHandle, title, initialPath); | ||
| } | ||
|
|
||
| private static string? InternalShowDialog(IntPtr ownerHandle, string title, string? initialPath) | ||
| { | ||
| // Constants | ||
| const int MAX_PATH = 260; | ||
| const uint BIF_RETURNONLYFSDIRS = 0x0001; | ||
| const uint BIF_NEWDIALOGSTYLE = 0x0040; | ||
| const int BFFM_INITIALIZED = 1; | ||
| const int BFFM_SETSELECTIONW = 0x0467; // WM_USER + 103 (Unicode) | ||
|
|
||
| BrowseCallbackProc callback = (hwnd, msg, lParam, lpData) => | ||
| { | ||
| if (msg == BFFM_INITIALIZED && lpData != IntPtr.Zero) | ||
| { | ||
| // lpData contains pointer to initial path (Unicode) | ||
| SendMessage(hwnd, BFFM_SETSELECTIONW, new IntPtr(1), lpData); | ||
| } | ||
| return 0; | ||
| }; | ||
|
|
||
| IntPtr pidl = IntPtr.Zero; | ||
| IntPtr pszDisplayName = IntPtr.Zero; | ||
| IntPtr initialPathPtr = IntPtr.Zero; | ||
| try | ||
| { | ||
| pszDisplayName = Marshal.AllocHGlobal(MAX_PATH * 2); | ||
|
|
||
| var bi = new BROWSEINFO | ||
| { | ||
| hwndOwner = ownerHandle, | ||
| pidlRoot = IntPtr.Zero, | ||
| pszDisplayName = pszDisplayName, | ||
| lpszTitle = title, | ||
| ulFlags = BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE, | ||
| lpfn = callback, | ||
| lParam = IntPtr.Zero, | ||
| iImage = 0 | ||
| }; | ||
|
|
||
| if (!string.IsNullOrWhiteSpace(initialPath)) | ||
| { | ||
| initialPathPtr = Marshal.StringToHGlobalUni(initialPath); | ||
| bi.lParam = initialPathPtr; | ||
| } | ||
|
|
||
| pidl = SHBrowseForFolder(ref bi); | ||
| if (pidl == IntPtr.Zero) | ||
| return null; | ||
|
|
||
| var sb = new StringBuilder(MAX_PATH); | ||
| if (SHGetPathFromIDList(pidl, sb)) | ||
| { | ||
| return sb.ToString(); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| finally | ||
| { | ||
| if (initialPathPtr != IntPtr.Zero) Marshal.FreeHGlobal(initialPathPtr); | ||
| if (pszDisplayName != IntPtr.Zero) Marshal.FreeHGlobal(pszDisplayName); | ||
| if (pidl != IntPtr.Zero) CoTaskMemFree(pidl); | ||
| } | ||
| } | ||
|
|
||
| #region Native | ||
| private delegate int BrowseCallbackProc(IntPtr hwnd, int msg, IntPtr lParam, IntPtr lpData); | ||
|
|
||
| [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] | ||
| private struct BROWSEINFO | ||
| { | ||
| public IntPtr hwndOwner; | ||
| public IntPtr pidlRoot; | ||
| public IntPtr pszDisplayName; | ||
| [MarshalAs(UnmanagedType.LPWStr)] | ||
| public string lpszTitle; | ||
| public uint ulFlags; | ||
| [MarshalAs(UnmanagedType.FunctionPtr)] | ||
| public BrowseCallbackProc lpfn; | ||
| public IntPtr lParam; | ||
| public int iImage; | ||
| } | ||
|
|
||
| [DllImport("shell32.dll", CharSet = CharSet.Unicode)] | ||
| private static extern IntPtr SHBrowseForFolder([In] ref BROWSEINFO lpbi); | ||
|
|
||
| [DllImport("shell32.dll", CharSet = CharSet.Unicode)] | ||
| private static extern bool SHGetPathFromIDList(IntPtr pidl, [Out] StringBuilder pszPath); | ||
|
|
||
| [DllImport("ole32.dll", CharSet = CharSet.Unicode)] | ||
| private static extern void CoTaskMemFree(IntPtr pv); | ||
|
|
||
| [DllImport("user32.dll", CharSet = CharSet.Unicode)] | ||
| private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam); | ||
| #endregion | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 80 additions & 15 deletions
95
src/BfmeFoundationProject_AllInOneLauncher/Popups/InstallGamePopup.xaml.cs
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please don't use this naming convention: "_nameOfSomething". |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,48 +1,113 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.IO; | ||
| using System.Windows; | ||
| using BfmeFoundationProject.AllInOneLauncher.Core.Utils; | ||
| using BfmeFoundationProject.AllInOneLauncher.Elements.Disk; | ||
| using BfmeFoundationProject.AllInOneLauncher.Elements.Generic; | ||
| using BfmeFoundationProject.AllInOneLauncher.Elements.Native; | ||
|
|
||
| namespace BfmeFoundationProject.AllInOneLauncher.Popups; | ||
|
|
||
| public partial class InstallGamePopup : PopupBody | ||
| { | ||
| private static readonly Dictionary<string, DriveInfo> Drives = DriveInfo.GetDrives().ToDictionary(x => x.RootDirectory.FullName); | ||
| private static readonly List<DriveInfo> _drives = DriveUtils.GetValidDrives(); | ||
| private string _defaultPath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86); | ||
|
|
||
| public InstallGamePopup() | ||
| { | ||
| InitializeComponent(); | ||
|
|
||
| locations.Children.Clear(); | ||
| foreach (var drive in Drives.Values) | ||
| if (_drives.Count <= 0) | ||
| { | ||
| if (!drive.IsReady) | ||
| continue; | ||
|
|
||
| try | ||
| SelectLocationError.Visibility = Visibility.Visible; | ||
| SelectLocationArea.Visibility = Visibility.Collapsed; | ||
| } | ||
| else | ||
| { | ||
| locations.Children.Clear(); | ||
| _drives.ForEach(drive => | ||
| { | ||
| locations.Children.Add(new Selectable() | ||
| { | ||
| Title = new LibraryDriveHeader() | ||
| { | ||
| LibraryDriveName = string.Concat(drive.VolumeLabel, " (", drive.Name.Replace(@"\", ""), ")"), | ||
| LibraryDriveSize = $"{Math.Floor(drive.AvailableFreeSpace / Math.Pow(1024, 3)):N0} GB {App.Current.FindResource("GenericFree")}", | ||
| LibraryDriveSize = GetDriveFreeSpaceFormatted(drive), | ||
| Mini = true | ||
| }, | ||
| Tag = drive.RootDirectory.FullName, | ||
| Tag = DriveUtils.GetDriveRootName(drive), | ||
| Margin = new Thickness(0, 0, 0, 5), | ||
| UseLayoutRounding = true, | ||
| SnapsToDevicePixels = true | ||
| }); | ||
|
|
||
| }); | ||
|
|
||
| SetSelectedPath(_defaultPath); | ||
| } | ||
| } | ||
|
|
||
| private void SetSelectedPath(string path) | ||
| { | ||
| string? _selectedPath = null; | ||
| var _selectedFreeText = string.Empty; | ||
| try | ||
| { | ||
| _selectedPath = DriveUtils.GetValidPath(path); | ||
| var drive = DriveUtils.GetDriveForPath(_drives, _selectedPath); | ||
| if (drive == null) | ||
| { | ||
| _selectedPath = DriveUtils.GetValidPath(_defaultPath); | ||
| drive = DriveUtils.GetDriveForPath(_drives, _selectedPath); | ||
| } | ||
| catch { } | ||
|
|
||
| if (drive != null) | ||
| { | ||
| _selectedFreeText = GetDriveFreeSpaceFormatted(drive); | ||
| } | ||
| } | ||
| catch { /* ignore errors here */ } | ||
|
|
||
| SelectedPathText.Text = _selectedPath; | ||
| SelectedFreeSpaceText.Text = _selectedFreeText; | ||
| ButtonAccept.IsEnabled = !string.IsNullOrWhiteSpace(_selectedPath); | ||
| } | ||
|
|
||
| private static string GetDriveFreeSpaceFormatted(DriveInfo drive) | ||
| { | ||
| return $"{Math.Floor(drive.AvailableFreeSpace / Math.Pow(1024, 3)):N0} GB {App.Current.FindResource("GenericFree")}"; | ||
| } | ||
|
|
||
| private void OnSelectFolderClicked(object sender, RoutedEventArgs e) | ||
| { | ||
| try | ||
| { | ||
| var ownerWindow = Window.GetWindow(this); | ||
| var folderDialogTitle = (string)App.Current.FindResource("InstallGamePopupSelectFolder"); | ||
| var _selectedPath = SelectedPathText.Text; | ||
| var selected = FolderPicker.ShowDialog(ownerWindow, folderDialogTitle, _selectedPath); | ||
| if (!string.IsNullOrWhiteSpace(selected)) | ||
| SetSelectedPath(selected); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| PopupVisualizer.ShowPopup(new ErrorPopup(ex)); | ||
| Dismiss(); | ||
| } | ||
| } | ||
|
|
||
| private void OnInstallClicked(object sender, RoutedEventArgs e) => Submit(LanguageDropdown.SelectedValue, Selectable.GetSelectedTagInContainer(locations)!.ToString()!); | ||
| private void OnAdvancedSettingsSwitched(object sender, EventArgs e) | ||
| { | ||
| var isActive = SelectLocationAdvancedToggle.IsToggled; | ||
| SelectLocationList.Visibility = isActive ? Visibility.Collapsed : Visibility.Visible; | ||
| SelectFolderButton.Visibility = isActive ? Visibility.Visible : Visibility.Collapsed; | ||
| } | ||
|
|
||
| private void OnInstallClicked(object sender, RoutedEventArgs e) | ||
| { | ||
| var language = LanguageDropdown.SelectedValue; | ||
| var path = (SelectLocationAdvancedToggle.IsToggled == true) ? SelectedPathText.Text : Selectable.GetSelectedTagInContainer(locations)!.ToString()!; | ||
| Submit(language, path); | ||
| } | ||
|
|
||
| private void OnCancelClicked(object sender, RoutedEventArgs e) => Dismiss(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this necesary? Use Microsoft.Win32.OpenFolderDialog instead, it's built in and as a bonus it's the new style folder browser, not the old style crappy one.