blob: 8a586ce5b63c3d351c6f6c7f0af69004f3bd488e [file] [log] [blame]
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_ASH_INPUT_METHOD_MULTI_WORD_SUGGESTER_H_
#define CHROME_BROWSER_ASH_INPUT_METHOD_MULTI_WORD_SUGGESTER_H_
#include "base/time/time.h"
#include "chrome/browser/ash/input_method/suggester.h"
#include "chrome/browser/ash/input_method/suggestion_enums.h"
#include "chrome/browser/ash/input_method/suggestion_handler_interface.h"
#include "chromeos/services/ime/public/cpp/suggestions.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
namespace ash {
namespace input_method {
// TODO(crbug/1146266): move these to an internal state class / struct
struct LastKnownTextState {
std::u16string text;
bool cursor_at_end_of_text;
};
// TODO(crbug/1146266): move these to the internal SuggestionState class
struct LastKnownSuggestionState {
size_t start_pos;
std::u16string text;
size_t confirmed_length;
size_t predicted_text_start_pos;
size_t predicted_text_length;
ime::TextSuggestionMode suggestion_mode;
base::TimeTicks time_shown_to_user;
};
// Integrates multi word suggestions produced by the system with the assistive
// framework. Handles showing / accepting / dismissing any multi word
// suggestions generated by the system.
class MultiWordSuggester : public Suggester {
public:
// `suggestion_handler` needs to exist longer than the lifetime of this
// object.
explicit MultiWordSuggester(SuggestionHandlerInterface* suggestion_handler);
~MultiWordSuggester() override;
// Suggester overrides:
void OnFocus(int context_id) override;
void OnBlur() override;
void OnExternalSuggestionsUpdated(
const std::vector<ime::TextSuggestion>& suggestions) override;
SuggestionStatus HandleKeyEvent(const ui::KeyEvent& event) override;
bool Suggest(const std::u16string& text,
size_t cursor_pos,
size_t anchor_pos) override;
bool AcceptSuggestion(size_t index = 0) override;
void DismissSuggestion() override;
AssistiveType GetProposeActionType() override;
bool HasSuggestions() override;
std::vector<ime::TextSuggestion> GetSuggestions() override;
void OnSurroundingTextChanged(const std::u16string& text,
size_t cursor_pos,
size_t anchor_pos);
private:
// Used to capture any internal state around the previously or currently
// shown suggestions.
class SuggestionState {
public:
enum State {
kNoSuggestionShown,
kPredictionSuggestionShown,
kCompletionSuggestionShown,
kTrackingLastSuggestionShown,
kSuggestionDismissed,
kSuggestionAccepted,
};
explicit SuggestionState(MultiWordSuggester* suggester);
~SuggestionState();
// As the name suggests, used to update the current state and perform
// any actions required during a transition.
void UpdateState(const State& state);
// Returns the last suggestion type shown to the user. This suggestion may,
// or may not, be currently showing to the user.
AssistiveType GetLastSuggestionType();
private:
// Not owned by this class
MultiWordSuggester* suggester_;
// Current state
State state_ = State::kNoSuggestionShown;
// The last suggestion type shown to the user.
AssistiveType last_suggestion_type_ = AssistiveType::kGenericAction;
};
void DisplaySuggestion(const std::u16string& text, int confirmed_length);
void ResetSuggestionState();
void ResetTextState();
// Announce the given message to the user.
void Announce(const std::u16string& message);
// The currently focused input (zero if none are focused)
int focused_context_id_ = 0;
// Previous suggestion details
//
// TODO(crbug/1146266): replace this with the new SuggestionState class
absl::optional<LastKnownSuggestionState> suggestion_state_;
// The last known state of text in the focused text input
LastKnownTextState text_state_;
// Not owned by this class
SuggestionHandlerInterface* suggestion_handler_;
// Current suggestion state
SuggestionState state_;
};
} // namespace input_method
} // namespace ash
#endif // CHROME_BROWSER_ASH_INPUT_METHOD_MULTI_WORD_SUGGESTER_H_