mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-24 09:48:28 +02:00
Custom font selection (#226)
This commit is contained in:
16
src-tauri/yaak-fonts/Cargo.toml
Normal file
16
src-tauri/yaak-fonts/Cargo.toml
Normal file
@@ -0,0 +1,16 @@
|
||||
[package]
|
||||
name = "yaak-fonts"
|
||||
links = "yaak-fonts"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
publish = false
|
||||
|
||||
[dependencies]
|
||||
font-loader = "0.11.0"
|
||||
tauri = { workspace = true }
|
||||
ts-rs = { workspace = true }
|
||||
serde = "1.0"
|
||||
thiserror = { workspace = true }
|
||||
|
||||
[build-dependencies]
|
||||
tauri-plugin = { workspace = true, features = ["build"] }
|
||||
3
src-tauri/yaak-fonts/bindings/gen_fonts.ts
Normal file
3
src-tauri/yaak-fonts/bindings/gen_fonts.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
|
||||
|
||||
export type Fonts = { editorFonts: Array<string>, uiFonts: Array<string>, };
|
||||
5
src-tauri/yaak-fonts/build.rs
Normal file
5
src-tauri/yaak-fonts/build.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
const COMMANDS: &[&str] = &["list"];
|
||||
|
||||
fn main() {
|
||||
tauri_plugin::Builder::new(COMMANDS).build();
|
||||
}
|
||||
14
src-tauri/yaak-fonts/index.ts
Normal file
14
src-tauri/yaak-fonts/index.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { invoke } from '@tauri-apps/api/core';
|
||||
import { Fonts } from './bindings/gen_fonts';
|
||||
|
||||
export async function listFonts() {
|
||||
return invoke<Fonts>('plugin:yaak-fonts|list', {});
|
||||
}
|
||||
|
||||
export function useFonts() {
|
||||
return useQuery({
|
||||
queryKey: ['list_fonts'],
|
||||
queryFn: () => listFonts(),
|
||||
});
|
||||
}
|
||||
6
src-tauri/yaak-fonts/package.json
Normal file
6
src-tauri/yaak-fonts/package.json
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"name": "@yaakapp-internal/fonts",
|
||||
"private": true,
|
||||
"version": "1.0.0",
|
||||
"main": "index.ts"
|
||||
}
|
||||
3
src-tauri/yaak-fonts/permissions/default.toml
Normal file
3
src-tauri/yaak-fonts/permissions/default.toml
Normal file
@@ -0,0 +1,3 @@
|
||||
[default]
|
||||
description = "Default permissions for the plugin"
|
||||
permissions = ["allow-list"]
|
||||
41
src-tauri/yaak-fonts/src/commands.rs
Normal file
41
src-tauri/yaak-fonts/src/commands.rs
Normal file
@@ -0,0 +1,41 @@
|
||||
use crate::Result;
|
||||
use font_loader::system_fonts;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashSet;
|
||||
use tauri::command;
|
||||
use ts_rs::TS;
|
||||
|
||||
#[derive(Default, Debug, Clone, Serialize, Deserialize, TS, PartialEq)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
#[ts(export, export_to = "gen_fonts.ts")]
|
||||
pub struct Fonts {
|
||||
pub editor_fonts: Vec<String>,
|
||||
pub ui_fonts: Vec<String>,
|
||||
}
|
||||
|
||||
#[command]
|
||||
pub(crate) async fn list() -> Result<Fonts> {
|
||||
let mut ui_fonts = HashSet::new();
|
||||
let mut editor_fonts = HashSet::new();
|
||||
|
||||
let mut property = system_fonts::FontPropertyBuilder::new().monospace().build();
|
||||
for font in &system_fonts::query_specific(&mut property) {
|
||||
editor_fonts.insert(font.to_string());
|
||||
}
|
||||
for font in &system_fonts::query_all() {
|
||||
if !editor_fonts.contains(font) {
|
||||
ui_fonts.insert(font.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
let mut ui_fonts: Vec<String> = ui_fonts.into_iter().collect();
|
||||
let mut editor_fonts: Vec<String> = editor_fonts.into_iter().collect();
|
||||
|
||||
ui_fonts.sort();
|
||||
editor_fonts.sort();
|
||||
|
||||
Ok(Fonts {
|
||||
ui_fonts,
|
||||
editor_fonts,
|
||||
})
|
||||
}
|
||||
15
src-tauri/yaak-fonts/src/error.rs
Normal file
15
src-tauri/yaak-fonts/src/error.rs
Normal file
@@ -0,0 +1,15 @@
|
||||
use serde::{ser::Serializer, Serialize};
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum Error {}
|
||||
|
||||
impl Serialize for Error {
|
||||
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
serializer.serialize_str(self.to_string().as_ref())
|
||||
}
|
||||
}
|
||||
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
15
src-tauri/yaak-fonts/src/lib.rs
Normal file
15
src-tauri/yaak-fonts/src/lib.rs
Normal file
@@ -0,0 +1,15 @@
|
||||
use tauri::{
|
||||
generate_handler,
|
||||
plugin::{Builder, TauriPlugin},
|
||||
Runtime,
|
||||
};
|
||||
|
||||
mod commands;
|
||||
mod error;
|
||||
|
||||
use crate::commands::list;
|
||||
pub use error::{Error, Result};
|
||||
|
||||
pub fn init<R: Runtime>() -> TauriPlugin<R> {
|
||||
Builder::new("yaak-fonts").invoke_handler(generate_handler![list]).build()
|
||||
}
|
||||
Reference in New Issue
Block a user