Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Clash Verge
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
many-archive
Clash Verge
Commits
d75fb152
There was an error fetching the commit references. Please try again later.
Commit
d75fb152
authored
3 years ago
by
GyDi
Browse files
Options
Downloads
Patches
Plain Diff
feat: lock profiles file and support more cmds
parent
eb3daf7b
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src-tauri/src/cmd.rs
+69
-3
69 additions, 3 deletions
src-tauri/src/cmd.rs
src-tauri/src/events/state.rs
+3
-0
3 additions, 0 deletions
src-tauri/src/events/state.rs
src-tauri/src/main.rs
+6
-3
6 additions, 3 deletions
src-tauri/src/main.rs
with
78 additions
and
6 deletions
src-tauri/src/cmd.rs
+
69
−
3
View file @
d75fb152
use
crate
::{
config
::{
read_profiles
,
save_profiles
,
ProfileItem
},
events
::{
emit
::
ClashInfoPayload
,
state
::
ClashInfoState
},
config
::{
read_profiles
,
save_profiles
,
ProfileItem
,
ProfilesConfig
},
events
::{
emit
::
ClashInfoPayload
,
state
::{
ClashInfoState
,
ProfileLock
},
},
utils
::{
app_home_dir
,
clash
,
fetch
::
fetch_profile
},
};
use
std
::
fs
::
File
;
...
...
@@ -28,7 +31,7 @@ pub fn get_clash_info(clash_info: State<'_, ClashInfoState>) -> Option<ClashInfo
/// Import the Profile from url and
/// save to the `profiles.yaml` file
#[tauri::command]
pub
async
fn
import_profile
(
url
:
String
)
->
Result
<
String
,
String
>
{
pub
async
fn
import_profile
(
url
:
String
,
lock
:
State
<
'_
,
ProfileLock
>
)
->
Result
<
String
,
String
>
{
let
result
=
match
fetch_profile
(
&
url
)
.await
{
Some
(
r
)
=>
r
,
None
=>
{
...
...
@@ -43,6 +46,12 @@ pub async fn import_profile(url: String) -> Result<String, String> {
.write
(
result
.data
.as_bytes
())
.unwrap
();
// get lock
match
lock
.0
.lock
()
{
Ok
(
_
)
=>
{}
Err
(
_
)
=>
return
Err
(
format!
(
"can not get file locked"
)),
};
// update profiles.yaml
let
mut
profiles
=
read_profiles
();
let
mut
items
=
match
profiles
.items
{
...
...
@@ -65,3 +74,60 @@ pub async fn import_profile(url: String) -> Result<String, String> {
Ok
(
format!
(
"success"
))
}
#[tauri::command]
pub
fn
get_profiles
(
lock
:
State
<
'_
,
ProfileLock
>
)
->
Option
<
ProfilesConfig
>
{
match
lock
.0
.lock
()
{
Ok
(
_
)
=>
Some
(
read_profiles
()),
Err
(
_
)
=>
None
,
}
}
#[tauri::command]
pub
fn
set_profiles
(
current
:
usize
,
profile
:
ProfileItem
,
lock
:
State
<
'_
,
ProfileLock
>
,
)
->
Result
<
(),
String
>
{
match
lock
.0
.lock
()
{
Ok
(
_
)
=>
{}
Err
(
_
)
=>
return
Err
(
format!
(
"can not get file locked"
)),
};
let
mut
profiles
=
read_profiles
();
let
mut
items
=
match
profiles
.items
{
Some
(
p
)
=>
p
,
None
=>
vec!
[],
};
if
current
>=
items
.len
()
{
return
Err
(
format!
(
"out of profiles bound"
));
}
let
mut
origin
=
items
[
current
]
.clone
();
if
profile
.name
.is_some
()
{
origin
.name
=
profile
.name
;
}
if
profile
.file
.is_some
()
{
origin
.file
=
profile
.file
;
}
if
profile
.mode
.is_some
()
{
origin
.mode
=
profile
.mode
;
}
if
profile
.url
.is_some
()
{
origin
.url
=
profile
.url
;
}
if
profile
.selected
.is_some
()
{
origin
.selected
=
profile
.selected
;
}
if
profile
.extra
.is_some
()
{
origin
.extra
=
profile
.extra
;
}
items
[
current
]
=
origin
;
profiles
.items
=
Some
(
items
);
save_profiles
(
&
profiles
);
Ok
(())
}
This diff is collapsed.
Click to expand it.
src-tauri/src/events/state.rs
+
3
−
0
View file @
d75fb152
...
...
@@ -4,3 +4,6 @@ use super::emit::ClashInfoPayload;
#[derive(Default)]
pub
struct
ClashInfoState
(
pub
Arc
<
Mutex
<
ClashInfoPayload
>>
);
#[derive(Default)]
pub
struct
ProfileLock
(
pub
Mutex
<
bool
>
);
This diff is collapsed.
Click to expand it.
src-tauri/src/main.rs
+
6
−
3
View file @
d75fb152
...
...
@@ -10,7 +10,7 @@ mod config;
mod
events
;
mod
utils
;
use
crate
::{
events
::
state
::
ClashInfoState
,
utils
::
clash
::
put_clash_profile
};
use
crate
::{
events
::
state
,
utils
::
clash
::
put_clash_profile
};
use
std
::
sync
::{
Arc
,
Mutex
};
use
tauri
::{
api
,
CustomMenuItem
,
Manager
,
SystemTray
,
SystemTrayEvent
,
SystemTrayMenu
,
SystemTrayMenuItem
,
...
...
@@ -57,9 +57,11 @@ fn main() -> std::io::Result<()> {
_
=>
{}
})
.invoke_handler
(
tauri
::
generate_handler!
[
cmd
::
import_profile
,
cmd
::
restart_sidebar
,
cmd
::
get_clash_info
,
cmd
::
import_profile
,
cmd
::
get_profiles
,
cmd
::
set_profiles
])
.build
(
tauri
::
generate_context!
())
.expect
(
"error while running tauri application"
);
...
...
@@ -77,7 +79,8 @@ fn main() -> std::io::Result<()> {
};
});
app
.manage
(
ClashInfoState
(
Arc
::
new
(
Mutex
::
new
(
info
))));
app
.manage
(
state
::
ClashInfoState
(
Arc
::
new
(
Mutex
::
new
(
info
))));
app
.manage
(
state
::
ProfileLock
::
default
());
app
.run
(|
app_handle
,
e
|
match
e
{
tauri
::
Event
::
CloseRequested
{
label
,
api
,
..
}
=>
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment