refactor: replace signed out view with explore banner
This commit is contained in:
@@ -1,11 +1,10 @@
|
||||
pub mod helpers;
|
||||
mod placeholder;
|
||||
mod sign_in;
|
||||
|
||||
use gpui::prelude::FluentBuilder as _;
|
||||
use gpui::{
|
||||
AnyView, AppContext as _, Context, Entity, InteractiveElement as _, IntoElement,
|
||||
ParentElement as _, Render, Styled as _, Subscription, Window, div, px,
|
||||
App, AppContext as _, Context, Entity, InteractiveElement as _, IntoElement,
|
||||
ParentElement as _, Render, Styled as _, Subscription, WeakEntity, Window, div,
|
||||
};
|
||||
use helpers::apply_appearance_mode;
|
||||
use settings::SettingsStore;
|
||||
@@ -18,7 +17,6 @@ use ui::layout::titlebar::Titlebar;
|
||||
use ui::toast::Toast;
|
||||
|
||||
use crate::app::placeholder::PlaceholderView;
|
||||
use crate::app::sign_in::SignedOutView;
|
||||
use crate::explore::ExploreView;
|
||||
use crate::identity::passphrase::PassphraseDialog;
|
||||
use crate::shared::log_dropped;
|
||||
@@ -30,9 +28,6 @@ use crate::titlebar::{
|
||||
pub struct AppRoot {
|
||||
panels: Entity<PanelStack>,
|
||||
titlebar: Entity<TitlebarView>,
|
||||
sign_in: Entity<SignedOutView>,
|
||||
explore: Entity<ExploreView>,
|
||||
empty: Entity<PlaceholderView>,
|
||||
passphrase: Option<Entity<PassphraseDialog>>,
|
||||
_subscriptions: Vec<Subscription>,
|
||||
}
|
||||
@@ -44,19 +39,17 @@ impl AppRoot {
|
||||
let app = cx.entity().downgrade();
|
||||
let panels = cx.new(|cx| PanelStack::new("signed-panel", cx));
|
||||
let titlebar = cx.new(|cx| TitlebarView::new(app.clone(), window, cx));
|
||||
let sign_in = cx.new(|_| SignedOutView::new());
|
||||
let explore = cx.new(|cx| ExploreView::new(app.clone(), cx));
|
||||
let empty = cx.new(|_| PlaceholderView::new("Select a repository"));
|
||||
panels.update(cx, |panels, cx| panels.set_empty(empty, None, cx));
|
||||
|
||||
let backend = Backend::global(cx);
|
||||
let signed_in = backend.read(cx).current_user().is_some();
|
||||
let settings = SettingsStore::global(cx);
|
||||
let mut subscriptions = Vec::new();
|
||||
|
||||
subscriptions.push(cx.subscribe_in(
|
||||
&backend,
|
||||
window,
|
||||
|this, backend, event, window, cx| match event {
|
||||
|this, _backend, event, window, cx| match event {
|
||||
BackendEvent::PassphraseRequired => {
|
||||
this.open_passphrase(cx);
|
||||
}
|
||||
@@ -65,18 +58,8 @@ impl AppRoot {
|
||||
}
|
||||
BackendEvent::SignerChanged => {
|
||||
let app = cx.entity().downgrade();
|
||||
let backend = backend.clone();
|
||||
|
||||
window.defer(cx, move |window, cx| {
|
||||
let signed_in = backend.read(cx).current_user().is_some();
|
||||
|
||||
log_dropped(app.update(cx, |this, cx| {
|
||||
this.sync_session(signed_in, cx);
|
||||
}));
|
||||
|
||||
if signed_in {
|
||||
ExploreView::open(&app, window, cx);
|
||||
}
|
||||
Self::reset_to_explore(&app, window, cx)
|
||||
});
|
||||
}
|
||||
_ => {}
|
||||
@@ -88,19 +71,16 @@ impl AppRoot {
|
||||
apply_appearance_mode(settings.appearance, cx);
|
||||
}));
|
||||
|
||||
cx.defer_in(window, move |this, _window, cx| {
|
||||
window.defer(cx, move |window, cx| {
|
||||
if backend.read(cx).passphrase_required() {
|
||||
log_dropped(app.update(cx, |this, cx| this.open_passphrase(cx)));
|
||||
}
|
||||
this.sync_session(signed_in, cx);
|
||||
Self::reset_to_explore(&app, window, cx);
|
||||
});
|
||||
|
||||
Self {
|
||||
panels,
|
||||
titlebar,
|
||||
sign_in,
|
||||
explore,
|
||||
empty,
|
||||
passphrase: None,
|
||||
_subscriptions: subscriptions,
|
||||
}
|
||||
@@ -120,20 +100,13 @@ impl AppRoot {
|
||||
&self.panels
|
||||
}
|
||||
|
||||
/// Empties the stack and sets what it shows until a panel opens.
|
||||
fn sync_session(&mut self, signed_in: bool, cx: &mut Context<Self>) {
|
||||
let main: AnyView = if signed_in {
|
||||
self.empty.clone().into()
|
||||
} else {
|
||||
self.sign_in.clone().into()
|
||||
};
|
||||
|
||||
let split = (!signed_in).then(|| (self.explore.clone().into(), px(300.)));
|
||||
|
||||
self.panels.update(cx, |panels, cx| {
|
||||
panels.clear(cx);
|
||||
panels.set_empty(main, split, cx);
|
||||
/// Empties the stack and opens it on the explore panel, where every session starts.
|
||||
fn reset_to_explore(app: &WeakEntity<Self>, window: &mut Window, cx: &mut App) {
|
||||
let result = app.update(cx, |this, cx| {
|
||||
this.panels.update(cx, |panels, cx| panels.clear(cx))
|
||||
});
|
||||
log_dropped(result);
|
||||
ExploreView::open(app, window, cx);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,105 +0,0 @@
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
use gpui::prelude::FluentBuilder as _;
|
||||
use gpui::{
|
||||
Context, Entity, IntoElement, ObjectFit, ParentElement as _, Render, SharedString, Styled as _,
|
||||
StyledImage as _, Window, div, img,
|
||||
};
|
||||
use ui::button::{Button, ButtonVariant};
|
||||
use ui::common::v_flex;
|
||||
use ui::theme::ActiveTheme as _;
|
||||
|
||||
use crate::identity::import::ImportDialog;
|
||||
use crate::identity::onboarding::OnboardingDialog;
|
||||
|
||||
pub struct SignedOutView {
|
||||
banner: SharedString,
|
||||
onboarding: Option<Entity<OnboardingDialog>>,
|
||||
import: Option<Entity<ImportDialog>>,
|
||||
}
|
||||
|
||||
impl SignedOutView {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
banner: pick_banner(),
|
||||
onboarding: None,
|
||||
import: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn open_onboarding(&mut self, cx: &mut Context<Self>) {
|
||||
self.onboarding = Some(OnboardingDialog::new(cx));
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
fn open_import(&mut self, cx: &mut Context<Self>) {
|
||||
self.import = Some(ImportDialog::new(cx));
|
||||
cx.notify();
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for SignedOutView {
|
||||
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
||||
let theme = cx.theme();
|
||||
let colors = theme.colors;
|
||||
|
||||
v_flex()
|
||||
.size_full()
|
||||
.child(
|
||||
v_flex()
|
||||
.size_full()
|
||||
.relative()
|
||||
.bg(colors.background)
|
||||
.rounded(theme.radius.overlay)
|
||||
.overflow_hidden()
|
||||
.child(
|
||||
div().absolute().inset_0().child(
|
||||
img(self.banner.clone())
|
||||
.size_full()
|
||||
.object_fit(ObjectFit::Cover)
|
||||
.rounded(theme.radius.overlay),
|
||||
),
|
||||
)
|
||||
.child(
|
||||
v_flex()
|
||||
.size_full()
|
||||
.justify_end()
|
||||
.p_4()
|
||||
.gap_6()
|
||||
.child(img("backgrounds/headline.png").max_w_48())
|
||||
.child(
|
||||
v_flex()
|
||||
.gap_2()
|
||||
.w_full()
|
||||
.child(
|
||||
Button::new("join")
|
||||
.label("Join now")
|
||||
.variant(ButtonVariant::Primary)
|
||||
.on_click(cx.listener(|this, _, _, cx| {
|
||||
this.open_onboarding(cx)
|
||||
})),
|
||||
)
|
||||
.child(
|
||||
Button::new("import")
|
||||
.label("Import identity")
|
||||
.variant(ButtonVariant::Secondary)
|
||||
.on_click(
|
||||
cx.listener(|this, _, _, cx| this.open_import(cx)),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
.when_some(self.onboarding.clone(), |el, view| el.child(view))
|
||||
.when_some(self.import.clone(), |el, view| el.child(view))
|
||||
}
|
||||
}
|
||||
|
||||
/// One of the three banner photos, picked per process launch.
|
||||
fn pick_banner() -> SharedString {
|
||||
let nanos = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.map(|now| now.subsec_nanos())
|
||||
.unwrap_or(0);
|
||||
format!("backgrounds/banner{}.jpg", nanos % 3 + 1).into()
|
||||
}
|
||||
@@ -1,3 +1,6 @@
|
||||
use std::sync::LazyLock;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
use gpui::prelude::FluentBuilder as _;
|
||||
use gpui::{
|
||||
AnyElement, Entity, InteractiveElement as _, IntoElement, ParentElement as _, SharedString,
|
||||
@@ -18,6 +21,18 @@ use crate::repo::helpers::{announcement_display_name, upstream_display_name};
|
||||
|
||||
const RECENT_COUNT: usize = 10;
|
||||
|
||||
/// One of the three banner photos, picked per process launch.
|
||||
pub fn pick_banner() -> SharedString {
|
||||
static BANNER: LazyLock<SharedString> = LazyLock::new(|| {
|
||||
let nanos = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.map(|now| now.subsec_nanos())
|
||||
.unwrap_or(0);
|
||||
SharedString::from(format!("backgrounds/banner{}.jpg", nanos % 3 + 1))
|
||||
});
|
||||
BANNER.clone()
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
||||
pub(super) enum RepoFilter {
|
||||
#[default]
|
||||
|
||||
@@ -1,26 +1,33 @@
|
||||
mod helpers;
|
||||
use std::sync::Arc;
|
||||
|
||||
use gpui::{
|
||||
AnyElement, App, AppContext as _, Context, Entity, IntoElement, ListAlignment, ListState,
|
||||
ParentElement as _, Render, SharedString, Styled as _, Subscription, WeakEntity, Window, div,
|
||||
list, px,
|
||||
ObjectFit, ParentElement as _, Pixels, Render, SharedString, Styled as _, StyledImage as _,
|
||||
Subscription, WeakEntity, Window, div, img, list, px,
|
||||
};
|
||||
use helpers::{RepoFilter, render_card};
|
||||
use metadata::ProfileDirectory;
|
||||
use nostr::prelude::PublicKey;
|
||||
use signed_state::RepoListStore;
|
||||
use signed_state::{Backend, RepoListStore};
|
||||
use ui::button::{Button, ButtonVariant};
|
||||
use ui::common::{h_flex, v_flex};
|
||||
use ui::empty_state::EmptyState;
|
||||
use ui::icons::{Icon, IconRole};
|
||||
use ui::input::{Input, InputState, InputStateEvent};
|
||||
use ui::layout::panel::Panel;
|
||||
use ui::layout::panel::{Panel, PanelSlot, SplitSide};
|
||||
use ui::layout::root::RootContext as _;
|
||||
use ui::scrollbar::Scrollbar;
|
||||
use ui::segmented::{Segmented, SegmentedOption};
|
||||
use ui::theme::ActiveTheme as _;
|
||||
|
||||
use crate::app::AppRoot;
|
||||
use crate::explore::helpers::pick_banner;
|
||||
use crate::identity::import::ImportDialog;
|
||||
use crate::identity::onboarding::OnboardingDialog;
|
||||
use crate::shared::panels;
|
||||
|
||||
mod helpers;
|
||||
|
||||
const COLUMNS: usize = 2;
|
||||
// Name row, avatar strip, meta row, and their borders and gaps, in px.
|
||||
const CARD_HEIGHT: f32 = 40. + 64. + 48. + 2. + 6.;
|
||||
@@ -37,6 +44,8 @@ pub struct ExploreView {
|
||||
directory: Entity<ProfileDirectory>,
|
||||
_search_subscription: Subscription,
|
||||
_store_subscription: Subscription,
|
||||
/// Observes the panel stack for the onboarding sidebar, set once this view is in it.
|
||||
_stack_subscription: Option<Subscription>,
|
||||
}
|
||||
|
||||
impl ExploreView {
|
||||
@@ -74,9 +83,13 @@ impl ExploreView {
|
||||
this.rebuild_rows(cx);
|
||||
});
|
||||
|
||||
// The root and the stack are still being updated while this view is built.
|
||||
cx.defer(move |cx| {
|
||||
if let Err(error) = weak.update(cx, |this, cx| this.rebuild_rows(cx)) {
|
||||
log::warn!("failed to load explore rows: {error}");
|
||||
if let Err(error) = weak.update(cx, |this, cx| {
|
||||
this.rebuild_rows(cx);
|
||||
this.watch_stack(cx);
|
||||
}) {
|
||||
log::warn!("failed to set up the explore panel: {error}");
|
||||
}
|
||||
});
|
||||
|
||||
@@ -90,6 +103,7 @@ impl ExploreView {
|
||||
directory,
|
||||
_search_subscription: search_subscription,
|
||||
_store_subscription: store_subscription,
|
||||
_stack_subscription: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,6 +138,50 @@ impl ExploreView {
|
||||
}
|
||||
}
|
||||
|
||||
/// Observes the panel stack so the onboarding sidebar follows the shown panel.
|
||||
fn watch_stack(&mut self, cx: &mut Context<Self>) {
|
||||
let Ok(stack) = self
|
||||
.workspace
|
||||
.read_with(cx, |workspace, _| workspace.panels().clone())
|
||||
else {
|
||||
log::warn!("failed to watch the panel stack: the app root is gone");
|
||||
return;
|
||||
};
|
||||
self._stack_subscription =
|
||||
Some(cx.observe(&stack, |this, _stack, cx| this.sync_sidebar(cx)));
|
||||
self.sync_sidebar(cx);
|
||||
}
|
||||
|
||||
/// Shows the onboarding sidebar while signed out and an explore panel is shown, and closes it otherwise.
|
||||
fn sync_sidebar(&mut self, cx: &mut Context<Self>) {
|
||||
let signed_out = Backend::global(cx).read(cx).current_user().is_none();
|
||||
let explore = SharedString::from(Self::PANEL_ID);
|
||||
let sidebar = SharedString::from(OnboardingSidebar::PANEL_ID);
|
||||
|
||||
let result = self.workspace.update(cx, |workspace, cx| {
|
||||
workspace.panels().clone().update(cx, |stack, cx| {
|
||||
let shown = signed_out && stack.is_active(PanelSlot::Main, &explore);
|
||||
|
||||
if shown && !stack.is_open(PanelSlot::Split, &sidebar) {
|
||||
let view = Arc::new(cx.new(|_| OnboardingSidebar));
|
||||
stack.insert_split_at(
|
||||
sidebar.clone(),
|
||||
OnboardingSidebar::WIDTH,
|
||||
SplitSide::Leading,
|
||||
view,
|
||||
cx,
|
||||
);
|
||||
} else if !shown {
|
||||
stack.close(PanelSlot::Split, &sidebar, cx);
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
if let Err(error) = result {
|
||||
log::warn!("failed to update the onboarding sidebar: {error}");
|
||||
}
|
||||
}
|
||||
|
||||
fn render_header(
|
||||
search: Entity<InputState>,
|
||||
filter: RepoFilter,
|
||||
@@ -260,3 +318,72 @@ impl Render for ExploreView {
|
||||
.child(Scrollbar::vertical("explore-scroll", scroll_handle))
|
||||
}
|
||||
}
|
||||
|
||||
/// The onboarding sidebar shown beside the explore panel while signed out.
|
||||
struct OnboardingSidebar;
|
||||
|
||||
impl OnboardingSidebar {
|
||||
/// The identifier of the sidebar in the panel stack; shown as its tab label.
|
||||
const PANEL_ID: &'static str = "Onboarding";
|
||||
/// The width the sidebar opens at.
|
||||
const WIDTH: Pixels = px(300.);
|
||||
}
|
||||
|
||||
impl Panel for OnboardingSidebar {
|
||||
fn title(&self, _cx: &App) -> SharedString {
|
||||
SharedString::from(Self::PANEL_ID)
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for OnboardingSidebar {
|
||||
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
||||
let theme = cx.theme();
|
||||
let colors = theme.colors;
|
||||
|
||||
v_flex()
|
||||
.size_full()
|
||||
.relative()
|
||||
.bg(colors.background)
|
||||
.rounded(theme.radius.overlay)
|
||||
.overflow_hidden()
|
||||
.child(
|
||||
div().absolute().inset_0().child(
|
||||
img(pick_banner())
|
||||
.size_full()
|
||||
.object_fit(ObjectFit::Cover)
|
||||
.rounded(theme.radius.overlay),
|
||||
),
|
||||
)
|
||||
.child(
|
||||
v_flex()
|
||||
.size_full()
|
||||
.justify_end()
|
||||
.p_4()
|
||||
.gap_6()
|
||||
.child(img("backgrounds/headline.png").max_w_48())
|
||||
.child(
|
||||
v_flex()
|
||||
.gap_2()
|
||||
.w_full()
|
||||
.child(
|
||||
Button::new("join")
|
||||
.label("Join now")
|
||||
.variant(ButtonVariant::Primary)
|
||||
.on_click(|_, _, cx| {
|
||||
let dialog = OnboardingDialog::new(cx);
|
||||
cx.open_dialog(dialog)
|
||||
}),
|
||||
)
|
||||
.child(
|
||||
Button::new("import")
|
||||
.label("Import identity")
|
||||
.variant(ButtonVariant::Secondary)
|
||||
.on_click(|_, _, cx| {
|
||||
let dialog = ImportDialog::new(cx);
|
||||
cx.open_dialog(dialog)
|
||||
}),
|
||||
),
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user