Change TitleBar to a view

This commit is contained in:
Marshall Bowers 2023-10-13 12:21:51 -04:00
parent b30b1d145c
commit f3679b37a2
3 changed files with 44 additions and 35 deletions

View file

@ -153,10 +153,7 @@ impl ComponentStory {
ui::ThemeSelectorStory::new().into_any() ui::ThemeSelectorStory::new().into_any()
}) })
.into_any(), .into_any(),
Self::TitleBar => view(cx.entity(|cx| ()), |_, _| { Self::TitleBar => ui::TitleBarStory::view(cx).into_any(),
ui::TitleBarStory::new().into_any()
})
.into_any(),
Self::Toast => { Self::Toast => {
view(cx.entity(|cx| ()), |_, _| ui::ToastStory::new().into_any()).into_any() view(cx.entity(|cx| ()), |_, _| ui::ToastStory::new().into_any()).into_any()
} }

View file

@ -1,11 +1,12 @@
use std::marker::PhantomData;
use std::sync::atomic::AtomicBool; use std::sync::atomic::AtomicBool;
use std::sync::Arc; use std::sync::Arc;
use gpui3::{view, Context, View};
use crate::prelude::*; use crate::prelude::*;
use crate::{ use crate::{
theme, Avatar, Button, Icon, IconButton, IconColor, PlayerStack, PlayerWithCallStatus, random_players_with_call_status, theme, Avatar, Button, Icon, IconButton, IconColor,
ToolDivider, TrafficLights, PlayerStack, PlayerWithCallStatus, ToolDivider, TrafficLights,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -15,16 +16,15 @@ pub struct Livestream {
// windows // windows
} }
#[derive(Element)] #[derive(Clone)]
pub struct TitleBar<S: 'static + Send + Sync + Clone> { pub struct TitleBar {
state_type: PhantomData<S>,
/// If the window is active from the OS's perspective. /// If the window is active from the OS's perspective.
is_active: Arc<AtomicBool>, is_active: Arc<AtomicBool>,
livestream: Option<Livestream>, livestream: Option<Livestream>,
} }
impl<S: 'static + Send + Sync + Clone> TitleBar<S> { impl TitleBar {
pub fn new(cx: &mut ViewContext<S>) -> Self { pub fn new(cx: &mut ViewContext<Self>) -> Self {
let is_active = Arc::new(AtomicBool::new(true)); let is_active = Arc::new(AtomicBool::new(true));
let active = is_active.clone(); let active = is_active.clone();
@ -35,7 +35,6 @@ impl<S: 'static + Send + Sync + Clone> TitleBar<S> {
// .detach(); // .detach();
Self { Self {
state_type: PhantomData,
is_active, is_active,
livestream: None, livestream: None,
} }
@ -46,7 +45,19 @@ impl<S: 'static + Send + Sync + Clone> TitleBar<S> {
self self
} }
fn render(&mut self, _view: &mut S, cx: &mut ViewContext<S>) -> impl Element<ViewState = S> { pub fn view(cx: &mut WindowContext) -> View<Self> {
view(
cx.entity(|cx| {
Self::new(cx).set_livestream(Some(Livestream {
players: random_players_with_call_status(7),
channel: Some("gpui2-ui".to_string()),
}))
}),
Self::render,
)
}
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl Element<ViewState = Self> {
let theme = theme(cx); let theme = theme(cx);
// let has_focus = cx.window_is_active(); // let has_focus = cx.window_is_active();
let has_focus = true; let has_focus = true;
@ -127,23 +138,25 @@ mod stories {
use super::*; use super::*;
#[derive(Element)] pub struct TitleBarStory {
pub struct TitleBarStory<S: 'static + Send + Sync + Clone> { title_bar: View<TitleBar>,
state_type: PhantomData<S>,
} }
impl<S: 'static + Send + Sync + Clone> TitleBarStory<S> { impl TitleBarStory {
pub fn new() -> Self { pub fn view(cx: &mut WindowContext) -> View<Self> {
Self { view(
state_type: PhantomData, cx.entity(|cx| Self {
} title_bar: TitleBar::view(cx),
}),
Self::render,
)
} }
fn render(&mut self, _view: &mut S, cx: &mut ViewContext<S>) -> impl Element<ViewState = S> { fn render(&mut self, cx: &mut ViewContext<Self>) -> impl Element<ViewState = Self> {
Story::container(cx) Story::container(cx)
.child(Story::title_for::<_, TitleBar<S>>(cx)) .child(Story::title_for::<_, TitleBar>(cx))
.child(Story::label(cx, "Default")) .child(Story::label(cx, "Default"))
.child(TitleBar::new(cx)) .child(self.title_bar.clone())
} }
} }
} }

View file

@ -2,15 +2,16 @@ use chrono::DateTime;
use gpui3::{px, relative, rems, view, Context, Size, View}; use gpui3::{px, relative, rems, view, Context, Size, View};
use crate::{ use crate::{
hello_world_rust_editor_with_status_example, random_players_with_call_status, theme, v_stack, hello_world_rust_editor_with_status_example, theme, v_stack, AssistantPanel, Button,
AssistantPanel, Button, ChatMessage, ChatPanel, CollabPanel, EditorPane, Label, ChatMessage, ChatPanel, CollabPanel, EditorPane, Label, LanguageSelector, Pane, PaneGroup,
LanguageSelector, Livestream, Pane, PaneGroup, Panel, PanelAllowedSides, PanelSide, Panel, PanelAllowedSides, PanelSide, ProjectPanel, SplitDirection, StatusBar, Terminal,
ProjectPanel, SplitDirection, StatusBar, Terminal, TitleBar, Toast, ToastOrigin, TitleBar, Toast, ToastOrigin,
}; };
use crate::{prelude::*, NotificationToast}; use crate::{prelude::*, NotificationToast};
#[derive(Clone)] #[derive(Clone)]
pub struct Workspace { pub struct Workspace {
title_bar: View<TitleBar>,
show_project_panel: bool, show_project_panel: bool,
show_collab_panel: bool, show_collab_panel: bool,
show_chat_panel: bool, show_chat_panel: bool,
@ -24,8 +25,9 @@ pub struct Workspace {
} }
impl Workspace { impl Workspace {
pub fn new() -> Self { pub fn new(cx: &mut ViewContext<Self>) -> Self {
Self { Self {
title_bar: TitleBar::view(cx),
show_project_panel: true, show_project_panel: true,
show_collab_panel: false, show_collab_panel: false,
show_chat_panel: true, show_chat_panel: true,
@ -106,7 +108,7 @@ impl Workspace {
} }
pub fn view(cx: &mut WindowContext) -> View<Self> { pub fn view(cx: &mut WindowContext) -> View<Self> {
view(cx.entity(|cx| Self::new()), Self::render) view(cx.entity(|cx| Self::new(cx)), Self::render)
} }
pub fn render(&mut self, cx: &mut ViewContext<Self>) -> impl Element<ViewState = Self> { pub fn render(&mut self, cx: &mut ViewContext<Self>) -> impl Element<ViewState = Self> {
@ -167,10 +169,7 @@ impl Workspace {
.items_start() .items_start()
.text_color(theme.lowest.base.default.foreground) .text_color(theme.lowest.base.default.foreground)
.fill(theme.lowest.base.default.background) .fill(theme.lowest.base.default.background)
.child(TitleBar::new(cx).set_livestream(Some(Livestream { .child(self.title_bar.clone())
players: random_players_with_call_status(7),
channel: Some("gpui2-ui".to_string()),
})))
.child( .child(
div() div()
.flex_1() .flex_1()