add theme selector
Some checks failed
Rust / build (ubuntu-latest, stable) (push) Failing after 1m42s
Rust / build (ubuntu-latest, stable) (pull_request) Failing after 1m57s
Rust / build (macos-latest, stable) (push) Has been cancelled
Rust / build (windows-latest, stable) (push) Has been cancelled
Rust / build (macos-latest, stable) (pull_request) Has been cancelled
Rust / build (windows-latest, stable) (pull_request) Has been cancelled

This commit is contained in:
2026-02-26 10:48:06 +07:00
parent cba3f976c6
commit bd1910ce03
12 changed files with 926 additions and 26 deletions

View File

@@ -4,7 +4,7 @@ use gpui::{
Styled, Window,
};
use settings::{AppSettings, AuthMode};
use theme::ActiveTheme;
use theme::{ActiveTheme, ThemeMode};
use ui::button::{Button, ButtonVariants};
use ui::group_box::{GroupBox, GroupBoxVariants};
use ui::input::{InputState, TextInput};
@@ -53,11 +53,15 @@ impl Render for Preferences {
"When opening a request, a popup will appear to help you identify the sender.";
const AVATAR: &str =
"Hide all avatar pictures to improve performance and protect your privacy.";
const AUTH: &str = "Authentication before send events for some relays.";
const MODE: &str =
"Choose whether to use the selected light or dark theme, or to follow the OS.";
const AUTH: &str = "Choose the authentication behavior for relays.";
const RESET: &str = "Reset the theme to the default one.";
let screening = AppSettings::get_screening(cx);
let hide_avatar = AppSettings::get_hide_avatar(cx);
let auth_mode = AppSettings::get_auth_mode(cx);
let theme_mode = AppSettings::get_theme_mode(cx);
v_flex()
.gap_4()
@@ -131,6 +135,78 @@ impl Render for Preferences {
),
),
)
.child(
GroupBox::new()
.id("appearance")
.title("Appearance")
.fill()
.child(
h_flex()
.gap_3()
.justify_between()
.child(
v_flex()
.child(div().text_sm().child(SharedString::from("Mode")))
.child(
div()
.text_xs()
.text_color(cx.theme().text_muted)
.child(SharedString::from(MODE)),
),
)
.child(
Button::new("theme-mode")
.label(theme_mode.name())
.ghost_alt()
.small()
.dropdown_menu(|this, _window, _cx| {
this.min_w(px(256.))
.item(PopupMenuItem::new("Light").on_click(
|_ev, _window, cx| {
AppSettings::update_theme_mode(
ThemeMode::Light,
cx,
);
},
))
.item(PopupMenuItem::new("Dark").on_click(
|_ev, _window, cx| {
AppSettings::update_theme_mode(
ThemeMode::Dark,
cx,
);
},
))
}),
),
)
.child(
h_flex()
.gap_3()
.justify_between()
.child(
v_flex()
.child(div().text_sm().child(SharedString::from("Reset theme")))
.child(
div()
.text_xs()
.text_color(cx.theme().text_muted)
.child(SharedString::from(RESET)),
),
)
.child(
Button::new("reset")
.label("Reset")
.ghost_alt()
.small()
.on_click(move |_ev, window, cx| {
AppSettings::global(cx).update(cx, |this, cx| {
this.reset_theme(window, cx);
})
}),
),
),
)
.child(
GroupBox::new()
.id("media")

View File

@@ -88,7 +88,7 @@ fn main() {
device::init(window, cx);
// Initialize settings
settings::init(cx);
settings::init(window, cx);
// Initialize relay auth registry
relay_auth::init(window, cx);

View File

@@ -1,5 +1,6 @@
use std::sync::Arc;
use ::settings::AppSettings;
use chat::{ChatEvent, ChatRegistry, InboxState};
use gpui::prelude::FluentBuilder;
use gpui::{
@@ -10,7 +11,7 @@ use person::PersonRegistry;
use serde::Deserialize;
use smallvec::{smallvec, SmallVec};
use state::{NostrRegistry, RelayState};
use theme::{ActiveTheme, Theme, SIDEBAR_WIDTH};
use theme::{ActiveTheme, Theme, ThemeRegistry, SIDEBAR_WIDTH};
use title_bar::TitleBar;
use ui::avatar::Avatar;
use ui::button::{Button, ButtonVariants};
@@ -271,10 +272,91 @@ impl Workspace {
this.ensure_messaging_relays(cx);
});
}
Command::ToggleTheme => {}
Command::ToggleTheme => {
self.theme_selector(window, cx);
}
}
}
fn theme_selector(&mut self, window: &mut Window, cx: &mut Context<Self>) {
window.open_modal(cx, move |this, _window, cx| {
let registry = ThemeRegistry::global(cx);
let themes = registry.read(cx).themes();
this.width(px(520.))
.show_close(true)
.title("Select theme")
.pb_4()
.child(v_flex().gap_2().w_full().children({
let mut items = vec![];
for (ix, (path, theme)) in themes.iter().enumerate() {
items.push(
h_flex()
.group("")
.px_2()
.h_8()
.w_full()
.justify_between()
.rounded(cx.theme().radius)
.hover(|this| this.bg(cx.theme().elevated_surface_background))
.child(
h_flex()
.gap_1p5()
.flex_1()
.text_sm()
.child(theme.name.clone())
.child(
div()
.text_xs()
.italic()
.text_color(cx.theme().text_muted)
.child(theme.author.clone()),
),
)
.child(
h_flex()
.gap_1()
.invisible()
.group_hover("", |this| this.visible())
.child(
Button::new(format!("url-{ix}"))
.icon(IconName::Link)
.ghost()
.small()
.on_click({
let theme = theme.clone();
move |_ev, _window, cx| {
cx.open_url(&theme.url);
}
}),
)
.child(
Button::new(format!("set-{ix}"))
.icon(IconName::Check)
.primary()
.small()
.on_click({
let path = path.clone();
move |_ev, window, cx| {
let settings = AppSettings::global(cx);
let path = path.clone();
settings.update(cx, |this, cx| {
this.set_theme(path, window, cx);
})
}
}),
),
),
);
}
items
}))
});
}
fn titlebar_left(&mut self, _window: &mut Window, cx: &Context<Self>) -> impl IntoElement {
let nostr = NostrRegistry::global(cx);
let signer = nostr.read(cx).signer();

View File

@@ -5,6 +5,7 @@ edition.workspace = true
publish.workspace = true
[dependencies]
theme = { path = "../theme" }
common = { path = "../common" }
nostr-sdk.workspace = true

View File

@@ -1,15 +1,17 @@
use std::collections::{HashMap, HashSet};
use std::fmt::Display;
use std::rc::Rc;
use anyhow::{anyhow, Error};
use common::config_dir;
use gpui::{App, AppContext, Context, Entity, Global, Subscription, Task};
use gpui::{App, AppContext, Context, Entity, Global, Subscription, Task, Window};
use nostr_sdk::prelude::*;
use serde::{Deserialize, Serialize};
use smallvec::{smallvec, SmallVec};
use theme::{Theme, ThemeFamily, ThemeMode};
pub fn init(cx: &mut App) {
AppSettings::set_global(cx.new(AppSettings::new), cx)
pub fn init(window: &mut Window, cx: &mut App) {
AppSettings::set_global(cx.new(|cx| AppSettings::new(window, cx)), cx)
}
macro_rules! setting_accessors {
@@ -34,6 +36,8 @@ macro_rules! setting_accessors {
}
setting_accessors! {
pub theme: Option<String>,
pub theme_mode: ThemeMode,
pub hide_avatar: bool,
pub screening: bool,
pub auth_mode: AuthMode,
@@ -53,7 +57,7 @@ pub enum AuthMode {
impl Display for AuthMode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
AuthMode::Auto => write!(f, "Auto authentication"),
AuthMode::Auto => write!(f, "Auto"),
AuthMode::Manual => write!(f, "Ask every time"),
}
}
@@ -114,6 +118,12 @@ impl RoomConfig {
/// Settings
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Settings {
/// Theme
pub theme: Option<String>,
/// Theme mode
pub theme_mode: ThemeMode,
/// Hide user avatars
pub hide_avatar: bool,
@@ -136,6 +146,8 @@ pub struct Settings {
impl Default for Settings {
fn default() -> Self {
Self {
theme: None,
theme_mode: ThemeMode::default(),
hide_avatar: false,
screening: true,
auth_mode: AuthMode::default(),
@@ -176,7 +188,7 @@ impl AppSettings {
cx.set_global(GlobalAppSettings(state));
}
fn new(cx: &mut Context<Self>) -> Self {
fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
let mut subscriptions = smallvec![];
subscriptions.push(
@@ -186,12 +198,9 @@ impl AppSettings {
}),
);
cx.defer(|cx| {
let settings = AppSettings::global(cx);
settings.update(cx, |this, cx| {
this.load(cx);
});
// Run at the end of current cycle
cx.defer_in(window, |this, window, cx| {
this.load(window, cx);
});
Self {
@@ -207,7 +216,7 @@ impl AppSettings {
}
/// Load settings
fn load(&mut self, cx: &mut Context<Self>) {
fn load(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let task: Task<Result<Settings, Error>> = cx.background_spawn(async move {
let path = config_dir().join(".settings");
@@ -218,12 +227,13 @@ impl AppSettings {
}
});
cx.spawn(async move |this, cx| {
cx.spawn_in(window, async move |this, cx| {
let settings = task.await.unwrap_or(Settings::default());
// Update settings
this.update(cx, |this, cx| {
this.update_in(cx, |this, window, cx| {
this.set_settings(settings, cx);
this.apply_theme(window, cx);
})
.ok();
})
@@ -247,6 +257,36 @@ impl AppSettings {
task.detach();
}
/// Set theme
pub fn set_theme<T>(&mut self, theme: T, window: &mut Window, cx: &mut Context<Self>)
where
T: Into<String>,
{
// Update settings
self.values.theme = Some(theme.into());
cx.notify();
// Apply the new theme
self.apply_theme(window, cx);
}
/// Apply theme
pub fn apply_theme(&mut self, window: &mut Window, cx: &mut Context<Self>) {
if let Some(name) = self.values.theme.as_ref() {
if let Ok(new_theme) = ThemeFamily::from_assets(name) {
Theme::apply_theme(Rc::new(new_theme), Some(window), cx);
}
} else {
Theme::apply_theme(Rc::new(ThemeFamily::default()), Some(window), cx);
}
}
/// Reset theme
pub fn reset_theme(&mut self, window: &mut Window, cx: &mut Context<Self>) {
self.values.theme = None;
self.apply_theme(window, cx);
}
/// Check if the given relay is already authenticated
pub fn trusted_relay(&self, url: &RelayUrl, _cx: &App) -> bool {
self.values.trusted_relays.iter().any(|relay| {

View File

@@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize};
use crate::ThemeColors;
#[derive(Debug, Clone, Copy, Default, PartialEq, PartialOrd, Eq, Hash)]
#[derive(Debug, Clone, Copy, Default, PartialEq, PartialOrd, Eq, Hash, Deserialize, Serialize)]
pub enum ThemeMode {
#[default]
Light,
@@ -18,11 +18,11 @@ impl ThemeMode {
matches!(self, Self::Dark)
}
/// Return lower_case theme name: `light`, `dark`.
/// Return theme name: `light`, `dark`.
pub fn name(&self) -> &'static str {
match self {
ThemeMode::Light => "light",
ThemeMode::Dark => "dark",
ThemeMode::Light => "Light",
ThemeMode::Dark => "Dark",
}
}
}
@@ -153,14 +153,14 @@ impl ThemeFamily {
///
/// # fn main() -> anyhow::Result<()> {
/// // Assuming the file exists at `assets/themes/my-theme.json`
/// let theme = ThemeFamily::from_assets("my-theme")?;
/// let theme = ThemeFamily::from_assets("themes/my-theme.json")?;
///
/// println!("Loaded theme: {}", theme.name);
/// # Ok(())
/// # }
/// ```
pub fn from_assets(name: &str) -> anyhow::Result<Self> {
let path = format!("assets/themes/{}.json", name);
pub fn from_assets(target: &str) -> anyhow::Result<Self> {
let path = format!("assets/{target}");
Self::from_file(path)
}
}