1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65

ash / quick_insert / quick_insert_action_on_next_focus_request.cc [blame]

// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/quick_insert/quick_insert_action_on_next_focus_request.h"

#include <utility>

#include "base/check.h"
#include "base/functional/callback.h"
#include "base/location.h"
#include "base/scoped_observation.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "ui/base/ime/input_method.h"
#include "ui/base/ime/input_method_observer.h"
#include "ui/base/ime/text_input_client.h"
#include "ui/base/ime/text_input_type.h"

namespace ash {

QuickInsertActionOnNextFocusRequest::QuickInsertActionOnNextFocusRequest(
    ui::InputMethod* input_method,
    base::TimeDelta action_timeout,
    base::OnceClosure action_callback,
    base::OnceClosure timeout_callback)
    : action_callback_(std::move(action_callback)),
      timeout_callback_(std::move(timeout_callback)) {
  observation_.Observe(input_method);
  action_timeout_timer_.Start(FROM_HERE, action_timeout, this,
                              &QuickInsertActionOnNextFocusRequest::OnTimeout);
}

QuickInsertActionOnNextFocusRequest::~QuickInsertActionOnNextFocusRequest() =
    default;

void QuickInsertActionOnNextFocusRequest::OnTextInputStateChanged(
    const ui::TextInputClient* client) {
  ui::TextInputClient* mutable_client =
      observation_.GetSource()->GetTextInputClient();
  CHECK_EQ(mutable_client, client);
  if (mutable_client == nullptr ||
      mutable_client->GetTextInputType() ==
          ui::TextInputType::TEXT_INPUT_TYPE_NONE) {
    return;
  }

  action_timeout_timer_.Stop();
  observation_.Reset();
  std::move(action_callback_).Run();
}

void QuickInsertActionOnNextFocusRequest::OnInputMethodDestroyed(
    const ui::InputMethod* input_method) {
  if (observation_.GetSource() == input_method) {
    observation_.Reset();
  }
}

void QuickInsertActionOnNextFocusRequest::OnTimeout() {
  observation_.Reset();
  std::move(timeout_callback_).Run();
}

}  // namespace ash