Compare commits
164 Commits
add-macos-
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
65a48efdde | ||
|
|
0613238876 | ||
|
|
501f6df473 | ||
|
|
67c8c4bdfa | ||
|
|
b5f5d3bd28 | ||
|
|
378d5aef69 | ||
|
|
10ee3cd9d3 | ||
|
|
97d2450fa7 | ||
|
|
18828ab043 | ||
|
|
3b80eb77b4 | ||
|
|
6e2514adbd | ||
|
|
82074ab46a | ||
|
|
8158d49050 | ||
|
|
3052878869 | ||
|
|
73ca224ad8 | ||
|
|
ff7721d17f | ||
|
|
fc642ac0e3 | ||
|
|
d5d616ade9 | ||
|
|
96977be623 | ||
|
|
d8a1b9b9c6 | ||
|
|
f83b1ba2a7 | ||
|
|
8ac0065234 | ||
|
|
31806b6057 | ||
|
|
533bfaf45b | ||
|
|
459705af70 | ||
|
|
84e556ddad | ||
|
|
b50a20c7d4 | ||
|
|
d4ccd780b2 | ||
|
|
aef934e9a2 | ||
|
|
1fb927c26b | ||
|
|
8974624b3c | ||
|
|
d99b35bf4c | ||
|
|
594d0ffe3f | ||
|
|
7b08a87766 | ||
|
|
ab5ca24270 | ||
|
|
c593b07187 | ||
|
|
c088dde749 | ||
|
|
1fdf7c499d | ||
|
|
01dfc616d4 | ||
|
|
8d7d655581 | ||
|
|
5292538dd7 | ||
|
|
bab98d4576 | ||
|
|
6067fa7029 | ||
|
|
61860b400f | ||
|
|
50518b6c21 | ||
|
|
b5d3ce9910 | ||
|
|
abac92d8d5 | ||
|
|
9ea7c9b1ff | ||
|
|
fcbc77fb5a | ||
|
|
60b34a118b | ||
|
|
3e0839f3da | ||
|
|
bd61faf660 | ||
|
|
0e48f4f71c | ||
|
|
24fe7144f8 | ||
|
|
e92eee1ecf | ||
|
|
1996298f0c | ||
|
|
c879c63b17 | ||
|
|
892fe78d03 | ||
|
|
e5860f63c7 | ||
|
|
fa9656bfd7 | ||
|
|
03954748b6 | ||
|
|
4a627cb32e | ||
|
|
3029303e95 | ||
|
|
fc7cd165a8 | ||
|
|
f267df3f71 | ||
|
|
b07707e973 | ||
|
|
6b0111b89f | ||
|
|
e029ddf2ba | ||
|
|
731cfc5bd7 | ||
|
|
cbd8dc52cd | ||
|
|
d1ad1af71a | ||
|
|
121f9c6118 | ||
|
|
770f60f30c | ||
|
|
5c92b5acab | ||
|
|
8e49455acf | ||
|
|
859def21bf | ||
|
|
6145306ee8 | ||
|
|
d0f7b7b833 | ||
|
|
f221606ae2 | ||
|
|
cd00ada3ac | ||
|
|
be6611133a | ||
|
|
9e682ceafc | ||
|
|
5510bedf7f | ||
|
|
ea34b7a404 | ||
|
|
ce94543baa | ||
|
|
89a8304b9e | ||
|
|
9652a54f08 | ||
|
|
ca71f07f3a | ||
|
|
00eb6bed2b | ||
|
|
95dc7a88d2 | ||
|
|
6aec9cbae2 | ||
|
|
4e58bc4b2c | ||
|
|
a9a4b5319c | ||
|
|
6523fef12b | ||
|
|
b8affcd4a1 | ||
|
|
595ae676b7 | ||
|
|
5c76c92c95 | ||
|
|
f03ad8a6c8 | ||
|
|
386ebb60c0 | ||
|
|
17c7227a44 | ||
|
|
23faaf6fc3 | ||
|
|
3131d3cea4 | ||
|
|
3014dc8839 | ||
|
|
829d3868c4 | ||
|
|
6584504142 | ||
|
|
01c51d83d6 | ||
|
|
29442826c5 | ||
|
|
e249c02123 | ||
|
|
7ac4508e8d | ||
|
|
450baccc92 | ||
|
|
bd0c9a740b | ||
|
|
fca11a9001 | ||
|
|
1aa30ee5bc | ||
|
|
cdaa151028 | ||
|
|
fd8d5819b8 | ||
|
|
4a5a4da399 | ||
|
|
efaaf73cd7 | ||
|
|
86540ad1a9 | ||
|
|
950482608d | ||
|
|
412c8d8612 | ||
|
|
de3c78a5aa | ||
|
|
eafa704ca5 | ||
|
|
86357079f8 | ||
|
|
ed118151cc | ||
|
|
50b26e2d9e | ||
|
|
a4aacc16d9 | ||
|
|
9aa7d23632 | ||
|
|
99b316da19 | ||
|
|
828c84762b | ||
|
|
5dae5d1cc1 | ||
|
|
23372655ca | ||
|
|
f5b33af7f1 | ||
|
|
993da9a8ad | ||
|
|
93f1024230 | ||
|
|
7b5e528060 | ||
|
|
1d5ba3ab07 | ||
|
|
f93c527561 | ||
|
|
6065353ac9 | ||
|
|
783cb73b29 | ||
|
|
ee75f0d119 | ||
|
|
aaac874f2c | ||
|
|
cd9e454991 | ||
|
|
d0fc79238b | ||
|
|
3ed84c2318 | ||
|
|
bd039398ba | ||
|
|
568db6aba0 | ||
|
|
2eb10933e7 | ||
|
|
5c6cf18139 | ||
|
|
01c31d884a | ||
|
|
d48d4af7d2 | ||
|
|
876d14f9d9 | ||
|
|
a8e090c9be | ||
|
|
c30df6cee0 | ||
|
|
b833769c25 | ||
|
|
855fb2a168 | ||
|
|
d2735ec13b | ||
|
|
c40fc5818a | ||
|
|
a553ebd593 | ||
|
|
232166eb89 | ||
|
|
99144950d9 | ||
|
|
32d4f45144 | ||
|
|
6bc78b41ef | ||
|
|
cd54beee04 | ||
|
|
ee45d21bbe |
2
.env
@@ -1,5 +1,3 @@
|
||||
COCO_SERVER_URL=http://localhost:9000 #https://coco.infini.cloud #http://localhost:9000
|
||||
|
||||
COCO_WEBSOCKET_URL=ws://localhost:9000/ws #wss://coco.infini.cloud/ws #ws://localhost:9000/ws
|
||||
|
||||
#TAURI_DEV_HOST=0.0.0.0
|
||||
70
.github/workflows/frontend-ci.yml
vendored
Normal file
@@ -0,0 +1,70 @@
|
||||
name: Frontend Code Check
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
# Only run it when Frontend code changes
|
||||
paths:
|
||||
- 'src/**'
|
||||
- 'tsup.config.ts'
|
||||
- 'package.json'
|
||||
|
||||
jobs:
|
||||
check:
|
||||
strategy:
|
||||
matrix:
|
||||
platform: [ubuntu-latest, windows-latest, macos-latest]
|
||||
|
||||
runs-on: ${{ matrix.platform }}
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
ref: ${{ github.head_ref }}
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Setup Node.js
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: '20'
|
||||
|
||||
# No need to pass the version arg as it is specified by "packageManager" in package.json
|
||||
- name: Install pnpm
|
||||
uses: pnpm/action-setup@v4
|
||||
|
||||
- name: Install dependencies
|
||||
run: pnpm install --frozen-lockfile
|
||||
|
||||
- name: Switch platformAdapter to Web adapter
|
||||
shell: bash
|
||||
run: >
|
||||
node -e "const fs=require('fs');const f='src/utils/platformAdapter.ts';
|
||||
let s=fs.readFileSync(f,'utf8');
|
||||
s=s.replace(/import\\s*\\{\\s*createTauriAdapter\\s*\\}\\s*from\\s*\\\"\\.\\/tauriAdapter\\\";/,'import { createWebAdapter } from \\\"./webAdapter\\\";');
|
||||
s=s.replace(/let\\s+platformAdapter\\s*=\\s*createTauriAdapter\\(\\);/,'let platformAdapter = createWebAdapter();');
|
||||
fs.writeFileSync(f,s);"
|
||||
|
||||
- name: Build web (Tauri dependency check)
|
||||
run: pnpm build:web
|
||||
|
||||
- name: Verify no Tauri refs in web output
|
||||
shell: bash
|
||||
run: |
|
||||
if grep -R -n -E '@tauri-apps|tauri-plugin' out/search-chat; then
|
||||
echo 'Tauri references found in web build output';
|
||||
exit 1;
|
||||
else
|
||||
echo 'No Tauri references found';
|
||||
fi
|
||||
|
||||
- name: Restore platformAdapter to Tauri adapter
|
||||
shell: bash
|
||||
run: >
|
||||
node -e "const fs=require('fs');const f='src/utils/platformAdapter.ts';
|
||||
let s=fs.readFileSync(f,'utf8');
|
||||
s=s.replace(/import\\s*\\{\\s*createWebAdapter\\s*\\}\\s*from\\s*\\\"\\.\\/webAdapter\\\";/,'import { createTauriAdapter } from \\\"./tauriAdapter\\\";');
|
||||
s=s.replace(/let\\s+platformAdapter\\s*=\\s*createWebAdapter\\(\\);/,'let platformAdapter = createTauriAdapter();');
|
||||
fs.writeFileSync(f,s);"
|
||||
|
||||
- name: Build frontend
|
||||
run: pnpm build
|
||||
12
.github/workflows/release.yml
vendored
@@ -104,7 +104,17 @@ jobs:
|
||||
if: startsWith(matrix.platform, 'ubuntu-22.04')
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y libwebkit2gtk-4.1-dev libappindicator3-dev librsvg2-dev patchelf xdg-utils
|
||||
sudo apt-get install -y libwebkit2gtk-4.1-dev libappindicator3-dev librsvg2-dev patchelf xdg-utils libtracker-sparql-3.0-dev
|
||||
|
||||
|
||||
# On Windows, we need to generate bindings for 'searchapi.h' using bindgen.
|
||||
# And bindgen relies on 'libclang'
|
||||
# https://rust-lang.github.io/rust-bindgen/requirements.html#windows
|
||||
#
|
||||
# We don't need to install it because it is already included in GitHub
|
||||
# Action runner image:
|
||||
# https://github.com/actions/runner-images/blob/main/images/windows/Windows2025-Readme.md#language-and-runtime
|
||||
|
||||
|
||||
- name: Add Rust build target
|
||||
working-directory: src-tauri
|
||||
|
||||
10
.github/workflows/rust_code_check.yml
vendored
@@ -30,7 +30,15 @@ jobs:
|
||||
if: startsWith(matrix.platform, 'ubuntu-latest')
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y libwebkit2gtk-4.1-dev libappindicator3-dev librsvg2-dev patchelf xdg-utils
|
||||
sudo apt-get install -y libwebkit2gtk-4.1-dev libappindicator3-dev librsvg2-dev patchelf xdg-utils libtracker-sparql-3.0-dev
|
||||
|
||||
# On Windows, we need to generate bindings for 'searchapi.h' using bindgen.
|
||||
# And bindgen relies on 'libclang'
|
||||
# https://rust-lang.github.io/rust-bindgen/requirements.html#windows
|
||||
#
|
||||
# We don't need to install it because it is already included in GitHub
|
||||
# Action runner image:
|
||||
# https://github.com/actions/runner-images/blob/main/images/windows/Windows2025-Readme.md#language-and-runtime
|
||||
|
||||
- name: Add pizza engine as a dependency
|
||||
working-directory: src-tauri
|
||||
|
||||
5
.vscode/settings.json
vendored
@@ -14,6 +14,7 @@
|
||||
"dyld",
|
||||
"elif",
|
||||
"errmsg",
|
||||
"frontmost",
|
||||
"fullscreen",
|
||||
"fulltext",
|
||||
"headlessui",
|
||||
@@ -40,6 +41,7 @@
|
||||
"nowrap",
|
||||
"nspanel",
|
||||
"nsstring",
|
||||
"objc",
|
||||
"overscan",
|
||||
"partialize",
|
||||
"patchelf",
|
||||
@@ -83,5 +85,6 @@
|
||||
"i18n-ally.keystyle": "nested",
|
||||
"editor.tabSize": 2,
|
||||
"editor.insertSpaces": true,
|
||||
"editor.detectIndentation": false
|
||||
"editor.detectIndentation": false,
|
||||
"i18n-ally.displayLanguage": "zh"
|
||||
}
|
||||
@@ -64,9 +64,9 @@ At Coco AI, we aim to streamline workplace collaboration by centralizing access
|
||||
|
||||
### Prerequisites
|
||||
|
||||
- Node.js >= 18.12
|
||||
- Rust (latest stable)
|
||||
- pnpm (package manager)
|
||||
- [Node.js >= 18.12](https://nodejs.org/en/download/)
|
||||
- [Rust (latest stable)](https://www.rust-lang.org/tools/install)
|
||||
- [pnpm (package manager)](https://pnpm.io/installation)
|
||||
|
||||
### Development Setup
|
||||
|
||||
|
||||
22
components.json
Normal file
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"$schema": "https://ui.shadcn.com/schema.json",
|
||||
"style": "new-york",
|
||||
"rsc": false,
|
||||
"tsx": true,
|
||||
"tailwind": {
|
||||
"config": "tailwind.config.js",
|
||||
"css": "src/main.css",
|
||||
"baseColor": "neutral",
|
||||
"cssVariables": true,
|
||||
"prefix": ""
|
||||
},
|
||||
"iconLibrary": "lucide",
|
||||
"aliases": {
|
||||
"components": "@/components",
|
||||
"utils": "@/lib/utils",
|
||||
"ui": "@/components/ui",
|
||||
"lib": "@/lib",
|
||||
"hooks": "@/hooks"
|
||||
},
|
||||
"registries": {}
|
||||
}
|
||||
@@ -9,7 +9,7 @@ Coco AI is a fully open-source, cross-platform unified search and productivity t
|
||||
|
||||
{{% load-img "/img/coco-preview.gif" "" %}}
|
||||
|
||||
For more details on Coco Server, visit: [https://docs.infinilabs.com/coco-app/](https://docs.infinilabs.com/coco-app/).
|
||||
For more details on Coco Server, visit: [https://docs.infinilabs.com/coco-server/](https://docs.infinilabs.com/coco-server/).
|
||||
|
||||
## Community
|
||||
|
||||
|
||||
59
docs/content.en/docs/core-features/AI Overview.md
Normal file
@@ -0,0 +1,59 @@
|
||||
---
|
||||
weight: 1
|
||||
title: AI Overview
|
||||
---
|
||||
|
||||
# AI Overview
|
||||
|
||||
The **AI Overview** feature can automatically refine and summarize current search results in search mode, helping users quickly grasp the key points of the search results without having to browse each individual result. This feature is particularly useful in scenarios where information needs to be extracted quickly.
|
||||
|
||||
{{% load-img "/img/core-features/ai_overview_01.png" "" %}}
|
||||
|
||||
|
||||
|
||||
|
||||
## Feature Overview
|
||||
|
||||
- **Automatic Refinement and Summary**: When a user performs a search, AI Overview automatically generates a concise summary based on the current search results, providing key information from the results.
|
||||
|
||||
- **Improve Work Efficiency**: By avoiding the need to manually browse through numerous results, AI Overview helps users quickly focus on the most relevant information, saving time.
|
||||
|
||||
|
||||
|
||||
|
||||
## Enabling AI Overview
|
||||
|
||||
{{% load-img "/img/core-features/ai_overview_02.png" "" %}}
|
||||
|
||||
|
||||
|
||||
To use the **AI Overview** feature, you need to configure it in the settings:
|
||||
|
||||
1. Open the **Settings** page and select the **Extensions** option.
|
||||
|
||||
2. In the **AI Overview Extension** configuration, choose an **AI assistant** that you want to use for summarization.
|
||||
|
||||
3. Configure the **trigger strategy**:
|
||||
- **Minimum number of search results**: Set the minimum number of search results required to trigger AI Overview.
|
||||
- **Minimum input length**: Set the minimum length of the input query; the summary function will only start when the input content is long enough.
|
||||
- **Delay after typing stops**: Set the time delay after input stops to start the summary function, avoiding unnecessary summaries triggered by frequent input.
|
||||
|
||||
4. After saving the settings, in search mode, press `Meta + O` to enable the AI Overview feature, and AI Overview will automatically generate summaries for the search results according to your configuration.
|
||||
|
||||
|
||||
|
||||
|
||||
> 💡 **Tip**: **The style and depth of the summary depend on the AI assistant you choose.**
|
||||
>
|
||||
> Think of it as an "information assistant"; the role you assign to it determines its reporting style:
|
||||
>
|
||||
> - **"Summary Abstract" assistant**: Provides quick, general summaries.
|
||||
>
|
||||
> - **"Technical Expert" assistant**: May generate summaries that focus more on technical specifications and code snippets.
|
||||
>
|
||||
> - **"Market Analyst" assistant**: Will pay more attention to market data, competitive dynamics, etc.
|
||||
>
|
||||
>
|
||||
> 💡 **Tip**: **For faster response speed**
|
||||
>
|
||||
> If you pursue **ultimate response speed**, it is recommended to configure an assistant using a **fast token-generation, non-inference type model** for the AI Overview feature. Such models can quickly generate summaries for you, making information acquisition smooth.
|
||||
41
docs/content.en/docs/core-features/Application Search.md
Normal file
@@ -0,0 +1,41 @@
|
||||
---
|
||||
weight: 4
|
||||
title: Application Search
|
||||
---
|
||||
|
||||
# Application Search
|
||||
|
||||
The **Applications** feature allows you to directly search for and launch locally installed applications in Coco AI. You can quickly find and open any application through the unified search entry without switching windows or manually searching.
|
||||
|
||||
{{% load-img "/img/core-features/application_search_01.png" "" %}}
|
||||
|
||||
|
||||
## Feature Overview
|
||||
|
||||
- **Quick Launch**: Enter the application name in the search box to instantly match results and quickly open the program.
|
||||
|
||||
- **Custom Search Scope**: Control which directories' applications are indexed and displayed through settings.
|
||||
|
||||
|
||||
|
||||
|
||||
## Feature Settings
|
||||
|
||||
{{% load-img "/img/core-features/application_search_02.png" "" %}}
|
||||
|
||||
To use the **AI Overview** feature, you need to configure it in the settings:
|
||||
|
||||
1. **Search Scope**
|
||||
Specify the paths where Coco AI will search for executable applications.
|
||||
|
||||
- For example:
|
||||
- macOS: `/Applications`, `~/Applications`
|
||||
- Windows: `C:\Program Files`, `C:\Users\<User>\AppData\Local`
|
||||
- You can add or remove paths according to actual needs to avoid displaying irrelevant programs.
|
||||
|
||||
2. **Rebuild Index**
|
||||
|
||||
Rescan and update the local application index.
|
||||
|
||||
- Usually, there is no need to perform this manually.
|
||||
- If you find that an installed application does not appear in the search results, you can click **Rebuild Index** to manually retry and update the results.
|
||||
32
docs/content.en/docs/core-features/Calculator.md
Normal file
@@ -0,0 +1,32 @@
|
||||
---
|
||||
weight: 5
|
||||
title: Calculator
|
||||
---
|
||||
|
||||
# Calculator
|
||||
|
||||
Coco AI provides a concise calculator function that allows users to perform quickquick basic mathematical calculations directly in the input box without opening a separate calculator application. Simply enter an arithmetic expression, and the system will instantly provide the result. It also supports copying the arithmetic expression and the calculation result for easy use at any time.
|
||||
|
||||
{{% load-img "/img/core-features/calculator_01.png" "" %}}
|
||||
|
||||
|
||||
|
||||
## Feature Overview
|
||||
|
||||
- **Quick Calculation**: Enter basic mathematical expressions in Coco AI's input box, and the system will automatically calculate and display the result.
|
||||
- **Support for Basic Mathematical Operations**: Currently supports basic arithmetic operations such as addition, subtraction, multiplication, and division.
|
||||
- **Copy Expression and Result**: Supports copying the complete arithmetic expression and calculation result for easy pasting into other applications.
|
||||
|
||||
|
||||
|
||||
## Usage Method
|
||||
|
||||
1. **Enter an Expression**:
|
||||
- Directly input a basic mathematical expression in Coco AI's input box, for example: `256 * 42`
|
||||
- The system will automatically calculate and display the result.
|
||||
2. **Copy Expression and Result**:
|
||||
- When the result is displayed, press `Enter` to copy the calculation result.
|
||||
- Use the shortcut key `Meta + K` to open more operations, and select **Copy Answer**, **Copy Question and Answer**, or **Copy Answer (in Word)**
|
||||
|
||||
{{% load-img "/img/core-features/calculator_02.png" "" %}}
|
||||
|
||||
55
docs/content.en/docs/core-features/File Search.md
Normal file
@@ -0,0 +1,55 @@
|
||||
---
|
||||
weight: 3
|
||||
title: File Search
|
||||
---
|
||||
|
||||
# File Search
|
||||
|
||||
The File Search feature allows you to directly use the system's local search capability in Coco AI to quickly find files on your computer. You can flexibly set the search scope, excluded directories, file types, and search methods to get more accurate results.
|
||||
|
||||
{{% load-img "/img/core-features/filesearch_01.png" "" %}}
|
||||
|
||||
|
||||
|
||||
## Feature Overview
|
||||
|
||||
- **System-level search integration**: Coco AI leverages the file indexing capabilities provided by the operating system (such as macOS Spotlight, Windows Search, etc.) to achieve efficient local file search.
|
||||
- **Flexible search control**: Supports custom search scopes and excluded paths, and can filter file types according to needs.
|
||||
- **Content-level search**: On supported systems, you can choose to search file contents at the same time, not just file names.
|
||||
|
||||
|
||||
|
||||
## Feature Settings
|
||||
|
||||
{{% load-img "/img/core-features/filesearch_02.png" "" %}}
|
||||
|
||||
Coco AI is already equipped with local file search capabilities. You don't need any additional operations; you can start typing keywords in the search box to experience it immediately. If you want to exclude certain folders or add new search locations, you can manage your preferences at any time through **"Settings → Extensions → File Search"**.
|
||||
|
||||
1. **Search By**
|
||||
Select the matching method for the search:
|
||||
|
||||
- **Name**: Only match file names (faster).
|
||||
- **Name + Contents**: Match both file names and file contents (depending on operating system support).
|
||||
|
||||
2. **Search Scope**
|
||||
Select the folders or disk locations to be included in the search.
|
||||
|
||||
- For example: `/Users/username/Documents` or `D:\Projects`
|
||||
|
||||
3. **Exclude Scope**
|
||||
Specify paths that are not included in the search, used to reduce irrelevant results or improve search speed.
|
||||
|
||||
- For example: `node_modules`, `tmp`, `Library` and other system cache directories.
|
||||
|
||||
4. **Search File Types**
|
||||
Limit the file extensions or types to be searched.
|
||||
|
||||
- For example: `.pdf`, `.docx`, `.md`, `.txt`
|
||||
|
||||
|
||||
|
||||
> 💡 **Tips**: **System Support Differences**
|
||||
>
|
||||
> - **macOS**: Implements mixed search of file names and contents through **Spotlight**, supporting fast response and fuzzy matching.
|
||||
> - **Windows**: Relies on the system's **Windows Search Indexer**, supporting file name search; content search requires enabling content indexing for corresponding file types in system index settings.
|
||||
> - **Linux**: Generally only supports file name search, depending on the distribution and configuration.
|
||||
32
docs/content.en/docs/core-features/Quick AI Access.md
Normal file
@@ -0,0 +1,32 @@
|
||||
---
|
||||
weight: 2
|
||||
title: Quick AI Access
|
||||
---
|
||||
|
||||
# Quick AI Access
|
||||
|
||||
The **Quick AI Access** feature allows you to directly start a conversation with AI through the search box without switching to chat mode. This feature provides users with a smoother and more efficient interaction experience, especially suitable for scenarios where quick feedback or handling simple questions is needed.
|
||||
|
||||
{{% load-img "/img/core-features/quick_ai_access_01.png" "" %}}
|
||||
|
||||
|
||||
|
||||
## Feature Overview
|
||||
|
||||
{{% load-img "/img/core-features/quick_ai_access_02.png" "" %}}
|
||||
|
||||
- **Quickly Start a Conversation**: After entering content in the search box, press `Meta + Enter` to directly start a conversation with the AI assistant without switching to chat mode.
|
||||
- **Instant Response**: Coco AI will display the conversation reply in the same window, providing answers or suggestions quickly.
|
||||
- **Switch from Conversation Mode**: After completing a quick conversation, press `Meta + Enter` to switch to the full chat mode and continue multi-turn conversations.
|
||||
|
||||
|
||||
|
||||
## Enabling Quick AI Access
|
||||
|
||||
{{% load-img "/img/core-features/quick_ai_access_03.png" "" %}}
|
||||
|
||||
To use the **Quick AI Access** feature, you need to configure it in the settings:
|
||||
|
||||
1. Open the **Settings** page and select the **Extensions** option.
|
||||
2. In the **Quick AI Access Extension** configuration, associate an AI assistant that you want to quickly access via `Meta + Enter`.
|
||||
3. After saving the settings, you can directly start a conversation with the selected assistant through `Meta + Enter` in the search box.
|
||||
45
docs/content.en/docs/core-features/Window Management.md
Normal file
@@ -0,0 +1,45 @@
|
||||
---
|
||||
weight: 6
|
||||
title: Window Management
|
||||
---
|
||||
|
||||
# Window Management
|
||||
|
||||
Easily adjust, reorganize, and move the windows you're focusing on.
|
||||
No need for manual dragging—quickly perform window layout operations through commands.
|
||||
|
||||
{{% load-img "/img/core-features/window_management_01.png" "" %}}
|
||||
|
||||
|
||||
|
||||
|
||||
## Feature Overview
|
||||
|
||||
- **Move Windows**: Move the current window to the left half, right half, top half, or bottom half of the screen.
|
||||
- **Resize Windows**: Quickly adjust to full screen, centered, 1/3, or 2/3 size layouts.
|
||||
- **Multi-monitor Support**: Quickly move windows between multiple monitors.
|
||||
- **Focus Windows**: Quickly focus on a specified window or application via shortcut keys.
|
||||
|
||||
|
||||
|
||||
|
||||
## Usage
|
||||
|
||||
{{% load-img "/img/core-features/window_management_02.png" "" %}}
|
||||
|
||||
Enter commands included in Window Management in the **Coco AI search box** to browse and execute window management commands, such as:
|
||||
|
||||
- **Almost Maximize Bottom** — Maximize the window to the lower area of the screen
|
||||
- **Bottom Half** — Move the current window to the lower half of the screen
|
||||
- **Bottom Left Quarter** — Position the window to the bottom-left quarter
|
||||
- **Bottom Right Sixth** — Place the window in the bottom-right sixth area
|
||||
|
||||
The window's position and size will be adjusted immediately after selecting a command.
|
||||
|
||||
|
||||
|
||||
|
||||
> 💡 **Tips**
|
||||
>
|
||||
> - System-level window operations are supported; some special types of windows (such as full-screen or independent floating windows) may not be controllable.
|
||||
> - It is recommended to combine custom shortcuts for commands to quickly achieve common window layouts.
|
||||
5
docs/content.en/docs/core-features/_index.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
weight: 2
|
||||
title: Core Features
|
||||
bookCollapseSection: true
|
||||
---
|
||||
103
docs/content.en/docs/getting-started/AI Chat.md
Normal file
@@ -0,0 +1,103 @@
|
||||
---
|
||||
weight: 4
|
||||
title: AI Chat
|
||||
---
|
||||
|
||||
# AI Chat
|
||||
|
||||
Coco AI is not just a search tool, but your AI intelligent center.
|
||||
In chat mode, you can communicate with AI in natural language, ask questions, analyze files, and summarize knowledge.
|
||||
|
||||
{{% load-img "/img/core-features/basics_02.png" "" %}}
|
||||
|
||||
|
||||
|
||||
|
||||
## Chat Entry
|
||||
|
||||
- Use the global shortcut (default: `Shift + Meta + Space`) to open the Coco AI interface.
|
||||
|
||||
- The interface is in chat mode (use the switch button or the shortcut `Meta + T` to switch modes).
|
||||
|
||||
- Enter natural language questions in the input box. Press `Enter` to start the conversation.
|
||||
|
||||
|
||||
|
||||
|
||||
## Chat Interface and Functions
|
||||
|
||||
Coco AI's chat interface is designed to be concise and intuitive, allowing you to quickly switch AI assistants, access different Coco Servers, browse historical conversations, or use advanced capabilities such as deep thinking, web search, and tool calls.
|
||||
|
||||
{{% load-img "/img/core-features/ai_chat_01.png" "" %}}
|
||||
|
||||
#### Interface Overview
|
||||
|
||||
In chat mode, the Coco AI interface mainly consists of the following areas:
|
||||
|
||||
- **Top Bar**
|
||||
- **Assistant Selection**: The drop-down menu in the upper left corner allows you to quickly switch between different AI assistants.
|
||||
- **Historical Conversations**: Click the icon in the upper left corner to view recent conversations, and click any one to restore the conversation context.
|
||||
- **Server Switching**: The cloud icon in the upper right corner shows the currently connected Coco Server, and you can switch or refresh the server with one click.
|
||||
- **Independent Window Mode**: The icon in the upper right corner can pop up the current conversation into an independent window, facilitating multi-task collaboration or comparison viewing.
|
||||
- **Middle Area**
|
||||
- Displays conversation content and AI responses.
|
||||
- **Bottom Input Area**
|
||||
- Enter messages and press `Enter` to send, supporting voice input.
|
||||
- The left function bar includes controls such as web search, tool call (MCP), and deep thinking switch.
|
||||
|
||||
|
||||
|
||||
|
||||
#### Multiple Servers and Assistants
|
||||
|
||||
##### Switching Coco Server
|
||||
|
||||
Coco AI supports connecting to multiple Coco Servers, and each server can contain a different number of AI assistants.
|
||||
|
||||
Click the **server icon** in the upper right corner to view the current connection status:
|
||||
|
||||
- Displays the server name and online status.
|
||||
- Lists the number of available AI assistants on the server.
|
||||
- Supports one-click switching, refreshing, or entering the settings page.
|
||||
|
||||
{{% load-img "/img/core-features/ai_chat_03.png" "" %}}
|
||||
|
||||
|
||||
|
||||
##### Switching AI Assistants
|
||||
|
||||
The drop-down menu in the upper left corner lists all assistants in the current server.
|
||||
|
||||
Each assistant may have different capabilities and modes according to the configuration.
|
||||
|
||||
{{% load-img "/img/core-features/ai_chat_02.png" "" %}}
|
||||
|
||||
|
||||
|
||||
> 💡 **Tip**: When switching assistants in the same conversation, Coco AI will automatically retain the context of the current conversation. This means you can let different assistants take turns answering or supplementing analysis in the same round of conversation without re-entering background content.
|
||||
|
||||
|
||||
|
||||
#### Bottom Function Bar
|
||||
|
||||
The function buttons at the bottom left of the input box can quickly call the following capabilities:
|
||||
|
||||
| Function | Icon | Description |
|
||||
| -------------------- | ---- | ------------------------------------------------------------ |
|
||||
| **Deep Think Switch** | 🧠 | Turn on or off the deep think capability (only available for assistants in deep think mode). |
|
||||
| **Search Switch** | 🌐 | Call the data sources connected in the Coco Server for real-time search. (Some data sources can be selected as needed) |
|
||||
| **MCP Switch** | 🔨 | Call external tools or commands, such as database query, translation, task execution, etc. |
|
||||
|
||||
> **💡 Tip**: Search and MCP tool calls rely on the currently connected Coco Server, and their availability depends on server configuration.
|
||||
|
||||
{{% load-img "/img/core-features/ai_chat_04.png" "" %}}
|
||||
|
||||
|
||||
|
||||
#### Interactive Operations
|
||||
|
||||
- Press `Enter` to send a message
|
||||
- Press `Shift + Enter` to wrap lines
|
||||
- Press `Meta + U` to switch AI assistants
|
||||
- Press `Meta + S` to switch Coco Server
|
||||
- Press `Meta + E` to pop up the current conversation into an independent window
|
||||
63
docs/content.en/docs/getting-started/Extension.md
Normal file
@@ -0,0 +1,63 @@
|
||||
---
|
||||
weight: 5
|
||||
title: Extension
|
||||
---
|
||||
|
||||
# Extension
|
||||
|
||||
Extensions of Coco AI are plug-in modules that add specific functions to the core system. By installing extensions, you can greatly enhance the capabilities of Coco AI and create a personalized intelligent working environment.
|
||||
|
||||
{{% load-img "/img/core-features/extension_01.png" "" %}}
|
||||
|
||||
|
||||
|
||||
## How to Install Extensions
|
||||
|
||||
#### Install via Extension Store
|
||||
|
||||
In the Extension Store, you can browse or search for the required extensions. After finding the desired extension, press `↵` to view details, and click the install button on the details page. Coco AI will automatically complete the download and installation process.
|
||||
|
||||
{{% load-img "/img/core-features/extension_02.png" "" %}}
|
||||
|
||||
|
||||
|
||||
## How to Use Extensions
|
||||
|
||||
After installing an extension, you can call it through the unified search box.
|
||||
|
||||
#### Command-type Extensions (Commands)
|
||||
|
||||
In search mode, enter the command name or keywords, select the corresponding command from the search results, and press Enter to execute it.
|
||||
|
||||
|
||||
|
||||
#### View-type Extensions (Views)
|
||||
|
||||
View-type extensions provide a complete user interface, embedding visual applications in Coco AI, which can display complex information and offer rich interactive experiences.
|
||||
|
||||
In search mode, enter the extension name or keywords, select the corresponding extension from the search results, and press `↵` to enter the corresponding extension's interaction interface.
|
||||
|
||||
{{% load-img "/img/core-features/extension_04.png" "" %}}
|
||||
|
||||
|
||||
|
||||
## Extension Management
|
||||
|
||||
#### View Installed Extensions
|
||||
|
||||
Open Settings (shortcut key: `Meta+,`). On the Extensions page, you can:
|
||||
|
||||
- Filter by type
|
||||
- Check the extension status (enabled/disabled)
|
||||
- View and modify extension configurations
|
||||
- Uninstall extensions
|
||||
- Set extension command shortcuts or aliases
|
||||
|
||||
{{% load-img "/img/core-features/extension_03.png" "" %}}
|
||||
|
||||
|
||||
|
||||
|
||||
#### Uninstall Extensions
|
||||
|
||||
On the Extensions page in Settings, select the extension you want to uninstall. On the right side of the extension title in the details section, click the `…` button and select Uninstall.
|
||||
87
docs/content.en/docs/getting-started/Keyboard Shortcuts.md
Normal file
@@ -0,0 +1,87 @@
|
||||
---
|
||||
weight: 6
|
||||
title: Keyboard Shortcuts
|
||||
---
|
||||
|
||||
# Keyboard Shortcuts
|
||||
|
||||
Coco AI provides an intuitive set of keyboard shortcuts to help you navigate efficiently, execute commands, switch modes, and manage conversations. Mastering these shortcuts can greatly enhance your user experience.
|
||||
|
||||
|
||||
|
||||
|
||||
## You don't need to memorize the shortcuts
|
||||
|
||||
Simply go to **Settings** (shortcut: `Meta + ,`) → **General → Tooltip**, and turn on the shortcut hint switch. After enabling, when you hold down the modifier key, the corresponding shortcut hints will be displayed in real-time in each functional area of the interface.
|
||||
|
||||
{{% load-img "/img/core-features/shortcuts_01.png" "" %}}
|
||||
|
||||
{{% load-img "/img/core-features/shortcuts_03.png" "" %}}
|
||||
|
||||
|
||||
## Global Shortcuts
|
||||
|
||||
These shortcuts work across any interface, helping you quickly access Coco AI's core functions:
|
||||
|
||||
- `Shift + Meta + Space` Open the Coco AI window
|
||||
|
||||
- `Meta + T` Switch between search/conversation modes
|
||||
|
||||
- `Meta + I` Return to the input box
|
||||
|
||||
- `Meta + P` Pin the window, keeping the Coco AI window displayed at the front of the desktop
|
||||
|
||||
- `Meta + ,` Open the Coco AI settings page
|
||||
|
||||
- `Esc` Close the Coco AI window
|
||||
|
||||
|
||||
|
||||
|
||||
## Search Mode Shortcuts
|
||||
|
||||
In Coco AI's search mode, keyboard shortcuts can help you browse and filter search results more efficiently:
|
||||
|
||||
- `Enter` Open the selected result
|
||||
|
||||
- `Meta + Number` Select the result corresponding to the number and open it
|
||||
|
||||
- `Meta + K` View actionable items for the selected result
|
||||
|
||||
- `Tab` Use the data source of the current result as a filter condition
|
||||
|
||||
- `Arrow Up / Down` Select search results up and down
|
||||
|
||||
- `Meta + Arrow Up / Down` Quickly jump to the first result of the upper/lower category
|
||||
|
||||
|
||||
|
||||
|
||||
## Chat Mode Shortcuts
|
||||
|
||||
In Coco AI's chat mode, keyboard shortcuts can help you quickly switch assistants, control conversations, and input information:
|
||||
|
||||
- `Enter` Send a message
|
||||
|
||||
- `Shift + Enter` Enter a new line
|
||||
|
||||
- `Meta + N` Create a new conversation
|
||||
|
||||
- `Meta + Y` View conversation history
|
||||
|
||||
- `Meta + U` Switch assistants
|
||||
|
||||
- `Meta + S` Switch Coco Server
|
||||
|
||||
- `Meta + E` Pop out the current conversation into an independent window
|
||||
|
||||
|
||||
|
||||
|
||||
## Custom Shortcuts
|
||||
|
||||
Coco AI allows you to customize shortcuts in the settings to adjust according to your personal needs.
|
||||
|
||||
Simply go to **Settings** (shortcut: `Meta + ,`) → **Advanced → Keyboard Shortcuts** to modify the default shortcuts.
|
||||
|
||||
{{% load-img "/img/core-features/shortcuts_02.png" "" %}}
|
||||
69
docs/content.en/docs/getting-started/Search.md
Normal file
@@ -0,0 +1,69 @@
|
||||
---
|
||||
weight: 3
|
||||
title: Search
|
||||
---
|
||||
|
||||
# Search
|
||||
|
||||
Coco AI's search function is designed to provide a unified, intelligent, and efficient cross-platform information retrieval experience. In search mode, you can quickly find local files, applications, commands, extensions, data sources in Coco Server (including Google Drive, Notion, Yuque, Hugo sites, RSS, Github, Postgres, etc.), and AI assistants through the search box.
|
||||
|
||||
{{% load-img "/img/core-features/search_02.png" "" %}}
|
||||
|
||||
|
||||
|
||||
## Search Entrance
|
||||
|
||||
- Open the Coco AI interface using the global shortcut (default: `Shift + Meta + Space`).
|
||||
- The interface is in search mode (switch modes using the toggle button or the shortcut `Meta + T`).
|
||||
- Enter keywords, file names, or natural language questions in the input box.
|
||||
|
||||
{{% load-img "/img/core-features/search_01.png" "" %}}
|
||||
|
||||
|
||||
|
||||
## Search Results
|
||||
|
||||
Coco AI will automatically return a set of concise, structured search results after you enter keywords.
|
||||
|
||||
|
||||
|
||||
#### Default Display Rules
|
||||
|
||||
- By default, the **first 10 results** are displayed.
|
||||
- When results come from multiple data sources (such as Hugo sites, Google Drive, local files, etc.), they will be displayed **grouped by data source**.
|
||||
- If there are fewer than 10 results, they will be displayed **without grouping** in a single list.
|
||||
- Each result shows:
|
||||
- **Title** (file name, note name, or conversation title)
|
||||
- **Directory information** (belonging path or location)
|
||||
- Key matching fragments or summaries
|
||||
|
||||
> 💡 **Tip**: Grouping allows you to quickly understand the range of matching content in different data sources, saving time in filtering.
|
||||
|
||||
|
||||
|
||||
#### Quick Filtering and Navigation
|
||||
|
||||
{{% load-img "/img/core-features/search_03.png" "" %}}
|
||||
|
||||
When browsing results, you can use the **Tab key** to quickly filter the currently selected result:
|
||||
|
||||
- After pressing `Tab`, Coco AI will automatically use the data source of the current result as the filtering condition.
|
||||
- The interface will switch to the separate search view of that data source.
|
||||
|
||||
In the single data source search view, Coco AI will display more abundant content, including:
|
||||
|
||||
- **More search results** (no longer limited to 10)
|
||||
- **More file attributes** (size, type, modification time, etc.)
|
||||
- **Content thumbnails** or **preview summaries** to facilitate quick judgment of relevance
|
||||
|
||||
When the selected result is an AI assistant or extension, the Tab key can initiate a quick conversation with the AI assistant or open the extension.
|
||||
|
||||
|
||||
|
||||
#### Interactive Operations
|
||||
|
||||
- Use the `↓↑` arrow keys or mouse to select result items.
|
||||
- Press `Enter` or `Meta + number` to open the result item.
|
||||
- Press `Tab` to filter to the results of that data source or further interact with the selected result.
|
||||
- Press the `Backspace` key to delete input content and return to the previous level.
|
||||
- Press `Esc` to exit the Coco AI interface.
|
||||
48
docs/content.en/docs/getting-started/Settings.md
Normal file
@@ -0,0 +1,48 @@
|
||||
---
|
||||
weight: 7
|
||||
title: Settings
|
||||
---
|
||||
|
||||
# Settings
|
||||
|
||||
In Coco AI, you can adjust various settings of the application according to your personal needs (shortcut: `Meta + ,`) . The settings page is divided into several main sections, allowing you to easily manage startup items, shortcuts, extensions, connections, and advanced features.
|
||||
|
||||
|
||||
|
||||
## General
|
||||
|
||||
In the General Settings section, you can adjust Coco AI's startup items, startup shortcuts, interface appearance, and language.
|
||||
|
||||
{{% load-img "/img/core-features/settings_01.png" "" %}}
|
||||
|
||||
|
||||
|
||||
## Extension
|
||||
|
||||
In the Extension Settings, you can view, manage, and configure installed extensions.
|
||||
|
||||
{{% load-img "/img/core-features/settings_02.png" "" %}}
|
||||
|
||||
|
||||
|
||||
## Connect
|
||||
|
||||
In the Connect Settings, you can view and manage connections to Coco Server. This section involves logging in, enabling/disabling, and deleting connected servers.
|
||||
|
||||
{{% load-img "/img/core-features/settings_03.png" "" %}}
|
||||
|
||||
|
||||
|
||||
## Advanced
|
||||
|
||||
In the Advanced Settings, you can configure more detailed options such as startup, connections, shortcuts, and version updates.
|
||||
|
||||
{{% load-img "/img/core-features/settings_04.png" "" %}}
|
||||
|
||||
|
||||
|
||||
## About
|
||||
|
||||
In the About Us section, you can view current version information, access help documentation, and submit feedback.
|
||||
|
||||
{{% load-img "/img/core-features/settings_05.png" "" %}}
|
||||
33
docs/content.en/docs/getting-started/The Basics.md
Normal file
@@ -0,0 +1,33 @@
|
||||
---
|
||||
weight: 2
|
||||
title: The Basics
|
||||
---
|
||||
|
||||
# The Basics
|
||||
|
||||
Coco AI is always ready to help you quickly go from "wanting to ask" to "finding answers". This page will briefly introduce the core concepts and quick start process of Coco AI.
|
||||
|
||||
|
||||
|
||||
## Core Operations
|
||||
|
||||
- Use the global shortcut (default: `Shift + Meta + Space`) to open the Coco AI interface.
|
||||
- In the interface, use the toggle button or shortcut key (default: `Meta + T`) to switch between search and AI chat modes.
|
||||
- In search mode, enter keywords in the input box to search for local files, cloud data sources, applications, commands, etc., then press `Enter` to open them.
|
||||
- In chat mode, select different AI assistants and talk directly to them.
|
||||
- Enhance functions with extensions: such as multimedia control, screenshot, window management, etc.
|
||||
|
||||
|
||||
|
||||
## Quick Start
|
||||
|
||||
The following operations can help you quickly get familiar with Coco AI:
|
||||
|
||||
1. Press the shortcut key `Shift + Meta + Space` to open Coco AI.
|
||||
2. In search mode, enter a keyword (e.g., "project report") in the input box to see Coco AI search for related files.
|
||||
3. In search mode, enter a mathematical operation (e.g., `256*42`) in the input box to view the quick calculation result.
|
||||
4. In search mode, select a search result and press the hotkey `tab` to activate the data source or category filter.
|
||||
5. In search mode, search for and open the Extensions Store, then install an extension.
|
||||
6. In chat mode, enter a question (e.g., "What is Coco AI") in the input box to see the answer from the AI assistant.
|
||||
7. In chat mode, click the icon in the upper right corner of the window (shortcut key: `Meta + E`) to activate the independent window chat.
|
||||
8. In the settings (shortcut key: `Meta + ,`), go to the connection settings to connect to Coco Cloud or your self-deployed Coco Server, so as to access cloud data sources and AI assistants, allowing Coco AI to achieve one-stop search.
|
||||
@@ -1,5 +1,5 @@
|
||||
---
|
||||
weight: 10
|
||||
weight: 1
|
||||
title: "Getting Started"
|
||||
bookCollapseSection: false
|
||||
---
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
---
|
||||
weight: 10
|
||||
weight: 1
|
||||
title: "Installation"
|
||||
bookCollapseSection: true
|
||||
---
|
||||
|
||||
@@ -13,6 +13,12 @@ asciinema: true
|
||||
[x11_protocol]: https://en.wikipedia.org/wiki/X_Window_System
|
||||
[if_x11]: https://unix.stackexchange.com/q/202891/498440
|
||||
|
||||
## Install dependencies
|
||||
|
||||
```sh
|
||||
$ sudo apt-get update
|
||||
$ sudo apt-get install -y libwebkit2gtk-4.1-dev libappindicator3-dev librsvg2-dev patchelf xdg-utils libtracker-sparql-3.0-dev
|
||||
```
|
||||
|
||||
## Go to the download page
|
||||
|
||||
|
||||
@@ -13,14 +13,139 @@ Information about release notes of Coco App is provided here.
|
||||
|
||||
### 🚀 Features
|
||||
|
||||
### 🐛 Bug fix
|
||||
|
||||
- fix: fix the abnormal input height issue #1006
|
||||
- fix: implement custom serialization for Extension.minimum_coco_version #1010
|
||||
|
||||
### ✈️ Improvements
|
||||
|
||||
- chore: show error msg (not err code) when installing exts via deeplink fails #1007
|
||||
|
||||
## 0.9.1 (2025-12-05)
|
||||
|
||||
### ❌ Breaking changes
|
||||
|
||||
### 🚀 Features
|
||||
|
||||
- feat: add selection toolbar window for mac #980
|
||||
- feat: add a heartbeat worker to check Coco server availability #988
|
||||
- feat: selection settings add & delete #992
|
||||
|
||||
### 🐛 Bug fix
|
||||
|
||||
- fix: search_extension should not panic when ext is not found #983
|
||||
- fix: persist configuration settings properly #987
|
||||
|
||||
### ✈️ Improvements
|
||||
|
||||
- chore: write panic message to stdout in panic hook #989
|
||||
- refactor: error handling in install_extension interfaces #995
|
||||
- chore: adjust the position of the compact mode window #997
|
||||
|
||||
## 0.9.0 (2025-11-19)
|
||||
|
||||
### ❌ Breaking changes
|
||||
|
||||
### 🚀 Features
|
||||
|
||||
- feat: support switching groups via keyboard shortcuts #911
|
||||
- feat: support opening logs from about page #915
|
||||
- feat: support moving cursor with home and end keys #918
|
||||
- feat: support pageup/pagedown to navigate search results #920
|
||||
- feat: standardize multi-level menu label structure #925
|
||||
- feat(View Extension): page field now accepts HTTP(s) links #925
|
||||
- feat: return sub-exts when extension type exts themselves are matched #928
|
||||
- feat: open quick ai with modifier key + enter #939
|
||||
- feat: allow navigate back when cursor is at the beginning #940
|
||||
- feat(extension compatibility): minimum_coco_version #946
|
||||
- feat: add compact mode for window #947
|
||||
- feat: advanced settings search debounce & local query source weight #950
|
||||
- feat: add window opacity configuration option #963
|
||||
- feat: add auto collapse delay for compact mode #981
|
||||
|
||||
### 🐛 Bug fix
|
||||
|
||||
- fix: automatic update of service list #913
|
||||
- fix: duplicate chat content #916
|
||||
- fix: resolve pinned window shortcut not working #917
|
||||
- fix: WM ext does not work when operating focused win from another display #919
|
||||
- fix(Window Management): Next/Previous Desktop do not work #926
|
||||
- fix: fix page rapidly flickering issue #935
|
||||
- fix(view extension): broken search bar UI when opening extensions via hotkey #938
|
||||
- fix: allow deletion after selecting all text #943
|
||||
- fix: prevent shaking when switching between chat and search pages #955
|
||||
- fix: prevent duplicate login success messages #977
|
||||
- fix: fix quick ai not continuing conversation #979
|
||||
|
||||
### ✈️ Improvements
|
||||
|
||||
- refactor: improve sorting logic of search results #910
|
||||
- style: add dark drop shadow to images #912
|
||||
- chore: add cross-domain configuration for web component #921
|
||||
- refactor: retry if AXUIElementSetAttributeValue() does not work #924
|
||||
- refactor(calculator): skip evaluation if expr is in form "num => num" #929
|
||||
- chore: use a custom log directory #930
|
||||
- chore: bump tauri_nspanel to v2.1 #933
|
||||
- refactor: show_coco/hide_coco now use NSPanel's function on macOS #933
|
||||
- refactor: procedure that convert_pages() into a func #934
|
||||
- refactor(post-search): collect at least 2 documents from each query source #948
|
||||
- refactor: custom_version_comparator() now compares semantic versions #941
|
||||
- chore: center the main window vertically #959
|
||||
- refactor(view extension): load HTML/resources via local HTTP server #973
|
||||
|
||||
## 0.8.0 (2025-09-28)
|
||||
|
||||
### ❌ Breaking changes
|
||||
|
||||
- chore: update request accesstoken api #866
|
||||
|
||||
### 🚀 Features
|
||||
|
||||
- feat: enhance ui for skipped version #834
|
||||
- feat: support installing local extensions #749
|
||||
- feat: support sending files in chat messages #764
|
||||
- feat: sub extension can set 'platforms' now #847
|
||||
- feat: add extension uninstall option in settings #855
|
||||
- feat: impl extension settings 'hide_before_open' #862
|
||||
- feat: index both en/zh_CN app names and show app name in chosen language #875
|
||||
- feat: support context menu in debug mode #882
|
||||
- feat: file search for Linux/GNOME #884
|
||||
- feat: file search for Linux/KDE #886
|
||||
- feat: extension Window Management for macOS #892
|
||||
- feat: new extension type View #894
|
||||
- feat: support opening file in its containing folder #900
|
||||
|
||||
### 🐛 Bug fix
|
||||
|
||||
- fix: fix issue with update check failure #833
|
||||
- fix: web component login state #857
|
||||
- fix: shortcut key not opening extension store #877
|
||||
- fix: set up hotkey on main thread or Windows will complain #879
|
||||
- fix: resolve deeplink login issue #881
|
||||
- fix: use kill_on_drop() to avoid zombie proc in error case #887
|
||||
- fix: settings window rendering/loading issue 889
|
||||
- fix: ensure search paths are indexed #896
|
||||
- fix: bump applications-rs to fix empty app name issue #898
|
||||
|
||||
### ✈️ Improvements
|
||||
|
||||
- refactor: calling service related interfaces #831
|
||||
- refactor: split query_coco_fusion() #836
|
||||
- chore: web component loading font icon #838
|
||||
- chore: delete unused code files and dependencies #841
|
||||
- chore: ignore tauri::AppHandle's generic argument R #845
|
||||
- refactor: check Extension/plugin.json from all sources #846
|
||||
- refactor: pinning window won't set CanJoinAllSpaces on macOS #854
|
||||
- build: web component build error #858
|
||||
- refactor: coordinate third-party extension operations using lock #867
|
||||
- refactor: index iOS apps and macOS apps that store icon in Assets.car #872
|
||||
- refactor: accept both '-' and '\_' as locale str separator #876
|
||||
- refactor: relax the file search conditions on macOS #883
|
||||
- refactor: ensure Coco won't take focus #891
|
||||
- chore: skip login check for web widget #895
|
||||
- chore: convertFileSrc() "link[href]" and "img[src]" #901
|
||||
|
||||
## 0.7.1 (2025-07-27)
|
||||
|
||||
### ❌ Breaking changes
|
||||
|
||||
BIN
docs/static/img/core-features/ai_chat_01.png
vendored
Normal file
|
After Width: | Height: | Size: 802 KiB |
BIN
docs/static/img/core-features/ai_chat_02.png
vendored
Normal file
|
After Width: | Height: | Size: 846 KiB |
BIN
docs/static/img/core-features/ai_chat_03.png
vendored
Normal file
|
After Width: | Height: | Size: 785 KiB |
BIN
docs/static/img/core-features/ai_chat_04.png
vendored
Normal file
|
After Width: | Height: | Size: 806 KiB |
BIN
docs/static/img/core-features/ai_overview_01.png
vendored
Normal file
|
After Width: | Height: | Size: 836 KiB |
BIN
docs/static/img/core-features/ai_overview_02.png
vendored
Normal file
|
After Width: | Height: | Size: 657 KiB |
BIN
docs/static/img/core-features/application_search_01.png
vendored
Normal file
|
After Width: | Height: | Size: 851 KiB |
BIN
docs/static/img/core-features/application_search_02.png
vendored
Normal file
|
After Width: | Height: | Size: 674 KiB |
BIN
docs/static/img/core-features/basics_02.png
vendored
Normal file
|
After Width: | Height: | Size: 777 KiB |
BIN
docs/static/img/core-features/calculator_01.png
vendored
Normal file
|
After Width: | Height: | Size: 845 KiB |
BIN
docs/static/img/core-features/calculator_02.png
vendored
Normal file
|
After Width: | Height: | Size: 854 KiB |
BIN
docs/static/img/core-features/extension_01.png
vendored
Normal file
|
After Width: | Height: | Size: 849 KiB |
BIN
docs/static/img/core-features/extension_02.png
vendored
Normal file
|
After Width: | Height: | Size: 840 KiB |
BIN
docs/static/img/core-features/extension_03.png
vendored
Normal file
|
After Width: | Height: | Size: 650 KiB |
BIN
docs/static/img/core-features/extension_04.png
vendored
Normal file
|
After Width: | Height: | Size: 694 KiB |
BIN
docs/static/img/core-features/filesearch_01.png
vendored
Normal file
|
After Width: | Height: | Size: 878 KiB |
BIN
docs/static/img/core-features/filesearch_02.png
vendored
Normal file
|
After Width: | Height: | Size: 671 KiB |
BIN
docs/static/img/core-features/quick_ai_access_01.png
vendored
Normal file
|
After Width: | Height: | Size: 896 KiB |
BIN
docs/static/img/core-features/quick_ai_access_02.png
vendored
Normal file
|
After Width: | Height: | Size: 826 KiB |
BIN
docs/static/img/core-features/quick_ai_access_03.png
vendored
Normal file
|
After Width: | Height: | Size: 654 KiB |
BIN
docs/static/img/core-features/search_01.png
vendored
Normal file
|
After Width: | Height: | Size: 849 KiB |
BIN
docs/static/img/core-features/search_02.png
vendored
Normal file
|
After Width: | Height: | Size: 871 KiB |
BIN
docs/static/img/core-features/search_03.png
vendored
Normal file
|
After Width: | Height: | Size: 876 KiB |
BIN
docs/static/img/core-features/settings_01.png
vendored
Normal file
|
After Width: | Height: | Size: 611 KiB |
BIN
docs/static/img/core-features/settings_02.png
vendored
Normal file
|
After Width: | Height: | Size: 637 KiB |
BIN
docs/static/img/core-features/settings_03.png
vendored
Normal file
|
After Width: | Height: | Size: 721 KiB |
BIN
docs/static/img/core-features/settings_04.png
vendored
Normal file
|
After Width: | Height: | Size: 631 KiB |
BIN
docs/static/img/core-features/settings_05.png
vendored
Normal file
|
After Width: | Height: | Size: 586 KiB |
BIN
docs/static/img/core-features/shortcuts_01.png
vendored
Normal file
|
After Width: | Height: | Size: 620 KiB |
BIN
docs/static/img/core-features/shortcuts_02.png
vendored
Normal file
|
After Width: | Height: | Size: 613 KiB |
BIN
docs/static/img/core-features/shortcuts_03.png
vendored
Normal file
|
After Width: | Height: | Size: 832 KiB |
BIN
docs/static/img/core-features/window_management_01.png
vendored
Normal file
|
After Width: | Height: | Size: 874 KiB |
BIN
docs/static/img/core-features/window_management_02.png
vendored
Normal file
|
After Width: | Height: | Size: 646 KiB |
14
package.json
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "coco",
|
||||
"private": true,
|
||||
"version": "0.7.1",
|
||||
"version": "0.9.1",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "vite",
|
||||
@@ -19,23 +19,28 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@headlessui/react": "^2.2.2",
|
||||
"@infinilabs/custom-icons": "0.0.4",
|
||||
"@radix-ui/react-separator": "^1.1.8",
|
||||
"@radix-ui/react-slot": "^1.2.3",
|
||||
"@tauri-apps/api": "^2.5.0",
|
||||
"@tauri-apps/plugin-autostart": "~2.2.0",
|
||||
"@tauri-apps/plugin-clipboard-manager": "~2.3.2",
|
||||
"@tauri-apps/plugin-deep-link": "^2.2.1",
|
||||
"@tauri-apps/plugin-dialog": "^2.2.1",
|
||||
"@tauri-apps/plugin-global-shortcut": "~2.0.0",
|
||||
"@tauri-apps/plugin-http": "~2.0.2",
|
||||
"@tauri-apps/plugin-log": "~2.4.0",
|
||||
"@tauri-apps/plugin-opener": "^2.2.7",
|
||||
"@tauri-apps/plugin-opener": "^2.5.0",
|
||||
"@tauri-apps/plugin-os": "^2.2.1",
|
||||
"@tauri-apps/plugin-process": "^2.2.1",
|
||||
"@tauri-apps/plugin-shell": "^2.2.1",
|
||||
"@tauri-apps/plugin-updater": "github:infinilabs/tauri-plugin-updater#v2",
|
||||
"@tauri-apps/plugin-websocket": "~2.3.0",
|
||||
"@tauri-apps/plugin-window": "2.0.0-alpha.1",
|
||||
"@tauri-store/zustand": "^1.1.0",
|
||||
"@wavesurfer/react": "^1.0.11",
|
||||
"ahooks": "^3.8.4",
|
||||
"axios": "^1.9.0",
|
||||
"axios": "^1.12.0",
|
||||
"class-variance-authority": "^0.7.1",
|
||||
"clsx": "^2.1.1",
|
||||
"dayjs": "^1.11.13",
|
||||
"dotenv": "^16.5.0",
|
||||
@@ -60,6 +65,7 @@
|
||||
"remark-gfm": "^4.0.1",
|
||||
"remark-math": "^6.0.0",
|
||||
"tailwind-merge": "^3.3.1",
|
||||
"tailwindcss-animate": "^1.0.7",
|
||||
"tauri-plugin-fs-pro-api": "^2.4.0",
|
||||
"tauri-plugin-macos-permissions-api": "^2.3.0",
|
||||
"tauri-plugin-screenshots-api": "^2.2.0",
|
||||
|
||||
1231
pnpm-lock.yaml
generated
3976
src-tauri/Cargo.lock
generated
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "coco"
|
||||
version = "0.7.1"
|
||||
version = "0.9.1"
|
||||
description = "Search, connect, collaborate – all in one place."
|
||||
authors = ["INFINI Labs"]
|
||||
edition = "2024"
|
||||
@@ -15,6 +15,7 @@ crate-type = ["staticlib", "cdylib", "rlib"]
|
||||
|
||||
[build-dependencies]
|
||||
tauri-build = { version = "2", features = ["default"] }
|
||||
cfg-if = "1.0.1"
|
||||
|
||||
[features]
|
||||
default = ["desktop"]
|
||||
@@ -51,7 +52,6 @@ serde = { version = "1", features = ["derive"] }
|
||||
# see: https://docs.rs/serde_json/latest/serde_json/struct.Number.html#method.from_u128
|
||||
serde_json = { version = "1", features = ["arbitrary_precision", "preserve_order"] }
|
||||
tauri-plugin-http = "2"
|
||||
tauri-plugin-websocket = "2"
|
||||
tauri-plugin-deep-link = "2.0.0"
|
||||
tauri-plugin-store = "2.2.0"
|
||||
tauri-plugin-os = "2"
|
||||
@@ -62,7 +62,7 @@ tauri-plugin-drag = "2"
|
||||
tauri-plugin-macos-permissions = "2"
|
||||
tauri-plugin-fs-pro = "2"
|
||||
tauri-plugin-screenshots = "2"
|
||||
applications = { git = "https://github.com/infinilabs/applications-rs", rev = "31b0c030a0f3bc82275fe12debe526153978671d" }
|
||||
applications = { git = "https://github.com/infinilabs/applications-rs", rev = "b5fac4034a40d42e72f727f1aa1cc1f19fe86653" }
|
||||
tokio-native-tls = "0.3" # For wss connections
|
||||
tokio = { version = "1", features = ["full"] }
|
||||
tokio-tungstenite = { version = "0.20", features = ["native-tls"] }
|
||||
@@ -104,15 +104,49 @@ zip = "4.0.0"
|
||||
url = "2.5.2"
|
||||
camino = "1.1.10"
|
||||
tokio-stream = { version = "0.1.17", features = ["io-util"] }
|
||||
cfg-if = "1.0.1"
|
||||
sysinfo = "0.35.2"
|
||||
indexmap = { version = "2.10.0", features = ["serde"] }
|
||||
strum = { version = "0.27.2", features = ["derive"] }
|
||||
sys-locale = "0.3.2"
|
||||
tauri-plugin-prevent-default = "1"
|
||||
oneshot = "0.1.11"
|
||||
bitflags = "2.9.3"
|
||||
cfg-if = "1.0.1"
|
||||
dunce = "1.0.5"
|
||||
urlencoding = "2.1.3"
|
||||
scraper = "0.17"
|
||||
toml = "0.8"
|
||||
path-clean = "1.0.1"
|
||||
actix-files = "0.6.8"
|
||||
actix-web = "4.11.0"
|
||||
tauri-plugin-clipboard-manager = "2"
|
||||
tauri-plugin-zustand = "1"
|
||||
snafu = "0.8.9"
|
||||
|
||||
[dev-dependencies]
|
||||
tempfile = "3.23.0"
|
||||
|
||||
[target."cfg(target_os = \"macos\")".dependencies]
|
||||
tauri-nspanel = { git = "https://github.com/ahkohd/tauri-nspanel", branch = "v2" }
|
||||
cocoa = "0.24"
|
||||
tauri-nspanel = { git = "https://github.com/ahkohd/tauri-nspanel", branch = "v2.1" }
|
||||
objc2-app-kit = { version = "0.3.1", features = ["NSWindow"] }
|
||||
objc2 = "0.6.2"
|
||||
objc2-core-foundation = {version = "0.3.1", features = ["CFString", "CFCGTypes", "CFArray"] }
|
||||
objc2-application-services = { version = "0.3.1", features = ["HIServices"] }
|
||||
objc2-core-graphics = { version = "=0.3.1", features = ["CGEvent"] }
|
||||
# macOS-only: used by selection_monitor.rs to check AX trust/prompt
|
||||
macos-accessibility-client = "0.0.1"
|
||||
|
||||
[target."cfg(target_os = \"linux\")".dependencies]
|
||||
gio = "0.21.2"
|
||||
glib = "0.21.2"
|
||||
tracker-rs = "0.7"
|
||||
which = "8.0.0"
|
||||
configparser = "3.1.0"
|
||||
|
||||
[target."cfg(any(target_os = \"macos\", windows, target_os = \"linux\"))".dependencies]
|
||||
tauri-plugin-single-instance = { version = "2.0.0", features = ["deep-link"] }
|
||||
serde = { version = "1.0.219", features = ["derive"], optional = true }
|
||||
|
||||
|
||||
[profile.dev]
|
||||
incremental = true # Compile your binary in smaller steps.
|
||||
@@ -133,4 +167,8 @@ semver = { version = "1", features = ["serde"] }
|
||||
|
||||
[target."cfg(target_os = \"windows\")".dependencies]
|
||||
enigo="0.3"
|
||||
windows = { version = "0.61.3", features = ["Win32_Foundation", "Win32_System_Com", "Win32_System_Ole", "Win32_System_Search", "Win32_UI_Shell_PropertiesSystem", "Win32_Data"] }
|
||||
windows = { version = "0.61", features = ["Win32_Foundation", "Win32_System_Com", "Win32_System_Ole", "Win32_System_Search", "Win32_UI_Shell_PropertiesSystem", "Win32_Data"] }
|
||||
windows-sys = { version = "0.61", features = ["Win32", "Win32_System", "Win32_System_Com"] }
|
||||
|
||||
[target."cfg(target_os = \"windows\")".build-dependencies]
|
||||
bindgen = "0.72.1"
|
||||
|
||||
@@ -38,5 +38,9 @@
|
||||
<string>Coco AI requires camera access for scanning documents and capturing images.</string>
|
||||
<key>NSSpeechRecognitionUsageDescription</key>
|
||||
<string>Coco AI uses speech recognition to convert your voice into text for a hands-free experience.</string>
|
||||
<key>NSAppleEventsUsageDescription</key>
|
||||
<string>Coco AI requires access to Apple Events to enable certain features, such as opening files and applications.</string>
|
||||
<key>NSAccessibility</key>
|
||||
<true/>
|
||||
</dict>
|
||||
</plist>
|
||||
@@ -11,4 +11,32 @@ fn main() {
|
||||
//
|
||||
// unexpected condition name: `ci`
|
||||
println!("cargo::rustc-check-cfg=cfg(ci)");
|
||||
|
||||
// Bindgen searchapi.h on Windows as the windows create does not provide
|
||||
// bindings for it
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(target_os = "windows")] {
|
||||
use std::env;
|
||||
use std::path::PathBuf;
|
||||
|
||||
let wrapper_header = r#"#include <windows.h>
|
||||
#include <searchapi.h>"#;
|
||||
|
||||
let searchapi_bindings = bindgen::Builder::default()
|
||||
.header_contents("wrapper.h", wrapper_header)
|
||||
.generate()
|
||||
.expect("failed to generate bindings for <searchapi.h>");
|
||||
|
||||
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
|
||||
searchapi_bindings
|
||||
.write_to_file(out_path.join("searchapi_bindings.rs"))
|
||||
.expect("couldn't write bindings to <OUT_DIR/searchapi_bindings.rs>")
|
||||
|
||||
// Looks like there is no need to link the library that contains the
|
||||
// implementation of functions declared in 'searchapi.h' manually as
|
||||
// the FFI bindings work (without doing that).
|
||||
//
|
||||
// This is wield, I do not expect the linker will link it automatically.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
"$schema": "../gen/schemas/desktop-schema.json",
|
||||
"identifier": "default",
|
||||
"description": "Capability for the main window",
|
||||
"windows": ["main", "chat", "settings", "check"],
|
||||
"windows": ["main", "chat", "settings", "check", "selection"],
|
||||
"permissions": [
|
||||
"core:default",
|
||||
"core:event:allow-emit",
|
||||
@@ -30,6 +30,7 @@
|
||||
"core:window:allow-set-always-on-top",
|
||||
"core:window:deny-internal-toggle-maximize",
|
||||
"core:window:allow-set-shadow",
|
||||
"core:window:allow-set-position",
|
||||
"core:app:allow-set-app-theme",
|
||||
"shell:default",
|
||||
"http:default",
|
||||
@@ -37,9 +38,6 @@
|
||||
"http:allow-fetch-cancel",
|
||||
"http:allow-fetch-read-body",
|
||||
"http:allow-fetch-send",
|
||||
"websocket:default",
|
||||
"websocket:allow-connect",
|
||||
"websocket:allow-send",
|
||||
"autostart:allow-enable",
|
||||
"autostart:allow-disable",
|
||||
"autostart:allow-is-enabled",
|
||||
@@ -72,6 +70,7 @@
|
||||
"updater:default",
|
||||
"windows-version:default",
|
||||
"log:default",
|
||||
"opener:default"
|
||||
"opener:default",
|
||||
"core:window:allow-unminimize"
|
||||
]
|
||||
}
|
||||
|
||||
5
src-tauri/capabilities/zustand.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"identifier": "zustand",
|
||||
"windows": ["*"],
|
||||
"permissions": ["zustand:default", "core:event:default"]
|
||||
}
|
||||
@@ -1,25 +1,26 @@
|
||||
use crate::common::assistant::ChatRequestMessage;
|
||||
use crate::common::http::{GetResponse, convert_query_params_to_strings};
|
||||
use crate::common::http::convert_query_params_to_strings;
|
||||
use crate::common::register::SearchSourceRegistry;
|
||||
use crate::server::http_client::HttpClient;
|
||||
use crate::server::http_client::{DecodeResponseSnafu, HttpClient, HttpRequestError};
|
||||
use crate::{common, server::servers::COCO_SERVERS};
|
||||
use futures::StreamExt;
|
||||
use futures::stream::FuturesUnordered;
|
||||
use futures_util::TryStreamExt;
|
||||
use http::Method;
|
||||
use serde_json::Value;
|
||||
use snafu::ResultExt;
|
||||
use std::collections::HashMap;
|
||||
use tauri::{AppHandle, Emitter, Manager, Runtime};
|
||||
use tauri::{AppHandle, Emitter, Manager};
|
||||
use tokio::io::AsyncBufReadExt;
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn chat_history<R: Runtime>(
|
||||
_app_handle: AppHandle<R>,
|
||||
pub async fn chat_history(
|
||||
_app_handle: AppHandle,
|
||||
server_id: String,
|
||||
from: u32,
|
||||
size: u32,
|
||||
query: Option<String>,
|
||||
) -> Result<String, String> {
|
||||
) -> Result<String, HttpRequestError> {
|
||||
let mut query_params = Vec::new();
|
||||
|
||||
// Add from/size as number values
|
||||
@@ -32,24 +33,19 @@ pub async fn chat_history<R: Runtime>(
|
||||
}
|
||||
}
|
||||
|
||||
let response = HttpClient::get(&server_id, "/chat/_history", Some(query_params))
|
||||
.await
|
||||
.map_err(|e| {
|
||||
dbg!("Error get history: {}", &e);
|
||||
format!("Error get history: {}", e)
|
||||
})?;
|
||||
let response = HttpClient::get(&server_id, "/chat/_history", Some(query_params)).await?;
|
||||
|
||||
common::http::get_response_body_text(response).await
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn session_chat_history<R: Runtime>(
|
||||
_app_handle: AppHandle<R>,
|
||||
pub async fn session_chat_history(
|
||||
_app_handle: AppHandle,
|
||||
server_id: String,
|
||||
session_id: String,
|
||||
from: u32,
|
||||
size: u32,
|
||||
) -> Result<String, String> {
|
||||
) -> Result<String, HttpRequestError> {
|
||||
let mut query_params = Vec::new();
|
||||
|
||||
// Add from/size as number values
|
||||
@@ -58,126 +54,83 @@ pub async fn session_chat_history<R: Runtime>(
|
||||
|
||||
let path = format!("/chat/{}/_history", session_id);
|
||||
|
||||
let response = HttpClient::get(&server_id, path.as_str(), Some(query_params))
|
||||
.await
|
||||
.map_err(|e| format!("Error get session message: {}", e))?;
|
||||
let response = HttpClient::get(&server_id, path.as_str(), Some(query_params)).await?;
|
||||
|
||||
common::http::get_response_body_text(response).await
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn open_session_chat<R: Runtime>(
|
||||
_app_handle: AppHandle<R>,
|
||||
pub async fn open_session_chat(
|
||||
_app_handle: AppHandle,
|
||||
server_id: String,
|
||||
session_id: String,
|
||||
) -> Result<String, String> {
|
||||
) -> Result<String, HttpRequestError> {
|
||||
let path = format!("/chat/{}/_open", session_id);
|
||||
|
||||
let response = HttpClient::post(&server_id, path.as_str(), None, None)
|
||||
.await
|
||||
.map_err(|e| format!("Error open session: {}", e))?;
|
||||
let response = HttpClient::post(&server_id, path.as_str(), None, None).await?;
|
||||
|
||||
common::http::get_response_body_text(response).await
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn close_session_chat<R: Runtime>(
|
||||
_app_handle: AppHandle<R>,
|
||||
pub async fn close_session_chat(
|
||||
_app_handle: AppHandle,
|
||||
server_id: String,
|
||||
session_id: String,
|
||||
) -> Result<String, String> {
|
||||
) -> Result<String, HttpRequestError> {
|
||||
let path = format!("/chat/{}/_close", session_id);
|
||||
|
||||
let response = HttpClient::post(&server_id, path.as_str(), None, None)
|
||||
.await
|
||||
.map_err(|e| format!("Error close session: {}", e))?;
|
||||
let response = HttpClient::post(&server_id, path.as_str(), None, None).await?;
|
||||
|
||||
common::http::get_response_body_text(response).await
|
||||
}
|
||||
#[tauri::command]
|
||||
pub async fn cancel_session_chat<R: Runtime>(
|
||||
_app_handle: AppHandle<R>,
|
||||
pub async fn cancel_session_chat(
|
||||
_app_handle: AppHandle,
|
||||
server_id: String,
|
||||
session_id: String,
|
||||
query_params: Option<HashMap<String, Value>>,
|
||||
) -> Result<String, String> {
|
||||
) -> Result<String, HttpRequestError> {
|
||||
let path = format!("/chat/{}/_cancel", session_id);
|
||||
let query_params = convert_query_params_to_strings(query_params);
|
||||
|
||||
let response = HttpClient::post(&server_id, path.as_str(), query_params, None)
|
||||
.await
|
||||
.map_err(|e| format!("Error cancel session: {}", e))?;
|
||||
let response = HttpClient::post(&server_id, path.as_str(), query_params, None).await?;
|
||||
|
||||
common::http::get_response_body_text(response).await
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn new_chat<R: Runtime>(
|
||||
_app_handle: AppHandle<R>,
|
||||
pub async fn chat_create(
|
||||
app_handle: AppHandle,
|
||||
server_id: String,
|
||||
websocket_id: String,
|
||||
message: String,
|
||||
query_params: Option<HashMap<String, Value>>,
|
||||
) -> Result<GetResponse, String> {
|
||||
let body = if !message.is_empty() {
|
||||
let message = ChatRequestMessage {
|
||||
message: Some(message),
|
||||
};
|
||||
Some(
|
||||
serde_json::to_string(&message)
|
||||
.map_err(|e| format!("Failed to serialize message: {}", e))?
|
||||
.into(),
|
||||
)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let mut headers = HashMap::new();
|
||||
headers.insert("WEBSOCKET-SESSION-ID".to_string(), websocket_id.into());
|
||||
|
||||
let response = HttpClient::advanced_post(
|
||||
&server_id,
|
||||
"/chat/_new",
|
||||
Some(headers),
|
||||
convert_query_params_to_strings(query_params),
|
||||
body,
|
||||
)
|
||||
.await
|
||||
.map_err(|e| format!("Error sending message: {}", e))?;
|
||||
|
||||
let body_text = common::http::get_response_body_text(response).await?;
|
||||
|
||||
log::debug!("New chat response: {}", &body_text);
|
||||
|
||||
let chat_response: GetResponse = serde_json::from_str(&body_text)
|
||||
.map_err(|e| format!("Failed to parse response JSON: {}", e))?;
|
||||
|
||||
if chat_response.result != "created" {
|
||||
return Err(format!("Unexpected result: {}", chat_response.result));
|
||||
}
|
||||
|
||||
Ok(chat_response)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn chat_create<R: Runtime>(
|
||||
app_handle: AppHandle<R>,
|
||||
server_id: String,
|
||||
message: String,
|
||||
message: Option<String>,
|
||||
attachments: Option<Vec<String>>,
|
||||
query_params: Option<HashMap<String, Value>>,
|
||||
client_id: String,
|
||||
) -> Result<(), String> {
|
||||
let body = if !message.is_empty() {
|
||||
let message = ChatRequestMessage {
|
||||
message: Some(message),
|
||||
println!("chat_create message: {:?}", message);
|
||||
println!("chat_create attachments: {:?}", attachments);
|
||||
|
||||
let message_empty = message.as_ref().map_or(true, |m| m.is_empty());
|
||||
let attachments_empty = attachments.as_ref().map_or(true, |a| a.is_empty());
|
||||
|
||||
if message_empty && attachments_empty {
|
||||
return Err("Message and attachments are empty".to_string());
|
||||
}
|
||||
|
||||
let body = {
|
||||
let request_message: ChatRequestMessage = ChatRequestMessage {
|
||||
message,
|
||||
attachments,
|
||||
};
|
||||
|
||||
println!("chat_create body: {:?}", request_message);
|
||||
|
||||
Some(
|
||||
serde_json::to_string(&message)
|
||||
serde_json::to_string(&request_message)
|
||||
.map_err(|e| format!("Failed to serialize message: {}", e))?
|
||||
.into(),
|
||||
)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let response = HttpClient::advanced_post(
|
||||
@@ -213,8 +166,6 @@ pub async fn chat_create<R: Runtime>(
|
||||
if let Err(err) = app_handle.emit(&client_id, line) {
|
||||
log::error!("Emit failed: {:?}", err);
|
||||
|
||||
print!("Error sending message: {:?}", err);
|
||||
|
||||
let _ = app_handle.emit("chat-create-error", format!("Emit failed: {:?}", err));
|
||||
}
|
||||
}
|
||||
@@ -223,56 +174,38 @@ pub async fn chat_create<R: Runtime>(
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn send_message<R: Runtime>(
|
||||
_app_handle: AppHandle<R>,
|
||||
server_id: String,
|
||||
websocket_id: String,
|
||||
session_id: String,
|
||||
message: String,
|
||||
query_params: Option<HashMap<String, Value>>, //search,deep_thinking
|
||||
) -> Result<String, String> {
|
||||
let path = format!("/chat/{}/_send", session_id);
|
||||
let msg = ChatRequestMessage {
|
||||
message: Some(message),
|
||||
};
|
||||
|
||||
let mut headers = HashMap::new();
|
||||
headers.insert("WEBSOCKET-SESSION-ID".to_string(), websocket_id.into());
|
||||
|
||||
let body = reqwest::Body::from(serde_json::to_string(&msg).unwrap());
|
||||
let response = HttpClient::advanced_post(
|
||||
&server_id,
|
||||
path.as_str(),
|
||||
Some(headers),
|
||||
convert_query_params_to_strings(query_params),
|
||||
Some(body),
|
||||
)
|
||||
.await
|
||||
.map_err(|e| format!("Error cancel session: {}", e))?;
|
||||
|
||||
common::http::get_response_body_text(response).await
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn chat_chat<R: Runtime>(
|
||||
app_handle: AppHandle<R>,
|
||||
pub async fn chat_chat(
|
||||
app_handle: AppHandle,
|
||||
server_id: String,
|
||||
session_id: String,
|
||||
message: String,
|
||||
message: Option<String>,
|
||||
attachments: Option<Vec<String>>,
|
||||
query_params: Option<HashMap<String, Value>>, //search,deep_thinking
|
||||
client_id: String,
|
||||
) -> Result<(), String> {
|
||||
let body = if !message.is_empty() {
|
||||
let message = ChatRequestMessage {
|
||||
message: Some(message),
|
||||
println!("chat_chat message: {:?}", message);
|
||||
println!("chat_chat attachments: {:?}", attachments);
|
||||
|
||||
let message_empty = message.as_ref().map_or(true, |m| m.is_empty());
|
||||
let attachments_empty = attachments.as_ref().map_or(true, |a| a.is_empty());
|
||||
|
||||
if message_empty && attachments_empty {
|
||||
return Err("Message and attachments are empty".to_string());
|
||||
}
|
||||
|
||||
let body = {
|
||||
let request_message = ChatRequestMessage {
|
||||
message,
|
||||
attachments,
|
||||
};
|
||||
|
||||
println!("chat_chat body: {:?}", request_message);
|
||||
|
||||
Some(
|
||||
serde_json::to_string(&message)
|
||||
serde_json::to_string(&request_message)
|
||||
.map_err(|e| format!("Failed to serialize message: {}", e))?
|
||||
.into(),
|
||||
)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let path = format!("/chat/{}/_chat", session_id);
|
||||
@@ -314,6 +247,9 @@ pub async fn chat_chat<R: Runtime>(
|
||||
|
||||
if let Err(err) = app_handle.emit(&client_id, line) {
|
||||
log::error!("Emit failed: {:?}", err);
|
||||
|
||||
print!("Error sending message: {:?}", err);
|
||||
|
||||
let _ = app_handle.emit("chat-create-error", format!("Emit failed: {:?}", err));
|
||||
}
|
||||
}
|
||||
@@ -322,14 +258,23 @@ pub async fn chat_chat<R: Runtime>(
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn delete_session_chat(server_id: String, session_id: String) -> Result<bool, String> {
|
||||
pub async fn delete_session_chat(
|
||||
server_id: String,
|
||||
session_id: String,
|
||||
) -> Result<bool, HttpRequestError> {
|
||||
let response =
|
||||
HttpClient::delete(&server_id, &format!("/chat/{}", session_id), None, None).await?;
|
||||
|
||||
if response.status().is_success() {
|
||||
let status = response.status();
|
||||
|
||||
if status.is_success() {
|
||||
Ok(true)
|
||||
} else {
|
||||
Err(format!("Delete failed with status: {}", response.status()))
|
||||
Err(HttpRequestError::RequestFailed {
|
||||
status: status.as_u16(),
|
||||
error_response_body_str: None,
|
||||
coco_server_api_error_response_body: None,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -339,7 +284,7 @@ pub async fn update_session_chat(
|
||||
session_id: String,
|
||||
title: Option<String>,
|
||||
context: Option<HashMap<String, Value>>,
|
||||
) -> Result<bool, String> {
|
||||
) -> Result<bool, HttpRequestError> {
|
||||
let mut body = HashMap::new();
|
||||
if let Some(title) = title {
|
||||
body.insert("title".to_string(), Value::String(title));
|
||||
@@ -358,46 +303,36 @@ pub async fn update_session_chat(
|
||||
None,
|
||||
Some(reqwest::Body::from(serde_json::to_string(&body).unwrap())),
|
||||
)
|
||||
.await
|
||||
.map_err(|e| format!("Error updating session: {}", e))?;
|
||||
.await?;
|
||||
|
||||
Ok(response.status().is_success())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn assistant_search<R: Runtime>(
|
||||
_app_handle: AppHandle<R>,
|
||||
pub async fn assistant_search(
|
||||
_app_handle: AppHandle,
|
||||
server_id: String,
|
||||
query_params: Option<Vec<String>>,
|
||||
) -> Result<Value, String> {
|
||||
let response = HttpClient::post(&server_id, "/assistant/_search", query_params, None)
|
||||
.await
|
||||
.map_err(|e| format!("Error searching assistants: {}", e))?;
|
||||
) -> Result<Value, HttpRequestError> {
|
||||
let response = HttpClient::post(&server_id, "/assistant/_search", query_params, None).await?;
|
||||
|
||||
response
|
||||
.json::<Value>()
|
||||
.await
|
||||
.map_err(|err| err.to_string())
|
||||
response.json::<Value>().await.context(DecodeResponseSnafu)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn assistant_get<R: Runtime>(
|
||||
_app_handle: AppHandle<R>,
|
||||
pub async fn assistant_get(
|
||||
_app_handle: AppHandle,
|
||||
server_id: String,
|
||||
assistant_id: String,
|
||||
) -> Result<Value, String> {
|
||||
) -> Result<Value, HttpRequestError> {
|
||||
let response = HttpClient::get(
|
||||
&server_id,
|
||||
&format!("/assistant/{}", assistant_id),
|
||||
None, // headers
|
||||
)
|
||||
.await
|
||||
.map_err(|e| format!("Error getting assistant: {}", e))?;
|
||||
.await?;
|
||||
|
||||
response
|
||||
.json::<Value>()
|
||||
.await
|
||||
.map_err(|err| err.to_string())
|
||||
response.json::<Value>().await.context(DecodeResponseSnafu)
|
||||
}
|
||||
|
||||
/// Gets the information of the assistant specified by `assistant_id` by querying **all**
|
||||
@@ -405,10 +340,10 @@ pub async fn assistant_get<R: Runtime>(
|
||||
///
|
||||
/// Returns as soon as the assistant is found on any Coco server.
|
||||
#[tauri::command]
|
||||
pub async fn assistant_get_multi<R: Runtime>(
|
||||
app_handle: AppHandle<R>,
|
||||
pub async fn assistant_get_multi(
|
||||
app_handle: AppHandle,
|
||||
assistant_id: String,
|
||||
) -> Result<Value, String> {
|
||||
) -> Result<Option<Value>, HttpRequestError> {
|
||||
let search_sources = app_handle.state::<SearchSourceRegistry>();
|
||||
let sources_future = search_sources.get_sources();
|
||||
let sources_list = sources_future.await;
|
||||
@@ -427,19 +362,17 @@ pub async fn assistant_get_multi<R: Runtime>(
|
||||
let path = format!("/assistant/{}", assistant_id);
|
||||
|
||||
let fut = async move {
|
||||
let res_response = HttpClient::get(
|
||||
let response = HttpClient::get(
|
||||
&coco_server_id,
|
||||
&path,
|
||||
None, // headers
|
||||
)
|
||||
.await;
|
||||
match res_response {
|
||||
Ok(response) => response
|
||||
.json::<serde_json::Value>()
|
||||
.await
|
||||
.map_err(|e| e.to_string()),
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
.await?;
|
||||
|
||||
response
|
||||
.json::<serde_json::Value>()
|
||||
.await
|
||||
.context(DecodeResponseSnafu)
|
||||
};
|
||||
|
||||
futures.push(fut);
|
||||
@@ -471,15 +404,12 @@ pub async fn assistant_get_multi<R: Runtime>(
|
||||
// ```
|
||||
if let Some(found) = response_json.get("found") {
|
||||
if found == true {
|
||||
return Ok(response_json);
|
||||
return Ok(Some(response_json));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Err(format!(
|
||||
"could not find Assistant [{}] on all the Coco servers",
|
||||
assistant_id
|
||||
))
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
use regex::Regex;
|
||||
@@ -499,13 +429,13 @@ pub fn remove_icon_fields(json: &str) -> String {
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn ask_ai<R: Runtime>(
|
||||
app_handle: AppHandle<R>,
|
||||
pub async fn ask_ai(
|
||||
app_handle: AppHandle,
|
||||
message: String,
|
||||
server_id: String,
|
||||
assistant_id: String,
|
||||
client_id: String,
|
||||
) -> Result<(), String> {
|
||||
) -> Result<(), HttpRequestError> {
|
||||
let cleaned = remove_icon_fields(message.as_str());
|
||||
|
||||
let body = serde_json::json!({ "message": cleaned });
|
||||
@@ -524,13 +454,19 @@ pub async fn ask_ai<R: Runtime>(
|
||||
)
|
||||
.await?;
|
||||
|
||||
if response.status() == 429 {
|
||||
let status = response.status().as_u16();
|
||||
|
||||
if status == 429 {
|
||||
log::warn!("Rate limit exceeded for assistant: {}", &assistant_id);
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if !response.status().is_success() {
|
||||
return Err(format!("Request Failed: {}", response.status()));
|
||||
return Err(HttpRequestError::RequestFailed {
|
||||
status,
|
||||
error_response_body_str: None,
|
||||
coco_server_api_error_response_body: None,
|
||||
});
|
||||
}
|
||||
|
||||
let stream = response.bytes_stream();
|
||||
@@ -543,7 +479,7 @@ pub async fn ask_ai<R: Runtime>(
|
||||
dbg!("Received line: {}", &line);
|
||||
|
||||
let _ = app_handle.emit(&client_id, line).map_err(|err| {
|
||||
println!("Failed to emit: {:?}", err);
|
||||
log::error!("Failed to emit: {:?}", err);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
use std::{fs::create_dir, io::Read};
|
||||
|
||||
use tauri::{Manager, Runtime};
|
||||
use tauri::{AppHandle, Manager};
|
||||
use tauri_plugin_autostart::ManagerExt;
|
||||
|
||||
/// If the state reported from the OS and the state stored by us differ, our state is
|
||||
/// prioritized and seen as the correct one. Update the OS state to make them consistent.
|
||||
pub fn ensure_autostart_state_consistent(app: &mut tauri::App) -> Result<(), String> {
|
||||
let autostart_manager = app.autolaunch();
|
||||
pub fn ensure_autostart_state_consistent(tauri_app_handle: &AppHandle) -> Result<(), String> {
|
||||
let autostart_manager = tauri_app_handle.autolaunch();
|
||||
|
||||
let os_state = autostart_manager.is_enabled().map_err(|e| e.to_string())?;
|
||||
let coco_stored_state = current_autostart(app.app_handle()).map_err(|e| e.to_string())?;
|
||||
let coco_stored_state = current_autostart(tauri_app_handle).map_err(|e| e.to_string())?;
|
||||
|
||||
if os_state != coco_stored_state {
|
||||
log::warn!(
|
||||
@@ -42,7 +42,7 @@ pub fn ensure_autostart_state_consistent(app: &mut tauri::App) -> Result<(), Str
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn current_autostart<R: Runtime>(app: &tauri::AppHandle<R>) -> Result<bool, String> {
|
||||
fn current_autostart(app: &tauri::AppHandle) -> Result<bool, String> {
|
||||
use std::fs::File;
|
||||
|
||||
let path = app.path().app_config_dir().unwrap();
|
||||
@@ -65,10 +65,7 @@ fn current_autostart<R: Runtime>(app: &tauri::AppHandle<R>) -> Result<bool, Stri
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn change_autostart<R: Runtime>(
|
||||
app: tauri::AppHandle<R>,
|
||||
open: bool,
|
||||
) -> Result<(), String> {
|
||||
pub async fn change_autostart(app: tauri::AppHandle, open: bool) -> Result<(), String> {
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
|
||||
|
||||
@@ -3,7 +3,10 @@ use serde_json::Value;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ChatRequestMessage {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub message: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub attachments: Option<Vec<String>>,
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
// use std::collections::HashMap;
|
||||
use serde::{Deserialize, Serialize};
|
||||
// use crate::common::health::Status;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct RequestAccessTokenResponse {
|
||||
pub access_token: String,
|
||||
pub expire_in: u32,
|
||||
}
|
||||
@@ -1,7 +1,12 @@
|
||||
#[cfg(target_os = "macos")]
|
||||
use crate::extension::built_in::window_management::actions::Action;
|
||||
use crate::extension::view_extension::serve_files_in;
|
||||
use crate::extension::{ExtensionPermission, ExtensionSettings, ViewExtensionUISettings};
|
||||
use log::debug;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value as Json;
|
||||
use std::collections::HashMap;
|
||||
use tauri::AppHandle;
|
||||
use tauri::Runtime;
|
||||
use tauri::{AppHandle, Emitter};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct RichLabel {
|
||||
@@ -31,17 +36,68 @@ pub struct EditorInfo {
|
||||
pub timestamp: Option<String>,
|
||||
}
|
||||
|
||||
/// Defines the action that would be performed when a document gets opened.
|
||||
/// Defines the action that would be performed when a [document](Document) gets opened.
|
||||
///
|
||||
/// "Document" is a uniform type that the backend uses to send the search results
|
||||
/// back to the frontend. Since Coco can search many sources, "Document" can
|
||||
/// represent different things, application, web page, local file, extensions, and
|
||||
/// so on. Each has its own specific open action.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub(crate) enum OnOpened {
|
||||
/// Launch the application
|
||||
Application { app_path: String },
|
||||
/// Open the URL.
|
||||
Document { url: String },
|
||||
/// Perform this WM action.
|
||||
#[cfg(target_os = "macos")]
|
||||
WindowManagementAction { action: Action },
|
||||
/// The document is an extension.
|
||||
Extension(ExtensionOnOpened),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub(crate) struct ExtensionOnOpened {
|
||||
/// Different types of extensions have different open behaviors.
|
||||
pub(crate) ty: ExtensionOnOpenedType,
|
||||
/// Extensions settings. Some could affect open action.
|
||||
///
|
||||
/// Optional because not all extensions have their settings.
|
||||
pub(crate) settings: Option<ExtensionSettings>,
|
||||
/// Permission needed by this extension.
|
||||
///
|
||||
/// We do permission check when opening this permission. Currently, we only
|
||||
/// do this to View extensions.
|
||||
pub(crate) permission: Option<ExtensionPermission>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub(crate) enum ExtensionOnOpenedType {
|
||||
/// Spawn a child process to run the `CommandAction`.
|
||||
Command {
|
||||
action: crate::extension::CommandAction,
|
||||
},
|
||||
/// Open the `link`.
|
||||
//
|
||||
// NOTE that this variant has the same definition as `struct Quicklink`, but we
|
||||
// cannot use it directly, its `link` field should be deserialized/serialized
|
||||
// from/to a string, but we need a JSON object here.
|
||||
//
|
||||
// See also the comments in `struct Quicklink`.
|
||||
Quicklink {
|
||||
link: crate::extension::QuicklinkLink,
|
||||
open_with: Option<String>,
|
||||
},
|
||||
View {
|
||||
/// Extension name
|
||||
name: String,
|
||||
// An absolute path to the extension icon or a font code.
|
||||
icon: String,
|
||||
/// Path to the HTML file that coco will load and render.
|
||||
///
|
||||
/// It should be an absolute path or Tauri cannot open it.
|
||||
page: String,
|
||||
ui: Option<ViewExtensionUISettings>,
|
||||
},
|
||||
}
|
||||
|
||||
impl OnOpened {
|
||||
@@ -49,62 +105,190 @@ impl OnOpened {
|
||||
match self {
|
||||
Self::Application { app_path } => app_path.clone(),
|
||||
Self::Document { url } => url.clone(),
|
||||
Self::Command { action } => {
|
||||
const WHITESPACE: &str = " ";
|
||||
let mut ret = action.exec.clone();
|
||||
ret.push_str(WHITESPACE);
|
||||
if let Some(ref args) = action.args {
|
||||
ret.push_str(args.join(WHITESPACE).as_str());
|
||||
}
|
||||
#[cfg(target_os = "macos")]
|
||||
Self::WindowManagementAction { action: _ } => {
|
||||
// We don't have URL for this
|
||||
String::from("N/A")
|
||||
}
|
||||
Self::Extension(ext_on_opened) => {
|
||||
match &ext_on_opened.ty {
|
||||
ExtensionOnOpenedType::Command { action } => {
|
||||
const WHITESPACE: &str = " ";
|
||||
let mut ret = action.exec.clone();
|
||||
ret.push_str(WHITESPACE);
|
||||
if let Some(ref args) = action.args {
|
||||
ret.push_str(args.join(WHITESPACE).as_str());
|
||||
}
|
||||
|
||||
ret
|
||||
ret
|
||||
}
|
||||
// Currently, our URL is static and does not support dynamic parameters.
|
||||
// The URL of a quicklink is nearly useless without such dynamic user
|
||||
// inputs, so until we have dynamic URL support, we just use "N/A".
|
||||
ExtensionOnOpenedType::Quicklink { .. } => String::from("N/A"),
|
||||
ExtensionOnOpenedType::View {
|
||||
name: _,
|
||||
icon: _,
|
||||
page: _,
|
||||
ui: _,
|
||||
} => {
|
||||
// We currently don't have URL for this kind of extension.
|
||||
String::from("N/A")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub(crate) async fn open<R: Runtime>(
|
||||
tauri_app_handle: AppHandle<R>,
|
||||
pub(crate) async fn open(
|
||||
tauri_app_handle: AppHandle,
|
||||
on_opened: OnOpened,
|
||||
extra_args: Option<HashMap<String, Json>>,
|
||||
) -> Result<(), String> {
|
||||
log::debug!("open({})", on_opened.url());
|
||||
|
||||
use crate::util::open as homemade_tauri_shell_open;
|
||||
use std::process::Command;
|
||||
|
||||
match on_opened {
|
||||
OnOpened::Application { app_path } => {
|
||||
log::debug!("open application [{}]", app_path);
|
||||
|
||||
homemade_tauri_shell_open(tauri_app_handle.clone(), app_path).await?
|
||||
}
|
||||
OnOpened::Document { url } => {
|
||||
log::debug!("open document [{}]", url);
|
||||
|
||||
homemade_tauri_shell_open(tauri_app_handle.clone(), url).await?
|
||||
}
|
||||
OnOpened::Command { action } => {
|
||||
let mut cmd = Command::new(action.exec);
|
||||
if let Some(args) = action.args {
|
||||
cmd.args(args);
|
||||
}
|
||||
let output = cmd.output().map_err(|e| e.to_string())?;
|
||||
// Sometimes, we wanna see the result in logs even though it doesn't fail.
|
||||
log::debug!(
|
||||
"executing open(Command) result, exit code: [{}], stdout: [{}], stderr: [{}]",
|
||||
output.status,
|
||||
String::from_utf8_lossy(&output.stdout),
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
if !output.status.success() {
|
||||
log::warn!(
|
||||
"executing open(Command) failed, exit code: [{}], stdout: [{}], stderr: [{}]",
|
||||
output.status,
|
||||
String::from_utf8_lossy(&output.stdout),
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
#[cfg(target_os = "macos")]
|
||||
OnOpened::WindowManagementAction { action } => {
|
||||
log::debug!("perform Window Management action [{:?}]", action);
|
||||
|
||||
return Err(format!(
|
||||
"Command failed, stderr [{}]",
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
));
|
||||
crate::extension::built_in::window_management::perform_action_on_main_thread(
|
||||
&tauri_app_handle,
|
||||
action,
|
||||
)?;
|
||||
}
|
||||
OnOpened::Extension(ext_on_opened) => {
|
||||
// Apply the settings that would affect open behavior
|
||||
if let Some(settings) = ext_on_opened.settings {
|
||||
if let Some(should_hide) = settings.hide_before_open {
|
||||
if should_hide {
|
||||
crate::hide_coco(tauri_app_handle.clone()).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
let permission = ext_on_opened.permission;
|
||||
|
||||
match ext_on_opened.ty {
|
||||
ExtensionOnOpenedType::Command { action } => {
|
||||
log::debug!("open (execute) command [{:?}]", action);
|
||||
|
||||
let mut cmd = Command::new(action.exec);
|
||||
if let Some(args) = action.args {
|
||||
cmd.args(args);
|
||||
}
|
||||
let output = cmd.output().map_err(|e| e.to_string())?;
|
||||
// Sometimes, we wanna see the result in logs even though it doesn't fail.
|
||||
log::debug!(
|
||||
"executing open(Command) result, exit code: [{}], stdout: [{}], stderr: [{}]",
|
||||
output.status,
|
||||
String::from_utf8_lossy(&output.stdout),
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
if !output.status.success() {
|
||||
log::warn!(
|
||||
"executing open(Command) failed, exit code: [{}], stdout: [{}], stderr: [{}]",
|
||||
output.status,
|
||||
String::from_utf8_lossy(&output.stdout),
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
|
||||
return Err(format!(
|
||||
"Command failed, stderr [{}]",
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
));
|
||||
}
|
||||
}
|
||||
ExtensionOnOpenedType::Quicklink {
|
||||
link,
|
||||
open_with: opt_open_with,
|
||||
} => {
|
||||
let url = link.concatenate_url(&extra_args);
|
||||
|
||||
log::debug!("open quicklink [{}] with [{:?}]", url, opt_open_with);
|
||||
|
||||
cfg_if::cfg_if! {
|
||||
// The `open_with` functionality is only supported on macOS, provided
|
||||
// by the `open -a` command.
|
||||
if #[cfg(target_os = "macos")] {
|
||||
let mut cmd = Command::new("open");
|
||||
if let Some(ref open_with) = opt_open_with {
|
||||
cmd.arg("-a");
|
||||
cmd.arg(open_with.as_str());
|
||||
}
|
||||
cmd.arg(&url);
|
||||
|
||||
let output = cmd.output().map_err(|e| format!("failed to spawn [open] due to error [{}]", e))?;
|
||||
|
||||
if !output.status.success() {
|
||||
return Err(format!(
|
||||
"failed to open with app {:?}: {}",
|
||||
opt_open_with,
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
));
|
||||
}
|
||||
} else {
|
||||
homemade_tauri_shell_open(tauri_app_handle.clone(), url).await?
|
||||
}
|
||||
}
|
||||
}
|
||||
ExtensionOnOpenedType::View {
|
||||
name,
|
||||
icon,
|
||||
page,
|
||||
ui,
|
||||
} => {
|
||||
let page_path = Utf8Path::new(&page);
|
||||
let directory = page_path.parent().unwrap_or_else(|| {
|
||||
panic!("View extension page path should have a parent, i.e., it should be under a directory, but [{}] does not", page);
|
||||
});
|
||||
let mut url = serve_files_in(directory.as_ref()).await;
|
||||
|
||||
/*
|
||||
* Emit an event to let the frontend code open this extension.
|
||||
*
|
||||
* Payload `view_extension_opened` contains the information needed
|
||||
* to do that.
|
||||
*
|
||||
* See "src/pages/main/index.tsx" for more info.
|
||||
*/
|
||||
use camino::Utf8Path;
|
||||
use serde_json::Value as Json;
|
||||
use serde_json::to_value;
|
||||
|
||||
let html_filename = page_path
|
||||
.file_name()
|
||||
.unwrap_or_else(|| {
|
||||
panic!("View extension page path should have a file name, but [{}] does not have one", page);
|
||||
}).to_string();
|
||||
url.push('/');
|
||||
url.push_str(&html_filename);
|
||||
|
||||
let html_file_url = url;
|
||||
debug!("View extension listening on: {}", html_file_url);
|
||||
let view_extension_opened: [Json; 5] = [
|
||||
Json::String(name),
|
||||
Json::String(icon),
|
||||
Json::String(html_file_url),
|
||||
to_value(permission).unwrap(),
|
||||
to_value(ui).unwrap(),
|
||||
];
|
||||
tauri_app_handle
|
||||
.emit("open_view_extension", view_extension_opened)
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,81 +1,162 @@
|
||||
use reqwest::StatusCode;
|
||||
use serde::{Deserialize, Serialize, Serializer};
|
||||
use thiserror::Error;
|
||||
use snafu::prelude::*;
|
||||
|
||||
fn serialize_optional_status_code<S>(
|
||||
status_code: &Option<StatusCode>,
|
||||
use crate::server::http_client::HttpRequestError;
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct ApiErrorCause {
|
||||
/// Only the top-level error contains this.
|
||||
#[serde(default)]
|
||||
pub root_cause: Option<Vec<ApiErrorCause>>,
|
||||
|
||||
#[serde(default)]
|
||||
pub r#type: Option<String>,
|
||||
#[serde(default)]
|
||||
pub reason: Option<String>,
|
||||
|
||||
/// Recursion, [error A] cause by [error B] caused by [error C]
|
||||
#[serde(default)]
|
||||
pub caused_by: Option<Box<ApiErrorCause>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct ApiError {
|
||||
#[serde(default)]
|
||||
pub error: Option<ApiErrorCause>,
|
||||
#[serde(default)]
|
||||
pub status: Option<u16>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Snafu, Serialize)]
|
||||
#[snafu(visibility(pub(crate)))]
|
||||
pub enum SearchError {
|
||||
#[snafu(display("HTTP request error"))]
|
||||
HttpError { source: HttpRequestError },
|
||||
#[snafu(display("failed to decode query response"))]
|
||||
ResponseDecodeError {
|
||||
#[serde(serialize_with = "serialize_error")]
|
||||
source: serde_json::Error,
|
||||
},
|
||||
/// The search operation timed out.
|
||||
#[snafu(display("search operation timed out"))]
|
||||
SearchTimeout,
|
||||
#[snafu(display("an internal error occurred: '{}'", error))]
|
||||
InternalError { error: String },
|
||||
}
|
||||
|
||||
pub(crate) fn serialize_error<S, E: std::error::Error>(
|
||||
error: &E,
|
||||
serializer: S,
|
||||
) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
match status_code {
|
||||
Some(code) => serializer.serialize_str(&format!("{:?}", code)),
|
||||
None => serializer.serialize_none(),
|
||||
}
|
||||
serializer.serialize_str(&report_error(error, ReportErrorStyle::SingleLine))
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct ErrorCause {
|
||||
#[serde(default)]
|
||||
pub r#type: Option<String>,
|
||||
#[serde(default)]
|
||||
pub reason: Option<String>,
|
||||
/// `ReportErrorStyle` controls the error reporting format.
|
||||
pub(crate) enum ReportErrorStyle {
|
||||
/// Report it in one line of message. This is suitable when you write dump
|
||||
/// errors to logs.
|
||||
///
|
||||
/// ```text
|
||||
/// 'failed to installed extension', caused by ['Json parsing error' 'I/O error: file not found']
|
||||
/// ```
|
||||
SingleLine,
|
||||
/// Allow it to span multiple lines.
|
||||
///
|
||||
/// ```text
|
||||
/// failed to installed extension
|
||||
/// Caused by:
|
||||
///
|
||||
/// 0: Json parsing error
|
||||
/// 1: I/O error: file not found
|
||||
/// ```
|
||||
MultipleLines,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[allow(unused)]
|
||||
pub struct ErrorDetail {
|
||||
#[serde(default)]
|
||||
pub root_cause: Option<Vec<ErrorCause>>,
|
||||
#[serde(default)]
|
||||
pub r#type: Option<String>,
|
||||
#[serde(default)]
|
||||
pub reason: Option<String>,
|
||||
#[serde(default)]
|
||||
pub caused_by: Option<ErrorCause>,
|
||||
}
|
||||
/// In Rust, a typical Display impl of an Error won't contain it source information[1],
|
||||
/// so we need a reporter to report the full error message.
|
||||
///
|
||||
/// [1]: https://stackoverflow.com/q/62869360/14092446
|
||||
pub(crate) fn report_error<E: std::error::Error>(e: &E, style: ReportErrorStyle) -> String {
|
||||
use std::fmt::Write;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct ErrorResponse {
|
||||
#[serde(default)]
|
||||
pub error: Option<ErrorDetail>,
|
||||
#[serde(default)]
|
||||
#[allow(unused)]
|
||||
pub status: Option<u16>,
|
||||
}
|
||||
match style {
|
||||
ReportErrorStyle::SingleLine => {
|
||||
let mut error_msg = format!("'{}'", e);
|
||||
if let Some(cause) = e.source() {
|
||||
error_msg.push_str(", caused by: [");
|
||||
|
||||
#[derive(Debug, Error, Serialize)]
|
||||
pub enum SearchError {
|
||||
#[error("HttpError: status code [{status_code:?}], msg [{msg}]")]
|
||||
HttpError {
|
||||
#[serde(serialize_with = "serialize_optional_status_code")]
|
||||
status_code: Option<StatusCode>,
|
||||
msg: String,
|
||||
},
|
||||
|
||||
#[error("ParseError: {0}")]
|
||||
ParseError(String),
|
||||
|
||||
#[error("Timeout occurred")]
|
||||
Timeout,
|
||||
|
||||
#[error("InternalError: {0}")]
|
||||
InternalError(String),
|
||||
}
|
||||
|
||||
impl From<reqwest::Error> for SearchError {
|
||||
fn from(err: reqwest::Error) -> Self {
|
||||
if err.is_timeout() {
|
||||
SearchError::Timeout
|
||||
} else if err.is_decode() {
|
||||
SearchError::ParseError(err.to_string())
|
||||
} else {
|
||||
SearchError::HttpError {
|
||||
status_code: err.status(),
|
||||
msg: err.to_string(),
|
||||
for (i, e) in std::iter::successors(Some(cause), |e| e.source()).enumerate() {
|
||||
if i != 0 {
|
||||
error_msg.push(' ');
|
||||
}
|
||||
write!(&mut error_msg, "'{}'", e).expect("failed to write in-memory string");
|
||||
}
|
||||
error_msg.push(']');
|
||||
}
|
||||
|
||||
error_msg
|
||||
}
|
||||
ReportErrorStyle::MultipleLines => snafu::Report::from_error(e).to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::io;
|
||||
|
||||
#[derive(Debug, Snafu)]
|
||||
enum Error {
|
||||
#[snafu(display("I/O Error"))]
|
||||
Io { source: io::Error },
|
||||
#[snafu(display("Foo"))]
|
||||
Foo,
|
||||
#[snafu(display("Nested"))]
|
||||
Nested { source: ReadError },
|
||||
}
|
||||
|
||||
#[derive(Debug, Snafu)]
|
||||
enum ReadError {
|
||||
#[snafu(display("failed to read config file"))]
|
||||
ReadConfig { source: io::Error },
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_report_error_single_line_one_caused_by() {
|
||||
let err = Error::Io {
|
||||
source: io::Error::new(io::ErrorKind::NotFound, "file Cargo.toml not found"),
|
||||
};
|
||||
|
||||
let error_msg = report_error(&err, ReportErrorStyle::SingleLine);
|
||||
assert_eq!(
|
||||
error_msg,
|
||||
"'I/O Error', caused by: ['file Cargo.toml not found']"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_report_error_single_line_multiple_caused_by() {
|
||||
let err = Error::Nested {
|
||||
source: ReadError::ReadConfig {
|
||||
source: io::Error::new(io::ErrorKind::NotFound, "not found"),
|
||||
},
|
||||
};
|
||||
|
||||
let error_msg = report_error(&err, ReportErrorStyle::SingleLine);
|
||||
assert_eq!(
|
||||
error_msg,
|
||||
"'Nested', caused by: ['failed to read config file' 'not found']"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_report_error_single_line_no_caused_by() {
|
||||
let err = Error::Foo;
|
||||
|
||||
let error_msg = report_error(&err, ReportErrorStyle::SingleLine);
|
||||
assert_eq!(error_msg, "'Foo'");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
use crate::common;
|
||||
use crate::{
|
||||
common,
|
||||
server::http_client::{DecodeResponseSnafu, HttpRequestError},
|
||||
};
|
||||
use reqwest::Response;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
use snafu::ResultExt;
|
||||
use std::collections::HashMap;
|
||||
use tauri_plugin_store::JsonValue;
|
||||
|
||||
@@ -21,36 +25,35 @@ pub struct Source {
|
||||
pub status: String,
|
||||
}
|
||||
|
||||
pub async fn get_response_body_text(response: Response) -> Result<String, String> {
|
||||
pub async fn get_response_body_text(response: Response) -> Result<String, HttpRequestError> {
|
||||
let status = response.status().as_u16();
|
||||
let body = response
|
||||
.text()
|
||||
.await
|
||||
.map_err(|e| format!("Failed to read response body: {}, code: {}", e, status))?;
|
||||
.context(DecodeResponseSnafu)?
|
||||
.trim()
|
||||
.to_string();
|
||||
|
||||
log::debug!("Response status: {}, body: {}", status, &body);
|
||||
|
||||
if status < 200 || status >= 400 {
|
||||
// Try to parse the error body
|
||||
let fallback_error = "Failed to send message".to_string();
|
||||
|
||||
if body.trim().is_empty() {
|
||||
return Err(fallback_error);
|
||||
if body.is_empty() {
|
||||
return Err(HttpRequestError::RequestFailed {
|
||||
status,
|
||||
error_response_body_str: None,
|
||||
coco_server_api_error_response_body: None,
|
||||
});
|
||||
}
|
||||
|
||||
match serde_json::from_str::<common::error::ErrorResponse>(&body) {
|
||||
Ok(parsed_error) => {
|
||||
dbg!(&parsed_error);
|
||||
Err(format!(
|
||||
"Server error ({}): {:?}",
|
||||
status, parsed_error.error
|
||||
))
|
||||
}
|
||||
Err(_) => {
|
||||
log::warn!("Failed to parse error response: {}", &body);
|
||||
Err(fallback_error)
|
||||
}
|
||||
}
|
||||
// Ignore this error, including a `serde_json::Error` in `HttpRequestError::RequestFailed`
|
||||
// would be too verbose. And it is still easy to debug without this error, since we have
|
||||
// the raw error response body.
|
||||
let api_error = serde_json::from_str::<common::error::ApiError>(&body).ok();
|
||||
Err(HttpRequestError::RequestFailed {
|
||||
status,
|
||||
error_response_body_str: Some(body),
|
||||
coco_server_api_error_response_body: api_error,
|
||||
})
|
||||
} else {
|
||||
Ok(body)
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
pub mod assistant;
|
||||
pub mod auth;
|
||||
pub mod connector;
|
||||
pub mod datasource;
|
||||
pub mod document;
|
||||
|
||||
@@ -22,9 +22,11 @@ impl SearchSourceRegistry {
|
||||
sources.clear();
|
||||
}
|
||||
|
||||
pub async fn remove_source(&self, id: &str) {
|
||||
/// Remove the SearchSource specified by `id`, return a boolean indicating
|
||||
/// if it get removed or not.
|
||||
pub async fn remove_source(&self, id: &str) -> bool {
|
||||
let mut sources = self.sources.write().await;
|
||||
sources.remove(id);
|
||||
sources.remove(id).is_some()
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
|
||||
@@ -100,7 +100,7 @@ impl SearchQuery {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[derive(Debug, Clone, Serialize, Hash, PartialEq, Eq)]
|
||||
pub struct QuerySource {
|
||||
pub r#type: String, //coco-server/local/ etc.
|
||||
pub id: String, //coco server's id
|
||||
|
||||
5
src-tauri/src/extension/api/apis.toml
Normal file
@@ -0,0 +1,5 @@
|
||||
# Complete Coco extension API list grouped by its category.
|
||||
|
||||
fs = [
|
||||
"read_dir"
|
||||
]
|
||||
22
src-tauri/src/extension/api/fs.rs
Normal file
@@ -0,0 +1,22 @@
|
||||
//! File system APIs
|
||||
|
||||
use tokio::fs::read_dir as tokio_read_dir;
|
||||
|
||||
#[tauri::command]
|
||||
pub(crate) async fn read_dir(path: String) -> Result<Vec<String>, String> {
|
||||
let mut iter = tokio_read_dir(path).await.map_err(|e| e.to_string())?;
|
||||
|
||||
let mut file_names = Vec::new();
|
||||
|
||||
loop {
|
||||
let opt_entry = iter.next_entry().await.map_err(|e| e.to_string())?;
|
||||
let Some(entry) = opt_entry else {
|
||||
break;
|
||||
};
|
||||
|
||||
let file_name = entry.file_name().to_string_lossy().into_owned();
|
||||
file_names.push(file_name);
|
||||
}
|
||||
|
||||
Ok(file_names)
|
||||
}
|
||||
21
src-tauri/src/extension/api/mod.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
//! The Rust implementation of the Coco extension APIs.
|
||||
//!
|
||||
//! Extension developers do not use these Rust APIs directly, they use our
|
||||
//! [Typescript library][ts_lib], which eventually calls these APIs.
|
||||
//!
|
||||
//! [ts_lib]: https://github.com/infinilabs/coco-api
|
||||
|
||||
pub(crate) mod fs;
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
/// Return all the available APIs grouped by their category.
|
||||
#[tauri::command]
|
||||
pub(crate) fn apis() -> HashMap<String, Vec<String>> {
|
||||
static APIS_TOML: &str = include_str!("./apis.toml");
|
||||
|
||||
let apis: HashMap<String, Vec<String>> =
|
||||
toml::from_str(APIS_TOML).expect("Failed to parse apis.toml file");
|
||||
|
||||
apis
|
||||
}
|
||||
@@ -27,8 +27,8 @@ use pizza_engine::{Engine, EngineBuilder, doc};
|
||||
use serde_json::Value as Json;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
use tauri::{AppHandle, Manager, Runtime, async_runtime};
|
||||
use tauri_plugin_fs_pro::{IconOptions, icon, metadata, name};
|
||||
use tauri::{AppHandle, Manager, async_runtime};
|
||||
use tauri_plugin_fs_pro::{IconOptions, icon, metadata};
|
||||
use tauri_plugin_global_shortcut::GlobalShortcutExt;
|
||||
use tauri_plugin_global_shortcut::Shortcut;
|
||||
use tauri_plugin_global_shortcut::ShortcutEvent;
|
||||
@@ -36,7 +36,13 @@ use tauri_plugin_global_shortcut::ShortcutState;
|
||||
use tauri_plugin_store::StoreExt;
|
||||
use tokio::sync::oneshot::Sender as OneshotSender;
|
||||
|
||||
// Deprecated. We no longer index this field, but to be backward-compatible, we
|
||||
// have to keep it.
|
||||
const FIELD_APP_NAME: &str = "app_name";
|
||||
|
||||
const FIELD_APP_NAME_IN_SYSTEM_LANG: &str = "app_name_in_system_lang";
|
||||
const FIELD_APP_NAME_ZH: &str = "app_name_zh";
|
||||
const FIELD_APP_NAME_EN: &str = "app_name_en";
|
||||
const FIELD_ICON_PATH: &str = "icon_path";
|
||||
const FIELD_APP_ALIAS: &str = "app_alias";
|
||||
const APPLICATION_SEARCH_SOURCE_ID: &str = "application";
|
||||
@@ -58,37 +64,18 @@ const INDEX_DIR: &str = "local_application_index";
|
||||
pub(crate) const QUERYSOURCE_ID_DATASOURCE_ID_DATASOURCE_NAME: &str = "Applications";
|
||||
|
||||
pub fn get_default_search_paths() -> Vec<String> {
|
||||
#[cfg(target_os = "macos")]
|
||||
{
|
||||
let home_dir =
|
||||
PathBuf::from(std::env::var_os("HOME").expect("environment variable $HOME not found"));
|
||||
return vec![
|
||||
"/Applications".into(),
|
||||
"/System/Applications".into(),
|
||||
"/System/Library/CoreServices".into(),
|
||||
home_dir
|
||||
.join("Applications")
|
||||
.into_os_string()
|
||||
.into_string()
|
||||
.expect("this path should be UTF-8 encoded"),
|
||||
];
|
||||
let paths = applications::get_default_search_paths();
|
||||
let mut ret = Vec::with_capacity(paths.len());
|
||||
for search_path in paths {
|
||||
let path_string = search_path
|
||||
.into_os_string()
|
||||
.into_string()
|
||||
.expect("path should be UTF-8 encoded");
|
||||
|
||||
ret.push(path_string);
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
{
|
||||
let paths = applications::get_default_search_paths();
|
||||
let mut ret = Vec::with_capacity(paths.len());
|
||||
for search_path in paths {
|
||||
let path_string = search_path
|
||||
.into_os_string()
|
||||
.into_string()
|
||||
.expect("path should be UTF-8 encoded");
|
||||
|
||||
ret.push(path_string);
|
||||
}
|
||||
|
||||
ret
|
||||
}
|
||||
ret
|
||||
}
|
||||
|
||||
/// Helper function to return `app`'s path.
|
||||
@@ -115,26 +102,63 @@ fn get_app_path(app: &App) -> String {
|
||||
.expect("should be UTF-8 encoded")
|
||||
}
|
||||
|
||||
/// Helper function to return `app`'s path.
|
||||
///
|
||||
/// * macOS: extract `app_path`'s file name and remove the file extension
|
||||
/// * Windows/Linux: return the name specified in `.desktop` file
|
||||
async fn get_app_name(app: &App) -> String {
|
||||
if cfg!(any(target_os = "linux", target_os = "windows")) {
|
||||
app.name.clone()
|
||||
/// Helper function to return `app`'s Chinese name.
|
||||
async fn get_app_name_zh(app: &App) -> String {
|
||||
// zh_CN or zh-CN
|
||||
if let Some(name) = app.localized_app_names.get("zh_CN") {
|
||||
return name.clone();
|
||||
}
|
||||
if let Some(name) = app.localized_app_names.get("zh-CN") {
|
||||
return name.clone();
|
||||
}
|
||||
|
||||
// zh_Hans or zh-Hans
|
||||
if let Some(name) = app.localized_app_names.get("zh_Hans") {
|
||||
return name.clone();
|
||||
}
|
||||
if let Some(name) = app.localized_app_names.get("zh-Hans") {
|
||||
return name.clone();
|
||||
}
|
||||
|
||||
// Fall back to base name
|
||||
app.name.clone()
|
||||
}
|
||||
|
||||
/// Helper function to return `app`'s English name.
|
||||
async fn get_app_name_en(app: &App) -> String {
|
||||
// en_US or en-US
|
||||
if let Some(name) = app.localized_app_names.get("en_US") {
|
||||
return name.clone();
|
||||
}
|
||||
if let Some(name) = app.localized_app_names.get("en-US") {
|
||||
return name.clone();
|
||||
}
|
||||
|
||||
// English (General)
|
||||
if let Some(name) = app.localized_app_names.get("en") {
|
||||
return name.clone();
|
||||
}
|
||||
|
||||
// Fall back to base name
|
||||
app.name.clone()
|
||||
}
|
||||
|
||||
/// Helper function to return `app`'s name in system language.
|
||||
async fn get_app_name_in_system_lang(app: &App) -> String {
|
||||
let system_lang = crate::util::system_lang::get_system_lang();
|
||||
|
||||
if let Some(name) = app.localized_app_names.get(&system_lang) {
|
||||
name.clone()
|
||||
} else {
|
||||
let app_path = get_app_path(app);
|
||||
name(app_path.into()).await
|
||||
// Fall back to base name
|
||||
app.name.clone()
|
||||
}
|
||||
}
|
||||
|
||||
/// Helper function to return an absolute path to `app`'s icon.
|
||||
///
|
||||
/// On macOS/Windows, we cache icons in our data directory using the `icon()` function.
|
||||
async fn get_app_icon_path<R: Runtime>(
|
||||
tauri_app_handle: &AppHandle<R>,
|
||||
app: &App,
|
||||
) -> Result<String, String> {
|
||||
async fn get_app_icon_path(tauri_app_handle: &AppHandle, app: &App) -> Result<String, String> {
|
||||
let res_path = if cfg!(target_os = "linux") {
|
||||
let icon_path = app
|
||||
.icon_path
|
||||
@@ -213,8 +237,8 @@ impl SearchSourceState for ApplicationSearchSourceState {
|
||||
}
|
||||
|
||||
/// Index applications if they have not been indexed (by checking if `app_index_dir` exists).
|
||||
async fn index_applications_if_not_indexed<R: Runtime>(
|
||||
tauri_app_handle: &AppHandle<R>,
|
||||
async fn index_applications_if_not_indexed(
|
||||
tauri_app_handle: &AppHandle,
|
||||
app_index_dir: &Path,
|
||||
) -> anyhow::Result<ApplicationSearchSourceState> {
|
||||
let index_exists = app_index_dir.exists();
|
||||
@@ -224,9 +248,17 @@ async fn index_applications_if_not_indexed<R: Runtime>(
|
||||
pizza_engine_builder.set_data_store(disk_store);
|
||||
|
||||
let mut schema = Schema::new();
|
||||
let field_app_name = Property::builder(FieldType::Text).build();
|
||||
let field_app_name_zh = Property::builder(FieldType::Text).build();
|
||||
schema
|
||||
.add_property(FIELD_APP_NAME, field_app_name)
|
||||
.add_property(FIELD_APP_NAME_ZH, field_app_name_zh)
|
||||
.expect("no collision could happen");
|
||||
let field_app_name_en = Property::builder(FieldType::Text).build();
|
||||
schema
|
||||
.add_property(FIELD_APP_NAME_EN, field_app_name_en)
|
||||
.expect("no collision could happen");
|
||||
let field_app_name_in_system_lang = Property::builder(FieldType::Text).build();
|
||||
schema
|
||||
.add_property(FIELD_APP_NAME_IN_SYSTEM_LANG, field_app_name_in_system_lang)
|
||||
.expect("no collision could happen");
|
||||
let property_icon = Property::builder(FieldType::Text).index(false).build();
|
||||
schema
|
||||
@@ -271,21 +303,39 @@ async fn index_applications_if_not_indexed<R: Runtime>(
|
||||
|
||||
for app in apps.iter() {
|
||||
let app_path = get_app_path(app);
|
||||
let app_name = get_app_name(app).await;
|
||||
let app_name_zh = get_app_name_zh(app).await;
|
||||
let app_name_en = get_app_name_en(app).await;
|
||||
let app_name_in_system_lang = get_app_name_in_system_lang(app).await;
|
||||
let app_icon_path = get_app_icon_path(&tauri_app_handle, app)
|
||||
.await
|
||||
.map_err(|str| anyhow::anyhow!(str))?;
|
||||
let app_alias = get_app_alias(&tauri_app_handle, &app_path).unwrap_or(String::new());
|
||||
|
||||
if app_name.is_empty() || app_name.eq(&tauri_app_handle.package_info().name) {
|
||||
// Skip if all names are empty
|
||||
if app_name_zh.is_empty()
|
||||
&& app_name_en.is_empty()
|
||||
&& app_name_in_system_lang.is_empty()
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// Skip if this is Coco itself
|
||||
//
|
||||
// Coco does not have localized app names, so app_name_en and app_name_zh
|
||||
// should both have value "Coco-AI", so either should work.
|
||||
if app_name_en == tauri_app_handle.package_info().name {
|
||||
continue;
|
||||
}
|
||||
|
||||
// You cannot write `app_name.clone()` within the `doc!()` macro, we should fix this.
|
||||
let app_name_clone = app_name.clone();
|
||||
let app_name_zh_clone = app_name_zh.clone();
|
||||
let app_name_en_clone = app_name_en.clone();
|
||||
let app_name_in_system_lang = app_name_in_system_lang.clone();
|
||||
let app_path_clone = app_path.clone();
|
||||
let document = doc!( app_path_clone, {
|
||||
FIELD_APP_NAME => app_name_clone,
|
||||
FIELD_APP_NAME_ZH => app_name_zh_clone,
|
||||
FIELD_APP_NAME_EN => app_name_en_clone,
|
||||
FIELD_APP_NAME_IN_SYSTEM_LANG => app_name_in_system_lang,
|
||||
FIELD_ICON_PATH => app_icon_path,
|
||||
FIELD_APP_ALIAS => app_alias,
|
||||
}
|
||||
@@ -294,8 +344,8 @@ async fn index_applications_if_not_indexed<R: Runtime>(
|
||||
// We don't error out because one failure won't break the whole thing
|
||||
if let Err(e) = writer.create_document(document).await {
|
||||
warn!(
|
||||
"failed to index application [app name: '{}', app path: '{}'] due to error [{}]",
|
||||
app_name, app_path, e
|
||||
"failed to index application [app name zh: '{}', app name en: '{}', app path: '{}'] due to error [{}]",
|
||||
app_name_zh, app_name_en, app_path, e
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -315,13 +365,13 @@ async fn index_applications_if_not_indexed<R: Runtime>(
|
||||
}
|
||||
|
||||
/// Upon application start, index all the applications found in the `get_default_search_paths()`.
|
||||
struct IndexAllApplicationsTask<R: Runtime> {
|
||||
tauri_app_handle: AppHandle<R>,
|
||||
struct IndexAllApplicationsTask {
|
||||
tauri_app_handle: AppHandle,
|
||||
callback: Option<tokio::sync::oneshot::Sender<Result<(), String>>>,
|
||||
}
|
||||
|
||||
#[async_trait::async_trait(?Send)]
|
||||
impl<R: Runtime> Task for IndexAllApplicationsTask<R> {
|
||||
impl Task for IndexAllApplicationsTask {
|
||||
fn search_source_id(&self) -> &'static str {
|
||||
APPLICATION_SEARCH_SOURCE_ID
|
||||
}
|
||||
@@ -343,13 +393,13 @@ impl<R: Runtime> Task for IndexAllApplicationsTask<R> {
|
||||
}
|
||||
}
|
||||
|
||||
struct ReindexAllApplicationsTask<R: Runtime> {
|
||||
tauri_app_handle: AppHandle<R>,
|
||||
struct ReindexAllApplicationsTask {
|
||||
tauri_app_handle: AppHandle,
|
||||
callback: Option<tokio::sync::oneshot::Sender<Result<(), String>>>,
|
||||
}
|
||||
|
||||
#[async_trait::async_trait(?Send)]
|
||||
impl<R: Runtime> Task for ReindexAllApplicationsTask<R> {
|
||||
impl Task for ReindexAllApplicationsTask {
|
||||
fn search_source_id(&self) -> &'static str {
|
||||
APPLICATION_SEARCH_SOURCE_ID
|
||||
}
|
||||
@@ -377,14 +427,14 @@ impl<R: Runtime> Task for ReindexAllApplicationsTask<R> {
|
||||
}
|
||||
}
|
||||
|
||||
struct SearchApplicationsTask<R: Runtime> {
|
||||
tauri_app_handle: AppHandle<R>,
|
||||
struct SearchApplicationsTask {
|
||||
tauri_app_handle: AppHandle,
|
||||
query_string: String,
|
||||
callback: Option<OneshotSender<Result<SearchResult, PizzaEngineError>>>,
|
||||
}
|
||||
|
||||
#[async_trait::async_trait(?Send)]
|
||||
impl<R: Runtime> Task for SearchApplicationsTask<R> {
|
||||
impl Task for SearchApplicationsTask {
|
||||
fn search_source_id(&self) -> &'static str {
|
||||
APPLICATION_SEARCH_SOURCE_ID
|
||||
}
|
||||
@@ -424,9 +474,19 @@ impl<R: Runtime> Task for SearchApplicationsTask<R> {
|
||||
//
|
||||
// It will be passed to Pizza like "Google\nChrome". Using Display impl would result
|
||||
// in an invalid query DSL and serde will complain.
|
||||
//
|
||||
// In order to be backward compatible, we still do match and prefix queries to the
|
||||
// app_name field.
|
||||
let dsl = format!(
|
||||
"{{ \"query\": {{ \"bool\": {{ \"should\": [ {{ \"match\": {{ \"{FIELD_APP_NAME}\": {:?} }} }}, {{ \"prefix\": {{ \"{FIELD_APP_NAME}\": {:?} }} }} ] }} }} }}",
|
||||
self.query_string, self.query_string
|
||||
"{{ \"query\": {{ \"bool\": {{ \"should\": [ {{ \"match\": {{ \"{FIELD_APP_NAME_ZH}\": {:?} }} }}, {{ \"prefix\": {{ \"{FIELD_APP_NAME_ZH}\": {:?} }} }}, {{ \"match\": {{ \"{FIELD_APP_NAME_EN}\": {:?} }} }}, {{ \"prefix\": {{ \"{FIELD_APP_NAME_EN}\": {:?} }} }}, {{ \"match\": {{ \"{FIELD_APP_NAME_IN_SYSTEM_LANG}\": {:?} }} }}, {{ \"prefix\": {{ \"{FIELD_APP_NAME_IN_SYSTEM_LANG}\": {:?} }} }}, {{ \"match\": {{ \"{FIELD_APP_NAME}\": {:?} }} }}, {{ \"prefix\": {{ \"{FIELD_APP_NAME}\": {:?} }} }} ] }} }} }}",
|
||||
self.query_string,
|
||||
self.query_string,
|
||||
self.query_string,
|
||||
self.query_string,
|
||||
self.query_string,
|
||||
self.query_string,
|
||||
self.query_string,
|
||||
self.query_string
|
||||
);
|
||||
|
||||
let state = state
|
||||
@@ -514,9 +574,7 @@ impl Task for IndexNewApplicationsTask {
|
||||
pub struct ApplicationSearchSource;
|
||||
|
||||
impl ApplicationSearchSource {
|
||||
pub async fn prepare_index_and_store<R: Runtime>(
|
||||
app_handle: AppHandle<R>,
|
||||
) -> Result<(), String> {
|
||||
pub async fn prepare_index_and_store(app_handle: AppHandle) -> Result<(), String> {
|
||||
app_handle
|
||||
.store(TAURI_STORE_APP_HOTKEY)
|
||||
.map_err(|e| e.to_string())?;
|
||||
@@ -620,12 +678,12 @@ impl SearchSource for ApplicationSearchSource {
|
||||
.expect("tx dropped, the runtime thread is possibly dead")
|
||||
.map_err(|pizza_engine_err| {
|
||||
let err_str = pizza_engine_err.to_string();
|
||||
SearchError::InternalError(err_str)
|
||||
SearchError::InternalError { error: err_str }
|
||||
})?;
|
||||
|
||||
let total_hits = search_result.total_hits;
|
||||
let source = self.get_type();
|
||||
let hits = pizza_engine_hits_to_coco_hits(search_result.hits);
|
||||
let hits = pizza_engine_hits_to_coco_hits(search_result.hits).await;
|
||||
|
||||
Ok(QueryResponse {
|
||||
source,
|
||||
@@ -635,9 +693,11 @@ impl SearchSource for ApplicationSearchSource {
|
||||
}
|
||||
}
|
||||
|
||||
fn pizza_engine_hits_to_coco_hits(
|
||||
async fn pizza_engine_hits_to_coco_hits(
|
||||
pizza_engine_hits: Option<Vec<PizzaEngineDocument>>,
|
||||
) -> Vec<(Document, f64)> {
|
||||
use crate::util::app_lang::{Lang, get_app_lang};
|
||||
|
||||
let Some(engine_hits) = pizza_engine_hits else {
|
||||
return Vec::new();
|
||||
};
|
||||
@@ -646,10 +706,43 @@ fn pizza_engine_hits_to_coco_hits(
|
||||
for engine_hit in engine_hits {
|
||||
let score = engine_hit.score.unwrap_or(0.0) as f64;
|
||||
let mut document_fields = engine_hit.fields;
|
||||
let app_name = match document_fields.remove(FIELD_APP_NAME).unwrap() {
|
||||
FieldValue::Text(string) => string,
|
||||
_ => unreachable!("field name is of type Text"),
|
||||
|
||||
// Get both Chinese and English names
|
||||
let opt_app_name_zh = match document_fields.remove(FIELD_APP_NAME_ZH) {
|
||||
Some(FieldValue::Text(string)) => Some(string),
|
||||
_ => None,
|
||||
};
|
||||
let opt_app_name_en = match document_fields.remove(FIELD_APP_NAME_EN) {
|
||||
Some(FieldValue::Text(string)) => Some(string),
|
||||
_ => None,
|
||||
};
|
||||
let opt_app_name_deprecated = match document_fields.remove(FIELD_APP_NAME) {
|
||||
Some(FieldValue::Text(string)) => Some(string),
|
||||
_ => None,
|
||||
};
|
||||
|
||||
let app_name: String = {
|
||||
if let Some(legacy_app_name) = opt_app_name_deprecated {
|
||||
// Old version of index, which only contains the field app_name.
|
||||
legacy_app_name
|
||||
} else {
|
||||
// New version of index store the following 2 fields
|
||||
|
||||
let panic_msg = format!(
|
||||
"new version of index should contain field [{}] and [{}]",
|
||||
FIELD_APP_NAME_EN, FIELD_APP_NAME_ZH
|
||||
);
|
||||
let app_name_zh = opt_app_name_zh.expect(&panic_msg);
|
||||
let app_name_en = opt_app_name_en.expect(&panic_msg);
|
||||
|
||||
// Choose the appropriate name based on current language
|
||||
match get_app_lang().await {
|
||||
Lang::zh_CN => app_name_zh,
|
||||
Lang::en_US => app_name_en,
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let app_path = engine_hit.key.expect("key should be set to app path");
|
||||
let app_icon_path = match document_fields.remove(FIELD_ICON_PATH).unwrap() {
|
||||
FieldValue::Text(string) => string,
|
||||
@@ -669,7 +762,7 @@ fn pizza_engine_hits_to_coco_hits(
|
||||
}),
|
||||
id: app_path.clone(),
|
||||
category: Some("Application".to_string()),
|
||||
title: Some(app_name.clone()),
|
||||
title: Some(app_name),
|
||||
icon: Some(app_icon_path),
|
||||
on_opened: Some(on_opened),
|
||||
url: Some(url),
|
||||
@@ -683,7 +776,7 @@ fn pizza_engine_hits_to_coco_hits(
|
||||
coco_hits
|
||||
}
|
||||
|
||||
pub fn set_app_alias<R: Runtime>(tauri_app_handle: &AppHandle<R>, app_path: &str, alias: &str) {
|
||||
pub fn set_app_alias(tauri_app_handle: &AppHandle, app_path: &str, alias: &str) {
|
||||
let store = tauri_app_handle
|
||||
.store(TAURI_STORE_APP_ALIAS)
|
||||
.unwrap_or_else(|_| panic!("store [{}] not found/loaded", TAURI_STORE_APP_ALIAS));
|
||||
@@ -696,7 +789,7 @@ pub fn set_app_alias<R: Runtime>(tauri_app_handle: &AppHandle<R>, app_path: &str
|
||||
// deleted while updating it.
|
||||
}
|
||||
|
||||
fn get_app_alias<R: Runtime>(tauri_app_handle: &AppHandle<R>, app_path: &str) -> Option<String> {
|
||||
fn get_app_alias(tauri_app_handle: &AppHandle, app_path: &str) -> Option<String> {
|
||||
let store = tauri_app_handle
|
||||
.store(TAURI_STORE_APP_ALIAS)
|
||||
.unwrap_or_else(|_| panic!("store [{}] not found/loaded", TAURI_STORE_APP_ALIAS));
|
||||
@@ -714,9 +807,9 @@ fn get_app_alias<R: Runtime>(tauri_app_handle: &AppHandle<R>, app_path: &str) ->
|
||||
/// The handler that will be invoked when an application hotkey is pressed.
|
||||
///
|
||||
/// The `app_path` argument is for logging-only.
|
||||
fn app_hotkey_handler<R: Runtime>(
|
||||
fn app_hotkey_handler(
|
||||
app_path: String,
|
||||
) -> impl Fn(&AppHandle<R>, &Shortcut, ShortcutEvent) + Send + Sync + 'static {
|
||||
) -> impl Fn(&AppHandle, &Shortcut, ShortcutEvent) + Send + Sync + 'static {
|
||||
move |tauri_app_handle, _hot_key, event| {
|
||||
if event.state() == ShortcutState::Pressed {
|
||||
let app_path_clone = app_path.clone();
|
||||
@@ -732,7 +825,7 @@ fn app_hotkey_handler<R: Runtime>(
|
||||
}
|
||||
|
||||
/// For all the applications, if it is enabled & has hotkey set, then set it up.
|
||||
pub(crate) fn set_apps_hotkey<R: Runtime>(tauri_app_handle: &AppHandle<R>) -> Result<(), String> {
|
||||
pub(crate) fn set_apps_hotkey(tauri_app_handle: &AppHandle) -> Result<(), String> {
|
||||
let app_hotkey_store = tauri_app_handle
|
||||
.store(TAURI_STORE_APP_HOTKEY)
|
||||
.unwrap_or_else(|_| panic!("store [{}] not found/loaded", TAURI_STORE_APP_HOTKEY));
|
||||
@@ -756,7 +849,7 @@ pub(crate) fn set_apps_hotkey<R: Runtime>(tauri_app_handle: &AppHandle<R>) -> Re
|
||||
}
|
||||
|
||||
/// For all the applications, if it is enabled & has hotkey set, then unset it.
|
||||
pub(crate) fn unset_apps_hotkey<R: Runtime>(tauri_app_handle: &AppHandle<R>) -> Result<(), String> {
|
||||
pub(crate) fn unset_apps_hotkey(tauri_app_handle: &AppHandle) -> Result<(), String> {
|
||||
let app_hotkey_store = tauri_app_handle
|
||||
.store(TAURI_STORE_APP_HOTKEY)
|
||||
.unwrap_or_else(|_| panic!("store [{}] not found/loaded", TAURI_STORE_APP_HOTKEY));
|
||||
@@ -783,8 +876,8 @@ pub(crate) fn unset_apps_hotkey<R: Runtime>(tauri_app_handle: &AppHandle<R>) ->
|
||||
}
|
||||
|
||||
/// Set the hotkey but won't persist this settings change.
|
||||
pub(crate) fn set_app_hotkey<R: Runtime>(
|
||||
tauri_app_handle: &AppHandle<R>,
|
||||
pub(crate) fn set_app_hotkey(
|
||||
tauri_app_handle: &AppHandle,
|
||||
app_path: &str,
|
||||
hotkey: &str,
|
||||
) -> Result<(), String> {
|
||||
@@ -794,8 +887,8 @@ pub(crate) fn set_app_hotkey<R: Runtime>(
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
pub fn register_app_hotkey<R: Runtime>(
|
||||
tauri_app_handle: &AppHandle<R>,
|
||||
pub fn register_app_hotkey(
|
||||
tauri_app_handle: &AppHandle,
|
||||
app_path: &str,
|
||||
hotkey: &str,
|
||||
) -> Result<(), String> {
|
||||
@@ -812,10 +905,7 @@ pub fn register_app_hotkey<R: Runtime>(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn unregister_app_hotkey<R: Runtime>(
|
||||
tauri_app_handle: &AppHandle<R>,
|
||||
app_path: &str,
|
||||
) -> Result<(), String> {
|
||||
pub fn unregister_app_hotkey(tauri_app_handle: &AppHandle, app_path: &str) -> Result<(), String> {
|
||||
let app_hotkey_store = tauri_app_handle
|
||||
.store(TAURI_STORE_APP_HOTKEY)
|
||||
.unwrap_or_else(|_| panic!("store [{}] not found/loaded", TAURI_STORE_APP_HOTKEY));
|
||||
@@ -855,7 +945,7 @@ pub fn unregister_app_hotkey<R: Runtime>(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_disabled_app_list<R: Runtime>(tauri_app_handle: &AppHandle<R>) -> Vec<String> {
|
||||
fn get_disabled_app_list(tauri_app_handle: &AppHandle) -> Vec<String> {
|
||||
let store = tauri_app_handle
|
||||
.store(TAURI_STORE_DISABLED_APP_LIST_AND_SEARCH_PATH)
|
||||
.unwrap_or_else(|_| {
|
||||
@@ -892,10 +982,7 @@ pub fn is_app_search_enabled(app_path: &str) -> bool {
|
||||
disabled_app_list.iter().all(|path| path != app_path)
|
||||
}
|
||||
|
||||
pub fn disable_app_search<R: Runtime>(
|
||||
tauri_app_handle: &AppHandle<R>,
|
||||
app_path: &str,
|
||||
) -> Result<(), String> {
|
||||
pub fn disable_app_search(tauri_app_handle: &AppHandle, app_path: &str) -> Result<(), String> {
|
||||
let store = tauri_app_handle
|
||||
.store(TAURI_STORE_DISABLED_APP_LIST_AND_SEARCH_PATH)
|
||||
.unwrap_or_else(|_| {
|
||||
@@ -939,10 +1026,7 @@ pub fn disable_app_search<R: Runtime>(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn enable_app_search<R: Runtime>(
|
||||
tauri_app_handle: &AppHandle<R>,
|
||||
app_path: &str,
|
||||
) -> Result<(), String> {
|
||||
pub fn enable_app_search(tauri_app_handle: &AppHandle, app_path: &str) -> Result<(), String> {
|
||||
let store = tauri_app_handle
|
||||
.store(TAURI_STORE_DISABLED_APP_LIST_AND_SEARCH_PATH)
|
||||
.unwrap_or_else(|_| {
|
||||
@@ -984,8 +1068,8 @@ pub fn enable_app_search<R: Runtime>(
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn add_app_search_path<R: Runtime>(
|
||||
tauri_app_handle: AppHandle<R>,
|
||||
pub async fn add_app_search_path(
|
||||
tauri_app_handle: AppHandle,
|
||||
search_path: String,
|
||||
) -> Result<(), String> {
|
||||
let mut search_paths = get_app_search_path(tauri_app_handle.clone()).await;
|
||||
@@ -1010,8 +1094,8 @@ pub async fn add_app_search_path<R: Runtime>(
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn remove_app_search_path<R: Runtime>(
|
||||
tauri_app_handle: AppHandle<R>,
|
||||
pub async fn remove_app_search_path(
|
||||
tauri_app_handle: AppHandle,
|
||||
search_path: String,
|
||||
) -> Result<(), String> {
|
||||
let mut search_paths = get_app_search_path(tauri_app_handle.clone()).await;
|
||||
@@ -1036,7 +1120,7 @@ pub async fn remove_app_search_path<R: Runtime>(
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn get_app_search_path<R: Runtime>(tauri_app_handle: AppHandle<R>) -> Vec<String> {
|
||||
pub async fn get_app_search_path(tauri_app_handle: AppHandle) -> Vec<String> {
|
||||
let store = tauri_app_handle
|
||||
.store(TAURI_STORE_DISABLED_APP_LIST_AND_SEARCH_PATH)
|
||||
.unwrap_or_else(|_| {
|
||||
@@ -1065,18 +1149,25 @@ pub async fn get_app_search_path<R: Runtime>(tauri_app_handle: AppHandle<R>) ->
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn get_app_list<R: Runtime>(
|
||||
tauri_app_handle: AppHandle<R>,
|
||||
) -> Result<Vec<Extension>, String> {
|
||||
pub async fn get_app_list(tauri_app_handle: AppHandle) -> Result<Vec<Extension>, String> {
|
||||
use crate::util::app_lang::{Lang, get_app_lang};
|
||||
|
||||
let search_paths = get_app_search_path(tauri_app_handle.clone()).await;
|
||||
let apps = list_app_in(search_paths)?;
|
||||
|
||||
let mut app_entries = Vec::with_capacity(apps.len());
|
||||
let lang = get_app_lang().await;
|
||||
|
||||
for app in apps {
|
||||
let name = get_app_name(&app).await;
|
||||
let name = match lang {
|
||||
Lang::zh_CN => get_app_name_zh(&app).await,
|
||||
Lang::en_US => get_app_name_en(&app).await,
|
||||
};
|
||||
|
||||
// filter out Coco-AI
|
||||
//
|
||||
// Coco does not have localized app names, so regardless the chosen language, name
|
||||
// should have value "Coco-AI".
|
||||
if name.eq(&tauri_app_handle.package_info().name) {
|
||||
continue;
|
||||
}
|
||||
@@ -1136,6 +1227,7 @@ pub async fn get_app_list<R: Runtime>(
|
||||
name,
|
||||
platforms: None,
|
||||
developer: None,
|
||||
minimum_coco_version: None,
|
||||
// Leave it empty as it won't be used
|
||||
description: String::new(),
|
||||
icon: icon_path,
|
||||
@@ -1144,11 +1236,15 @@ pub async fn get_app_list<R: Runtime>(
|
||||
quicklink: None,
|
||||
commands: None,
|
||||
scripts: None,
|
||||
views: None,
|
||||
quicklinks: None,
|
||||
alias: Some(alias),
|
||||
hotkey,
|
||||
enabled,
|
||||
settings: None,
|
||||
page: None,
|
||||
ui: None,
|
||||
permission: None,
|
||||
screenshots: None,
|
||||
url: None,
|
||||
version: None,
|
||||
@@ -1202,9 +1298,7 @@ pub async fn get_app_metadata(app_name: String, app_path: String) -> Result<AppM
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn reindex_applications<R: Runtime>(
|
||||
tauri_app_handle: AppHandle<R>,
|
||||
) -> Result<(), String> {
|
||||
pub async fn reindex_applications(tauri_app_handle: AppHandle) -> Result<(), String> {
|
||||
let (tx, rx) = tokio::sync::oneshot::channel();
|
||||
let reindex_applications_task = ReindexAllApplicationsTask {
|
||||
tauri_app_handle: tauri_app_handle.clone(),
|
||||
|
||||
@@ -5,16 +5,14 @@ use crate::common::search::{QueryResponse, QuerySource, SearchQuery};
|
||||
use crate::common::traits::SearchSource;
|
||||
use crate::extension::LOCAL_QUERY_SOURCE_TYPE;
|
||||
use async_trait::async_trait;
|
||||
use tauri::{AppHandle, Runtime};
|
||||
use tauri::AppHandle;
|
||||
|
||||
pub(crate) const QUERYSOURCE_ID_DATASOURCE_ID_DATASOURCE_NAME: &str = "Applications";
|
||||
|
||||
pub struct ApplicationSearchSource;
|
||||
|
||||
impl ApplicationSearchSource {
|
||||
pub async fn prepare_index_and_store<R: Runtime>(
|
||||
_app_handle: AppHandle<R>,
|
||||
) -> Result<(), String> {
|
||||
pub async fn prepare_index_and_store(_app_handle: AppHandle) -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -45,37 +43,28 @@ impl SearchSource for ApplicationSearchSource {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_app_alias<R: Runtime>(_tauri_app_handle: &AppHandle<R>, _app_path: &str, _alias: &str) {
|
||||
pub fn set_app_alias(_tauri_app_handle: &AppHandle, _app_path: &str, _alias: &str) {
|
||||
unreachable!("app list should be empty, there is no way this can be invoked")
|
||||
}
|
||||
|
||||
pub fn register_app_hotkey<R: Runtime>(
|
||||
_tauri_app_handle: &AppHandle<R>,
|
||||
pub fn register_app_hotkey(
|
||||
_tauri_app_handle: &AppHandle,
|
||||
_app_path: &str,
|
||||
_hotkey: &str,
|
||||
) -> Result<(), String> {
|
||||
unreachable!("app list should be empty, there is no way this can be invoked")
|
||||
}
|
||||
|
||||
pub fn unregister_app_hotkey<R: Runtime>(
|
||||
_tauri_app_handle: &AppHandle<R>,
|
||||
_app_path: &str,
|
||||
) -> Result<(), String> {
|
||||
pub fn unregister_app_hotkey(_tauri_app_handle: &AppHandle, _app_path: &str) -> Result<(), String> {
|
||||
unreachable!("app list should be empty, there is no way this can be invoked")
|
||||
}
|
||||
|
||||
pub fn disable_app_search<R: Runtime>(
|
||||
_tauri_app_handle: &AppHandle<R>,
|
||||
_app_path: &str,
|
||||
) -> Result<(), String> {
|
||||
pub fn disable_app_search(_tauri_app_handle: &AppHandle, _app_path: &str) -> Result<(), String> {
|
||||
// no-op
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn enable_app_search<R: Runtime>(
|
||||
_tauri_app_handle: &AppHandle<R>,
|
||||
_app_path: &str,
|
||||
) -> Result<(), String> {
|
||||
pub fn enable_app_search(_tauri_app_handle: &AppHandle, _app_path: &str) -> Result<(), String> {
|
||||
// no-op
|
||||
Ok(())
|
||||
}
|
||||
@@ -85,8 +74,8 @@ pub fn is_app_search_enabled(_app_path: &str) -> bool {
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn add_app_search_path<R: Runtime>(
|
||||
_tauri_app_handle: AppHandle<R>,
|
||||
pub async fn add_app_search_path(
|
||||
_tauri_app_handle: AppHandle,
|
||||
_search_path: String,
|
||||
) -> Result<(), String> {
|
||||
// no-op
|
||||
@@ -94,8 +83,8 @@ pub async fn add_app_search_path<R: Runtime>(
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn remove_app_search_path<R: Runtime>(
|
||||
_tauri_app_handle: AppHandle<R>,
|
||||
pub async fn remove_app_search_path(
|
||||
_tauri_app_handle: AppHandle,
|
||||
_search_path: String,
|
||||
) -> Result<(), String> {
|
||||
// no-op
|
||||
@@ -103,43 +92,37 @@ pub async fn remove_app_search_path<R: Runtime>(
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn get_app_search_path<R: Runtime>(_tauri_app_handle: AppHandle<R>) -> Vec<String> {
|
||||
pub async fn get_app_search_path(_tauri_app_handle: AppHandle) -> Vec<String> {
|
||||
// Return an empty list
|
||||
Vec::new()
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn get_app_list<R: Runtime>(
|
||||
_tauri_app_handle: AppHandle<R>,
|
||||
) -> Result<Vec<Extension>, String> {
|
||||
pub async fn get_app_list(_tauri_app_handle: AppHandle) -> Result<Vec<Extension>, String> {
|
||||
// Return an empty list
|
||||
Ok(Vec::new())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn get_app_metadata<R: Runtime>(
|
||||
_tauri_app_handle: AppHandle<R>,
|
||||
pub async fn get_app_metadata(
|
||||
_tauri_app_handle: AppHandle,
|
||||
_app_path: String,
|
||||
) -> Result<AppMetadata, String> {
|
||||
unreachable!("app list should be empty, there is no way this can be invoked")
|
||||
}
|
||||
|
||||
pub(crate) fn set_apps_hotkey<R: Runtime>(_tauri_app_handle: &AppHandle<R>) -> Result<(), String> {
|
||||
pub(crate) fn set_apps_hotkey(_tauri_app_handle: &AppHandle) -> Result<(), String> {
|
||||
// no-op
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn unset_apps_hotkey<R: Runtime>(
|
||||
_tauri_app_handle: &AppHandle<R>,
|
||||
) -> Result<(), String> {
|
||||
pub(crate) fn unset_apps_hotkey(_tauri_app_handle: &AppHandle) -> Result<(), String> {
|
||||
// no-op
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn reindex_applications<R: Runtime>(
|
||||
_tauri_app_handle: AppHandle<R>,
|
||||
) -> Result<(), String> {
|
||||
pub async fn reindex_applications(_tauri_app_handle: AppHandle) -> Result<(), String> {
|
||||
// no-op
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -138,7 +138,7 @@ impl SearchSource for CalculatorSource {
|
||||
// will only be evaluated against non-whitespace characters.
|
||||
let query_string = query_string.trim();
|
||||
|
||||
if query_string.is_empty() || query_string.len() == 1 {
|
||||
if query_string.is_empty() {
|
||||
return Ok(QueryResponse {
|
||||
source: self.get_type(),
|
||||
hits: Vec::new(),
|
||||
@@ -150,6 +150,26 @@ impl SearchSource for CalculatorSource {
|
||||
let query_source = self.get_type();
|
||||
let base_score = self.base_score;
|
||||
let closure = move || -> QueryResponse {
|
||||
let Ok(tokens) = meval::tokenizer::tokenize(&query_string_clone) else {
|
||||
// Invalid expression, return nothing.
|
||||
return QueryResponse {
|
||||
source: query_source,
|
||||
hits: Vec::new(),
|
||||
total_hits: 0,
|
||||
};
|
||||
};
|
||||
// If it is only a number, no need to evaluate it as the result is
|
||||
// this number.
|
||||
// Actually, there is no need to return the result back to the users
|
||||
// in such case because letting them know "x = x" is meaningless.
|
||||
if tokens.len() == 1 && matches!(tokens[0], meval::tokenizer::Token::Number(_)) {
|
||||
return QueryResponse {
|
||||
source: query_source,
|
||||
hits: Vec::new(),
|
||||
total_hits: 0,
|
||||
};
|
||||
}
|
||||
|
||||
let res_num = meval::eval_str(&query_string_clone);
|
||||
|
||||
match res_num {
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
//! File Search configuration entries definition and getter/setter functions.
|
||||
|
||||
use crate::extension::built_in::file_search::implementation::apply_config;
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
use serde_json::Value;
|
||||
use std::sync::LazyLock;
|
||||
use tauri::AppHandle;
|
||||
use tauri::Runtime;
|
||||
use tauri_plugin_store::StoreExt;
|
||||
|
||||
// Tauri store keys for file system configuration
|
||||
@@ -24,7 +24,7 @@ static HOME_DIR: LazyLock<String> = LazyLock::new(|| {
|
||||
.expect("User home directory should be encoded with UTF-8")
|
||||
});
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Copy)]
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Copy, PartialEq)]
|
||||
pub enum SearchBy {
|
||||
Name,
|
||||
NameAndContents,
|
||||
@@ -54,7 +54,7 @@ impl Default for FileSearchConfig {
|
||||
}
|
||||
|
||||
impl FileSearchConfig {
|
||||
pub(crate) fn get<R: Runtime>(tauri_app_handle: &AppHandle<R>) -> Self {
|
||||
pub(crate) fn get(tauri_app_handle: &AppHandle) -> Self {
|
||||
let store = tauri_app_handle
|
||||
.store(TAURI_STORE_FILE_SYSTEM_CONFIG)
|
||||
.unwrap_or_else(|e| {
|
||||
@@ -185,28 +185,32 @@ impl FileSearchConfig {
|
||||
|
||||
// Tauri commands for managing file system configuration
|
||||
#[tauri::command]
|
||||
pub async fn get_file_system_config<R: Runtime>(
|
||||
tauri_app_handle: AppHandle<R>,
|
||||
) -> FileSearchConfig {
|
||||
pub async fn get_file_system_config(tauri_app_handle: AppHandle) -> FileSearchConfig {
|
||||
FileSearchConfig::get(&tauri_app_handle)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn set_file_system_config<R: Runtime>(
|
||||
tauri_app_handle: AppHandle<R>,
|
||||
pub async fn set_file_system_config(
|
||||
tauri_app_handle: AppHandle,
|
||||
config: FileSearchConfig,
|
||||
) -> Result<(), String> {
|
||||
let store = tauri_app_handle
|
||||
.store(TAURI_STORE_FILE_SYSTEM_CONFIG)
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
store.set(TAURI_STORE_KEY_SEARCH_PATHS, config.search_paths);
|
||||
store.set(TAURI_STORE_KEY_EXCLUDE_PATHS, config.exclude_paths);
|
||||
store.set(TAURI_STORE_KEY_FILE_TYPES, config.file_types);
|
||||
store.set(TAURI_STORE_KEY_SEARCH_PATHS, config.search_paths.as_slice());
|
||||
store.set(
|
||||
TAURI_STORE_KEY_EXCLUDE_PATHS,
|
||||
config.exclude_paths.as_slice(),
|
||||
);
|
||||
store.set(TAURI_STORE_KEY_FILE_TYPES, config.file_types.as_slice());
|
||||
store.set(
|
||||
TAURI_STORE_KEY_SEARCH_BY,
|
||||
serde_json::to_value(config.search_by).unwrap(),
|
||||
);
|
||||
|
||||
// Apply the config when we know that this set operation won't fail
|
||||
apply_config(&config)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -0,0 +1,388 @@
|
||||
//! File system powered by GNOME's Tracker engine.
|
||||
|
||||
use super::super::super::EXTENSION_ID;
|
||||
use super::super::super::config::FileSearchConfig;
|
||||
use super::super::should_be_filtered_out;
|
||||
use crate::common::document::DataSourceReference;
|
||||
use crate::extension::LOCAL_QUERY_SOURCE_TYPE;
|
||||
use crate::util::file::sync_get_file_icon;
|
||||
use crate::{
|
||||
common::document::{Document, OnOpened},
|
||||
extension::built_in::file_search::config::SearchBy,
|
||||
};
|
||||
use camino::Utf8Path;
|
||||
use gio::Cancellable;
|
||||
use gio::Settings;
|
||||
use gio::prelude::SettingsExtManual;
|
||||
use glib::GString;
|
||||
use glib::collections::strv::StrV;
|
||||
use tracker::{SparqlConnection, SparqlCursor, prelude::SparqlCursorExtManual};
|
||||
|
||||
/// The service that we will connect to.
|
||||
const SERVICE_NAME: &str = "org.freedesktop.Tracker3.Miner.Files";
|
||||
|
||||
/// Tracker won't return scores when we are not using full-text seach. In that
|
||||
/// case, we use this score.
|
||||
const SCORE: f64 = 1.0;
|
||||
|
||||
/// Helper function to return different SPARQL queries depending on the different configurations.
|
||||
fn query_sparql(query_string: &str, config: &FileSearchConfig) -> String {
|
||||
match config.search_by {
|
||||
SearchBy::Name => {
|
||||
// Cannot use the inverted index as that searches for all the attributes,
|
||||
// but we only want to search the filename.
|
||||
format!(
|
||||
"SELECT nie:url(?file_item) WHERE {{ ?file_item nfo:fileName ?fileName . FILTER(regex(?fileName, '{query_string}', 'i')) }}"
|
||||
)
|
||||
}
|
||||
SearchBy::NameAndContents => {
|
||||
// Full-text search against all attributes
|
||||
// OR
|
||||
// filename search
|
||||
format!(
|
||||
"SELECT nie:url(?file_item) fts:rank(?file_item) WHERE {{ {{ ?file_item fts:match '{query_string}' }} UNION {{ ?file_item nfo:fileName ?fileName . FILTER(regex(?fileName, '{query_string}', 'i')) }} }} ORDER BY DESC fts:rank(?file_item)"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Helper function to replace unsupported characters with whitespace.
|
||||
///
|
||||
/// Tracker will error out if it encounters these characters.
|
||||
///
|
||||
/// The complete list of unsupported characters is unknown and we don't know how
|
||||
/// to escape them, so let's replace them.
|
||||
fn query_string_cleanup(old: &str) -> String {
|
||||
const UNSUPPORTED_CHAR: [char; 3] = ['\'', '\n', '\\'];
|
||||
|
||||
// Using len in bytes is ok
|
||||
let mut chars = Vec::with_capacity(old.len());
|
||||
for char in old.chars() {
|
||||
if UNSUPPORTED_CHAR.contains(&char) {
|
||||
chars.push(' ');
|
||||
} else {
|
||||
chars.push(char);
|
||||
}
|
||||
}
|
||||
|
||||
chars.into_iter().collect()
|
||||
}
|
||||
|
||||
struct Query {
|
||||
conn: SparqlConnection,
|
||||
cursor: SparqlCursor,
|
||||
}
|
||||
|
||||
impl Query {
|
||||
fn new(query_string: &str, config: &FileSearchConfig) -> Result<Self, String> {
|
||||
let query_string = query_string_cleanup(query_string);
|
||||
let sparql = query_sparql(&query_string, config);
|
||||
let conn =
|
||||
SparqlConnection::bus_new(SERVICE_NAME, None, None).map_err(|e| e.to_string())?;
|
||||
let cursor = conn
|
||||
.query(&sparql, Cancellable::NONE)
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
Ok(Self { conn, cursor })
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Query {
|
||||
fn drop(&mut self) {
|
||||
self.cursor.close();
|
||||
self.conn.close();
|
||||
}
|
||||
}
|
||||
|
||||
impl Iterator for Query {
|
||||
/// It yields a tuple `(file path, score)`
|
||||
type Item = Result<(String, f64), String>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
loop {
|
||||
let has_next = match self
|
||||
.cursor
|
||||
.next(Cancellable::NONE)
|
||||
.map_err(|e| e.to_string())
|
||||
{
|
||||
Ok(has_next) => has_next,
|
||||
Err(err_str) => return Some(Err(err_str)),
|
||||
};
|
||||
|
||||
if !has_next {
|
||||
return None;
|
||||
}
|
||||
|
||||
// The first column is the URL
|
||||
let file_url_column = self.cursor.string(0);
|
||||
// It could be None (or NULL ptr if you use C), I have no clue why.
|
||||
let opt_str = file_url_column.as_ref().map(|gstr| gstr.as_str());
|
||||
|
||||
match opt_str {
|
||||
Some(url) => {
|
||||
// The returned URL has a prefix that we need to trim
|
||||
const PREFIX: &str = "file://";
|
||||
const PREFIX_LEN: usize = PREFIX.len();
|
||||
|
||||
let file_path = url[PREFIX_LEN..].to_string();
|
||||
assert!(!file_path.is_empty());
|
||||
assert_ne!(file_path, "/", "file search should not hit the root path");
|
||||
|
||||
let score = {
|
||||
// The second column is the score, this column may not
|
||||
// exist. We use SCORE if the real value is absent.
|
||||
let score_column = self.cursor.string(1);
|
||||
let opt_score_str = score_column.as_ref().map(|g_str| g_str.as_str());
|
||||
let opt_score = opt_score_str.map(|str| {
|
||||
str.parse::<f64>()
|
||||
.expect("score should be valid for type f64")
|
||||
});
|
||||
|
||||
opt_score.unwrap_or(SCORE)
|
||||
};
|
||||
|
||||
return Some(Ok((file_path, score)));
|
||||
}
|
||||
None => {
|
||||
// another try
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn hits(
|
||||
query_string: &str,
|
||||
from: usize,
|
||||
size: usize,
|
||||
config: &FileSearchConfig,
|
||||
) -> Result<Vec<(Document, f64)>, String> {
|
||||
// Special cases that will make querying faster.
|
||||
if query_string.is_empty() || size == 0 || config.search_paths.is_empty() {
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
|
||||
let mut result_hits = Vec::with_capacity(size);
|
||||
|
||||
let need_to_skip = {
|
||||
if matches!(config.search_by, SearchBy::Name) {
|
||||
// We don't use full-text search in this case, the returned documents
|
||||
// won't be scored, the query hits won't be sorted, so processing the
|
||||
// from parameter is meaningless.
|
||||
false
|
||||
} else {
|
||||
from > 0
|
||||
}
|
||||
};
|
||||
let mut num_skipped = 0;
|
||||
let should_skip = from;
|
||||
|
||||
let query = Query::new(query_string, config)?;
|
||||
for res_entry in query {
|
||||
let (file_path, score) = res_entry?;
|
||||
|
||||
// This should be called before processing the `from` parameter.
|
||||
if should_be_filtered_out(config, &file_path, true, true, true) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Process the `from` parameter.
|
||||
if need_to_skip && num_skipped < should_skip {
|
||||
// Skip this
|
||||
num_skipped += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
let icon = sync_get_file_icon(&file_path);
|
||||
let file_path_of_type_path = camino::Utf8Path::new(&file_path);
|
||||
let r#where = file_path_of_type_path
|
||||
.parent()
|
||||
.unwrap_or_else(|| {
|
||||
panic!(
|
||||
"expect path [{}] to have a parent, but it does not",
|
||||
file_path
|
||||
);
|
||||
})
|
||||
.to_string();
|
||||
|
||||
let file_name = file_path_of_type_path.file_name().unwrap_or_else(|| {
|
||||
panic!(
|
||||
"expect path [{}] to have a file name, but it does not",
|
||||
file_path
|
||||
);
|
||||
});
|
||||
let on_opened = OnOpened::Document {
|
||||
url: file_path.to_string(),
|
||||
};
|
||||
|
||||
let doc = Document {
|
||||
id: file_path.to_string(),
|
||||
title: Some(file_name.to_string()),
|
||||
source: Some(DataSourceReference {
|
||||
r#type: Some(LOCAL_QUERY_SOURCE_TYPE.into()),
|
||||
name: Some(EXTENSION_ID.into()),
|
||||
id: Some(EXTENSION_ID.into()),
|
||||
icon: Some(String::from("font_Filesearch")),
|
||||
}),
|
||||
category: Some(r#where),
|
||||
on_opened: Some(on_opened),
|
||||
url: Some(file_path),
|
||||
icon: Some(icon.to_string()),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
result_hits.push((doc, score));
|
||||
|
||||
// Collected enough documents, return
|
||||
if result_hits.len() >= size {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(result_hits)
|
||||
}
|
||||
|
||||
fn ensure_path_in_recursive_indexing_scope(list: &mut StrV, path: &str) {
|
||||
for item in list.iter() {
|
||||
let item_path = Utf8Path::new(item.as_str());
|
||||
let path = Utf8Path::new(path);
|
||||
|
||||
// It is already covered or listed
|
||||
if path.starts_with(item_path) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
list.push(
|
||||
GString::from_utf8_checked(path.as_bytes().to_vec())
|
||||
.expect("search_path_str contains an interior NUL"),
|
||||
);
|
||||
}
|
||||
|
||||
fn ensure_path_and_descendants_not_in_single_indexing_scope(list: &mut StrV, path: &str) {
|
||||
// Indexes to the items that should be removed
|
||||
let mut item_to_remove = Vec::new();
|
||||
for (idx, item) in list.iter().enumerate() {
|
||||
let item_path = Utf8Path::new(item.as_str());
|
||||
let path = Utf8Path::new(path);
|
||||
|
||||
if item_path.starts_with(path) {
|
||||
item_to_remove.push(idx);
|
||||
}
|
||||
}
|
||||
|
||||
// Reverse the indexes so that the remove operation won't invalidate them.
|
||||
for idx in item_to_remove.into_iter().rev() {
|
||||
list.remove(idx);
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn apply_config(config: &FileSearchConfig) -> Result<(), String> {
|
||||
// Tracker provides the following configuration entries to allow users to
|
||||
// tweak the indexing scope:
|
||||
//
|
||||
// 1. ignored-directories: A list of names, directories with such names will be ignored.
|
||||
// ['po', 'CVS', 'core-dumps', 'lost+found']
|
||||
// 2. ignored-directories-with-content: Avoid any directory containing a file blocklisted here
|
||||
// ['.trackerignore', '.git', '.hg', '.nomedia']
|
||||
// 3. ignored-files: List of file patterns to avoid
|
||||
// ['*~', '*.o', '*.la', '*.lo', '*.loT', '*.in', '*.m4', '*.rej', ...]
|
||||
// 4. index-recursive-directories: List of directories to index recursively
|
||||
// ['&DESKTOP', '&DOCUMENTS', '&MUSIC', '&PICTURES', '&VIDEOS']
|
||||
// 5. index-single-directories: List of directories to index without inspecting subfolders,
|
||||
// ['$HOME', '&DOWNLOAD']
|
||||
//
|
||||
// The first 3 entries specify patterns, in order to use them, we have to walk
|
||||
// through the whole directory tree listed in search paths, which is impractical.
|
||||
// So we only use the last 2 entries.
|
||||
//
|
||||
//
|
||||
// Just want to mention that setting search path to "/home" could break Tracker:
|
||||
//
|
||||
// ```text
|
||||
// Unknown target graph for uri:'file:///home' and mime:'inode/directory'
|
||||
// ```
|
||||
//
|
||||
// See the related bug reports:
|
||||
//
|
||||
// https://gitlab.gnome.org/GNOME/localsearch/-/issues/313
|
||||
// https://bugs.launchpad.net/bugs/2077181
|
||||
//
|
||||
//
|
||||
// There is nothing we can do.
|
||||
|
||||
const TRACKER_SETTINGS_SCHEMA: &str = "org.freedesktop.Tracker3.Miner.Files";
|
||||
const KEY_INDEX_RECURSIVE_DIRECTORIES: &str = "index-recursive-directories";
|
||||
const KEY_INDEX_SINGLE_DIRECTORIES: &str = "index-single-directories";
|
||||
|
||||
let search_paths = &config.search_paths;
|
||||
|
||||
let settings = Settings::new(TRACKER_SETTINGS_SCHEMA);
|
||||
let mut recursive_list: StrV = settings.strv(KEY_INDEX_RECURSIVE_DIRECTORIES);
|
||||
let mut single_list: StrV = settings.strv(KEY_INDEX_SINGLE_DIRECTORIES);
|
||||
|
||||
for search_path in search_paths {
|
||||
// We want our search path to be included in the recursive directories or
|
||||
// any directory within the list covers it.
|
||||
ensure_path_in_recursive_indexing_scope(&mut recursive_list, search_path);
|
||||
// We want our search path and its any descendants are not listed in
|
||||
// the index directories list.
|
||||
ensure_path_and_descendants_not_in_single_indexing_scope(&mut single_list, search_path);
|
||||
}
|
||||
|
||||
settings
|
||||
.set_strv(KEY_INDEX_RECURSIVE_DIRECTORIES, recursive_list)
|
||||
.expect("key is not read-only");
|
||||
settings
|
||||
.set_strv(KEY_INDEX_SINGLE_DIRECTORIES, single_list)
|
||||
.expect("key is not be read-only");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_query_string_cleanup_basic() {
|
||||
assert_eq!(query_string_cleanup("test"), "test");
|
||||
assert_eq!(query_string_cleanup("hello world"), "hello world");
|
||||
assert_eq!(query_string_cleanup("file.txt"), "file.txt");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_query_string_cleanup_unsupported_chars() {
|
||||
assert_eq!(query_string_cleanup("test'file"), "test file");
|
||||
assert_eq!(query_string_cleanup("test\nfile"), "test file");
|
||||
assert_eq!(query_string_cleanup("test\\file"), "test file");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_query_string_cleanup_multiple_unsupported() {
|
||||
assert_eq!(query_string_cleanup("test'file\nname"), "test file name");
|
||||
assert_eq!(query_string_cleanup("test\'file"), "test file");
|
||||
assert_eq!(query_string_cleanup("\n'test"), " test");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_query_string_cleanup_edge_cases() {
|
||||
assert_eq!(query_string_cleanup(""), "");
|
||||
assert_eq!(query_string_cleanup("'"), " ");
|
||||
assert_eq!(query_string_cleanup("\n"), " ");
|
||||
assert_eq!(query_string_cleanup("\\"), " ");
|
||||
assert_eq!(query_string_cleanup(" '\n\\ "), " ");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_query_string_cleanup_mixed_content() {
|
||||
assert_eq!(
|
||||
query_string_cleanup("document's content\nwith\\backslash"),
|
||||
"document s content with backslash"
|
||||
);
|
||||
assert_eq!(
|
||||
query_string_cleanup("path/to'file\nextension\\test"),
|
||||
"path/to file extension test"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,308 @@
|
||||
//! File search for KDE, powered by its Baloo engine.
|
||||
|
||||
use super::super::super::EXTENSION_ID;
|
||||
use super::super::super::config::FileSearchConfig;
|
||||
use super::super::super::config::SearchBy;
|
||||
use super::super::should_be_filtered_out;
|
||||
use crate::common::document::{DataSourceReference, Document};
|
||||
use crate::extension::LOCAL_QUERY_SOURCE_TYPE;
|
||||
use crate::extension::OnOpened;
|
||||
use crate::util::file::sync_get_file_icon;
|
||||
use camino::Utf8Path;
|
||||
use configparser::ini::Ini;
|
||||
use configparser::ini::WriteOptions;
|
||||
use futures::stream::Stream;
|
||||
use futures::stream::StreamExt;
|
||||
use std::os::fd::OwnedFd;
|
||||
use std::path::PathBuf;
|
||||
use tokio::io::AsyncBufReadExt;
|
||||
use tokio::io::BufReader;
|
||||
use tokio::process::Child;
|
||||
use tokio::process::Command;
|
||||
use tokio_stream::wrappers::LinesStream;
|
||||
|
||||
/// Baloo does not support scoring, use this score for all the documents.
|
||||
const SCORE: f64 = 1.0;
|
||||
|
||||
/// KDE6 updates the binary name to "baloosearch6", but I believe there still have
|
||||
/// distros using the original name. So we need to check both.
|
||||
fn cli_tool_lookup() -> Option<PathBuf> {
|
||||
use which::which;
|
||||
|
||||
let res_path = which("baloosearch").or_else(|_| which("baloosearch6"));
|
||||
res_path.ok()
|
||||
}
|
||||
|
||||
pub(crate) async fn hits(
|
||||
query_string: &str,
|
||||
_from: usize,
|
||||
size: usize,
|
||||
config: &FileSearchConfig,
|
||||
) -> Result<Vec<(Document, f64)>, String> {
|
||||
// Special cases that will make querying faster.
|
||||
if query_string.is_empty() || size == 0 || config.search_paths.is_empty() {
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
|
||||
// If the tool is not found, return an empty result as well.
|
||||
let Some(tool_path) = cli_tool_lookup() else {
|
||||
return Ok(Vec::new());
|
||||
};
|
||||
|
||||
let (mut iter, _baloosearch_child_process) =
|
||||
execute_baloosearch_query(tool_path, query_string, size, config)?;
|
||||
|
||||
// Convert results to documents
|
||||
let mut hits: Vec<(Document, f64)> = Vec::new();
|
||||
while let Some(res_file_path) = iter.next().await {
|
||||
let file_path = res_file_path.map_err(|io_err| io_err.to_string())?;
|
||||
|
||||
let icon = sync_get_file_icon(&file_path);
|
||||
let file_path_of_type_path = camino::Utf8Path::new(&file_path);
|
||||
let r#where = file_path_of_type_path
|
||||
.parent()
|
||||
.unwrap_or_else(|| {
|
||||
panic!(
|
||||
"expect path [{}] to have a parent, but it does not",
|
||||
file_path
|
||||
);
|
||||
})
|
||||
.to_string();
|
||||
|
||||
let file_name = file_path_of_type_path.file_name().unwrap_or_else(|| {
|
||||
panic!(
|
||||
"expect path [{}] to have a file name, but it does not",
|
||||
file_path
|
||||
);
|
||||
});
|
||||
let on_opened = OnOpened::Document {
|
||||
url: file_path.clone(),
|
||||
};
|
||||
|
||||
let doc = Document {
|
||||
id: file_path.clone(),
|
||||
title: Some(file_name.to_string()),
|
||||
source: Some(DataSourceReference {
|
||||
r#type: Some(LOCAL_QUERY_SOURCE_TYPE.into()),
|
||||
name: Some(EXTENSION_ID.into()),
|
||||
id: Some(EXTENSION_ID.into()),
|
||||
icon: Some(String::from("font_Filesearch")),
|
||||
}),
|
||||
category: Some(r#where),
|
||||
on_opened: Some(on_opened),
|
||||
url: Some(file_path),
|
||||
icon: Some(icon.to_string()),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
hits.push((doc, SCORE));
|
||||
}
|
||||
|
||||
Ok(hits)
|
||||
}
|
||||
|
||||
/// Return an array containing the `baloosearch` command and its arguments.
|
||||
fn build_baloosearch_query(
|
||||
tool_path: PathBuf,
|
||||
query_string: &str,
|
||||
config: &FileSearchConfig,
|
||||
) -> Vec<String> {
|
||||
let tool_path = tool_path
|
||||
.into_os_string()
|
||||
.into_string()
|
||||
.expect("binary path should be UTF-8 encoded");
|
||||
|
||||
let mut args = vec![tool_path];
|
||||
|
||||
match config.search_by {
|
||||
SearchBy::Name => {
|
||||
args.push(format!("filename:{query_string}"));
|
||||
}
|
||||
SearchBy::NameAndContents => {
|
||||
args.push(query_string.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
for search_path in config.search_paths.iter() {
|
||||
args.extend_from_slice(&["-d".into(), search_path.clone()]);
|
||||
}
|
||||
|
||||
args
|
||||
}
|
||||
|
||||
/// Spawn the `baloosearch` child process and return an async iterator over its output,
|
||||
/// allowing us to collect the results asynchronously.
|
||||
///
|
||||
/// # Return value:
|
||||
///
|
||||
/// * impl Stream: an async iterator that will yield the matched files
|
||||
/// * Child: The handle to the baloosearch process. The child process will be
|
||||
/// killed when this handle gets dropped so we need to keep it alive util we
|
||||
/// exhaust the stream.
|
||||
fn execute_baloosearch_query(
|
||||
tool_path: PathBuf,
|
||||
query_string: &str,
|
||||
size: usize,
|
||||
config: &FileSearchConfig,
|
||||
) -> Result<(impl Stream<Item = std::io::Result<String>>, Child), String> {
|
||||
let args = build_baloosearch_query(tool_path, query_string, config);
|
||||
|
||||
let (rx, tx) = std::io::pipe().unwrap();
|
||||
let rx_owned = OwnedFd::from(rx);
|
||||
let async_rx = tokio::net::unix::pipe::Receiver::from_owned_fd(rx_owned).unwrap();
|
||||
let buffered_rx = BufReader::new(async_rx);
|
||||
let lines = LinesStream::new(buffered_rx.lines());
|
||||
|
||||
let child = Command::new(&args[0])
|
||||
.args(&args[1..])
|
||||
.stdout(tx)
|
||||
.stderr(std::process::Stdio::null())
|
||||
// The child process will be killed when the Child instance gets dropped.
|
||||
.kill_on_drop(true)
|
||||
.spawn()
|
||||
.map_err(|e| format!("Failed to spawn baloosearch: {e}"))?;
|
||||
let config_clone = config.clone();
|
||||
let iter = lines
|
||||
.filter(move |res_path| {
|
||||
std::future::ready({
|
||||
match res_path {
|
||||
Ok(path) => !should_be_filtered_out(&config_clone, path, false, true, true),
|
||||
Err(_) => {
|
||||
// Don't filter out Err() values
|
||||
true
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
.take(size);
|
||||
|
||||
Ok((iter, child))
|
||||
}
|
||||
|
||||
pub(crate) fn apply_config(config: &FileSearchConfig) -> Result<(), String> {
|
||||
// Users can tweak Baloo via its configuration file, below are the fields that
|
||||
// we need to modify:
|
||||
//
|
||||
// * Indexing-Enabled: turn indexing on or off
|
||||
// * only basic indexing: If true, Baloo only indexes file names
|
||||
// * folders: directories to index
|
||||
// * exclude folders: directories to skip
|
||||
//
|
||||
// ```ini
|
||||
// [Basic Settings]
|
||||
// Indexing-Enabled=true
|
||||
//
|
||||
// [General]
|
||||
// only basic indexing=true
|
||||
// folders[$e]=$HOME/
|
||||
// exclude folders[$e]=$HOME/FolderA/,$HOME/FolderB/
|
||||
// ```
|
||||
|
||||
const SECTION_GENERAL: &str = "General";
|
||||
const KEY_INCLUDE_FOLDERS: &str = "folders[$e]";
|
||||
const KEY_EXCLUDE_FOLDERS: &str = "exclude folders[$e]";
|
||||
const FOLDERS_SEPARATOR: &str = ",";
|
||||
|
||||
let rc_file_path = {
|
||||
let mut home = dirs::home_dir()
|
||||
.expect("cannot find the home directory, Coco should never run in such a environment");
|
||||
home.push(".config/baloofilerc");
|
||||
home
|
||||
};
|
||||
|
||||
// Parse and load the rc file, it is in format INI
|
||||
//
|
||||
// Use `new_cs()`, the case-sensitive version of constructor as the config
|
||||
// file contains uppercase letters, so it is case-sensitive.
|
||||
let mut baloo_config = Ini::new_cs();
|
||||
if rc_file_path.try_exists().map_err(|e| e.to_string())? {
|
||||
let _ = baloo_config.load(rc_file_path.as_path())?;
|
||||
}
|
||||
|
||||
// Ensure indexing is enabled
|
||||
let _ = baloo_config.setstr("Basic Settings", "Indexing-Enabled", Some("true"));
|
||||
|
||||
// Let baloo index file content if we need that
|
||||
if config.search_by == SearchBy::NameAndContents {
|
||||
let _ = baloo_config.setstr(SECTION_GENERAL, "only basic indexing", Some("false"));
|
||||
}
|
||||
|
||||
let mut include_folders = {
|
||||
match baloo_config.get(SECTION_GENERAL, KEY_INCLUDE_FOLDERS) {
|
||||
Some(str) => str
|
||||
.split(FOLDERS_SEPARATOR)
|
||||
.map(|str| str.to_string())
|
||||
.collect::<Vec<String>>(),
|
||||
None => Vec::new(),
|
||||
}
|
||||
};
|
||||
|
||||
let mut exclude_folders = {
|
||||
match baloo_config.get(SECTION_GENERAL, KEY_EXCLUDE_FOLDERS) {
|
||||
Some(str) => str
|
||||
.split(FOLDERS_SEPARATOR)
|
||||
.map(|str| str.to_string())
|
||||
.collect::<Vec<String>>(),
|
||||
None => Vec::new(),
|
||||
}
|
||||
};
|
||||
|
||||
fn ensure_path_included_include_folders(
|
||||
include_folders: &mut Vec<String>,
|
||||
search_path: &Utf8Path,
|
||||
) {
|
||||
for include_folder in include_folders.iter() {
|
||||
let include_folder = Utf8Path::new(include_folder.as_str());
|
||||
if search_path.starts_with(include_folder) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
include_folders.push(search_path.as_str().to_string());
|
||||
}
|
||||
|
||||
fn ensure_path_and_descendants_not_excluded(
|
||||
exclude_folders: &mut Vec<String>,
|
||||
search_path: &Utf8Path,
|
||||
) {
|
||||
let mut items_to_remove = Vec::new();
|
||||
for (idx, exclude_folder) in exclude_folders.iter().enumerate() {
|
||||
let exclude_folder = Utf8Path::new(exclude_folder);
|
||||
|
||||
if exclude_folder.starts_with(search_path) {
|
||||
items_to_remove.push(idx);
|
||||
}
|
||||
}
|
||||
|
||||
for idx in items_to_remove.into_iter().rev() {
|
||||
exclude_folders.remove(idx);
|
||||
}
|
||||
}
|
||||
|
||||
for search_path in config.search_paths.iter() {
|
||||
let search_path = Utf8Path::new(search_path.as_str());
|
||||
|
||||
ensure_path_included_include_folders(&mut include_folders, search_path);
|
||||
ensure_path_and_descendants_not_excluded(&mut exclude_folders, search_path);
|
||||
}
|
||||
|
||||
let include_folders_str: String = include_folders.as_slice().join(FOLDERS_SEPARATOR);
|
||||
let exclude_folders_str: String = exclude_folders.as_slice().join(FOLDERS_SEPARATOR);
|
||||
|
||||
let _ = baloo_config.set(
|
||||
SECTION_GENERAL,
|
||||
KEY_INCLUDE_FOLDERS,
|
||||
Some(include_folders_str),
|
||||
);
|
||||
let _ = baloo_config.set(
|
||||
SECTION_GENERAL,
|
||||
KEY_EXCLUDE_FOLDERS,
|
||||
Some(exclude_folders_str),
|
||||
);
|
||||
|
||||
baloo_config
|
||||
.pretty_write(rc_file_path.as_path(), &WriteOptions::new())
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
mod gnome;
|
||||
mod kde;
|
||||
|
||||
use super::super::config::FileSearchConfig;
|
||||
use crate::common::document::Document;
|
||||
use crate::util::LinuxDesktopEnvironment;
|
||||
use crate::util::get_linux_desktop_environment;
|
||||
use std::ops::Deref;
|
||||
use std::sync::LazyLock;
|
||||
|
||||
static DESKTOP_ENVIRONMENT: LazyLock<Option<LinuxDesktopEnvironment>> =
|
||||
LazyLock::new(|| get_linux_desktop_environment());
|
||||
|
||||
/// Dispatch to implementations powered by different backends.
|
||||
pub(crate) async fn hits(
|
||||
query_string: &str,
|
||||
from: usize,
|
||||
size: usize,
|
||||
config: &FileSearchConfig,
|
||||
) -> Result<Vec<(Document, f64)>, String> {
|
||||
let de = DESKTOP_ENVIRONMENT.deref();
|
||||
match de {
|
||||
Some(LinuxDesktopEnvironment::Gnome) => gnome::hits(query_string, from, size, config).await,
|
||||
Some(LinuxDesktopEnvironment::Kde) => kde::hits(query_string, from, size, config).await,
|
||||
Some(LinuxDesktopEnvironment::Unsupported {
|
||||
xdg_current_desktop: _,
|
||||
}) => {
|
||||
return Err("file search is not supported on this desktop environment".into());
|
||||
}
|
||||
None => {
|
||||
return Err("could not determine Linux desktop environment".into());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn apply_config(config: &FileSearchConfig) -> Result<(), String> {
|
||||
let de = DESKTOP_ENVIRONMENT.deref();
|
||||
match de {
|
||||
Some(LinuxDesktopEnvironment::Gnome) => gnome::apply_config(config),
|
||||
Some(LinuxDesktopEnvironment::Kde) => kde::apply_config(config),
|
||||
Some(LinuxDesktopEnvironment::Unsupported {
|
||||
xdg_current_desktop: _,
|
||||
}) => {
|
||||
return Err("file search is not supported on this desktop environment".into());
|
||||
}
|
||||
None => {
|
||||
return Err("could not determine Linux desktop environment".into());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,11 @@
|
||||
use super::super::EXTENSION_ID;
|
||||
use super::super::config::FileSearchConfig;
|
||||
use super::super::config::SearchBy;
|
||||
use super::should_be_filtered_out;
|
||||
use crate::common::document::{DataSourceReference, Document};
|
||||
use crate::extension::LOCAL_QUERY_SOURCE_TYPE;
|
||||
use crate::extension::OnOpened;
|
||||
use crate::util::file::get_file_icon;
|
||||
use crate::util::file::sync_get_file_icon;
|
||||
use futures::stream::Stream;
|
||||
use futures::stream::StreamExt;
|
||||
use std::os::fd::OwnedFd;
|
||||
@@ -24,7 +25,7 @@ pub(crate) async fn hits(
|
||||
size: usize,
|
||||
config: &FileSearchConfig,
|
||||
) -> Result<Vec<(Document, f64)>, String> {
|
||||
let (mut iter, mut mdfind_child_process) =
|
||||
let (mut iter, _mdfind_child_process) =
|
||||
execute_mdfind_query(&query_string, from, size, &config)?;
|
||||
|
||||
// Convert results to documents
|
||||
@@ -32,7 +33,7 @@ pub(crate) async fn hits(
|
||||
while let Some(res_file_path) = iter.next().await {
|
||||
let file_path = res_file_path.map_err(|io_err| io_err.to_string())?;
|
||||
|
||||
let icon = get_file_icon(file_path.clone()).await;
|
||||
let icon = sync_get_file_icon(&file_path);
|
||||
let file_path_of_type_path = camino::Utf8Path::new(&file_path);
|
||||
let r#where = file_path_of_type_path
|
||||
.parent()
|
||||
@@ -72,12 +73,6 @@ pub(crate) async fn hits(
|
||||
|
||||
hits.push((doc, SCORE));
|
||||
}
|
||||
// Kill the mdfind process once we get the needed results to prevent zombie
|
||||
// processes.
|
||||
mdfind_child_process
|
||||
.kill()
|
||||
.await
|
||||
.map_err(|e| format!("{:?}", e))?;
|
||||
|
||||
Ok(hits)
|
||||
}
|
||||
@@ -88,13 +83,28 @@ fn build_mdfind_query(query_string: &str, config: &FileSearchConfig) -> Vec<Stri
|
||||
|
||||
match config.search_by {
|
||||
SearchBy::Name => {
|
||||
args.push(format!("kMDItemFSName == '*{}*'", query_string));
|
||||
// The tailing char 'c' makes the search case-insensitive.
|
||||
//
|
||||
// According to [1], we should use this syntax "kMDItemFSName ==[c] '*{}*'",
|
||||
// but it does not work on my machine (macOS 26 beta 7), and you
|
||||
// can find similar complaints as well [2].
|
||||
//
|
||||
// [1]: https://developer.apple.com/library/archive/documentation/Carbon/Conceptual/SpotlightQuery/Concepts/QueryFormat.html
|
||||
// [2]: https://apple.stackexchange.com/q/263671/394687
|
||||
args.push(format!("kMDItemFSName == '*{}*'c", query_string));
|
||||
}
|
||||
SearchBy::NameAndContents => {
|
||||
args.push(format!(
|
||||
"kMDItemFSName == '*{}*' || kMDItemTextContent == '{}'",
|
||||
query_string, query_string
|
||||
));
|
||||
// Do not specify any File System Metadata Attribute Keys to search
|
||||
// all of them, it is case-insensitive by default.
|
||||
//
|
||||
// Previously, we use:
|
||||
//
|
||||
// "kMDItemFSName == '*{}*' || kMDItemTextContent == '{}'"
|
||||
//
|
||||
// But the kMDItemTextContent attribute does not work as expected.
|
||||
// For example, if a PDF document contains both "Waterloo" and
|
||||
// "waterloo", it is only matched by "Waterloo".
|
||||
args.push(query_string.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -114,8 +124,9 @@ fn build_mdfind_query(query_string: &str, config: &FileSearchConfig) -> Vec<Stri
|
||||
/// # Return value:
|
||||
///
|
||||
/// * impl Stream: an async iterator that will yield the matched files
|
||||
/// * Child: The handle to the mdfind process, we need to kill it once we
|
||||
/// collect all the results to avoid zombie processes.
|
||||
/// * Child: The handle to the mdfind process. The child process will be killed
|
||||
/// when this handle gets dropped, we need to keep it alive until we exhaust
|
||||
/// all the query results.
|
||||
fn execute_mdfind_query(
|
||||
query_string: &str,
|
||||
from: usize,
|
||||
@@ -133,6 +144,7 @@ fn execute_mdfind_query(
|
||||
.args(&args[1..])
|
||||
.stdout(tx)
|
||||
.stderr(std::process::Stdio::null())
|
||||
.kill_on_drop(true)
|
||||
.spawn()
|
||||
.map_err(|e| format!("Failed to spawn mdfind: {}", e))?;
|
||||
let config_clone = config.clone();
|
||||
@@ -140,7 +152,7 @@ fn execute_mdfind_query(
|
||||
.filter(move |res_path| {
|
||||
std::future::ready({
|
||||
match res_path {
|
||||
Ok(path) => !should_be_filtered_out(&config_clone, path),
|
||||
Ok(path) => !should_be_filtered_out(&config_clone, path, false, true, true),
|
||||
Err(_) => {
|
||||
// Don't filter out Err() values
|
||||
true
|
||||
@@ -154,33 +166,25 @@ fn execute_mdfind_query(
|
||||
Ok((iter, child))
|
||||
}
|
||||
|
||||
/// If `file_path` should be removed from the search results given the filter
|
||||
/// conditions specified in `config`.
|
||||
fn should_be_filtered_out(config: &FileSearchConfig, file_path: &str) -> bool {
|
||||
let is_excluded = config
|
||||
.exclude_paths
|
||||
.iter()
|
||||
.any(|exclude_path| file_path.starts_with(exclude_path));
|
||||
|
||||
if is_excluded {
|
||||
return true;
|
||||
}
|
||||
|
||||
let matches_file_type = if config.file_types.is_empty() {
|
||||
true
|
||||
} else {
|
||||
let path_obj = camino::Utf8Path::new(&file_path);
|
||||
if let Some(extension) = path_obj.extension() {
|
||||
config
|
||||
.file_types
|
||||
.iter()
|
||||
.any(|file_type| file_type == extension)
|
||||
} else {
|
||||
// `config.file_types` is not empty, then the search results
|
||||
// should have extensions.
|
||||
false
|
||||
}
|
||||
};
|
||||
|
||||
!matches_file_type
|
||||
pub(crate) fn apply_config(_: &FileSearchConfig) -> Result<(), String> {
|
||||
// By default, macOS indexes all the files within a volume if indexing is
|
||||
// enabled. So, to ensure our search paths are indexed by Spotlight,
|
||||
// theoretically, we can do the following things:
|
||||
//
|
||||
// 1. Ensure indexing is enabled on the volumes where our search paths reside.
|
||||
// However, we cannot do this as doing so requires `sudo`.
|
||||
//
|
||||
// 2. Ensure the search paths are not excluded from indexing scope. Users can
|
||||
// stop Spotlight from indexing a directory by:
|
||||
// 1. adding it to the "Privacy" list in 'System Settings'. Coco cannot
|
||||
// modify this list, since the only way to change it is manually
|
||||
// through System Settings.
|
||||
// 2. Renaming directory name, adding a `.noindex` file extension to it.
|
||||
// I don't want to use this trick, users won't feel comfortable and it
|
||||
// could break at any time.
|
||||
// 3. Creating a `.metadata_never_index` file within the directory (no longer works
|
||||
// since macOS Mojave)
|
||||
//
|
||||
// There is nothing we can do.
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,10 +1,396 @@
|
||||
#[cfg(target_os = "macos")]
|
||||
mod macos;
|
||||
#[cfg(target_os = "windows")]
|
||||
mod windows;
|
||||
use cfg_if::cfg_if;
|
||||
|
||||
// `hits()` function is platform-specific, export the corresponding impl.
|
||||
#[cfg(target_os = "macos")]
|
||||
pub(crate) use macos::hits;
|
||||
#[cfg(target_os = "windows")]
|
||||
pub(crate) use windows::hits;
|
||||
// * hits: the implementation of search
|
||||
//
|
||||
// * apply_config: Routines that should be performed to keep "other things"
|
||||
// synchronous with the passed configuration.
|
||||
// Currently, "other things" only include system indexer's setting entries.
|
||||
cfg_if! {
|
||||
if #[cfg(target_os = "linux")] {
|
||||
mod linux;
|
||||
pub(crate) use linux::hits;
|
||||
pub(crate) use linux::apply_config;
|
||||
} else if #[cfg(target_os = "macos")] {
|
||||
mod macos;
|
||||
pub(crate) use macos::hits;
|
||||
pub(crate) use macos::apply_config;
|
||||
} else if #[cfg(target_os = "windows")] {
|
||||
mod windows;
|
||||
pub(crate) use windows::hits;
|
||||
pub(crate) use windows::apply_config;
|
||||
}
|
||||
}
|
||||
|
||||
cfg_if! {
|
||||
if #[cfg(not(target_os = "windows"))] {
|
||||
use super::config::FileSearchConfig;
|
||||
use camino::Utf8Path;
|
||||
}
|
||||
}
|
||||
|
||||
/// If `file_path` should be removed from the search results given the filter
|
||||
/// conditions specified in `config`.
|
||||
#[cfg(not(target_os = "windows"))] // Not used on Windows
|
||||
pub(crate) fn should_be_filtered_out(
|
||||
config: &FileSearchConfig,
|
||||
file_path: &str,
|
||||
check_search_paths: bool,
|
||||
check_exclude_paths: bool,
|
||||
check_file_type: bool,
|
||||
) -> bool {
|
||||
let file_path = Utf8Path::new(file_path);
|
||||
|
||||
if check_search_paths {
|
||||
// search path
|
||||
let in_search_paths = config.search_paths.iter().any(|search_path| {
|
||||
let search_path = Utf8Path::new(search_path);
|
||||
file_path.starts_with(search_path)
|
||||
});
|
||||
if !in_search_paths {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if check_exclude_paths {
|
||||
// exclude path
|
||||
let is_excluded = config
|
||||
.exclude_paths
|
||||
.iter()
|
||||
.any(|exclude_path| file_path.starts_with(exclude_path));
|
||||
if is_excluded {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if check_file_type {
|
||||
// file type
|
||||
let matches_file_type = if config.file_types.is_empty() {
|
||||
true
|
||||
} else {
|
||||
let path_obj = camino::Utf8Path::new(&file_path);
|
||||
if let Some(extension) = path_obj.extension() {
|
||||
config
|
||||
.file_types
|
||||
.iter()
|
||||
.any(|file_type| file_type == extension)
|
||||
} else {
|
||||
// `config.file_types` is not empty, the hit files should have extensions.
|
||||
false
|
||||
}
|
||||
};
|
||||
|
||||
if !matches_file_type {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
// should_be_filtered_out() is not defined for Windows
|
||||
#[cfg(all(test, not(target_os = "windows")))]
|
||||
mod tests {
|
||||
use super::super::config::SearchBy;
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_should_be_filtered_out_with_no_check() {
|
||||
let config = FileSearchConfig {
|
||||
search_paths: vec!["/home/user/Documents".to_string()],
|
||||
exclude_paths: vec![],
|
||||
file_types: vec!["fffffff".into()],
|
||||
search_by: SearchBy::Name,
|
||||
};
|
||||
|
||||
assert!(!should_be_filtered_out(
|
||||
&config, "abbc", false, false, false
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_should_be_filtered_out_search_paths() {
|
||||
let config = FileSearchConfig {
|
||||
search_paths: vec![
|
||||
"/home/user/Documents".to_string(),
|
||||
"/home/user/Downloads".to_string(),
|
||||
],
|
||||
exclude_paths: vec![],
|
||||
file_types: vec![],
|
||||
search_by: SearchBy::Name,
|
||||
};
|
||||
|
||||
// Files in search paths should not be filtered
|
||||
assert!(!should_be_filtered_out(
|
||||
&config,
|
||||
"/home/user/Documents/file.txt",
|
||||
true,
|
||||
true,
|
||||
true
|
||||
));
|
||||
assert!(!should_be_filtered_out(
|
||||
&config,
|
||||
"/home/user/Downloads/image.jpg",
|
||||
true,
|
||||
true,
|
||||
true
|
||||
));
|
||||
assert!(!should_be_filtered_out(
|
||||
&config,
|
||||
"/home/user/Documents/folder/file.txt",
|
||||
true,
|
||||
true,
|
||||
true
|
||||
));
|
||||
|
||||
// Files not in search paths should be filtered
|
||||
assert!(should_be_filtered_out(
|
||||
&config,
|
||||
"/home/user/Pictures/photo.jpg",
|
||||
true,
|
||||
true,
|
||||
true
|
||||
));
|
||||
assert!(should_be_filtered_out(
|
||||
&config,
|
||||
"/tmp/tempfile",
|
||||
true,
|
||||
true,
|
||||
true
|
||||
));
|
||||
assert!(should_be_filtered_out(
|
||||
&config,
|
||||
"/usr/bin/ls",
|
||||
true,
|
||||
true,
|
||||
true
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_should_be_filtered_out_exclude_paths() {
|
||||
let config = FileSearchConfig {
|
||||
search_paths: vec!["/home/user".to_string()],
|
||||
exclude_paths: vec![
|
||||
"/home/user/Trash".to_string(),
|
||||
"/home/user/.cache".to_string(),
|
||||
],
|
||||
file_types: vec![],
|
||||
search_by: SearchBy::Name,
|
||||
};
|
||||
|
||||
// Files in search paths but not excluded should not be filtered
|
||||
assert!(!should_be_filtered_out(
|
||||
&config,
|
||||
"/home/user/Documents/file.txt",
|
||||
true,
|
||||
true,
|
||||
true
|
||||
));
|
||||
assert!(!should_be_filtered_out(
|
||||
&config,
|
||||
"/home/user/Downloads/image.jpg",
|
||||
true,
|
||||
true,
|
||||
true
|
||||
));
|
||||
|
||||
// Files in excluded paths should be filtered
|
||||
assert!(should_be_filtered_out(
|
||||
&config,
|
||||
"/home/user/Trash/deleted_file",
|
||||
true,
|
||||
true,
|
||||
true
|
||||
));
|
||||
assert!(should_be_filtered_out(
|
||||
&config,
|
||||
"/home/user/.cache/temp",
|
||||
true,
|
||||
true,
|
||||
true
|
||||
));
|
||||
assert!(should_be_filtered_out(
|
||||
&config,
|
||||
"/home/user/Trash/folder/file.txt",
|
||||
true,
|
||||
true,
|
||||
true
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_should_be_filtered_out_file_types() {
|
||||
let config = FileSearchConfig {
|
||||
search_paths: vec!["/home/user/Documents".to_string()],
|
||||
exclude_paths: vec![],
|
||||
file_types: vec!["txt".to_string(), "md".to_string()],
|
||||
search_by: SearchBy::Name,
|
||||
};
|
||||
|
||||
// Files with allowed extensions should not be filtered
|
||||
assert!(!should_be_filtered_out(
|
||||
&config,
|
||||
"/home/user/Documents/notes.txt",
|
||||
true,
|
||||
true,
|
||||
true
|
||||
));
|
||||
assert!(!should_be_filtered_out(
|
||||
&config,
|
||||
"/home/user/Documents/readme.md",
|
||||
true,
|
||||
true,
|
||||
true
|
||||
));
|
||||
|
||||
// Files with disallowed extensions should be filtered
|
||||
assert!(should_be_filtered_out(
|
||||
&config,
|
||||
"/home/user/Documents/image.jpg",
|
||||
true,
|
||||
true,
|
||||
true
|
||||
));
|
||||
assert!(should_be_filtered_out(
|
||||
&config,
|
||||
"/home/user/Documents/document.pdf",
|
||||
true,
|
||||
true,
|
||||
true
|
||||
));
|
||||
|
||||
// Files without extensions should be filtered when file_types is not empty
|
||||
assert!(should_be_filtered_out(
|
||||
&config,
|
||||
"/home/user/Documents/file",
|
||||
true,
|
||||
true,
|
||||
true
|
||||
));
|
||||
assert!(should_be_filtered_out(
|
||||
&config,
|
||||
"/home/user/Documents/folder",
|
||||
true,
|
||||
true,
|
||||
true
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_should_be_filtered_out_empty_file_types() {
|
||||
let config = FileSearchConfig {
|
||||
search_paths: vec!["/home/user/Documents".to_string()],
|
||||
exclude_paths: vec![],
|
||||
file_types: vec![],
|
||||
search_by: SearchBy::Name,
|
||||
};
|
||||
|
||||
// When file_types is empty, all file types should be allowed
|
||||
assert!(!should_be_filtered_out(
|
||||
&config,
|
||||
"/home/user/Documents/file.txt",
|
||||
true,
|
||||
true,
|
||||
true
|
||||
));
|
||||
assert!(!should_be_filtered_out(
|
||||
&config,
|
||||
"/home/user/Documents/image.jpg",
|
||||
true,
|
||||
true,
|
||||
true
|
||||
));
|
||||
assert!(!should_be_filtered_out(
|
||||
&config,
|
||||
"/home/user/Documents/document",
|
||||
true,
|
||||
true,
|
||||
true
|
||||
));
|
||||
assert!(!should_be_filtered_out(
|
||||
&config,
|
||||
"/home/user/Documents/folder/",
|
||||
true,
|
||||
true,
|
||||
true
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_should_be_filtered_out_combined_filters() {
|
||||
let config = FileSearchConfig {
|
||||
search_paths: vec!["/home/user".to_string()],
|
||||
exclude_paths: vec!["/home/user/Trash".to_string()],
|
||||
file_types: vec!["txt".to_string()],
|
||||
search_by: SearchBy::Name,
|
||||
};
|
||||
|
||||
// Should pass all filters: in search path, not excluded, and correct file type
|
||||
assert!(!should_be_filtered_out(
|
||||
&config,
|
||||
"/home/user/Documents/notes.txt",
|
||||
true,
|
||||
true,
|
||||
true
|
||||
));
|
||||
|
||||
// Fails file type filter
|
||||
assert!(should_be_filtered_out(
|
||||
&config,
|
||||
"/home/user/Documents/image.jpg",
|
||||
true,
|
||||
true,
|
||||
true
|
||||
));
|
||||
|
||||
// Fails exclude path filter
|
||||
assert!(should_be_filtered_out(
|
||||
&config,
|
||||
"/home/user/Trash/deleted.txt",
|
||||
true,
|
||||
true,
|
||||
true
|
||||
));
|
||||
|
||||
// Fails search path filter
|
||||
assert!(should_be_filtered_out(
|
||||
&config,
|
||||
"/tmp/temp.txt",
|
||||
true,
|
||||
true,
|
||||
true
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_should_be_filtered_out_edge_cases() {
|
||||
let config = FileSearchConfig {
|
||||
search_paths: vec!["/home/user".to_string()],
|
||||
exclude_paths: vec![],
|
||||
file_types: vec!["txt".to_string()],
|
||||
search_by: SearchBy::Name,
|
||||
};
|
||||
|
||||
// Empty path
|
||||
assert!(should_be_filtered_out(&config, "", true, true, true));
|
||||
|
||||
// Root path
|
||||
assert!(should_be_filtered_out(&config, "/", true, true, true));
|
||||
|
||||
// Path that starts with search path but continues differently
|
||||
assert!(!should_be_filtered_out(
|
||||
&config,
|
||||
"/home/user/document.txt",
|
||||
true,
|
||||
true,
|
||||
true
|
||||
));
|
||||
assert!(should_be_filtered_out(
|
||||
&config,
|
||||
"/home/user_other/file.txt",
|
||||
true,
|
||||
true,
|
||||
true
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,234 @@
|
||||
//! Wraps Windows `ISearchCrawlScopeManager`
|
||||
|
||||
mod searchapi_h_bindings;
|
||||
|
||||
use searchapi_h_bindings::CLSID_CSEARCH_MANAGER;
|
||||
use searchapi_h_bindings::IID_ISEARCH_MANAGER;
|
||||
use searchapi_h_bindings::{
|
||||
HRESULT, ISearchCatalogManager, ISearchCatalogManagerVtbl, ISearchCrawlScopeManager,
|
||||
ISearchCrawlScopeManagerVtbl, ISearchManager,
|
||||
};
|
||||
use std::ffi::OsStr;
|
||||
use std::ffi::OsString;
|
||||
use std::os::windows::ffi::OsStrExt;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
use std::ptr::null_mut;
|
||||
use windows::core::w;
|
||||
use windows_sys::Win32::Foundation::S_OK;
|
||||
use windows_sys::Win32::System::Com::{
|
||||
CLSCTX_LOCAL_SERVER, COINIT_APARTMENTTHREADED, CoCreateInstance, CoInitializeEx, CoUninitialize,
|
||||
};
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
#[error("{msg}, function [{function}], HRESULT [{hresult}]")]
|
||||
pub(crate) struct WindowSearchApiError {
|
||||
function: &'static str,
|
||||
hresult: HRESULT,
|
||||
msg: String,
|
||||
}
|
||||
|
||||
/// See doc of [`Rule`].
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub(crate) enum RuleMode {
|
||||
Inclusion,
|
||||
Exclusion,
|
||||
}
|
||||
|
||||
/// A rule adds or removes one or more paths to/from the Windows Search index.
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct Rule {
|
||||
/// A path or path pattern (wildcard supported, only for exclusion rule) that
|
||||
/// specifies the paths that this rule applies to.
|
||||
///
|
||||
/// The rules used by Windows Search actually specify URLs rather than paths,
|
||||
/// but we only care about paths, i.e., URLs with schema `file://`
|
||||
pub(crate) paths: PathBuf,
|
||||
/// Add or remove paths to/from the index.
|
||||
pub(crate) mode: RuleMode,
|
||||
}
|
||||
|
||||
/// A wrapper around Window's `ISearchCrawlScopeManager` type
|
||||
pub(crate) struct CrawlScopeManager {
|
||||
i_search_crawl_scope_manager: *mut ISearchCrawlScopeManager,
|
||||
}
|
||||
|
||||
impl CrawlScopeManager {
|
||||
fn vtable(&self) -> *mut ISearchCrawlScopeManagerVtbl {
|
||||
unsafe { (*self.i_search_crawl_scope_manager).lpVtbl }
|
||||
}
|
||||
|
||||
pub(crate) fn new() -> Result<Self, WindowSearchApiError> {
|
||||
unsafe {
|
||||
// 1. Initialize the COM library, use Apartment-threading as Self is not Send/Sync
|
||||
let hr = CoInitializeEx(null_mut(), COINIT_APARTMENTTHREADED as u32);
|
||||
if hr != S_OK {
|
||||
return Err(WindowSearchApiError {
|
||||
function: "CoInitializeEx()",
|
||||
hresult: hr,
|
||||
msg: "failed to initialize the COM library".into(),
|
||||
});
|
||||
}
|
||||
|
||||
// 2. Create an instance of the CSearchManager.
|
||||
let mut search_manager: *mut ISearchManager = null_mut();
|
||||
let hr = CoCreateInstance(
|
||||
&CLSID_CSEARCH_MANAGER, // CLSID of the object
|
||||
null_mut(), // No outer unknown
|
||||
CLSCTX_LOCAL_SERVER, // Server context
|
||||
&IID_ISEARCH_MANAGER, // IID of the interface we want
|
||||
&mut search_manager as *mut _ as *mut _, // Pointer to receive the interface
|
||||
);
|
||||
if hr != S_OK {
|
||||
return Err(WindowSearchApiError {
|
||||
function: "CoCreateInstance()",
|
||||
hresult: hr,
|
||||
msg: "failed to initialize ISearchManager".into(),
|
||||
});
|
||||
}
|
||||
assert!(!search_manager.is_null());
|
||||
|
||||
let search_manger_vtable = (*search_manager).lpVtbl;
|
||||
let search_manager_fn_get_catalog = (*search_manger_vtable).GetCatalog.unwrap();
|
||||
let mut search_catalog_manager: *mut ISearchCatalogManager = null_mut();
|
||||
let string_literal_system_index = w!("SystemIndex");
|
||||
let hr: HRESULT = search_manager_fn_get_catalog(
|
||||
search_manager,
|
||||
string_literal_system_index.0,
|
||||
&mut search_catalog_manager as *mut *mut ISearchCatalogManager,
|
||||
);
|
||||
if hr != S_OK {
|
||||
return Err(WindowSearchApiError {
|
||||
function: "ISearchManager::GetCatalog()",
|
||||
hresult: hr,
|
||||
msg: "failed to initialize ISearchCatalogManager".into(),
|
||||
});
|
||||
}
|
||||
assert!(!search_catalog_manager.is_null());
|
||||
|
||||
let search_catalog_manager_vtable: *mut ISearchCatalogManagerVtbl =
|
||||
(*search_catalog_manager).lpVtbl;
|
||||
let fn_get_crawl_scope_manager = (*search_catalog_manager_vtable)
|
||||
.GetCrawlScopeManager
|
||||
.unwrap();
|
||||
let mut search_crawl_scope_manager: *mut ISearchCrawlScopeManager = null_mut();
|
||||
let hr =
|
||||
fn_get_crawl_scope_manager(search_catalog_manager, &mut search_crawl_scope_manager);
|
||||
if hr != S_OK {
|
||||
return Err(WindowSearchApiError {
|
||||
function: "ISearchCatalogManager::GetCrawlScopeManager()",
|
||||
hresult: hr,
|
||||
msg: "failed to initialize ISearchCrawlScopeManager".into(),
|
||||
});
|
||||
}
|
||||
assert!(!search_crawl_scope_manager.is_null());
|
||||
|
||||
Ok(Self {
|
||||
i_search_crawl_scope_manager: search_crawl_scope_manager,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Does nothing unless you [`commit()`] the changes.
|
||||
pub(crate) fn add_rule(&mut self, rule: Rule) -> Result<(), WindowSearchApiError> {
|
||||
unsafe {
|
||||
let vtable = self.vtable();
|
||||
|
||||
let fn_add_rule = (*vtable).AddUserScopeRule.unwrap();
|
||||
|
||||
let url: Vec<u16> = encode_path(&rule.paths);
|
||||
let inclusion = (rule.mode == RuleMode::Inclusion) as i32;
|
||||
let override_child_rules = true as i32;
|
||||
let follow_flag = 0x1_u32; /* FF_INDEXCOMPLEXURLS */
|
||||
|
||||
let hr = fn_add_rule(
|
||||
self.i_search_crawl_scope_manager,
|
||||
url.as_ptr(),
|
||||
inclusion,
|
||||
override_child_rules,
|
||||
follow_flag,
|
||||
);
|
||||
|
||||
if hr != S_OK {
|
||||
return Err(WindowSearchApiError {
|
||||
function: "ISearchCrawlScopeManager::AddUserScopeRule()",
|
||||
hresult: hr,
|
||||
msg: "failed to add scope rule".into(),
|
||||
});
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn is_path_included<P: AsRef<Path> + ?Sized>(
|
||||
&self,
|
||||
path: &P,
|
||||
) -> Result<bool, WindowSearchApiError> {
|
||||
unsafe {
|
||||
let vtable = self.vtable();
|
||||
let fn_included_in_crawl_scope = (*vtable).IncludedInCrawlScope.unwrap();
|
||||
let path: Vec<u16> = encode_path(path);
|
||||
|
||||
let mut included: i32 = 0 /* false */;
|
||||
|
||||
let hr = fn_included_in_crawl_scope(
|
||||
self.i_search_crawl_scope_manager,
|
||||
path.as_ptr(),
|
||||
&mut included,
|
||||
);
|
||||
if hr != S_OK {
|
||||
return Err(WindowSearchApiError {
|
||||
function: "ISearchCrawlScopeManager::IncludedInCrawlScope()",
|
||||
hresult: hr,
|
||||
msg: "failed to call IncludedInCrawlScope()".into(),
|
||||
});
|
||||
}
|
||||
|
||||
Ok(included == 1)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn commit(&self) -> Result<(), WindowSearchApiError> {
|
||||
unsafe {
|
||||
let vtable = self.vtable();
|
||||
let fn_commit = (*vtable).SaveAll.unwrap();
|
||||
let hr = fn_commit(self.i_search_crawl_scope_manager);
|
||||
if hr != S_OK {
|
||||
return Err(WindowSearchApiError {
|
||||
function: "ISearchCrawlScopeManager::SaveAll()",
|
||||
hresult: hr,
|
||||
msg: "failed to commit the changes".into(),
|
||||
});
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for CrawlScopeManager {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
CoUninitialize();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn encode_path<P: AsRef<Path> + ?Sized>(path: &P) -> Vec<u16> {
|
||||
let mut buffer = OsString::new();
|
||||
|
||||
// schema
|
||||
buffer.push("file:///");
|
||||
buffer.push(path.as_ref().as_os_str());
|
||||
|
||||
osstr_to_wstr(&buffer)
|
||||
}
|
||||
|
||||
fn osstr_to_wstr<S: AsRef<OsStr> + ?Sized>(str: &S) -> Vec<u16> {
|
||||
let os_str: &OsStr = str.as_ref();
|
||||
let mut chars = os_str.encode_wide().collect::<Vec<u16>>();
|
||||
chars.push(0 /* NUL */);
|
||||
|
||||
chars
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
//! Rust binding of the types and functions declared in 'searchapi.h'
|
||||
|
||||
#![allow(unused)]
|
||||
#![allow(non_camel_case_types)]
|
||||
#![allow(non_snake_case)]
|
||||
#![allow(non_upper_case_globals)]
|
||||
#![allow(unsafe_op_in_unsafe_fn)]
|
||||
#![allow(unnecessary_transmutes)]
|
||||
|
||||
include!(concat!(env!("OUT_DIR"), "/searchapi_bindings.rs"));
|
||||
|
||||
// The bindings.rs contains a GUID type as well, we use the one provided by
|
||||
// the windows_sys crate here.
|
||||
use windows_sys::core::GUID as WIN_SYS_GUID;
|
||||
|
||||
// https://github.com/search?q=CLSID_CSearchManager+language%3AC&type=code&l=C
|
||||
pub(crate) static CLSID_CSEARCH_MANAGER: WIN_SYS_GUID = WIN_SYS_GUID {
|
||||
data1: 0x7d096c5f,
|
||||
data2: 0xac08,
|
||||
data3: 0x4f1f,
|
||||
data4: [0xbe, 0xb7, 0x5c, 0x22, 0xc5, 0x17, 0xce, 0x39],
|
||||
};
|
||||
|
||||
// https://github.com/search?q=IID_ISearchManager+language%3AC&type=code
|
||||
pub(crate) static IID_ISEARCH_MANAGER: WIN_SYS_GUID = WIN_SYS_GUID {
|
||||
data1: 0xAB310581,
|
||||
data2: 0xac80,
|
||||
data3: 0x11d1,
|
||||
data4: [0x8d, 0xf3, 0x00, 0xc0, 0x4f, 0xb6, 0xef, 0x69],
|
||||
};
|
||||
@@ -3,13 +3,17 @@
|
||||
//! https://github.com/IRONAGE-Park/rag-sample/blob/3f0ad8c8012026cd3a7e453d08f041609426cb91/src/native/windows.rs
|
||||
//! is the starting point of this implementation.
|
||||
|
||||
mod crawl_scope_manager;
|
||||
|
||||
use super::super::EXTENSION_ID;
|
||||
use super::super::config::FileSearchConfig;
|
||||
use super::super::config::SearchBy;
|
||||
use crate::common::document::{DataSourceReference, Document};
|
||||
use crate::extension::LOCAL_QUERY_SOURCE_TYPE;
|
||||
use crate::extension::OnOpened;
|
||||
use crate::util::file::get_file_icon;
|
||||
use crate::util::file::sync_get_file_icon;
|
||||
use std::borrow::Borrow;
|
||||
use std::path::PathBuf;
|
||||
use windows::{
|
||||
Win32::System::{
|
||||
Com::{CLSCTX_INPROC_SERVER, CoCreateInstance},
|
||||
@@ -420,7 +424,7 @@ pub(crate) async fn hits(
|
||||
// "file:C:/Users/desktop.ini" => "C:/Users/desktop.ini"
|
||||
let file_path = &item_url[ITEM_URL_PREFIX_LEN..];
|
||||
|
||||
let icon = get_file_icon(file_path.to_string()).await;
|
||||
let icon = sync_get_file_icon(file_path);
|
||||
let file_path_of_type_path = camino::Utf8Path::new(&file_path);
|
||||
let r#where = file_path_of_type_path
|
||||
.parent()
|
||||
@@ -468,6 +472,85 @@ pub(crate) async fn hits(
|
||||
Ok(hits)
|
||||
}
|
||||
|
||||
pub(crate) fn apply_config(config: &FileSearchConfig) -> Result<(), String> {
|
||||
// To ensure Windows Search indexer index the paths we specified in the
|
||||
// config, we will:
|
||||
//
|
||||
// 1. Add an inclusion rule for every search path to ensure indexer index
|
||||
// them
|
||||
// 2. For the exclude paths, we exclude them from the crawl scope if they
|
||||
// were not included in the scope before we update the scope. Otherwise,
|
||||
// we cannot exclude them as doing that could potentially break other
|
||||
// apps (by removing the indexes they rely on).
|
||||
//
|
||||
// Windows APIs are pretty smart. They won't blindly add an inclusion rule if
|
||||
// the path you are trying to include is already included. The same applies
|
||||
// to exclusion rules as well. Since Windows APIs handle these checks for us,
|
||||
// we don't need to worry about them.
|
||||
|
||||
use crawl_scope_manager::CrawlScopeManager;
|
||||
use crawl_scope_manager::Rule;
|
||||
use crawl_scope_manager::RuleMode;
|
||||
use std::borrow::Cow;
|
||||
|
||||
/// Windows APIs need the path to contain a tailing '\'
|
||||
fn add_tailing_backslash(path: &str) -> Cow<'_, str> {
|
||||
if path.ends_with(r#"\"#) {
|
||||
Cow::Borrowed(path)
|
||||
} else {
|
||||
let mut owned = path.to_string();
|
||||
owned.push_str(r#"\"#);
|
||||
|
||||
Cow::Owned(owned)
|
||||
}
|
||||
}
|
||||
|
||||
let mut manager = CrawlScopeManager::new().map_err(|e| e.to_string())?;
|
||||
|
||||
let search_paths = &config.search_paths;
|
||||
let exclude_paths = &config.exclude_paths;
|
||||
|
||||
// indexes to `exclude_paths` of the paths we need to exclude
|
||||
let mut paths_to_exclude: Vec<usize> = Vec::new();
|
||||
for (idx, exclude_path) in exclude_paths.into_iter().enumerate() {
|
||||
let exclude_path = add_tailing_backslash(&exclude_path);
|
||||
let exclude_path: &str = exclude_path.borrow();
|
||||
|
||||
if !manager
|
||||
.is_path_included(exclude_path)
|
||||
.map_err(|e| e.to_string())?
|
||||
{
|
||||
paths_to_exclude.push(idx);
|
||||
}
|
||||
}
|
||||
|
||||
for search_path in search_paths {
|
||||
let inclusion_rule = Rule {
|
||||
paths: PathBuf::from(add_tailing_backslash(&search_path).into_owned()),
|
||||
mode: RuleMode::Inclusion,
|
||||
};
|
||||
|
||||
manager
|
||||
.add_rule(inclusion_rule)
|
||||
.map_err(|e| e.to_string())?;
|
||||
}
|
||||
|
||||
for idx in paths_to_exclude {
|
||||
let exclusion_rule = Rule {
|
||||
paths: PathBuf::from(add_tailing_backslash(&exclude_paths[idx]).into_owned()),
|
||||
mode: RuleMode::Exclusion,
|
||||
};
|
||||
|
||||
manager
|
||||
.add_rule(exclusion_rule)
|
||||
.map_err(|e| e.to_string())?;
|
||||
}
|
||||
|
||||
manager.commit().map_err(|e| e.to_string())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Skip these tests in our CI, they fail with the following error
|
||||
// "SQL is invalid: "0x80041820""
|
||||
//
|
||||
@@ -19,7 +19,7 @@ pub(crate) const PLUGIN_JSON_FILE: &str = r#"
|
||||
{
|
||||
"id": "File Search",
|
||||
"name": "File Search",
|
||||
"platforms": ["macos", "windows"],
|
||||
"platforms": ["macos", "windows", "linux"],
|
||||
"description": "Search files on your system",
|
||||
"icon": "font_Filesearch",
|
||||
"type": "extension"
|
||||
@@ -85,7 +85,7 @@ impl SearchSource for FileSearchExtensionSearchSource {
|
||||
|
||||
let hits = implementation::hits(&query_string, from, size, &config)
|
||||
.await
|
||||
.map_err(SearchError::InternalError)?;
|
||||
.map_err(|e| SearchError::InternalError { error: e })?;
|
||||
|
||||
let total_hits = hits.len();
|
||||
Ok(QueryResponse {
|
||||
|
||||
@@ -3,24 +3,26 @@
|
||||
pub mod ai_overview;
|
||||
pub mod application;
|
||||
pub mod calculator;
|
||||
#[cfg(any(target_os = "macos", target_os = "windows"))]
|
||||
pub mod file_search;
|
||||
pub mod pizza_engine_runtime;
|
||||
pub mod quick_ai_access;
|
||||
#[cfg(target_os = "macos")]
|
||||
pub mod window_management;
|
||||
|
||||
use super::Extension;
|
||||
use crate::SearchSourceRegistry;
|
||||
use crate::common::error::{ReportErrorStyle, report_error};
|
||||
use crate::extension::built_in::application::{set_apps_hotkey, unset_apps_hotkey};
|
||||
use crate::extension::{
|
||||
ExtensionBundleIdBorrowed, PLUGIN_JSON_FILE_NAME, alter_extension_json_file,
|
||||
};
|
||||
use anyhow::Context;
|
||||
use file_search::config::FileSearchConfig;
|
||||
use file_search::implementation::apply_config as file_search_apply_config;
|
||||
use std::path::{Path, PathBuf};
|
||||
use tauri::{AppHandle, Manager, Runtime};
|
||||
use tauri::{AppHandle, Manager};
|
||||
|
||||
pub(crate) fn get_built_in_extension_directory<R: Runtime>(
|
||||
tauri_app_handle: &AppHandle<R>,
|
||||
) -> PathBuf {
|
||||
pub(crate) fn get_built_in_extension_directory(tauri_app_handle: &AppHandle) -> PathBuf {
|
||||
let mut resource_dir = tauri_app_handle.path().app_data_dir().expect(
|
||||
"User home directory not found, which should be impossible on desktop environments",
|
||||
);
|
||||
@@ -136,8 +138,8 @@ async fn load_built_in_extension(
|
||||
/// We only read alias/hotkey/enabled from the JSON file, we have ensured that if
|
||||
/// alias/hotkey is not supported, then it will be `None`. Besides that, no further
|
||||
/// validation is needed because nothing could go wrong.
|
||||
pub(crate) async fn list_built_in_extensions<R: Runtime>(
|
||||
tauri_app_handle: &AppHandle<R>,
|
||||
pub(crate) async fn list_built_in_extensions(
|
||||
tauri_app_handle: &AppHandle,
|
||||
) -> Result<Vec<Extension>, String> {
|
||||
let dir = get_built_in_extension_directory(tauri_app_handle);
|
||||
|
||||
@@ -175,24 +177,33 @@ pub(crate) async fn list_built_in_extensions<R: Runtime>(
|
||||
.await?,
|
||||
);
|
||||
|
||||
built_in_extensions.push(
|
||||
load_built_in_extension(
|
||||
&dir,
|
||||
file_search::EXTENSION_ID,
|
||||
file_search::PLUGIN_JSON_FILE,
|
||||
)
|
||||
.await?,
|
||||
);
|
||||
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(any(target_os = "macos", target_os = "windows"))] {
|
||||
built_in_extensions.push(
|
||||
load_built_in_extension(
|
||||
&dir,
|
||||
file_search::EXTENSION_ID,
|
||||
file_search::PLUGIN_JSON_FILE,
|
||||
)
|
||||
.await?,
|
||||
);
|
||||
}
|
||||
if #[cfg(target_os = "macos")] {
|
||||
built_in_extensions.push(
|
||||
load_built_in_extension(
|
||||
&dir,
|
||||
window_management::EXTENSION_ID,
|
||||
window_management::PLUGIN_JSON_FILE,
|
||||
)
|
||||
.await?,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(built_in_extensions)
|
||||
}
|
||||
|
||||
pub(super) async fn init_built_in_extension<R: Runtime>(
|
||||
tauri_app_handle: &AppHandle<R>,
|
||||
pub(super) async fn init_built_in_extension(
|
||||
tauri_app_handle: &AppHandle,
|
||||
extension: &Extension,
|
||||
search_source_registry: &SearchSourceRegistry,
|
||||
) -> Result<(), String> {
|
||||
@@ -214,16 +225,28 @@ pub(super) async fn init_built_in_extension<R: Runtime>(
|
||||
log::debug!("built-in extension [{}] initialized", extension.id);
|
||||
}
|
||||
|
||||
if extension.id == file_search::EXTENSION_ID {
|
||||
let file_system_search = file_search::FileSearchExtensionSearchSource;
|
||||
search_source_registry
|
||||
.register_source(file_system_search)
|
||||
.await;
|
||||
let file_search_config = FileSearchConfig::get(tauri_app_handle);
|
||||
file_search_apply_config(&file_search_config)?;
|
||||
log::debug!("built-in extension [{}] initialized", extension.id);
|
||||
}
|
||||
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(any(target_os = "macos", target_os = "windows"))] {
|
||||
if extension.id == file_search::EXTENSION_ID {
|
||||
let file_system_search = file_search::FileSearchExtensionSearchSource;
|
||||
search_source_registry
|
||||
.register_source(file_system_search)
|
||||
.await;
|
||||
log::debug!("built-in extension [{}] initialized", extension.id);
|
||||
}
|
||||
}
|
||||
if #[cfg(target_os = "macos")] {
|
||||
if extension.id == window_management::EXTENSION_ID {
|
||||
let file_system_search = window_management::search_source::WindowManagementSearchSource;
|
||||
search_source_registry
|
||||
.register_source(file_system_search)
|
||||
.await;
|
||||
|
||||
window_management::set_up_commands_hotkeys(tauri_app_handle, extension)?;
|
||||
log::debug!("built-in extension [{}] initialized", extension.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -233,8 +256,8 @@ pub(crate) fn is_extension_built_in(bundle_id: &ExtensionBundleIdBorrowed<'_>) -
|
||||
bundle_id.developer.is_none()
|
||||
}
|
||||
|
||||
pub(crate) async fn enable_built_in_extension<R: Runtime>(
|
||||
tauri_app_handle: &AppHandle<R>,
|
||||
pub(crate) async fn enable_built_in_extension(
|
||||
tauri_app_handle: &AppHandle,
|
||||
bundle_id: &ExtensionBundleIdBorrowed<'_>,
|
||||
) -> Result<(), String> {
|
||||
let search_source_registry_tauri_state = tauri_app_handle.state::<SearchSourceRegistry>();
|
||||
@@ -301,19 +324,51 @@ pub(crate) async fn enable_built_in_extension<R: Runtime>(
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if bundle_id.extension_id == file_search::EXTENSION_ID {
|
||||
let file_system_search = file_search::FileSearchExtensionSearchSource;
|
||||
search_source_registry_tauri_state
|
||||
.register_source(file_system_search)
|
||||
.await;
|
||||
alter_extension_json_file(
|
||||
&get_built_in_extension_directory(tauri_app_handle),
|
||||
bundle_id,
|
||||
update_extension,
|
||||
)?;
|
||||
let file_search_config = FileSearchConfig::get(tauri_app_handle);
|
||||
file_search_apply_config(&file_search_config)?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(any(target_os = "macos", target_os = "windows"))] {
|
||||
if bundle_id.extension_id == file_search::EXTENSION_ID {
|
||||
let file_system_search = file_search::FileSearchExtensionSearchSource;
|
||||
search_source_registry_tauri_state
|
||||
.register_source(file_system_search)
|
||||
.await;
|
||||
alter_extension_json_file(
|
||||
&get_built_in_extension_directory(tauri_app_handle),
|
||||
bundle_id,
|
||||
update_extension,
|
||||
)?;
|
||||
return Ok(());
|
||||
if #[cfg(target_os = "macos")] {
|
||||
if bundle_id.extension_id == window_management::EXTENSION_ID
|
||||
&& bundle_id.sub_extension_id.is_none()
|
||||
{
|
||||
let built_in_extension_dir = get_built_in_extension_directory(tauri_app_handle);
|
||||
|
||||
let file_system_search = window_management::search_source::WindowManagementSearchSource;
|
||||
search_source_registry_tauri_state
|
||||
.register_source(file_system_search)
|
||||
.await;
|
||||
|
||||
let extension =
|
||||
load_extension_from_json_file(&built_in_extension_dir, bundle_id.extension_id)?;
|
||||
window_management::set_up_commands_hotkeys(tauri_app_handle, &extension)?;
|
||||
|
||||
alter_extension_json_file(&built_in_extension_dir, bundle_id, update_extension)?;
|
||||
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if bundle_id.extension_id == window_management::EXTENSION_ID {
|
||||
if let Some(command_id) = bundle_id.sub_extension_id {
|
||||
let built_in_extension_dir = get_built_in_extension_directory(tauri_app_handle);
|
||||
alter_extension_json_file(&built_in_extension_dir, bundle_id, update_extension)?;
|
||||
|
||||
let extension =
|
||||
load_extension_from_json_file(&built_in_extension_dir, bundle_id.extension_id)?;
|
||||
window_management::set_up_command_hotkey(tauri_app_handle, &extension, command_id)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -321,8 +376,8 @@ pub(crate) async fn enable_built_in_extension<R: Runtime>(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) async fn disable_built_in_extension<R: Runtime>(
|
||||
tauri_app_handle: &AppHandle<R>,
|
||||
pub(crate) async fn disable_built_in_extension(
|
||||
tauri_app_handle: &AppHandle,
|
||||
bundle_id: &ExtensionBundleIdBorrowed<'_>,
|
||||
) -> Result<(), String> {
|
||||
let search_source_registry_tauri_state = tauri_app_handle.state::<SearchSourceRegistry>();
|
||||
@@ -389,18 +444,44 @@ pub(crate) async fn disable_built_in_extension<R: Runtime>(
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if bundle_id.extension_id == file_search::EXTENSION_ID {
|
||||
search_source_registry_tauri_state
|
||||
.remove_source(bundle_id.extension_id)
|
||||
.await;
|
||||
alter_extension_json_file(
|
||||
&get_built_in_extension_directory(tauri_app_handle),
|
||||
bundle_id,
|
||||
update_extension,
|
||||
)?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(any(target_os = "macos", target_os = "windows"))] {
|
||||
if bundle_id.extension_id == file_search::EXTENSION_ID {
|
||||
if #[cfg(target_os = "macos")] {
|
||||
if bundle_id.extension_id == window_management::EXTENSION_ID
|
||||
&& bundle_id.sub_extension_id.is_none()
|
||||
{
|
||||
let built_in_extension_dir = get_built_in_extension_directory(tauri_app_handle);
|
||||
|
||||
search_source_registry_tauri_state
|
||||
.remove_source(bundle_id.extension_id)
|
||||
.await;
|
||||
alter_extension_json_file(
|
||||
&get_built_in_extension_directory(tauri_app_handle),
|
||||
bundle_id,
|
||||
update_extension,
|
||||
)?;
|
||||
return Ok(());
|
||||
alter_extension_json_file(&built_in_extension_dir, bundle_id, update_extension)?;
|
||||
|
||||
let extension =
|
||||
load_extension_from_json_file(&built_in_extension_dir, bundle_id.extension_id)?;
|
||||
window_management::unset_commands_hotkeys(tauri_app_handle, &extension)?;
|
||||
}
|
||||
|
||||
if bundle_id.extension_id == window_management::EXTENSION_ID {
|
||||
if let Some(command_id) = bundle_id.sub_extension_id {
|
||||
let built_in_extension_dir = get_built_in_extension_directory(tauri_app_handle);
|
||||
alter_extension_json_file(&built_in_extension_dir, bundle_id, update_extension)?;
|
||||
|
||||
let extension =
|
||||
load_extension_from_json_file(&built_in_extension_dir, bundle_id.extension_id)?;
|
||||
window_management::unset_command_hotkey(tauri_app_handle, &extension, command_id)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -408,20 +489,40 @@ pub(crate) async fn disable_built_in_extension<R: Runtime>(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn set_built_in_extension_alias<R: Runtime>(
|
||||
tauri_app_handle: &AppHandle<R>,
|
||||
pub(crate) fn set_built_in_extension_alias(
|
||||
tauri_app_handle: &AppHandle,
|
||||
bundle_id: &ExtensionBundleIdBorrowed<'_>,
|
||||
alias: &str,
|
||||
) {
|
||||
) -> Result<(), String> {
|
||||
if bundle_id.extension_id == application::QUERYSOURCE_ID_DATASOURCE_ID_DATASOURCE_NAME {
|
||||
if let Some(app_path) = bundle_id.sub_extension_id {
|
||||
application::set_app_alias(tauri_app_handle, app_path, alias);
|
||||
}
|
||||
}
|
||||
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(target_os = "macos")] {
|
||||
if bundle_id.extension_id == window_management::EXTENSION_ID
|
||||
&& bundle_id.sub_extension_id.is_some()
|
||||
{
|
||||
let update_function = |ext: &mut Extension| {
|
||||
ext.alias = Some(alias.to_string());
|
||||
Ok(())
|
||||
};
|
||||
alter_extension_json_file(
|
||||
&get_built_in_extension_directory(tauri_app_handle),
|
||||
bundle_id,
|
||||
update_function,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn register_built_in_extension_hotkey<R: Runtime>(
|
||||
tauri_app_handle: &AppHandle<R>,
|
||||
pub(crate) fn register_built_in_extension_hotkey(
|
||||
tauri_app_handle: &AppHandle,
|
||||
bundle_id: &ExtensionBundleIdBorrowed<'_>,
|
||||
hotkey: &str,
|
||||
) -> Result<(), String> {
|
||||
@@ -430,11 +531,34 @@ pub(crate) fn register_built_in_extension_hotkey<R: Runtime>(
|
||||
application::register_app_hotkey(&tauri_app_handle, app_path, hotkey)?;
|
||||
}
|
||||
}
|
||||
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(target_os = "macos")] {
|
||||
let update_function = |ext: &mut Extension| {
|
||||
ext.hotkey = Some(hotkey.into());
|
||||
|
||||
Ok(())
|
||||
};
|
||||
|
||||
if bundle_id.extension_id == window_management::EXTENSION_ID {
|
||||
if let Some(command_id) = bundle_id.sub_extension_id {
|
||||
alter_extension_json_file(
|
||||
&get_built_in_extension_directory(tauri_app_handle),
|
||||
bundle_id,
|
||||
update_function,
|
||||
)?;
|
||||
|
||||
window_management::register_command_hotkey(tauri_app_handle, command_id, hotkey)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn unregister_built_in_extension_hotkey<R: Runtime>(
|
||||
tauri_app_handle: &AppHandle<R>,
|
||||
pub(crate) fn unregister_built_in_extension_hotkey(
|
||||
tauri_app_handle: &AppHandle,
|
||||
bundle_id: &ExtensionBundleIdBorrowed<'_>,
|
||||
) -> Result<(), String> {
|
||||
if bundle_id.extension_id == application::QUERYSOURCE_ID_DATASOURCE_ID_DATASOURCE_NAME {
|
||||
@@ -442,6 +566,35 @@ pub(crate) fn unregister_built_in_extension_hotkey<R: Runtime>(
|
||||
application::unregister_app_hotkey(&tauri_app_handle, app_path)?;
|
||||
}
|
||||
}
|
||||
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(target_os = "macos")] {
|
||||
let update_function = |ext: &mut Extension| {
|
||||
ext.hotkey = None;
|
||||
|
||||
Ok(())
|
||||
};
|
||||
|
||||
if bundle_id.extension_id == window_management::EXTENSION_ID {
|
||||
if let Some(command_id) = bundle_id.sub_extension_id {
|
||||
|
||||
let extension = load_extension_from_json_file(
|
||||
&get_built_in_extension_directory(tauri_app_handle),
|
||||
bundle_id.extension_id,
|
||||
)
|
||||
.unwrap();
|
||||
window_management::unregister_command_hotkey(tauri_app_handle, &extension, command_id)?;
|
||||
alter_extension_json_file(
|
||||
&get_built_in_extension_directory(tauri_app_handle),
|
||||
bundle_id,
|
||||
update_function,
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -476,13 +629,17 @@ fn load_extension_from_json_file(
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
super::canonicalize_relative_icon_path(extension_directory, &mut extension)?;
|
||||
// TODO: refactor error handling
|
||||
super::canonicalize_relative_icon_path(extension_directory, &mut extension)
|
||||
.map_err(|e| report_error(&e, ReportErrorStyle::SingleLine))?;
|
||||
|
||||
Ok(extension)
|
||||
}
|
||||
|
||||
pub(crate) async fn is_built_in_extension_enabled<R: Runtime>(
|
||||
tauri_app_handle: &AppHandle<R>,
|
||||
#[allow(unused_macros)] // #[function_name::named] only used on macOS
|
||||
#[function_name::named]
|
||||
pub(crate) async fn is_built_in_extension_enabled(
|
||||
tauri_app_handle: &AppHandle,
|
||||
bundle_id: &ExtensionBundleIdBorrowed<'_>,
|
||||
) -> Result<bool, String> {
|
||||
let search_source_registry_tauri_state = tauri_app_handle.state::<SearchSourceRegistry>();
|
||||
@@ -526,9 +683,17 @@ pub(crate) async fn is_built_in_extension_enabled<R: Runtime>(
|
||||
return Ok(extension.enabled);
|
||||
}
|
||||
|
||||
if bundle_id.extension_id == file_search::EXTENSION_ID && bundle_id.sub_extension_id.is_none() {
|
||||
return Ok(search_source_registry_tauri_state
|
||||
.get_source(bundle_id.extension_id)
|
||||
.await
|
||||
.is_some());
|
||||
}
|
||||
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(any(target_os = "macos", target_os = "windows"))] {
|
||||
if bundle_id.extension_id == file_search::EXTENSION_ID
|
||||
if #[cfg(target_os = "macos")] {
|
||||
// Window Management
|
||||
if bundle_id.extension_id == window_management::EXTENSION_ID
|
||||
&& bundle_id.sub_extension_id.is_none()
|
||||
{
|
||||
return Ok(search_source_registry_tauri_state
|
||||
@@ -536,6 +701,25 @@ pub(crate) async fn is_built_in_extension_enabled<R: Runtime>(
|
||||
.await
|
||||
.is_some());
|
||||
}
|
||||
|
||||
// Window Management commands
|
||||
if bundle_id.extension_id == window_management::EXTENSION_ID
|
||||
&& let Some(command_id) = bundle_id.sub_extension_id
|
||||
{
|
||||
let extension = load_extension_from_json_file(
|
||||
&get_built_in_extension_directory(tauri_app_handle),
|
||||
bundle_id.extension_id,
|
||||
)?;
|
||||
let commands = extension
|
||||
.commands
|
||||
.expect("window management extension has commands");
|
||||
|
||||
let extension = commands.iter().find( |cmd| cmd.id == command_id).unwrap_or_else(|| {
|
||||
panic!("function [{}()] invoked with a Window Management command that does not exist, extension ID [{}] ", function_name!(), command_id)
|
||||
});
|
||||
|
||||
return Ok(extension.enabled);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
134
src-tauri/src/extension/built_in/window_management/actions.rs
Normal file
@@ -0,0 +1,134 @@
|
||||
#[derive(Debug, Clone, PartialEq, Copy, Hash, serde::Serialize, serde::Deserialize)]
|
||||
pub enum Action {
|
||||
/// Move the window to fill left half of the screen.
|
||||
TopHalf,
|
||||
/// Move the window to fill bottom half of the screen.
|
||||
BottomHalf,
|
||||
/// Move the window to fill left half of the screen.
|
||||
LeftHalf,
|
||||
/// Move the window to fill right half of the screen.
|
||||
RightHalf,
|
||||
/// Move the window to fill center half of the screen.
|
||||
CenterHalf,
|
||||
|
||||
/// Resize window to the top left quarter of the screen.
|
||||
TopLeftQuarter,
|
||||
/// Resize window to the top right quarter of the screen.
|
||||
TopRightQuarter,
|
||||
/// Resize window to the bottom left quarter of the screen.
|
||||
BottomLeftQuarter,
|
||||
/// Resize window to the bottom right quarter of the screen.
|
||||
BottomRightQuarter,
|
||||
|
||||
/// Resize window to the top left sixth of the screen.
|
||||
TopLeftSixth,
|
||||
/// Resize window to the top center sixth of the screen.
|
||||
TopCenterSixth,
|
||||
/// Resize window to the top right sixth of the screen.
|
||||
TopRightSixth,
|
||||
/// Resize window to the bottom left sixth of the screen.
|
||||
BottomLeftSixth,
|
||||
/// Resize window to the bottom center sixth of the screen.
|
||||
BottomCenterSixth,
|
||||
/// Resize window to the bottom right sixth of the screen.
|
||||
BottomRightSixth,
|
||||
|
||||
/// Resize window to the top third of the screen.
|
||||
TopThird,
|
||||
/// Resize window to the middle third of the screen.
|
||||
MiddleThird,
|
||||
/// Resize window to the bottom third of the screen.
|
||||
BottomThird,
|
||||
|
||||
/// Center window in the screen.
|
||||
Center,
|
||||
|
||||
/// Resize window to the first fourth of the screen.
|
||||
FirstFourth,
|
||||
/// Resize window to the second fourth of the screen.
|
||||
SecondFourth,
|
||||
/// Resize window to the third fourth of the screen.
|
||||
ThirdFourth,
|
||||
/// Resize window to the last fourth of the screen.
|
||||
LastFourth,
|
||||
|
||||
/// Resize window to the first third of the screen.
|
||||
FirstThird,
|
||||
/// Resize window to the center third of the screen.
|
||||
CenterThird,
|
||||
/// Resize window to the last third of the screen.
|
||||
LastThird,
|
||||
|
||||
/// Resize window to the first two thirds of the screen.
|
||||
FirstTwoThirds,
|
||||
/// Resize window to the center two thirds of the screen.
|
||||
CenterTwoThirds,
|
||||
/// Resize window to the last two thirds of the screen.
|
||||
LastTwoThirds,
|
||||
|
||||
/// Resize window to the first three fourths of the screen.
|
||||
FirstThreeFourths,
|
||||
/// Resize window to the center three fourths of the screen.
|
||||
CenterThreeFourths,
|
||||
/// Resize window to the last three fourths of the screen.
|
||||
LastThreeFourths,
|
||||
|
||||
/// Resize window to the top three fourths of the screen.
|
||||
TopThreeFourths,
|
||||
/// Resize window to the bottom three fourths of the screen.
|
||||
BottomThreeFourths,
|
||||
|
||||
/// Resize window to the top two thirds of the screen.
|
||||
TopTwoThirds,
|
||||
/// Resize window to the bottom two thirds of the screen.
|
||||
BottomTwoThirds,
|
||||
/// Resize window to the top center two thirds of the screen.
|
||||
TopCenterTwoThirds,
|
||||
|
||||
/// Resize window to the top first fourth of the screen.
|
||||
TopFirstFourth,
|
||||
/// Resize window to the top second fourth of the screen.
|
||||
TopSecondFourth,
|
||||
/// Resize window to the top third fourth of the screen.
|
||||
TopThirdFourth,
|
||||
/// Resize window to the top last fourth of the screen.
|
||||
TopLastFourth,
|
||||
|
||||
/// Increase the window until it reaches the screen size.
|
||||
MakeLarger,
|
||||
/// Decrease the window until it reaches its minimal size.
|
||||
MakeSmaller,
|
||||
|
||||
/// Maximize window to almost fit the screen.
|
||||
AlmostMaximize,
|
||||
/// Maximize window to fit the screen.
|
||||
Maximize,
|
||||
/// Maximize width of window to fit the screen.
|
||||
MaximizeWidth,
|
||||
/// Maximize height of window to fit the screen.
|
||||
MaximizeHeight,
|
||||
|
||||
/// Move window to the top edge of the screen.
|
||||
MoveUp,
|
||||
/// Move window to the bottom of the screen.
|
||||
MoveDown,
|
||||
/// Move window to the left edge of the screen.
|
||||
MoveLeft,
|
||||
/// Move window to the right edge of the screen.
|
||||
MoveRight,
|
||||
|
||||
/// Move window to the next desktop.
|
||||
NextDesktop,
|
||||
/// Move window to the previous desktop.
|
||||
PreviousDesktop,
|
||||
/// Move window to the next display.
|
||||
NextDisplay,
|
||||
/// Move window to the previous display.
|
||||
PreviousDisplay,
|
||||
|
||||
/// Restore window to its last position.
|
||||
Restore,
|
||||
|
||||
/// Toggle fullscreen mode.
|
||||
ToggleFullscreen,
|
||||
}
|
||||
@@ -0,0 +1,796 @@
|
||||
//! This module calls macOS APIs to implement various helper functions needed by
|
||||
//! to perform the defined actions.
|
||||
|
||||
mod private;
|
||||
|
||||
use std::ffi::c_uint;
|
||||
use std::ffi::c_ushort;
|
||||
use std::ffi::c_void;
|
||||
use std::ops::Deref;
|
||||
use std::ptr::NonNull;
|
||||
use std::time::Duration;
|
||||
|
||||
use objc2::MainThreadMarker;
|
||||
use objc2_app_kit::NSEvent;
|
||||
use objc2_app_kit::NSScreen;
|
||||
use objc2_app_kit::NSWorkspace;
|
||||
use objc2_application_services::AXError;
|
||||
use objc2_application_services::AXUIElement;
|
||||
use objc2_application_services::AXValue;
|
||||
use objc2_application_services::AXValueType;
|
||||
use objc2_core_foundation::CFBoolean;
|
||||
use objc2_core_foundation::CFRetained;
|
||||
use objc2_core_foundation::CFString;
|
||||
use objc2_core_foundation::CFType;
|
||||
use objc2_core_foundation::CGPoint;
|
||||
use objc2_core_foundation::CGRect;
|
||||
use objc2_core_foundation::CGSize;
|
||||
use objc2_core_foundation::Type;
|
||||
use objc2_core_foundation::{CFArray, CFDictionary, CFNumber};
|
||||
use objc2_core_graphics::CGError;
|
||||
use objc2_core_graphics::CGEvent;
|
||||
use objc2_core_graphics::CGEventFlags;
|
||||
use objc2_core_graphics::CGEventTapLocation;
|
||||
use objc2_core_graphics::CGEventType;
|
||||
use objc2_core_graphics::CGMouseButton;
|
||||
use objc2_core_graphics::CGRectGetMidX;
|
||||
use objc2_core_graphics::CGRectGetMinY;
|
||||
use objc2_core_graphics::CGRectIntersectsRect;
|
||||
use objc2_core_graphics::CGWindowID;
|
||||
|
||||
use super::error::Error;
|
||||
|
||||
use private::CGSCopyManagedDisplaySpaces;
|
||||
use private::CGSGetActiveSpace;
|
||||
use private::CGSMainConnectionID;
|
||||
use private::CGSSpaceID;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{LazyLock, Mutex};
|
||||
|
||||
fn intersects(r1: CGRect, r2: CGRect) -> bool {
|
||||
unsafe { CGRectIntersectsRect(r1, r2) }
|
||||
}
|
||||
|
||||
/// Core graphics APIs use flipped coordinate system, while AppKit uses the
|
||||
/// unflippled version, they differ in the y-axis. We need to do the conversion
|
||||
/// (to `CGPoint.y`) manually.
|
||||
fn flip_frame_y(main_screen_height: f64, frame_height: f64, frame_unflipped_y: f64) -> f64 {
|
||||
main_screen_height - (frame_unflipped_y + frame_height)
|
||||
}
|
||||
|
||||
/// Helper function to extract an UI element's origin.
|
||||
fn get_ui_element_origin(ui_element: &CFRetained<AXUIElement>) -> Result<CGPoint, Error> {
|
||||
let mut position_value: *const CFType = std::ptr::null();
|
||||
let ptr_to_position_value = NonNull::new(&mut position_value).unwrap();
|
||||
let position_attr = CFString::from_static_str("AXPosition");
|
||||
let error = unsafe { ui_element.copy_attribute_value(&position_attr, ptr_to_position_value) };
|
||||
|
||||
if error != AXError::Success {
|
||||
return Err(Error::AXError(error));
|
||||
}
|
||||
assert!(!position_value.is_null());
|
||||
|
||||
let position: CFRetained<AXValue> =
|
||||
unsafe { CFRetained::from_raw(NonNull::new(position_value.cast_mut().cast()).unwrap()) };
|
||||
|
||||
let mut position_cg_point = CGPoint::ZERO;
|
||||
let ptr_to_position_cg_point =
|
||||
NonNull::new((&mut position_cg_point as *mut CGPoint).cast()).unwrap();
|
||||
|
||||
let result = unsafe { position.value(AXValueType::CGPoint, ptr_to_position_cg_point) };
|
||||
assert!(result, "type mismatched");
|
||||
|
||||
Ok(position_cg_point)
|
||||
}
|
||||
|
||||
/// Send a set origin request to the `ui_element`, return once request is sent.
|
||||
fn set_ui_element_origin_oneshot(
|
||||
ui_element: &CFRetained<AXUIElement>,
|
||||
mut origin: CGPoint,
|
||||
) -> Result<(), Error> {
|
||||
let ptr_to_origin = NonNull::new((&mut origin as *mut CGPoint).cast::<c_void>()).unwrap();
|
||||
let pos_value = unsafe { AXValue::new(AXValueType::CGPoint, ptr_to_origin) }.unwrap();
|
||||
let pos_attr = CFString::from_static_str("AXPosition");
|
||||
|
||||
let error = unsafe { ui_element.set_attribute_value(&pos_attr, pos_value.deref()) };
|
||||
if error != AXError::Success {
|
||||
return Err(Error::AXError(error));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Helper function to extract an UI element's size.
|
||||
fn get_ui_element_size(ui_element: &CFRetained<AXUIElement>) -> Result<CGSize, Error> {
|
||||
let mut size_value: *const CFType = std::ptr::null();
|
||||
let ptr_to_size_value = NonNull::new(&mut size_value).unwrap();
|
||||
let size_attr = CFString::from_static_str("AXSize");
|
||||
let error = unsafe { ui_element.copy_attribute_value(&size_attr, ptr_to_size_value) };
|
||||
|
||||
if error != AXError::Success {
|
||||
return Err(Error::AXError(error));
|
||||
}
|
||||
assert!(!size_value.is_null());
|
||||
|
||||
let size: CFRetained<AXValue> =
|
||||
unsafe { CFRetained::from_raw(NonNull::new(size_value.cast_mut().cast()).unwrap()) };
|
||||
|
||||
let mut size_cg_size = CGSize::ZERO;
|
||||
let ptr_to_size_cg_size = NonNull::new((&mut size_cg_size as *mut CGSize).cast()).unwrap();
|
||||
|
||||
let result = unsafe { size.value(AXValueType::CGSize, ptr_to_size_cg_size) };
|
||||
assert!(result, "type mismatched");
|
||||
|
||||
Ok(size_cg_size)
|
||||
}
|
||||
|
||||
/// Send a set size request to the `ui_element`, return once request is sent.
|
||||
fn set_ui_element_size_oneshot(
|
||||
ui_element: &CFRetained<AXUIElement>,
|
||||
mut size: CGSize,
|
||||
) -> Result<(), Error> {
|
||||
let ptr_to_size = NonNull::new((&mut size as *mut CGSize).cast::<c_void>()).unwrap();
|
||||
let size_value = unsafe { AXValue::new(AXValueType::CGSize, ptr_to_size) }.unwrap();
|
||||
let size_attr = CFString::from_static_str("AXSize");
|
||||
|
||||
let error = unsafe { ui_element.set_attribute_value(&size_attr, size_value.deref()) };
|
||||
if error != AXError::Success {
|
||||
return Err(Error::AXError(error));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Get the frontmost/focused window (as an UI element).
|
||||
fn get_frontmost_window() -> Result<CFRetained<AXUIElement>, Error> {
|
||||
let workspace = unsafe { NSWorkspace::sharedWorkspace() };
|
||||
let frontmost_app =
|
||||
unsafe { workspace.frontmostApplication() }.ok_or(Error::CannotFindFocusWindow)?;
|
||||
|
||||
let pid = unsafe { frontmost_app.processIdentifier() };
|
||||
|
||||
let app_element = unsafe { AXUIElement::new_application(pid) };
|
||||
|
||||
let mut window_element: *const CFType = std::ptr::null();
|
||||
let ptr_to_window_element = NonNull::new(&mut window_element).unwrap();
|
||||
let focused_window_attr = CFString::from_static_str("AXFocusedWindow");
|
||||
|
||||
let error =
|
||||
unsafe { app_element.copy_attribute_value(&focused_window_attr, ptr_to_window_element) };
|
||||
|
||||
if error != AXError::Success {
|
||||
return Err(Error::AXError(error));
|
||||
}
|
||||
assert!(!window_element.is_null());
|
||||
|
||||
let window_element: *mut AXUIElement = window_element.cast::<AXUIElement>().cast_mut();
|
||||
|
||||
let window = unsafe { CFRetained::from_raw(NonNull::new(window_element).unwrap()) };
|
||||
|
||||
Ok(window)
|
||||
}
|
||||
|
||||
/// Get the CGWindowID of the frontmost/focused window.
|
||||
#[allow(unused)] // In case we need it in the future
|
||||
pub(crate) fn get_frontmost_window_id() -> Result<CGWindowID, Error> {
|
||||
let element = get_frontmost_window()?;
|
||||
let ptr: NonNull<AXUIElement> = CFRetained::as_ptr(&element);
|
||||
|
||||
let mut window_id_buffer: CGWindowID = 0;
|
||||
let error =
|
||||
unsafe { private::_AXUIElementGetWindow(ptr.as_ptr(), &mut window_id_buffer as *mut _) };
|
||||
if error != AXError::Success {
|
||||
return Err(Error::AXError(error));
|
||||
}
|
||||
|
||||
Ok(window_id_buffer)
|
||||
}
|
||||
|
||||
/// Returns the workspace ID list grouped by display. For example, suppose you
|
||||
/// have 2 displays and 10 workspaces (5 workspaces per display), then this
|
||||
/// function might return something like:
|
||||
///
|
||||
/// ```text
|
||||
/// [
|
||||
/// [8, 11, 12, 13, 24],
|
||||
/// [519, 77, 15, 249, 414]
|
||||
/// ]
|
||||
/// ```
|
||||
///
|
||||
/// Even though this function return macOS internal space IDs, they should correspond
|
||||
/// to the logical workspace that users are familiar with. The display that contains
|
||||
/// workspaces `[8, 11, 12, 13, 24]` should be your main display; workspace 8 represents
|
||||
/// Desktop 1, and workspace 414 represents Desktop 10.
|
||||
fn workspace_ids_grouped_by_display() -> Vec<Vec<CGSSpaceID>> {
|
||||
unsafe {
|
||||
let mut ret = Vec::new();
|
||||
let conn = CGSMainConnectionID();
|
||||
|
||||
let display_spaces_raw = CGSCopyManagedDisplaySpaces(conn);
|
||||
let display_spaces: CFRetained<CFArray> =
|
||||
CFRetained::from_raw(NonNull::new(display_spaces_raw).unwrap());
|
||||
|
||||
let key_spaces: CFRetained<CFString> = CFString::from_static_str("Spaces");
|
||||
let key_spaces_ptr: NonNull<CFString> = CFRetained::as_ptr(&key_spaces);
|
||||
let key_id64: CFRetained<CFString> = CFString::from_static_str("id64");
|
||||
let key_id64_ptr: NonNull<CFString> = CFRetained::as_ptr(&key_id64);
|
||||
|
||||
for i in 0..display_spaces.count() {
|
||||
let mut workspaces_of_this_display = Vec::new();
|
||||
|
||||
let dict_ref = display_spaces.value_at_index(i);
|
||||
let dict: &CFDictionary = &*(dict_ref as *const CFDictionary);
|
||||
|
||||
let mut ptr_to_value_buffer: *const c_void = std::ptr::null();
|
||||
let key_exists = dict.value_if_present(
|
||||
key_spaces_ptr.as_ptr().cast::<c_void>().cast_const(),
|
||||
&mut ptr_to_value_buffer as *mut _,
|
||||
);
|
||||
assert!(key_exists);
|
||||
assert!(!ptr_to_value_buffer.is_null());
|
||||
|
||||
let spaces_raw: *const CFArray = ptr_to_value_buffer.cast::<CFArray>();
|
||||
|
||||
let spaces = &*spaces_raw;
|
||||
|
||||
for idx in 0..spaces.count() {
|
||||
let workspace_dictionary: &CFDictionary =
|
||||
&*spaces.value_at_index(idx).cast::<CFDictionary>();
|
||||
|
||||
let mut ptr_to_value_buffer: *const c_void = std::ptr::null();
|
||||
let key_exists = workspace_dictionary.value_if_present(
|
||||
key_id64_ptr.as_ptr().cast::<c_void>().cast_const(),
|
||||
&mut ptr_to_value_buffer as *mut _,
|
||||
);
|
||||
assert!(key_exists);
|
||||
assert!(!ptr_to_value_buffer.is_null());
|
||||
|
||||
let ptr_workspace_id = ptr_to_value_buffer.cast::<CFNumber>();
|
||||
let workspace_id = (&*ptr_workspace_id).as_i32().unwrap();
|
||||
|
||||
workspaces_of_this_display.push(workspace_id);
|
||||
}
|
||||
|
||||
ret.push(workspaces_of_this_display);
|
||||
}
|
||||
|
||||
ret
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the next workspace's logical ID. By logical ID, we mean the ID that
|
||||
/// users are familiar with, workspace 1/2/3 and so on, rather than the internal
|
||||
/// `CGSSpaceID`.
|
||||
///
|
||||
/// NOTE that this function returns None when the current workspace is the last
|
||||
/// workspace in the current display.
|
||||
pub(crate) fn get_next_workspace_logical_id() -> Option<usize> {
|
||||
let window_server_connection = unsafe { CGSMainConnectionID() };
|
||||
let current_workspace_id = unsafe { CGSGetActiveSpace(window_server_connection) };
|
||||
|
||||
// Logical ID starts from 1
|
||||
let mut logical_id = 1_usize;
|
||||
|
||||
for workspaces_in_a_display in workspace_ids_grouped_by_display() {
|
||||
for (idx, workspace_raw_id) in workspaces_in_a_display.iter().enumerate() {
|
||||
if *workspace_raw_id == current_workspace_id {
|
||||
// We found it, now check if it is the last workspace in this display
|
||||
if idx == workspaces_in_a_display.len() - 1 {
|
||||
return None;
|
||||
} else {
|
||||
return Some(logical_id + 1);
|
||||
}
|
||||
} else {
|
||||
logical_id += 1;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unreachable!(
|
||||
"unless the private API CGSGetActiveSpace() is broken, it should return an ID that is in the workspace ID list"
|
||||
)
|
||||
}
|
||||
|
||||
/// Get the previous workspace's logical ID.
|
||||
///
|
||||
/// See [`get_next_workspace_logical_id`] for the doc.
|
||||
pub(crate) fn get_previous_workspace_logical_id() -> Option<usize> {
|
||||
let window_server_connection = unsafe { CGSMainConnectionID() };
|
||||
let current_workspace_id = unsafe { CGSGetActiveSpace(window_server_connection) };
|
||||
|
||||
// Logical ID starts from 1
|
||||
let mut logical_id = 1_usize;
|
||||
|
||||
for workspaces_in_a_display in workspace_ids_grouped_by_display() {
|
||||
for (idx, workspace_raw_id) in workspaces_in_a_display.iter().enumerate() {
|
||||
if *workspace_raw_id == current_workspace_id {
|
||||
// We found it, now check if it is the first workspace in this display
|
||||
if idx == 0 {
|
||||
return None;
|
||||
} else {
|
||||
// this sub operation is safe, logical_id is at least 2
|
||||
return Some(logical_id - 1);
|
||||
}
|
||||
} else {
|
||||
logical_id += 1;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unreachable!(
|
||||
"unless the private API CGSGetActiveSpace() is broken, it should return an ID that is in the workspace ID list"
|
||||
)
|
||||
}
|
||||
|
||||
/// Move the frontmost window to the specified workspace.
|
||||
///
|
||||
/// Credits to the Silica library
|
||||
///
|
||||
/// * https://github.com/ianyh/Silica/blob/b91a18dbb822e99ce6b487d1cb4841e863139b2a/Silica/Sources/SIWindow.m#L215-L260
|
||||
/// * https://github.com/ianyh/Silica/blob/b91a18dbb822e99ce6b487d1cb4841e863139b2a/Silica/Sources/SISystemWideElement.m#L29-L65
|
||||
pub(crate) fn move_frontmost_window_to_workspace(space: usize) -> Result<(), Error> {
|
||||
assert!(space >= 1);
|
||||
if space > 16 {
|
||||
return Err(Error::TooManyWorkspace);
|
||||
}
|
||||
|
||||
let window_frame = get_frontmost_window_frame()?;
|
||||
let close_button_frame = get_frontmost_window_close_button_frame()?;
|
||||
let prev_mouse_position = unsafe {
|
||||
let event = CGEvent::new(None);
|
||||
CGEvent::location(event.as_deref())
|
||||
};
|
||||
|
||||
let mouse_cursor_point = CGPoint::new(
|
||||
unsafe { CGRectGetMidX(close_button_frame) },
|
||||
window_frame.origin.y
|
||||
+ (window_frame.origin.y - unsafe { CGRectGetMinY(close_button_frame) }).abs() / 2.0,
|
||||
);
|
||||
|
||||
let mouse_move_event = unsafe {
|
||||
CGEvent::new_mouse_event(
|
||||
None,
|
||||
CGEventType::MouseMoved,
|
||||
mouse_cursor_point,
|
||||
CGMouseButton::Left,
|
||||
)
|
||||
};
|
||||
let mouse_drag_event = unsafe {
|
||||
CGEvent::new_mouse_event(
|
||||
None,
|
||||
CGEventType::LeftMouseDragged,
|
||||
mouse_cursor_point,
|
||||
CGMouseButton::Left,
|
||||
)
|
||||
};
|
||||
let mouse_down_event = unsafe {
|
||||
CGEvent::new_mouse_event(
|
||||
None,
|
||||
CGEventType::LeftMouseDown,
|
||||
mouse_cursor_point,
|
||||
CGMouseButton::Left,
|
||||
)
|
||||
};
|
||||
let mouse_up_event = unsafe {
|
||||
CGEvent::new_mouse_event(
|
||||
None,
|
||||
CGEventType::LeftMouseUp,
|
||||
mouse_cursor_point,
|
||||
CGMouseButton::Left,
|
||||
)
|
||||
};
|
||||
|
||||
unsafe {
|
||||
CGEvent::set_flags(mouse_move_event.as_deref(), CGEventFlags(0));
|
||||
CGEvent::set_flags(mouse_down_event.as_deref(), CGEventFlags(0));
|
||||
CGEvent::set_flags(mouse_up_event.as_deref(), CGEventFlags(0));
|
||||
|
||||
// Move the mouse into place at the window's toolbar
|
||||
CGEvent::post(CGEventTapLocation::HIDEventTap, mouse_move_event.as_deref());
|
||||
// Mouse down to set up the drag
|
||||
CGEvent::post(CGEventTapLocation::HIDEventTap, mouse_down_event.as_deref());
|
||||
// Drag event to grab hold of the window
|
||||
CGEvent::post(CGEventTapLocation::HIDEventTap, mouse_drag_event.as_deref());
|
||||
}
|
||||
|
||||
// Make a slight delay to make sure the window is grabbed
|
||||
std::thread::sleep(Duration::from_millis(50));
|
||||
|
||||
// cast is safe as space is in range [1, 16]
|
||||
let hot_key: c_ushort = 118 + space as c_ushort - 1;
|
||||
|
||||
let mut flags: c_uint = 0;
|
||||
let mut key_code: c_ushort = 0;
|
||||
let error = unsafe {
|
||||
private::CGSGetSymbolicHotKeyValue(hot_key, std::ptr::null_mut(), &mut key_code, &mut flags)
|
||||
};
|
||||
if error != CGError::Success {
|
||||
return Err(Error::CGError(error));
|
||||
}
|
||||
|
||||
unsafe {
|
||||
// If the hotkey is disabled, enable it.
|
||||
if !private::CGSIsSymbolicHotKeyEnabled(hot_key) {
|
||||
if private::CGSSetSymbolicHotKeyEnabled(hot_key, true) != CGError::Success {
|
||||
return Err(Error::CGError(error));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let opt_keyboard_event = unsafe { CGEvent::new_keyboard_event(None, key_code, true) };
|
||||
unsafe {
|
||||
// cast is safe (uint -> u64)
|
||||
CGEvent::set_flags(opt_keyboard_event.as_deref(), CGEventFlags(flags as u64));
|
||||
}
|
||||
|
||||
let keyboard_event = opt_keyboard_event.unwrap();
|
||||
let event = unsafe { NSEvent::eventWithCGEvent(&keyboard_event) }.unwrap();
|
||||
|
||||
let keyboard_event_up = unsafe { CGEvent::new_keyboard_event(None, event.keyCode(), false) };
|
||||
unsafe {
|
||||
CGEvent::set_flags(keyboard_event_up.as_deref(), CGEventFlags(0));
|
||||
|
||||
// Send the shortcut command to get Mission Control to switch spaces from under the window.
|
||||
CGEvent::post(CGEventTapLocation::HIDEventTap, event.CGEvent().as_deref());
|
||||
CGEvent::post(
|
||||
CGEventTapLocation::HIDEventTap,
|
||||
keyboard_event_up.as_deref(),
|
||||
);
|
||||
}
|
||||
|
||||
// Make a slight delay to finish the space transition animation
|
||||
std::thread::sleep(Duration::from_millis(50));
|
||||
|
||||
/*
|
||||
* Cleanup
|
||||
*/
|
||||
unsafe {
|
||||
// Let go of the window.
|
||||
CGEvent::post(CGEventTapLocation::HIDEventTap, mouse_up_event.as_deref());
|
||||
|
||||
// Reset mouse position
|
||||
let mouse_reset_event = {
|
||||
CGEvent::new_mouse_event(
|
||||
None,
|
||||
CGEventType::MouseMoved,
|
||||
prev_mouse_position,
|
||||
CGMouseButton::Left,
|
||||
)
|
||||
};
|
||||
CGEvent::set_flags(mouse_reset_event.as_deref(), CGEventFlags(0));
|
||||
CGEvent::post(
|
||||
CGEventTapLocation::HIDEventTap,
|
||||
mouse_reset_event.as_deref(),
|
||||
);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn get_frontmost_window_origin() -> Result<CGPoint, Error> {
|
||||
let frontmost_window = get_frontmost_window()?;
|
||||
get_ui_element_origin(&frontmost_window)
|
||||
}
|
||||
|
||||
pub(crate) fn get_frontmost_window_size() -> Result<CGSize, Error> {
|
||||
let frontmost_window = get_frontmost_window()?;
|
||||
get_ui_element_size(&frontmost_window)
|
||||
}
|
||||
|
||||
pub(crate) fn get_frontmost_window_frame() -> Result<CGRect, Error> {
|
||||
let origin = get_frontmost_window_origin()?;
|
||||
let size = get_frontmost_window_size()?;
|
||||
|
||||
Ok(CGRect { origin, size })
|
||||
}
|
||||
|
||||
/// Get the frontmost window's close button, then extract its frame.
|
||||
fn get_frontmost_window_close_button_frame() -> Result<CGRect, Error> {
|
||||
let window = get_frontmost_window()?;
|
||||
|
||||
let mut ptr_to_close_button: *const CFType = std::ptr::null();
|
||||
let ptr_to_buffer = NonNull::new(&mut ptr_to_close_button).unwrap();
|
||||
|
||||
let close_button_attribute = CFString::from_static_str("AXCloseButton");
|
||||
let error = unsafe { window.copy_attribute_value(&close_button_attribute, ptr_to_buffer) };
|
||||
if error != AXError::Success {
|
||||
return Err(Error::AXError(error));
|
||||
}
|
||||
assert!(!ptr_to_close_button.is_null());
|
||||
|
||||
let close_button_element = ptr_to_close_button.cast::<AXUIElement>().cast_mut();
|
||||
let close_button = unsafe { CFRetained::from_raw(NonNull::new(close_button_element).unwrap()) };
|
||||
|
||||
let origin = get_ui_element_origin(&close_button)?;
|
||||
let size = get_ui_element_size(&close_button)?;
|
||||
|
||||
Ok(CGRect { origin, size })
|
||||
}
|
||||
|
||||
/// This function returns the "visible frame" [^1] of all the screens.
|
||||
///
|
||||
/// FIXME: This function relies on the [`visibleFrame()`][vf_doc] API, which
|
||||
/// has 2 bugs we need to work around:
|
||||
///
|
||||
/// 1. It assumes the Dock is on the main display, which in reality depends on
|
||||
/// how users arrange their displays and the "Dock position on screen" setting
|
||||
/// entry.
|
||||
/// 2. For non-main displays, it assumes that they don't have a menu bar, but macOS
|
||||
/// puts a menu bar on every display.
|
||||
///
|
||||
/// Update: This could be wrong, but looks like Apple fixed these 2 bugs in macOS
|
||||
/// 26. At least the buggy behaviors disappear in my test.
|
||||
///
|
||||
///
|
||||
/// [^1]: Visible frame: a rectangle defines the portion of the screen in which it
|
||||
/// is currently safe to draw your app’s content.
|
||||
///
|
||||
/// [vf_doc]: https://developer.apple.com/documentation/AppKit/NSScreen/visibleFrame
|
||||
pub(crate) fn list_visible_frame_of_all_screens() -> Result<Vec<CGRect>, Error> {
|
||||
let main_thread_marker = MainThreadMarker::new().ok_or(Error::NotInMainThread)?;
|
||||
let screens = NSScreen::screens(main_thread_marker).to_vec();
|
||||
|
||||
if screens.is_empty() {
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
|
||||
let main_screen = screens.first().expect("screens is not empty");
|
||||
|
||||
let frames = screens
|
||||
.iter()
|
||||
.map(|ns_screen| {
|
||||
// NSScreen is an AppKit API, which uses unflipped coordinate
|
||||
// system, flip it
|
||||
let mut unflipped_frame = ns_screen.visibleFrame();
|
||||
let flipped_frame_origin_y = flip_frame_y(
|
||||
main_screen.frame().size.height,
|
||||
unflipped_frame.size.height,
|
||||
unflipped_frame.origin.y,
|
||||
);
|
||||
unflipped_frame.origin.y = flipped_frame_origin_y;
|
||||
|
||||
unflipped_frame
|
||||
})
|
||||
.collect();
|
||||
|
||||
Ok(frames)
|
||||
}
|
||||
|
||||
/// Get the Visible frame of the "active screen"[^1].
|
||||
///
|
||||
///
|
||||
/// [^1]: the screen which the frontmost window is on.
|
||||
pub(crate) fn get_active_screen_visible_frame() -> Result<CGRect, Error> {
|
||||
let main_thread_marker = MainThreadMarker::new().ok_or(Error::NotInMainThread)?;
|
||||
|
||||
let frontmost_window_frame = get_frontmost_window_frame()?;
|
||||
|
||||
let screens = NSScreen::screens(main_thread_marker)
|
||||
.into_iter()
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
if screens.is_empty() {
|
||||
return Err(Error::NoDisplay);
|
||||
}
|
||||
|
||||
let main_screen_height = screens[0].frame().size.height;
|
||||
|
||||
// AppKit uses Unflipped Coordinate System, but Accessibility APIs use
|
||||
// Flipped Coordinate System, we need to flip the origin of these screens.
|
||||
for screen in screens {
|
||||
let mut screen_frame = screen.frame();
|
||||
let unflipped_y = screen_frame.origin.y;
|
||||
let flipped_y = flip_frame_y(main_screen_height, screen_frame.size.height, unflipped_y);
|
||||
screen_frame.origin.y = flipped_y;
|
||||
|
||||
if intersects(screen_frame, frontmost_window_frame) {
|
||||
let mut visible_frame = screen.visibleFrame();
|
||||
let flipped_y = flip_frame_y(
|
||||
main_screen_height,
|
||||
visible_frame.size.height,
|
||||
visible_frame.origin.y,
|
||||
);
|
||||
visible_frame.origin.y = flipped_y;
|
||||
|
||||
return Ok(visible_frame);
|
||||
}
|
||||
}
|
||||
|
||||
unreachable!()
|
||||
}
|
||||
|
||||
/// Move the frontmost window's origin to the point specified by `x` and `y`.
|
||||
pub fn move_frontmost_window(x: f64, y: f64) -> Result<(), Error> {
|
||||
let frontmost_window = get_frontmost_window()?;
|
||||
|
||||
let mut point = CGPoint::new(x, y);
|
||||
let ptr_to_point = NonNull::new((&mut point as *mut CGPoint).cast::<c_void>()).unwrap();
|
||||
let pos_value = unsafe { AXValue::new(AXValueType::CGPoint, ptr_to_point) }.unwrap();
|
||||
let pos_attr = CFString::from_static_str("AXPosition");
|
||||
|
||||
let error = unsafe { frontmost_window.set_attribute_value(&pos_attr, pos_value.deref()) };
|
||||
if error != AXError::Success {
|
||||
return Err(Error::AXError(error));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Set the frontmost window's frame to the specified frame - adjust size and
|
||||
/// location at the same time.
|
||||
///
|
||||
/// This function **retries** up to `RETRY` times until the set operations
|
||||
/// successfully get performed.
|
||||
///
|
||||
/// # Retry
|
||||
///
|
||||
/// Retry is added because I encountered a case where `AXUIElementSetAttributeValue()`
|
||||
/// does not work in the expected way. When I execute the `NextDisplay` command
|
||||
/// to move the focused window from a big display (2560x1440) to a small display
|
||||
/// (1440*900), the window size could be set to 1460 sometimes. No idea if this
|
||||
/// is a bug of the Accessibility APIs or due to the improper API uses. So we
|
||||
/// retry for `RETRY` times at most to try our beest make it behave correctly.
|
||||
pub fn set_frontmost_window_frame(frame: CGRect) -> Result<(), Error> {
|
||||
const RETRY: usize = 5;
|
||||
/// Sleep for 50ms as I don't want to send too many requests to the focused
|
||||
/// app and WindowServer because doing that could make them busy and then
|
||||
/// they won't process my set requests.
|
||||
///
|
||||
/// The above is simply my observation, I don't know how the messaging really
|
||||
/// works under the hood.
|
||||
const SLEEP: Duration = Duration::from_millis(50);
|
||||
|
||||
let frontmost_window = get_frontmost_window()?;
|
||||
|
||||
/*
|
||||
* Set window origin
|
||||
*/
|
||||
set_ui_element_origin_oneshot(&frontmost_window, frame.origin)?;
|
||||
for _ in 0..RETRY {
|
||||
std::thread::sleep(SLEEP);
|
||||
|
||||
let current = get_ui_element_origin(&frontmost_window)?;
|
||||
if current == frame.origin {
|
||||
break;
|
||||
} else {
|
||||
set_ui_element_origin_oneshot(&frontmost_window, frame.origin)?;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Set window size
|
||||
*/
|
||||
set_ui_element_size_oneshot(&frontmost_window, frame.size)?;
|
||||
for _ in 0..RETRY {
|
||||
std::thread::sleep(SLEEP);
|
||||
|
||||
let current = get_ui_element_size(&frontmost_window)?;
|
||||
// For size, we do not check if `current` has the exact same value as
|
||||
// `frame.size` as I have encountered a case where I ask macOS to set
|
||||
// the height to 1550, but the height gets set to 1551.
|
||||
if cgsize_roughly_equal(current, frame.size, 3.0) {
|
||||
break;
|
||||
} else {
|
||||
set_ui_element_size_oneshot(&frontmost_window, frame.size)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn toggle_fullscreen() -> Result<(), Error> {
|
||||
let frontmost_window = get_frontmost_window()?;
|
||||
let fullscreen_attr = CFString::from_static_str("AXFullScreen");
|
||||
|
||||
let mut current_value_ref: *const CFType = std::ptr::null();
|
||||
let error = unsafe {
|
||||
frontmost_window.copy_attribute_value(
|
||||
&fullscreen_attr,
|
||||
NonNull::new(&mut current_value_ref).unwrap(),
|
||||
)
|
||||
};
|
||||
|
||||
// TODO: If the attribute doesn't exist, error won't be Success as well.
|
||||
// Before we handle that, we need to know the error case that will be
|
||||
// returned in that case.
|
||||
if error != AXError::Success {
|
||||
return Err(Error::AXError(error));
|
||||
}
|
||||
assert!(!current_value_ref.is_null());
|
||||
|
||||
let current_value = unsafe {
|
||||
let retained_boolean: CFRetained<CFBoolean> = CFRetained::from_raw(
|
||||
NonNull::new(current_value_ref.cast::<CFBoolean>().cast_mut()).unwrap(),
|
||||
);
|
||||
retained_boolean.as_bool()
|
||||
};
|
||||
|
||||
let new_value = !current_value;
|
||||
let new_value_ref: CFRetained<CFBoolean> = CFBoolean::new(new_value).retain();
|
||||
|
||||
let error =
|
||||
unsafe { frontmost_window.set_attribute_value(&fullscreen_attr, new_value_ref.deref()) };
|
||||
|
||||
if error != AXError::Success {
|
||||
return Err(Error::AXError(error));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Check if `lhs` roughly equals to `rhs`. The Roughness can be controlled by
|
||||
/// argument `tolerance`.
|
||||
fn cgsize_roughly_equal(lhs: CGSize, rhs: CGSize, tolerance: f64) -> bool {
|
||||
let width_diff = (lhs.width - rhs.width).abs();
|
||||
let height_diff = (lhs.height - rhs.height).abs();
|
||||
|
||||
width_diff <= tolerance && height_diff <= tolerance
|
||||
}
|
||||
|
||||
static LAST_FRAME: LazyLock<Mutex<HashMap<CGWindowID, CGRect>>> =
|
||||
LazyLock::new(|| Mutex::new(HashMap::new()));
|
||||
|
||||
pub(crate) fn set_frontmost_window_last_frame(window_id: CGWindowID, frame: CGRect) {
|
||||
let mut map = LAST_FRAME.lock().unwrap();
|
||||
map.insert(window_id, frame);
|
||||
}
|
||||
|
||||
pub(crate) fn get_frontmost_window_last_frame(window_id: CGWindowID) -> Option<CGRect> {
|
||||
let map = LAST_FRAME.lock().unwrap();
|
||||
map.get(&window_id).cloned()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_intersects_adjacent_rects_x() {
|
||||
let r1 = CGRect::new(CGPoint::new(0.0, 0.0), CGSize::new(100.0, 100.0));
|
||||
let r2 = CGRect::new(CGPoint::new(100.0, 0.0), CGSize::new(100.0, 100.0));
|
||||
assert!(
|
||||
!intersects(r1, r2),
|
||||
"Adjacent rects on X should not intersect"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_intersects_adjacent_rects_y() {
|
||||
let r1 = CGRect::new(CGPoint::new(0.0, 0.0), CGSize::new(100.0, 100.0));
|
||||
let r2 = CGRect::new(CGPoint::new(0.0, 100.0), CGSize::new(100.0, 100.0));
|
||||
assert!(
|
||||
!intersects(r1, r2),
|
||||
"Adjacent rects on Y should not intersect"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_intersects_overlapping_rects() {
|
||||
let r1 = CGRect::new(CGPoint::new(0.0, 0.0), CGSize::new(100.0, 100.0));
|
||||
let r2 = CGRect::new(CGPoint::new(50.0, 50.0), CGSize::new(100.0, 100.0));
|
||||
assert!(intersects(r1, r2), "Overlapping rects should intersect");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_intersects_separate_rects() {
|
||||
let r1 = CGRect::new(CGPoint::new(0.0, 0.0), CGSize::new(100.0, 100.0));
|
||||
let r2 = CGRect::new(CGPoint::new(101.0, 101.0), CGSize::new(100.0, 100.0));
|
||||
assert!(!intersects(r1, r2), "Separate rects should not intersect");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_intersects_contained_rect() {
|
||||
let r1 = CGRect::new(CGPoint::new(0.0, 0.0), CGSize::new(100.0, 100.0));
|
||||
let r2 = CGRect::new(CGPoint::new(10.0, 10.0), CGSize::new(50.0, 50.0));
|
||||
assert!(intersects(r1, r2), "Contained rect should intersect");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_intersects_identical_rects() {
|
||||
let r1 = CGRect::new(CGPoint::new(0.0, 0.0), CGSize::new(100.0, 100.0));
|
||||
let r2 = CGRect::new(CGPoint::new(0.0, 0.0), CGSize::new(100.0, 100.0));
|
||||
assert!(intersects(r1, r2), "Identical rects should intersect");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
//! Private macOS APIs.
|
||||
|
||||
use bitflags::bitflags;
|
||||
use objc2_application_services::AXError;
|
||||
use objc2_application_services::AXUIElement;
|
||||
use objc2_core_foundation::CFArray;
|
||||
use objc2_core_graphics::CGError;
|
||||
use objc2_core_graphics::CGWindowID;
|
||||
use std::ffi::c_int;
|
||||
use std::ffi::c_uint;
|
||||
use std::ffi::c_ushort;
|
||||
|
||||
pub(crate) type CGSConnectionID = u32;
|
||||
pub(crate) type CGSSpaceID = c_int;
|
||||
|
||||
bitflags! {
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||
#[repr(transparent)]
|
||||
pub struct CGSSpaceMask: c_int {
|
||||
const INCLUDE_CURRENT = 1 << 0;
|
||||
const INCLUDE_OTHERS = 1 << 1;
|
||||
|
||||
const INCLUDE_USER = 1 << 2;
|
||||
const INCLUDE_OS = 1 << 3;
|
||||
|
||||
const VISIBLE = 1 << 16;
|
||||
|
||||
const CURRENT_SPACES = Self::INCLUDE_USER.bits() | Self::INCLUDE_CURRENT.bits();
|
||||
const OTHER_SPACES = Self::INCLUDE_USER.bits() | Self::INCLUDE_OTHERS.bits();
|
||||
const ALL_SPACES =
|
||||
Self::INCLUDE_USER.bits() | Self::INCLUDE_OTHERS.bits() | Self::INCLUDE_CURRENT.bits();
|
||||
|
||||
const ALL_VISIBLE_SPACES = Self::ALL_SPACES.bits() | Self::VISIBLE.bits();
|
||||
|
||||
const CURRENT_OS_SPACES = Self::INCLUDE_OS.bits() | Self::INCLUDE_CURRENT.bits();
|
||||
const OTHER_OS_SPACES = Self::INCLUDE_OS.bits() | Self::INCLUDE_OTHERS.bits();
|
||||
const ALL_OS_SPACES =
|
||||
Self::INCLUDE_OS.bits() | Self::INCLUDE_OTHERS.bits() | Self::INCLUDE_CURRENT.bits();
|
||||
}
|
||||
}
|
||||
|
||||
unsafe extern "C" {
|
||||
/// Extract `window_id` from an AXUIElement.
|
||||
pub(crate) fn _AXUIElementGetWindow(
|
||||
elem: *mut AXUIElement,
|
||||
window_id: *mut CGWindowID,
|
||||
) -> AXError;
|
||||
|
||||
/// Connect to the WindowServer and get a connection descriptor.
|
||||
pub(crate) fn CGSMainConnectionID() -> CGSConnectionID;
|
||||
|
||||
/// It returns a CFArray of dictionaries. Each dictionary contains information
|
||||
/// about a display, including a list of all the spaces (CGSSpaceID) on that display.
|
||||
pub(crate) fn CGSCopyManagedDisplaySpaces(cid: CGSConnectionID) -> *mut CFArray;
|
||||
|
||||
/// Gets the ID of the space currently visible to the user.
|
||||
pub(crate) fn CGSGetActiveSpace(cid: CGSConnectionID) -> CGSSpaceID;
|
||||
|
||||
/// Returns the values the symbolic hot key represented by the given UID is configured with.
|
||||
pub(crate) fn CGSGetSymbolicHotKeyValue(
|
||||
hotKey: c_ushort,
|
||||
outKeyEquivalent: *mut c_ushort,
|
||||
outVirtualKeyCode: *mut c_ushort,
|
||||
outModifiers: *mut c_uint,
|
||||
) -> CGError;
|
||||
/// Returns whether the symbolic hot key represented by the given UID is enabled.
|
||||
pub(crate) fn CGSIsSymbolicHotKeyEnabled(hotKey: c_ushort) -> bool;
|
||||
/// Sets whether the symbolic hot key represented by the given UID is enabled.
|
||||
pub(crate) fn CGSSetSymbolicHotKeyEnabled(hotKey: c_ushort, isEnabled: bool) -> CGError;
|
||||
}
|
||||
25
src-tauri/src/extension/built_in/window_management/error.rs
Normal file
@@ -0,0 +1,25 @@
|
||||
use objc2_application_services::AXError;
|
||||
use objc2_core_graphics::CGError;
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum Error {
|
||||
/// Cannot find the focused window.
|
||||
#[error("Cannot find the focused window.")]
|
||||
CannotFindFocusWindow,
|
||||
/// Error code from the macOS Accessibility APIs.
|
||||
#[error("Error code from the macOS Accessibility APIs: {0:?}")]
|
||||
AXError(AXError),
|
||||
/// Function should be in called from the main thread, but it is not.
|
||||
#[error("Function should be in called from the main thread, but it is not.")]
|
||||
NotInMainThread,
|
||||
/// No monitor detected.
|
||||
#[error("No monitor detected.")]
|
||||
NoDisplay,
|
||||
/// Can only handle 16 Workspaces at most.
|
||||
#[error("libwmgr can only handle 16 Workspaces at most.")]
|
||||
TooManyWorkspace,
|
||||
/// Error code from the macOS Core Graphics APIs.
|
||||
#[error("Error code from the macOS Core Graphics APIs: {0:?}")]
|
||||
CGError(CGError),
|
||||
}
|
||||
974
src-tauri/src/extension/built_in/window_management/mod.rs
Normal file
@@ -0,0 +1,974 @@
|
||||
pub(crate) mod actions;
|
||||
mod backend;
|
||||
mod error;
|
||||
pub(crate) mod on_opened;
|
||||
pub(crate) mod search_source;
|
||||
|
||||
use crate::common::document::open;
|
||||
use crate::extension::Extension;
|
||||
use actions::Action;
|
||||
use backend::get_active_screen_visible_frame;
|
||||
use backend::get_frontmost_window_frame;
|
||||
use backend::get_frontmost_window_id;
|
||||
use backend::get_frontmost_window_last_frame;
|
||||
use backend::get_next_workspace_logical_id;
|
||||
use backend::get_previous_workspace_logical_id;
|
||||
use backend::list_visible_frame_of_all_screens;
|
||||
use backend::move_frontmost_window;
|
||||
use backend::move_frontmost_window_to_workspace;
|
||||
use backend::set_frontmost_window_frame;
|
||||
use backend::set_frontmost_window_last_frame;
|
||||
use backend::toggle_fullscreen;
|
||||
use error::Error;
|
||||
use objc2_core_foundation::{CGPoint, CGRect, CGSize};
|
||||
use oneshot::channel as oneshot_channel;
|
||||
use tauri::AppHandle;
|
||||
use tauri::async_runtime;
|
||||
use tauri_plugin_global_shortcut::GlobalShortcutExt;
|
||||
use tauri_plugin_global_shortcut::ShortcutState;
|
||||
|
||||
pub(crate) const EXTENSION_ID: &str = "Window Management";
|
||||
pub(crate) const EXTENSION_NAME_LOWERCASE: &str = "window management";
|
||||
|
||||
/// JSON file for this extension.
|
||||
pub(crate) const PLUGIN_JSON_FILE: &str = include_str!("./plugin.json");
|
||||
|
||||
pub(crate) fn perform_action_on_main_thread(
|
||||
tauri_app_handle: &AppHandle,
|
||||
action: Action,
|
||||
) -> Result<(), String> {
|
||||
let (tx, rx) = oneshot_channel();
|
||||
|
||||
tauri_app_handle
|
||||
.run_on_main_thread(move || {
|
||||
let res = perform_action(action).map_err(|e| e.to_string());
|
||||
tx.send(res)
|
||||
.expect("oneshot channel receiver unexpectedly dropped");
|
||||
})
|
||||
.expect("tauri internal bug, channel receiver dropped");
|
||||
|
||||
rx.recv()
|
||||
.expect("oneshot channel sender unexpectedly dropped before sending function return value")
|
||||
}
|
||||
|
||||
/// Perform this action to the focused window.
|
||||
fn perform_action(action: Action) -> Result<(), Error> {
|
||||
let visible_frame = get_active_screen_visible_frame()?;
|
||||
let frontmost_window_id = get_frontmost_window_id()?;
|
||||
let frontmost_window_frame = get_frontmost_window_frame()?;
|
||||
|
||||
set_frontmost_window_last_frame(frontmost_window_id, frontmost_window_frame);
|
||||
|
||||
match action {
|
||||
Action::TopHalf => {
|
||||
let origin = CGPoint {
|
||||
x: visible_frame.origin.x,
|
||||
y: visible_frame.origin.y,
|
||||
};
|
||||
let size = CGSize {
|
||||
width: visible_frame.size.width,
|
||||
height: visible_frame.size.height / 2.0,
|
||||
};
|
||||
let new_frame = CGRect { origin, size };
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
Action::BottomHalf => {
|
||||
let origin = CGPoint {
|
||||
x: visible_frame.origin.x,
|
||||
y: visible_frame.origin.y + visible_frame.size.height / 2.0,
|
||||
};
|
||||
let size = CGSize {
|
||||
width: visible_frame.size.width,
|
||||
height: visible_frame.size.height / 2.0,
|
||||
};
|
||||
let new_frame = CGRect { origin, size };
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
Action::LeftHalf => {
|
||||
let origin = CGPoint {
|
||||
x: visible_frame.origin.x,
|
||||
y: visible_frame.origin.y,
|
||||
};
|
||||
let size = CGSize {
|
||||
width: visible_frame.size.width / 2.0,
|
||||
height: visible_frame.size.height,
|
||||
};
|
||||
let new_frame = CGRect { origin, size };
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
Action::RightHalf => {
|
||||
let origin = CGPoint {
|
||||
x: visible_frame.origin.x + visible_frame.size.width / 2.0,
|
||||
y: visible_frame.origin.y,
|
||||
};
|
||||
let size = CGSize {
|
||||
width: visible_frame.size.width / 2.0,
|
||||
height: visible_frame.size.height,
|
||||
};
|
||||
let new_frame = CGRect { origin, size };
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
Action::CenterHalf => {
|
||||
let origin = CGPoint {
|
||||
x: visible_frame.origin.x + visible_frame.size.width / 4.0,
|
||||
y: visible_frame.origin.y,
|
||||
};
|
||||
let size = CGSize {
|
||||
width: visible_frame.size.width / 2.0,
|
||||
height: visible_frame.size.height,
|
||||
};
|
||||
let new_frame = CGRect { origin, size };
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
Action::TopLeftQuarter => {
|
||||
let origin = visible_frame.origin;
|
||||
let size = CGSize {
|
||||
width: visible_frame.size.width / 2.0,
|
||||
height: visible_frame.size.height / 2.0,
|
||||
};
|
||||
let new_frame = CGRect { origin, size };
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
Action::TopRightQuarter => {
|
||||
let origin = CGPoint {
|
||||
x: visible_frame.origin.x + visible_frame.size.width / 2.0,
|
||||
y: visible_frame.origin.y,
|
||||
};
|
||||
let size = CGSize {
|
||||
width: visible_frame.size.width / 2.0,
|
||||
height: visible_frame.size.height / 2.0,
|
||||
};
|
||||
let new_frame = CGRect { origin, size };
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
Action::BottomLeftQuarter => {
|
||||
let origin = CGPoint {
|
||||
x: visible_frame.origin.x,
|
||||
y: visible_frame.origin.y + visible_frame.size.height / 2.0,
|
||||
};
|
||||
let size = CGSize {
|
||||
width: visible_frame.size.width / 2.0,
|
||||
height: visible_frame.size.height / 2.0,
|
||||
};
|
||||
let new_frame = CGRect { origin, size };
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
Action::BottomRightQuarter => {
|
||||
let origin = CGPoint {
|
||||
x: visible_frame.origin.x + visible_frame.size.width / 2.0,
|
||||
y: visible_frame.origin.y + visible_frame.size.height / 2.0,
|
||||
};
|
||||
let size = CGSize {
|
||||
width: visible_frame.size.width / 2.0,
|
||||
height: visible_frame.size.height / 2.0,
|
||||
};
|
||||
let new_frame = CGRect { origin, size };
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
Action::TopLeftSixth => {
|
||||
let origin = visible_frame.origin;
|
||||
let size = CGSize {
|
||||
width: visible_frame.size.width / 3.0,
|
||||
height: visible_frame.size.height / 2.0,
|
||||
};
|
||||
let new_frame = CGRect { origin, size };
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
Action::TopCenterSixth => {
|
||||
let origin = CGPoint {
|
||||
x: visible_frame.origin.x + visible_frame.size.width / 3.0,
|
||||
y: visible_frame.origin.y,
|
||||
};
|
||||
let size = CGSize {
|
||||
width: visible_frame.size.width / 3.0,
|
||||
height: visible_frame.size.height / 2.0,
|
||||
};
|
||||
let new_frame = CGRect { origin, size };
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
Action::TopRightSixth => {
|
||||
let origin = CGPoint {
|
||||
x: visible_frame.origin.x + visible_frame.size.width * 2.0 / 3.0,
|
||||
y: visible_frame.origin.y,
|
||||
};
|
||||
let size = CGSize {
|
||||
width: visible_frame.size.width / 3.0,
|
||||
height: visible_frame.size.height / 2.0,
|
||||
};
|
||||
let new_frame = CGRect { origin, size };
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
Action::BottomLeftSixth => {
|
||||
let origin = CGPoint {
|
||||
x: visible_frame.origin.x,
|
||||
y: visible_frame.origin.y + visible_frame.size.height / 2.0,
|
||||
};
|
||||
let size = CGSize {
|
||||
width: visible_frame.size.width / 3.0,
|
||||
height: visible_frame.size.height / 2.0,
|
||||
};
|
||||
let new_frame = CGRect { origin, size };
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
Action::BottomCenterSixth => {
|
||||
let origin = CGPoint {
|
||||
x: visible_frame.origin.x + visible_frame.size.width / 3.0,
|
||||
y: visible_frame.origin.y + visible_frame.size.height / 2.0,
|
||||
};
|
||||
let size = CGSize {
|
||||
width: visible_frame.size.width / 3.0,
|
||||
height: visible_frame.size.height / 2.0,
|
||||
};
|
||||
let new_frame = CGRect { origin, size };
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
Action::BottomRightSixth => {
|
||||
let origin = CGPoint {
|
||||
x: visible_frame.origin.x + visible_frame.size.width * 2.0 / 3.0,
|
||||
y: visible_frame.origin.y + visible_frame.size.height / 2.0,
|
||||
};
|
||||
let size = CGSize {
|
||||
width: visible_frame.size.width / 3.0,
|
||||
height: visible_frame.size.height / 2.0,
|
||||
};
|
||||
let new_frame = CGRect { origin, size };
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
Action::TopThird => {
|
||||
let origin = visible_frame.origin;
|
||||
let size = CGSize {
|
||||
width: visible_frame.size.width,
|
||||
height: visible_frame.size.height / 3.0,
|
||||
};
|
||||
let new_frame = CGRect { origin, size };
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
Action::MiddleThird => {
|
||||
let origin = CGPoint {
|
||||
x: visible_frame.origin.x,
|
||||
y: visible_frame.origin.y + visible_frame.size.height / 3.0,
|
||||
};
|
||||
let size = CGSize {
|
||||
width: visible_frame.size.width,
|
||||
height: visible_frame.size.height / 3.0,
|
||||
};
|
||||
let new_frame = CGRect { origin, size };
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
Action::BottomThird => {
|
||||
let origin = CGPoint {
|
||||
x: visible_frame.origin.x,
|
||||
y: visible_frame.origin.y + visible_frame.size.height * 2.0 / 3.0,
|
||||
};
|
||||
let size = CGSize {
|
||||
width: visible_frame.size.width,
|
||||
height: visible_frame.size.height / 3.0,
|
||||
};
|
||||
let new_frame = CGRect { origin, size };
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
Action::Center => {
|
||||
let window_size = frontmost_window_frame.size;
|
||||
let origin = CGPoint {
|
||||
x: visible_frame.origin.x + (visible_frame.size.width - window_size.width) / 2.0,
|
||||
y: visible_frame.origin.y + (visible_frame.size.height - window_size.height) / 2.0,
|
||||
};
|
||||
move_frontmost_window(origin.x, origin.y)
|
||||
}
|
||||
Action::FirstFourth => {
|
||||
let origin = visible_frame.origin;
|
||||
let size = CGSize {
|
||||
width: visible_frame.size.width / 4.0,
|
||||
height: visible_frame.size.height,
|
||||
};
|
||||
let new_frame = CGRect { origin, size };
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
Action::SecondFourth => {
|
||||
let origin = CGPoint {
|
||||
x: visible_frame.origin.x + visible_frame.size.width / 4.0,
|
||||
y: visible_frame.origin.y,
|
||||
};
|
||||
let size = CGSize {
|
||||
width: visible_frame.size.width / 4.0,
|
||||
height: visible_frame.size.height,
|
||||
};
|
||||
let new_frame = CGRect { origin, size };
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
Action::ThirdFourth => {
|
||||
let origin = CGPoint {
|
||||
x: visible_frame.origin.x + visible_frame.size.width * 2.0 / 4.0,
|
||||
y: visible_frame.origin.y,
|
||||
};
|
||||
let size = CGSize {
|
||||
width: visible_frame.size.width / 4.0,
|
||||
height: visible_frame.size.height,
|
||||
};
|
||||
let new_frame = CGRect { origin, size };
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
Action::LastFourth => {
|
||||
let origin = CGPoint {
|
||||
x: visible_frame.origin.x + visible_frame.size.width * 3.0 / 4.0,
|
||||
y: visible_frame.origin.y,
|
||||
};
|
||||
let size = CGSize {
|
||||
width: visible_frame.size.width / 4.0,
|
||||
height: visible_frame.size.height,
|
||||
};
|
||||
let new_frame = CGRect { origin, size };
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
Action::FirstThird => {
|
||||
let origin = CGPoint {
|
||||
x: visible_frame.origin.x,
|
||||
y: visible_frame.origin.y,
|
||||
};
|
||||
let size = CGSize {
|
||||
width: visible_frame.size.width / 3.0,
|
||||
height: visible_frame.size.height,
|
||||
};
|
||||
let new_frame = CGRect { origin, size };
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
Action::CenterThird => {
|
||||
let origin = CGPoint {
|
||||
x: visible_frame.origin.x + visible_frame.size.width / 3.0,
|
||||
y: visible_frame.origin.y,
|
||||
};
|
||||
let size = CGSize {
|
||||
width: visible_frame.size.width / 3.0,
|
||||
height: visible_frame.size.height,
|
||||
};
|
||||
let new_frame = CGRect { origin, size };
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
Action::LastThird => {
|
||||
let origin = CGPoint {
|
||||
x: visible_frame.origin.x + visible_frame.size.width * 2.0 / 3.0,
|
||||
y: visible_frame.origin.y,
|
||||
};
|
||||
let size = CGSize {
|
||||
width: visible_frame.size.width / 3.0,
|
||||
height: visible_frame.size.height,
|
||||
};
|
||||
let new_frame = CGRect { origin, size };
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
Action::FirstTwoThirds => {
|
||||
let origin = CGPoint {
|
||||
x: visible_frame.origin.x,
|
||||
y: visible_frame.origin.y,
|
||||
};
|
||||
let size = CGSize {
|
||||
width: visible_frame.size.width * 2.0 / 3.0,
|
||||
height: visible_frame.size.height,
|
||||
};
|
||||
let new_frame = CGRect { origin, size };
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
Action::CenterTwoThirds => {
|
||||
let origin = CGPoint {
|
||||
x: visible_frame.origin.x + visible_frame.size.width / 6.0,
|
||||
y: visible_frame.origin.y,
|
||||
};
|
||||
let size = CGSize {
|
||||
width: visible_frame.size.width * 2.0 / 3.0,
|
||||
height: visible_frame.size.height,
|
||||
};
|
||||
let new_frame = CGRect { origin, size };
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
Action::LastTwoThirds => {
|
||||
let origin = CGPoint {
|
||||
x: visible_frame.origin.x + visible_frame.size.width / 3.0,
|
||||
y: visible_frame.origin.y,
|
||||
};
|
||||
let size = CGSize {
|
||||
width: visible_frame.size.width * 2.0 / 3.0,
|
||||
height: visible_frame.size.height,
|
||||
};
|
||||
let new_frame = CGRect { origin, size };
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
Action::FirstThreeFourths => {
|
||||
let origin = CGPoint {
|
||||
x: visible_frame.origin.x,
|
||||
y: visible_frame.origin.y,
|
||||
};
|
||||
let size = CGSize {
|
||||
width: visible_frame.size.width * 3.0 / 4.0,
|
||||
height: visible_frame.size.height,
|
||||
};
|
||||
let new_frame = CGRect { origin, size };
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
Action::CenterThreeFourths => {
|
||||
let origin = CGPoint {
|
||||
x: visible_frame.origin.x + visible_frame.size.width / 8.0,
|
||||
y: visible_frame.origin.y,
|
||||
};
|
||||
let size = CGSize {
|
||||
width: visible_frame.size.width * 3.0 / 4.0,
|
||||
height: visible_frame.size.height,
|
||||
};
|
||||
let new_frame = CGRect { origin, size };
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
Action::LastThreeFourths => {
|
||||
let origin = CGPoint {
|
||||
x: visible_frame.origin.x + visible_frame.size.width / 4.0,
|
||||
y: visible_frame.origin.y,
|
||||
};
|
||||
let size = CGSize {
|
||||
width: visible_frame.size.width * 3.0 / 4.0,
|
||||
height: visible_frame.size.height,
|
||||
};
|
||||
let new_frame = CGRect { origin, size };
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
Action::TopThreeFourths => {
|
||||
let origin = CGPoint {
|
||||
x: visible_frame.origin.x,
|
||||
y: visible_frame.origin.y,
|
||||
};
|
||||
let size = CGSize {
|
||||
width: visible_frame.size.width,
|
||||
height: visible_frame.size.height * 3.0 / 4.0,
|
||||
};
|
||||
let new_frame = CGRect { origin, size };
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
Action::BottomThreeFourths => {
|
||||
let origin = CGPoint {
|
||||
x: visible_frame.origin.x,
|
||||
y: visible_frame.origin.y + visible_frame.size.height / 4.0,
|
||||
};
|
||||
let size = CGSize {
|
||||
width: visible_frame.size.width,
|
||||
height: visible_frame.size.height * 3.0 / 4.0,
|
||||
};
|
||||
let new_frame = CGRect { origin, size };
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
Action::TopTwoThirds => {
|
||||
let origin = CGPoint {
|
||||
x: visible_frame.origin.x,
|
||||
y: visible_frame.origin.y,
|
||||
};
|
||||
let size = CGSize {
|
||||
width: visible_frame.size.width,
|
||||
height: visible_frame.size.height * 2.0 / 3.0,
|
||||
};
|
||||
let new_frame = CGRect { origin, size };
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
Action::BottomTwoThirds => {
|
||||
let origin = CGPoint {
|
||||
x: visible_frame.origin.x,
|
||||
y: visible_frame.origin.y + visible_frame.size.height / 3.0,
|
||||
};
|
||||
let size = CGSize {
|
||||
width: visible_frame.size.width,
|
||||
height: visible_frame.size.height * 2.0 / 3.0,
|
||||
};
|
||||
let new_frame = CGRect { origin, size };
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
|
||||
Action::TopCenterTwoThirds => {
|
||||
let origin = CGPoint {
|
||||
x: visible_frame.origin.x + visible_frame.size.width / 6.0,
|
||||
y: visible_frame.origin.y,
|
||||
};
|
||||
let size = CGSize {
|
||||
width: visible_frame.size.width * 2.0 / 3.0,
|
||||
height: visible_frame.size.height * 2.0 / 3.0,
|
||||
};
|
||||
let new_frame = CGRect { origin, size };
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
Action::TopFirstFourth => {
|
||||
let origin = CGPoint {
|
||||
x: visible_frame.origin.x,
|
||||
y: visible_frame.origin.y,
|
||||
};
|
||||
let size = CGSize {
|
||||
width: visible_frame.size.width,
|
||||
height: visible_frame.size.height / 4.0,
|
||||
};
|
||||
let new_frame = CGRect { origin, size };
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
Action::TopSecondFourth => {
|
||||
let origin = CGPoint {
|
||||
x: visible_frame.origin.x,
|
||||
y: visible_frame.origin.y + visible_frame.size.height / 4.0,
|
||||
};
|
||||
let size = CGSize {
|
||||
width: visible_frame.size.width,
|
||||
height: visible_frame.size.height / 4.0,
|
||||
};
|
||||
let new_frame = CGRect { origin, size };
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
Action::TopThirdFourth => {
|
||||
let origin = CGPoint {
|
||||
x: visible_frame.origin.x,
|
||||
y: visible_frame.origin.y + visible_frame.size.height * 2.0 / 4.0,
|
||||
};
|
||||
let size = CGSize {
|
||||
width: visible_frame.size.width,
|
||||
height: visible_frame.size.height / 4.0,
|
||||
};
|
||||
let new_frame = CGRect { origin, size };
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
Action::TopLastFourth => {
|
||||
let origin = CGPoint {
|
||||
x: visible_frame.origin.x,
|
||||
y: visible_frame.origin.y + visible_frame.size.height * 3.0 / 4.0,
|
||||
};
|
||||
let size = CGSize {
|
||||
width: visible_frame.size.width,
|
||||
height: visible_frame.size.height / 4.0,
|
||||
};
|
||||
let new_frame = CGRect { origin, size };
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
Action::MakeLarger => {
|
||||
let window_origin = frontmost_window_frame.origin;
|
||||
let window_size = frontmost_window_frame.size;
|
||||
let delta_width = 20_f64;
|
||||
let delta_height = window_size.height / window_size.width * delta_width;
|
||||
let delta_origin_x = delta_width / 2.0;
|
||||
let delta_origin_y = delta_height / 2.0;
|
||||
|
||||
let new_width = {
|
||||
let possible_value = window_size.width + delta_width;
|
||||
if possible_value > visible_frame.size.width {
|
||||
visible_frame.size.width
|
||||
} else {
|
||||
possible_value
|
||||
}
|
||||
};
|
||||
let new_height = {
|
||||
let possible_value = window_size.height + delta_height;
|
||||
if possible_value > visible_frame.size.height {
|
||||
visible_frame.size.height
|
||||
} else {
|
||||
possible_value
|
||||
}
|
||||
};
|
||||
|
||||
let new_origin_x = {
|
||||
let possible_value = window_origin.x - delta_origin_x;
|
||||
if possible_value < visible_frame.origin.x {
|
||||
visible_frame.origin.x
|
||||
} else {
|
||||
possible_value
|
||||
}
|
||||
};
|
||||
let new_origin_y = {
|
||||
let possible_value = window_origin.y - delta_origin_y;
|
||||
if possible_value < visible_frame.origin.y {
|
||||
visible_frame.origin.y
|
||||
} else {
|
||||
possible_value
|
||||
}
|
||||
};
|
||||
|
||||
let origin = CGPoint {
|
||||
x: new_origin_x,
|
||||
y: new_origin_y,
|
||||
};
|
||||
let size = CGSize {
|
||||
width: new_width,
|
||||
height: new_height,
|
||||
};
|
||||
let new_frame = CGRect { origin, size };
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
Action::MakeSmaller => {
|
||||
let window_origin = frontmost_window_frame.origin;
|
||||
let window_size = frontmost_window_frame.size;
|
||||
|
||||
let delta_width = 20_f64;
|
||||
let delta_height = window_size.height / window_size.width * delta_width;
|
||||
|
||||
let delta_origin_x = delta_width / 2.0;
|
||||
let delta_origin_y = delta_height / 2.0;
|
||||
|
||||
let origin = CGPoint {
|
||||
x: window_origin.x + delta_origin_x,
|
||||
y: window_origin.y + delta_origin_y,
|
||||
};
|
||||
let size = CGSize {
|
||||
width: window_size.width - delta_width,
|
||||
height: window_size.height - delta_height,
|
||||
};
|
||||
let new_frame = CGRect { origin, size };
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
Action::AlmostMaximize => {
|
||||
let new_size = CGSize {
|
||||
width: visible_frame.size.width * 0.9,
|
||||
height: visible_frame.size.height * 0.9,
|
||||
};
|
||||
let new_origin = CGPoint {
|
||||
x: visible_frame.origin.x + (visible_frame.size.width * 0.1),
|
||||
y: visible_frame.origin.y + (visible_frame.size.height * 0.1),
|
||||
};
|
||||
let new_frame = CGRect {
|
||||
origin: new_origin,
|
||||
size: new_size,
|
||||
};
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
Action::Maximize => {
|
||||
let new_frame = CGRect {
|
||||
origin: visible_frame.origin,
|
||||
size: visible_frame.size,
|
||||
};
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
Action::MaximizeWidth => {
|
||||
let window_origin = frontmost_window_frame.origin;
|
||||
let window_size = frontmost_window_frame.size;
|
||||
let origin = CGPoint {
|
||||
x: visible_frame.origin.x,
|
||||
y: window_origin.y,
|
||||
};
|
||||
let size = CGSize {
|
||||
width: visible_frame.size.width,
|
||||
height: window_size.height,
|
||||
};
|
||||
|
||||
let new_frame = CGRect { origin, size };
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
Action::MaximizeHeight => {
|
||||
let window_origin = frontmost_window_frame.origin;
|
||||
let window_size = frontmost_window_frame.size;
|
||||
let origin = CGPoint {
|
||||
x: window_origin.x,
|
||||
y: visible_frame.origin.y,
|
||||
};
|
||||
let size = CGSize {
|
||||
width: window_size.width,
|
||||
height: visible_frame.size.height,
|
||||
};
|
||||
|
||||
let new_frame = CGRect { origin, size };
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
Action::MoveUp => {
|
||||
let window_origin = frontmost_window_frame.origin;
|
||||
let new_y = (window_origin.y - 10.0).max(visible_frame.origin.y);
|
||||
move_frontmost_window(window_origin.x, new_y)
|
||||
}
|
||||
Action::MoveDown => {
|
||||
let window_origin = frontmost_window_frame.origin;
|
||||
let window_size = frontmost_window_frame.size;
|
||||
let new_y = (window_origin.y + 10.0)
|
||||
.min(visible_frame.origin.y + visible_frame.size.height - window_size.height);
|
||||
move_frontmost_window(window_origin.x, new_y)
|
||||
}
|
||||
Action::MoveLeft => {
|
||||
let window_origin = frontmost_window_frame.origin;
|
||||
let new_x = (window_origin.x - 10.0).max(visible_frame.origin.x);
|
||||
move_frontmost_window(new_x, window_origin.y)
|
||||
}
|
||||
Action::MoveRight => {
|
||||
let window_origin = frontmost_window_frame.origin;
|
||||
let window_size = frontmost_window_frame.size;
|
||||
let new_x = (window_origin.x + 10.0)
|
||||
.min(visible_frame.origin.x + visible_frame.size.width - window_size.width);
|
||||
move_frontmost_window(new_x, window_origin.y)
|
||||
}
|
||||
Action::NextDesktop => {
|
||||
let Some(next_workspace_logical_id) = get_next_workspace_logical_id() else {
|
||||
// nothing to do
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
move_frontmost_window_to_workspace(next_workspace_logical_id)
|
||||
}
|
||||
Action::PreviousDesktop => {
|
||||
let Some(previous_workspace_logical_id) = get_previous_workspace_logical_id() else {
|
||||
// nothing to do
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
// Now let's switch the workspace
|
||||
move_frontmost_window_to_workspace(previous_workspace_logical_id)
|
||||
}
|
||||
Action::NextDisplay => {
|
||||
const TOO_MANY_MONITORS: &str = "I don't think you can have so many monitors";
|
||||
|
||||
let frames = list_visible_frame_of_all_screens()?;
|
||||
let n_frames = frames.len();
|
||||
if n_frames == 0 {
|
||||
return Err(Error::NoDisplay);
|
||||
}
|
||||
if n_frames == 1 {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let index = frames
|
||||
.iter()
|
||||
.position(|fr| fr == &visible_frame)
|
||||
.expect("active screen should be in the list");
|
||||
let new_index: usize = {
|
||||
let index_i32: i32 = index.try_into().expect(TOO_MANY_MONITORS);
|
||||
let index_i32_plus_one = index_i32.checked_add(1).expect(TOO_MANY_MONITORS);
|
||||
let final_value = index_i32_plus_one % n_frames as i32;
|
||||
|
||||
final_value
|
||||
.try_into()
|
||||
.expect("final value should be positive")
|
||||
};
|
||||
|
||||
let new_frame = frames[new_index];
|
||||
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
Action::PreviousDisplay => {
|
||||
const TOO_MANY_MONITORS: &str = "I don't think you can have so many monitors";
|
||||
|
||||
let frames = list_visible_frame_of_all_screens()?;
|
||||
let n_frames = frames.len();
|
||||
if n_frames == 0 {
|
||||
return Err(Error::NoDisplay);
|
||||
}
|
||||
if n_frames == 1 {
|
||||
return Ok(());
|
||||
}
|
||||
let index = frames
|
||||
.iter()
|
||||
.position(|fr| fr == &visible_frame)
|
||||
.expect("active screen should be in the list");
|
||||
let new_index: usize = {
|
||||
let index_i32: i32 = index.try_into().expect(TOO_MANY_MONITORS);
|
||||
let index_i32_minus_one = index_i32 - 1;
|
||||
let n_frames_i32: i32 = n_frames.try_into().expect(TOO_MANY_MONITORS);
|
||||
let final_value = (index_i32_minus_one + n_frames_i32) % n_frames_i32;
|
||||
|
||||
final_value
|
||||
.try_into()
|
||||
.expect("final value should be positive")
|
||||
};
|
||||
|
||||
let new_frame = frames[new_index];
|
||||
|
||||
set_frontmost_window_frame(new_frame)
|
||||
}
|
||||
Action::Restore => {
|
||||
let Some(previous_frame) = get_frontmost_window_last_frame(frontmost_window_id) else {
|
||||
// Previous frame found, Nothing to do
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
set_frontmost_window_frame(previous_frame)
|
||||
}
|
||||
Action::ToggleFullscreen => toggle_fullscreen(),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn set_up_commands_hotkeys(
|
||||
tauri_app_handle: &AppHandle,
|
||||
wm_extension: &Extension,
|
||||
) -> Result<(), String> {
|
||||
for command in wm_extension
|
||||
.commands
|
||||
.as_ref()
|
||||
.expect("Window Management extension has commands")
|
||||
.iter()
|
||||
.filter(|cmd| cmd.enabled)
|
||||
{
|
||||
if let Some(ref hotkey) = command.hotkey {
|
||||
let on_opened = on_opened::on_opened(&command.id);
|
||||
|
||||
let extension_id_clone = command.id.clone();
|
||||
|
||||
tauri_app_handle
|
||||
.global_shortcut()
|
||||
.on_shortcut(hotkey.as_str(), move |tauri_app_handle, _hotkey, event| {
|
||||
let on_opened_clone = on_opened.clone();
|
||||
let extension_id_clone = extension_id_clone.clone();
|
||||
let app_handle_clone = tauri_app_handle.clone();
|
||||
|
||||
if event.state() == ShortcutState::Pressed {
|
||||
async_runtime::spawn(async move {
|
||||
let result = open(app_handle_clone, on_opened_clone, None).await;
|
||||
if let Err(msg) = result {
|
||||
log::warn!(
|
||||
"failed to open extension [{}], error [{}]",
|
||||
extension_id_clone,
|
||||
msg
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn unset_commands_hotkeys(
|
||||
tauri_app_handle: &AppHandle,
|
||||
wm_extension: &Extension,
|
||||
) -> Result<(), String> {
|
||||
for command in wm_extension
|
||||
.commands
|
||||
.as_ref()
|
||||
.expect("Window Management extension has commands")
|
||||
.iter()
|
||||
.filter(|cmd| cmd.enabled)
|
||||
{
|
||||
if let Some(ref hotkey) = command.hotkey {
|
||||
tauri_app_handle
|
||||
.global_shortcut()
|
||||
.unregister(hotkey.as_str())
|
||||
.map_err(|e| e.to_string())?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn set_up_command_hotkey(
|
||||
tauri_app_handle: &AppHandle,
|
||||
wm_extension: &Extension,
|
||||
command_id: &str,
|
||||
) -> Result<(), String> {
|
||||
let commands = wm_extension
|
||||
.commands
|
||||
.as_ref()
|
||||
.expect("Window Management has commands");
|
||||
let opt_command = commands.iter().find(|ext| ext.id == command_id);
|
||||
|
||||
let Some(command) = opt_command else {
|
||||
panic!("Window Management command does not exist {}", command_id);
|
||||
};
|
||||
|
||||
if let Some(ref hotkey) = command.hotkey {
|
||||
let on_opened = on_opened::on_opened(&command.id);
|
||||
|
||||
let extension_id_clone = command.id.clone();
|
||||
|
||||
tauri_app_handle
|
||||
.global_shortcut()
|
||||
.on_shortcut(hotkey.as_str(), move |tauri_app_handle, _hotkey, event| {
|
||||
let on_opened_clone = on_opened.clone();
|
||||
let extension_id_clone = extension_id_clone.clone();
|
||||
let app_handle_clone = tauri_app_handle.clone();
|
||||
|
||||
if event.state() == ShortcutState::Pressed {
|
||||
async_runtime::spawn(async move {
|
||||
let result = open(app_handle_clone, on_opened_clone, None).await;
|
||||
if let Err(msg) = result {
|
||||
log::warn!(
|
||||
"failed to open extension [{}], error [{}]",
|
||||
extension_id_clone,
|
||||
msg
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn unset_command_hotkey(
|
||||
tauri_app_handle: &AppHandle,
|
||||
wm_extension: &Extension,
|
||||
command_id: &str,
|
||||
) -> Result<(), String> {
|
||||
let commands = wm_extension
|
||||
.commands
|
||||
.as_ref()
|
||||
.expect("Window Management has commands");
|
||||
let opt_command = commands.iter().find(|ext| ext.id == command_id);
|
||||
|
||||
let Some(command) = opt_command else {
|
||||
panic!("Window Management command does not exist {}", command_id);
|
||||
};
|
||||
|
||||
if let Some(ref hotkey) = command.hotkey {
|
||||
tauri_app_handle
|
||||
.global_shortcut()
|
||||
.unregister(hotkey.as_str())
|
||||
.map_err(|e| e.to_string())?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn register_command_hotkey(
|
||||
tauri_app_handle: &AppHandle,
|
||||
command_id: &str,
|
||||
hotkey: &str,
|
||||
) -> Result<(), String> {
|
||||
let on_opened = on_opened::on_opened(&command_id);
|
||||
|
||||
let extension_id_clone = command_id.to_string();
|
||||
|
||||
tauri_app_handle
|
||||
.global_shortcut()
|
||||
.on_shortcut(hotkey, move |tauri_app_handle, _hotkey, event| {
|
||||
let on_opened_clone = on_opened.clone();
|
||||
let extension_id_clone = extension_id_clone.clone();
|
||||
let app_handle_clone = tauri_app_handle.clone();
|
||||
|
||||
if event.state() == ShortcutState::Pressed {
|
||||
async_runtime::spawn(async move {
|
||||
let result = open(app_handle_clone, on_opened_clone, None).await;
|
||||
if let Err(msg) = result {
|
||||
log::warn!(
|
||||
"failed to open extension [{}], error [{}]",
|
||||
extension_id_clone,
|
||||
msg
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn unregister_command_hotkey(
|
||||
tauri_app_handle: &AppHandle,
|
||||
wm_extension: &Extension,
|
||||
command_id: &str,
|
||||
) -> Result<(), String> {
|
||||
let commands = wm_extension
|
||||
.commands
|
||||
.as_ref()
|
||||
.expect("Window Management has commands");
|
||||
let opt_command = commands.iter().find(|ext| ext.id == command_id);
|
||||
|
||||
let Some(command) = opt_command else {
|
||||
panic!("Window Management command does not exist {}", command_id);
|
||||
};
|
||||
|
||||
let Some(ref hotkey) = command.hotkey else {
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
tauri_app_handle
|
||||
.global_shortcut()
|
||||
.unregister(hotkey.as_str())
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
use super::actions::Action;
|
||||
use crate::common::document::OnOpened;
|
||||
use serde_plain;
|
||||
|
||||
pub(crate) fn on_opened(command_id: &str) -> OnOpened {
|
||||
let action: Action = serde_plain::from_str(command_id).unwrap_or_else(|_| {
|
||||
panic!("Window Management commands IDs should be valid for `enum Action`, someone corrupts the JSON file");
|
||||
});
|
||||
OnOpened::WindowManagementAction { action }
|
||||
}
|
||||
415
src-tauri/src/extension/built_in/window_management/plugin.json
Normal file
@@ -0,0 +1,415 @@
|
||||
{
|
||||
"id": "Window Management",
|
||||
"name": "Window Management",
|
||||
"platforms": [
|
||||
"macos"
|
||||
],
|
||||
"description": "Resize, reorganize and move your focused window effortlessly",
|
||||
"icon": "font_a-Windowmanagement",
|
||||
"type": "extension",
|
||||
"category": "Utilities",
|
||||
"tags": [
|
||||
"Productivity"
|
||||
],
|
||||
"commands": [
|
||||
{
|
||||
"id": "TopHalf",
|
||||
"name": "Top Half",
|
||||
"description": "Move the focused window to fill left half of the screen.",
|
||||
"icon": "font_a-TopHalf",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "BottomHalf",
|
||||
"name": "Bottom Half",
|
||||
"description": "Move the focused window to fill bottom half of the screen.",
|
||||
"icon": "font_a-BottomHalf",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "LeftHalf",
|
||||
"name": "Left Half",
|
||||
"description": "Move the focused window to fill left half of the screen.",
|
||||
"icon": "font_a-LeftHalf",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "RightHalf",
|
||||
"name": "Right Half",
|
||||
"description": "Move the focused window to fill right half of the screen.",
|
||||
"icon": "font_a-RightHalf",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "CenterHalf",
|
||||
"name": "Center Half",
|
||||
"description": "Move the focused window to fill center half of the screen.",
|
||||
"icon": "font_a-CenterHalf",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "Maximize",
|
||||
"name": "Maximize",
|
||||
"description": "Maximize the focused window to fit the screen.",
|
||||
"icon": "font_Maximize",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "TopLeftQuarter",
|
||||
"name": "Top Left Quarter",
|
||||
"description": "Resize the focused window to the top left quarter of the screen.",
|
||||
"icon": "font_a-TopLeftQuarter",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "TopRightQuarter",
|
||||
"name": "Top Right Quarter",
|
||||
"description": "Resize the focused window to the top right quarter of the screen.",
|
||||
"icon": "font_a-TopRightQuarter",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "BottomLeftQuarter",
|
||||
"name": "Bottom Left Quarter",
|
||||
"description": "Resize the focused window to the bottom left quarter of the screen.",
|
||||
"icon": "font_a-BottomLeftQuarter",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "BottomRightQuarter",
|
||||
"name": "Bottom Right Quarter",
|
||||
"description": "Resize the focused window to the bottom right quarter of the screen.",
|
||||
"icon": "font_a-BottomRightQuarter",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "TopLeftSixth",
|
||||
"name": "Top Left Sixth",
|
||||
"description": "Resize the focused window to the top left sixth of the screen.",
|
||||
"icon": "font_a-TopLeftSixth",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "TopCenterSixth",
|
||||
"name": "Top Center Sixth",
|
||||
"description": "Resize the focused window to the top center sixth of the screen.",
|
||||
"icon": "font_a-TopCenterSixth",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "TopRightSixth",
|
||||
"name": "Top Right Sixth",
|
||||
"description": "Resize the focused window to the top right sixth of the screen.",
|
||||
"icon": "font_a-TopRightSixth",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "BottomLeftSixth",
|
||||
"name": "Bottom Left Sixth",
|
||||
"description": "Resize the focused window to the bottom left sixth of the screen.",
|
||||
"icon": "font_a-BottomLeftSixth",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "BottomCenterSixth",
|
||||
"name": "Bottom Center Sixth",
|
||||
"description": "Resize the focused window to the bottom center sixth of the screen.",
|
||||
"icon": "font_a-BottomCenterSixth",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "BottomRightSixth",
|
||||
"name": "Bottom Right Sixth",
|
||||
"description": "Resize the focused window to the bottom right sixth of the screen.",
|
||||
"icon": "font_a-BottomRightSixth",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "TopThird",
|
||||
"name": "Top Third",
|
||||
"description": "Resize the focused window to the top third of the screen.",
|
||||
"icon": "font_a-TopThirdFourth",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "MiddleThird",
|
||||
"name": "Middle Third",
|
||||
"description": "Resize the focused window to the middle third of the screen.",
|
||||
"icon": "font_a-MiddleThird",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "BottomThird",
|
||||
"name": "Bottom Third",
|
||||
"description": "Resize the focused window to the bottom third of the screen.",
|
||||
"icon": "font_a-BottomThird",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "Center",
|
||||
"name": "Center",
|
||||
"description": "Center the focused window in the screen.",
|
||||
"icon": "font_Center",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "FirstFourth",
|
||||
"name": "First Fourth",
|
||||
"description": "Resize the focused window to the first fourth of the screen.",
|
||||
"icon": "font_a-FirstFourth",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "SecondFourth",
|
||||
"name": "Second Fourth",
|
||||
"description": "Resize the focused window to the second fourth of the screen.",
|
||||
"icon": "font_a-SecondFourth",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "ThirdFourth",
|
||||
"name": "Third Fourth",
|
||||
"description": "Resize the focused window to the third fourth of the screen.",
|
||||
"icon": "font_a-ThirdFourth",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "LastFourth",
|
||||
"name": "Last Fourth",
|
||||
"description": "Resize the focused window to the last fourth of the screen.",
|
||||
"icon": "font_a-LastFourth",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "FirstThird",
|
||||
"name": "First Third",
|
||||
"description": "Resize the focused window to the first third of the screen.",
|
||||
"icon": "font_a-FirstThird",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "CenterThird",
|
||||
"name": "Center Third",
|
||||
"description": "Resize the focused window to the center third of the screen.",
|
||||
"icon": "font_a-CenterThird",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "LastThird",
|
||||
"name": "Last Third",
|
||||
"description": "Resize the focused window to the last third of the screen.",
|
||||
"icon": "font_a-LastThird",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "FirstTwoThirds",
|
||||
"name": "First Two Thirds",
|
||||
"description": "Resize the focused window to the first two thirds of the screen.",
|
||||
"icon": "font_a-FirstTwoThirds",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "CenterTwoThirds",
|
||||
"name": "Center Two Thirds",
|
||||
"description": "Resize the focused window to the center two thirds of the screen.",
|
||||
"icon": "font_a-CenterTwoThirds",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "LastTwoThirds",
|
||||
"name": "Last Two Thirds",
|
||||
"description": "Resize the focused window to the last two thirds of the screen.",
|
||||
"icon": "font_a-LastTwoThirds",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "FirstThreeFourths",
|
||||
"name": "First Three Fourths",
|
||||
"description": "Resize the focused window to the first three fourths of the screen.",
|
||||
"icon": "font_a-FirstThreeFourths",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "CenterThreeFourths",
|
||||
"name": "Center Three Fourths",
|
||||
"description": "Resize the focused window to the center three fourths of the screen.",
|
||||
"icon": "font_a-CenterThreeFourths",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "LastThreeFourths",
|
||||
"name": "Last Three Fourths",
|
||||
"description": "Resize the focused window to the last three fourths of the screen.",
|
||||
"icon": "font_a-LastThreeFourths",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "TopThreeFourths",
|
||||
"name": "Top Three Fourths",
|
||||
"description": "Resize the focused window to the top three fourths of the screen.",
|
||||
"icon": "font_a-TopThreeFourths",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "BottomThreeFourths",
|
||||
"name": "Bottom Three Fourths",
|
||||
"description": "Resize the focused window to the bottom three fourths of the screen.",
|
||||
"icon": "font_a-BottomThreeFourths",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "TopTwoThirds",
|
||||
"name": "Top Two Thirds",
|
||||
"description": "Resize the focused window to the top two thirds of the screen.",
|
||||
"icon": "font_a-TopTwoThirds",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "BottomTwoThirds",
|
||||
"name": "Bottom Two Thirds",
|
||||
"description": "Resize the focused window to the bottom two thirds of the screen.",
|
||||
"icon": "font_a-BottomTwoThirds",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "TopCenterTwoThirds",
|
||||
"name": "Top Center Two Thirds",
|
||||
"description": "Resize the focused window to the top center two thirds of the screen.",
|
||||
"icon": "font_a-TopCenterTwoThirds",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "TopFirstFourth",
|
||||
"name": "Top First Fourth",
|
||||
"description": "Resize the focused window to the top first fourth of the screen.",
|
||||
"icon": "font_a-TopFirstFourth",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "TopSecondFourth",
|
||||
"name": "Top Second Fourth",
|
||||
"description": "Resize the focused window to the top second fourth of the screen.",
|
||||
"icon": "font_a-TopSecondFourth",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "TopThirdFourth",
|
||||
"name": "Top Third Fourth",
|
||||
"description": "Resize the focused window to the top third fourth of the screen.",
|
||||
"icon": "font_a-TopThirdFourth",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "TopLastFourth",
|
||||
"name": "Top Last Fourth",
|
||||
"description": "Resize the focused window to the top last fourth of the screen.",
|
||||
"icon": "font_a-TopLastFourth",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "MakeLarger",
|
||||
"name": "Make Larger",
|
||||
"description": "Increase the focused window until it reaches the screen size.",
|
||||
"icon": "font_a-MakeLarger",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "MakeSmaller",
|
||||
"name": "Make Smaller",
|
||||
"description": "Decrease the focused window until it reaches its minimal size.",
|
||||
"icon": "font_a-MakeSmaller",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "AlmostMaximize",
|
||||
"name": "Almost Maximize",
|
||||
"description": "Maximize the focused window to almost fit the screen.",
|
||||
"icon": "font_a-AlmostMaximize",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "MaximizeWidth",
|
||||
"name": "Maximize Width",
|
||||
"description": "Maximize width of the focused window to fit the screen.",
|
||||
"icon": "font_a-MaximizeWidth",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "MaximizeHeight",
|
||||
"name": "Maximize Height",
|
||||
"description": "Maximize height of the focused window to fit the screen.",
|
||||
"icon": "font_a-MaximizeHeight",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "MoveUp",
|
||||
"name": "Move Up",
|
||||
"description": "Move the focused window to the top edge of the screen.",
|
||||
"icon": "font_a-MoveUp",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "MoveDown",
|
||||
"name": "Move Down",
|
||||
"description": "Move the focused window to the bottom of the screen.",
|
||||
"icon": "font_a-MoveDown",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "MoveLeft",
|
||||
"name": "Move Left",
|
||||
"description": "Move the focused window to the left edge of the screen.",
|
||||
"icon": "font_a-MoveLeft",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "MoveRight",
|
||||
"name": "Move Right",
|
||||
"description": "Move the focused window to the right edge of the screen.",
|
||||
"icon": "font_a-MoveRight",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "NextDesktop",
|
||||
"name": "Next Desktop",
|
||||
"description": "Move the focused window to the next desktop.",
|
||||
"icon": "font_a-NextDesktop",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "PreviousDesktop",
|
||||
"name": "Previous Desktop",
|
||||
"description": "Move the focused window to the previous desktop.",
|
||||
"icon": "font_a-PreviousDesktop",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "NextDisplay",
|
||||
"name": "Next Display",
|
||||
"description": "Move the focused window to the next display.",
|
||||
"icon": "font_a-NextDisplay",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "PreviousDisplay",
|
||||
"name": "Previous Display",
|
||||
"description": "Move the focused window to the previous display.",
|
||||
"icon": "font_a-PreviousDisplay",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "Restore",
|
||||
"name": "Restore",
|
||||
"description": "Restore the focused window to its last position.",
|
||||
"icon": "font_Restore",
|
||||
"type": "command"
|
||||
},
|
||||
{
|
||||
"id": "ToggleFullscreen",
|
||||
"name": "Toggle Fullscreen",
|
||||
"description": "Toggle fullscreen mode.",
|
||||
"icon": "font_a-ToggleFullscreen",
|
||||
"type": "command"
|
||||
}
|
||||
]
|
||||
}
|
||||