WIP: Render dummy chat messages to test List

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Antonio Scandurra 2021-08-23 17:29:46 +02:00
parent 94e9a83326
commit a182db863f
4 changed files with 73 additions and 32 deletions

View file

@ -155,25 +155,42 @@ impl<T: Element> AnyElement for Lifecycle<T> {
} }
fn paint(&mut self, origin: Vector2F, cx: &mut PaintContext) { fn paint(&mut self, origin: Vector2F, cx: &mut PaintContext) {
*self = if let Lifecycle::PostLayout { *self = match mem::take(self) {
mut element, Lifecycle::PostLayout {
constraint, mut element,
size, constraint,
mut layout, size,
} = mem::take(self) mut layout,
{ } => {
let bounds = RectF::new(origin, size); let bounds = RectF::new(origin, size);
let paint = element.paint(bounds, &mut layout, cx); let paint = element.paint(bounds, &mut layout, cx);
Lifecycle::PostPaint {
element,
constraint,
bounds,
layout,
paint,
}
}
Lifecycle::PostPaint { Lifecycle::PostPaint {
element, mut element,
constraint, constraint,
bounds, bounds,
layout, mut layout,
paint, ..
} => {
let bounds = RectF::new(origin, bounds.size());
let paint = element.paint(bounds, &mut layout, cx);
Lifecycle::PostPaint {
element,
constraint,
bounds,
layout,
paint,
}
} }
} else { _ => panic!("invalid element lifecycle state"),
panic!("invalid element lifecycle state"); }
};
} }
fn dispatch_event(&mut self, event: &Event, cx: &mut EventContext) -> bool { fn dispatch_event(&mut self, event: &Event, cx: &mut EventContext) -> bool {

View file

@ -1,14 +1,41 @@
use crate::Settings;
use super::channel::{Channel, ChannelList}; use super::channel::{Channel, ChannelList};
use gpui::{elements::*, Entity, ModelHandle, View}; use gpui::{elements::*, Entity, ModelHandle, RenderContext, View, ViewContext};
use postage::watch;
pub struct ChatPanel { pub struct ChatPanel {
channel_list: ModelHandle<ChannelList>, // channel_list: ModelHandle<ChannelList>,
active_channel: Option<ModelHandle<Channel>>, // active_channel: Option<ModelHandle<Channel>>,
messages: ListState, messages: ListState,
} }
pub enum Event {} pub enum Event {}
impl ChatPanel {
pub fn new(settings: watch::Receiver<Settings>) -> Self {
let settings = settings.borrow();
let mut messages = Vec::new();
for i in 0..1000 {
messages.push(
Container::new(
Label::new(
format!("This is message {}", i),
settings.ui_font_family,
settings.ui_font_size,
)
.with_style(&settings.theme.selector.label)
.boxed(),
)
.boxed(),
);
}
Self {
messages: ListState::new(messages),
}
}
}
impl Entity for ChatPanel { impl Entity for ChatPanel {
type Event = Event; type Event = Event;
} }
@ -18,7 +45,7 @@ impl View for ChatPanel {
"ChatPanel" "ChatPanel"
} }
fn render(&self, cx: &gpui::RenderContext<'_, Self>) -> gpui::ElementBox { fn render(&self, cx: &RenderContext<Self>) -> gpui::ElementBox {
todo!() List::new(self.messages.clone()).boxed()
} }
} }

View file

@ -3,6 +3,7 @@ pub mod pane_group;
pub mod sidebar; pub mod sidebar;
use crate::{ use crate::{
chat_panel::ChatPanel,
editor::{Buffer, Editor}, editor::{Buffer, Editor},
fs::Fs, fs::Fs,
language::LanguageRegistry, language::LanguageRegistry,
@ -28,7 +29,7 @@ use log::error;
pub use pane::*; pub use pane::*;
pub use pane_group::*; pub use pane_group::*;
use postage::watch; use postage::watch;
use sidebar::{Side, Sidebar}; use sidebar::{Side, Sidebar, ToggleSidebarItem};
use smol::prelude::*; use smol::prelude::*;
use std::{ use std::{
collections::{hash_map::Entry, HashMap, HashSet}, collections::{hash_map::Entry, HashMap, HashSet},
@ -44,7 +45,6 @@ action!(ShareWorktree);
action!(JoinWorktree, Arc<AppState>); action!(JoinWorktree, Arc<AppState>);
action!(Save); action!(Save);
action!(DebugElements); action!(DebugElements);
action!(ToggleSidebarItem, (Side, usize));
pub fn init(cx: &mut MutableAppContext) { pub fn init(cx: &mut MutableAppContext) {
cx.add_global_action(open); cx.add_global_action(open);
@ -372,7 +372,8 @@ impl Workspace {
let mut right_sidebar = Sidebar::new(Side::Right); let mut right_sidebar = Sidebar::new(Side::Right);
right_sidebar.add_item( right_sidebar.add_item(
"icons/comment-16.svg", "icons/comment-16.svg",
cx.add_view(|_| ProjectBrowser).into(), cx.add_view(|_| ChatPanel::new(app_state.settings.clone()))
.into(),
); );
right_sidebar.add_item("icons/user-16.svg", cx.add_view(|_| ProjectBrowser).into()); right_sidebar.add_item("icons/user-16.svg", cx.add_view(|_| ProjectBrowser).into());
@ -752,16 +753,12 @@ impl Workspace {
} }
} }
pub fn toggle_sidebar_item( pub fn toggle_sidebar_item(&mut self, action: &ToggleSidebarItem, cx: &mut ViewContext<Self>) {
&mut self, let sidebar = match action.0.side {
ToggleSidebarItem((side, item_ix)): &ToggleSidebarItem,
cx: &mut ViewContext<Self>,
) {
let sidebar = match side {
Side::Left => &mut self.left_sidebar, Side::Left => &mut self.left_sidebar,
Side::Right => &mut self.right_sidebar, Side::Right => &mut self.right_sidebar,
}; };
sidebar.toggle_item(*item_ix); sidebar.toggle_item(action.0.item_index);
cx.notify(); cx.notify();
} }

View file

@ -28,8 +28,8 @@ action!(ToggleSidebarItem, ToggleArg);
#[derive(Clone)] #[derive(Clone)]
pub struct ToggleArg { pub struct ToggleArg {
side: Side, pub side: Side,
item_index: usize, pub item_index: usize,
} }
impl Sidebar { impl Sidebar {