Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 94 additions & 14 deletions source/interface_derived.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ const int TIMER_LENGTH = 5 * 1000 * 60;
constexpr std::string_view config_name = "com.ravbug.unityhubnative.config";
constexpr std::string_view config_sortCol = "sortColIndex";
constexpr std::string_view config_sortAscending = "sortAscending";
constexpr std::string_view config_windowWidth = "windowWidth";
constexpr std::string_view config_windowHeight = "windowHeight";
constexpr std::string_view config_windowX = "windowX";
constexpr std::string_view config_windowY = "windowY";
constexpr std::string_view config_columnWidth = "projectColumnWidth";

//Declare events here
wxBEGIN_EVENT_TABLE(MainFrameDerived, wxFrame)
Expand All @@ -43,6 +48,9 @@ EVT_MENU(wxID_REFRESH, MainFrameDerived::OnReloadData)
EVT_MENU(wxID_UNINDENT, MainFrameDerived::OnSource)
EVT_MENU(wxID_TOP, MainFrameDerived::OnUpdate)
EVT_MENU(wxID_EXIT, MainFrameDerived::OnQuit)
EVT_CLOSE(MainFrameDerived::OnClose)
EVT_SIZE(MainFrameDerived::OnSize)
EVT_MOVE(MainFrameDerived::OnMove)
EVT_BUTTON(wxID_ADD,MainFrameDerived::OnAddProject)
EVT_BUTTON(wxID_NEW,MainFrameDerived::OnCreateProject)
EVT_BUTTON(wxID_FIND,MainFrameDerived::OnLocateInstall)
Expand All @@ -61,6 +69,7 @@ EVT_LIST_ITEM_SELECTED(wxID_HARDDISK, MainFrameDerived::OnSelectProject)
EVT_LIST_ITEM_DESELECTED(wxID_HARDDISK, MainFrameDerived::OnDeselectProject)
EVT_LIST_DELETE_ITEM(wxID_HARDDISK, MainFrameDerived::OnDeselectProject)
EVT_LIST_COL_CLICK(wxID_HARDDISK, MainFrameDerived::OnColumnClick)
EVT_LIST_COL_END_DRAG(wxID_HARDDISK, MainFrameDerived::OnColumnResized)

EVT_LISTBOX(wxID_FLOPPY, MainFrameDerived::OnSelectEditor)
EVT_LISTBOX(wxID_HOME, MainFrameDerived::OnSelectEditorPath)
Expand All @@ -74,11 +83,19 @@ wxEND_EVENT_TABLE()

