ferroui_xaml_interactions_custom/file_upload/
upload_file_behavior_base.rs1use ferroui_base::{
4 ferro_class, ferro_properties, BoxedValue, FerroObjectImpl, FerroObjectImplExt, FerroProperty, StyledElementImpl,
5 StyledProperty, Visual,
6};
7use ferroui_xaml_interactivity::helpers::member_lookup::object_of;
8use ferroui_xaml_interactivity::{InvokeCommandBehaviorBase, StyledElementBehaviorImpl};
9
10#[repr(C)]
20pub struct UploadFileBehaviorBase {
21 base: InvokeCommandBehaviorBase,
22}
23
24ferro_class!(UploadFileBehaviorBase: InvokeCommandBehaviorBase);
25ferroui_base::ferro_impl_classes!(UploadFileBehaviorBase: StyledElementImpl, StyledElementBehaviorImpl);
26
27ferro_properties! {
28 impl UploadFileBehaviorBase {
29 pub fn file_path_property() -> StyledProperty<Option<String>> {
31 FerroProperty::register::<UploadFileBehaviorBase, _>("FilePath", None)
32 }
33
34 pub fn url_property() -> StyledProperty<Option<String>> {
36 FerroProperty::register::<UploadFileBehaviorBase, _>("Url", None)
37 }
38 }
39}
40
41ferroui_base::ferro_overrides! {
42 impl FerroObjectImpl for UploadFileBehaviorBase {
43 fn constructed(this: &Self) {
45 Self::parent_constructed(this);
46
47 this.set_pass_event_args_to_command(true);
48 }
49 }
50}
51
52impl UploadFileBehaviorBase {
53 pub fn construct() -> Self {
55 Self { base: InvokeCommandBehaviorBase::construct() }
56 }
57
58 pub fn file_path(&self) -> Option<String> {
60 self.get_value(Self::file_path_property())
61 }
62
63 pub fn set_file_path(&self, value: Option<String>) {
64 self.set_value(Self::file_path_property(), value)
65 }
66
67 pub fn url(&self) -> Option<String> {
69 self.get_value(Self::url_property())
70 }
71
72 pub fn set_url(&self, value: Option<String>) {
73 self.set_value(Self::url_property(), value)
74 }
75
76 pub fn execute(&self, sender: Option<&BoxedValue>, _parameter: Option<&BoxedValue>) {
79 let is_visual = sender.and_then(object_of).is_some_and(|sender| sender.cast::<Visual>().is_some());
80 if !is_visual {
81 return;
82 }
83
84 self.upload_async();
85 }
86
87 fn upload_async(&self) {
88 if !self.is_enabled() || self.command().is_none() {
89 return;
90 }
91
92 let (Some(file_path), Some(_url)) = (self.file_path(), self.url()) else {
93 return;
94 };
95
96 if !file_exists(&file_path) {
97 return;
98 }
99
100 }
104}
105
106#[cfg(not(target_arch = "wasm32"))]
108pub(crate) fn file_exists(path: &str) -> bool {
109 std::path::Path::new(path).is_file()
110}
111
112#[cfg(target_arch = "wasm32")]
114pub(crate) fn file_exists(_path: &str) -> bool {
115 false
116}