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
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103

content / browser / renderer_host / input / stylus_handwriting_controller_win.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 "content/browser/renderer_host/input/stylus_handwriting_controller_win.h"

#include "components/stylus_handwriting/win/features.h"
#include "content/public/browser/browser_thread.h"
#include "ui/base/ime/text_input_client.h"
#include "ui/base/ime/win/tsf_bridge.h"

namespace content {

namespace {

StylusHandwritingControllerWin* g_instance = nullptr;
StylusHandwritingControllerWin* g_instance_for_testing = nullptr;

}  // namespace

// static
base::AutoReset<StylusHandwritingControllerWin*>
StylusHandwritingControllerWin::SetInstanceForTesting(
    StylusHandwritingControllerWin* instance) {
  return {&g_instance_for_testing, instance};
}

// static
bool StylusHandwritingControllerWin::IsHandwritingAPIAvailable() {
  CHECK_CURRENTLY_ON(content::BrowserThread::UI);
  if (!stylus_handwriting::win::IsStylusHandwritingWinEnabled()) {
    return false;
  }

  return GetInstance();
}

// static
void StylusHandwritingControllerWin::Initialize() {
  CHECK_CURRENTLY_ON(content::BrowserThread::UI);
  if (!stylus_handwriting::win::IsStylusHandwritingWinEnabled() ||
      g_instance_for_testing) {
    return;
  }

  // See the constructor and BindInterfaces() for more initialization details
  // where g_instance is set.
  static base::NoDestructor<StylusHandwritingControllerWin> instance;
}

// static
StylusHandwritingControllerWin* StylusHandwritingControllerWin::GetInstance() {
  CHECK_CURRENTLY_ON(content::BrowserThread::UI);
  CHECK(stylus_handwriting::win::IsStylusHandwritingWinEnabled());
  return g_instance_for_testing ? g_instance_for_testing : g_instance;
}

// static
Microsoft::WRL::ComPtr<ITfThreadMgr>
StylusHandwritingControllerWin::GetThreadManager() {
  return ui::TSFBridge::GetInstance()
             ? ui::TSFBridge::GetInstance()->GetThreadManager()
             : nullptr;
}

StylusHandwritingControllerWin::StylusHandwritingControllerWin() {
  BindInterfaces();
}

StylusHandwritingControllerWin::~StylusHandwritingControllerWin() = default;

void StylusHandwritingControllerWin::OnStartStylusWriting(
    OnFocusHandwritingTargetCallback callback,
    uint32_t pointer_id,
    uint64_t stroke_id,
    ui::TextInputClient& text_input_client) {
  // TODO(crbug.com/355578906): Implement
  // SetInputEvaluation(::TfInputEvaluation::TF_IE_HANDWRITING) logic.
}

void StylusHandwritingControllerWin::OnFocusHandled(
    ui::TextInputClient& text_input_client) {
  // TODO(crbug.com/355578906): Forward call to ::ITfHandwritingSink controller.
}

void StylusHandwritingControllerWin::OnFocusFailed(
    ui::TextInputClient& text_input_client) {
  // TODO(crbug.com/355578906): Forward call to ::ITfHandwritingSink controller.
}

void StylusHandwritingControllerWin::BindInterfaces() {
  if (auto thread_manager = GetThreadManager()) {
    const bool initialized_successfully =
        SUCCEEDED(thread_manager.As(&handwriting_)) &&
        SUCCEEDED(handwriting_->SetHandwritingState(
            ::TF_HANDWRITING_POINTERDELIVERY));
    if (initialized_successfully) {
      g_instance = this;
    }
  }
}

}  // namespace content