Skip to content

Commit

Permalink
fix(WeaselUI): specify default label format in constructor
Browse files Browse the repository at this point in the history
If weasel.yaml is not updated to include style/label_format due to issue
of librime 1.3.0, fallback to default value initialized with WeaselUI's
construction.

Fixes rime#147
  • Loading branch information
Prcuvu authored and nameoverflow committed Mar 25, 2018
1 parent fbdb667 commit 7194339
Show file tree
Hide file tree
Showing 22 changed files with 625 additions and 270 deletions.
93 changes: 53 additions & 40 deletions RimeWithWeasel/RimeWithWeasel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
#include <StringAlgorithm.hpp>
#include <WeaselUtility.h>
#include <WeaselVersion.h>
#include <algorithm>
#include <list>
#include <set>
#include <string>

#include <rime_api.h>

Expand Down Expand Up @@ -252,6 +248,35 @@ void RimeWithWeaselHandler::_ReadClientInfo(UINT session_id, LPWSTR buffer)
RimeSetOption(session_id, "soft_cursor", Bool(!inline_preedit));
}

void RimeWithWeaselHandler::_GetCandidateInfo(weasel::CandidateInfo & cinfo, RimeContext & ctx)
{
cinfo.candies.resize(ctx.menu.num_candidates);
cinfo.comments.resize(ctx.menu.num_candidates);
cinfo.labels.resize(ctx.menu.num_candidates);
for (int i = 0; i < ctx.menu.num_candidates; ++i)
{
cinfo.candies[i].str = utf8towcs(ctx.menu.candidates[i].text);
if (ctx.menu.candidates[i].comment)
{
cinfo.comments[i].str = utf8towcs(ctx.menu.candidates[i].comment);
}
if (RIME_STRUCT_HAS_MEMBER(ctx, ctx.select_labels) && ctx.select_labels)
{
cinfo.labels[i].str = utf8towcs(ctx.select_labels[i]);
}
else if (ctx.menu.select_keys)
{
cinfo.labels[i].str = std::wstring(1, ctx.menu.select_keys[i]);
}
else
{
cinfo.labels[i].str = std::to_wstring((i + 1) % 10);
}
}
cinfo.highlighted = ctx.menu.highlighted_candidate_index;
cinfo.currentPage = ctx.menu.page_no;
}

void RimeWithWeaselHandler::StartMaintenance()
{
Finalize();
Expand Down Expand Up @@ -281,6 +306,7 @@ bool RimeWithWeaselHandler::_IsDeployerRunning()

void RimeWithWeaselHandler::_UpdateUI(UINT session_id)
{
return;
weasel::Status weasel_status;
weasel::Context weasel_context;

Expand Down Expand Up @@ -322,31 +348,7 @@ void RimeWithWeaselHandler::_UpdateUI(UINT session_id)
if (ctx.menu.num_candidates)
{
weasel::CandidateInfo &cinfo(weasel_context.cinfo);
cinfo.candies.resize(ctx.menu.num_candidates);
cinfo.comments.resize(ctx.menu.num_candidates);
cinfo.labels.resize(ctx.menu.num_candidates);
for (int i = 0; i < ctx.menu.num_candidates; ++i)
{
cinfo.candies[i].str = utf8towcs(ctx.menu.candidates[i].text);
if (ctx.menu.candidates[i].comment)
{
cinfo.comments[i].str = utf8towcs(ctx.menu.candidates[i].comment);
}
if (RIME_STRUCT_HAS_MEMBER(ctx, ctx.select_labels) && ctx.select_labels)
{
cinfo.labels[i].str = utf8towcs(ctx.select_labels[i]);
}
else if (ctx.menu.select_keys)
{
cinfo.labels[i].str = std::wstring(1, ctx.menu.select_keys[i]);
}
else
{
cinfo.labels[i].str = std::to_wstring((i + 1) % 10);
}
}
cinfo.highlighted = ctx.menu.highlighted_candidate_index;
cinfo.currentPage = ctx.menu.page_no;
_GetCandidateInfo(cinfo, ctx);
}
RimeFreeContext(&ctx);
}
Expand Down Expand Up @@ -458,7 +460,7 @@ bool RimeWithWeaselHandler::_Respond(UINT session_id, EatLine eat)
actions.insert("ctx");
switch (m_ui->style().preedit_type)
{
case weasel::PREVIEW:
case weasel::UIStyle::PREVIEW:
if (ctx.commit_text_preview != NULL)
{
std::string first = ctx.commit_text_preview;
Expand All @@ -469,7 +471,7 @@ bool RimeWithWeaselHandler::_Respond(UINT session_id, EatLine eat)
break;
}
// no preview, fall back to composition
case weasel::COMPOSITION:
case weasel::UIStyle::COMPOSITION:
messages.push_back(std::string("ctx.preedit=") + ctx.composition.preedit + '\n');
if (ctx.composition.sel_start <= ctx.composition.sel_end)
{
Expand All @@ -480,6 +482,17 @@ bool RimeWithWeaselHandler::_Respond(UINT session_id, EatLine eat)
break;
}
}
if (ctx.menu.num_candidates)
{
weasel::CandidateInfo cinfo;
std::wstringstream ss;
boost::archive::text_woarchive oa(ss);
_GetCandidateInfo(cinfo, ctx);

oa << cinfo;

messages.push_back(std::string("ctx.cand=") + wcstoutf8(ss.str().c_str()) + '\n');
}
RimeFreeContext(&ctx);
}

