Skip to content

Commit

Permalink
Add tab created time tooltip for not-saved-yet tab
Browse files Browse the repository at this point in the history
  • Loading branch information
donho committed Sep 24, 2024
1 parent 8baa155 commit ff6215a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 40 deletions.
21 changes: 11 additions & 10 deletions PowerEditor/src/NppIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1949,7 +1949,8 @@ bool Notepad_plus::fileRename(BufferID id)
scnN.nmhdr.idFrom = (uptr_t)bufferID;

bool success = false;
bool isFileExisting = doesFileExist(buf->getFullPathName());
wstring oldFileNamePath = buf->getFullPathName();
bool isFileExisting = doesFileExist(oldFileNamePath.c_str());
if (isFileExisting)
{
CustomFileDialog fDlg(_pPublicInterface->getHSelf());
Expand Down Expand Up @@ -2023,18 +2024,18 @@ bool Notepad_plus::fileRename(BufferID id)
bool isSnapshotMode = NppParameters::getInstance().getNppGUI().isSnapshotMode();
if (isSnapshotMode)
{
std::wstring oldBackUpFile = buf->getBackupFileName();
std::wstring oldBackUpFileName = buf->getBackupFileName();
std::wstring newBackUpFileName = oldBackUpFileName;

// Change the backup file name and let MainFileManager decide the new filename
buf->setBackupFileName(L"");
size_t index = newBackUpFileName.find_last_of(oldFileNamePath) - oldFileNamePath.length() + 1;
newBackUpFileName.replace(index, oldFileNamePath.length(), tabNewNameStr);

// Create new backup
buf->setModifiedStatus(true);
bool bRes = MainFileManager.backupCurrentBuffer();
if (doesFileExist(newBackUpFileName.c_str()))
::ReplaceFile(newBackUpFileName.c_str(), oldBackUpFileName.c_str(), nullptr, REPLACEFILE_IGNORE_MERGE_ERRORS | REPLACEFILE_IGNORE_ACL_ERRORS, 0, 0);
else
::MoveFileEx(oldBackUpFileName.c_str(), newBackUpFileName.c_str(), MOVEFILE_REPLACE_EXISTING);

// Delete old backup
if (bRes)
::DeleteFile(oldBackUpFile.c_str());
buf->setBackupFileName(newBackUpFileName);
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion PowerEditor/src/NppNotification.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -995,14 +995,22 @@ BOOL Notepad_plus::notify(SCNotification *notification)
else
return FALSE;

Buffer * buf = MainFileManager.getBufferByID(idd);
Buffer* buf = MainFileManager.getBufferByID(idd);
if (buf == nullptr)
return FALSE;

tipTmp = buf->getFullPathName();

wstring tabCreatedTime = buf->tabCreatedTimeString();
if (!tabCreatedTime.empty())
{
tipTmp += L" : ";
tipTmp += tabCreatedTime;
}

if (tipTmp.length() >= tipMaxLen)
return FALSE;

wcscpy_s(docTip, tipTmp.c_str());
lpttt->lpszText = docTip;
return TRUE;
Expand Down
61 changes: 32 additions & 29 deletions PowerEditor/src/ScintillaComponent/Buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,41 +402,40 @@ int64_t Buffer::getFileLength() const
wstring Buffer::getFileTime(fileTimeType ftt) const
{
wstring result;
wstring filePath;

if (_currentStatus != DOC_UNNAMED)
WIN32_FILE_ATTRIBUTE_DATA attributes{};
if (GetFileAttributesEx(_currentStatus == DOC_UNNAMED ? _backupFileName.c_str() : _fullPathName.c_str(), GetFileExInfoStandard, &attributes) != 0)
{
WIN32_FILE_ATTRIBUTE_DATA attributes{};
if (GetFileAttributesEx(_fullPathName.c_str(), GetFileExInfoStandard, &attributes) != 0)
FILETIME rawtime;
switch (ftt)
{
FILETIME rawtime;
switch (ftt)
{
case ft_created:
rawtime = attributes.ftCreationTime;
break;
case ft_modified:
rawtime = attributes.ftLastWriteTime;
break;
default:
rawtime = attributes.ftLastAccessTime;
break;
}
case ft_created:
rawtime = attributes.ftCreationTime;
break;
case ft_modified:
rawtime = attributes.ftLastWriteTime;
break;
default:
rawtime = attributes.ftLastAccessTime;
break;
}

SYSTEMTIME utcSystemTime, localSystemTime;
FileTimeToSystemTime(&rawtime, &utcSystemTime);
SystemTimeToTzSpecificLocalTime(nullptr, &utcSystemTime, &localSystemTime);
SYSTEMTIME utcSystemTime, localSystemTime;
FileTimeToSystemTime(&rawtime, &utcSystemTime);
SystemTimeToTzSpecificLocalTime(nullptr, &utcSystemTime, &localSystemTime);

const size_t dateTimeStrLen = 256;
wchar_t bufDate[dateTimeStrLen] = {'\0'};
GetDateFormat(LOCALE_USER_DEFAULT, 0, &localSystemTime, nullptr, bufDate, dateTimeStrLen);
result += bufDate;
result += ' ';
const size_t dateTimeStrLen = 256;
wchar_t bufDate[dateTimeStrLen] = {'\0'};
GetDateFormat(LOCALE_USER_DEFAULT, 0, &localSystemTime, nullptr, bufDate, dateTimeStrLen);
result += bufDate;
result += ' ';

wchar_t bufTime[dateTimeStrLen] = {'\0'};
GetTimeFormat(LOCALE_USER_DEFAULT, 0, &localSystemTime, nullptr, bufTime, dateTimeStrLen);
result += bufTime;
}
wchar_t bufTime[dateTimeStrLen] = {'\0'};
GetTimeFormat(LOCALE_USER_DEFAULT, 0, &localSystemTime, nullptr, bufTime, dateTimeStrLen);
result += bufTime;
}

return result;
}

Expand Down Expand Up @@ -779,7 +778,10 @@ BufferID FileManager::loadFile(const wchar_t* filename, Document doc, int encodi
{
newBuf->_backupFileName = backupFileName;
if (!doesFileExist(fullpath))
{
newBuf->_currentStatus = DOC_UNNAMED;
newBuf->setTabCreatedTimeStringFromBakFile();
}
}

const FILETIME zeroTimeStamp = {};
Expand Down Expand Up @@ -1085,7 +1087,8 @@ bool FileManager::backupCurrentBuffer()
::MoveFileEx(fullpathTemp.c_str(), fullpath, MOVEFILE_REPLACE_EXISTING);
}

buffer->setModifiedStatus(false);
buffer->setTabCreatedTimeStringFromBakFile();

result = true; //all done
}
}
Expand Down
8 changes: 8 additions & 0 deletions PowerEditor/src/ScintillaComponent/Buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,12 @@ class Buffer final {
bool getNeedReload() const { return _needReloading; }
void setNeedReload(bool reload) { _needReloading = reload; }

std::wstring tabCreatedTimeString() const { return _tabCreatedTimeString; }
void setTabCreatedTimeStringFromBakFile() {
if (_currentStatus == DOC_UNNAMED)
_tabCreatedTimeString = getFileTime(Buffer::ft_created); // while DOC_UNNAMED, getFileTime will retrieve time from backup file
}

size_t docLength() const {
assert(_pManager != nullptr);
return _pManager->docLength(_id);
Expand Down Expand Up @@ -391,6 +397,8 @@ class Buffer final {
wchar_t * _fileName = nullptr; // points to filename part in _fullPathName
bool _needReloading = false; // True if Buffer needs to be reloaded on activation

std::wstring _tabCreatedTimeString;

long _recentTag = -1;
static long _recentTagCtr;

Expand Down

0 comments on commit ff6215a

Please sign in to comment.