Skip to content

Commit ede973c

Browse files
authored
auto collapse if no preedit (#45)
1 parent 36ff5f1 commit ede973c

File tree

6 files changed

+47
-3
lines changed

6 files changed

+47
-3
lines changed

src/api.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export type SystemEvent = {
5151
scrollState: ScrollState
5252
scrollStart: boolean
5353
scrollEnd: boolean
54+
hasClientPreedit: boolean
5455
}
5556
} | {
5657
type: 'CANDIDATE_ACTIONS'

src/candidates.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ const textEncoder = new TextEncoder()
3636
const textDecoder = new TextDecoder()
3737
let timer: number | null = null
3838
let show = false
39+
let hasPanelPreedit = false
3940

4041
export function setPreedit(auxUp: string, preedit: string, caret: number) {
4142
if (timer) {
4243
clearInterval(timer)
4344
timer = null
4445
}
46+
hasPanelPreedit = !!preedit
4547
const container = getCandidateBar().querySelector('.fcitx-keyboard-candidates-container')!
4648
let element = container.querySelector('.fcitx-keyboard-preedit')
4749
if (auxUp || preedit) {
@@ -87,7 +89,7 @@ export function setPreedit(auxUp: string, preedit: string, caret: number) {
8789
setDisplayMode('candidates')
8890
}
8991

90-
export function setCandidates(cands: Candidate[], highlighted: number, scrollState: ScrollState, scrollStart: boolean, scrollEnd: boolean) {
92+
export function setCandidates(cands: Candidate[], highlighted: number, scrollState: ScrollState, scrollStart: boolean, scrollEnd: boolean, hasClientPreedit: boolean) {
9193
scrollState_ = scrollState
9294
touchId = null
9395
longPressId = null
@@ -139,6 +141,9 @@ export function setCandidates(cands: Candidate[], highlighted: number, scrollSta
139141
container.appendChild(candidate)
140142
}
141143
setPagingButtons(container)
144+
if (!hasPanelPreedit && !hasClientPreedit) {
145+
collapse()
146+
}
142147
setDisplayMode('candidates')
143148
}
144149

src/keyboard.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export function onMessage(message: string) {
117117
setPreedit(event.data.auxUp, event.data.preedit, event.data.caret)
118118
break
119119
case 'CANDIDATES':
120-
setCandidates(event.data.candidates, event.data.highlighted, event.data.scrollState, event.data.scrollStart, event.data.scrollEnd)
120+
setCandidates(event.data.candidates, event.data.highlighted, event.data.scrollState, event.data.scrollStart, event.data.scrollEnd, event.data.hasClientPreedit)
121121
break
122122
case 'CANDIDATE_ACTIONS':
123123
setCandidateActions(event.data.index, event.data.actions)

tests/test-candidates.spec.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ test('Show and clear', async ({ page }) => {
3030
scrollState: SCROLL_NONE,
3131
scrollStart: false,
3232
scrollEnd: false,
33+
hasClientPreedit: true,
3334
} })
3435
await expect(candidateBar.locator('.fcitx-keyboard-candidate')).toHaveCount(2)
3536
await expect(toolbar).not.toBeVisible()
@@ -52,6 +53,7 @@ test('Select', async ({ page }) => {
5253
scrollState: SCROLL_NONE,
5354
scrollStart: false,
5455
scrollEnd: false,
56+
hasClientPreedit: true,
5557
} })
5658

5759
const firstCandidate = candidateBar.locator('.fcitx-keyboard-candidate').first()
@@ -71,6 +73,7 @@ test('Overflow', async ({ page }) => {
7173
scrollState: SCROLL_NONE,
7274
scrollStart: false,
7375
scrollEnd: false,
76+
hasClientPreedit: true,
7477
} }
7578
const candidate = getCandidateBar(page).locator('.fcitx-keyboard-candidate')
7679
await sendSystemEvent(page, event)
@@ -95,6 +98,7 @@ test('Actions', async ({ page }) => {
9598
scrollState: SCROLL_NONE,
9699
scrollStart: false,
97100
scrollEnd: false,
101+
hasClientPreedit: true,
98102
} })
99103
const candidate = page.locator('.fcitx-keyboard-candidate')
100104
await longPress(candidate)
@@ -121,6 +125,7 @@ test('Actions disappear on clear', async ({ page }) => {
121125
scrollState: SCROLL_NONE,
122126
scrollStart: false,
123127
scrollEnd: false,
128+
hasClientPreedit: true,
124129
} })
125130
await sendSystemEvent(page, { type: 'CANDIDATE_ACTIONS', data: {
126131
index: 0,
@@ -153,6 +158,7 @@ test('Preedit', async ({ page }) => {
153158
scrollState: SCROLL_NONE,
154159
scrollStart: false,
155160
scrollEnd: false,
161+
hasClientPreedit: true,
156162
} })
157163
const newBox = (await preedit.boundingBox())!
158164
expect(newBox, 'No layout shift').toEqual(box)
@@ -213,6 +219,7 @@ test('Horizontal scroll', async ({ page }) => {
213219
scrollState: SCROLLING,
214220
scrollStart: true,
215221
scrollEnd: false,
222+
hasClientPreedit: true,
216223
} })
217224
const candidates = page.locator('.fcitx-keyboard-candidates')
218225
await candidates.evaluate(element => element.scrollBy(element.lastElementChild!.getBoundingClientRect().right, 0))
@@ -230,6 +237,7 @@ test('Horizontal scroll', async ({ page }) => {
230237
scrollState: SCROLLING,
231238
scrollStart: false,
232239
scrollEnd: true,
240+
hasClientPreedit: true,
233241
} })
234242
const candidate = candidates.locator('.fcitx-keyboard-candidate')
235243
await expect(candidate).toHaveCount(30)
@@ -258,6 +266,7 @@ test('Expand/collapse', async ({ page }) => {
258266
scrollState: SCROLLING,
259267
scrollStart: true,
260268
scrollEnd: false,
269+
hasClientPreedit: true,
261270
} })
262271
const c29 = page.getByText('词29')
263272
await expect(c29).not.toBeInViewport()
@@ -277,6 +286,32 @@ test('Expand/collapse', async ({ page }) => {
277286
await expect(c29).not.toBeInViewport()
278287
})
279288

