feat: add re-open window on macos

This commit is contained in:
2025-02-11 09:56:19 +07:00
parent f5f9b66df5
commit 76d2c14870

View File

@@ -8,12 +8,13 @@ use common::{
profile::NostrProfile, profile::NostrProfile,
}; };
use gpui::{ use gpui::{
actions, point, px, size, App, AppContext, Application, BorrowAppContext, Bounds, Menu, actions, point, px, size, App, AppContext, Application, AsyncApp, BorrowAppContext, Bounds,
MenuItem, SharedString, TitlebarOptions, WindowBounds, WindowKind, WindowOptions, KeyBinding, Menu, MenuItem, SharedString, TitlebarOptions, WindowBounds, WindowKind,
WindowOptions,
}; };
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
use gpui::{WindowBackgroundAppearance, WindowDecorations}; use gpui::{WindowBackgroundAppearance, WindowDecorations};
use log::error; use log::{error, info};
use nostr_sdk::prelude::*; use nostr_sdk::prelude::*;
use state::{get_client, initialize_client}; use state::{get_client, initialize_client};
use std::{borrow::Cow, collections::HashSet, ops::Deref, str::FromStr, sync::Arc, time::Duration}; use std::{borrow::Cow, collections::HashSet, ops::Deref, str::FromStr, sync::Arc, time::Duration};
@@ -35,10 +36,7 @@ pub enum Signal {
} }
fn main() { fn main() {
// Log // Initialize Nostr client
tracing_subscriber::fmt::init();
// Initialize nostr client
initialize_client(); initialize_client();
// Get client // Get client
@@ -210,18 +208,57 @@ fn main() {
} }
}); });
Application::new() let app = Application::new()
.with_assets(Assets) .with_assets(Assets)
.with_http_client(Arc::new(reqwest_client::ReqwestClient::new())) .with_http_client(Arc::new(reqwest_client::ReqwestClient::new()));
.run(move |cx| {
app.on_reopen(move |cx| {
let client = get_client();
let (tx, rx) = oneshot::channel::<Option<NostrProfile>>();
cx.spawn(|mut cx| async move {
cx.background_spawn(async move {
if let Ok(signer) = client.signer().await {
if let Ok(public_key) = signer.get_public_key().await {
let metadata = if let Ok(Some(metadata)) =
client.database().metadata(public_key).await
{
metadata
} else {
Metadata::new()
};
_ = tx.send(Some(NostrProfile::new(public_key, metadata)));
} else {
_ = tx.send(None);
}
} else {
_ = tx.send(None);
}
})
.detach();
if let Ok(result) = rx.await {
_ = restore_window(result, &mut cx).await;
}
})
.detach();
});
app.run(move |cx| {
// Initialize chat global state // Initialize chat global state
ChatRegistry::set_global(cx); ChatRegistry::set_global(cx);
// Initialize components // Initialize components
ui::init(cx); ui::init(cx);
// Bring the app to the foreground
cx.activate(true); cx.activate(true);
// Register the `quit` function
cx.on_action(quit); cx.on_action(quit);
// Register the `quit` function with CMD+Q
cx.bind_keys([KeyBinding::new("cmd-q", Quit, None)]);
// Set menu items
cx.set_menus(vec![Menu { cx.set_menus(vec![Menu {
name: "Coop".into(), name: "Coop".into(),
items: vec![MenuItem::action("Quit", Quit)], items: vec![MenuItem::action("Quit", Quit)],
@@ -313,24 +350,20 @@ fn main() {
while let Some(signal) = signal_rx.recv().await { while let Some(signal) = signal_rx.recv().await {
match signal { match signal {
Signal::Eose => { Signal::Eose => {
if let Err(e) = if let Err(e) = cx.update_window(*window.deref(), |_this, window, cx| {
cx.update_window(*window.deref(), |_this, window, cx| {
cx.update_global::<ChatRegistry, _>(|this, cx| { cx.update_global::<ChatRegistry, _>(|this, cx| {
this.load(window, cx); this.load(window, cx);
}); });
}) }) {
{
error!("Error: {}", e) error!("Error: {}", e)
} }
} }
Signal::Event(event) => { Signal::Event(event) => {
if let Err(e) = if let Err(e) = cx.update_window(*window.deref(), |_this, window, cx| {
cx.update_window(*window.deref(), |_this, window, cx| {
cx.update_global::<ChatRegistry, _>(|this, cx| { cx.update_global::<ChatRegistry, _>(|this, cx| {
this.new_room_message(event, window, cx); this.new_room_message(event, window, cx);
}); });
}) }) {
{
error!("Error: {}", e) error!("Error: {}", e)
} }
} }
@@ -341,6 +374,59 @@ fn main() {
}); });
} }
async fn restore_window(profile: Option<NostrProfile>, cx: &mut AsyncApp) -> Result<()> {
let opts = cx
.update(|cx| WindowOptions {
#[cfg(not(target_os = "linux"))]
titlebar: Some(TitlebarOptions {
title: Some(SharedString::new_static(APP_NAME)),
traffic_light_position: Some(point(px(9.0), px(9.0))),
appears_transparent: true,
}),
window_bounds: Some(WindowBounds::Windowed(Bounds::centered(
None,
size(px(900.0), px(680.0)),
cx,
))),
#[cfg(target_os = "linux")]
window_background: WindowBackgroundAppearance::Transparent,
#[cfg(target_os = "linux")]
window_decorations: Some(WindowDecorations::Client),
kind: WindowKind::Normal,
..Default::default()
})
.expect("Failed to set window options.");
if let Some(profile) = profile {
_ = cx.open_window(opts, |window, cx| {
window.set_window_title(APP_NAME);
window.set_app_id(APP_ID);
window
.observe_window_appearance(|window, cx| {
Theme::sync_system_appearance(Some(window), cx);
})
.detach();
cx.new(|cx| Root::new(app::init(profile, window, cx).into(), window, cx))
});
} else {
_ = cx.open_window(opts, |window, cx| {
window.set_window_title(APP_NAME);
window.set_app_id(APP_ID);
window
.observe_window_appearance(|window, cx| {
Theme::sync_system_appearance(Some(window), cx);
})
.detach();
cx.new(|cx| Root::new(onboarding::init(window, cx).into(), window, cx))
});
};
Ok(())
}
fn quit(_: &Quit, cx: &mut App) { fn quit(_: &Quit, cx: &mut App) {
info!("Gracefully quitting the application . . .");
cx.quit(); cx.quit();
} }