//call superclass constructor
MainFrameDerived::MainFrameDerived() : MainFrame(NULL){

// load prefs
wxConfig config(config_name.data());

// set saved position and size
int width, height, x, y;
if (config.Read(config_windowWidth.data(), &width) && config.Read(config_windowHeight.data(), &height) &&
config.Read(config_windowX.data(), &x) && config.Read(config_windowY.data(), &y)) {
this->SetSize(x, y, width, height);
}
windowRect = this->GetRect();

sortColumn = std::clamp<long>(config.ReadLong(config_sortCol.data(), 0), 0, 3);
sortAscending = config.ReadBool(config_sortAscending.data(), false);
sortAscending = config.ReadBool(config_sortAscending.data(), true);

//set up project list columns
{
Expand All @@ -87,6 +104,22 @@ MainFrameDerived::MainFrameDerived() : MainFrame(NULL){
projectsList->AppendColumn(str,wxLIST_FORMAT_LEFT);
}
}
//restore column widths if the user resized them in a previous session
{
const int cols = projectsList->GetColumnCount();
std::vector<int> widths(cols);
bool allRead = true;
for (int i = 0; i < cols && allRead; i++){
allRead = config.Read(std::format("{}{}", config_columnWidth, i), &widths[i]);
}
if (allRead){
for (int i = 0; i < cols; i++){
projectsList->SetColumnWidth(i, widths[i]);
}
customColumnWidths = true;
}
}
projectsList->ShowSortIndicator(sortColumn, sortAscending);
//make the data folder if it does not already exist (with readwrite for all groups)
#if defined __APPLE__ || defined __linux__
int status = mkdir(datapath.c_str(),S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
Expand Down Expand Up @@ -114,6 +147,52 @@ MainFrameDerived::MainFrameDerived() : MainFrame(NULL){
projSearchCtrl->SetFocus();
}

void MainFrameDerived::OnQuit(wxCommandEvent&){
Close();
}

void MainFrameDerived::OnClose(wxCloseEvent& event){
// save the last window size and position from before it was minimized (if it is)
wxConfig config(config_name.data());
config.Write(config_windowWidth.data(), windowRect.width);
config.Write(config_windowHeight.data(), windowRect.height);
config.Write(config_windowX.data(), windowRect.x);
config.Write(config_windowY.data(), windowRect.y);

// save column widths only if the user resized them, otherwise keep autosizing
for (int i = 0; i < projectsList->GetColumnCount(); i++){
const auto key = std::format("{}{}", config_columnWidth, i);
if (customColumnWidths){
config.Write(key, projectsList->GetColumnWidth(i));
}
else{
config.DeleteEntry(key);
}
}

// let the default handler destroy the window
event.Skip();
}

void MainFrameDerived::OnSize(wxSizeEvent& event){
RememberWindowRect();
event.Skip();
}

void MainFrameDerived::OnMove(wxMoveEvent& event){
RememberWindowRect();
event.Skip();
}

/**
Stores the current window geometry, unless the window is minimized (its geometry is not meaningful then)
*/
void MainFrameDerived::RememberWindowRect(){
if (!this->IsIconized()) {
windowRect = this->GetRect();
}
}

void MainFrameDerived::OnSelectProject(wxListEvent&){
for (auto ptr : projectActionItems){
ptr->Enable();
Expand Down Expand Up @@ -158,11 +237,6 @@ void MainFrameDerived::OnSelectEditorPath(wxCommandEvent&){
}
}

void MainFrameDerived::OnQuit(wxCommandEvent&)
{
Close();
}

/**
Loads the data in the main view. If anything is currently loaded, it will be cleared and re-loaded
*/
Expand Down Expand Up @@ -607,10 +681,12 @@ void MainFrameDerived::AddProject(const project& p, const std::string& filter, b
projectsList->SetItem(itemIndex, 2, p.modifyDateString());
projectsList->SetItem(itemIndex, 3, p.path.string());

//resize columns
int cols = projectsList->GetColumnCount();
for (int j = 0; j < cols; j++){
projectsList->SetColumnWidth(j, wxLIST_AUTOSIZE_USEHEADER);
//resize columns, unless the user has set their widths
if (!customColumnWidths){
int cols = projectsList->GetColumnCount();
for (int j = 0; j < cols; j++){
projectsList->SetColumnWidth(j, wxLIST_AUTOSIZE_USEHEADER);
}
}

// Sort the list to place the new item in the correct position
Expand Down Expand Up @@ -747,9 +823,13 @@ void MainFrameDerived::OnColumnClick(wxListEvent& event) {
}

SortProjects();
// update UI
const bool ascending = projectsList->GetUpdatedAscendingSortIndicator(col);
projectsList->ShowSortIndicator(col, ascending);
// update UI
projectsList->ShowSortIndicator(sortColumn, sortAscending);
}

void MainFrameDerived::OnColumnResized(wxListEvent& event) {
customColumnWidths = true;
event.Skip();
}

// Sort comparison function
Expand Down
10 changes: 10 additions & 0 deletions source/interface_derived.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ class MainFrameDerived : public MainFrame{
void OnSelectEditorPath(wxCommandEvent&);

void OnQuit(wxCommandEvent&);
void OnClose(wxCloseEvent&);
void OnSize(wxSizeEvent&);
void OnMove(wxMoveEvent&);
void RememberWindowRect();

wxWindow* const projectActionItems[3]{
revealProjBtn,
Expand Down Expand Up @@ -92,6 +96,11 @@ class MainFrameDerived : public MainFrame{
// Sorting state
int sortColumn = 0; // Default sort by name (column 0)
bool sortAscending = true; // Default ascending
// Set once the user resizes a column; disables autosizing and persists the widths
bool customColumnWidths = false;

// Last window geometry while not minimized, saved on close
wxRect windowRect;

//window events
void OnAbout(wxCommandEvent& event);
Expand Down Expand Up @@ -177,6 +186,7 @@ class MainFrameDerived : public MainFrame{
this->LoadEditorVersions();
}
void OnColumnClick(wxListEvent& event);
void OnColumnResized(wxListEvent& event);
static int wxCALLBACK CompareItems(wxIntPtr item1, wxIntPtr item2, wxIntPtr sortData);
void SortProjects();
wxDECLARE_EVENT_TABLE();
Expand Down
Loading