Skip to content

Commit

Permalink
feat: support show background image
Browse files Browse the repository at this point in the history
  • Loading branch information
d1y committed Dec 18, 2024
1 parent 4ec077f commit cba03dd
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 3 deletions.
2 changes: 2 additions & 0 deletions crates/theme/src/default_colors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down
1 change: 1 addition & 0 deletions crates/theme/src/fallback_themes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
11 changes: 11 additions & 0 deletions crates/theme/src/schema.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -172,6 +175,10 @@ pub struct ThemeColorsContent {
#[serde(rename = "background")]
pub background: Option<String>,

// Background image. Used for displaying a picture behind the workspace.
#[serde(rename = "background.image_file")]
pub background_image_file: Option<String>,

/// 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...
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 3 additions & 1 deletion crates/theme/src/styles/colors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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<Arc<PathBuf>>,
/// 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...
Expand Down
17 changes: 15 additions & 2 deletions crates/workspace/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()?;
Expand Down

0 comments on commit cba03dd

Please sign in to comment.