wip: refactor
This commit is contained in:
@@ -17,7 +17,7 @@ use crate::states::account::AccountRegistry;
|
||||
#[derive(Clone, PartialEq, Eq, Deserialize)]
|
||||
pub struct AddPanel {
|
||||
pub title: Option<String>,
|
||||
pub receiver: PublicKey,
|
||||
pub from: PublicKey,
|
||||
}
|
||||
|
||||
impl_actions!(dock, [AddPanel]);
|
||||
@@ -98,7 +98,7 @@ impl AppView {
|
||||
}
|
||||
|
||||
fn on_action_add_panel(&mut self, action: &AddPanel, cx: &mut ViewContext<Self>) {
|
||||
let chat_panel = Arc::new(ChatPanel::new(action.receiver, cx));
|
||||
let chat_panel = Arc::new(ChatPanel::new(action.from, cx));
|
||||
|
||||
self.dock.update(cx, |dock_area, cx| {
|
||||
dock_area.add_panel(chat_panel, DockPlacement::Center, cx);
|
||||
|
||||
66
crates/app/src/views/dock/chat/messages.rs
Normal file
66
crates/app/src/views/dock/chat/messages.rs
Normal file
@@ -0,0 +1,66 @@
|
||||
use gpui::*;
|
||||
use nostr_sdk::prelude::*;
|
||||
|
||||
use crate::get_client;
|
||||
|
||||
pub struct Messages {
|
||||
messages: Model<Option<Events>>,
|
||||
}
|
||||
|
||||
impl Messages {
|
||||
pub fn new(from: PublicKey, cx: &mut ViewContext<'_, Self>) -> Self {
|
||||
let messages = cx.new_model(|_| None);
|
||||
let async_messages = messages.clone();
|
||||
|
||||
let mut async_cx = cx.to_async();
|
||||
|
||||
cx.foreground_executor()
|
||||
.spawn(async move {
|
||||
let client = get_client();
|
||||
let signer = client.signer().await.unwrap();
|
||||
let public_key = signer.get_public_key().await.unwrap();
|
||||
|
||||
let recv_filter = Filter::new()
|
||||
.kind(Kind::PrivateDirectMessage)
|
||||
.author(from)
|
||||
.pubkey(public_key);
|
||||
|
||||
let sender_filter = Filter::new()
|
||||
.kind(Kind::PrivateDirectMessage)
|
||||
.author(public_key)
|
||||
.pubkey(from);
|
||||
|
||||
let events = async_cx
|
||||
.background_executor()
|
||||
.spawn(async move {
|
||||
client
|
||||
.database()
|
||||
.query(vec![recv_filter, sender_filter])
|
||||
.await
|
||||
})
|
||||
.await;
|
||||
|
||||
if let Ok(events) = events {
|
||||
_ = async_cx.update_model(&async_messages, |a, b| {
|
||||
*a = Some(events);
|
||||
b.notify();
|
||||
});
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
|
||||
Self { messages }
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for Messages {
|
||||
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
|
||||
let mut content = div().size_full().flex().flex_col().justify_end();
|
||||
|
||||
if let Some(messages) = self.messages.read(cx).as_ref() {
|
||||
content = content.children(messages.clone().into_iter().map(|m| div().child(m.content)))
|
||||
}
|
||||
|
||||
div().flex_1().child(content)
|
||||
}
|
||||
}
|
||||
@@ -7,9 +7,10 @@ use coop_ui::{
|
||||
Sizable,
|
||||
};
|
||||
use gpui::*;
|
||||
use messages::Messages;
|
||||
use nostr_sdk::*;
|
||||
|
||||
pub mod list;
|
||||
pub mod messages;
|
||||
|
||||
pub struct ChatPanel {
|
||||
// Panel
|
||||
@@ -18,13 +19,14 @@ pub struct ChatPanel {
|
||||
zoomable: bool,
|
||||
focus_handle: FocusHandle,
|
||||
// Chat Room
|
||||
receiver: PublicKey,
|
||||
messages: View<Messages>,
|
||||
input: View<TextInput>,
|
||||
}
|
||||
|
||||
impl ChatPanel {
|
||||
pub fn new(receiver: PublicKey, cx: &mut WindowContext) -> View<Self> {
|
||||
pub fn new(from: PublicKey, cx: &mut WindowContext) -> View<Self> {
|
||||
let input = cx.new_view(TextInput::new);
|
||||
let messages = cx.new_view(|cx| Messages::new(from, cx));
|
||||
|
||||
input.update(cx, |input, _cx| {
|
||||
input.set_placeholder("Message");
|
||||
@@ -35,7 +37,7 @@ impl ChatPanel {
|
||||
closeable: true,
|
||||
zoomable: true,
|
||||
focus_handle: cx.focus_handle(),
|
||||
receiver,
|
||||
messages,
|
||||
input,
|
||||
})
|
||||
}
|
||||
@@ -89,14 +91,7 @@ impl Render for ChatPanel {
|
||||
.size_full()
|
||||
.flex()
|
||||
.flex_col()
|
||||
.child(
|
||||
div()
|
||||
.flex_1()
|
||||
.flex()
|
||||
.items_center()
|
||||
.justify_center()
|
||||
.child(self.receiver.to_hex()),
|
||||
)
|
||||
.child(self.messages.clone())
|
||||
.child(
|
||||
div()
|
||||
.flex_shrink_0()
|
||||
|
||||
@@ -130,7 +130,7 @@ impl RenderOnce for ChatItem {
|
||||
.on_click(move |_, cx| {
|
||||
cx.dispatch_action(Box::new(AddPanel {
|
||||
title: self.title.clone(),
|
||||
receiver: self.public_key,
|
||||
from: self.public_key,
|
||||
}))
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,3 +1,18 @@
|
||||
Copyright 2024 Longbridge <https://longbridge.com>
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
@@ -174,28 +189,3 @@ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
@@ -138,10 +138,18 @@ impl Dock {
|
||||
Self::subscribe_panel_events(dock_area.clone(), &panel, cx);
|
||||
|
||||
if !open {
|
||||
if let DockItem::Tabs { view, .. } = panel.clone() {
|
||||
view.update(cx, |panel, cx| {
|
||||
panel.set_collapsed(true, cx);
|
||||
});
|
||||
match panel.clone() {
|
||||
DockItem::Tabs { view, .. } => {
|
||||
view.update(cx, |panel, cx| {
|
||||
panel.set_collapsed(true, cx);
|
||||
});
|
||||
}
|
||||
DockItem::Split { items, .. } => {
|
||||
for item in items {
|
||||
item.set_collapsed(true, cx);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use gpui::{
|
||||
div, prelude::FluentBuilder, px, rems, AnchorCorner, AppContext, DefiniteLength, DismissEvent,
|
||||
DragMoveEvent, Empty, Entity, EventEmitter, FocusHandle, FocusableView,
|
||||
@@ -7,7 +5,12 @@ use gpui::{
|
||||
SharedString, StatefulInteractiveElement, Styled, View, ViewContext, VisualContext as _,
|
||||
WeakView, WindowContext,
|
||||
};
|
||||
use std::sync::Arc;
|
||||
|
||||
use super::{
|
||||
ClosePanel, DockArea, DockItemState, DockPlacement, Panel, PanelEvent, PanelStyle, PanelView,
|
||||
StackPanel, ToggleZoom,
|
||||
};
|
||||
use crate::{
|
||||
button::{Button, ButtonVariants as _},
|
||||
dock::DockItemInfo,
|
||||
@@ -18,11 +21,6 @@ use crate::{
|
||||
v_flex, AxisExt, IconName, Placement, Selectable, Sizable,
|
||||
};
|
||||
|
||||
use super::{
|
||||
ClosePanel, DockArea, DockItemState, DockPlacement, Panel, PanelEvent, PanelStyle, PanelView,
|
||||
StackPanel, ToggleZoom,
|
||||
};
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
struct TabState {
|
||||
closeable: bool,
|
||||
@@ -173,6 +171,15 @@ impl TabPanel {
|
||||
|
||||
/// Add a panel to the end of the tabs
|
||||
pub fn add_panel(&mut self, panel: Arc<dyn PanelView>, cx: &mut ViewContext<Self>) {
|
||||
self.add_panel_with_active(panel, true, cx);
|
||||
}
|
||||
|
||||
fn add_panel_with_active(
|
||||
&mut self,
|
||||
panel: Arc<dyn PanelView>,
|
||||
active: bool,
|
||||
cx: &mut ViewContext<Self>,
|
||||
) {
|
||||
assert_ne!(
|
||||
panel.panel_name(cx),
|
||||
"StackPanel",
|
||||
@@ -189,7 +196,9 @@ impl TabPanel {
|
||||
|
||||
self.panels.push(panel);
|
||||
// set the active panel to the new panel
|
||||
self.set_active_ix(self.panels.len() - 1, cx);
|
||||
if active {
|
||||
self.set_active_ix(self.panels.len() - 1, cx);
|
||||
}
|
||||
cx.emit(PanelEvent::LayoutChanged);
|
||||
cx.notify();
|
||||
}
|
||||
@@ -543,6 +552,7 @@ impl TabPanel {
|
||||
)
|
||||
.children(self.panels.iter().enumerate().map(|(ix, panel)| {
|
||||
let mut active = ix == self.active_ix;
|
||||
let disabled = self.is_collapsed;
|
||||
|
||||
// Always not show active tab style, if the panel is collapsed
|
||||
if self.is_collapsed {
|
||||
@@ -552,31 +562,34 @@ impl TabPanel {
|
||||
Tab::new(("tab", ix), panel.title(cx))
|
||||
.py_2()
|
||||
.selected(active)
|
||||
.on_click(cx.listener(move |view, _, cx| {
|
||||
view.set_active_ix(ix, cx);
|
||||
}))
|
||||
.when(state.draggable, |this| {
|
||||
this.on_drag(
|
||||
DragPanel::new(panel.clone(), view.clone()),
|
||||
|drag, _, cx| {
|
||||
cx.stop_propagation();
|
||||
cx.new_view(|_| drag.clone())
|
||||
},
|
||||
)
|
||||
})
|
||||
.when(state.droppable, |this| {
|
||||
this.drag_over::<DragPanel>(|this, _, cx| {
|
||||
this.rounded_l_none()
|
||||
.border_l_2()
|
||||
.border_r_0()
|
||||
.border_color(cx.theme().drag_border)
|
||||
.disabled(disabled)
|
||||
.when(!disabled, |this| {
|
||||
this.on_click(cx.listener(move |view, _, cx| {
|
||||
view.set_active_ix(ix, cx);
|
||||
}))
|
||||
.when(state.draggable, |this| {
|
||||
this.on_drag(
|
||||
DragPanel::new(panel.clone(), view.clone()),
|
||||
|drag, _, cx| {
|
||||
cx.stop_propagation();
|
||||
cx.new_view(|_| drag.clone())
|
||||
},
|
||||
)
|
||||
})
|
||||
.when(state.droppable, |this| {
|
||||
this.drag_over::<DragPanel>(|this, _, cx| {
|
||||
this.rounded_l_none()
|
||||
.border_l_2()
|
||||
.border_r_0()
|
||||
.border_color(cx.theme().drag_border)
|
||||
})
|
||||
.on_drop(cx.listener(
|
||||
move |this, drag: &DragPanel, cx| {
|
||||
this.will_split_placement = None;
|
||||
this.on_drop(drag, Some(ix), true, cx)
|
||||
},
|
||||
))
|
||||
})
|
||||
.on_drop(cx.listener(
|
||||
move |this, drag: &DragPanel, cx| {
|
||||
this.will_split_placement = None;
|
||||
this.on_drop(drag, Some(ix), cx)
|
||||
},
|
||||
))
|
||||
})
|
||||
}))
|
||||
.child(
|
||||
@@ -597,7 +610,7 @@ impl TabPanel {
|
||||
None
|
||||
};
|
||||
|
||||
this.on_drop(drag, ix, cx)
|
||||
this.on_drop(drag, ix, false, cx)
|
||||
}))
|
||||
}),
|
||||
)
|
||||
@@ -620,6 +633,10 @@ impl TabPanel {
|
||||
}
|
||||
|
||||
fn render_active_panel(&self, state: TabState, cx: &mut ViewContext<Self>) -> impl IntoElement {
|
||||
if self.is_collapsed {
|
||||
return Empty {}.into_any_element();
|
||||
}
|
||||
|
||||
self.active_panel()
|
||||
.map(|panel| {
|
||||
div()
|
||||
@@ -658,7 +675,7 @@ impl TabPanel {
|
||||
})
|
||||
.group_drag_over::<DragPanel>("", |this| this.visible())
|
||||
.on_drop(cx.listener(|this, drag: &DragPanel, cx| {
|
||||
this.on_drop(drag, None, cx)
|
||||
this.on_drop(drag, None, true, cx)
|
||||
})),
|
||||
)
|
||||
})
|
||||
@@ -688,17 +705,28 @@ impl TabPanel {
|
||||
cx.notify()
|
||||
}
|
||||
|
||||
fn on_drop(&mut self, drag: &DragPanel, ix: Option<usize>, cx: &mut ViewContext<Self>) {
|
||||
/// Handle the drop event when dragging a panel
|
||||
///
|
||||
/// - `active` - When true, the panel will be active after the drop
|
||||
fn on_drop(
|
||||
&mut self,
|
||||
drag: &DragPanel,
|
||||
ix: Option<usize>,
|
||||
active: bool,
|
||||
cx: &mut ViewContext<Self>,
|
||||
) {
|
||||
let panel = drag.panel.clone();
|
||||
let is_same_tab = drag.tab_panel == *cx.view();
|
||||
|
||||
// If target is same tab, and it is only one panel, do nothing.
|
||||
if is_same_tab && ix.is_none() {
|
||||
#[allow(clippy::if_same_then_else)]
|
||||
if self.will_split_placement.is_none() {
|
||||
return;
|
||||
} else if self.panels.len() == 1 {
|
||||
return;
|
||||
} else {
|
||||
#[allow(clippy::collapsible_else_if)]
|
||||
if self.panels.len() == 1 {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -721,7 +749,7 @@ impl TabPanel {
|
||||
} else if let Some(ix) = ix {
|
||||
self.insert_panel_at(panel, ix, cx)
|
||||
} else {
|
||||
self.add_panel(panel, cx)
|
||||
self.add_panel_with_active(panel, active, cx)
|
||||
}
|
||||
|
||||
self.remove_self_if_empty(cx);
|
||||
|
||||
@@ -81,7 +81,7 @@ pub trait DropdownDelegate: Sized {
|
||||
}
|
||||
|
||||
fn perform_search(&mut self, _query: &str, _cx: &mut ViewContext<Dropdown<Self>>) -> Task<()> {
|
||||
Task::Ready(Some(()))
|
||||
Task::ready(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -178,11 +178,9 @@ where
|
||||
}
|
||||
|
||||
fn perform_search(&mut self, query: &str, cx: &mut ViewContext<List<Self>>) -> Task<()> {
|
||||
self.dropdown
|
||||
.upgrade()
|
||||
.map_or(Task::Ready(None), |dropdown| {
|
||||
dropdown.update(cx, |_, cx| self.delegate.perform_search(query, cx))
|
||||
})
|
||||
self.dropdown.upgrade().map_or(Task::ready(()), |dropdown| {
|
||||
dropdown.update(cx, |_, cx| self.delegate.perform_search(query, cx))
|
||||
})
|
||||
}
|
||||
|
||||
fn set_selected_index(&mut self, ix: Option<usize>, _: &mut ViewContext<List<Self>>) {
|
||||
@@ -285,7 +283,7 @@ impl<T: DropdownItem + Clone> DropdownDelegate for SearchableVec<T> {
|
||||
.cloned()
|
||||
.collect();
|
||||
|
||||
Task::Ready(Some(()))
|
||||
Task::ready(())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -42,7 +42,6 @@ pub mod skeleton;
|
||||
pub mod slider;
|
||||
pub mod switch;
|
||||
pub mod tab;
|
||||
pub mod table;
|
||||
pub mod theme;
|
||||
pub mod tooltip;
|
||||
|
||||
@@ -78,5 +77,4 @@ pub fn init(cx: &mut gpui::AppContext) {
|
||||
modal::init(cx);
|
||||
popover::init(cx);
|
||||
popup_menu::init(cx);
|
||||
table::init(cx);
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ pub trait ListDelegate: Sized + 'static {
|
||||
/// When Query Input change, this method will be called.
|
||||
/// You can perform search here.
|
||||
fn perform_search(&mut self, query: &str, cx: &mut ViewContext<List<Self>>) -> Task<()> {
|
||||
Task::Ready(Some(()))
|
||||
Task::ready(())
|
||||
}
|
||||
|
||||
/// Return the number of items in the list.
|
||||
@@ -126,7 +126,7 @@ where
|
||||
enable_scrollbar: true,
|
||||
loading: false,
|
||||
size: Size::default(),
|
||||
_search_task: Task::Ready(None),
|
||||
_search_task: Task::ready(()),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -121,7 +121,7 @@ impl Element for ScrollableMask {
|
||||
bounds,
|
||||
border_widths: Edges::all(px(1.0)),
|
||||
border_color: color,
|
||||
background: gpui::transparent_white(),
|
||||
background: gpui::transparent_white().into(),
|
||||
corner_radii: Corners::all(px(0.)),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -597,7 +597,7 @@ impl Element for Scrollbar {
|
||||
cx.paint_quad(PaintQuad {
|
||||
bounds,
|
||||
corner_radii: (0.).into(),
|
||||
background: gpui::transparent_black(),
|
||||
background: gpui::transparent_black().into(),
|
||||
border_widths: if is_vertical {
|
||||
Edges {
|
||||
top: px(0.),
|
||||
|
||||
@@ -42,6 +42,12 @@ impl Tab {
|
||||
self.suffix = Some(suffix.into());
|
||||
self
|
||||
}
|
||||
|
||||
/// Set disabled state to the tab
|
||||
pub fn disabled(mut self, disabled: bool) -> Self {
|
||||
self.disabled = disabled;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl Selectable for Tab {
|
||||
@@ -72,9 +78,11 @@ impl Styled for Tab {
|
||||
impl RenderOnce for Tab {
|
||||
fn render(self, cx: &mut WindowContext) -> impl IntoElement {
|
||||
let (text_color, bg_color) = match (self.selected, self.disabled) {
|
||||
(true, _) => (cx.theme().tab_active_foreground, cx.theme().tab_active),
|
||||
(false, true) => (cx.theme().tab_foreground.opacity(0.5), cx.theme().tab),
|
||||
(true, false) => (cx.theme().tab_active_foreground, cx.theme().tab_active),
|
||||
(false, false) => (cx.theme().muted_foreground, cx.theme().tab),
|
||||
// disabled
|
||||
(true, true) => (cx.theme().muted_foreground, cx.theme().tab_active),
|
||||
(false, true) => (cx.theme().muted_foreground, cx.theme().tab),
|
||||
};
|
||||
|
||||
self.base
|
||||
@@ -89,7 +97,6 @@ impl RenderOnce for Tab {
|
||||
.border_color(cx.theme().transparent)
|
||||
.when(self.selected, |this| this.border_color(cx.theme().border))
|
||||
.text_sm()
|
||||
.when(self.disabled, |this| this)
|
||||
.when_some(self.prefix, |this, prefix| {
|
||||
this.child(prefix).text_color(text_color)
|
||||
})
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user