Skip to content

Commit

Permalink
Merge branch 'feat/api'
Browse files Browse the repository at this point in the history
  • Loading branch information
0xzio committed Jun 28, 2024
2 parents 0dada6e + bf7400c commit 424a7c3
Show file tree
Hide file tree
Showing 15 changed files with 299 additions and 159 deletions.
20 changes: 17 additions & 3 deletions apps/desktop/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions apps/desktop/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ tauri-build = { version = "2.0.0-beta", features = [] }
[dependencies]
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
tauri = { version = "2.0.0-beta.22", features = ["devtools"] }
tauri = { version = "2.0.0-beta.22", features = ["devtools", "image-png", "tray-icon"] }

actix-web = "4"
actix-cors = "0.6.1"
Expand Down Expand Up @@ -47,7 +47,6 @@ tauri-plugin-jarvis = { path = "../../../packages/tauri-plugin-jarvis" }
tauri-plugin-dialog = "2.0.0-beta.9"
tauri-plugin-notification = "2.0.0-beta.8"
tauri-plugin-shellx = "2.0.7"

winapi = { version = "0.3", features = ["errhandlingapi"] }


Expand Down
9 changes: 9 additions & 0 deletions apps/desktop/src-tauri/capabilities/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,16 @@
"resources:default",
"image:default",
"menu:default",
"menu:allow-new",
"menu:allow-items",
"tray:default",
"tray:allow-new",
"tray:allow-set-icon",
"tray:allow-set-menu",
"tray:allow-set-title",
"tray:allow-set-tooltip",
"tray:allow-set-visible",
"tray:allow-set-show-menu-on-left-click",
"fs:default",
"fs:read-all",
"fs:write-all",
Expand Down
5 changes: 3 additions & 2 deletions apps/desktop/src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod hello;
mod menu;
mod server;
mod util;
mod tray;

