add GetState rpc

This commit is contained in:
llama 2025-10-20 09:58:29 +08:00
parent 0d1204521b
commit bcd28c03ec
No known key found for this signature in database
GPG key ID: 0B7543854B9413C3
5 changed files with 41 additions and 4 deletions

View file

@ -18,6 +18,8 @@ service LauncherService {
rpc WindowReady(generic.Empty) returns (generic.Empty);
rpc ZoomWebview(ZoomWebviewRequest) returns (generic.Empty);
rpc GetState(generic.Empty) returns (State);
rpc GetAvailableVersions(generic.Empty) returns (Versions);
rpc GetExistingVersions(generic.Empty) returns (ExistingVersions);
@ -109,3 +111,16 @@ message ExistingVersions {
optional Version previous = 2;
bool pyproject_modified_by_user = 3;
}
message NormalState {
Options options = 1;
}
message State {
oneof kind {
string os_unsupported = 1;
string unknown_error = 2;
NormalState normal = 3;
google.protobuf.Empty uninstall = 4;
}
}

View file

@ -20,7 +20,7 @@ use crate::uv;
pub const PROTOCOL: &str = "anki";
pub fn init() -> Option<State> {
let mut state = State::init().unwrap_or_else(State::Error);
let mut state = State::init().unwrap_or_else(State::UnknownError);
match state {
State::Normal(ref mut state) => state.check_versions(),

View file

@ -4,13 +4,16 @@
use anki_proto::generic;
use anki_proto::launcher::get_langs_response;
use anki_proto::launcher::get_mirrors_response;
use anki_proto::launcher::state::Kind as StateProtoKind;
use anki_proto::launcher::ChooseVersionRequest;
use anki_proto::launcher::ChooseVersionResponse;
use anki_proto::launcher::GetLangsResponse;
use anki_proto::launcher::GetMirrorsResponse;
use anki_proto::launcher::I18nResourcesRequest;
use anki_proto::launcher::Mirror;
use anki_proto::launcher::NormalState as NormalStateProto;
use anki_proto::launcher::Options;
use anki_proto::launcher::State as StateProto;
use anki_proto::launcher::ZoomWebviewRequest;
use anyhow::anyhow;
use anyhow::Context;
@ -88,6 +91,23 @@ pub async fn set_lang<R: Runtime>(
Ok(())
}
pub async fn get_state<R: Runtime>(
app: AppHandle<R>,
_window: WebviewWindow<R>,
) -> Result<StateProto> {
let state = app.state::<State>();
let kind = match &*state {
State::LaunchAnki(_) => unreachable!(),
State::OsUnsupported(e) => StateProtoKind::OsUnsupported(format!("{e:?}")),
State::UnknownError(e) => StateProtoKind::UnknownError(format!("{e:?}")),
State::Uninstall(_) => StateProtoKind::Uninstall(()),
State::Normal(normal) => StateProtoKind::Normal(NormalStateProto {
options: Some((&normal.initial_options).into()),
}),
};
Ok(StateProto { kind: Some(kind) })
}
pub async fn get_mirrors<R: Runtime>(
app: AppHandle<R>,
_window: WebviewWindow<R>,

View file

@ -73,8 +73,8 @@ impl From<NormalState> for State {
pub enum State {
LaunchAnki(Arc<uv::Paths>),
#[allow(dead_code)] // TODO: use
Error(anyhow::Error),
OsUnsupported(anyhow::Error),
UnknownError(anyhow::Error),
Uninstall(Arc<uv::Paths>),
Normal(NormalState),
}

View file

@ -734,7 +734,9 @@ impl crate::state::State {
let _ = remove_file(&paths.launcher_trigger_file);
}
ensure_os_supported()?;
if let Err(e) = ensure_os_supported() {
return Ok(Self::OsUnsupported(e));
}
Ok(Self::Normal(paths.into()))
}