Tracker: style fixes to TemplatesMenu

This commit is contained in:
John Scipione
2014-06-20 21:29:42 -04:00
parent 754d953d6d
commit a2d1822e99
4 changed files with 169 additions and 111 deletions
+139 -89
View File
@@ -39,19 +39,104 @@ All rights reserved.
#include "TaskLoop.h" #include "TaskLoop.h"
const float kTaskOverhead = 0.01f;
// this should really be specified by the task itself
const float kIdleTreshold = 0.15f;
const bigtime_t kInfinity = B_INFINITE_TIMEOUT;
static bigtime_t
ActivityLevel()
{
// stolen from roster server
bigtime_t time = 0;
system_info sinfo;
get_system_info(&sinfo);
cpu_info* cpuInfos = new cpu_info[sinfo.cpu_count];
get_cpu_info(0, sinfo.cpu_count, cpuInfos);
for (uint32 index = 0; index < sinfo.cpu_count; index++)
time += cpuInfos[index].active_time;
delete[] cpuInfos;
return time / ((bigtime_t) sinfo.cpu_count);
}
class AccumulatedOneShotDelayedTask : public OneShotDelayedTask {
// supports accumulating functors
public:
AccumulatedOneShotDelayedTask(AccumulatingFunctionObject* functor,
bigtime_t delay, bigtime_t maxAccumulatingTime = 0,
int32 maxAccumulateCount = 0)
:
OneShotDelayedTask(functor, delay),
maxAccumulateCount(maxAccumulateCount),
accumulateCount(1),
maxAccumulatingTime(maxAccumulatingTime),
initialTime(system_time())
{
}
bool CanAccumulate(const AccumulatingFunctionObject* accumulateThis) const
{
if (maxAccumulateCount && accumulateCount > maxAccumulateCount)
// don't accumulate if too may accumulated already
return false;
if (maxAccumulatingTime && system_time() > initialTime
+ maxAccumulatingTime) {
// don't accumulate if too late past initial task
return false;
}
return static_cast<AccumulatingFunctionObject*>(fFunctor)->
CanAccumulate(accumulateThis);
}
virtual void Accumulate(AccumulatingFunctionObject* accumulateThis,
bigtime_t delay)
{
fRunAfter = system_time() + delay;
// reset fRunAfter
accumulateCount++;
static_cast<AccumulatingFunctionObject*>(fFunctor)->
Accumulate(accumulateThis);
}
private:
int32 maxAccumulateCount;
int32 accumulateCount;
bigtime_t maxAccumulatingTime;
bigtime_t initialTime;
};
// #pragma mark - DelayedTask
DelayedTask::DelayedTask(bigtime_t delay) DelayedTask::DelayedTask(bigtime_t delay)
: fRunAfter(system_time() + delay) :
fRunAfter(system_time() + delay)
{ {
} }
DelayedTask::~DelayedTask() DelayedTask::~DelayedTask()
{ {
} }
// #pragma mark - OneShotDelayedTask
OneShotDelayedTask::OneShotDelayedTask(FunctionObject* functor, OneShotDelayedTask::OneShotDelayedTask(FunctionObject* functor,
bigtime_t delay) bigtime_t delay)
: DelayedTask(delay), :
fFunctor(functor) DelayedTask(delay),
fFunctor(functor)
{ {
} }
@@ -73,12 +158,16 @@ OneShotDelayedTask::RunIfNeeded(bigtime_t currentTime)
} }
// #pragma mark - PeriodicDelayedTask
PeriodicDelayedTask::PeriodicDelayedTask( PeriodicDelayedTask::PeriodicDelayedTask(
FunctionObjectWithResult<bool>* functor, bigtime_t initialDelay, FunctionObjectWithResult<bool>* functor, bigtime_t initialDelay,
bigtime_t period) bigtime_t period)
: DelayedTask(initialDelay), :
fPeriod(period), DelayedTask(initialDelay),
fFunctor(functor) fPeriod(period),
fFunctor(functor)
{ {
} }
@@ -104,8 +193,9 @@ PeriodicDelayedTask::RunIfNeeded(bigtime_t currentTime)
PeriodicDelayedTaskWithTimeout::PeriodicDelayedTaskWithTimeout( PeriodicDelayedTaskWithTimeout::PeriodicDelayedTaskWithTimeout(
FunctionObjectWithResult<bool>* functor, bigtime_t initialDelay, FunctionObjectWithResult<bool>* functor, bigtime_t initialDelay,
bigtime_t period, bigtime_t timeout) bigtime_t period, bigtime_t timeout)
: PeriodicDelayedTask(functor, initialDelay, period), :
fTimeoutAfter(system_time() + timeout) PeriodicDelayedTask(functor, initialDelay, period),
fTimeoutAfter(system_time() + timeout)
{ {
} }
@@ -128,13 +218,17 @@ PeriodicDelayedTaskWithTimeout::RunIfNeeded(bigtime_t currentTime)
RunWhenIdleTask::RunWhenIdleTask(FunctionObjectWithResult<bool>* functor, RunWhenIdleTask::RunWhenIdleTask(FunctionObjectWithResult<bool>* functor,
bigtime_t initialDelay, bigtime_t idleFor, bigtime_t heartBeat) bigtime_t initialDelay, bigtime_t idleFor, bigtime_t heartBeat)
: PeriodicDelayedTask(functor, initialDelay, heartBeat), :
fIdleFor(idleFor), PeriodicDelayedTask(functor, initialDelay, heartBeat),
fState(kInitialDelay) fIdleFor(idleFor),
fState(kInitialDelay)
{ {
} }
// #pragma mark - RunWhenIdleTask
RunWhenIdleTask::~RunWhenIdleTask() RunWhenIdleTask::~RunWhenIdleTask()
{ {
} }
@@ -147,8 +241,8 @@ RunWhenIdleTask::RunIfNeeded(bigtime_t currentTime)
return false; return false;
fRunAfter = currentTime + fPeriod; fRunAfter = currentTime + fPeriod;
// PRINT(("runWhenIdle: runAfter %Ld, current time %Ld, period %Ld\n", // PRINT(("runWhenIdle: runAfter %Ld, current time %Ld, period %Ld\n",
// fRunAfter, currentTime, fPeriod)); // fRunAfter, currentTime, fPeriod));
if (fState == kInitialDelay) { if (fState == kInitialDelay) {
// PRINT(("run when idle task - past intial delay\n")); // PRINT(("run when idle task - past intial delay\n"));
@@ -161,29 +255,11 @@ RunWhenIdleTask::RunIfNeeded(bigtime_t currentTime)
(*fFunctor)(); (*fFunctor)();
return fFunctor->Result(); return fFunctor->Result();
} }
return false; return false;
} }
static bigtime_t
ActivityLevel()
{
// stolen from roster server
bigtime_t time = 0;
system_info sinfo;
get_system_info(&sinfo);
cpu_info* cpuInfos = new cpu_info[sinfo.cpu_count];
get_cpu_info(0, sinfo.cpu_count, cpuInfos);
for (uint32 index = 0; index < sinfo.cpu_count; index++)
time += cpuInfos[index].active_time;
delete[] cpuInfos;
return time / ((bigtime_t) sinfo.cpu_count);
}
void void
RunWhenIdleTask::ResetIdleTimer(bigtime_t currentTime) RunWhenIdleTask::ResetIdleTimer(bigtime_t currentTime)
{ {
@@ -193,9 +269,6 @@ RunWhenIdleTask::ResetIdleTimer(bigtime_t currentTime)
fState = kInitialIdleWait; fState = kInitialIdleWait;
} }
const float kTaskOverhead = 0.01f;
// this should really be specified by the task itself
const float kIdleTreshold = 0.15f;
bool bool
RunWhenIdleTask::IsIdle(bigtime_t currentTime, float taskOverhead) RunWhenIdleTask::IsIdle(bigtime_t currentTime, float taskOverhead)
@@ -246,9 +319,13 @@ RunWhenIdleTask::StillIdle(bigtime_t currentTime)
} }
// #pragma mark - TaskLoop
TaskLoop::TaskLoop(bigtime_t heartBeat) TaskLoop::TaskLoop(bigtime_t heartBeat)
: fTaskList(10, true), :
fHeartBeat(heartBeat) fTaskList(10, true),
fHeartBeat(heartBeat)
{ {
} }
@@ -297,51 +374,8 @@ TaskLoop::RunWhenIdle(FunctionObjectWithResult<bool>* functor,
} }
class AccumulatedOneShotDelayedTask : public OneShotDelayedTask { // #pragma mark - TaskLoop
// supports accumulating functors
public:
AccumulatedOneShotDelayedTask(AccumulatingFunctionObject* functor,
bigtime_t delay, bigtime_t maxAccumulatingTime = 0,
int32 maxAccumulateCount = 0)
: OneShotDelayedTask(functor, delay),
maxAccumulateCount(maxAccumulateCount),
accumulateCount(1),
maxAccumulatingTime(maxAccumulatingTime),
initialTime(system_time())
{}
bool CanAccumulate(const AccumulatingFunctionObject* accumulateThis) const
{
if (maxAccumulateCount && accumulateCount > maxAccumulateCount)
// don't accumulate if too may accumulated already
return false;
if (maxAccumulatingTime && system_time() > initialTime
+ maxAccumulatingTime) {
// don't accumulate if too late past initial task
return false;
}
return static_cast<AccumulatingFunctionObject*>(fFunctor)->
CanAccumulate(accumulateThis);
}
virtual void Accumulate(AccumulatingFunctionObject* accumulateThis,
bigtime_t delay)
{
fRunAfter = system_time() + delay;
// reset fRunAfter
accumulateCount++;
static_cast<AccumulatingFunctionObject*>(fFunctor)->
Accumulate(accumulateThis);
}
private:
int32 maxAccumulateCount;
int32 accumulateCount;
bigtime_t maxAccumulatingTime;
bigtime_t initialTime;
};
void void
TaskLoop::AccumulatedRunLater(AccumulatingFunctionObject* functor, TaskLoop::AccumulatedRunLater(AccumulatingFunctionObject* functor,
@@ -391,7 +425,6 @@ TaskLoop::Pulse()
return count == 0 && !KeepPulsingWhenEmpty(); return count == 0 && !KeepPulsingWhenEmpty();
} }
const bigtime_t kInfinity = B_INFINITE_TIMEOUT;
bigtime_t bigtime_t
TaskLoop::LatestRunTime() const TaskLoop::LatestRunTime() const
@@ -448,11 +481,15 @@ TaskLoop::AddTask(DelayedTask* task)
} }
// #pragma mark - StandAloneTaskLoop
StandAloneTaskLoop::StandAloneTaskLoop(bool keepThread, bigtime_t heartBeat) StandAloneTaskLoop::StandAloneTaskLoop(bool keepThread, bigtime_t heartBeat)
: TaskLoop(heartBeat), :
fNeedToQuit(false), TaskLoop(heartBeat),
fScanThread(-1), fNeedToQuit(false),
fKeepThread(keepThread) fScanThread(-1),
fKeepThread(keepThread)
{ {
} }
@@ -486,6 +523,7 @@ StandAloneTaskLoop::~StandAloneTaskLoop()
} }
} }
void void
StandAloneTaskLoop::StartPulsingIfNeeded() StandAloneTaskLoop::StartPulsingIfNeeded()
{ {
@@ -498,12 +536,14 @@ StandAloneTaskLoop::StartPulsingIfNeeded()
} }
} }
bool bool
StandAloneTaskLoop::KeepPulsingWhenEmpty() const StandAloneTaskLoop::KeepPulsingWhenEmpty() const
{ {
return fKeepThread; return fKeepThread;
} }
status_t status_t
StandAloneTaskLoop::RunBinder(void* castToThis) StandAloneTaskLoop::RunBinder(void* castToThis)
{ {
@@ -512,6 +552,7 @@ StandAloneTaskLoop::RunBinder(void* castToThis)
return B_OK; return B_OK;
} }
void void
StandAloneTaskLoop::Run() StandAloneTaskLoop::Run()
{ {
@@ -550,6 +591,7 @@ StandAloneTaskLoop::Run()
} }
} }
void void
StandAloneTaskLoop::AddTask(DelayedTask* delayedTask) StandAloneTaskLoop::AddTask(DelayedTask* delayedTask)
{ {
@@ -567,10 +609,15 @@ StandAloneTaskLoop::AddTask(DelayedTask* delayedTask)
} }
} }
// #pragma mark - PiggybackTaskLoop
PiggybackTaskLoop::PiggybackTaskLoop(bigtime_t heartBeat) PiggybackTaskLoop::PiggybackTaskLoop(bigtime_t heartBeat)
: TaskLoop(heartBeat), :
fNextHeartBeatTime(0), TaskLoop(heartBeat),
fPulseMe(false) fNextHeartBeatTime(0),
fPulseMe(false)
{ {
} }
@@ -579,6 +626,7 @@ PiggybackTaskLoop::~PiggybackTaskLoop()
{ {
} }
void void
PiggybackTaskLoop::PulseMe() PiggybackTaskLoop::PulseMe()
{ {
@@ -594,12 +642,14 @@ PiggybackTaskLoop::PulseMe()
} }
} }
bool bool
PiggybackTaskLoop::KeepPulsingWhenEmpty() const PiggybackTaskLoop::KeepPulsingWhenEmpty() const
{ {
return false; return false;
} }
void void
PiggybackTaskLoop::StartPulsingIfNeeded() PiggybackTaskLoop::StartPulsingIfNeeded()
{ {
+5 -4
View File
@@ -31,8 +31,8 @@ of Be Incorporated in the United States and other countries. Other brand product
names are registered trademarks or trademarks of their respective holders. names are registered trademarks or trademarks of their respective holders.
All rights reserved. All rights reserved.
*/ */
#ifndef __TASK_LOOP__ #ifndef _TASK_LOOP_H
#define __TASK_LOOP__ #define _TASK_LOOP_H
// Delayed Tasks, Periodic Delayed Tasks, Periodic Delayed Tasks with timeout, // Delayed Tasks, Periodic Delayed Tasks, Periodic Delayed Tasks with timeout,
@@ -40,9 +40,9 @@ All rights reserved.
#include <Locker.h> #include <Locker.h>
#include <ObjectList.h>
#include "FunctionObject.h" #include "FunctionObject.h"
#include "ObjectList.h"
namespace BPrivate { namespace BPrivate {
@@ -259,4 +259,5 @@ DelayedTask::RunAfterTime() const
using namespace BPrivate; using namespace BPrivate;
#endif // __TASK_LOOP__
#endif // _TASK_LOOP_H
+16 -10
View File
@@ -64,10 +64,15 @@ const char* kTemplatesDirectory = "Tracker/Tracker New Templates";
} // namespace BPrivate } // namespace BPrivate
// #pragma mark - TemplatesMenu
TemplatesMenu::TemplatesMenu(const BMessenger &target, const char* label) TemplatesMenu::TemplatesMenu(const BMessenger &target, const char* label)
: BMenu(label), :
fTarget(target), BMenu(label),
fOpenItem(NULL) fTarget(target),
fOpenItem(NULL)
{ {
} }
@@ -118,19 +123,19 @@ TemplatesMenu::UpdateMenuState()
bool bool
TemplatesMenu::BuildMenu(bool addItems) TemplatesMenu::BuildMenu(bool addItems)
{ {
// Clear everything... // clear everything...
fOpenItem = NULL; fOpenItem = NULL;
int32 count = CountItems(); int32 count = CountItems();
while (count--) while (count--)
delete RemoveItem((int32)0); delete RemoveItem((int32)0);
// Add the Folder // add the folder
IconMenuItem* menuItem = new IconMenuItem(B_TRANSLATE("New folder"), IconMenuItem* menuItem = new IconMenuItem(B_TRANSLATE("New folder"),
new BMessage(kNewFolder), B_DIR_MIMETYPE, B_MINI_ICON); new BMessage(kNewFolder), B_DIR_MIMETYPE, B_MINI_ICON);
AddItem(menuItem); AddItem(menuItem);
menuItem->SetShortcut('N', 0); menuItem->SetShortcut('N', 0);
// The Templates folder // the templates folder
BPath path; BPath path;
find_directory (B_USER_SETTINGS_DIRECTORY, &path, true); find_directory (B_USER_SETTINGS_DIRECTORY, &path, true);
path.Append(kTemplatesDirectory); path.Append(kTemplatesDirectory);
@@ -176,16 +181,17 @@ TemplatesMenu::BuildMenu(bool addItems)
AddSeparatorItem(); AddSeparatorItem();
// This is the message sent to open the templates folder. // this is the message sent to open the templates folder
BMessage* message = new BMessage(B_REFS_RECEIVED); BMessage* message = new BMessage(B_REFS_RECEIVED);
entry_ref dirRef; entry_ref dirRef;
if (templatesDir.GetEntry(&entry) == B_OK) if (templatesDir.GetEntry(&entry) == B_OK)
entry.GetRef(&dirRef); entry.GetRef(&dirRef);
message->AddRef("refs", &dirRef); message->AddRef("refs", &dirRef);
// Add item to show templates folder. // add item to show templates folder
fOpenItem = new BMenuItem(B_TRANSLATE("Edit templates" B_UTF8_ELLIPSIS), fOpenItem = new BMenuItem(B_TRANSLATE("Edit templates" B_UTF8_ELLIPSIS),
message); message);
AddItem(fOpenItem); AddItem(fOpenItem);
if (dirRef == entry_ref()) if (dirRef == entry_ref())
fOpenItem->SetEnabled(false); fOpenItem->SetEnabled(false);
+9 -8
View File
@@ -31,8 +31,8 @@ of Be Incorporated in the United States and other countries. Other brand product
names are registered trademarks or trademarks of their respective holders. names are registered trademarks or trademarks of their respective holders.
All rights reserved. All rights reserved.
*/ */
#ifndef __TEMPLATES_MENU__ #ifndef _TEMPLATES_MENU_H
#define __TEMPLATES_MENU__ #define _TEMPLATES_MENU_H
#include <Menu.h> #include <Menu.h>
@@ -45,13 +45,13 @@ extern const char* kTemplatesMenuName;
class TemplatesMenu : public BMenu { class TemplatesMenu : public BMenu {
public: public:
TemplatesMenu(const BMessenger& target, const char* label); TemplatesMenu(const BMessenger& target, const char* label);
virtual ~TemplatesMenu(); virtual ~TemplatesMenu();
virtual void AttachedToWindow(); virtual void AttachedToWindow();
virtual status_t SetTargetForItems(BHandler*); virtual status_t SetTargetForItems(BHandler*);
virtual status_t SetTargetForItems(BMessenger); virtual status_t SetTargetForItems(BMessenger);
void UpdateMenuState(); void UpdateMenuState();
@@ -66,4 +66,5 @@ private:
using namespace BPrivate; using namespace BPrivate;
#endif // __TEMPLATES_MENU__
#endif // _TEMPLATES_MENU_H