mirror of
https://github.com/zed-industries/zed.git
synced 2025-02-12 05:27:07 +00:00
There's still a bit more work to do on this, but this PR is compiling (with warnings) after eliminating the key types. When the tasks below are complete, this will be the new narrative for GPUI: - `Entity<T>` - This replaces `View<T>`/`Model<T>`. It represents a unit of state, and if `T` implements `Render`, then `Entity<T>` implements `Element`. - `&mut App` This replaces `AppContext` and represents the app. - `&mut Context<T>` This replaces `ModelContext` and derefs to `App`. It is provided by the framework when updating an entity. - `&mut Window` Broken out of `&mut WindowContext` which no longer exists. Every method that once took `&mut WindowContext` now takes `&mut Window, &mut App` and every method that took `&mut ViewContext<T>` now takes `&mut Window, &mut Context<T>` Not pictured here are the two other failed attempts. It's been quite a month! Tasks: - [x] Remove `View`, `ViewContext`, `WindowContext` and thread through `Window` - [x] [@cole-miller @mikayla-maki] Redraw window when entities change - [x] [@cole-miller @mikayla-maki] Get examples and Zed running - [x] [@cole-miller @mikayla-maki] Fix Zed rendering - [x] [@mikayla-maki] Fix todo! macros and comments - [x] Fix a bug where the editor would not be redrawn because of view caching - [x] remove publicness window.notify() and replace with `AppContext::notify` - [x] remove `observe_new_window_models`, replace with `observe_new_models` with an optional window - [x] Fix a bug where the project panel would not be redrawn because of the wrong refresh() call being used - [x] Fix the tests - [x] Fix warnings by eliminating `Window` params or using `_` - [x] Fix conflicts - [x] Simplify generic code where possible - [x] Rename types - [ ] Update docs ### issues post merge - [x] Issues switching between normal and insert mode - [x] Assistant re-rendering failure - [x] Vim test failures - [x] Mac build issue Release Notes: - N/A --------- Co-authored-by: Antonio Scandurra <me@as-cii.com> Co-authored-by: Cole Miller <cole@zed.dev> Co-authored-by: Mikayla <mikayla@zed.dev> Co-authored-by: Joseph <joseph@zed.dev> Co-authored-by: max <max@zed.dev> Co-authored-by: Michael Sloan <michael@zed.dev> Co-authored-by: Mikayla Maki <mikaylamaki@Mikaylas-MacBook-Pro.local> Co-authored-by: Mikayla <mikayla.c.maki@gmail.com> Co-authored-by: joão <joao@zed.dev>
172 lines
5.2 KiB
Rust
172 lines
5.2 KiB
Rust
use std::fs;
|
|
use std::path::PathBuf;
|
|
use std::str::FromStr;
|
|
use std::sync::Arc;
|
|
|
|
use anyhow::Result;
|
|
use gpui::{
|
|
actions, div, img, prelude::*, px, rgb, size, App, AppContext, Application, AssetSource,
|
|
Bounds, Context, ImageSource, KeyBinding, Menu, MenuItem, Point, SharedString, SharedUri,
|
|
TitlebarOptions, Window, WindowBounds, WindowOptions,
|
|
};
|
|
|
|
struct Assets {
|
|
base: PathBuf,
|
|
}
|
|
|
|
impl AssetSource for Assets {
|
|
fn load(&self, path: &str) -> Result<Option<std::borrow::Cow<'static, [u8]>>> {
|
|
fs::read(self.base.join(path))
|
|
.map(|data| Some(std::borrow::Cow::Owned(data)))
|
|
.map_err(|e| e.into())
|
|
}
|
|
|
|
fn list(&self, path: &str) -> Result<Vec<SharedString>> {
|
|
fs::read_dir(self.base.join(path))
|
|
.map(|entries| {
|
|
entries
|
|
.filter_map(|entry| {
|
|
entry
|
|
.ok()
|
|
.and_then(|entry| entry.file_name().into_string().ok())
|
|
.map(SharedString::from)
|
|
})
|
|
.collect()
|
|
})
|
|
.map_err(|e| e.into())
|
|
}
|
|
}
|
|
|
|
#[derive(IntoElement)]
|
|
struct ImageContainer {
|
|
text: SharedString,
|
|
src: ImageSource,
|
|
}
|
|
|
|
impl ImageContainer {
|
|
pub fn new(text: impl Into<SharedString>, src: impl Into<ImageSource>) -> Self {
|
|
Self {
|
|
text: text.into(),
|
|
src: src.into(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl RenderOnce for ImageContainer {
|
|
fn render(self, _window: &mut Window, _: &mut App) -> impl IntoElement {
|
|
div().child(
|
|
div()
|
|
.flex_row()
|
|
.size_full()
|
|
.gap_4()
|
|
.child(self.text)
|
|
.child(img(self.src).size(px(256.0))),
|
|
)
|
|
}
|
|
}
|
|
|
|
struct ImageShowcase {
|
|
local_resource: Arc<std::path::Path>,
|
|
remote_resource: SharedUri,
|
|
asset_resource: SharedString,
|
|
}
|
|
|
|
impl Render for ImageShowcase {
|
|
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
|
div()
|
|
.size_full()
|
|
.flex()
|
|
.flex_col()
|
|
.justify_center()
|
|
.items_center()
|
|
.gap_8()
|
|
.bg(rgb(0xffffff))
|
|
.child(
|
|
div()
|
|
.flex()
|
|
.flex_row()
|
|
.justify_center()
|
|
.items_center()
|
|
.gap_8()
|
|
.child(ImageContainer::new(
|
|
"Image loaded from a local file",
|
|
self.local_resource.clone(),
|
|
))
|
|
.child(ImageContainer::new(
|
|
"Image loaded from a remote resource",
|
|
self.remote_resource.clone(),
|
|
))
|
|
.child(ImageContainer::new(
|
|
"Image loaded from an asset",
|
|
self.asset_resource.clone(),
|
|
)),
|
|
)
|
|
.child(
|
|
div()
|
|
.flex()
|
|
.flex_row()
|
|
.gap_8()
|
|
.child(
|
|
div()
|
|
.flex_col()
|
|
.child("Auto Width")
|
|
.child(img("https://picsum.photos/800/400").h(px(180.))),
|
|
)
|
|
.child(
|
|
div()
|
|
.flex_col()
|
|
.child("Auto Height")
|
|
.child(img("https://picsum.photos/480/640").w(px(180.))),
|
|
),
|
|
)
|
|
}
|
|
}
|
|
|
|
actions!(image, [Quit]);
|
|
|
|
fn main() {
|
|
env_logger::init();
|
|
|
|
Application::new()
|
|
.with_assets(Assets {
|
|
base: PathBuf::from("crates/gpui/examples"),
|
|
})
|
|
.run(|cx: &mut App| {
|
|
cx.activate(true);
|
|
cx.on_action(|_: &Quit, cx| cx.quit());
|
|
cx.bind_keys([KeyBinding::new("cmd-q", Quit, None)]);
|
|
cx.set_menus(vec![Menu {
|
|
name: "Image".into(),
|
|
items: vec![MenuItem::action("Quit", Quit)],
|
|
}]);
|
|
|
|
let window_options = WindowOptions {
|
|
titlebar: Some(TitlebarOptions {
|
|
title: Some(SharedString::from("Image Example")),
|
|
appears_transparent: false,
|
|
..Default::default()
|
|
}),
|
|
|
|
window_bounds: Some(WindowBounds::Windowed(Bounds {
|
|
size: size(px(1100.), px(600.)),
|
|
origin: Point::new(px(200.), px(200.)),
|
|
})),
|
|
|
|
..Default::default()
|
|
};
|
|
|
|
cx.open_window(window_options, |_, cx| {
|
|
cx.new(|_| ImageShowcase {
|
|
// Relative path to your root project path
|
|
local_resource: PathBuf::from_str("crates/gpui/examples/image/app-icon.png")
|
|
.unwrap()
|
|
.into(),
|
|
|
|
remote_resource: "https://picsum.photos/512/512".into(),
|
|
|
|
asset_resource: "image/color.svg".into(),
|
|
})
|
|
})
|
|
.unwrap();
|
|
});
|
|
}
|