Remove MouseDownThread and its usages

This code comes from an old Be Newsletter and since then the API
received the addition of SetMouseEventMask. In several places the
MouseDownThread was misused: it would spawn a new thread on every mouse
click and not clear the previous one. This could for example lead to
BSpinner skipping values if you clicked it at the right speed.

There are functional changes in BSpinner, before it updated for the
first time 100ms after mouse down, and then as you moved the mouse
around the button, now it activates immediately on first click and then
every 200ms (which may be a bit short). In other places, no functional
changes intended.

Change-Id: Ie600dc68cbb87d1e237633953e5189918bf36575
Reviewed-on: https://review.haiku-os.org/c/haiku/+/2599
Reviewed-by: waddlesplash <[email protected]>
This commit is contained in:
Adrien Destugues
2020-05-07 22:08:23 +00:00
committed by waddlesplash
parent 7bf88c5132
commit 20f2ebae4b
8 changed files with 102 additions and 281 deletions
-151
View File
@@ -45,46 +45,6 @@ All rights reserved.
namespace BPrivate {
class MessengerAutoLocker {
// move this into AutoLock.h
public:
MessengerAutoLocker(BMessenger* messenger)
: fMessenger(messenger),
fHasLock(messenger->LockTarget())
{}
~MessengerAutoLocker()
{
Unlock();
}
bool operator!() const
{
return !fHasLock;
}
bool IsLocked() const
{
return fHasLock;
}
void Unlock()
{
if (fHasLock) {
BLooper* looper;
fMessenger->Target(&looper);
if (looper)
looper->Unlock();
fHasLock = false;
}
}
private:
BMessenger* fMessenger;
bool fHasLock;
};
class SimpleThread {
// this should only be used as a base class,
// subclass needs to add proper locking mechanism
@@ -313,117 +273,6 @@ LaunchInNewThread(const char* name, int32 priority,
}
template<class View>
class MouseDownThread {
public:
static void TrackMouse(View* view, void (View::*)(BPoint),
void (View::*)(BPoint, uint32) = 0,
bigtime_t pressingPeriod = 100000);
protected:
MouseDownThread(View* view, void (View::*)(BPoint),
void (View::*)(BPoint, uint32), bigtime_t pressingPeriod);
virtual ~MouseDownThread();
void Go();
virtual void Track();
static status_t TrackBinder(void*);
private:
BMessenger fOwner;
void (View::*fDonePressing)(BPoint);
void (View::*fPressing)(BPoint, uint32);
bigtime_t fPressingPeriod;
volatile thread_id fThreadID;
};
template<class View>
void
MouseDownThread<View>::TrackMouse(View* view,
void(View::*donePressing)(BPoint),
void(View::*pressing)(BPoint, uint32), bigtime_t pressingPeriod)
{
(new MouseDownThread(view, donePressing, pressing, pressingPeriod))->Go();
}
template<class View>
MouseDownThread<View>::MouseDownThread(View* view,
void (View::*donePressing)(BPoint),
void (View::*pressing)(BPoint, uint32), bigtime_t pressingPeriod)
: fOwner(view, view->Window()),
fDonePressing(donePressing),
fPressing(pressing),
fPressingPeriod(pressingPeriod)
{
}
template<class View>
MouseDownThread<View>::~MouseDownThread()
{
}
template<class View>
void
MouseDownThread<View>::Go()
{
fThreadID = spawn_thread(&MouseDownThread::TrackBinder,
"MouseTrackingThread", B_NORMAL_PRIORITY, this);
if (fThreadID <= 0 || resume_thread(fThreadID) != B_OK)
// didn't start, don't leak self
delete this;
}
template<class View>
status_t
MouseDownThread<View>::TrackBinder(void* castToThis)
{
MouseDownThread* self = static_cast<MouseDownThread*>(castToThis);
self->Track();
// dead at this point
return B_OK;
}
template<class View>
void
MouseDownThread<View>::Track()
{
for (;;) {
MessengerAutoLocker lock(&fOwner);
if (!lock)
break;
BLooper* looper;
View* view = dynamic_cast<View*>(fOwner.Target(&looper));
if (!view)
break;
uint32 buttons;
BPoint location;
view->GetMouse(&location, &buttons, false);
if (!buttons) {
(view->*fDonePressing)(location);
break;
}
if (fPressing)
(view->*fPressing)(location, buttons);
lock.Unlock();
snooze(fPressingPeriod);
}
delete this;
}
} // namespace BPrivate
using namespace BPrivate;