This source file includes following definitions.
- taskbar_has_focus_
- OnActivationChanged
- OnViewHidden
- MaybeDismissAppList
- ShouldDismissAppList
#include "chrome/browser/ui/views/app_list/win/activation_tracker_win.h"
#include "base/time/time.h"
#include "ui/app_list/views/app_list_view.h"
#include "ui/views/widget/widget.h"
namespace {
const wchar_t kJumpListClassName[] = L"DV2ControlHost";
const wchar_t kTrayClassName[] = L"Shell_TrayWnd";
const int kFocusCheckIntervalMS = 250;
}
ActivationTrackerWin::ActivationTrackerWin(
app_list::AppListView* view,
const base::Closure& on_should_dismiss)
: view_(view),
on_should_dismiss_(on_should_dismiss),
reactivate_on_next_focus_loss_(false),
taskbar_has_focus_(false) {
view_->AddObserver(this);
}
ActivationTrackerWin::~ActivationTrackerWin() {
view_->RemoveObserver(this);
timer_.Stop();
}
void ActivationTrackerWin::OnActivationChanged(views::Widget* ,
bool active) {
if (active) {
timer_.Stop();
return;
}
taskbar_has_focus_ = false;
timer_.Start(FROM_HERE,
base::TimeDelta::FromMilliseconds(kFocusCheckIntervalMS), this,
&ActivationTrackerWin::MaybeDismissAppList);
}
void ActivationTrackerWin::OnViewHidden() {
timer_.Stop();
}
void ActivationTrackerWin::MaybeDismissAppList() {
if (!ShouldDismissAppList())
return;
if (reactivate_on_next_focus_loss_) {
reactivate_on_next_focus_loss_ = false;
view_->GetWidget()->Activate();
return;
}
on_should_dismiss_.Run();
}
bool ActivationTrackerWin::ShouldDismissAppList() {
bool taskbar_had_focus = taskbar_has_focus_;
taskbar_has_focus_ = false;
HWND jump_list_hwnd = FindWindow(kJumpListClassName, NULL);
HWND taskbar_hwnd = FindWindow(kTrayClassName, NULL);
int swapped = GetSystemMetrics(SM_SWAPBUTTON);
int left_button = swapped ? VK_RBUTTON : VK_LBUTTON;
bool left_button_down = GetAsyncKeyState(left_button) < 0;
int right_button = swapped ? VK_LBUTTON : VK_RBUTTON;
bool right_button_down = GetAsyncKeyState(right_button) < 0;
HWND focused_hwnd = GetForegroundWindow();
if (!focused_hwnd) {
return !right_button_down && !left_button_down;
}
while (focused_hwnd) {
if (focused_hwnd == jump_list_hwnd || focused_hwnd == view_->GetHWND())
return false;
if (focused_hwnd == taskbar_hwnd) {
if (right_button_down)
return false;
if (!taskbar_had_focus) {
taskbar_has_focus_ = true;
return false;
}
return true;
}
focused_hwnd = GetParent(focused_hwnd);
}
return true;
}