@@ -13,8 +13,8 @@ use std::path::PathBuf;
13
13
use std:: sync:: { Arc , LazyLock , Mutex , mpsc} ;
14
14
15
15
use iced:: widget:: button:: Style ;
16
- use iced:: widget:: { Column , button, column, container, horizontal_space , row, text} ;
17
- use iced:: { Element , Fill , Subscription , Theme } ;
16
+ use iced:: widget:: { Column , button, column, container, row, text} ;
17
+ use iced:: { Element , Fill , Subscription , Task , Theme , clipboard } ;
18
18
use tracing:: { debug, error, trace} ;
19
19
use uuid:: Uuid ;
20
20
@@ -30,6 +30,7 @@ mod log;
30
30
mod request;
31
31
mod saves;
32
32
33
+ const WEB_ORIGIN : & str = dotenvy_macro:: dotenv!( "WEB_ORIGIN" ) ;
33
34
const SERVER_HOST : & str = dotenvy_macro:: dotenv!( "SERVER_HOST" ) ;
34
35
static SERVER_PORT : LazyLock < u16 > = LazyLock :: new ( || {
35
36
dotenvy_macro:: dotenv!( "SERVER_PORT" )
@@ -50,7 +51,9 @@ pub fn main() -> iced::Result {
50
51
. run ( )
51
52
}
52
53
53
- fn update ( state : & mut State , message : Message ) {
54
+ fn update ( state : & mut State , message : Message ) -> Task < Message > {
55
+ let none = Task :: none ( ) ;
56
+
54
57
match message {
55
58
Message :: RegisterOnServer => {
56
59
let selected_profile = state
@@ -64,20 +67,20 @@ fn update(state: &mut State, message: Message) {
64
67
let save_path = save_file_for_profile ( & install_dir. 1 , OsStr :: new ( selected_profile) ) ;
65
68
66
69
let Some ( save_packed) = read_save_packed ( & save_path) else {
67
- return ;
70
+ return none ;
68
71
} ;
69
72
70
73
let Some ( config) = & mut state. config else {
71
74
error ! ( "config not loaded, skipping saving" ) ;
72
- return ;
75
+ return none ;
73
76
} ;
74
77
let Some ( key) = config. auth_key ( ) else {
75
78
error ! ( "not authorized, skipping register" ) ;
76
- return ;
79
+ return none ;
77
80
} ;
78
81
79
82
let Ok ( resp) = send_register ( key, save_packed) else {
80
- return ;
83
+ return none ;
81
84
} ;
82
85
83
86
state
@@ -96,20 +99,20 @@ fn update(state: &mut State, message: Message) {
96
99
97
100
let Some ( config) = & mut state. config else {
98
101
error ! ( "config not loaded, skipping saving" ) ;
99
- return ;
102
+ return none ;
100
103
} ;
101
104
102
105
let Some ( save_packed) = read_save_packed ( & path) else {
103
- return ;
106
+ return none ;
104
107
} ;
105
108
106
109
let Some ( id) = config. find_profile ( & name) else {
107
110
debug ! ( "ignoring file update for non-tracked profile" ) ;
108
- return ;
111
+ return none ;
109
112
} ;
110
113
let Some ( key) = config. auth_key ( ) else {
111
114
error ! ( "not authorized, skipping register" ) ;
112
- return ;
115
+ return none ;
113
116
} ;
114
117
115
118
let _ = send_register_update ( id, key, save_packed) ;
@@ -118,14 +121,18 @@ fn update(state: &mut State, message: Message) {
118
121
if let Some ( ref current) = state. selected_profile
119
122
&& current == & name
120
123
{
121
- return ;
124
+ return none ;
122
125
}
123
126
state. selected_profile . replace ( name. clone ( ) ) ;
124
127
}
128
+ Message :: ShareProfile ( id) => {
129
+ let url = format ! ( "{WEB_ORIGIN}/ow-tracker?profile={id}" ) ;
130
+ return clipboard:: write ( url) ;
131
+ }
125
132
Message :: ForgetRegister ( id) => {
126
133
let Some ( config) = & mut state. config else {
127
134
error ! ( "config not loaded, skipping forgetting" ) ;
128
- return ;
135
+ return none ;
129
136
} ;
130
137
131
138
state
@@ -144,6 +151,8 @@ fn update(state: &mut State, message: Message) {
144
151
. log_msg ( "failed to save config on disk" ) ;
145
152
}
146
153
}
154
+
155
+ none
147
156
}
148
157
149
158
fn view ( state : & State ) -> Element < ' _ , Message > {
@@ -168,6 +177,7 @@ fn view(state: &State) -> Element<'_, Message> {
168
177
profiles. sort_unstable ( ) ;
169
178
let profiles = profiles. into_iter ( ) . map ( |p| {
170
179
let cloned_p = p. clone ( ) ;
180
+ let profile_register = config. profiles ( ) . iter ( ) . find ( |profile| profile. name == p) ;
171
181
row ! [
172
182
text( format!( "- {p}" ) ) . width( 200 ) . size( 20 ) ,
173
183
row![
@@ -186,15 +196,12 @@ fn view(state: &State) -> Element<'_, Message> {
186
196
}
187
197
} )
188
198
. on_press( Message :: SelectProfile ( p. to_string( ) ) ) ,
189
- horizontal_space( ) . width( 10 ) ,
190
- button( "Forget register" ) . on_press_maybe(
191
- config
192
- . profiles( )
193
- . iter( )
194
- . find( |profile| profile. name == p)
195
- . map( |p| Message :: ForgetRegister ( p. id) )
196
- ) ,
197
- ] ,
199
+ button( "Forget register" )
200
+ . on_press_maybe( profile_register. map( |p| Message :: ForgetRegister ( p. id) ) ) ,
201
+ button( "Share" )
202
+ . on_press_maybe( profile_register. map( |p| Message :: ShareProfile ( p. id) ) ) ,
203
+ ]
204
+ . spacing( 10 ) ,
198
205
]
199
206
. width ( 500 )
200
207
. into ( )
@@ -251,6 +258,7 @@ enum Message {
251
258
RegisterOnServer ,
252
259
SelectProfile ( String ) ,
253
260
FileUpdated ( FileUpdateEvent ) ,
261
+ ShareProfile ( Uuid ) ,
254
262
ForgetRegister ( Uuid ) ,
255
263
}
256
264
0 commit comments