use rusqlite::{Connection, ParamsFromIter, Result, ToSql};
use std::{path::PathBuf, thread};
Expand Down Expand Up @@ -113,10 +114,10 @@ pub fn run() {
open_command,
handle_input
])
.setup(|mut app| {
.setup(|app| {
let handle = app.handle();
let conn = Connection::open_in_memory();

tray::create_tray(handle)?;
// let boxed_handle = Box::new(handle);
let boxed_conn = Box::new(conn.unwrap());

Expand Down
45 changes: 45 additions & 0 deletions apps/desktop/src-tauri/src/tray.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2019-2024 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

// pub use tauri_macros::;
use tauri::{
menu::{Menu, MenuItem},
tray::TrayIconBuilder,
Manager, Runtime,
};

pub fn create_tray<R: Runtime>(app: &tauri::AppHandle<R>) -> tauri::Result<()> {
let toggle_i = MenuItem::with_id(app, "toggle", "Toggle", true, None::<&str>)?;
let quit_i = MenuItem::with_id(app, "quit", "Quit", true, None::<&str>)?;
let menu1 = Menu::with_items(app, &[&toggle_i, &quit_i])?;
let _ = TrayIconBuilder::with_id("tray-1")
.tooltip("Tauri")
.icon(app.default_window_icon().unwrap().clone())
.menu(&menu1)
.menu_on_left_click(true)
.on_menu_event(move |app, event| match event.id.as_ref() {
"quit" => {
app.exit(0);
}
"toggle" => {
if let Some(window) = app.get_webview_window("main") {
let visible = window.is_visible().unwrap();
println!("visible: {}", visible);
let new_title = if window.is_visible().unwrap_or_default() {
let _ = window.hide();
"Show"
} else {
let _ = window.show();
let _ = window.set_focus();
"Hide"
};
toggle_i.set_text(new_title).unwrap();
}
}
_ => {}
})
.build(app);

Ok(())
}
11 changes: 0 additions & 11 deletions apps/desktop/src/components/CommandPalette/CommandPalette.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useEffect } from 'react'
import { Box } from '@fower/react'
import { clipboard } from '@penxio/api/native'
import { Spinner } from 'uikit'
import { Command } from '@penx/model'
import { store } from '@penx/store'
Expand All @@ -26,16 +25,6 @@ export const CommandPalette = () => {
const { value, setValue } = useValue()

const { developingItems, commandItems, applicationItems } = useItems()
useEffect(() => {
clipboard.readText().then(console.log)
}, [])
// console.log('========items:', items)

// console.log(
// '=========developingItems, commandItems:',
// developingItems,
// commandItems,
// )

const { isRoot, isCommandApp } = useCommandPosition()
const { currentCommand } = useCurrentCommand()
Expand Down
4 changes: 2 additions & 2 deletions apps/docs/pages/api-reference/_meta.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"clipboard": "clipboard",
"clipboard": "Clipboard",
"http": "Http",
"runAppleScript": "runAppleScript"
"fs": "File System"
}
38 changes: 36 additions & 2 deletions apps/docs/pages/api-reference/clipboard.mdx
Original file line number Diff line number Diff line change
@@ -1,3 +1,37 @@
# clipboard
# Clipboard

wip...
Access clipboard text/image/files/HTML/RTF.

## Sample Code

```ts
import { clipboard } from '@penxio/api'

await clipboard.writeText('Hello, PenX!')
const text = await clipboard.readText()

const imageBase64 = await clipboard.readImageBase64()
```

## APIs

https://penx-api.penx.io/interfaces/IClipboard.html

- `hasFiles`
- `hasHTML`
- `hasImage`
- `hasRTF`
- `hasText`
- `readFiles`
- `readHtml`
- `readImageBase64`
- `readImageBinary`
- `readRtf`
- `readText`
- `writeFiles`
- `writeHtml`
- `writeHtmlAndText`
- `writeImageBase64`
- `writeImageBinary`
- `writeRtf`
- `writeText`
25 changes: 25 additions & 0 deletions apps/docs/pages/api-reference/dialog.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Dialog

Dialog API provides functions to interact with user, get user input, and more.

https://penx-api.penx.io/interfaces/IDialog.html


```ts
import { dialog } from '@penxio/api'

const name = await dialog.ask("What's your name?")
const confirmed = await dialog.confirm('Are you sure you want to delete this file?')

// Ask for save path
const savePath = await dialog.save()
console.log(savePath);
```

## APIs

- `ask`
- `confirm`
- `message`
- `open`
- `save`
34 changes: 34 additions & 0 deletions apps/docs/pages/api-reference/fs.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# File System

https://penx-api.penx.io/interfaces/IFs.html

```ts
import { fs } from '@penxio/api'

const fileContent = await fs.readFile('/home/penx/package.json')

const fileExists = await fs.exists('/home/penx/package.json')

await fs.rename('/home/a.txt', '/home/q.txt')

await fs.writeTextFile('/home/b.txt', 'Hello from PenX!')

await fs.copyFile('/home/b.txt', '/home/c.txt')
```

## APIs

- `copyFile`
- `create`
- `exists`
- `lstat`
- `mkdir`
- `readDir`
- `readFile`
- `readTextFile`
- `remove`
- `rename`
- `stat`
- `truncate`
- `writeFile`
- `writeTextFile`
71 changes: 12 additions & 59 deletions apps/docs/pages/api-reference/http.mdx
Original file line number Diff line number Diff line change
@@ -1,68 +1,21 @@
# HTTP

Sending HTTP Requests in Command App.
Sending HTTP requests and bypass CORS restrictions.

## Using `fetch`
https://penx-api.penx.io/functions/fetch.html

In most cases, you can use web standarn API [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API). And this is the recommanded way to send a HTTP request.

```js
import { MarkdownBuilder, render } from '@penxio/api'

export async function main() {
const res = await fetch('https://jsonplaceholder.typicode.com/todos/1').then(
(res) => res.json(),
)

const md = new MarkdownBuilder(JSON.stringify(res, null, 2))
render(md)
}
```

## Using `request`

If you encounter a CORS error, you can use the `request` API instead.

![cors](/images/cors-error.webp)

### GET

To send a GET request:
> Sending request from browser may be restricted by CORS policy. You can use the provided `fetch` API to bypass this restriction.
>
> The `fetch` API works just like a regular `fetch` function provided in browser.
>
> If you don't need to bypass CORS, you can use the regular `fetch` function provided by the browser, or other libraries like `axios`.
```ts
import { MarkdownBuilder, request } from '@penxio/api'

export async function main() {
const res = await request({
method: 'GET',
url: 'https://jsonplaceholder.typicode.com/todos/1',
})

const md = new MarkdownBuilder(JSON.stringify(res.data, null, 2))
render(md)
}
```

### POST

To send a POST request:

```ts
import { MarkdownBuilder, request } from '@penxio/api'

export async function main() {
const res = await request({
method: 'POST',
url: 'https://jsonplaceholder.typicode.com/psots',
json: {
title: 'This is a title',
content: 'foo...',
},
})
import { fetch } from '@penxio/api'

const md = new MarkdownBuilder(JSON.stringify(res.data, null, 2))
render(md)
}
fetch('https://jsonplaceholder.typicode.com/todos/1').then(console.log)
fetch('https://jsonplaceholder.typicode.com/posts', {
method: 'POST',
}).then(console.log)
```

Of course, You can other methods like: PUT, PATCH, DELETE..
14 changes: 14 additions & 0 deletions apps/docs/pages/api-reference/notification.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Notification

Display system notification.

https://penx-api.penx.io/interfaces/INotification.html

```ts
import { notification } from '@penxio/api'

const permissionGranted = await notification.isPermissionGranted()
if (permissionGranted) {
await notification.sendNotification('Hello from Penx')
}
```
Loading

0 comments on commit 424a7c3

Please sign in to comment.