Skip to content

Commit 23937a2

Browse files
committed
[*] refactor
1 parent bc7c26d commit 23937a2

File tree

8 files changed

+72
-16
lines changed

8 files changed

+72
-16
lines changed

lib/video-editor/src/font.rs

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::Result;
22
use cosmic_text::fontdb;
33
use std::path::PathBuf;
44

5-
pub fn get_fonts_info() -> Result<Vec<(String, PathBuf)>> {
5+
pub fn get_fonts_info() -> Result<Vec<(String, PathBuf, String)>> {
66
let mut db = fontdb::Database::new();
77
db.load_system_fonts();
88

@@ -13,7 +13,8 @@ pub fn get_fonts_info() -> Result<Vec<(String, PathBuf)>> {
1313
&& let fontdb::Source::File(path) = &source
1414
&& let Some(family) = face.families.first()
1515
{
16-
fonts.push((family.0.clone(), path.clone()));
16+
let style = format_font_style(face.weight, face.style);
17+
fonts.push((family.0.clone(), path.clone(), style));
1718
}
1819
}
1920

@@ -23,19 +24,53 @@ pub fn get_fonts_info() -> Result<Vec<(String, PathBuf)>> {
2324
Ok(fonts)
2425
}
2526

26-
pub fn get_font_family_from_file(path: &PathBuf) -> Result<String> {
27+
fn format_font_style(weight: fontdb::Weight, style: fontdb::Style) -> String {
28+
let weight_str = match weight {
29+
fontdb::Weight::THIN => "Thin",
30+
fontdb::Weight::EXTRA_LIGHT => "ExtraLight",
31+
fontdb::Weight::LIGHT => "Light",
32+
fontdb::Weight::NORMAL => "",
33+
fontdb::Weight::MEDIUM => "Medium",
34+
fontdb::Weight::SEMIBOLD => "SemiBold",
35+
fontdb::Weight::BOLD => "Bold",
36+
fontdb::Weight::EXTRA_BOLD => "ExtraBold",
37+
fontdb::Weight::BLACK => "Black",
38+
_ => "",
39+
};
40+
41+
let style_str = match style {
42+
fontdb::Style::Normal => "",
43+
fontdb::Style::Italic => "Italic",
44+
fontdb::Style::Oblique => "Oblique",
45+
};
46+
47+
if weight_str.is_empty() && style_str.is_empty() {
48+
"Normal".to_string()
49+
} else if weight_str.is_empty() {
50+
style_str.to_string()
51+
} else if style_str.is_empty() {
52+
weight_str.to_string()
53+
} else {
54+
format!("{} {}", weight_str, style_str)
55+
}
56+
}
57+
58+
pub fn get_font_family_from_file(path: &PathBuf) -> Result<(String, String)> {
2759
let mut db = fontdb::Database::new();
2860
db.load_font_file(path.clone())?;
2961

3062
for face in db.faces() {
3163
if let Some(family) = face.families.first() {
32-
return Ok(family.0.clone());
64+
let style = format_font_style(face.weight, face.style);
65+
return Ok((family.0.clone(), style));
3366
}
3467
}
3568

36-
Ok(path
37-
.file_stem()
38-
.and_then(|s| s.to_str())
39-
.unwrap_or("Unknown")
40-
.to_string())
69+
Ok((
70+
path.file_stem()
71+
.and_then(|s| s.to_str())
72+
.unwrap_or("Unknown")
73+
.to_string(),
74+
"Normal".to_string(),
75+
))
4176
}

todo.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
## 问题
22
- subtitle需要重新设计
33

4-
- fly in preview显示时机有问题
4+
- 选中的font没有显示在字体列表中
55
- fly in filter参数面板暂停时会恢复默认值
66

77
## 待验证
8-
- color picker弹出时需要设置现有的颜色
98
- font 列表显示有问题,条目变少了。刷新时有275条,但是显示时却只有254条
109

1110
- 视频轨道分离字幕

wayshot/src/db.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ pub struct FontEntry {
222222
pub id: String,
223223
pub family: String,
224224
pub path: String,
225+
pub style: String,
225226
pub marked: bool,
226227
pub source: FontSource,
227228
}

wayshot/src/logic/video_editor/conversion.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,11 @@ pub fn video_filter_to_json_detail(filter: &Box<dyn VideoFilter>) -> String {
896896
return serde_json::to_string(f).unwrap_or_default();
897897
}
898898
}
899+
FlyInFilter::NAME => {
900+
if let Some(f) = filter.as_any().downcast_ref::<FlyInFilter>() {
901+
return serde_json::to_string(f).unwrap_or_default();
902+
}
903+
}
899904
_ => {}
900905
}
901906
String::new()

wayshot/src/logic/video_editor/font.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,15 @@ async fn refresh_font_entries_impl(ui_weak: Weak<AppWindow>) {
9191

9292
let mut font_entries: Vec<FontEntry> = Vec::new();
9393

94-
for (family, path) in system_fonts {
94+
for (family, path, style) in system_fonts {
9595
let path_str = path.to_string_lossy().to_string();
9696
let marked = marked_paths.contains(&path_str);
9797

9898
let entry = FontEntry {
9999
id: path_str.clone(),
100100
family,
101101
path: path_str,
102+
style,
102103
marked,
103104
source: FontSource::System,
104105
};
@@ -173,7 +174,7 @@ fn import_font_files(ui: &AppWindow) {
173174
}
174175

175176
async fn import_font_to_db(ui_weak: Weak<AppWindow>, file_path: PathBuf) -> Option<FontEntry> {
176-
let family = match get_font_family_from_file(&file_path) {
177+
let (family, style) = match get_font_family_from_file(&file_path) {
177178
Ok(f) => f,
178179
Err(e) => {
179180
toast::async_toast_warn(
@@ -190,6 +191,7 @@ async fn import_font_to_db(ui_weak: Weak<AppWindow>, file_path: PathBuf) -> Opti
190191
id: path_str.clone(),
191192
family,
192193
path: path_str,
194+
style,
193195
marked: true,
194196
source: FontSource::Imported,
195197
};

wayshot/ui/panel/desktop/font-dialog.slint

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ component FontItem inherits Rectangle {
108108
in property <int> index;
109109
in property <FontEntry> entry;
110110
in-out property <length> family-width;
111+
in-out property <length> style-width;
111112
out property <length> family-preferred-width <=> family-lb.preferred-width;
113+
out property <length> style-preferred-width <=> style-lb.preferred-width;
112114

113115
background: ta.has-hover ? Theme.checked-background : (Math.mod(index, 2) == 0 ? Theme.table-item-first : Theme.table-item-second);
114116

@@ -148,6 +150,13 @@ component FontItem inherits Rectangle {
148150
width: root.family-width;
149151
}
150152

153+
style-lb := Label {
154+
text: entry.style;
155+
color: Theme.secondary-text-color;
156+
horizontal-alignment: left;
157+
width: root.style-width;
158+
}
159+
151160
Label {
152161
text: entry.path;
153162
font-size: Theme.default-font-size * 0.8;
@@ -200,15 +209,18 @@ export component FontDialog inherits MovingDialog {
200209
alignment: start;
201210

202211
private property <length> font-item-max-family-width;
212+
private property <length> font-item-max-style-width;
203213

204214
for entry[index] in Store.font-entries: FontItem {
205215
index: index;
206216
entry: entry;
207217
height: font-item-height;
208218
family-width: parent.font-item-max-family-width;
219+
style-width: parent.font-item-max-style-width;
209220

210221
init => {
211222
parent.font-item-max-family-width = max(parent.font-item-max-family-width, self.family-preferred-width);
223+
parent.font-item-max-style-width = max(parent.font-item-max-style-width, self.style-preferred-width);
212224
}
213225
}
214226
}

wayshot/ui/panel/desktop/video-editor/right-panel/filter/subtitle/font-path.slint

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
Icons,
55
Util,
66
Store,
7+
FontEntry,
78
} from "../../../../../def.slint";
89
import {
910
Label,
@@ -19,11 +20,11 @@ import {
1920

2021
export component FontPath inherits Rectangle {
2122
in-out property <FontPathDetail> config;
22-
private property <string> selected-font-path <=> Store.selected-font-entry.path;
23+
private property <FontEntry> selected-font <=> Store.selected-font-entry;
2324

24-
changed selected-font-path => {
25+
changed selected-font => {
2526
font-li.text = Util.file-name(config.font-path);
26-
config.font-path = selected-font-path;
27+
config.font-path = selected-font.path;
2728
self.apply(config);
2829
}
2930

wayshot/ui/store.slint

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,7 @@ export struct FontEntry {
620620
id: string,
621621
family: string,
622622
path: string,
623+
style: string,
623624
marked: bool,
624625
source: FontSource,
625626
}

0 commit comments

Comments
 (0)