Skip to content

Commit b6d6286

Browse files
committed
[*] refactor: video-editor
1 parent 1c57e31 commit b6d6286

File tree

8 files changed

+391
-68
lines changed

8 files changed

+391
-68
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/record-audio/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,11 @@ impl AudioRecorder {
120120

121121
let mut result = Vec::new();
122122
for device in devices {
123-
if let Ok(desc) = device.description()
123+
if let Ok(id) = device.id()
124124
&& let Ok(config) = device.default_input_config()
125125
{
126126
result.push(AudioDeviceInfo {
127-
name: desc.name().to_string(),
127+
name: id.to_string(),
128128
channels: config.channels(),
129129
sample_rate: config.sample_rate(),
130130
});

wayshot/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ mp4-player.workspace = true
3939
downloader.workspace = true
4040
audio-utils.workspace = true
4141
video-utils.workspace = true
42+
record-audio.workspace = true
4243
image-effect.workspace = true
4344
video-editor.workspace = true
4445
native-dialog.workspace = true

wayshot/src/logic/video_editor/common_type.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::slint_generatedAppWindow::{
99
VideoEditorPreferenceConfig as UIVideoEditorPreferenceConfig,
1010
VideoEditorPreferenceTrackConfig as UIVideoEditorPreferenceTrackConfig,
1111
VideoEditorPreviewConfig as UIVideoEditorPreviewConfig,
12+
VideoEditorRecordAudioConfig as UIVideoEditorRecordAudioConfig,
1213
VideoEditorTrackType as UIVideoEditorTrackType, VideoEditorUIState as UIVideoEditorUIState,
1314
};
1415
use derivative::Derivative;
@@ -127,3 +128,12 @@ pub struct VideoEditorExportAudioConfig {
127128
pub channels: UIAudioChannels,
128129
pub sample_rate: UIAudioSampleRate,
129130
}
131+
132+
#[derive(Serialize, Deserialize, Debug, Clone, Derivative, SlintFromConvert)]
133+
#[derivative(Default)]
134+
#[from("UIVideoEditorRecordAudioConfig")]
135+
#[serde(default)]
136+
pub struct VideoEditorRecordAudioConfig {
137+
pub save_dir: String,
138+
pub device: String,
139+
}

wayshot/src/logic/video_editor/playlist.rs

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -39,55 +39,59 @@ fn video_editor_playlist_item_import(ui: &AppWindow) {
3939
return;
4040
};
4141

42-
let playlist_item = {
43-
let mut state = PROJECT_STATE.lock().unwrap();
44-
if let Some(ref mut s) = *state {
45-
match s.playlist.add_file(file_path.clone()) {
46-
Ok(index) => s.playlist.get_item(index).cloned(),
47-
Err(e) => {
48-
toast::async_toast_warn(
49-
ui_weak.clone(),
50-
format!("Failed to add to playlist: {}", e),
51-
);
52-
return;
53-
}
54-
}
55-
} else {
56-
None
57-
}
58-
};
59-
60-
let Some(playlist_item) = playlist_item else {
61-
toast::async_toast_warn(ui_weak.clone(), "Failed to get playlist item".to_string());
62-
return;
63-
};
42+
import_file_to_playlist(ui_weak, file_path).await;
43+
});
44+
}
6445

65-
// Preload audio cache for video/audio files
66-
let metadata = get_metadata(&file_path).ok();
67-
if let Some(ref metadata) = metadata
68-
&& let Some(audio_meta) = metadata.audios.first()
69-
{
70-
let file_path = file_path.clone();
71-
let audio_meta = audio_meta.clone();
72-
tokio::task::spawn_blocking(move || {
73-
let cache = get_global_audio_display_cache();
74-
if let Err(e) = cache.load_and_cache(&file_path, audio_meta.index, &audio_meta) {
75-
log::warn!(
76-
"Failed to preload audio cache for {}: {:?}",
77-
file_path.display(),
78-
e
46+
pub async fn import_file_to_playlist(ui_weak: Weak<AppWindow>, file_path: PathBuf) {
47+
let playlist_item = {
48+
let mut state = PROJECT_STATE.lock().unwrap();
49+
if let Some(ref mut s) = *state {
50+
match s.playlist.add_file(file_path.clone()) {
51+
Ok(index) => s.playlist.get_item(index).cloned(),
52+
Err(e) => {
53+
toast::async_toast_warn(
54+
ui_weak.clone(),
55+
format!("Failed to add to playlist: {}", e),
7956
);
80-
} else {
81-
log::debug!("Preloaded audio cache for {}", file_path.display());
57+
return;
8258
}
83-
});
59+
}
60+
} else {
61+
None
8462
}
63+
};
8564

86-
_ = ui_weak.upgrade_in_event_loop(move |ui| {
87-
let item: UIVideoEditorPlaylistItem = playlist_item.into();
88-
crate::toast_success!(ui, format!("Imported: {}", item.name));
89-
store_video_editor_playlist!(ui).push(item);
65+
let Some(playlist_item) = playlist_item else {
66+
toast::async_toast_warn(ui_weak.clone(), "Failed to get playlist item".to_string());
67+
return;
68+
};
69+
70+
// Preload audio cache for video/audio files
71+
let metadata = get_metadata(&file_path).ok();
72+
if let Some(ref metadata) = metadata
73+
&& let Some(audio_meta) = metadata.audios.first()
74+
{
75+
let file_path = file_path.clone();
76+
let audio_meta = audio_meta.clone();
77+
tokio::task::spawn_blocking(move || {
78+
let cache = get_global_audio_display_cache();
79+
if let Err(e) = cache.load_and_cache(&file_path, audio_meta.index, &audio_meta) {
80+
log::warn!(
81+
"Failed to preload audio cache for {}: {:?}",
82+
file_path.display(),
83+
e
84+
);
85+
} else {
86+
log::debug!("Preloaded audio cache for {}", file_path.display());
87+
}
9088
});
89+
}
90+
91+
_ = ui_weak.upgrade_in_event_loop(move |ui| {
92+
let item: UIVideoEditorPlaylistItem = playlist_item.into();
93+
crate::toast_success!(ui, format!("Imported: {}", item.name));
94+
store_video_editor_playlist!(ui).push(item);
9195
});
9296
}
9397

0 commit comments

Comments
 (0)