Expand Down Expand Up @@ -538,9 +551,9 @@ static void _UpdateUIStyle(RimeConfig* config, weasel::UI* ui, bool initialize)
if (RimeConfigGetString(config, "style/preedit_type", preedit_type, sizeof(preedit_type) - 1))
{
if (!std::strcmp(preedit_type, "composition"))
style.preedit_type = weasel::COMPOSITION;
style.preedit_type = weasel::UIStyle::COMPOSITION;
else if (!std::strcmp(preedit_type, "preview"))
style.preedit_type = weasel::PREVIEW;
style.preedit_type = weasel::UIStyle::PREVIEW;
}
Bool display_tray_icon = False;
if (RimeConfigGetBool(config, "style/display_tray_icon", &display_tray_icon) || initialize)
Expand All @@ -550,13 +563,13 @@ static void _UpdateUIStyle(RimeConfig* config, weasel::UI* ui, bool initialize)
Bool horizontal = False;
if (RimeConfigGetBool(config, "style/horizontal", &horizontal) || initialize)
{
style.layout_type = horizontal ? weasel::LAYOUT_HORIZONTAL : weasel::LAYOUT_VERTICAL;
style.layout_type = horizontal ? weasel::UIStyle::LAYOUT_HORIZONTAL : weasel::UIStyle::LAYOUT_VERTICAL;
}
Bool fullscreen = False;
if (RimeConfigGetBool(config, "style/fullscreen", &fullscreen) && fullscreen)
{
style.layout_type = (style.layout_type == weasel::LAYOUT_HORIZONTAL)
? weasel::LAYOUT_HORIZONTAL_FULLSCREEN : weasel::LAYOUT_VERTICAL_FULLSCREEN;
style.layout_type = (style.layout_type == weasel::UIStyle::LAYOUT_HORIZONTAL)
? weasel::UIStyle::LAYOUT_HORIZONTAL_FULLSCREEN : weasel::UIStyle::LAYOUT_VERTICAL_FULLSCREEN;
}
char label_text_format[128] = { 0 };
if (RimeConfigGetString(config, "style/label_format", label_text_format, sizeof(label_text_format) - 1))
Expand All @@ -568,13 +581,13 @@ static void _UpdateUIStyle(RimeConfig* config, weasel::UI* ui, bool initialize)
if (RimeConfigGetString(config, "style/layout/type", layout_type, sizeof(layout_type) - 1))
{
if (!std::strcmp(layout_type, "vertical"))
style.layout_type = weasel::LAYOUT_VERTICAL;
style.layout_type = weasel::UIStyle::LAYOUT_VERTICAL;
else if (!std::strcmp(layout_type, "horizontal"))
style.layout_type = weasel::LAYOUT_HORIZONTAL;
style.layout_type = weasel::UIStyle::LAYOUT_HORIZONTAL;
if (!std::strcmp(layout_type, "vertical+fullscreen"))
style.layout_type = weasel::LAYOUT_VERTICAL_FULLSCREEN;
style.layout_type = weasel::UIStyle::LAYOUT_VERTICAL_FULLSCREEN;
else if (!std::strcmp(layout_type, "horizontal+fullscreen"))
style.layout_type = weasel::LAYOUT_HORIZONTAL_FULLSCREEN;
style.layout_type = weasel::UIStyle::LAYOUT_HORIZONTAL_FULLSCREEN;
else
LOG(WARNING) << "Invalid style type: " << layout_type;
}
Expand Down
23 changes: 0 additions & 23 deletions RimeWithWeasel/WeaselUtility.cpp
Original file line number Diff line number Diff line change
@@ -1,29 +1,6 @@
#include "stdafx.h"
#include <string>

