#include "ash/style/system_textfield_controller.h"
#include "ui/views/controls/focus_ring.h"
#include "ui/views/focus/focus_manager.h"
#include "ui/views/widget/widget.h"
namespace ash {
SystemTextfieldController::SystemTextfieldController(SystemTextfield* textfield)
: textfield_(textfield) {
textfield_->SetController(this);
textfield_->set_delegate(this);
}
SystemTextfieldController::~SystemTextfieldController() = default;
void SystemTextfieldController::OnTextfieldFocused(SystemTextfield* textfield) {
DCHECK_EQ(textfield_, textfield);
textfield_->SetShowFocusRing(true);
}
void SystemTextfieldController::OnTextfieldBlurred(SystemTextfield* textfield) {
DCHECK_EQ(textfield_, textfield);
textfield_->SetActive(false);
textfield_->SetShowFocusRing(false);
}
bool SystemTextfieldController::HandleKeyEvent(views::Textfield* sender,
const ui::KeyEvent& key_event) {
DCHECK_EQ(textfield_, sender);
if (key_event.type() != ui::ET_KEY_PRESSED) {
return false;
}
const bool active = textfield_->IsActive();
if (key_event.key_code() == ui::VKEY_RETURN) {
if (!active) {
textfield_->SetActive(true);
textfield_->SelectAll(false);
return true;
}
textfield_->SetActive(false);
return true;
}
if (key_event.key_code() == ui::VKEY_ESCAPE) {
if (active) {
textfield_->RestoreText();
ContentsChanged(textfield_, textfield_->GetText());
textfield_->SetActive(false);
return true;
}
}
return false;
}
bool SystemTextfieldController::HandleMouseEvent(
views::Textfield* sender,
const ui::MouseEvent& mouse_event) {
DCHECK_EQ(textfield_, sender);
if (!mouse_event.IsOnlyLeftMouseButton() &&
!mouse_event.IsOnlyRightMouseButton()) {
return false;
}
switch (mouse_event.type()) {
case ui::ET_MOUSE_PRESSED:
if (!textfield_->IsActive()) {
defer_select_all_ = true;
textfield_->SetActive(true);
}
break;
case ui::ET_MOUSE_RELEASED:
if (defer_select_all_) {
defer_select_all_ = false;
if (!textfield_->HasSelection()) {
textfield_->SelectAll(false);
}
return true;
}
break;
default:
break;
}
return false;
}
}