Skip to content

Commit

Permalink
refactor ModelFileItemAction and DownloadAction (#300)
Browse files Browse the repository at this point in the history
* refactor ModelFileItemAction and DownloadAction

* Change it to WidgetMatchEvent.

* use WidgetMatchEvent instead of MatchEvent.
Guocork authored Nov 15, 2024
1 parent 2a2115f commit edfa4fe
Showing 4 changed files with 11 additions and 51 deletions.
4 changes: 2 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -202,7 +202,7 @@ impl MatchEvent for App {
_ => {}
}

match action.as_widget_action().cast() {
match action.cast() {
ModelFileItemAction::Download(file_id) => {
let (model, file) = self.store.get_model_and_file_download(&file_id);
self.store.downloads.download_file(model, file);
@@ -211,7 +211,7 @@ impl MatchEvent for App {
_ => {}
}

match action.as_widget_action().cast() {
match action.cast() {
DownloadAction::Play(file_id) => {
let (model, file) = self.store.get_model_and_file_download(&file_id);
self.store.downloads.download_file(model, file);
21 changes: 3 additions & 18 deletions src/landing/download_item.rs
Original file line number Diff line number Diff line change
@@ -344,33 +344,18 @@ impl WidgetMatchEvent for DownloadItem {
for button_id in [id!(play_button), id!(retry_button)] {
if self.button(button_id).clicked(&actions) {
let Some(file_id) = &self.file_id else { return };
let widget_uid = self.widget_uid();
cx.widget_action(
widget_uid,
&scope.path,
DownloadAction::Play(file_id.clone()),
)
cx.action(DownloadAction::Play(file_id.clone()));
}
}

if self.button(id!(pause_button)).clicked(&actions) {
let Some(file_id) = &self.file_id else { return };
let widget_uid = self.widget_uid();
cx.widget_action(
widget_uid,
&scope.path,
DownloadAction::Pause(file_id.clone()),
)
cx.action(DownloadAction::Pause(file_id.clone()));
}

if self.button(id!(cancel_button)).clicked(&actions) {
let Some(file_id) = &self.file_id else { return };
let widget_uid = self.widget_uid();
cx.widget_action(
widget_uid,
&scope.path,
DownloadAction::Cancel(file_id.clone()),
)
cx.action(DownloadAction::Cancel(file_id.clone()));
}
}
}
25 changes: 4 additions & 21 deletions src/landing/model_files_item.rs
Original file line number Diff line number Diff line change
@@ -320,17 +320,12 @@ impl Widget for ModelFilesItem {

impl WidgetMatchEvent for ModelFilesItem {
fn handle_actions(&mut self, cx: &mut Cx, actions: &Actions, scope: &mut Scope) {
let widget_uid = self.widget_uid();
let Some(file_id) = self.file_id.clone() else {
return;
};

if self.button(id!(download_button)).clicked(&actions) {
cx.widget_action(
widget_uid,
&scope.path,
ModelFileItemAction::Download(file_id.clone()),
);
cx.action(ModelFileItemAction::Download(file_id.clone()));
}

if self.button(id!(start_chat_button)).clicked(&actions) {
@@ -341,27 +336,15 @@ impl WidgetMatchEvent for ModelFilesItem {
.iter()
.any(|id| self.button(*id).clicked(&actions))
{
cx.widget_action(
widget_uid,
&scope.path,
DownloadAction::Play(file_id.clone()),
);
cx.action(DownloadAction::Play(file_id.clone()));
}

if self.button(id!(pause_download_button)).clicked(&actions) {
cx.widget_action(
widget_uid,
&scope.path,
DownloadAction::Pause(file_id.clone()),
);
cx.action(DownloadAction::Pause(file_id.clone()));
}

if self.button(id!(cancel_download_button)).clicked(&actions) {
cx.widget_action(
widget_uid,
&scope.path,
DownloadAction::Cancel(file_id.clone()),
);
cx.action(DownloadAction::Cancel(file_id.clone()));
}
}
}
12 changes: 2 additions & 10 deletions src/shared/download_notification_popup.rs
Original file line number Diff line number Diff line change
@@ -264,21 +264,13 @@ impl WidgetMatchEvent for DownloadNotificationPopup {

if self.link_label(id!(retry_link)).clicked(actions) {
let Some(file_id) = &self.file_id else { return };
cx.widget_action(
widget_uid,
&scope.path,
DownloadAction::Play(file_id.clone()),
);
cx.action(DownloadAction::Play(file_id.clone()));
cx.action(DownloadNotificationPopupAction::ActionLinkClicked);
}

if self.link_label(id!(cancel_link)).clicked(actions) {
let Some(file_id) = &self.file_id else { return };
cx.widget_action(
widget_uid,
&scope.path,
DownloadAction::Cancel(file_id.clone()),
);
cx.action(DownloadAction::Cancel(file_id.clone()));
cx.action(DownloadNotificationPopupAction::ActionLinkClicked);
}
}

0 comments on commit edfa4fe

Please sign in to comment.