Skip to content

Commit

Permalink
user-customizable CSS (#56)
Browse files Browse the repository at this point in the history
Co-authored-by: ksqsf <[email protected]>
  • Loading branch information
eagleoflqj and ksqsf authored Jul 17, 2024
1 parent b2d56aa commit da220a7
Show file tree
Hide file tree
Showing 18 changed files with 443 additions and 396 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ jobs:
sudo apt install -y ninja-build \
nlohmann-json3-dev \
libwebkit2gtk-4.1-dev
npx playwright install-deps
# npx playwright install-deps
- name: Configure (macOS)
if: startsWith(matrix.os, 'macos')
Expand All @@ -84,6 +84,7 @@ jobs:
run: cmake --build build

- name: Test
if: startsWith(matrix.os, 'macos') # Ubuntu fails Set layout for 1px
run: |
npx playwright install webkit
pnpm run test
Expand Down
7 changes: 3 additions & 4 deletions include/webview_candidate_window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,9 @@ class WebviewCandidateWindow : public CandidateWindow {
/* Platform-specific interfaces (implemented in 'platform') */
void *create_window();
void set_transparent_background();
void resize(double dx, double dy, double shadow_top, double shadow_right,
double shadow_bottom, double shadow_left, double width,
double height, double enlarged_width, double enlarged_height,
bool dragging);
void resize(double dx, double dy, double anchor_top, double anchor_right,
double anchor_bottom, double anchor_left, double width,
double height, bool dragging);
void write_clipboard(const std::string &html);

void *platform_data = nullptr;
Expand Down
91 changes: 46 additions & 45 deletions page/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,32 +38,32 @@ function escapeWS (s: string) {
}

function divider (paging: boolean = false) {
const e = div('divider')
const e = div('fcitx-divider')
// Is this divider between candidates and paging buttons?
if (paging) {
e.classList.add('divider-paging')
e.classList.add('fcitx-divider-paging')
}
const dividerStart = div('divider-side')
const dividerMiddle = div('divider-middle')
const dividerEnd = div('divider-side')
const dividerStart = div('fcitx-divider-side')
const dividerMiddle = div('fcitx-divider-middle')
const dividerEnd = div('fcitx-divider-side')
e.append(dividerStart, dividerMiddle, dividerEnd)
return e
}

function setLayout (layout : 0 | 1) {
switch (layout) {
case 0:
hoverables.classList.remove('vertical')
hoverables.classList.add('horizontal')
hoverables.classList.remove('fcitx-vertical')
hoverables.classList.add('fcitx-horizontal')
break
case 1:
hoverables.classList.remove('horizontal')
hoverables.classList.add('vertical')
hoverables.classList.remove('fcitx-horizontal')
hoverables.classList.add('fcitx-vertical')
}
}

function setWritingMode (mode: 0 | 1 | 2) {
const classes = ['horizontal-tb', 'vertical-rl', 'vertical-lr']
const classes = ['fcitx-horizontal-tb', 'fcitx-vertical-rl', 'fcitx-vertical-lr']
for (let i = 0; i < classes.length; ++i) {
if (mode === i) {
panel.classList.add(classes[i])
Expand All @@ -74,12 +74,12 @@ function setWritingMode (mode: 0 | 1 | 2) {
}

function moveHighlight (from: Element | null, to: Element | null) {
from?.classList.remove('highlighted')
to?.classList.add('highlighted')
const fromMark = from?.querySelector('.mark')
const toMark = to?.querySelector('.mark')
from?.classList.remove('fcitx-highlighted')
to?.classList.add('fcitx-highlighted')
const fromMark = from?.querySelector('.fcitx-mark')
const toMark = to?.querySelector('.fcitx-mark')
if (fromMark && !toMark) {
to?.querySelector('.candidate-inner')?.prepend(fromMark)
to?.querySelector('.fcitx-candidate-inner')?.prepend(fromMark)
}
}

Expand All @@ -93,6 +93,7 @@ const arrowBack = common.replace('{}', '0 0 24 24').replace('{}', 'M16.62 2.99a1
const arrowForward = common.replace('{}', '0 0 24 24').replace('{}', 'M7.38 21.01c.49.49 1.28.49 1.77 0l8.31-8.31a.996.996 0 0 0 0-1.41L9.15 2.98c-.49-.49-1.28-.49-1.77 0s-.49 1.28 0 1.77L14.62 12l-7.25 7.25c-.48.48-.48 1.28.01 1.76z')

function setCandidates (cands: Candidate[], highlighted: number, markText: string, pageable: boolean, hasPrev: boolean, hasNext: boolean, scrollState: SCROLL_STATE, scrollStart: boolean, scrollEnd: boolean) {
hideContextmenu()
setScrollState(scrollState)
// Clear existing candidates when scroll continues.
if (scrollState !== 2 || scrollStart) {
Expand All @@ -102,48 +103,48 @@ function setCandidates (cands: Candidate[], highlighted: number, markText: strin
fetchComplete()
}
if (scrollState === 2) {
hoverables.classList.add('horizontal-scroll')
hoverables.classList.add('fcitx-horizontal-scroll')
setScrollEnd(scrollEnd)
} else {
hoverables.classList.remove('horizontal-scroll')
hoverables.classList.remove('fcitx-horizontal-scroll')
}
for (let i = 0; i < cands.length; ++i) {
const candidate = div('candidate', 'hoverable')
const candidate = div('fcitx-candidate', 'fcitx-hoverable')
if (i === 0 && scrollState !== 2) {
candidate.classList.add('candidate-first')
candidate.classList.add('fcitx-candidate-first')
}
if (i === highlighted) {
candidate.classList.add('highlighted', 'highlighted-original')
candidate.classList.add('fcitx-highlighted', 'fcitx-highlighted-original')
}
if (i === cands.length - 1 && scrollState !== 2) {
candidate.classList.add('candidate-last')
candidate.classList.add('fcitx-candidate-last')
}

const candidateInner = div('candidate-inner', 'hoverable-inner')
const candidateInner = div('fcitx-candidate-inner', 'fcitx-hoverable-inner')

// Render placeholder for vertical/scroll non-highlighted candidates
if (hoverables.classList.contains('vertical') || hoverables.classList.contains('horizontal-scroll') || i === highlighted) {
const mark = div('mark')
if (hoverables.classList.contains('fcitx-vertical') || hoverables.classList.contains('fcitx-horizontal-scroll') || i === highlighted) {
const mark = div('fcitx-mark')
if (markText === '') {
mark.classList.add('no-text')
mark.classList.add('fcitx-no-text')
} else {
mark.innerHTML = markText
}
candidateInner.append(mark)
}

if (cands[i].label || scrollState === 2) {
const label = div('label')
const label = div('fcitx-label')
label.innerHTML = escapeWS(cands[i].label || '0')
candidateInner.append(label)
}

const text = div('text')
const text = div('fcitx-text')
text.innerHTML = escapeWS(cands[i].text)
candidateInner.append(text)

if (cands[i].comment) {
const comment = div('comment')
const comment = div('fcitx-comment')
comment.innerHTML = escapeWS(cands[i].comment)
candidateInner.append(comment)
}
Expand All @@ -163,36 +164,36 @@ function setCandidates (cands: Candidate[], highlighted: number, markText: strin

if (scrollState === 1) {
hoverables.append(divider(true))
const expand = div('expand', 'hoverable-inner')
const expand = div('fcitx-expand', 'fcitx-hoverable-inner')
expand.innerHTML = arrowForward
const paging = div('paging', 'scroll', 'hoverable')
const paging = div('fcitx-paging', 'fcitx-scroll', 'fcitx-hoverable')
paging.append(expand)
hoverables.append(paging)
} else if (scrollState === 0 && pageable) {
const isArrow = getPagingButtonsStyle() === 'Arrow'
hoverables.append(divider(true))

const prev = div('prev', 'hoverable')
const prevInner = div('paging-inner')
const prev = div('fcitx-prev', 'fcitx-hoverable')
const prevInner = div('fcitx-paging-inner')
if (hasPrev) {
prevInner.classList.add('hoverable-inner')
prevInner.classList.add('fcitx-hoverable-inner')
}
prevInner.innerHTML = isArrow ? arrowBack : caretLeft
prev.appendChild(prevInner)

const next = div('next', 'hoverable')
const nextInner = div('paging-inner')
const next = div('fcitx-next', 'fcitx-hoverable')
const nextInner = div('fcitx-paging-inner')
if (hasNext) {
nextInner.classList.add('hoverable-inner')
nextInner.classList.add('fcitx-hoverable-inner')
}
nextInner.innerHTML = isArrow ? arrowForward : caretRight
next.appendChild(nextInner)

const paging = div('paging')
const paging = div('fcitx-paging')
if (isArrow) {
paging.classList.add('arrow')
paging.classList.add('fcitx-arrow')
} else {
paging.classList.add('triangle')
paging.classList.add('fcitx-triangle')
}
paging.appendChild(prev)
paging.appendChild(next)
Expand All @@ -203,11 +204,11 @@ function setCandidates (cands: Candidate[], highlighted: number, markText: strin
})
}

for (const hoverable of hoverables.querySelectorAll('.hoverable')) {
for (const hoverable of hoverables.querySelectorAll('.fcitx-hoverable')) {
hoverable.addEventListener('mouseenter', () => {
const hoverBehavior = getHoverBehavior()
if (hoverBehavior === 'Move') {
const lastHighlighted = hoverables.querySelector('.highlighted')
const lastHighlighted = hoverables.querySelector('.fcitx-highlighted')
moveHighlight(lastHighlighted, hoverable)
}
})
Expand All @@ -216,10 +217,10 @@ function setCandidates (cands: Candidate[], highlighted: number, markText: strin

function updateElement (element: Element, innerHTML: string) {
if (innerHTML === '') {
element.classList.add('hidden')
element.classList.add('fcitx-hidden')
} else {
element.innerHTML = innerHTML
element.classList.remove('hidden')
element.classList.remove('fcitx-hidden')
}
}

Expand All @@ -238,8 +239,8 @@ function copyHTML () {
hoverables.addEventListener('mouseleave', () => {
const hoverBehavior = getHoverBehavior()
if (hoverBehavior === 'Move') {
const lastHighlighted = hoverables.querySelector('.highlighted')
const originalHighlighted = hoverables.querySelector('.highlighted-original')
const lastHighlighted = hoverables.querySelector('.fcitx-highlighted')
const originalHighlighted = hoverables.querySelector('.fcitx-highlighted-original')
moveHighlight(lastHighlighted, originalHighlighted)
}
})
Expand Down
Loading

0 comments on commit da220a7

Please sign in to comment.