Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DictypeFcitx/src/DictypeFcitx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ void DictypeFcitx::trigger_(const fcitx::KeyEvent& keyEvent,
that2->updateUI_();

that2->closeUI_();
that2->state_.clear();
that2->state_.end();

that2->running_.store(false, std::memory_order_release);
});
Expand Down
32 changes: 19 additions & 13 deletions DictypeFcitx/src/DictypeState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,33 @@

DictypeState::DictypeState() = default;

void DictypeState::clear() {
void DictypeState::end() {
latestCommittableBeginTime_ = -1;
if (!texts_.empty()) {
DICTYPE_WARN() << "uncommitted texts: " << getUncommittedText();
texts_.clear();
}
cleared_ = true;
ended_ = true;
}

bool DictypeState::newSession(fcitx::InputContext* inputContext) {
if (!cleared_) {
DICTYPE_WARN() << "Previous session is not cleared.";
if (!ended_) {
DICTYPE_WARN() << "Previous session is not ended.";
return false;
}
if (stage_ == DictypeStage::Errored) {
stage_ = DictypeStage::Closed;
errorMsg_.clear();
inputContext_.unwatch();
}
if (inputContext == nullptr) {
inputContext_.unwatch();
} else {
inputContext_ = inputContext->watch();
DICTYPE_WARN() << "inputContext is nullptr";
return false;
}
cleared_ = false;

// Clear states from the last session
stage_ = DictypeStage::Closed;
errorMsg_.clear();
inputContext_.unwatch();

// Set up for the new session
inputContext_ = inputContext->watch();
ended_ = false;
return true;
}

Expand All @@ -56,6 +58,10 @@ void DictypeState::setConnecting() {
}

void DictypeState::setText(const Dictype::TranscribeResponse& response) {
if (ended_) {
DICTYPE_WARN() << "Previous session is not ended.";
return;
}
if (!(stage_ == DictypeStage::Connecting ||
stage_ == DictypeStage::Transcribing ||
stage_ == DictypeStage::Stopping)) {
Expand Down
10 changes: 8 additions & 2 deletions DictypeFcitx/src/DictypeState.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class DictypeState final : public fcitx::InputContextProperty {
public:
explicit DictypeState();

void clear();
void end();
bool newSession(fcitx::InputContext* inputContext);

void stop();
Expand All @@ -52,6 +52,12 @@ class DictypeState final : public fcitx::InputContextProperty {
std::map<uint32_t, std::string> texts_;
std::string errorMsg_;
fcitx::TrackableObjectReference<fcitx::InputContext> inputContext_;
bool cleared_ = true;

/**
* A flag tied to the lifetime of a session.
*
* Only live sessions can accept state mutations.
*/
bool ended_ = true;
DictypeStage stage_{DictypeStage::Closed};
};
33 changes: 27 additions & 6 deletions DictypeFcitx/src/DictypeState.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include <gtest/gtest.h>

#include <fcitx/inputcontextmanager.h>

#include "DictypeState.h"
#include "dictype.grpc.pb.h"

Expand All @@ -27,10 +29,12 @@ TEST(DictypeStateTest, DefaultStateIsClosed) {

TEST(DictypeStateTest, NewSessionClearsStateAndData) {
DictypeState state;
const fcitx::InputContextManager icm{};
state.newSession(icm.dummyInputContext());

state.setText(MakeResponse(1, "hello ", true));

state.clear();
state.end();

EXPECT_EQ(state.getStage(), DictypeStage::Closed);
EXPECT_EQ(state.getErrorMsg(), "");
Expand All @@ -40,14 +44,19 @@ TEST(DictypeStateTest, NewSessionClearsStateAndData) {

TEST(DictypeStateTest, NewSessionRequiresPreviousClear) {
DictypeState state;
EXPECT_TRUE(state.newSession(nullptr));
const fcitx::InputContextManager icm{};

EXPECT_TRUE(state.newSession(icm.dummyInputContext()));
EXPECT_FALSE(state.newSession(nullptr));
state.clear();
EXPECT_TRUE(state.newSession(nullptr));
state.end();
EXPECT_TRUE(state.newSession(icm.dummyInputContext()));
}

TEST(DictypeStateTest, StopTransitionsOnlyFromConnectingOrTranscribing) {
DictypeState state;
const fcitx::InputContextManager icm{};
state.newSession(icm.dummyInputContext());

state.stop();
EXPECT_EQ(state.getStage(), DictypeStage::Closed);

Expand All @@ -62,6 +71,9 @@ TEST(DictypeStateTest, StopTransitionsOnlyFromConnectingOrTranscribing) {

TEST(DictypeStateTest, SetWordIgnoredUnlessConnectingOrTranscribing) {
DictypeState state;
const fcitx::InputContextManager icm{};
state.newSession(icm.dummyInputContext());

state.setText(MakeResponse(1, "hello "));
EXPECT_EQ(state.getStage(), DictypeStage::Closed);
EXPECT_EQ(state.getUncommittedText(), "");
Expand All @@ -71,6 +83,9 @@ TEST(DictypeStateTest, SetWordIgnoredUnlessConnectingOrTranscribing) {
TEST(DictypeStateTest,
SetWordTransitionsToTranscribingAndTracksCommitBoundaries) {
DictypeState state;
const fcitx::InputContextManager icm{};
state.newSession(icm.dummyInputContext());

state.setConnecting();
state.setText(MakeResponse(0, "hello ", false));
EXPECT_EQ(state.getStage(), DictypeStage::Transcribing);
Expand All @@ -92,6 +107,9 @@ TEST(DictypeStateTest,

TEST(DictypeStateTest, SetErrorStoresFirstErrorAndLocksStage) {
DictypeState state;
const fcitx::InputContextManager icm{};
state.newSession(icm.dummyInputContext());

state.setError("boom");
EXPECT_EQ(state.getStage(), DictypeStage::Errored);
EXPECT_EQ(state.getErrorMsg(), "boom");
Expand All @@ -103,15 +121,18 @@ TEST(DictypeStateTest, SetErrorStoresFirstErrorAndLocksStage) {

TEST(DictypeStateTest, RetainErrorMessageUntilNewSession) {
DictypeState state;
const fcitx::InputContextManager icm{};
state.newSession(icm.dummyInputContext());

state.setError("boom");
EXPECT_EQ(state.getStage(), DictypeStage::Errored);
EXPECT_EQ(state.getErrorMsg(), "boom");

state.clear();
state.end();
EXPECT_EQ(state.getStage(), DictypeStage::Errored);
EXPECT_EQ(state.getErrorMsg(), "boom");

state.newSession(nullptr);
state.newSession(icm.dummyInputContext());
EXPECT_EQ(state.getStage(), DictypeStage::Closed);
EXPECT_EQ(state.getErrorMsg(), "");
}