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"
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)
: fRunAfter(system_time() + delay)
:
fRunAfter(system_time() + delay)
{
}
DelayedTask::~DelayedTask()
{
}
// #pragma mark - OneShotDelayedTask
OneShotDelayedTask::OneShotDelayedTask(FunctionObject* functor,
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(
FunctionObjectWithResult<bool>* functor, bigtime_t initialDelay,
bigtime_t period)
: DelayedTask(initialDelay),
fPeriod(period),
fFunctor(functor)
:
DelayedTask(initialDelay),
fPeriod(period),
fFunctor(functor)
{
}
@@ -104,8 +193,9 @@ PeriodicDelayedTask::RunIfNeeded(bigtime_t currentTime)
PeriodicDelayedTaskWithTimeout::PeriodicDelayedTaskWithTimeout(
FunctionObjectWithResult<bool>* functor, bigtime_t initialDelay,
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,
bigtime_t initialDelay, bigtime_t idleFor, bigtime_t heartBeat)
: PeriodicDelayedTask(functor, initialDelay, heartBeat),
fIdleFor(idleFor),
fState(kInitialDelay)
:
PeriodicDelayedTask(functor, initialDelay, heartBeat),
fIdleFor(idleFor),
fState(kInitialDelay)
{
}
// #pragma mark - RunWhenIdleTask
RunWhenIdleTask::~RunWhenIdleTask()
{
}
@@ -147,8 +241,8 @@ RunWhenIdleTask::RunIfNeeded(bigtime_t currentTime)
return false;
fRunAfter = currentTime + fPeriod;
// PRINT(("runWhenIdle: runAfter %Ld, current time %Ld, period %Ld\n",
// fRunAfter, currentTime, fPeriod));
// PRINT(("runWhenIdle: runAfter %Ld, current time %Ld, period %Ld\n",
// fRunAfter, currentTime, fPeriod));
if (fState == kInitialDelay) {
// PRINT(("run when idle task - past intial delay\n"));
@@ -161,29 +255,11 @@ RunWhenIdleTask::RunIfNeeded(bigtime_t currentTime)
(*fFunctor)();
return fFunctor->Result();
}
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
RunWhenIdleTask::ResetIdleTimer(bigtime_t currentTime)
{
@@ -193,9 +269,6 @@ RunWhenIdleTask::ResetIdleTimer(bigtime_t currentTime)
fState = kInitialIdleWait;
}
const float kTaskOverhead = 0.01f;
// this should really be specified by the task itself
const float kIdleTreshold = 0.15f;
bool
RunWhenIdleTask::IsIdle(bigtime_t currentTime, float taskOverhead)
@@ -246,9 +319,13 @@ RunWhenIdleTask::StillIdle(bigtime_t currentTime)
}
// #pragma mark - TaskLoop
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 {
// 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())
{}
// #pragma mark - TaskLoop
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
TaskLoop::AccumulatedRunLater(AccumulatingFunctionObject* functor,
@@ -391,7 +425,6 @@ TaskLoop::Pulse()
return count == 0 && !KeepPulsingWhenEmpty();
}
const bigtime_t kInfinity = B_INFINITE_TIMEOUT;
bigtime_t
TaskLoop::LatestRunTime() const
@@ -448,11 +481,15 @@ TaskLoop::AddTask(DelayedTask* task)
}
// #pragma mark - StandAloneTaskLoop
StandAloneTaskLoop::StandAloneTaskLoop(bool keepThread, bigtime_t heartBeat)
: TaskLoop(heartBeat),
fNeedToQuit(false),
fScanThread(-1),
fKeepThread(keepThread)
:
TaskLoop(heartBeat),
fNeedToQuit(false),
fScanThread(-1),
fKeepThread(keepThread)
{
}
@@ -486,6 +523,7 @@ StandAloneTaskLoop::~StandAloneTaskLoop()
}
}
void
StandAloneTaskLoop::StartPulsingIfNeeded()
{
@@ -498,12 +536,14 @@ StandAloneTaskLoop::StartPulsingIfNeeded()
}
}
bool
StandAloneTaskLoop::KeepPulsingWhenEmpty() const
{
return fKeepThread;
}
status_t
StandAloneTaskLoop::RunBinder(void* castToThis)
{
@@ -512,6 +552,7 @@ StandAloneTaskLoop::RunBinder(void* castToThis)
return B_OK;
}
void
StandAloneTaskLoop::Run()
{
@@ -550,6 +591,7 @@ StandAloneTaskLoop::Run()
}
}
void
StandAloneTaskLoop::AddTask(DelayedTask* delayedTask)
{
@@ -567,10 +609,15 @@ StandAloneTaskLoop::AddTask(DelayedTask* delayedTask)
}
}
// #pragma mark - PiggybackTaskLoop
PiggybackTaskLoop::PiggybackTaskLoop(bigtime_t heartBeat)
: TaskLoop(heartBeat),
fNextHeartBeatTime(0),
fPulseMe(false)
:
TaskLoop(heartBeat),
fNextHeartBeatTime(0),
fPulseMe(false)
{
}
@@ -579,6 +626,7 @@ PiggybackTaskLoop::~PiggybackTaskLoop()
{
}
void
PiggybackTaskLoop::PulseMe()
{
@@ -594,12 +642,14 @@ PiggybackTaskLoop::PulseMe()
}
}
bool
PiggybackTaskLoop::KeepPulsingWhenEmpty() const
{
return false;
}
void
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.
All rights reserved.
*/
#ifndef __TASK_LOOP__
#define __TASK_LOOP__
#ifndef _TASK_LOOP_H
#define _TASK_LOOP_H
// Delayed Tasks, Periodic Delayed Tasks, Periodic Delayed Tasks with timeout,
@@ -40,9 +40,9 @@ All rights reserved.
#include <Locker.h>
#include <ObjectList.h>
#include "FunctionObject.h"
#include "ObjectList.h"
namespace BPrivate {
@@ -259,4 +259,5 @@ DelayedTask::RunAfterTime() const
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
// #pragma mark - TemplatesMenu
TemplatesMenu::TemplatesMenu(const BMessenger &target, const char* label)
: BMenu(label),
fTarget(target),
fOpenItem(NULL)
:
BMenu(label),
fTarget(target),
fOpenItem(NULL)
{
}
@@ -118,19 +123,19 @@ TemplatesMenu::UpdateMenuState()
bool
TemplatesMenu::BuildMenu(bool addItems)
{
// Clear everything...
// clear everything...
fOpenItem = NULL;
int32 count = CountItems();
while (count--)
delete RemoveItem((int32)0);
// Add the Folder
// add the folder
IconMenuItem* menuItem = new IconMenuItem(B_TRANSLATE("New folder"),
new BMessage(kNewFolder), B_DIR_MIMETYPE, B_MINI_ICON);
AddItem(menuItem);
menuItem->SetShortcut('N', 0);
// The Templates folder
// the templates folder
BPath path;
find_directory (B_USER_SETTINGS_DIRECTORY, &path, true);
path.Append(kTemplatesDirectory);
@@ -176,16 +181,17 @@ TemplatesMenu::BuildMenu(bool addItems)
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);
entry_ref dirRef;
if (templatesDir.GetEntry(&entry) == B_OK)
entry.GetRef(&dirRef);
message->AddRef("refs", &dirRef);
// Add item to show templates folder.
fOpenItem = new BMenuItem(B_TRANSLATE("Edit templates" B_UTF8_ELLIPSIS),
message);
// add item to show templates folder
fOpenItem = new BMenuItem(B_TRANSLATE("Edit templates" B_UTF8_ELLIPSIS),
message);
AddItem(fOpenItem);
if (dirRef == entry_ref())
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.
All rights reserved.
*/
#ifndef __TEMPLATES_MENU__
#define __TEMPLATES_MENU__
#ifndef _TEMPLATES_MENU_H
#define _TEMPLATES_MENU_H
#include <Menu.h>
@@ -45,13 +45,13 @@ extern const char* kTemplatesMenuName;
class TemplatesMenu : public BMenu {
public:
TemplatesMenu(const BMessenger& target, const char* label);
virtual ~TemplatesMenu();
TemplatesMenu(const BMessenger& target, const char* label);
virtual ~TemplatesMenu();
virtual void AttachedToWindow();
virtual void AttachedToWindow();
virtual status_t SetTargetForItems(BHandler*);
virtual status_t SetTargetForItems(BMessenger);
virtual status_t SetTargetForItems(BHandler*);
virtual status_t SetTargetForItems(BMessenger);
void UpdateMenuState();
@@ -66,4 +66,5 @@ private:
using namespace BPrivate;
#endif // __TEMPLATES_MENU__
#endif // _TEMPLATES_MENU_H