chore: restructure ui components
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
mod helpers;
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use gpui::prelude::FluentBuilder as _;
|
||||
use gpui::{
|
||||
App, AppContext as _, Context, Entity, IntoElement, ParentElement as _, Render, Styled as _,
|
||||
Window, px,
|
||||
App, AppContext as _, Context, ElementId, Entity, IntoElement, ParentElement as _, Render,
|
||||
SharedString, Styled as _, Subscription, Task, Window, px,
|
||||
};
|
||||
use helpers::{
|
||||
appearance_select, default_folder_selector, event_fetching_select, grasp_servers_editor,
|
||||
@@ -16,10 +18,38 @@ use ui::dialog::{Dialog, DialogHandle};
|
||||
use ui::input::InputState;
|
||||
use ui::separator::Separator;
|
||||
|
||||
use crate::shared::labels::{RelayHostExt as _, ServerInputExt as _};
|
||||
|
||||
/// One grasp server row, with its display strings built when settings change.
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct ServerRow {
|
||||
pub(crate) id: ElementId,
|
||||
/// The stored server URL.
|
||||
pub(crate) url: String,
|
||||
/// The host shown on the row.
|
||||
pub(crate) host: SharedString,
|
||||
}
|
||||
|
||||
/// One scan path row, with its label built when settings change.
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct PathRow {
|
||||
pub(crate) id: ElementId,
|
||||
pub(crate) path: PathBuf,
|
||||
pub(crate) label: SharedString,
|
||||
}
|
||||
|
||||
pub struct SettingsDialog {
|
||||
handle: DialogHandle,
|
||||
grasp_input: Entity<InputState>,
|
||||
default_folder: Entity<InputState>,
|
||||
/// Grasp server rows, rebuilt when the settings change.
|
||||
servers: Vec<ServerRow>,
|
||||
/// Scan path rows, rebuilt when the settings change.
|
||||
scan_paths: Vec<PathRow>,
|
||||
/// Prompts this dialog started, cancelled when it drops.
|
||||
tasks: Vec<Task<()>>,
|
||||
/// Rebuilds the rows when the settings change.
|
||||
_subscription: Option<Subscription>,
|
||||
}
|
||||
|
||||
impl SettingsDialog {
|
||||
@@ -39,16 +69,70 @@ impl SettingsDialog {
|
||||
.read_only()
|
||||
.build(cx);
|
||||
|
||||
cx.new(|_| Self {
|
||||
handle: DialogHandle::new(true),
|
||||
grasp_input,
|
||||
default_folder,
|
||||
})
|
||||
let dialog = cx.new(|cx| {
|
||||
let mut dialog = Self {
|
||||
handle: DialogHandle::new(true),
|
||||
grasp_input,
|
||||
default_folder,
|
||||
servers: Vec::new(),
|
||||
scan_paths: Vec::new(),
|
||||
tasks: Vec::new(),
|
||||
_subscription: None,
|
||||
};
|
||||
dialog.rebuild_rows(cx);
|
||||
dialog
|
||||
});
|
||||
|
||||
dialog.update(cx, |dialog, cx| {
|
||||
dialog._subscription = Some(cx.observe(&store, |this, _store, cx| {
|
||||
this.rebuild_rows(cx);
|
||||
cx.notify();
|
||||
}));
|
||||
});
|
||||
|
||||
dialog
|
||||
}
|
||||
|
||||
/// Rebuilds the row caches from the current settings.
|
||||
fn rebuild_rows(&mut self, cx: &App) {
|
||||
let store = SettingsStore::global(cx);
|
||||
let settings = store.read(cx).settings();
|
||||
|
||||
self.servers = settings
|
||||
.grasp_servers
|
||||
.default_servers
|
||||
.iter()
|
||||
.map(|url| ServerRow {
|
||||
id: ElementId::from((
|
||||
ElementId::from("settings-remove-server"),
|
||||
SharedString::from(url.clone()),
|
||||
)),
|
||||
url: url.clone(),
|
||||
host: url.normalize_server().map_or_else(
|
||||
|| SharedString::from(url.clone()),
|
||||
|(_, relay)| relay.host_label(),
|
||||
),
|
||||
})
|
||||
.collect();
|
||||
|
||||
self.scan_paths = settings
|
||||
.local_repos
|
||||
.scan_paths
|
||||
.iter()
|
||||
.map(|path| PathRow {
|
||||
id: ElementId::from((
|
||||
ElementId::from("settings-remove-path"),
|
||||
SharedString::from(path.display().to_string()),
|
||||
)),
|
||||
path: path.clone(),
|
||||
label: SharedString::from(path.display().to_string()),
|
||||
})
|
||||
.collect();
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for SettingsDialog {
|
||||
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
||||
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
||||
const APPEARANCE_DESC: &str =
|
||||
"Choose whether the app follows the system theme or uses a light/dark theme.";
|
||||
const FETCH_DESC: &str = "Curated fetches events from relays in the repository's announcement. Uncensored also fetches from every maintainer's relays.";
|
||||
@@ -59,6 +143,9 @@ impl Render for SettingsDialog {
|
||||
|
||||
let grasp_input = self.grasp_input.clone();
|
||||
let default_folder = self.default_folder.clone();
|
||||
let servers = self.servers.clone();
|
||||
let scan_paths = self.scan_paths.clone();
|
||||
let this = cx.weak_entity();
|
||||
|
||||
Dialog::new("settings-dialog")
|
||||
.handle(self.handle.clone())
|
||||
@@ -66,7 +153,8 @@ impl Render for SettingsDialog {
|
||||
.height(px(560.))
|
||||
.title("Settings")
|
||||
.content(move |_, cx| {
|
||||
let error = SettingsStore::global(cx).read(cx).error.clone();
|
||||
let store = SettingsStore::global(cx);
|
||||
let error = store.read(cx).error.clone();
|
||||
|
||||
v_flex()
|
||||
.gap_4()
|
||||
@@ -97,20 +185,20 @@ impl Render for SettingsDialog {
|
||||
cx,
|
||||
"Grasp Servers",
|
||||
GRASP_DESC,
|
||||
grasp_servers_editor(&grasp_input, cx),
|
||||
grasp_servers_editor(&grasp_input, &servers, cx),
|
||||
))
|
||||
.child(Separator::horizontal())
|
||||
.child(setting_block(
|
||||
cx,
|
||||
"Scan Directories",
|
||||
SCAN_DESC,
|
||||
scan_paths_editor(cx),
|
||||
scan_paths_editor(&this, &scan_paths, cx),
|
||||
))
|
||||
.child(setting_block(
|
||||
cx,
|
||||
"Default Folder",
|
||||
FOLDER_DESC,
|
||||
default_folder_selector(&default_folder),
|
||||
default_folder_selector(&default_folder, &this),
|
||||
))
|
||||
.into_any_element()
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user