Skip to content

Commit

Permalink
1、修复切换方案样式不更新
Browse files Browse the repository at this point in the history
2、同步官网添加「重启算法服务」菜单
  • Loading branch information
Techince committed Mar 1, 2024
1 parent b9e819e commit f0c0391
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 28 deletions.
69 changes: 43 additions & 26 deletions RimeWithWeasel/RimeWithWeasel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,17 @@ RimeSessionId RimeWithWeaselHandler::AddSession(LPWSTR buffer, EatLine eat)
if (m_disabled) return 0;
}
RimeSessionId session_id = RimeCreateSession();
if (m_global_ascii_mode)
{
for (const auto& pair : m_session_status_map)
{
if (pair.first)
{
RimeSetOption(session_id, "ascii_mode", !!pair.second.status.is_ascii_mode);
break;
}
}
}
DLOG(INFO) << "Add session: created session_id = " << session_id;
_ReadClientInfo(session_id, buffer);

Expand Down Expand Up @@ -614,32 +625,32 @@ void RimeWithWeaselHandler::_LoadIconSettingFromSchema(RimeConfig& config, char*
void RimeWithWeaselHandler::_LoadSchemaSpecificSettings(RimeSessionId session_id, const std::string& schema_id)
{
if (!m_ui) return;
const int BUF_SIZE = 255;
char buffer[BUF_SIZE + 1];
RimeConfig config;
if (!RimeSchemaOpen(schema_id.c_str(), &config))
return;
_UpdateShowNotifications(&config);
m_ui->style() = m_base_style;
_UpdateUIStyle(&config, m_ui, false);
SessionStatus& session_status = m_session_status_map[session_id];
session_status.style = m_ui->style();

// load schema color style config
memset(buffer, '\0', sizeof(buffer));
const int BUF_SIZE = 255;
char buffer[BUF_SIZE + 1]{};
if (!m_current_dark_mode && RimeConfigGetString(&config, "style/color_scheme", buffer, BUF_SIZE))
{
std::string color_name{ buffer };
RimeConfigIterator preset{ 0 };
if (RimeConfigBeginMap(&preset, &config, std::format("preset_color_schemes/{}", color_name).data()))
{
_UpdateUIStyleColor(&config, m_ui->style(), color_name);
_UpdateUIStyleColor(&config, session_status.style, color_name);
}
else
{
RimeConfig weaselconfig;
if (RimeConfigOpen("weasel", &weaselconfig))
{
_UpdateUIStyleColor(&weaselconfig, m_ui->style(), std::string(buffer));
_UpdateUIStyleColor(&weaselconfig, session_status.style, std::string(buffer));
RimeConfigClose(&weaselconfig);
}
}
Expand All @@ -650,14 +661,14 @@ void RimeWithWeaselHandler::_LoadSchemaSpecificSettings(RimeSessionId session_id
RimeConfigIterator preset{};
if (RimeConfigBeginMap(&preset, &config, std::format("preset_color_schemes/{}", color_name).data()))
{
_UpdateUIStyleColor(&config, m_ui->style(), color_name);
_UpdateUIStyleColor(&config, session_status.style, color_name);
}
else
{
RimeConfig weaselconfig;
if (RimeConfigOpen("weasel", &weaselconfig))
{
_UpdateUIStyleColor(&weaselconfig, m_ui->style(), std::string(buffer));
_UpdateUIStyleColor(&weaselconfig, session_status.style, std::string(buffer));
RimeConfigClose(&weaselconfig);
}
}
Expand Down Expand Up @@ -688,7 +699,6 @@ void RimeWithWeaselHandler::_LoadAppInlinePreeditSet(RimeSessionId session_id, b
bool inline_preedit = session_status.style.inline_preedit;
if (!app_name.empty())
{
// auto it = m_app_options.find(app_name);
if (auto it = m_app_options.find(app_name); it != m_app_options.end())
{
AppOptions& options(m_app_options[it->first]);
Expand All @@ -714,6 +724,7 @@ void RimeWithWeaselHandler::_LoadAppInlinePreeditSet(RimeSessionId session_id, b
else
{
load_schema_inline:
session_status.style.inline_preedit = m_base_style.inline_preedit;
RIME_STRUCT(RimeStatus, status);
if (RimeGetStatus(session_id, &status))
{
Expand Down Expand Up @@ -843,7 +854,7 @@ bool RimeWithWeaselHandler::_Respond(RimeSessionId session_id, EatLine eat)
if (is_composing)
{
actions.insert("ctx");
switch (m_ui->style().preedit_type)
switch (session_status.style.preedit_type)
{
case weasel::UIStyle::PREVIEW:
if (ctx.commit_text_preview != NULL)
Expand Down Expand Up @@ -914,7 +925,7 @@ bool RimeWithWeaselHandler::_Respond(RimeSessionId session_id, EatLine eat)
{
std::wstringstream ss;
boost::archive::text_woarchive oa(ss);
oa << m_ui->style();
oa << session_status.style;

actions.insert("style");
messages.emplace_back(std::format("style={}\n", to_string(ss.str(), CP_UTF8)));
Expand Down Expand Up @@ -1058,14 +1069,17 @@ static void _RimeGetBool(RimeConfig* config, const char* key, bool cond, T& valu

// parse string option to T type value, with fallback
template <typename T>
void _RimeParseStringOpt(RimeConfig* config, const std::string& key, T& value, const std::map<std::string, T>& amap)
void _RimeParseStringOptWithFallback(RimeConfig* config, const std::string& key, T& value, const std::map<std::string, T>& amap, const T& fallback)
{
char str_buff[256]{};
if (RimeConfigGetString(config, key.data(), str_buff, sizeof(str_buff) - 1))
{
auto it = amap.find(str_buff);
if (it != amap.end())
value = it->second;
value = (it != amap.end()) ? it->second : fallback;
}
else
{
value = fallback;
}
}

Expand Down Expand Up @@ -1151,9 +1165,6 @@ void RimeWithWeaselHandler::_UpdateShowNotifications(RimeConfig* config, bool in
static void _UpdateUIStyle(RimeConfig* config, weasel::UI* ui, bool initialize)
{
weasel::UIStyle& style(ui->style());

const int BUF_SIZE = 255;
char buffer[BUF_SIZE + 1]{};
// get font faces
_RimeGetStringWithFunc(config, "style/font_face", style.font_face, nullptr, _RemoveSpaceAroundSep);
std::wstring* const pFallbackFontFace = initialize ? &style.font_face : nullptr;
Expand All @@ -1175,6 +1186,7 @@ static void _UpdateUIStyle(RimeConfig* config, weasel::UI* ui, bool initialize)
_RimeGetIntWithFallback(config, "style/label_font_point", &style.label_font_point, "style/font_point", _abs);
_RimeGetIntWithFallback(config, "style/comment_font_point", &style.comment_font_point, "style/font_point", _abs);
_RimeGetIntWithFallback(config, "style/mouse_hover_ms", &style.mouse_hover_ms, nullptr, _abs);
_RimeGetIntWithFallback(config, "style/candidate_abbreviate_length", &style.candidate_abbreviate_length, nullptr, _abs);
_RimeGetBool(config, "style/inline_preedit", initialize, style.inline_preedit, true, false);
_RimeGetBool(config, "style/vertical_auto_reverse", initialize, style.vertical_auto_reverse, true, false);
const std::map<std::string, UIStyle::PreeditType> _preeditMap
Expand All @@ -1184,7 +1196,7 @@ static void _UpdateUIStyle(RimeConfig* config, weasel::UI* ui, bool initialize)
{ "preview_all", UIStyle::PREVIEW_ALL }
};

_RimeParseStringOpt(config, "style/preedit_type", style.preedit_type, _preeditMap);
_RimeParseStringOptWithFallback(config, "style/preedit_type", style.preedit_type, _preeditMap, style.preedit_type);
const std::map<std::string, UIStyle::AntiAliasMode> _aliasModeMap
{
{ "force_dword", UIStyle::FORCE_DWORD },
Expand All @@ -1194,14 +1206,14 @@ static void _UpdateUIStyle(RimeConfig* config, weasel::UI* ui, bool initialize)
{ "default", UIStyle::DEFAULT }
};

_RimeParseStringOpt(config, "style/antialias_mode", style.antialias_mode, _aliasModeMap);
_RimeParseStringOptWithFallback(config, "style/antialias_mode", style.antialias_mode, _aliasModeMap, style.antialias_mode);
const std::map<std::string, UIStyle::LayoutAlignType> _alignType
{
{ "top", UIStyle::ALIGN_TOP },
{ "center", UIStyle::ALIGN_CENTER },
{ "bottom", UIStyle::ALIGN_BOTTOM }
};
_RimeParseStringOpt(config, "style/layout/align_type", style.align_type, _alignType);
_RimeParseStringOptWithFallback(config, "style/layout/align_type", style.align_type, _alignType, style.align_type);
_RimeGetBool(config, "style/display_tray_icon", initialize, style.display_tray_icon, true, false);
_RimeGetBool(config, "style/ascii_tip_follow_cursor", initialize, style.ascii_tip_follow_cursor, true, false);
_RimeGetBool(config, "style/horizontal", initialize, style.layout_type, UIStyle::LAYOUT_HORIZONTAL, UIStyle::LAYOUT_VERTICAL);
Expand All @@ -1219,7 +1231,7 @@ static void _UpdateUIStyle(RimeConfig* config, weasel::UI* ui, bool initialize)
{ std::string{ "vertical" }, true}
};
bool _text_orientation_bool{ false };
_RimeParseStringOpt(config, "style/text_orientation", _text_orientation_bool, _text_orientation);
_RimeParseStringOptWithFallback(config, "style/text_orientation", _text_orientation_bool, _text_orientation, _text_orientation_bool);
if (_text_orientation_bool)
{
style.layout_type = UIStyle::LAYOUT_VERTICAL_TEXT;
Expand All @@ -1240,7 +1252,7 @@ static void _UpdateUIStyle(RimeConfig* config, weasel::UI* ui, bool initialize)
{ "vertical+fullscreen", UIStyle::LAYOUT_VERTICAL_FULLSCREEN},
{ "horizontal+fullscreen", UIStyle::LAYOUT_HORIZONTAL_FULLSCREEN}
};
_RimeParseStringOpt(config, "style/layout/type", style.layout_type, _layoutMap);
_RimeParseStringOptWithFallback(config, "style/layout/type", style.layout_type, _layoutMap, style.layout_type);
// disable max_width when full screen
if (style.layout_type == UIStyle::LAYOUT_HORIZONTAL_FULLSCREEN || style.layout_type == UIStyle::LAYOUT_VERTICAL_FULLSCREEN)
{
Expand Down Expand Up @@ -1277,7 +1289,8 @@ static void _UpdateUIStyle(RimeConfig* config, weasel::UI* ui, bool initialize)
style.candidate_spacing = max(style.candidate_spacing, style.hilite_padding_x * 2); // horizontal, if hilite_padding_x over candidate spacing, increase candidate spacing
}
// hilite_padding_x vs hilite_spacing
style.hilite_spacing = max(style.hilite_spacing, style.hilite_padding_x);
if (!style.inline_preedit)
style.hilite_spacing = max(style.hilite_spacing, style.hilite_padding_x);
}
else // LAYOUT_VERTICAL_TEXT
{
Expand All @@ -1289,7 +1302,8 @@ static void _UpdateUIStyle(RimeConfig* config, weasel::UI* ui, bool initialize)
if (style.vertical_text_with_wrap)
style.candidate_spacing = max(style.candidate_spacing, style.hilite_padding_y * 2);
// hilite_padding_y vs hilite_spacing
style.hilite_spacing = max(style.hilite_spacing, style.hilite_padding_y);
if (!style.inline_preedit)
style.hilite_spacing = max(style.hilite_spacing, style.hilite_padding_y);
}
// fix padding and margin settings
int scale = style.margin_x < 0 ? -1 : 1;
Expand All @@ -1299,6 +1313,8 @@ static void _UpdateUIStyle(RimeConfig* config, weasel::UI* ui, bool initialize)
// get enhanced_position
_RimeGetBool(config, "style/enhanced_position", initialize, style.enhanced_position, true, false);
// get color scheme
const int BUF_SIZE{ 255 };
char buffer[BUF_SIZE + 1]{};
if (initialize && RimeConfigGetString(config, "style/color_scheme", buffer, BUF_SIZE))
{
_UpdateUIStyleColor(config, style);
Expand All @@ -1309,8 +1325,7 @@ static void _UpdateUIStyle(RimeConfig* config, weasel::UI* ui, bool initialize)
static bool _UpdateUIStyleColor(RimeConfig* config, UIStyle& style, std::string color)
{
const int BUF_SIZE = 255;
char buffer[BUF_SIZE + 1];
memset(buffer, '\0', sizeof(buffer));
char buffer[BUF_SIZE + 1]{};
std::string color_mark = "style/color_scheme";
// color scheme
if (RimeConfigGetString(config, color_mark.c_str(), buffer, BUF_SIZE) || !color.empty())
Expand All @@ -1324,7 +1339,7 @@ static bool _UpdateUIStyleColor(RimeConfig* config, UIStyle& style, std::string
{std::string("rgba"), COLOR_RGBA},
{std::string("abgr"), COLOR_ABGR}
};
_RimeParseStringOpt(config, (prefix + "/color_format"), fmt, _colorFmt);
_RimeParseStringOptWithFallback(config, (prefix + "/color_format"), fmt, _colorFmt, COLOR_ABGR);
_RimeConfigGetColor32bWithFallback(config, (prefix + "/back_color"), style.back_color, fmt, 0xffffffff);
_RimeConfigGetColor32bWithFallback(config, (prefix + "/shadow_color"), style.shadow_color, fmt, TRANSPARENT_COLOR);
_RimeConfigGetColor32bWithFallback(config, (prefix + "/prevpage_color"), style.prevpage_color, fmt, TRANSPARENT_COLOR);
Expand Down Expand Up @@ -1390,6 +1405,7 @@ void RimeWithWeaselHandler::_GetStatus(weasel::Status& stat, RimeSessionId sessi
stat.prediction = !!status.is_predictable;
if (schema_id != m_last_schema_id)
{
m_session_status_map[session_id].__synced = false;
m_last_schema_id = schema_id;
if (schema_id != ".default") { // don't load for schema select menu
bool inline_preedit = m_session_status_map[session_id].style.inline_preedit;
Expand All @@ -1398,6 +1414,7 @@ void RimeWithWeaselHandler::_GetStatus(weasel::Status& stat, RimeSessionId sessi
if (m_session_status_map[session_id].style.inline_preedit != inline_preedit)
_UpdateInlinePreeditStatus(session_id); // in case of inline_preedit set in schema
_RefreshTrayIcon(session_id, _UpdateUICallback); // refresh icon after schema changed
m_ui->style() = m_session_status_map[session_id].style;
if (m_show_notifications.find("schema") != m_show_notifications.end() && m_show_notifications_time > 0)
{
ctx.aux.str = stat.schema_name;
Expand Down
6 changes: 6 additions & 0 deletions WeaselTSF/CandidateList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module;
#include <WeaselUI.h>
#include <WeaselCommon.h>
#include "Globals.h"
#include "test.h"
module CandidateList;
import WeaselUtility;
using namespace weasel;
Expand Down Expand Up @@ -402,6 +403,11 @@ void CCandidateList::StartUI()
}
_ui.style() = _style;
_MakeUIWindow();
#ifdef TEST
std::wstring buffer{ std::format(L"From CCandidateList::StartUI. horizontal = {}\n", (int)_style.layout_type) };
_tsf.WriteConsole(buffer);
#endif // TEST

}
else
{
Expand Down
8 changes: 8 additions & 0 deletions WeaselTSF/LanguageBarTSF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module;
#include "Globals.h"
#include "ctffunc.h"
#include <format>
#include <thread>
#include <shellapi.h>
#include "resource.h"
module WeaselTSF;
Expand Down Expand Up @@ -71,6 +72,13 @@ void WeaselTSF::_HandleLangBarMenuSelect(UINT wID)
_UninitPreservedKey();
break;

case ID_WEASELTRAY_RERUN_SERVICE:
{
std::wstring rootPath{ WeaselRootPath() };
ShellExecuteW(nullptr, L"open", std::format(LR"({}\start_service.bat)", rootPath).data(), NULL, rootPath.data(), SW_HIDE);
}
break;

DEFAULT:
default:
m_client.TrayCommand(wID);
Expand Down
2 changes: 1 addition & 1 deletion WeaselTSF/WeaselTSF.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,6 @@ export
L"WoW.exe"
};

HANDLE m_hConsole;
HANDLE m_hConsole{};
};
}
Binary file modified WeaselTSF/WeaselTSF.rc
Binary file not shown.
3 changes: 2 additions & 1 deletion WeaselTSF/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@
#define ID_STYLE_CARET_FOLLOWING 40015
#define ID_STYLE_PRESERVED_KEY_SWITCH 40016
#define ID_WEASELTRAY_VIEW_LOG 40017
#define ID_WEASELTRAY_RERUN_SERVICE 40018

// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 104
#define _APS_NEXT_COMMAND_VALUE 40018
#define _APS_NEXT_COMMAND_VALUE 40019
#define _APS_NEXT_CONTROL_VALUE 1001
#define _APS_NEXT_SYMED_VALUE 101
#endif
Expand Down
10 changes: 10 additions & 0 deletions WeaselUI/WeaselUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,16 @@ void UI::Update(const Context &ctx, const Status &status)
{
m_data->ctx = ctx;
m_data->status = status;
if (m_data->style.candidate_abbreviate_length > 0)
{
for (auto& c : m_data->ctx.cinfo.candies)
{
if (c.str.length() > m_data->style.candidate_abbreviate_length)
{
c.str = std::format(L"{}...{}", c.str.substr(0, m_data->style.candidate_abbreviate_length - 1), c.str.substr(c.str.length() - 1));
}
}
}
Refresh();
}

Expand Down
5 changes: 5 additions & 0 deletions include/WeaselCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,8 @@ namespace weasel
int font_point;
int label_font_point;
int comment_font_point;
int candidate_abbreviate_length;

bool inline_preedit;
bool display_tray_icon;
bool ascii_tip_follow_cursor;
Expand Down Expand Up @@ -359,6 +361,7 @@ namespace weasel
UIStyle() : font_face(),
label_font_face(),
comment_font_face(),
candidate_abbreviate_length{},
mouse_hover_ms(0),
font_point(0),
label_font_point(0),
Expand Down Expand Up @@ -441,6 +444,7 @@ namespace weasel
|| font_point != st.font_point
|| label_font_point != st.label_font_point
|| comment_font_point != st.comment_font_point
|| candidate_abbreviate_length != st.candidate_abbreviate_length
|| inline_preedit != st.inline_preedit
|| mark_text != st.mark_text
|| display_tray_icon != st.display_tray_icon
Expand Down Expand Up @@ -508,6 +512,7 @@ namespace boost {
ar& s.font_point;
ar& s.label_font_point;
ar& s.comment_font_point;
ar& s.candidate_abbreviate_length;
ar& s.inline_preedit;
ar& s.align_type;
ar& s.antialias_mode;
Expand Down
7 changes: 7 additions & 0 deletions output/data/weasel.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ app_options:
conhost.exe:
ascii_mode: true

show_notifications: true
# how long a period notifications shows
show_notifications_time: 1200
# use global ascii status
global_ascii: false

style:
antialias_mode: default
color_scheme: dark_temple
Expand All @@ -19,6 +25,7 @@ style:
font_point: 14
label_font_point: 14
comment_font_point: 14
candidate_abbreviate_length: 0
click_to_capture: false
horizontal: true
fullscreen: false
Expand Down
Loading

0 comments on commit f0c0391

Please sign in to comment.