const char* wcstoutf8(const WCHAR* wstr)
{
const int buffer_len = 1024;
static char buffer[buffer_len];
memset(buffer, 0, sizeof(buffer));
WideCharToMultiByte(CP_UTF8, 0, wstr, -1, buffer, buffer_len - 1, NULL, NULL);
return buffer;
}

const WCHAR* utf8towcs(const char* utf8_str)
{
const int buffer_len = 4096;
static WCHAR buffer[buffer_len];
memset(buffer, 0, sizeof(buffer));
MultiByteToWideChar(CP_UTF8, 0, utf8_str, -1, buffer, buffer_len - 1);
return buffer;
}

int utf8towcslen(const char* utf8_str, int utf8_len)
{
return MultiByteToWideChar(CP_UTF8, 0, utf8_str, utf8_len, NULL, 0);
}

std::wstring WeaselUserDataPath() {
WCHAR path[MAX_PATH] = {0};
const WCHAR KEY[] = L"Software\\Rime\\Weasel";
Expand Down
8 changes: 8 additions & 0 deletions RimeWithWeasel/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@
#pragma warning(disable : 4819)

#include <boost/interprocess/streams/bufferstream.hpp>
#include <boost/archive/text_woarchive.hpp>
#include <boost/archive/text_wiarchive.hpp>

#pragma warning(default : 4819)

#include <algorithm>
#include <list>
#include <set>
#include <string>
#include <sstream>

using boost::interprocess::wbufferstream;
36 changes: 5 additions & 31 deletions WeaselIPC/ContextUpdater.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "stdafx.h"
#include <StringAlgorithm.hpp>
#include <WeaselUtility.h>

#include "Deserializer.h"
#include "ContextUpdater.h"

Expand Down Expand Up @@ -76,38 +78,10 @@ void ContextUpdater::_StoreText(Text& target, Deserializer::KeyType k, std::wstr
void ContextUpdater::_StoreCand(Deserializer::KeyType k, std::wstring const& value)
{
CandidateInfo& cinfo = m_pTarget->p_context->cinfo;
if(k.size() < 3)
return;
if (k[2] == L"length")
{
cinfo.clear();
int size = _wtoi(value.c_str());
cinfo.candies.resize(size);
return;
}
if (k[2] == L"cursor")
{
int cur = _wtoi(value.c_str());
cinfo.highlighted = cur;
return;
}
if (k[2] == L"page")
{
std::vector<std::wstring> vec;
split(vec, value, L"/");
if (vec.size() == 0)
return;
int i = _wtoi(vec[0].c_str());
cinfo.currentPage = i;
int n = (vec.size() >= 2) ? _wtoi(vec[1].c_str()) : 0;
cinfo.totalPages = n;
return;
}
std::wstringstream ss(value);
boost::archive::text_wiarchive ia(ss);

size_t idx = _wtoi(k[2].c_str());
if (idx >= cinfo.candies.size())
return;
cinfo.candies[idx].str = value;
ia >> cinfo;
}

// StatusUpdater
Expand Down
2 changes: 2 additions & 0 deletions WeaselIPC/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
#include <boost/interprocess/streams/bufferstream.hpp>
#include <boost/interprocess/windows_shared_memory.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/archive/text_wiarchive.hpp>

#pragma warning(default: 4819)
#pragma warning(default: 4996)

#include <map>
#include <string>
#include <vector>
#include <sstream>

using boost::interprocess::wbufferstream;
Loading

0 comments on commit 7194339

Please sign in to comment.