Add View::update which provides a ViewContext

This commit is contained in:
Nathan Sobo 2023-10-26 19:41:42 +02:00
parent 8e3314e680
commit a1c3826858
17 changed files with 403 additions and 247 deletions

View file

@ -1,4 +1,4 @@
use gpui2::{view, Context, View};
use gpui2::{AppContext, Context, View};
use crate::prelude::*;
use crate::{h_stack, Icon, IconButton, IconColor, Input};
@ -21,8 +21,12 @@ impl BufferSearch {
cx.notify();
}
pub fn view(cx: &mut WindowContext) -> View<Self> {
view(cx.entity(|cx| Self::new()), Self::render)
pub fn view(cx: &mut AppContext) -> View<Self> {
{
let state = cx.entity(|cx| Self::new());
let render = Self::render;
View::for_handle(state, render)
}
}
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl Component<Self> {

View file

@ -1,6 +1,6 @@
use std::path::PathBuf;
use gpui2::{view, Context, View};
use gpui2::{AppContext, Context, View};
use crate::prelude::*;
use crate::{
@ -20,7 +20,7 @@ pub struct EditorPane {
impl EditorPane {
pub fn new(
cx: &mut WindowContext,
cx: &mut AppContext,
tabs: Vec<Tab>,
path: PathBuf,
symbols: Vec<Symbol>,
@ -42,11 +42,12 @@ impl EditorPane {
cx.notify();
}
pub fn view(cx: &mut WindowContext) -> View<Self> {
view(
cx.entity(|cx| hello_world_rust_editor_with_status_example(cx)),
Self::render,
)
pub fn view(cx: &mut AppContext) -> View<Self> {
{
let state = cx.entity(|cx| hello_world_rust_editor_with_status_example(cx));
let render = Self::render;
View::for_handle(state, render)
}
}
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl Component<Self> {

View file

@ -1,7 +1,7 @@
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
use gpui2::{view, Context, View};
use gpui2::{AppContext, Context, ModelContext, View};
use crate::prelude::*;
use crate::settings::user_settings;
@ -28,7 +28,7 @@ pub struct TitleBar {
}
impl TitleBar {
pub fn new(cx: &mut ViewContext<Self>) -> Self {
pub fn new(cx: &mut ModelContext<Self>) -> Self {
let is_active = Arc::new(AtomicBool::new(true));
let active = is_active.clone();
@ -80,11 +80,12 @@ impl TitleBar {
cx.notify();
}
pub fn view(cx: &mut WindowContext, livestream: Option<Livestream>) -> View<Self> {
view(
cx.entity(|cx| Self::new(cx).set_livestream(livestream)),
Self::render,
)
pub fn view(cx: &mut AppContext, livestream: Option<Livestream>) -> View<Self> {
{
let state = cx.entity(|cx| Self::new(cx).set_livestream(livestream));
let render = Self::render;
View::for_handle(state, render)
}
}
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl Component<Self> {
@ -195,13 +196,14 @@ mod stories {
}
impl TitleBarStory {
pub fn view(cx: &mut WindowContext) -> View<Self> {
view(
cx.entity(|cx| Self {
pub fn view(cx: &mut AppContext) -> View<Self> {
{
let state = cx.entity(|cx| Self {
title_bar: TitleBar::view(cx, None),
}),
Self::render,
)
});
let render = Self::render;
View::for_handle(state, render)
}
}
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl Component<Self> {

View file

@ -1,7 +1,7 @@
use std::sync::Arc;
use chrono::DateTime;
use gpui2::{px, relative, rems, view, Context, Size, View};
use gpui2::{px, relative, rems, AppContext, Context, Size, View};
use crate::{
old_theme, static_livestream, user_settings_mut, v_stack, AssistantPanel, Button, ChatMessage,
@ -44,7 +44,7 @@ pub struct Workspace {
}
impl Workspace {
pub fn new(cx: &mut ViewContext<Self>) -> Self {
pub fn new(cx: &mut AppContext) -> Self {
Self {
title_bar: TitleBar::view(cx, None),
editor_1: EditorPane::view(cx),
@ -170,8 +170,12 @@ impl Workspace {
cx.notify();
}
pub fn view(cx: &mut WindowContext) -> View<Self> {
view(cx.entity(|cx| Self::new(cx)), Self::render)
pub fn view(cx: &mut AppContext) -> View<Self> {
{
let state = cx.entity(|cx| Self::new(cx));
let render = Self::render;
View::for_handle(state, render)
}
}
pub fn render(&mut self, cx: &mut ViewContext<Self>) -> impl Component<Self> {
@ -351,6 +355,8 @@ pub use stories::*;
#[cfg(feature = "stories")]
mod stories {
use gpui2::VisualContext;
use super::*;
pub struct WorkspaceStory {
@ -359,10 +365,10 @@ mod stories {
impl WorkspaceStory {
pub fn view(cx: &mut WindowContext) -> View<Self> {
view(
cx.entity(|cx| Self {
cx.build_view(
|cx| Self {
workspace: Workspace::view(cx),
}),
},
|view, cx| view.workspace.clone(),
)
}

View file

@ -1,7 +1,7 @@
use std::path::PathBuf;
use std::str::FromStr;
use gpui2::WindowContext;
use gpui2::{AppContext, WindowContext};
use rand::Rng;
use theme2::Theme;
@ -781,7 +781,7 @@ pub fn hello_world_rust_buffer_rows(theme: &Theme) -> Vec<BufferRow> {
]
}
pub fn hello_world_rust_editor_with_status_example(cx: &mut WindowContext) -> EditorPane {
pub fn hello_world_rust_editor_with_status_example(cx: &mut AppContext) -> EditorPane {
let theme = theme(cx);
EditorPane::new(

View file

@ -1,6 +1,6 @@
use gpui2::{
AnyElement, Bounds, Component, Element, Hsla, LayoutId, Pixels, Result, ViewContext,
WindowContext,
AnyElement, AppContext, Bounds, Component, Element, Hsla, LayoutId, Pixels, Result,
ViewContext, WindowContext,
};
use serde::{de::Visitor, Deserialize, Deserializer};
use std::collections::HashMap;
@ -220,6 +220,6 @@ pub fn old_theme(cx: &WindowContext) -> Arc<Theme> {
Arc::new(cx.global::<Theme>().clone())
}
pub fn theme(cx: &WindowContext) -> Arc<theme2::Theme> {
pub fn theme(cx: &AppContext) -> Arc<theme2::Theme> {
theme2::active_theme(cx).clone()
}