From cba03dd6d3da776b1d542b1a62cc737033722959 Mon Sep 17 00:00:00 2001 From: d1y Date: Wed, 18 Dec 2024 18:16:59 +0800 Subject: [PATCH] feat: support show background image --- crates/theme/src/default_colors.rs | 2 ++ crates/theme/src/fallback_themes.rs | 1 + crates/theme/src/schema.rs | 11 +++++++++++ crates/theme/src/styles/colors.rs | 4 +++- crates/workspace/src/workspace.rs | 17 +++++++++++++++-- 5 files changed, 32 insertions(+), 3 deletions(-) diff --git a/crates/theme/src/default_colors.rs b/crates/theme/src/default_colors.rs index b9780a304..26ff5cc9a 100644 --- a/crates/theme/src/default_colors.rs +++ b/crates/theme/src/default_colors.rs @@ -28,6 +28,7 @@ impl ThemeColors { elevated_surface_background: neutral().light().step_2(), surface_background: neutral().light().step_2(), background: neutral().light().step_1(), + background_image_file: None, element_background: neutral().light().step_3(), element_hover: neutral().light_alpha().step_4(), element_active: neutral().light_alpha().step_5(), @@ -134,6 +135,7 @@ impl ThemeColors { elevated_surface_background: neutral().dark().step_2(), surface_background: neutral().dark().step_2(), background: neutral().dark().step_1(), + background_image_file: None, element_background: neutral().dark().step_3(), element_hover: neutral().dark_alpha().step_4(), element_active: neutral().dark_alpha().step_5(), diff --git a/crates/theme/src/fallback_themes.rs b/crates/theme/src/fallback_themes.rs index 9f665ea96..8333561b7 100644 --- a/crates/theme/src/fallback_themes.rs +++ b/crates/theme/src/fallback_themes.rs @@ -53,6 +53,7 @@ pub(crate) fn zed_default_dark() -> Theme { elevated_surface_background: elevated_surface, surface_background: bg, background: bg, + background_image_file: None, element_background: hsla(223.0 / 360., 13. / 100., 21. / 100., 1.0), element_hover: hsla(225.0 / 360., 11.8 / 100., 26.7 / 100., 1.0), element_active: hsla(220.0 / 360., 11.8 / 100., 20.0 / 100., 1.0), diff --git a/crates/theme/src/schema.rs b/crates/theme/src/schema.rs index 440ac332c..365ed94ee 100644 --- a/crates/theme/src/schema.rs +++ b/crates/theme/src/schema.rs @@ -1,5 +1,8 @@ #![allow(missing_docs)] +use std::path::PathBuf; +use std::sync::Arc; + use anyhow::Result; use gpui::{FontStyle, FontWeight, HighlightStyle, Hsla, WindowBackgroundAppearance}; use indexmap::IndexMap; @@ -172,6 +175,10 @@ pub struct ThemeColorsContent { #[serde(rename = "background")] pub background: Option, + // Background image. Used for displaying a picture behind the workspace. + #[serde(rename = "background.image_file")] + pub background_image_file: Option, + /// Background Color. Used for the background of an element that should have a different background than the surface it's on. /// /// Elements might include: Buttons, Inputs, Checkboxes, Radio Buttons... @@ -595,6 +602,10 @@ impl ThemeColorsContent { .background .as_ref() .and_then(|color| try_parse_color(color).ok()), + background_image_file: self + .background_image_file + .as_ref() + .map(|image_file| Arc::new(PathBuf::from(image_file))), element_background: self .element_background .as_ref() diff --git a/crates/theme/src/styles/colors.rs b/crates/theme/src/styles/colors.rs index 99c165621..672aac432 100644 --- a/crates/theme/src/styles/colors.rs +++ b/crates/theme/src/styles/colors.rs @@ -2,7 +2,7 @@ use gpui::{Hsla, SharedString, WindowBackgroundAppearance, WindowContext}; use refineable::Refineable; -use std::sync::Arc; +use std::{path::PathBuf, sync::Arc}; use strum::{AsRefStr, EnumIter, IntoEnumIterator}; use crate::{ @@ -31,6 +31,8 @@ pub struct ThemeColors { pub surface_background: Hsla, /// Background Color. Used for the app background and blank panels or windows. pub background: Hsla, + /// Background image. Used for displaying a picture behind the workspace. + pub background_image_file: Option>, /// Background Color. Used for the background of an element that should have a different background than the surface it's on. /// /// Elements might include: Buttons, Inputs, Checkboxes, Radio Buttons... diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rs index be7c13d59..01c8a874c 100644 --- a/crates/workspace/src/workspace.rs +++ b/crates/workspace/src/workspace.rs @@ -37,7 +37,7 @@ use gpui::{ EventEmitter, Flatten, FocusHandle, FocusableView, Global, Hsla, KeyContext, Keystroke, ManagedView, Model, ModelContext, MouseButton, PathPromptOptions, Point, PromptLevel, Render, ResizeEdge, Size, Stateful, Subscription, Task, Tiling, View, WeakView, WindowBounds, - WindowHandle, WindowId, WindowOptions, + WindowHandle, WindowId, WindowOptions, img }; pub use item::{ FollowableItem, FollowableItemHandle, Item, ItemHandle, ItemSettings, PreviewTabsSettings, @@ -4982,7 +4982,20 @@ impl Render for Workspace { DockPosition::Right, &self.right_dock, cx, - )), + )) + // 传递图片请记得透明化 8% + // 因为 element 中没有 .opacity 属性 + .when_some( + colors.background_image_file.as_ref(), + |this, image_file| { + this.child( + img(image_file.as_ref().clone()) + .absolute() + .object_fit(gpui::ObjectFit::Cover) + .size_full(), + ) + }, + ), ) .children(self.zoomed.as_ref().and_then(|view| { let zoomed_view = view.upgrade()?;