Lumenarium/src/app/editor/panels/foldhaus_panel_file_view.h

130 lines
4.3 KiB
C
Raw Normal View History

2020-03-08 23:15:26 +00:00
//
// File: foldhaus_panel_file_view.h
// Author: Peter Slattery
// Creation Date: 2020-03-08
//
#ifndef FOLDHAUS_PANEL_FILE_VIEW_H
struct file_view_state
{
gs_string WorkingDirectory;
gs_memory_arena FileNamesArena;
gs_file_info_array FileNames;
gs_file_info SelectedFile;
2020-03-08 23:15:26 +00:00
};
2020-10-24 20:28:10 +00:00
internal void
FileView_Exit_(panel* FileViewPanel, app_state* State, context Context)
{
Assert(FileViewPanel->IsModalOverrideFor != 0);
panel* ReturnTo = FileViewPanel->IsModalOverrideFor;
if (ReturnTo->ModalOverrideCB)
{
ReturnTo->ModalOverrideCB(FileViewPanel, State, Context);
}
Panel_PopModalOverride(ReturnTo, &State->PanelSystem);
}
global input_command* FileView_Commands = 0;
2020-03-08 23:15:26 +00:00
s32 FileView_CommandsCount = 0;
internal void
FileViewUpdateWorkingDirectory(gs_const_string WorkingDirectory, file_view_state* State, context Context)
{
ClearArena(&State->FileNamesArena);
gs_const_string SanitizedDirectory = WorkingDirectory;
u32 LastSlashIndex = FindLast(SanitizedDirectory, '\\');
gs_const_string LastDir = Substring(SanitizedDirectory, LastSlashIndex + 1, SanitizedDirectory.Length);
if (StringsEqual(LastDir, ConstString("..")))
{
u32 SecondLastSlashIndex = FindLast(SanitizedDirectory, LastSlashIndex - 1, '\\');
SanitizedDirectory = Substring(SanitizedDirectory, 0, SecondLastSlashIndex);
}
else if (StringsEqual(LastDir, ConstString(".")))
{
SanitizedDirectory = Substring(SanitizedDirectory, 0, LastSlashIndex);
}
State->WorkingDirectory = PushString(&State->FileNamesArena, WorkingDirectory.Length + 2);
PrintF(&State->WorkingDirectory, "%S", SanitizedDirectory);
if (State->WorkingDirectory.Str[State->WorkingDirectory.Length - 1] != '\\')
{
AppendPrintF(&State->WorkingDirectory, "\\");
}
if (State->WorkingDirectory.Str[State->WorkingDirectory.Length - 1] != '*')
{
AppendPrintF(&State->WorkingDirectory, "*");
}
State->FileNames = EnumerateDirectory(Context.ThreadContext.FileHandler, &State->FileNamesArena, State->WorkingDirectory.ConstString, EnumerateDirectory_IncludeDirectories);
}
2020-03-08 23:15:26 +00:00
GSMetaTag(panel_init);
GSMetaTag(panel_type_file_view);
internal void
FileView_Init(panel* Panel, app_state* State, context Context)
2020-03-08 23:15:26 +00:00
{
// TODO: :FreePanelMemory
file_view_state* FileViewState = PushStruct(&State->Permanent, file_view_state);
Panel->StateMemory = StructToData(FileViewState, file_view_state);
FileViewState->FileNamesArena = CreateMemoryArena(Context.ThreadContext.Allocator);
FileViewUpdateWorkingDirectory(ConstString("."), FileViewState, Context);
2020-03-08 23:15:26 +00:00
}
GSMetaTag(panel_cleanup);
GSMetaTag(panel_type_file_view);
internal void
FileView_Cleanup(panel* Panel, app_state* State)
{
}
GSMetaTag(panel_render);
GSMetaTag(panel_type_file_view);
internal void
FileView_Render(panel* Panel, rect2 PanelBounds, render_command_buffer* RenderBuffer, app_state* State, context Context)
2020-03-08 23:15:26 +00:00
{
file_view_state* FileViewState = Panel_GetStateStruct(Panel, file_view_state);
ui_PushLayout(&State->Interface, PanelBounds, LayoutDirection_TopDown, MakeString("FileView Layout"));
2020-11-08 06:54:59 +00:00
if (ui_Button(&State->Interface, MakeString("Exit")))
2020-10-24 20:28:10 +00:00
{
FileView_Exit_(Panel, State, Context);
}
// Header
2020-11-15 07:44:06 +00:00
ui_Label(&State->Interface, FileViewState->WorkingDirectory);
2020-03-08 23:15:26 +00:00
// File Display
for (u32 i = 0; i < FileViewState->FileNames.Count; i++)
{
gs_file_info File = FileViewState->FileNames.Values[i];
u32 LastSlashIndex = FindLast(File.Path, '\\');
gs_const_string FileName = Substring(File.Path, LastSlashIndex + 1, File.Path.Length);
gs_string PathString = PushString(State->Transient, FileName.Length);
PrintF(&PathString, "%S", FileName);
if (ui_LayoutListButton(&State->Interface, PathString, i))
{
if (File.IsDirectory)
{
FileViewUpdateWorkingDirectory(File.Path, FileViewState, Context);
}
else
{
FileViewState->SelectedFile = File;
2020-10-24 20:28:10 +00:00
FileView_Exit_(Panel, State, Context);
}
}
}
2020-03-08 23:15:26 +00:00
ui_PopLayout(&State->Interface, MakeString("FileView Layout"));
2020-03-08 23:15:26 +00:00
}
#define FOLDHAUS_PANEL_FILE_VIEW_H
#endif // FOLDHAUS_PANEL_FILE_VIEW_H