Skip to content

Commit 9357f45

Browse files
authored
obey new thread rule of webview to fix layout shift for Esc on Rime F4 (#277)
1 parent e7ec141 commit 9357f45

File tree

4 files changed

+47
-41
lines changed

4 files changed

+47
-41
lines changed

src/fcitx-public.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
#ifndef FCITX5_MACOS_FCITX_H
2-
#define FCITX5_MACOS_FCITX_H
1+
#pragma once
32

4-
#include <cstdint>
53
#include <string>
64

75
void start_fcitx_thread(const char *locale) noexcept;
86
void stop_fcitx_thread() noexcept;
9-
void restart_fcitx_thread() noexcept;
107

118
// NOTE: It's impossible to use std::vector<std::string> directly
129
// until Swift fixes C++ interop.
@@ -43,5 +40,3 @@ void activateActionById(int id) noexcept;
4340

4441
// Tunnel variables
4542
#include "tunnel.h"
46-
47-
#endif

src/fcitx.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,6 @@ void stop_fcitx_thread() noexcept {
234234
fcitx_thread_started = false;
235235
}
236236

237-
void restart_fcitx_thread() noexcept {
238-
stop_fcitx_thread();
239-
start_fcitx_thread(current_locale.c_str());
240-
}
241-
242237
std::string imGetGroupNames() noexcept {
243238
return with_fcitx([](Fcitx &fcitx) {
244239
nlohmann::json j;

webpanel/webpanel.cpp

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ WebPanel::WebPanel(Instance *instance)
9999
actionableList->candidateActions(candidate)) {
100100
actions.push_back({action.id(), action.text()});
101101
}
102-
window_->answer_actions(actions);
102+
dispatch_async(dispatch_get_main_queue(), ^{
103+
window_->answer_actions(actions);
104+
});
103105
}
104106
} catch (const std::invalid_argument &e) {
105107
FCITX_ERROR() << "action candidate index out of range";
@@ -151,7 +153,9 @@ WebPanel::WebPanel(Instance *instance)
151153
if (keyEvent.isRelease()) {
152154
return;
153155
}
154-
window_->copy_html();
156+
dispatch_async(dispatch_get_main_queue(), ^{
157+
window_->copy_html();
158+
});
155159
return keyEvent.filterAndAccept();
156160
}
157161
if (scrollState_ == candidate_window::scroll_state_t::ready &&
@@ -209,7 +213,9 @@ WebPanel::WebPanel(Instance *instance)
209213
if (keyEvent.isRelease()) {
210214
return;
211215
}
212-
window_->scroll_key_action(selectActions[keyIndex]);
216+
dispatch_async(dispatch_get_main_queue(), ^{
217+
window_->scroll_key_action(selectActions[keyIndex]);
218+
});
213219
return keyEvent.filterAndAccept();
214220
}
215221
const std::vector<std::pair<
@@ -237,7 +243,10 @@ WebPanel::WebPanel(Instance *instance)
237243
for (const auto &pair : actionMap) {
238244
if (key.checkKeyList(*pair.first)) {
239245
if (!keyEvent.isRelease()) {
240-
window_->scroll_key_action(pair.second);
246+
auto captured = pair.second;
247+
dispatch_async(dispatch_get_main_queue(), ^{
248+
window_->scroll_key_action(captured);
249+
});
241250
}
242251
// Must not send release event to engine, which resets
243252
// scroll mode.
@@ -250,8 +259,10 @@ WebPanel::WebPanel(Instance *instance)
250259
}
251260
// Instead of directly calling collapse, let webview handle
252261
// animation and call it.
253-
window_->scroll_key_action(
254-
candidate_window::scroll_key_action_t::collapse);
262+
dispatch_async(dispatch_get_main_queue(), ^{
263+
window_->scroll_key_action(
264+
candidate_window::scroll_key_action_t::collapse);
265+
});
255266
return keyEvent.filterAndAccept();
256267
}
257268
// Karabiner-Elements defines Hyper as Ctrl+Alt+Shift+Cmd, but
@@ -282,28 +293,30 @@ WebPanel::WebPanel(Instance *instance)
282293
}
283294

284295
void WebPanel::updateConfig() {
285-
window_->set_layout(config_.typography->layout.value());
286-
window_->set_theme(config_.basic->theme.value());
287-
window_->set_caret_text(config_.caret->style.value() == CaretStyle::Text
288-
? config_.caret->text.value()
289-
: "");
290-
window_->set_highlight_mark_text(config_.highlight->markStyle.value() ==
291-
HighlightMarkStyle::Text
292-
? config_.highlight->markText.value()
293-
: "");
294-
window_->set_native_blur(config_.background->blur.value());
295-
// Keep CSS shadow as native may leave a ghost shadow of last frame when
296-
// typing fast.
297-
// window_->set_native_shadow(config_.background->shadow.value());
298-
auto style = configValueToJson(config_).dump();
299-
window_->set_style(style.c_str());
300-
window_->unload_plugins();
301-
using namespace candidate_window;
302-
uint64_t apis = (config_.advanced->unsafeAPI->curl.value() ? kCurl : 0);
303-
window_->set_api(apis);
304-
if (*config_.advanced->pluginNotice) {
305-
window_->load_plugins({*config_.advanced->plugins});
306-
}
296+
dispatch_async(dispatch_get_main_queue(), ^{
297+
window_->set_layout(config_.typography->layout.value());
298+
window_->set_theme(config_.basic->theme.value());
299+
window_->set_caret_text(config_.caret->style.value() == CaretStyle::Text
300+
? config_.caret->text.value()
301+
: "");
302+
window_->set_highlight_mark_text(config_.highlight->markStyle.value() ==
303+
HighlightMarkStyle::Text
304+
? config_.highlight->markText.value()
305+
: "");
306+
window_->set_native_blur(config_.background->blur.value());
307+
// Keep CSS shadow as native may leave a ghost shadow of last frame when
308+
// typing fast.
309+
// window_->set_native_shadow(config_.background->shadow.value());
310+
auto style = configValueToJson(config_).dump();
311+
window_->set_style(style.c_str());
312+
window_->unload_plugins();
313+
using namespace candidate_window;
314+
uint64_t apis = (config_.advanced->unsafeAPI->curl.value() ? kCurl : 0);
315+
window_->set_api(apis);
316+
if (*config_.advanced->pluginNotice) {
317+
window_->load_plugins({*config_.advanced->plugins});
318+
}
319+
});
307320
}
308321

309322
void WebPanel::reloadConfig() {
@@ -578,7 +591,10 @@ void WebPanel::collapse() {
578591
}
579592

580593
void WebPanel::applyAppAccentColor(const std::string &accentColor) {
581-
window_->apply_app_accent_color(accentColor);
594+
auto captured = accentColor;
595+
dispatch_async(dispatch_get_main_queue(), ^{
596+
window_->apply_app_accent_color(captured);
597+
});
582598
}
583599

584600
} // namespace fcitx

0 commit comments

Comments
 (0)