mirror of
https://github.com/modrinth/code.git
synced 2026-08-27 01:54:47 +00:00
refactor: app event bus (#6985)
* refactor: app event bus * fix: use messagepack + cleanup * fix: use postcard * fix: lint * fix: import gate * fix: prettierignore + lint * fix: lint * fix: rev
This commit is contained in:
+13
-9
@@ -3,9 +3,9 @@ use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::time::{Duration, Instant};
|
||||
use tauri::plugin::TauriPlugin;
|
||||
use tauri::{Emitter, Manager, PhysicalPosition, PhysicalSize, Runtime};
|
||||
use tauri::{Manager, PhysicalPosition, PhysicalSize, Runtime};
|
||||
use tauri_plugin_opener::OpenerExt;
|
||||
use theseus::settings;
|
||||
use theseus::{AppEvent, EventState, settings};
|
||||
use tokio::sync::RwLock;
|
||||
|
||||
pub struct AdsState {
|
||||
@@ -20,7 +20,6 @@ pub struct AdsState {
|
||||
}
|
||||
|
||||
const AD_LINK: &str = "https://modrinth.com/wrapper/app-ads-cookie";
|
||||
const ADS_CONSENT_REQUIRED_EVENT: &str = "ads-consent-required";
|
||||
const APP_TITLE_BAR_HEIGHT: f32 = 48.0;
|
||||
#[cfg(any(windows, target_os = "macos"))]
|
||||
pub(super) const OCCLUDED_AREA_THRESHOLD: f64 = 0.5;
|
||||
@@ -38,6 +37,12 @@ const ADS_USER_AGENT: &str = concat!(
|
||||
" (Modrinth App)",
|
||||
);
|
||||
|
||||
fn emit_ads_consent_required(required: bool) {
|
||||
EventState::get()
|
||||
.send(AppEvent::AdsConsentRequired(required))
|
||||
.ok();
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn ads_user_agent_override_params() -> String {
|
||||
serde_json::json!({
|
||||
@@ -666,7 +671,7 @@ pub async fn init_ads_window<R: Runtime>(
|
||||
&& state.consent_required
|
||||
&& state.consent_notification_enabled
|
||||
{
|
||||
app.emit_to("main", ADS_CONSENT_REQUIRED_EVENT, true).ok();
|
||||
emit_ads_consent_required(true);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -704,7 +709,7 @@ pub async fn update_ads_window_hold<R: Runtime>(
|
||||
&& state.consent_required
|
||||
&& state.consent_notification_enabled
|
||||
{
|
||||
app.emit_to("main", ADS_CONSENT_REQUIRED_EVENT, true).ok();
|
||||
emit_ads_consent_required(true);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -730,7 +735,7 @@ pub async fn hide_ads_window<R: Runtime>(
|
||||
}
|
||||
|
||||
if reset {
|
||||
app.emit_to("main", ADS_CONSENT_REQUIRED_EVENT, false).ok();
|
||||
emit_ads_consent_required(false);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -761,8 +766,7 @@ pub async fn show_ads_consent_ui<R: Runtime>(
|
||||
)?;
|
||||
}
|
||||
|
||||
app.emit_to("main", ADS_CONSENT_REQUIRED_EVENT, show_notification)
|
||||
.ok();
|
||||
emit_ads_consent_required(show_notification);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -841,7 +845,7 @@ pub async fn finish_ads_consent_flow<R: Runtime>(
|
||||
}
|
||||
}
|
||||
|
||||
app.emit_to("main", ADS_CONSENT_REQUIRED_EVENT, false).ok();
|
||||
emit_ads_consent_required(false);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
+12
-2
@@ -25,9 +25,12 @@ mod updater_impl_noop;
|
||||
// Should be called in launcher initialization
|
||||
#[tracing::instrument(skip_all)]
|
||||
#[tauri::command]
|
||||
async fn initialize_state(app: tauri::AppHandle) -> api::Result<()> {
|
||||
async fn initialize_state(
|
||||
app: tauri::AppHandle,
|
||||
events: tauri::ipc::Channel<tauri::ipc::InvokeResponseBody>,
|
||||
) -> api::Result<()> {
|
||||
tracing::info!("Initializing app event state...");
|
||||
theseus::EventState::init(app.clone()).await?;
|
||||
theseus::EventState::init(app.clone(), events).await?;
|
||||
|
||||
tracing::info!("Initializing app state...");
|
||||
State::init(app.config().identifier.clone()).await?;
|
||||
@@ -111,6 +114,13 @@ async fn set_restart_after_pending_update(
|
||||
// if Tauri app is called with arguments, then those arguments will be treated as commands
|
||||
// ie: deep links or filepaths for .mrpacks
|
||||
fn main() {
|
||||
#[cfg(feature = "export-app-events")]
|
||||
theseus::export_app_event_bindings(
|
||||
std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
||||
.join("../app-frontend/src/generated/app-events"),
|
||||
)
|
||||
.expect("failed to export app event TypeScript bindings");
|
||||
|
||||
/*
|
||||
tracing is set basd on the environment variable RUST_LOG=xxx, depending on the amount of logs to show
|
||||
ERROR > WARN > INFO > DEBUG > TRACE
|
||||
|
||||
Reference in New Issue
Block a user