wip: refactor
This commit is contained in:
@@ -3,14 +3,13 @@ use components::{
|
||||
theme::{ActiveTheme, Theme},
|
||||
Root, TitleBar,
|
||||
};
|
||||
use coop_ui::block::BlockContainer;
|
||||
use gpui::*;
|
||||
use nostr_sdk::prelude::*;
|
||||
use serde::Deserialize;
|
||||
use std::sync::Arc;
|
||||
|
||||
use super::{
|
||||
dock::{left_dock::LeftDock, welcome::WelcomeBlock},
|
||||
dock::{chat::ChatPanel, left_dock::LeftDock, welcome::WelcomePanel},
|
||||
onboarding::Onboarding,
|
||||
};
|
||||
use crate::states::account::AccountState;
|
||||
@@ -66,7 +65,7 @@ impl AppView {
|
||||
}
|
||||
|
||||
fn init_layout(dock_area: WeakView<DockArea>, cx: &mut WindowContext) {
|
||||
let left = DockItem::panel(Arc::new(BlockContainer::panel::<LeftDock>(cx)));
|
||||
let left = DockItem::panel(Arc::new(LeftDock::new(cx)));
|
||||
let center = Self::init_dock_items(&dock_area, cx);
|
||||
|
||||
_ = dock_area.update(cx, |view, cx| {
|
||||
@@ -90,7 +89,7 @@ impl AppView {
|
||||
Axis::Vertical,
|
||||
vec![DockItem::tabs(
|
||||
vec![
|
||||
Arc::new(BlockContainer::panel::<WelcomeBlock>(cx)),
|
||||
Arc::new(WelcomePanel::new(cx)),
|
||||
// TODO: add chat block
|
||||
],
|
||||
None,
|
||||
@@ -103,12 +102,11 @@ impl AppView {
|
||||
)
|
||||
}
|
||||
|
||||
fn on_action_add_panel(&mut self, _action: &AddPanel, cx: &mut ViewContext<Self>) {
|
||||
// TODO: add chat panel
|
||||
let panel = Arc::new(BlockContainer::panel::<WelcomeBlock>(cx));
|
||||
fn on_action_add_panel(&mut self, action: &AddPanel, cx: &mut ViewContext<Self>) {
|
||||
let chat_panel = Arc::new(ChatPanel::new(action.receiver, cx));
|
||||
|
||||
self.dock.update(cx, |dock_area, cx| {
|
||||
dock_area.add_panel(panel, DockPlacement::Center, cx);
|
||||
dock_area.add_panel(chat_panel, DockPlacement::Center, cx);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,49 +0,0 @@
|
||||
use coop_ui::block::Block;
|
||||
use gpui::*;
|
||||
|
||||
pub struct ChatBlock {
|
||||
focus_handle: FocusHandle,
|
||||
}
|
||||
|
||||
impl ChatBlock {
|
||||
pub fn view(cx: &mut WindowContext) -> View<Self> {
|
||||
cx.new_view(Self::new)
|
||||
}
|
||||
|
||||
fn new(cx: &mut ViewContext<Self>) -> Self {
|
||||
Self {
|
||||
focus_handle: cx.focus_handle(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Block for ChatBlock {
|
||||
fn title() -> &'static str {
|
||||
"Chat"
|
||||
}
|
||||
|
||||
fn new_view(cx: &mut WindowContext) -> View<impl FocusableView> {
|
||||
Self::view(cx)
|
||||
}
|
||||
|
||||
fn zoomable() -> bool {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
impl FocusableView for ChatBlock {
|
||||
fn focus_handle(&self, _: &gpui::AppContext) -> gpui::FocusHandle {
|
||||
self.focus_handle.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for ChatBlock {
|
||||
fn render(&mut self, _cx: &mut gpui::ViewContext<Self>) -> impl IntoElement {
|
||||
div()
|
||||
.size_full()
|
||||
.flex()
|
||||
.items_center()
|
||||
.justify_center()
|
||||
.child("Test")
|
||||
}
|
||||
}
|
||||
87
crates/app/src/views/dock/chat/mod.rs
Normal file
87
crates/app/src/views/dock/chat/mod.rs
Normal file
@@ -0,0 +1,87 @@
|
||||
use components::{
|
||||
button::Button,
|
||||
dock::{DockItemState, Panel, PanelEvent, TitleStyle},
|
||||
popup_menu::PopupMenu,
|
||||
theme::{ActiveTheme, Colorize},
|
||||
StyledExt,
|
||||
};
|
||||
use gpui::*;
|
||||
use nostr_sdk::*;
|
||||
|
||||
pub struct ChatPanel {
|
||||
// Panel
|
||||
name: SharedString,
|
||||
closeable: bool,
|
||||
zoomable: bool,
|
||||
focus_handle: FocusHandle,
|
||||
// Chat Room
|
||||
receiver: PublicKey,
|
||||
}
|
||||
|
||||
impl ChatPanel {
|
||||
pub fn new(receiver: PublicKey, cx: &mut WindowContext) -> View<Self> {
|
||||
cx.new_view(|cx| Self {
|
||||
name: "Chat".into(),
|
||||
closeable: true,
|
||||
zoomable: true,
|
||||
focus_handle: cx.focus_handle(),
|
||||
receiver,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Panel for ChatPanel {
|
||||
fn panel_name(&self) -> &'static str {
|
||||
"ChatPanel"
|
||||
}
|
||||
|
||||
fn title(&self, _cx: &WindowContext) -> AnyElement {
|
||||
self.name.clone().into_any_element()
|
||||
}
|
||||
|
||||
fn title_style(&self, _cx: &WindowContext) -> Option<TitleStyle> {
|
||||
None
|
||||
}
|
||||
|
||||
fn closeable(&self, _cx: &WindowContext) -> bool {
|
||||
self.closeable
|
||||
}
|
||||
|
||||
fn zoomable(&self, _cx: &WindowContext) -> bool {
|
||||
self.zoomable
|
||||
}
|
||||
|
||||
fn popup_menu(&self, menu: PopupMenu, _cx: &WindowContext) -> PopupMenu {
|
||||
menu.track_focus(&self.focus_handle)
|
||||
}
|
||||
|
||||
fn toolbar_buttons(&self, _cx: &WindowContext) -> Vec<Button> {
|
||||
vec![]
|
||||
}
|
||||
|
||||
fn dump(&self, _cx: &AppContext) -> DockItemState {
|
||||
DockItemState::new(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl EventEmitter<PanelEvent> for ChatPanel {}
|
||||
|
||||
impl FocusableView for ChatPanel {
|
||||
fn focus_handle(&self, _: &AppContext) -> gpui::FocusHandle {
|
||||
self.focus_handle.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for ChatPanel {
|
||||
fn render(&mut self, cx: &mut gpui::ViewContext<Self>) -> impl IntoElement {
|
||||
div()
|
||||
.size_full()
|
||||
.flex()
|
||||
.items_center()
|
||||
.justify_center()
|
||||
.child(self.receiver.to_hex())
|
||||
.text_color(cx.theme().muted.darken(0.1))
|
||||
.font_black()
|
||||
.text_sm()
|
||||
}
|
||||
}
|
||||
@@ -1,47 +1,82 @@
|
||||
use components::{scroll::ScrollbarAxis, StyledExt};
|
||||
use coop_ui::block::Block;
|
||||
use components::{
|
||||
button::Button,
|
||||
dock::{DockItemState, Panel, PanelEvent, TitleStyle},
|
||||
popup_menu::PopupMenu,
|
||||
scroll::ScrollbarAxis,
|
||||
StyledExt,
|
||||
};
|
||||
use gpui::*;
|
||||
|
||||
use super::inbox::Inbox;
|
||||
|
||||
pub struct LeftDock {
|
||||
inbox: View<Inbox>,
|
||||
// Panel
|
||||
name: SharedString,
|
||||
closeable: bool,
|
||||
zoomable: bool,
|
||||
focus_handle: FocusHandle,
|
||||
// Dock
|
||||
inbox: View<Inbox>,
|
||||
view_id: EntityId,
|
||||
}
|
||||
|
||||
impl LeftDock {
|
||||
pub fn view(cx: &mut WindowContext) -> View<Self> {
|
||||
cx.new_view(Self::new)
|
||||
pub fn new(cx: &mut WindowContext) -> View<Self> {
|
||||
cx.new_view(Self::view)
|
||||
}
|
||||
|
||||
fn new(cx: &mut ViewContext<Self>) -> Self {
|
||||
fn view(cx: &mut ViewContext<Self>) -> Self {
|
||||
let inbox = cx.new_view(Inbox::new);
|
||||
|
||||
Self {
|
||||
inbox,
|
||||
name: "Left Dock".into(),
|
||||
closeable: true,
|
||||
zoomable: true,
|
||||
focus_handle: cx.focus_handle(),
|
||||
view_id: cx.view().entity_id(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Block for LeftDock {
|
||||
fn title() -> &'static str {
|
||||
"Left Dock"
|
||||
impl Panel for LeftDock {
|
||||
fn panel_name(&self) -> &'static str {
|
||||
"ChatPanel"
|
||||
}
|
||||
|
||||
fn new_view(cx: &mut WindowContext) -> View<impl FocusableView> {
|
||||
Self::view(cx)
|
||||
fn title(&self, _cx: &WindowContext) -> AnyElement {
|
||||
self.name.clone().into_any_element()
|
||||
}
|
||||
|
||||
fn zoomable() -> bool {
|
||||
false
|
||||
fn title_style(&self, _cx: &WindowContext) -> Option<TitleStyle> {
|
||||
None
|
||||
}
|
||||
|
||||
fn closeable(&self, _cx: &WindowContext) -> bool {
|
||||
self.closeable
|
||||
}
|
||||
|
||||
fn zoomable(&self, _cx: &WindowContext) -> bool {
|
||||
self.zoomable
|
||||
}
|
||||
|
||||
fn popup_menu(&self, menu: PopupMenu, _cx: &WindowContext) -> PopupMenu {
|
||||
menu.track_focus(&self.focus_handle)
|
||||
}
|
||||
|
||||
fn toolbar_buttons(&self, _cx: &WindowContext) -> Vec<Button> {
|
||||
vec![]
|
||||
}
|
||||
|
||||
fn dump(&self, _cx: &AppContext) -> DockItemState {
|
||||
DockItemState::new(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl EventEmitter<PanelEvent> for LeftDock {}
|
||||
|
||||
impl FocusableView for LeftDock {
|
||||
fn focus_handle(&self, _: &gpui::AppContext) -> gpui::FocusHandle {
|
||||
fn focus_handle(&self, _: &AppContext) -> gpui::FocusHandle {
|
||||
self.focus_handle.clone()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,47 +1,77 @@
|
||||
use components::{
|
||||
button::Button,
|
||||
dock::{DockItemState, Panel, PanelEvent, TitleStyle},
|
||||
popup_menu::PopupMenu,
|
||||
theme::{ActiveTheme, Colorize},
|
||||
StyledExt,
|
||||
};
|
||||
use coop_ui::block::Block;
|
||||
use gpui::*;
|
||||
|
||||
pub struct WelcomeBlock {
|
||||
pub struct WelcomePanel {
|
||||
name: SharedString,
|
||||
closeable: bool,
|
||||
zoomable: bool,
|
||||
focus_handle: FocusHandle,
|
||||
}
|
||||
|
||||
impl WelcomeBlock {
|
||||
pub fn view(cx: &mut WindowContext) -> View<Self> {
|
||||
cx.new_view(Self::new)
|
||||
impl WelcomePanel {
|
||||
pub fn new(cx: &mut WindowContext) -> View<Self> {
|
||||
cx.new_view(Self::view)
|
||||
}
|
||||
|
||||
fn new(cx: &mut ViewContext<Self>) -> Self {
|
||||
fn view(cx: &mut ViewContext<Self>) -> Self {
|
||||
Self {
|
||||
name: "Welcome".into(),
|
||||
closeable: true,
|
||||
zoomable: true,
|
||||
focus_handle: cx.focus_handle(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Block for WelcomeBlock {
|
||||
fn title() -> &'static str {
|
||||
"Welcome"
|
||||
impl Panel for WelcomePanel {
|
||||
fn panel_name(&self) -> &'static str {
|
||||
"WelcomePanel"
|
||||
}
|
||||
|
||||
fn new_view(cx: &mut WindowContext) -> View<impl FocusableView> {
|
||||
Self::view(cx)
|
||||
fn title(&self, _cx: &WindowContext) -> AnyElement {
|
||||
self.name.clone().into_any_element()
|
||||
}
|
||||
|
||||
fn zoomable() -> bool {
|
||||
false
|
||||
fn title_style(&self, _cx: &WindowContext) -> Option<TitleStyle> {
|
||||
None
|
||||
}
|
||||
|
||||
fn closeable(&self, _cx: &WindowContext) -> bool {
|
||||
self.closeable
|
||||
}
|
||||
|
||||
fn zoomable(&self, _cx: &WindowContext) -> bool {
|
||||
self.zoomable
|
||||
}
|
||||
|
||||
fn popup_menu(&self, menu: PopupMenu, _cx: &WindowContext) -> PopupMenu {
|
||||
menu.track_focus(&self.focus_handle)
|
||||
}
|
||||
|
||||
fn toolbar_buttons(&self, _cx: &WindowContext) -> Vec<Button> {
|
||||
vec![]
|
||||
}
|
||||
|
||||
fn dump(&self, _cx: &AppContext) -> DockItemState {
|
||||
DockItemState::new(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl FocusableView for WelcomeBlock {
|
||||
fn focus_handle(&self, _: &gpui::AppContext) -> gpui::FocusHandle {
|
||||
impl EventEmitter<PanelEvent> for WelcomePanel {}
|
||||
|
||||
impl FocusableView for WelcomePanel {
|
||||
fn focus_handle(&self, _: &AppContext) -> gpui::FocusHandle {
|
||||
self.focus_handle.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for WelcomeBlock {
|
||||
impl Render for WelcomePanel {
|
||||
fn render(&mut self, cx: &mut gpui::ViewContext<Self>) -> impl IntoElement {
|
||||
div()
|
||||
.size_full()
|
||||
|
||||
@@ -1,188 +0,0 @@
|
||||
use components::{
|
||||
button::Button,
|
||||
dock::{DockItemState, Panel, PanelEvent, TitleStyle},
|
||||
h_flex,
|
||||
popup_menu::PopupMenu,
|
||||
theme::ActiveTheme,
|
||||
v_flex,
|
||||
};
|
||||
use gpui::*;
|
||||
use prelude::FluentBuilder;
|
||||
|
||||
actions!(block, [PanelInfo]);
|
||||
|
||||
pub fn section(title: impl IntoElement, cx: &WindowContext) -> Div {
|
||||
h_flex()
|
||||
.items_center()
|
||||
.gap_4()
|
||||
.p_4()
|
||||
.w_full()
|
||||
.rounded_lg()
|
||||
.border_1()
|
||||
.border_color(cx.theme().border)
|
||||
.flex_wrap()
|
||||
.justify_around()
|
||||
.child(div().flex_none().w_full().child(title))
|
||||
}
|
||||
|
||||
pub struct BlockContainer {
|
||||
focus_handle: FocusHandle,
|
||||
name: SharedString,
|
||||
title_bg: Option<Hsla>,
|
||||
description: SharedString,
|
||||
width: Option<Pixels>,
|
||||
height: Option<Pixels>,
|
||||
block: Option<AnyView>,
|
||||
closeable: bool,
|
||||
zoomable: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum ContainerEvent {
|
||||
Close,
|
||||
}
|
||||
|
||||
pub trait Block: FocusableView {
|
||||
fn klass() -> &'static str {
|
||||
std::any::type_name::<Self>().split("::").last().unwrap()
|
||||
}
|
||||
|
||||
fn title() -> &'static str;
|
||||
|
||||
fn description() -> &'static str {
|
||||
""
|
||||
}
|
||||
|
||||
fn closeable() -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn zoomable() -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn title_bg() -> Option<Hsla> {
|
||||
None
|
||||
}
|
||||
|
||||
fn new_view(cx: &mut WindowContext) -> View<impl FocusableView>;
|
||||
}
|
||||
|
||||
impl EventEmitter<ContainerEvent> for BlockContainer {}
|
||||
|
||||
impl BlockContainer {
|
||||
pub fn new(cx: &mut WindowContext) -> Self {
|
||||
let focus_handle = cx.focus_handle();
|
||||
|
||||
Self {
|
||||
focus_handle,
|
||||
name: "".into(),
|
||||
title_bg: None,
|
||||
description: "".into(),
|
||||
width: None,
|
||||
height: None,
|
||||
block: None,
|
||||
closeable: true,
|
||||
zoomable: true,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn panel<B: Block>(cx: &mut WindowContext) -> View<Self> {
|
||||
let name = B::title();
|
||||
let description = B::description();
|
||||
let block = B::new_view(cx);
|
||||
let focus_handle = block.focus_handle(cx);
|
||||
|
||||
cx.new_view(|cx| {
|
||||
let mut story = Self::new(cx).block(block.into());
|
||||
|
||||
story.focus_handle = focus_handle;
|
||||
story.closeable = B::closeable();
|
||||
story.zoomable = B::zoomable();
|
||||
story.name = name.into();
|
||||
story.description = description.into();
|
||||
story.title_bg = B::title_bg();
|
||||
|
||||
story
|
||||
})
|
||||
}
|
||||
|
||||
pub fn width(mut self, width: gpui::Pixels) -> Self {
|
||||
self.width = Some(width);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn height(mut self, height: gpui::Pixels) -> Self {
|
||||
self.height = Some(height);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn block(mut self, block: AnyView) -> Self {
|
||||
self.block = Some(block);
|
||||
self
|
||||
}
|
||||
|
||||
fn on_action_panel_info(&mut self, _: &PanelInfo, _cx: &mut ViewContext<Self>) {
|
||||
// struct Info;
|
||||
// let note = Notification::new(format!("You have clicked panel info on: {}", self.name)).id::<Info>();
|
||||
// cx.push_notification(note);
|
||||
}
|
||||
}
|
||||
|
||||
impl Panel for BlockContainer {
|
||||
fn panel_name(&self) -> &'static str {
|
||||
"BlockContainer"
|
||||
}
|
||||
|
||||
fn title(&self, _cx: &WindowContext) -> AnyElement {
|
||||
self.name.clone().into_any_element()
|
||||
}
|
||||
|
||||
fn title_style(&self, cx: &WindowContext) -> Option<TitleStyle> {
|
||||
self.title_bg.map(|bg| TitleStyle {
|
||||
background: bg,
|
||||
foreground: cx.theme().foreground,
|
||||
})
|
||||
}
|
||||
|
||||
fn closeable(&self, _cx: &WindowContext) -> bool {
|
||||
self.closeable
|
||||
}
|
||||
|
||||
fn zoomable(&self, _cx: &WindowContext) -> bool {
|
||||
self.zoomable
|
||||
}
|
||||
|
||||
fn popup_menu(&self, menu: PopupMenu, _cx: &WindowContext) -> PopupMenu {
|
||||
menu.track_focus(&self.focus_handle)
|
||||
.menu("Info", Box::new(PanelInfo))
|
||||
}
|
||||
|
||||
fn toolbar_buttons(&self, _cx: &WindowContext) -> Vec<Button> {
|
||||
vec![]
|
||||
}
|
||||
|
||||
fn dump(&self, _cx: &AppContext) -> DockItemState {
|
||||
DockItemState::new(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl EventEmitter<PanelEvent> for BlockContainer {}
|
||||
|
||||
impl FocusableView for BlockContainer {
|
||||
fn focus_handle(&self, _: &AppContext) -> gpui::FocusHandle {
|
||||
self.focus_handle.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for BlockContainer {
|
||||
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
|
||||
v_flex()
|
||||
.size_full()
|
||||
.track_focus(&self.focus_handle)
|
||||
.on_action(cx.listener(Self::on_action_panel_info))
|
||||
.when_some(self.block.clone(), |this, story| {
|
||||
this.child(v_flex().size_full().child(story))
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1 +1 @@
|
||||
pub mod block;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user