Skip to content

Commit

Permalink
Merge pull request #1981 from alcomposer/fix-tile-time
Browse files Browse the repository at this point in the history
Correctly show time as Today or Yesterday, instead of Today being 24 …
  • Loading branch information
timothyschoen authored Nov 29, 2024
2 parents 2ca53de + 174177a commit bd03f9c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 30 deletions.
14 changes: 6 additions & 8 deletions Source/Components/DraggableNumber.h
Original file line number Diff line number Diff line change
Expand Up @@ -370,21 +370,19 @@ class DraggableNumber : public Label

// NOTE: We could simply do `String(numberText.getDoubleValue(), 0)` but using string manipulation
// bypasses any potential issues with precision
auto removeDecimalNumString = [](String& numString) {
auto removeDecimalNumString = [](String& numString) -> String {
if (numString.contains(".")) {
// Split the string into the integer and fractional parts
StringArray parts = StringArray::fromTokens(numString, ".", "");

// If the fractional part is "0" or empty, remove the decimal point
if (parts[1] == "0" || parts[1].isEmpty()) {
return parts[0]; // Just keep the integer part
} else {
return numString; // Keep the full string (with decimal point & fractional part)
// If fractional only contains zeros, only return the first part (no decimal point)
if (parts[1].removeCharacters("0").isEmpty()) {
return parts[0];
}
} else {
// If there’s no decimal point, leave the string as it is
return numString;
}
// If there’s no decimal point, leave the string as it is
return numString;
};

// Only display the decimal point if fractional exists, but make sure to show it as a user hovers over the fractional decimal places
Expand Down
45 changes: 30 additions & 15 deletions Source/Components/WelcomePanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ class WelcomePanel : public Component

String creationTimeDescription = String();
String modifiedTimeDescription = String();
String accessedTimeDescription = String();
String fileSizeDescription = String();

File patchFile;
Expand All @@ -246,24 +247,33 @@ class WelcomePanel : public Component

auto is24Hour = OSUtils::is24HourTimeFormat();

auto formatTimeDescription = [is24Hour](const Time& openTime) {
auto diff = Time::getCurrentTime() - openTime;
auto formatTimeDescription = [is24Hour](const Time& openTime, bool showDayAndDate = false) {
auto const now = Time::getCurrentTime();

String date;
auto days = diff.inDays();
if (days < 1)
date = "Today";
else if (days > 1 && days < 2)
date = "Yesterday";
else
date = openTime.toString(true, false);
// Extract just the date part (YYYY-MM-DD) for comparison
auto const openDate = openTime.toISO8601(true).substring(0, 10);
auto const currentDate = now.toISO8601(true).substring(0, 10);
auto const yesterday = (now - RelativeTime::days(1)).toISO8601(true).substring(0, 10);

String dateOrDay;
if (openDate == currentDate) {
dateOrDay = "Today";
} else if (openDate == yesterday) {
dateOrDay = "Yesterday";
}

String time = openTime.toString(false, true, false, is24Hour);

return date + ", " + time;
if (showDayAndDate)
return (dateOrDay.isNotEmpty() ? dateOrDay + ", " : "") + openTime.toString(true, false) + ", " + time;

return (dateOrDay.isNotEmpty() ? dateOrDay : openTime.toString(true, false)) + ", " + time;

};

tileSubtitle = formatTimeDescription(Time(static_cast<int64>(subTree.getProperty("Time"))));
auto const accessedInPlugdasta = Time(static_cast<int64>(subTree.getProperty("Time")));

tileSubtitle = formatTimeDescription(accessedInPlugdasta);

auto const fileSize = patchFile.getSize();

Expand All @@ -275,8 +285,13 @@ class WelcomePanel : public Component
fileSizeDescription = String(fileSize / (1024.0 * 1024.0), 2) + " MiB"; // 1 MiB or more
}

creationTimeDescription = formatTimeDescription(patchFile.getCreationTime());
modifiedTimeDescription = formatTimeDescription(patchFile.getLastModificationTime());
creationTimeDescription = formatTimeDescription(patchFile.getCreationTime(), true);
modifiedTimeDescription = formatTimeDescription(patchFile.getLastModificationTime(), true);
// Accessed time will show the last time the file was read, which is when the Home panel has been refreshed.
// We need to show the time accessed from plugdata, which is saved in the settings XML
// We want to show this again as well as in the subtile, but format it differently (with both Today/Yesterday and date)
// because the popup menu may occlude the tile + subtitle
accessedTimeDescription = formatTimeDescription(accessedInPlugdasta, true);

updateGeneratedThumbnailIfNeeded(thumbImage, svgImage, iconColour);
}
Expand Down Expand Up @@ -331,7 +346,7 @@ class WelcomePanel : public Component
patchInfoSubMenu.addSeparator();
patchInfoSubMenu.addItem(String("Created: " + creationTimeDescription), false, false, nullptr);
patchInfoSubMenu.addItem(String("Modified: " + modifiedTimeDescription), false, false, nullptr);
patchInfoSubMenu.addItem(String("Accessed: " + tileSubtitle), false, false, nullptr);
patchInfoSubMenu.addItem(String("Accessed: " + accessedTimeDescription), false, false, nullptr);
tileMenu.addSubMenu(String(tileName + ".pd file info"), patchInfoSubMenu, true);
tileMenu.addSeparator();
// TODO: we may want to be clearer about this - that it doesn't delete the file on disk
Expand Down
4 changes: 1 addition & 3 deletions Source/Statusbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1329,11 +1329,9 @@ void Statusbar::updateZoomLevel()

void Statusbar::paint(Graphics& g)
{
if(welcomePanelIsShown) return;

g.setColour(findColour(PlugDataColour::toolbarOutlineColourId));

auto start = !editor->palettes->isExpanded() ? 29.0f : 0.0f;
auto start = !editor->palettes->isExpanded() && editor->palettes->isVisible() ? 29.0f : 0.0f;
auto end = editor->sidebar->isHidden() ? 29.0f : 0.0f;
g.drawLine(start, 0.5f, static_cast<float>(getWidth()) - end, 0.5f);

Expand Down
12 changes: 8 additions & 4 deletions Source/Utility/CachedTextRender.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ class CachedTextRender {
nvgFillRect(nvg, bounds.getX(), bounds.getY(), bounds.getWidth() + 3, bounds.getHeight());
}

AttributedString getSyntaxHighlightedString(String const& text, Font const& font, Colour const& colour)
AttributedString getSyntaxHighlightedString(String const& text, Font const& font, Colour const& colour, Colour const& nameColour)
{
auto attributedText = AttributedString();
auto tokens = StringArray::fromTokens(text, true);

auto nameColour = colour.interpolatedWith(LookAndFeel::getDefaultLookAndFeel().findColour(PlugDataColour::dataColourId), 0.7f);
auto flagColour = colour.interpolatedWith(LookAndFeel::getDefaultLookAndFeel().findColour(PlugDataColour::signalColourId), 0.7f);

bool firstToken = true;
Expand Down Expand Up @@ -55,10 +54,13 @@ class CachedTextRender {
{
auto textHash = hash(text);
bool needsUpdate = lastTextHash != textHash || colour != lastColour || cachedWidth != lastWidth || highlightObjectSyntax != isSyntaxHighlighted;
if (needsUpdate) {
auto nameColour = colour.interpolatedWith(LookAndFeel::getDefaultLookAndFeel().findColour(PlugDataColour::dataColourId), 0.7f);
bool highlightTextNeedsUpdaste = highlightObjectSyntax ? lastTextHighlightedColour != nameColour : false;

if (needsUpdate || highlightTextNeedsUpdaste) {
AttributedString attributedText;
if (highlightObjectSyntax) {
attributedText = getSyntaxHighlightedString(text, font, colour);
attributedText = getSyntaxHighlightedString(text, font, colour, nameColour);
attributedText.setJustification(Justification::centredLeft);
} else {
attributedText = AttributedString(text);
Expand All @@ -75,6 +77,7 @@ class CachedTextRender {

lastTextHash = textHash;
lastColour = colour;
lastTextHighlightedColour = nameColour;
isSyntaxHighlighted = highlightObjectSyntax;
updateImage = true;
}
Expand Down Expand Up @@ -103,6 +106,7 @@ class CachedTextRender {
hash32 lastTextHash = 0;
float lastScale = 1.0f;
Colour lastColour;
Colour lastTextHighlightedColour;
int lastWidth = 0;
int idealWidth = 0, idealHeight = 0;
Rectangle<int> lastRenderBounds;
Expand Down

0 comments on commit bd03f9c

Please sign in to comment.