Some general cleanup and store quickbar / drive existence on setup instead of checking per draw. (#887)

This commit is contained in:
Ottermandias 2022-06-19 10:53:51 +02:00 committed by GitHub
parent c0df0f2dee
commit cd41fda202
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 95 additions and 128 deletions

View file

@ -298,70 +298,25 @@ namespace Dalamud.Interface.ImGuiFileDialog
private void SetupSideBar()
{
var drives = DriveInfo.GetDrives();
foreach (var drive in drives)
foreach (var drive in DriveInfo.GetDrives())
{
this.drives.Add(new SideBarItem
{
Icon = FontAwesomeIcon.Server,
Location = drive.Name,
Text = drive.Name,
});
this.drives.Add(new SideBarItem(drive.Name, drive.Name, FontAwesomeIcon.Server));
}
var personal = Path.GetDirectoryName(Environment.GetFolderPath(Environment.SpecialFolder.Personal));
this.quickAccess.Add(new SideBarItem
{
Icon = FontAwesomeIcon.Desktop,
Location = Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
Text = "Desktop",
});
this.quickAccess.Add(new SideBarItem
{
Icon = FontAwesomeIcon.File,
Location = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
Text = "Documents",
});
this.quickAccess.Add(new SideBarItem("Desktop", Environment.GetFolderPath(Environment.SpecialFolder.Desktop), FontAwesomeIcon.Desktop));
this.quickAccess.Add(new SideBarItem("Documents", Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), FontAwesomeIcon.File));
if (!string.IsNullOrEmpty(personal))
{
this.quickAccess.Add(new SideBarItem
{
Icon = FontAwesomeIcon.Download,
Location = Path.Combine(personal, "Downloads"),
Text = "Downloads",
});
this.quickAccess.Add(new SideBarItem("Downloads", Path.Combine(personal, "Downloads"), FontAwesomeIcon.Download));
}
this.quickAccess.Add(new SideBarItem
{
Icon = FontAwesomeIcon.Star,
Location = Environment.GetFolderPath(Environment.SpecialFolder.Favorites),
Text = "Favorites",
});
this.quickAccess.Add(new SideBarItem
{
Icon = FontAwesomeIcon.Music,
Location = Environment.GetFolderPath(Environment.SpecialFolder.MyMusic),
Text = "Music",
});
this.quickAccess.Add(new SideBarItem
{
Icon = FontAwesomeIcon.Image,
Location = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures),
Text = "Pictures",
});
this.quickAccess.Add(new SideBarItem
{
Icon = FontAwesomeIcon.Video,
Location = Environment.GetFolderPath(Environment.SpecialFolder.MyVideos),
Text = "Videos",
});
this.quickAccess.Add(new SideBarItem("Favorites", Environment.GetFolderPath(Environment.SpecialFolder.Favorites), FontAwesomeIcon.Star));
this.quickAccess.Add(new SideBarItem("Music", Environment.GetFolderPath(Environment.SpecialFolder.MyMusic), FontAwesomeIcon.Music));
this.quickAccess.Add(new SideBarItem("Pictures", Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), FontAwesomeIcon.Image));
this.quickAccess.Add(new SideBarItem("Videos", Environment.GetFolderPath(Environment.SpecialFolder.MyVideos), FontAwesomeIcon.Video));
}
private void SortFields(SortingField sortingField, bool canChangeOrder = false)

View file