289+
test('Auto collapse if no preedit', async ({ page }) => {
290+
await init(page)
291+
const container = page.locator('.fcitx-keyboard-container')
292+
293+
function setCandidates(hasClientPreedit: boolean) {
294+
return sendSystemEvent(page, { type: 'CANDIDATES', data: {
295+
candidates: generateCandidates(0, 10),
296+
highlighted: 0,
297+
scrollState: SCROLLING,
298+
scrollStart: true,
299+
scrollEnd: false,
300+
hasClientPreedit,
301+
} })
302+
}
303+
304+
await setCandidates(true)
305+
await expandOrCollapse(page)
306+
await expect(container).toContainClass('fcitx-keyboard-expanded')
307+
308+
await setCandidates(true)
309+
await expect(container).toContainClass('fcitx-keyboard-expanded')
310+
311+
await setCandidates(false)
312+
await expect(container).not.toContainClass('fcitx-keyboard-expanded')
313+
})
314+
280315
test('Vertical scroll', async ({ page }) => {
281316
await init(page)
282317

@@ -286,6 +321,7 @@ test('Vertical scroll', async ({ page }) => {
286321
scrollState: SCROLLING,
287322
scrollStart: true,
288323
scrollEnd: false,
324+
hasClientPreedit: true,
289325
} })
290326
await expandOrCollapse(page)
291327

@@ -308,6 +344,7 @@ test('Paging button', async ({ page }) => {
308344
scrollState: SCROLLING,
309345
scrollStart: true,
310346
scrollEnd: true,
347+
hasClientPreedit: true,
311348
} })
312349
await expandOrCollapse(page)
313350
const top = (await page.getByText('词0').boundingBox())!.y

tests/test-space.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ test('Continuous slide shouldn\'t be interrupted by setCandidates', async ({ pag
9898
text: '一',
9999
label: '1',
100100
comment: '',
101-
}], scrollState: SCROLL_NONE, scrollStart: false, scrollEnd: false } })
101+
}], scrollState: SCROLL_NONE, scrollStart: false, scrollEnd: false, hasClientPreedit: true } })
102102
await expect(space).toHaveCSS('background-color', GRAY)
103103

104104
await touchUp(space, touchId)

tests/test-symbol-selector.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ async function renderCandidateAndClickSymbol(page: Page) {
7070
scrollState: SCROLL_NONE,
7171
scrollStart: false,
7272
scrollEnd: false,
73+
hasClientPreedit: true,
7374
} })
7475
const candidate = page.locator('.fcitx-keyboard-candidate')
7576
await expect(candidate).toBeVisible()

0 commit comments

Comments
 (0)