forked from slint-ui/slint
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
130 lines (102 loc) · 3.78 KB
/
main.rs
File metadata and controls
130 lines (102 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: MIT
#![deny(unsafe_code)]
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;
slint::include_modules!();
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
pub fn load_font_from_bytes(font_data: js_sys::Uint8Array, locale: &str) -> Result<(), JsValue> {
use slint::fontique_08::fontique;
let font_data = font_data.to_vec();
let blob = fontique::Blob::new(std::sync::Arc::new(font_data));
let mut collection = slint::fontique_08::shared_collection();
let fonts = collection.register_fonts(blob, None);
scripts_for_locale(locale, |script| {
collection
.append_fallbacks(fontique::FallbackKey::new(*script, None), fonts.iter().map(|x| x.0));
});
Ok(())
}
#[cfg(target_arch = "wasm32")]
fn scripts_for_locale(
locale: &str,
mut callback: impl FnMut(&slint::fontique_08::fontique::Script),
) {
use slint::fontique_08::fontique;
let Ok(locale) = icu_locale_core::Locale::try_from_str(locale) else {
return;
};
let scripts: &[fontique::Script] = match locale.id.language.as_str() {
"ja" => &[
fontique::Script::from_str_unchecked("Hira"),
fontique::Script::from_str_unchecked("Kana"),
fontique::Script::from_str_unchecked("Hani"),
],
"ko" => &[
fontique::Script::from_str_unchecked("Hang"),
fontique::Script::from_str_unchecked("Hani"),
],
"zh" => &[fontique::Script::from_str_unchecked("Hani")],
_ => {
if let Some(script) = locale.id.script {
&[fontique::Script::from_bytes(script.into_raw())]
} else {
&[]
}
}
};
for script in scripts {
callback(script);
}
}
use std::rc::Rc;
use slint::{Model, ModelExt, ModelRc, SharedString, StandardListViewItem, VecModel};
#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
pub fn main() {
// This provides better error messages in debug mode.
// It's disabled in release mode so it doesn't bloat up the file size.
#[cfg(all(debug_assertions, target_arch = "wasm32"))]
console_error_panic_hook::set_once();
// For native builds, initialize gettext translations
#[cfg(not(target_arch = "wasm32"))]
slint::init_translations!(concat!(env!("CARGO_MANIFEST_DIR"), "/lang/"));
let app = App::new().unwrap();
let row_data: Rc<VecModel<slint::ModelRc<StandardListViewItem>>> = Rc::new(VecModel::default());
for r in 1..101 {
let items = Rc::new(VecModel::default());
for c in 1..5 {
items.push(slint::format!("Item {r}.{c}").into());
}
row_data.push(items.into());
}
app.global::<TableViewPageAdapter>().set_row_data(row_data.clone().into());
app.global::<TableViewPageAdapter>().on_filter_sort_model(filter_sort_model);
app.run().unwrap();
}
fn filter_sort_model(
source_model: ModelRc<ModelRc<StandardListViewItem>>,
filter: SharedString,
sort_index: i32,
sort_ascending: bool,
) -> ModelRc<ModelRc<StandardListViewItem>> {
let mut model = source_model.clone();
if !filter.is_empty() {
let filter = filter.to_lowercase();
// filter by first row
model =
Rc::new(source_model.clone().filter(move |e| {
e.row_data(0).unwrap().text.to_lowercase().contains(filter.as_str())
}))
.into();
}
if sort_index >= 0 {
model = Rc::new(model.clone().sort_by(move |r_a, r_b| {
let c_a = r_a.row_data(sort_index as usize).unwrap();
let c_b = r_b.row_data(sort_index as usize).unwrap();
if sort_ascending { c_a.text.cmp(&c_b.text) } else { c_b.text.cmp(&c_a.text) }
}))
.into();
}
model
}