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() private void SetupSideBar()
{ {
var drives = DriveInfo.GetDrives(); foreach (var drive in DriveInfo.GetDrives())
foreach (var drive in drives)
{ {
this.drives.Add(new SideBarItem this.drives.Add(new SideBarItem(drive.Name, drive.Name, FontAwesomeIcon.Server));
{
Icon = FontAwesomeIcon.Server,
Location = drive.Name,
Text = drive.Name,
});
} }
var personal = Path.GetDirectoryName(Environment.GetFolderPath(Environment.SpecialFolder.Personal)); var personal = Path.GetDirectoryName(Environment.GetFolderPath(Environment.SpecialFolder.Personal));
this.quickAccess.Add(new SideBarItem 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));
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",
});
if (!string.IsNullOrEmpty(personal)) if (!string.IsNullOrEmpty(personal))
{ {
this.quickAccess.Add(new SideBarItem this.quickAccess.Add(new SideBarItem("Downloads", Path.Combine(personal, "Downloads"), FontAwesomeIcon.Download));
{
Icon = FontAwesomeIcon.Download,
Location = Path.Combine(personal, "Downloads"),
Text = "Downloads",
});
} }
this.quickAccess.Add(new SideBarItem 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));
Icon = FontAwesomeIcon.Star, this.quickAccess.Add(new SideBarItem("Pictures", Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), FontAwesomeIcon.Image));
Location = Environment.GetFolderPath(Environment.SpecialFolder.Favorites), this.quickAccess.Add(new SideBarItem("Videos", Environment.GetFolderPath(Environment.SpecialFolder.MyVideos), FontAwesomeIcon.Video));
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",
});
} }
private void SortFields(SortingField sortingField, bool canChangeOrder = false) private void SortFields(SortingField sortingField, bool canChangeOrder = false)

View file

@ -1,5 +1,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Numerics; using System.Numerics;
using Dalamud.Utility;
namespace Dalamud.Interface.ImGuiFileDialog namespace Dalamud.Interface.ImGuiFileDialog
{ {
@ -19,7 +21,27 @@ namespace Dalamud.Interface.ImGuiFileDialog
public string FileModifiedDate; 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 private struct FilterStruct
{ {

View file

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

View file

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