Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

自定义窗口宽度 #255

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/can-i-use.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Base } from './core/base'

const isWindowsOS = (platform: Base['platform']) => platform.os === 'win'
const isMacOS = (platform: Base['platform']) => platform.os === 'mac'
export const isWindowsOS = (platform: Base['platform']) => platform.os === 'win'
export const isMacOS = (platform: Base['platform']) => platform.os === 'mac'

const InitCanIUse = <A extends any[]>(
fn: (...args: A) => boolean
Expand Down
11 changes: 7 additions & 4 deletions src/core/base/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { load as loadPreferences, Preferences, SiteSettingFloorID, SiteSettings
import { hasStrongMobileAccessMode } from '../../preferences/site-settings'
import { getCurrentDisplayLimit, Limit } from './limit'
import { autoAdjustHeight, autoAdjustWidth } from './auto-adjust'
import { initSearchMatrix } from './search-matrix'
import { initWindowOptionMatrix } from './window-option-matrix'
import { getControlWindowHeight } from './control-window-height'
import { getFilteredFloor } from '../../x-state/filtered-floor'
import { specifyFloorIdxBySearchText } from '../../hooks/useSearchForm'
Expand Down Expand Up @@ -47,10 +47,13 @@ export function initLayoutInfo(
max_window_per_line, total_width, window_width
} = autoAdjustWidth(gap_horizontal, limit.width)

console.warn('max_window_per_line', max_window_per_line)

const [
total_row,
search_matrix
] = initSearchMatrix(max_window_per_line, site_settings)
window_option_matrix,
// search_matrix
] = initWindowOptionMatrix(max_window_per_line, site_settings)

const { window_height, total_height } = autoAdjustHeight(
[...cfg.SEARCH_WINDOW_HEIGHT_LIST],
Expand All @@ -67,7 +70,7 @@ export function initLayoutInfo(
total_width,
window_width,
total_row,
search_matrix,
window_option_matrix,
window_height,
total_height
} as const
Expand Down
113 changes: 0 additions & 113 deletions src/core/base/search-matrix.ts

This file was deleted.

150 changes: 150 additions & 0 deletions src/core/base/window-option-matrix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import { curry, nth, compose } from 'ramda'
import cfg from '../../config'
import { Matrix, Row } from '../common'

import {
toSearchURL,
addMobileIdentifier,
SiteOption,
SiteSettings,
toMatrix
} from '../../preferences/site-settings'

export type WindowOptionType = 'NORMAL' | 'FILL' | 'EMPTY' | 'PLAIN'
type ComposeSearchURLFn = (keyword: string) => string
type DefineWindowOption<T extends WindowOptionType, W extends number, S> = {
type: T
width_size: W
site_option: S
composeSearchURL: ComposeSearchURLFn
}

export type WindowOption =
DefineWindowOption<'NORMAL', number, SiteOption> |
DefineWindowOption<'FILL', 1, undefined> |
DefineWindowOption<'EMPTY', 1, undefined> |
DefineWindowOption<'PLAIN', 1, undefined>

type WindowOptionRow = Array<WindowOption>
export type WindowOptionMatrix = Array<WindowOptionRow>

function ComposeSearchURL(
plain_window_url_pattern: string,
site_row: Row<SiteOption>,
col: number
): ComposeSearchURLFn {
const site_opt = nth(col, site_row)
if (site_opt === undefined) {
return curry(toSearchURL)(plain_window_url_pattern)
} else {
const toUrl = curry(toSearchURL)(site_opt.url_pattern)
if (site_opt.access_mode === 'MOBILE') {
return compose(addMobileIdentifier, toUrl)
} else {
return toUrl
}
}
}

function fillRow(
plain_window_url_pattern: string,
max_window_per_line: number,
site_row: Row<SiteOption>,
): WindowOptionMatrix {
let result: WindowOptionRow = []

for (let idx = 0; idx < max_window_per_line; ++idx) {
const composeSearchURL = ComposeSearchURL(plain_window_url_pattern, site_row, idx)
const site_option = site_row[idx]
console.log('idx', idx, max_window_per_line, site_option)

if (result.length >= max_window_per_line) {
// 超过了,
// const remain: Row<SiteOption> = site_row.slice(idx, max_window_per_line)
const remain: Row<SiteOption> = site_row.slice(idx, max_window_per_line)
console.warn('超过了', remain, result.length, max_window_per_line)

if (remain.length) {
return [
result.slice(0, max_window_per_line),
...fillRow(plain_window_url_pattern, max_window_per_line, remain),
]
} else {
return [ result ]
}
}

if (site_option === undefined) {
result.push({
type: 'PLAIN', width_size: 1, site_option: undefined, composeSearchURL,
})
} else {
result.push({
type: 'NORMAL',
width_size: site_option.width_size,
site_option,
composeSearchURL,
})
if (site_option.width_size > 1) {
for (let r = site_option.width_size; r > 1; --r) {
result.push({
type: 'FILL',
width_size: 1,
site_option: undefined,
composeSearchURL,
})
}
}
}
}

return [ result ]
}

function createWindowOptionMatrix(
plain_window_url_pattern: string,
max_window_per_row: number,
site_matrix: Matrix<SiteOption>,
): WindowOptionMatrix {
if (site_matrix.length === 0) {
return []
} else {
const [cols, ...remain_matrix] = site_matrix
return [
...fillRow(plain_window_url_pattern, max_window_per_row, cols),
...createWindowOptionMatrix(plain_window_url_pattern, max_window_per_row, remain_matrix)
]
// if (cols.length > max_window_per_row) {
// return createSearchMatrix(
// plain_window_url_pattern,
// max_window_per_row,
// [
// cols.slice(0, max_window_per_row),
// cols.slice(max_window_per_row, cols.length),
// ...remain_matrix
// ],
// )
// } else {
// return [
// ...fillRow(plain_window_url_pattern, max_window_per_row, cols),
// ...createSearchMatrix(plain_window_url_pattern, max_window_per_row, remain_matrix)
// ]
// }
}
}

export function initWindowOptionMatrix(
max_window_per_line: number,
site_settings: SiteSettings
) {
const search_matrix = createWindowOptionMatrix(
cfg.PLAIN_SEARCH_WINDOW_URL_PATTERN,
max_window_per_line,
toMatrix(site_settings),
)
console.warn('search_matrix', search_matrix, toMatrix(site_settings))
const search_count = search_matrix.flat().length
const total_row = Math.ceil(search_count / max_window_per_line)

return [ total_row, search_matrix ] as const
}
25 changes: 20 additions & 5 deletions src/core/layout/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,32 @@ export async function CreateSearchLayout({
console.log('CreateSearchLayout')

function getRegIds(): number[] {
return getMatrix().flat().filter(u => u.state !== 'EMPTY').map(u => u.windowId)
return (
getMatrix()
.flat()
.filter(u => u.type !== 'EMPTY')
.filter(u => u.type !== 'FILL')
.map(u => u.windowId)
)
}

async function refreshLayout(skip_ids: number[]) {
await renderMatrix(base, layout_info, getMatrix(), true, false, skip_ids)
await renderMatrix(base.platform, base.limit, layout_info, getMatrix(), {
preset_focused: true,
reset_size: false,
skip_ids
})
if (skip_ids.indexOf(control_window_id) === -1) {
await chrome.windows.update(control_window_id, { focused: true })
}
}

const { search_matrix } = layout_info
const { window_option_matrix } = layout_info
const [getMatrix, setMatrix] = Memo(
await constructSearchWindowsFast(
base,
layout_info,
search_matrix,
window_option_matrix,
keyword,
creating_signal,
stop_creating_signal
Expand Down Expand Up @@ -78,7 +88,12 @@ export async function CreateSearchLayout({
const [need_update, update] = selectWindow(getMatrix(), focused_window_id)
if (need_update) {
const col_refresh_waiting = renderCol(
base, layout_info, update.new_matrix, update.col, true, true
base.platform, base.limit, layout_info, update.new_matrix,
{
select_col: update.col,
preset_focused: true,
reset_size: true
}
)

if (needRefocusingLayout()) {
Expand Down
Loading