Skip to content

Commit 94aff6d

Browse files
authored
Fix display leftover (#74)
1 parent bbe6662 commit 94aff6d

File tree

9 files changed

+33
-16
lines changed

9 files changed

+33
-16
lines changed

include/webview_candidate_window.hpp

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#else
99
#include "webview.h"
1010
#endif
11+
#include <cstdint>
1112
#include <iostream>
1213
#include <nlohmann/json.hpp>
1314
#include <sstream>
@@ -67,6 +68,8 @@ class WebviewCandidateWindow : public CandidateWindow {
6768
int accent_color_ = 0;
6869
layout_t layout_ = layout_t::horizontal;
6970
writing_mode_t writing_mode_ = writing_mode_t::horizontal_tb;
71+
uint32_t epoch = 0; // A timestamp for async results from
72+
// webview
7073

7174
private:
7275
/* Platform-specific interfaces (implemented in 'platform') */

page/global.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ declare global {
3434
_scroll: (start: number, length: number) => void
3535
_askActions: (index: number) => void
3636
_action: (index: number, id: number) => void
37-
_resize: (dx: number, dy: number, anchorTop: number, anchorRight: number, anchorBottom: number, anchorLeft: number, panelTop: number, panelRight: number, panelBottom: number, panelLeft: number, panelRadius: number, borderWidth: number, fullWidth: number, fullHeight: number, dragging: boolean) => void
37+
_resize: (epoch: number, dx: number, dy: number, anchorTop: number, anchorRight: number, anchorBottom: number, anchorLeft: number, panelTop: number, panelRight: number, panelBottom: number, panelLeft: number, panelRadius: number, borderWidth: number, fullWidth: number, fullHeight: number, dragging: boolean) => void
3838

3939
// JavaScript APIs that webview_candidate_window.mm calls
4040
setCandidates: (cands: Candidate[], highlighted: number, markText: string, pageable: boolean, hasPrev: boolean, hasNext: boolean, scrollState: SCROLL_STATE, scrollStart: boolean, scrollEnd: boolean) => void
4141
setLayout: (layout: 0 | 1) => void
4242
updateInputPanel: (preeditHTML: string, auxUpHTML: string, auxDownHTML: string) => void
43-
resize: (dx: number, dy: number, dragging: boolean, hasContextmenu: boolean) => void
43+
resize: (new_epoch: number, dx: number, dy: number, dragging: boolean, hasContextmenu: boolean) => void
4444
setTheme: (theme: 0 | 1 | 2) => void
4545
setAccentColor: (color: number | null) => void
4646
setStyle: (style: string) => void

page/ux.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212

1313
const DRAG_THRESHOLD = 10
1414

15+
let epoch = 0
1516
let pressed = false
1617
let dragging = false
1718
let startX = 0
@@ -37,11 +38,13 @@ interface ShadowBox {
3738
}
3839

3940
export function resize(
41+
new_epoch: number,
4042
dx: number,
4143
dy: number,
4244
dragging: boolean,
4345
hasContextmenu: boolean,
4446
) {
47+
epoch = new_epoch
4548
function adaptWindowSize(reserveSpaceForContextmenu: boolean) {
4649
let {
4750
anchorTop,
@@ -85,7 +88,7 @@ export function resize(
8588
const { borderRadius, borderWidth } = getComputedStyle(panel)
8689
const bWidth = Math.max(...borderWidth.split(' ').map(Number.parseFloat))
8790
const pRadius = Math.max(...borderRadius.split(' ').map(Number.parseFloat))
88-
window.fcitx._resize(dx, dy, anchorTop, anchorRight, anchorBottom, anchorLeft, pRect.top, pRect.right, pRect.bottom, pRect.left, pRadius, bWidth, right, bottom, dragging)
91+
window.fcitx._resize(epoch, dx, dy, anchorTop, anchorRight, anchorBottom, anchorLeft, pRect.top, pRect.right, pRect.bottom, pRect.left, pRadius, bWidth, right, bottom, dragging)
8992
}
9093
adaptWindowSize(hasContextmenu)
9194
if (!dragging) {
@@ -165,7 +168,7 @@ export function showContextmenu(x: number, y: number, index: number, actions: Ca
165168
contextmenu.style.top = `${y}px`
166169
contextmenu.style.left = `${x}px`
167170
contextmenu.style.display = 'block'
168-
resize(0, 0, false, true)
171+
resize(epoch, 0, 0, false, true)
169172
}
170173

171174
export function hideContextmenu() {
@@ -207,7 +210,7 @@ receiver.addEventListener('mousemove', (e) => {
207210
dX += dx
208211
dY += dy
209212
dragOffset = Math.max(dragOffset, dX * dX + dY * dY)
210-
resize(dx, dy, true, false)
213+
resize(epoch, dx, dy, true, false)
211214
})
212215

213216
receiver.addEventListener('mouseup', (e) => {

src/platform/js.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ void WebviewCandidateWindow::set_transparent_background() {}
1818

1919
void WebviewCandidateWindow::update_accent_color() {}
2020

21-
void WebviewCandidateWindow::hide() { EM_ASM(fcitx.hidePanel()); }
21+
void WebviewCandidateWindow::hide() {
22+
EM_ASM(fcitx.hidePanel());
23+
epoch += 1;
24+
}
2225

2326
void WebviewCandidateWindow::write_clipboard(const std::string &html) {}
2427

src/platform/linux.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void WebviewCandidateWindow::set_transparent_background() {
3737

3838
void WebviewCandidateWindow::update_accent_color() {}
3939

40-
void WebviewCandidateWindow::hide() {}
40+
void WebviewCandidateWindow::hide() { epoch += 1; }
4141

4242
void WebviewCandidateWindow::write_clipboard(const std::string &html) {}
4343

src/platform/macos.mm

+1
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ NSRect getNearestScreenFrame(double x, double y) {
217217
[window orderBack:nil];
218218
[window setIsVisible:NO];
219219
hidden_ = true;
220+
epoch += 1;
220221
}
221222

222223
void WebviewCandidateWindow::write_clipboard(const std::string &html) {

src/webview_candidate_window.cpp

+13-6
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,17 @@ WebviewCandidateWindow::WebviewCandidateWindow()
4444
update_accent_color();
4545

4646
bind("_resize",
47-
[this](double dx, double dy, double anchor_top, double anchor_right,
48-
double anchor_bottom, double anchor_left, double panel_top,
49-
double panel_right, double panel_bottom, double panel_left,
50-
double panel_radius, double border_width, double width,
51-
double height, bool dragging) {
47+
[this](uint32_t result_epoch, double dx, double dy, double anchor_top,
48+
double anchor_right, double anchor_bottom, double anchor_left,
49+
double panel_top, double panel_right, double panel_bottom,
50+
double panel_left, double panel_radius, double border_width,
51+
double width, double height, bool dragging) {
52+
// Drop results from previous epochs. This can happen
53+
// because JS code runs in another thread and can be slow
54+
// sometimes.
55+
// NOTE: accept result_epoch=0 because of wrapping.
56+
if (result_epoch != 0 && result_epoch < epoch)
57+
return;
5258
resize(dx, dy, anchor_top, anchor_right, anchor_bottom,
5359
anchor_left, panel_top, panel_right, panel_bottom,
5460
panel_left, panel_radius, border_width, width, height,
@@ -144,7 +150,8 @@ void WebviewCandidateWindow::show(double x, double y) {
144150
// warmed-up yet, and it won't be updated until user changes color.
145151
set_accent_color();
146152
}
147-
invoke_js("resize", 0., 0., false);
153+
epoch += 1;
154+
invoke_js("resize", epoch, 0., 0., false);
148155
}
149156

150157
static void build_html_open_tags(std::stringstream &ss, int flags) {

tests/global.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
declare global {
22
type CppCall = {
3-
resize: [number, number, number, number, number, number, number, number, number, number, number, number, number, number, boolean]
3+
resize: [number, number, number, number, number, number, number, number, number, number, number, number, number, number, number, boolean]
44
} | {
55
select: number
66
} | {

tests/util.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ export async function init(page: Page) {
1010
await page.evaluate(() => {
1111
window.fcitx.setTheme(2)
1212
window.cppCalls = []
13-
window.fcitx._resize = (dx: number, dy: number, anchorTop: number, anchorRight: number, anchorBottom: number, anchorLeft: number, panelTop: number, panelRight: number, panelBottom: number, panelLeft: number, panelRadius: number, borderWidth: number, fullWidth: number, fullHeight: number, dragging: boolean) => {
13+
window.fcitx._resize = (epoch: number, dx: number, dy: number, anchorTop: number, anchorRight: number, anchorBottom: number, anchorLeft: number, panelTop: number, panelRight: number, panelBottom: number, panelLeft: number, panelRadius: number, borderWidth: number, fullWidth: number, fullHeight: number, dragging: boolean) => {
1414
window.cppCalls.push({
15-
resize: [dx, dy, anchorTop, anchorRight, anchorBottom, anchorLeft, panelTop, panelRight, panelBottom, panelLeft, panelRadius, borderWidth, fullWidth, fullHeight, dragging],
15+
resize: [epoch, dx, dy, anchorTop, anchorRight, anchorBottom, anchorLeft, panelTop, panelRight, panelBottom, panelLeft, panelRadius, borderWidth, fullWidth, fullHeight, dragging],
1616
})
1717
}
1818
window.fcitx._select = (index: number) => {

0 commit comments

Comments
 (0)