Skip to content

Commit

Permalink
Merge pull request #192 from moxin-org/modals-revision
Browse files Browse the repository at this point in the history
Modals revision
  • Loading branch information
jmbejar authored Aug 6, 2024
2 parents 7cc9803 + 270c626 commit ca37d07
Show file tree
Hide file tree
Showing 10 changed files with 284 additions and 366 deletions.
64 changes: 0 additions & 64 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@ use crate::chat::chat_history_card_options::{
ChatHistoryCardOptionsAction, ChatHistoryCardOptionsWidgetRefExt,
};
use crate::chat::chat_panel::ChatPanelAction;
use crate::chat::delete_chat_modal::{DeleteChatAction, DeleteChatModalWidgetRefExt};
use crate::data::downloads::DownloadPendingNotification;
use crate::data::store::*;
use crate::landing::model_card::{ModelCardViewAllModalWidgetRefExt, ViewAllModalAction};
use crate::landing::model_files_item::ModelFileItemAction;
use crate::my_models::delete_model_modal::{DeleteModelAction, DeleteModelModalWidgetRefExt};
use crate::my_models::model_info_modal::{ModelInfoAction, ModelInfoModalWidgetRefExt};
use crate::shared::actions::{ChatAction, DownloadAction, TooltipAction};
use crate::shared::download_notification_popup::{
DownloadNotificationPopupWidgetRefExt, DownloadResult, PopupAction,
Expand All @@ -34,9 +30,6 @@ live_design! {
import crate::landing::model_card::ModelCardViewAllModal;
import crate::chat::chat_screen::ChatScreen;
import crate::my_models::my_models_screen::MyModelsScreen;
import crate::my_models::delete_model_modal::DeleteModelModal;
import crate::chat::delete_chat_modal::DeleteChatModal;
import crate::my_models::model_info_modal::ModelInfoModal;
import crate::chat::chat_history_card_options::ChatHistoryCardOptions ;


Expand Down Expand Up @@ -121,38 +114,6 @@ live_design! {
}

portal_root = <Portal> {
modal_model_card_view_all_portal_view = <PortalView> {
modal = <Modal> {
content = {
model_card_view_all_modal = <ModelCardViewAllModal> {}
}
}
}

modal_delete_model_portal_view = <PortalView> {
modal = <Modal> {
content = {
delete_model_modal = <DeleteModelModal> {}
}
}
}

modal_delete_chat_portal_view = <PortalView> {
modal = <Modal> {
content = {
delete_chat_modal = <DeleteChatModal> {}
}
}
}

modal_model_info_portal_view = <PortalView> {
modal = <Modal> {
content = {
model_info_modal = <ModelInfoModal> {}
}
}
}

popup_download_success_portal_view = <PortalView> {
align: {x: 1, y: 0}
popup_download_success = <DownloadNotificationPopup> {}
Expand Down Expand Up @@ -273,31 +234,6 @@ impl MatchEvent for App {
self.ui.redraw(cx);
}

// Set modal viewall model id
if let ViewAllModalAction::ModelSelected(model_id) = action.as_widget_action().cast() {
let mut modal = self
.ui
.model_card_view_all_modal(id!(model_card_view_all_modal));
modal.set_model_id(model_id);
}

// Set modal viewall model id
if let DeleteModelAction::FileSelected(file_id) = action.as_widget_action().cast() {
let mut modal = self.ui.delete_model_modal(id!(delete_model_modal));
modal.set_file_id(file_id);
}

// Set modal viewall model id
if let DeleteChatAction::ChatSelected(chat_id) = action.as_widget_action().cast() {
let mut modal = self.ui.delete_chat_modal(id!(delete_chat_modal));
modal.set_chat_id(chat_id);
}

if let ModelInfoAction::FileSelected(file_id) = action.as_widget_action().cast() {
let mut modal = self.ui.model_info_modal(id!(model_info_modal));
modal.set_file_id(file_id);
}

if let ChatAction::Start(_) = action.as_widget_action().cast() {
let chat_radio_button = self.ui.radio_button(id!(chat_tab));
chat_radio_button.select(cx, &mut Scope::empty());
Expand Down
27 changes: 15 additions & 12 deletions src/chat/chat_history_card_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use crate::{
shared::portal::PortalAction,
};
use makepad_widgets::*;

use super::{chat_history_card::ChatHistoryCardAction, delete_chat_modal::DeleteChatAction};
use super::chat_history_card::ChatHistoryCardAction;
use super::delete_chat_modal::DeleteChatModalWidgetExt;
use crate::shared::modal::ModalWidgetExt;

live_design! {
import makepad_widgets::base::*;
Expand All @@ -14,6 +15,8 @@ live_design! {
import makepad_draw::shader::draw_color::DrawColor;
import crate::shared::widgets::*;
import crate::shared::styles::*;
import crate::shared::modal::*;
import crate::chat::delete_chat_modal::DeleteChatModal;

ICON_DELETE = dep("crate://self/resources/icons/delete.svg")
ICON_EDIT = dep("crate://self/resources/icons/edit.svg")
Expand Down Expand Up @@ -92,7 +95,12 @@ live_design! {

text: "Delete Chat"
}
}

delete_chat_modal = <Modal> {
content: {
delete_chat_modal_inner = <DeleteChatModal> {}
}
}
}
}
Expand Down Expand Up @@ -158,16 +166,11 @@ impl WidgetMatchEvent for ChatHistoryCardOptions {
let widget_uid = self.widget_uid();

if self.button(id!(delete_chat)).clicked(actions) {
cx.widget_action(
widget_uid,
&scope.path,
DeleteChatAction::ChatSelected(self.chat_id),
);
cx.widget_action(
widget_uid,
&scope.path,
PortalAction::ShowPortalView(live_id!(modal_delete_chat_portal_view)),
);
let mut delete_modal_inner = self.delete_chat_modal(id!(delete_chat_modal_inner));
delete_modal_inner.set_chat_id(self.chat_id);

let modal = self.modal(id!(delete_chat_modal));
modal.open_modal(cx);
}

if self.button(id!(edit_chat_name)).clicked(actions) {
Expand Down
6 changes: 0 additions & 6 deletions src/chat/delete_chat_modal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,6 @@ live_design! {
}
}

#[derive(Clone, DefaultNone, Debug)]
pub enum DeleteChatAction {
ChatSelected(ChatID),
None,
}

#[derive(Live, LiveHook, Widget)]
pub struct DeleteChatModal {
#[deref]
Expand Down
4 changes: 1 addition & 3 deletions src/data/downloads/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,17 @@ pub enum DownloadState {
#[derive(Debug)]
pub struct Download {
pub file: File,
pub model: Model,
pub sender: Sender<DownloadFileAction>,
pub receiver: Receiver<DownloadFileAction>,
pub state: DownloadState,
pub notification_pending: bool,
}

impl Download {
pub fn new(file: File, model: Model, progress: f64, backend: &Backend) -> Self {
pub fn new(file: File, progress: f64, backend: &Backend) -> Self {
let (tx, rx) = channel();
let mut download = Self {
file: file,
model: model,
sender: tx,
receiver: rx,
state: DownloadState::Initializing(progress),
Expand Down
2 changes: 1 addition & 1 deletion src/data/downloads/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl Downloads {

self.current_downloads.insert(
file.id.clone(),
Download::new(file, model, current_progress, &self.backend.as_ref()),
Download::new(file, current_progress, &self.backend.as_ref()),
);
}

Expand Down
70 changes: 22 additions & 48 deletions src/landing/model_card.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::data::store::{ModelWithDownloadInfo, Store};
use crate::data::store::ModelWithDownloadInfo;
use crate::shared::external_link::ExternalLinkWidgetExt;
use crate::shared::modal::ModalWidgetExt;
use crate::shared::portal::PortalAction;
use crate::shared::utils::hugging_face_model_url;
use chrono::Utc;
use makepad_widgets::*;
use moxin_protocol::data::ModelID;
use unicode_segmentation::UnicodeSegmentation;

live_design! {
Expand All @@ -19,6 +19,7 @@ live_design! {
import crate::landing::shared::*;
import crate::landing::model_files::ModelFiles;
import crate::shared::external_link::*;
import crate::shared::modal::*;

ICON_DOWNLOADS = dep("crate://self/resources/icons/downloads.svg")
ICON_FAVORITE = dep("crate://self/resources/icons/favorite.svg")
Expand Down Expand Up @@ -294,6 +295,9 @@ live_design! {
width: Fill,
height: Fit,

// This is necesary because we have a Modal widget inside this widget
flow: Overlay,

<RoundedView> {
width: Fill,
height: Fit,
Expand All @@ -314,6 +318,12 @@ live_design! {
<ModelInformation> {}
<ModelFiles> {}
}

modal = <Modal> {
content: {
<ModelCardViewAllModal> {}
}
}
}
}

Expand Down Expand Up @@ -397,39 +407,18 @@ impl Widget for ModelCard {
}

impl WidgetMatchEvent for ModelCard {
fn handle_actions(&mut self, cx: &mut Cx, actions: &Actions, scope: &mut Scope) {
let widget_uid = self.widget_uid();

fn handle_actions(&mut self, cx: &mut Cx, actions: &Actions, _scope: &mut Scope) {
if self.link_label(id!(view_all_button.link)).clicked(actions) {
cx.widget_action(
widget_uid,
&scope.path,
PortalAction::ShowPortalView(live_id!(modal_model_card_view_all_portal_view)),
);

cx.widget_action(
widget_uid,
&scope.path,
ViewAllModalAction::ModelSelected(self.model_id.clone()),
);
self.modal(id!(modal)).open_modal(cx);
self.redraw(cx);
}
}
}

// TODO: Make it so this action and setting the model id from the outside isnt needed
// find a way to pass the data inside the Modal action itself.
#[derive(Clone, DefaultNone, Eq, Hash, PartialEq, Debug)]
pub enum ViewAllModalAction {
None,
ModelSelected(ModelID),
}

#[derive(Live, LiveHook, Widget)]
pub struct ModelCardViewAllModal {
#[deref]
view: View,
#[rust]
model_id: ModelID,
}

impl Widget for ModelCardViewAllModal {
Expand All @@ -439,20 +428,13 @@ impl Widget for ModelCardViewAllModal {
}

fn draw_walk(&mut self, cx: &mut Cx2d, scope: &mut Scope, walk: Walk) -> DrawStep {
let store = scope.data.get::<Store>().unwrap();
let model = store
.search
.models
.iter()
.find(|model| model.id == self.model_id);

if let Some(model) = model {
let name = &model.name;
self.label(id!(model_name)).set_text(name);

let summary = &model.summary;
self.label(id!(model_summary)).set_text(summary);
}
let model = &scope.data.get::<ModelWithDownloadInfo>().unwrap();

let name = &model.name;
self.label(id!(model_name)).set_text(name);

let summary = &model.summary;
self.label(id!(model_summary)).set_text(summary);

self.view
.draw_walk(cx, scope, walk.with_abs_pos(DVec2 { x: 0., y: 0. }))
Expand All @@ -469,14 +451,6 @@ impl WidgetMatchEvent for ModelCardViewAllModal {
}
}

impl ModelCardViewAllModalRef {
pub fn set_model_id(&mut self, model_id: ModelID) {
if let Some(mut inner) = self.borrow_mut() {
inner.model_id = model_id;
}
}
}

fn formatted_model_release_date(model: &ModelWithDownloadInfo) -> String {
let released_at = model.released_at.naive_local().format("%b %-d, %C%y");
let days_ago = (Utc::now() - model.released_at).num_days();
Expand Down
34 changes: 7 additions & 27 deletions src/my_models/delete_model_modal.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use makepad_widgets::*;
use moxin_protocol::data::FileID;

use crate::{chat::chat_panel::ChatPanelAction, data::store::Store, shared::portal::PortalAction};

use super::downloaded_files_row::DownloadedFilesRowProps;

live_design! {
import makepad_widgets::base::*;
import makepad_widgets::theme_desktop_dark::*;
Expand Down Expand Up @@ -132,12 +133,6 @@ live_design! {
}
}

#[derive(Clone, DefaultNone, Debug)]
pub enum DeleteModelAction {
FileSelected(FileID),
None,
}

#[derive(Live, LiveHook, Widget)]
pub struct DeleteModelModal {
#[deref]
Expand All @@ -153,17 +148,10 @@ impl Widget for DeleteModelModal {
}

fn draw_walk(&mut self, cx: &mut Cx2d, scope: &mut Scope, walk: Walk) -> DrawStep {
let downloaded_files = &scope
.data
.get::<Store>()
.unwrap()
.downloads
.downloaded_files;

let downloaded_file = downloaded_files
.iter()
.find(|f| f.file.id.eq(&self.file_id))
.expect("Downloaded file not found");
let props = scope.props.get::<DownloadedFilesRowProps>().unwrap();
let downloaded_file = &props.downloaded_file;

self.file_id = downloaded_file.file.id.clone();

let prompt_text = format!(
"Are you sure you want to delete {}?\nThis action cannot be undone.",
Expand Down Expand Up @@ -208,12 +196,4 @@ impl WidgetMatchEvent for DeleteModelModal {
cx.widget_action(widget_uid, &scope.path, PortalAction::Close);
}
}
}

impl DeleteModelModalRef {
pub fn set_file_id(&mut self, file_id: FileID) {
if let Some(mut inner) = self.borrow_mut() {
inner.file_id = file_id;
}
}
}
}
Loading

0 comments on commit ca37d07

Please sign in to comment.