@ -1,5 +1,7 @@
using System.Collections.Generic;
using System.IO;
using System.Numerics;
using Dalamud.Utility;
namespace Dalamud.Interface.ImGuiFileDialog
{
@ -19,7 +21,27 @@ namespace Dalamud.Interface.ImGuiFileDialog
public string FileModifiedDate;
}
private record struct SideBarItem(string Text, string Location, FontAwesomeIcon Icon);
private readonly struct SideBarItem
{
public SideBarItem(string text, string location, FontAwesomeIcon icon)
{
this.Text = text;
this.Location = location;
this.Icon = icon;
this.Exists = !this.Location.IsNullOrEmpty() && Directory.Exists(this.Location);
}
public string Text { get; init; }
public string Location { get; init; }
public FontAwesomeIcon Icon { get; init; }
public bool Exists { get; init; }
public bool CheckExistence()
=> !this.Location.IsNullOrEmpty() && Directory.Exists(this.Location);
}
private struct FilterStruct
{

View file

@ -148,7 +148,7 @@ namespace Dalamud.Interface.ImGuiFileDialog
private void DrawPathComposer()
{
ImGui.PushFont(UiBuilder.IconFont);
if (ImGui.Button($"{(this.pathInputActivated ? (char)FontAwesomeIcon.Times : (char)FontAwesomeIcon.Edit)}"))
if (ImGui.Button(this.pathInputActivated ? FontAwesomeIcon.Times.ToIconString() : FontAwesomeIcon.Edit.ToIconString()))
{
this.pathInputActivated = !this.pathInputActivated;
}
@ -159,11 +159,9 @@ namespace Dalamud.Interface.ImGuiFileDialog
if (this.pathDecomposition.Count > 0)
{
ImGui.SameLine();
if (this.pathInputActivated)
{
ImGui.PushItemWidth(ImGui.GetContentRegionAvail().X);
ImGui.SetNextItemWidth(ImGui.GetContentRegionAvail().X);
ImGui.InputText("##pathedit", ref this.pathInputBuffer, 255);
ImGui.PopItemWidth();
}
@ -204,7 +202,7 @@ namespace Dalamud.Interface.ImGuiFileDialog
private void DrawSearchBar()
{
ImGui.PushFont(UiBuilder.IconFont);
if (ImGui.Button($"{(char)FontAwesomeIcon.Home}"))
if (ImGui.Button(FontAwesomeIcon.Home.ToIconString()))
{
this.SetPath(".");
}
@ -225,10 +223,8 @@ namespace Dalamud.Interface.ImGuiFileDialog
ImGui.SameLine();
ImGui.TextUnformatted("Search :");
ImGui.SameLine();
ImGui.PushItemWidth(ImGui.GetContentRegionAvail().X);
var edited = ImGui.InputText("##InputImGuiFileDialogSearchField", ref this.searchBuffer, 255);
ImGui.PopItemWidth();
if (edited)
ImGui.SetNextItemWidth(ImGui.GetContentRegionAvail().X);
if (ImGui.InputText("##InputImGuiFileDialogSearchField", ref this.searchBuffer, 255))
{
this.ApplyFilteringOnFileList();
}
@ -240,14 +236,11 @@ namespace Dalamud.Interface.ImGuiFileDialog
if (this.flags.HasFlag(ImGuiFileDialogFlags.DisableCreateDirectoryButton)) return;
ImGui.PushFont(UiBuilder.IconFont);
if (ImGui.Button(FontAwesomeIcon.FolderPlus.ToIconString()))
{
if (!this.createDirectoryMode)
if (ImGui.Button(FontAwesomeIcon.FolderPlus.ToIconString()) && !this.createDirectoryMode)
{
this.createDirectoryMode = true;
this.createDirectoryBuffer = string.Empty;
}
}
ImGui.PopFont();
@ -262,9 +255,8 @@ namespace Dalamud.Interface.ImGuiFileDialog
ImGui.TextUnformatted("New Directory Name");
ImGui.SameLine();
ImGui.PushItemWidth(ImGui.GetContentRegionAvail().X - 100f);
ImGui.SetNextItemWidth(ImGui.GetContentRegionAvail().X - 100f);
ImGui.InputText("##DirectoryFileName", ref this.createDirectoryBuffer, 255);
ImGui.PopItemWidth();
ImGui.SameLine();
@ -293,10 +285,11 @@ namespace Dalamud.Interface.ImGuiFileDialog
if (!this.flags.HasFlag(ImGuiFileDialogFlags.HideSideBar))
{
ImGui.BeginChild("##FileDialog_ColumnChild", size);
if (ImGui.BeginChild("##FileDialog_ColumnChild", size))
{
ImGui.Columns(2, "##FileDialog_Columns");
this.DrawSideBar(new Vector2(150, size.Y));
this.DrawSideBar(size with { X = 150 });
ImGui.SetColumnWidth(0, 150);
ImGui.NextColumn();
@ -304,6 +297,8 @@ namespace Dalamud.Interface.ImGuiFileDialog
this.DrawFileListView(size - new Vector2(160, 0));
ImGui.Columns(1);
}
ImGui.EndChild();
}
else
@ -314,43 +309,44 @@ namespace Dalamud.Interface.ImGuiFileDialog
private void DrawSideBar(Vector2 size)
{
ImGui.BeginChild("##FileDialog_SideBar", size);
if (ImGui.BeginChild("##FileDialog_SideBar", size))
{
ImGui.SetCursorPosY(ImGui.GetCursorPosY() + 5);
var idx = 0;
foreach (var (name, location, icon) in this.drives.Concat(this.quickAccess))
foreach (var qa in this.drives.Concat(this.quickAccess).Where(qa => qa.Exists))
{
if (string.IsNullOrEmpty(location) || !Directory.Exists(location))
{
continue;
}
ImGui.PushID(idx++);
ImGui.SetCursorPosX(25);
if (ImGui.Selectable(name, name == this.selectedSideBar))
if (ImGui.Selectable(qa.Text, qa.Text == this.selectedSideBar) && qa.CheckExistence())
{
this.SetPath(location);
this.selectedSideBar = name;
this.SetPath(qa.Location);
this.selectedSideBar = qa.Text;
}
ImGui.PushFont(UiBuilder.IconFont);
ImGui.SameLine();
ImGui.SetCursorPosX(0);
ImGui.TextUnformatted(icon.ToIconString());
ImGui.TextUnformatted(qa.Icon.ToIconString());
ImGui.PopFont();
ImGui.PopID();
}
}
ImGui.EndChild();
}
private unsafe void DrawFileListView(Vector2 size)
{
ImGui.BeginChild("##FileDialog_FileList", size);
if (!ImGui.BeginChild("##FileDialog_FileList", size))
{
ImGui.EndChild();
return;
}
var tableFlags = ImGuiTableFlags.SizingFixedFit | ImGuiTableFlags.RowBg | ImGuiTableFlags.Hideable | ImGuiTableFlags.ScrollY | ImGuiTableFlags.NoHostExtendX;
const ImGuiTableFlags tableFlags = ImGuiTableFlags.SizingFixedFit | ImGuiTableFlags.RowBg | ImGuiTableFlags.Hideable | ImGuiTableFlags.ScrollY | ImGuiTableFlags.NoHostExtendX;
if (ImGui.BeginTable("##FileTable", 4, tableFlags, size))
{
ImGui.TableSetupScrollFreeze(0, 1);
@ -421,8 +417,7 @@ namespace Dalamud.Interface.ImGuiFileDialog
Icon = FontAwesomeIcon.Folder,
};
ImGui.PushStyleColor(ImGuiCol.Text, item.Color);
if (selected) ImGui.PushStyleColor(ImGuiCol.Text, selectedTextColor);
ImGui.PushStyleColor(ImGuiCol.Text, selected ? selectedTextColor : item.Color);
ImGui.TableNextRow();
@ -451,12 +446,10 @@ namespace Dalamud.Interface.ImGuiFileDialog
if (ImGui.TableNextColumn())
{
var sz = ImGui.CalcTextSize(file.FileModifiedDate);
ImGui.PushItemWidth(sz.X + 5);
ImGui.SetNextItemWidth(sz.X + 5);
ImGui.TextUnformatted(file.FileModifiedDate + " ");
ImGui.PopItemWidth();
}
if (selected) ImGui.PopStyleColor();
ImGui.PopStyleColor();
if (needToBreak) break;
@ -464,6 +457,7 @@ namespace Dalamud.Interface.ImGuiFileDialog
}
clipper.End();
clipper.Destroy();
}
}
@ -512,7 +506,7 @@ namespace Dalamud.Interface.ImGuiFileDialog
this.pathClicked = this.SelectDirectory(file);
return true;
}
else if (this.IsDirectoryMode())
if (this.IsDirectoryMode())
{
this.SelectFileName(file);
}
@ -718,18 +712,17 @@ namespace Dalamud.Interface.ImGuiFileDialog
var selectOnly = this.flags.HasFlag(ImGuiFileDialogFlags.SelectOnly);
ImGui.PushItemWidth(width);
ImGui.SetNextItemWidth(width);
if (selectOnly) ImGui.PushStyleVar(ImGuiStyleVar.Alpha, 0.5f);
ImGui.InputText("##FileName", ref this.fileNameBuffer, 255, selectOnly ? ImGuiInputTextFlags.ReadOnly : ImGuiInputTextFlags.None);
if (selectOnly) ImGui.PopStyleVar();
ImGui.PopItemWidth();
if (this.filters.Count > 0)
{
ImGui.SameLine();
var needToApplyNewFilter = false;
ImGui.PushItemWidth(150f);
ImGui.SetNextItemWidth(150f);
if (ImGui.BeginCombo("##Filters", this.selectedFilter.Filter, ImGuiComboFlags.None))
{
var idx = 0;
@ -749,8 +742,6 @@ namespace Dalamud.Interface.ImGuiFileDialog
ImGui.EndCombo();
}
ImGui.PopItemWidth();
if (needToApplyNewFilter)
{
this.SetPath(this.currentPath);
@ -814,12 +805,11 @@ namespace Dalamud.Interface.ImGuiFileDialog
{ // quit dialog, it doesn't exist anyway
return true;
}
else
{ // already exists, open dialog to confirm overwrite
// already exists, open dialog to confirm overwrite
this.isOk = false;
this.okResultToConfirm = true;
}
}
var name = $"The file Already Exists !##{this.title}{this.id}OverWriteDialog";
var res = false;

View file

@ -229,7 +229,7 @@ namespace Dalamud.Interface.ImGuiFileDialog
var result = this.fileNameBuffer;
// a collection like {.cpp, .h}, so can't decide on an extension
if (this.selectedFilter.CollectionFilters != null && this.selectedFilter.CollectionFilters.Count > 0)
if (this.selectedFilter.CollectionFilters is { Count: > 0 })
{
return result;
}
@ -240,7 +240,7 @@ namespace Dalamud.Interface.ImGuiFileDialog
var lastPoint = result.LastIndexOf('.');
if (lastPoint != -1)
{
result = result.Substring(0, lastPoint);
result = result[..lastPoint];
}
result += this.selectedFilter.Filter;
@ -275,7 +275,7 @@ namespace Dalamud.Interface.ImGuiFileDialog
this.currentPath = dir.FullName;
if (this.currentPath[^1] == Path.DirectorySeparatorChar)
{ // handle selecting a drive, like C: -> C:\
this.currentPath = this.currentPath[0..^1];
this.currentPath = this.currentPath[..^1];
}
this.pathInputBuffer = this.currentPath;