DeskBar: refactor expander code.

With the Tracker thread code in shared now it is possible to use the same
trick for the expanders in Deskbar to open and close in a separate thread
that we use in Tracker. See Find Panel and Info window for examples
of the code being used in Tracker.

Also eliminates the fClickedExpander variable and allow you to expand with any
mouse button.

Hopefully fixes #9676
This commit is contained in:
John Scipione 2013-10-08 20:23:59 -04:00
parent 285b7163ad
commit 4875d5a091
2 changed files with 42 additions and 118 deletions

View File

@ -45,6 +45,7 @@ All rights reserved.
#include <NodeInfo.h>
#include <Roster.h>
#include <Screen.h>
#include <Thread.h>
#include <Window.h>
#include "icons.h"
@ -91,8 +92,7 @@ TExpandoMenuBar::TExpandoMenuBar(BRect frame, const char* name,
fDeskbarMenuWidth(kMinMenuItemWidth),
fPreviousDragTargetItem(NULL),
fLastMousedOverItem(NULL),
fLastClickedItem(NULL),
fClickedExpander(false)
fLastClickedItem(NULL)
{
SetItemMargins(0.0f, 0.0f, 0.0f, 0.0f);
SetFont(be_plain_font);
@ -267,9 +267,6 @@ TExpandoMenuBar::MessageReceived(BMessage* message)
void
TExpandoMenuBar::MouseDown(BPoint where)
{
fClickedExpander = false;
// in case MouseUp() wasn't called
BMessage* message = Window()->CurrentMessage();
BMenuItem* menuItem;
TTeamMenuItem* item = TeamItemAtPoint(where, &menuItem);
@ -310,16 +307,13 @@ TExpandoMenuBar::MouseDown(BPoint where)
// absorb the message
}
int32 buttons = 0;
// check if within expander bounds to expand window items
if (fVertical && fShowTeamExpander
&& item->ExpanderBounds().Contains(where)
&& message->FindInt32("buttons", &buttons) == B_OK
&& buttons == B_PRIMARY_MOUSE_BUTTON) {
&& item->ExpanderBounds().Contains(where)) {
// start the animation here, finish on mouse up
fLastClickedItem = item;
fClickedExpander = true;
item->SetArrowDirection(BControlLook::B_RIGHT_DOWN_ARROW);
MouseDownThread<TExpandoMenuBar>::TrackMouse(this,
&TExpandoMenuBar::_DoneTracking, &TExpandoMenuBar::_Track);
Invalidate(item->ExpanderBounds());
return;
// absorb the message
@ -354,89 +348,6 @@ TExpandoMenuBar::MouseMoved(BPoint where, uint32 code, const BMessage* message)
// force a cleanup
_FinishedDrag();
switch (code) {
case B_ENTERED_VIEW:
{
TTeamMenuItem* lastItem
= dynamic_cast<TTeamMenuItem*>(fLastClickedItem);
if (fVertical && fShowTeamExpander && fClickedExpander
&& lastItem != NULL && buttons == B_PRIMARY_MOUSE_BUTTON) {
// Started expander animation, exited view then entered
// again, redraw the expanded arrow
lastItem->SetArrowDirection(BControlLook::B_RIGHT_DOWN_ARROW);
Invalidate(lastItem->ExpanderBounds());
}
break;
}
case B_INSIDE_VIEW:
{
BMenuItem* menuItem;
TTeamMenuItem* item = TeamItemAtPoint(where, &menuItem);
TWindowMenuItem* windowMenuItem
= dynamic_cast<TWindowMenuItem*>(menuItem);
if (item == NULL || menuItem == NULL) {
// item is NULL, remove the tooltip and break out
fLastMousedOverItem = NULL;
SetToolTip((const char*)NULL);
break;
}
if (menuItem == fLastMousedOverItem) {
// already set the tooltip for this item, break out
break;
}
if (windowMenuItem != NULL && fBarView->Vertical()
&& fBarView->ExpandoState() && item->IsExpanded()) {
// expando mode window menu item
fLastMousedOverItem = menuItem;
if (strcmp(windowMenuItem->Label(),
windowMenuItem->FullTitle()) != 0) {
// label is truncated, set tooltip
SetToolTip(windowMenuItem->FullTitle());
} else
SetToolTip((const char*)NULL);
break;
}
if (item->HasLabel()) {
// item has a visible label, remove the tooltip and break out
fLastMousedOverItem = menuItem;
SetToolTip((const char*)NULL);
break;
}
SetToolTip(item->Name());
// new item, set the tooltip to the item name
fLastMousedOverItem = menuItem;
// save the current menuitem for the next MouseMoved() call
break;
}
case B_OUTSIDE_VIEW:
// NOTE: Should not be here, but for the sake of defensive
// programming... fall-through
case B_EXITED_VIEW:
{
TTeamMenuItem* lastItem
= dynamic_cast<TTeamMenuItem*>(fLastClickedItem);
if (lastItem != NULL && fVertical && fShowTeamExpander
&& fClickedExpander) {
// Started expander animation, then exited view,
// since we can't track outside mouse movements
// redraw the original expander arrow
lastItem->SetArrowDirection(lastItem->IsExpanded()
? BControlLook::B_DOWN_ARROW
: BControlLook::B_RIGHT_ARROW);
Invalidate(lastItem->ExpanderBounds());
}
break;
}
}
BMenuBar::MouseMoved(where, code, message);
return;
}
@ -488,35 +399,12 @@ TExpandoMenuBar::MouseMoved(BPoint where, uint32 code, const BMessage* message)
void
TExpandoMenuBar::MouseUp(BPoint where)
{
bool clickedExpander = fClickedExpander;
fClickedExpander = false;
if (fBarView->Dragging()) {
_FinishedDrag(true);
return;
// absorb the message
}
TTeamMenuItem* item = TeamItemAtPoint(where, NULL);
TTeamMenuItem* lastItem = dynamic_cast<TTeamMenuItem*>(fLastClickedItem);
if (fVertical && fShowTeamExpander && clickedExpander) {
if (item != NULL && lastItem != NULL && item == lastItem
&& item->ExpanderBounds().Contains(where)) {
// Toggle the expanded state
BAutolock locker(sMonLocker);
// let the update thread wait...
item->ToggleExpandState(true);
item->Draw();
return;
// absorb the message
} else if (lastItem != NULL) {
// User changed their mind, redraw the original expander arrow
lastItem->SetArrowDirection(lastItem->IsExpanded()
? BControlLook::B_DOWN_ARROW : BControlLook::B_RIGHT_ARROW);
Invalidate(lastItem->ExpanderBounds());
}
}
BMenuBar::MouseUp(where);
}
@ -1051,3 +939,37 @@ TExpandoMenuBar::_FinishedDrag(bool invoke)
if (!invoke && fBarView->Dragging())
fBarView->DragStop(true);
}
void
TExpandoMenuBar::_DoneTracking(BPoint point)
{
TTeamMenuItem* lastItem = dynamic_cast<TTeamMenuItem*>(fLastClickedItem);
if (!lastItem->ExpanderBounds().Contains(point))
return;
lastItem->ToggleExpandState(true);
lastItem->SetArrowDirection(lastItem->IsExpanded()
? BControlLook::B_DOWN_ARROW
: BControlLook::B_RIGHT_ARROW);
Invalidate(lastItem->ExpanderBounds());
}
void
TExpandoMenuBar::_Track(BPoint point, uint32)
{
TTeamMenuItem* lastItem = dynamic_cast<TTeamMenuItem*>(fLastClickedItem);
if (lastItem->ExpanderBounds().Contains(point))
lastItem->SetArrowDirection(BControlLook::B_RIGHT_DOWN_ARROW);
else {
lastItem->SetArrowDirection(lastItem->IsExpanded()
? BControlLook::B_DOWN_ARROW
: BControlLook::B_RIGHT_ARROW);
}
Invalidate(lastItem->ExpanderBounds());
}

View File

@ -104,6 +104,9 @@ private:
void _FinishedDrag(bool invoke = false);
void _DoneTracking(BPoint where);
void _Track(BPoint where, uint32);
private:
TBarView* fBarView;
bool fVertical : 1;
@ -116,7 +119,6 @@ private:
TTeamMenuItem* fPreviousDragTargetItem;
BMenuItem* fLastMousedOverItem;
BMenuItem* fLastClickedItem;
bool fClickedExpander;
BList fTeamList;
static bool sDoMonitor;