timetracking: add timer warning colors

Lets the user choose a time for showing when the timer should turn orange.

Co-authored-by: jakewilson <jakewilsonfl@gmail.com>
This commit is contained in:
melkypie
2020-03-18 13:56:35 +02:00
committed by Adam
parent 59361c0d95
commit df18bdb542
6 changed files with 70 additions and 2 deletions

View File

@@ -94,6 +94,18 @@ public interface TimeTrackingConfig extends Config
return SortOrder.NONE;
}
@ConfigItem(
keyName = "timerWarningThreshold",
name = "Timer Warning Threshold",
description = "The time at which to change the timer color to the warning color",
position = 6
)
@Units(Units.SECONDS)
default int timerWarningThreshold()
{
return 10;
}
@ConfigItem(
keyName = "activeTab",
name = "Active Tab",

View File

@@ -230,6 +230,7 @@ public class TimeTrackingPlugin extends Plugin
{
clockDataChanged = clockManager.checkCompletion();
timerOrderChanged = clockManager.checkTimerOrder();
clockManager.checkForWarnings();
}
if (unitTime % panel.getUpdateInterval() == 0 || clockDataChanged || timerOrderChanged)

View File

@@ -161,6 +161,17 @@ public class ClockManager
return false;
}
/**
* Sets the warning flag on each timer that should be in the warning state
*/
public void checkForWarnings()
{
for (Timer timer : timers)
{
timer.setWarning(timer.getDisplayTime() <= config.timerWarningThreshold());
}
}
public void loadTimers()
{
final String timersJson = configManager.getConfiguration(TimeTrackingConfig.CONFIG_GROUP, TimeTrackingConfig.TIMERS);

View File

@@ -64,7 +64,7 @@ abstract class ClockPanel extends JPanel
private final FlatTextField nameInput;
private final JToggleButton startPauseButton;
private final FlatTextField displayInput;
protected final FlatTextField displayInput;
@Getter
private final Clock clock;
@@ -150,6 +150,7 @@ abstract class ClockPanel extends JPanel
clock.setDuration(Math.max(0, duration));
clock.reset();
clockManager.checkForWarnings();
updateDisplayInput();
updateActivityStatus();
clockManager.saveTimers();
@@ -199,6 +200,7 @@ abstract class ClockPanel extends JPanel
resetButton.addActionListener(e ->
{
clock.reset();
clockManager.checkForWarnings();
reset();
clockManager.saveToConfig();
});
@@ -238,7 +240,7 @@ abstract class ClockPanel extends JPanel
boolean isActive = clock.isActive();
displayInput.setEditable(editable && !isActive);
displayInput.getTextField().setForeground(isActive ? ACTIVE_CLOCK_COLOR : INACTIVE_CLOCK_COLOR);
displayInput.getTextField().setForeground(getColor());
startPauseButton.setToolTipText(isActive ? "Pause " + clockType : "Start " + clockType);
startPauseButton.setSelected(isActive);
@@ -248,6 +250,11 @@ abstract class ClockPanel extends JPanel
}
}
protected Color getColor()
{
return clock.isActive() ? ACTIVE_CLOCK_COLOR : INACTIVE_CLOCK_COLOR;
}
static String getFormattedDuration(long duration)
{
long hours = duration / (60 * 60);

View File

@@ -25,6 +25,7 @@
package net.runelite.client.plugins.timetracking.clocks;
import java.time.Instant;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
@@ -40,11 +41,16 @@ class Timer extends Clock
// the number of seconds remaining on the timer, as of last updated time
private long remaining;
// whether this timer is in the 'warning' state or not
@Getter(AccessLevel.NONE)
private transient boolean warning;
Timer(String name, long duration)
{
super(name);
this.duration = duration;
this.remaining = duration;
this.warning = false;
}
@Override
@@ -66,6 +72,7 @@ class Timer extends Clock
if (remaining <= 0)
{
remaining = duration;
warning = false;
}
lastUpdate = Instant.now().getEpochSecond();
active = true;
@@ -96,4 +103,9 @@ class Timer extends Clock
remaining = duration;
lastUpdate = Instant.now().getEpochSecond();
}
boolean isWarning()
{
return warning && (remaining > 0);
}
}

View File

@@ -24,12 +24,16 @@
*/
package net.runelite.client.plugins.timetracking.clocks;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JButton;
import net.runelite.client.ui.ColorScheme;
import net.runelite.client.util.SwingUtil;
class TimerPanel extends ClockPanel
{
private static final Color WARNING_COLOR = ColorScheme.BRAND_ORANGE;
TimerPanel(ClockManager clockManager, Timer timer)
{
super(clockManager, timer, "timer", true);
@@ -42,4 +46,25 @@ class TimerPanel extends ClockPanel
deleteButton.addActionListener(e -> clockManager.removeTimer(timer));
rightActions.add(deleteButton);
}
@Override
void updateDisplayInput()
{
super.updateDisplayInput();
Timer timer = (Timer) getClock();
if (timer.isWarning())
{
displayInput.getTextField().setForeground(getColor());
}
}
@Override
protected Color getColor()
{
Timer timer = (Timer) getClock();
Color warningColor = timer.isActive() ? WARNING_COLOR : WARNING_COLOR.darker();
return timer.isWarning() ? warningColor : super.getColor();
}
}