wip: refactor
This commit is contained in:
@@ -2,12 +2,42 @@ use gpui::*;
|
|||||||
use nostr_sdk::prelude::*;
|
use nostr_sdk::prelude::*;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
|
use crate::utils::get_room_id;
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Deserialize)]
|
#[derive(Clone, PartialEq, Eq, Deserialize)]
|
||||||
pub struct Room {
|
pub struct Room {
|
||||||
|
pub id: SharedString,
|
||||||
pub owner: PublicKey,
|
pub owner: PublicKey,
|
||||||
pub members: Vec<PublicKey>,
|
pub members: Vec<PublicKey>,
|
||||||
pub last_seen: Timestamp,
|
pub last_seen: Timestamp,
|
||||||
pub title: Option<String>,
|
pub title: Option<SharedString>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Room {
|
||||||
|
pub fn new(event: Event) -> Self {
|
||||||
|
let owner = event.pubkey;
|
||||||
|
let last_seen = event.created_at;
|
||||||
|
// Get all members from event's tag
|
||||||
|
let members: Vec<PublicKey> = event.tags.public_keys().copied().collect();
|
||||||
|
// Get title from event's tag
|
||||||
|
let title = if let Some(tag) = event.tags.find(TagKind::Title) {
|
||||||
|
tag.content().map(|s| s.to_owned().into())
|
||||||
|
} else {
|
||||||
|
// TODO: create random name?
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
|
// Get unique id based on members
|
||||||
|
let id = get_room_id(&owner, &members).into();
|
||||||
|
|
||||||
|
Self {
|
||||||
|
id,
|
||||||
|
title,
|
||||||
|
members,
|
||||||
|
last_seen,
|
||||||
|
owner,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
|
|||||||
@@ -27,6 +27,21 @@ pub fn get_keys_by_account(public_key: PublicKey) -> Result<Keys, anyhow::Error>
|
|||||||
Ok(keys)
|
Ok(keys)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_room_id(owner: &PublicKey, public_keys: &[PublicKey]) -> String {
|
||||||
|
let hex: Vec<String> = public_keys
|
||||||
|
.iter()
|
||||||
|
.map(|m| {
|
||||||
|
let hex = m.to_hex();
|
||||||
|
let split = &hex[..6];
|
||||||
|
|
||||||
|
split.to_owned()
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
let mems = hex.join("-");
|
||||||
|
|
||||||
|
format!("{}-{}", &owner.to_hex()[..6], mems)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn show_npub(public_key: PublicKey, len: usize) -> String {
|
pub fn show_npub(public_key: PublicKey, len: usize) -> String {
|
||||||
let bech32 = public_key.to_bech32().unwrap_or_default();
|
let bech32 = public_key.to_bech32().unwrap_or_default();
|
||||||
let separator = " ... ";
|
let separator = " ... ";
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use coop_ui::{
|
use coop_ui::{
|
||||||
button::Button,
|
button::Button,
|
||||||
dock::{Panel, PanelEvent, PanelState, TitleStyle},
|
dock::{Panel, PanelEvent, PanelState},
|
||||||
popup_menu::PopupMenu,
|
popup_menu::PopupMenu,
|
||||||
};
|
};
|
||||||
use gpui::*;
|
use gpui::*;
|
||||||
@@ -18,11 +18,13 @@ pub struct ChatPanel {
|
|||||||
zoomable: bool,
|
zoomable: bool,
|
||||||
focus_handle: FocusHandle,
|
focus_handle: FocusHandle,
|
||||||
// Room
|
// Room
|
||||||
|
id: SharedString,
|
||||||
room: View<ChatRoom>,
|
room: View<ChatRoom>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ChatPanel {
|
impl ChatPanel {
|
||||||
pub fn new(room: &Arc<Room>, cx: &mut WindowContext) -> View<Self> {
|
pub fn new(room: &Arc<Room>, cx: &mut WindowContext) -> View<Self> {
|
||||||
|
let id = room.id.clone();
|
||||||
let room = cx.new_view(|cx| {
|
let room = cx.new_view(|cx| {
|
||||||
let view = ChatRoom::new(room, cx);
|
let view = ChatRoom::new(room, cx);
|
||||||
// Load messages
|
// Load messages
|
||||||
@@ -38,24 +40,21 @@ impl ChatPanel {
|
|||||||
closeable: true,
|
closeable: true,
|
||||||
zoomable: true,
|
zoomable: true,
|
||||||
focus_handle: cx.focus_handle(),
|
focus_handle: cx.focus_handle(),
|
||||||
|
id,
|
||||||
room,
|
room,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Panel for ChatPanel {
|
impl Panel for ChatPanel {
|
||||||
fn panel_name(&self) -> &'static str {
|
fn panel_name(&self) -> SharedString {
|
||||||
"ChatPanel"
|
self.id.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn title(&self, _cx: &WindowContext) -> AnyElement {
|
fn title(&self, _cx: &WindowContext) -> AnyElement {
|
||||||
self.name.clone().into_any_element()
|
self.name.clone().into_any_element()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn title_style(&self, _cx: &WindowContext) -> Option<TitleStyle> {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
|
|
||||||
fn closeable(&self, _cx: &WindowContext) -> bool {
|
fn closeable(&self, _cx: &WindowContext) -> bool {
|
||||||
self.closeable
|
self.closeable
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -181,22 +181,20 @@ impl ChatRoom {
|
|||||||
cx.foreground_executor()
|
cx.foreground_executor()
|
||||||
.spawn({
|
.spawn({
|
||||||
let client = get_client();
|
let client = get_client();
|
||||||
|
let owner = self.owner;
|
||||||
let members = self.members.to_vec();
|
let members = self.members.to_vec();
|
||||||
|
|
||||||
|
let recv = Filter::new()
|
||||||
|
.kind(Kind::PrivateDirectMessage)
|
||||||
|
.author(owner)
|
||||||
|
.pubkeys(members.clone());
|
||||||
|
|
||||||
|
let send = Filter::new()
|
||||||
|
.kind(Kind::PrivateDirectMessage)
|
||||||
|
.authors(members)
|
||||||
|
.pubkey(owner);
|
||||||
|
|
||||||
async move {
|
async move {
|
||||||
let signer = client.signer().await.unwrap();
|
|
||||||
let public_key = signer.get_public_key().await.unwrap();
|
|
||||||
|
|
||||||
let recv = Filter::new()
|
|
||||||
.kind(Kind::PrivateDirectMessage)
|
|
||||||
.authors(members.clone())
|
|
||||||
.pubkey(public_key);
|
|
||||||
|
|
||||||
let send = Filter::new()
|
|
||||||
.kind(Kind::PrivateDirectMessage)
|
|
||||||
.author(public_key)
|
|
||||||
.pubkeys(members);
|
|
||||||
|
|
||||||
let events = async_cx
|
let events = async_cx
|
||||||
.background_executor()
|
.background_executor()
|
||||||
.spawn(async move { client.database().query(vec![recv, send]).await })
|
.spawn(async move { client.database().query(vec![recv, send]).await })
|
||||||
|
|||||||
@@ -126,7 +126,7 @@ impl RenderOnce for Item {
|
|||||||
)
|
)
|
||||||
.on_click(move |_, cx| {
|
.on_click(move |_, cx| {
|
||||||
cx.dispatch_action(Box::new(AddPanel {
|
cx.dispatch_action(Box::new(AddPanel {
|
||||||
room: self.room.clone(),
|
room: Arc::clone(&self.room),
|
||||||
position: coop_ui::dock::DockPlacement::Center,
|
position: coop_ui::dock::DockPlacement::Center,
|
||||||
}))
|
}))
|
||||||
})
|
})
|
||||||
@@ -136,31 +136,15 @@ impl RenderOnce for Item {
|
|||||||
pub struct InboxItem {
|
pub struct InboxItem {
|
||||||
room: Arc<Room>,
|
room: Arc<Room>,
|
||||||
metadata: Model<Option<Metadata>>,
|
metadata: Model<Option<Metadata>>,
|
||||||
pub(crate) sender: PublicKey,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl InboxItem {
|
impl InboxItem {
|
||||||
pub fn new(event: Event, cx: &mut ViewContext<'_, Self>) -> Self {
|
pub fn new(event: Event, cx: &mut ViewContext<'_, Self>) -> Self {
|
||||||
let sender = event.pubkey;
|
let room = Arc::new(Room::new(event));
|
||||||
let last_seen = event.created_at;
|
|
||||||
|
|
||||||
// Get all members from event's tag
|
|
||||||
let mut members: Vec<PublicKey> = event.tags.public_keys().copied().collect();
|
|
||||||
// Add sender to members
|
|
||||||
members.insert(0, sender);
|
|
||||||
|
|
||||||
// Get title from event's tag
|
|
||||||
let title = if let Some(tag) = event.tags.find(TagKind::Title) {
|
|
||||||
tag.content().map(|s| s.to_string())
|
|
||||||
} else {
|
|
||||||
// TODO: create random name?
|
|
||||||
None
|
|
||||||
};
|
|
||||||
|
|
||||||
let metadata = cx.new_model(|_| None);
|
let metadata = cx.new_model(|_| None);
|
||||||
|
|
||||||
// Request metadata
|
// Request metadata
|
||||||
_ = cx.global::<SignalRegistry>().tx.send(sender);
|
_ = cx.global::<SignalRegistry>().tx.send(room.owner);
|
||||||
|
|
||||||
// Reload when received metadata
|
// Reload when received metadata
|
||||||
cx.observe_global::<MetadataRegistry>(|chat, cx| {
|
cx.observe_global::<MetadataRegistry>(|chat, cx| {
|
||||||
@@ -168,22 +152,11 @@ impl InboxItem {
|
|||||||
})
|
})
|
||||||
.detach();
|
.detach();
|
||||||
|
|
||||||
let room = Arc::new(Room {
|
Self { room, metadata }
|
||||||
title,
|
|
||||||
members,
|
|
||||||
last_seen,
|
|
||||||
owner: sender,
|
|
||||||
});
|
|
||||||
|
|
||||||
Self {
|
|
||||||
room,
|
|
||||||
sender,
|
|
||||||
metadata,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load_metadata(&mut self, cx: &mut ViewContext<Self>) {
|
pub fn load_metadata(&mut self, cx: &mut ViewContext<Self>) {
|
||||||
let public_key = self.sender;
|
let public_key = self.room.owner;
|
||||||
let async_metadata = self.metadata.clone();
|
let async_metadata = self.metadata.clone();
|
||||||
let mut async_cx = cx.to_async();
|
let mut async_cx = cx.to_async();
|
||||||
|
|
||||||
@@ -205,9 +178,13 @@ impl InboxItem {
|
|||||||
.detach();
|
.detach();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn id(&self) -> String {
|
||||||
|
self.room.id.clone().into()
|
||||||
|
}
|
||||||
|
|
||||||
fn render_item(&self, cx: &mut ViewContext<Self>) -> impl IntoElement {
|
fn render_item(&self, cx: &mut ViewContext<Self>) -> impl IntoElement {
|
||||||
let metadata = self.metadata.read(cx).clone();
|
let metadata = self.metadata.read(cx).clone();
|
||||||
let room = self.room.clone();
|
let room = Arc::clone(&self.room);
|
||||||
|
|
||||||
Item::new(room, metadata)
|
Item::new(room, metadata)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ use nostr_sdk::prelude::*;
|
|||||||
use prelude::FluentBuilder;
|
use prelude::FluentBuilder;
|
||||||
use std::cmp::Reverse;
|
use std::cmp::Reverse;
|
||||||
|
|
||||||
use crate::{get_client, states::chat::ChatRegistry};
|
use crate::{get_client, states::chat::ChatRegistry, utils::get_room_id};
|
||||||
|
|
||||||
pub mod item;
|
pub mod item;
|
||||||
|
|
||||||
@@ -35,15 +35,18 @@ impl Inbox {
|
|||||||
let new_messages = state.new_messages.clone();
|
let new_messages = state.new_messages.clone();
|
||||||
|
|
||||||
// Get all current chats
|
// Get all current chats
|
||||||
let current: Vec<PublicKey> = items
|
let current_rooms: Vec<String> =
|
||||||
.iter()
|
items.iter().map(|item| item.model.read(cx).id()).collect();
|
||||||
.map(|item| item.model.read(cx).sender)
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
// Create view for only new chats
|
// Create view for new chats only
|
||||||
let new = new_messages
|
let new = new_messages
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter(|m| current.iter().any(|pk| pk == &m.event.pubkey))
|
.filter(|m| {
|
||||||
|
let keys = m.event.tags.public_keys().copied().collect::<Vec<_>>();
|
||||||
|
let nid = get_room_id(&m.event.pubkey, &keys);
|
||||||
|
|
||||||
|
current_rooms.iter().any(|id| id == &nid)
|
||||||
|
})
|
||||||
.map(|m| cx.new_view(|cx| InboxItem::new(m.event, cx)))
|
.map(|m| cx.new_view(|cx| InboxItem::new(m.event, cx)))
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use coop_ui::{
|
use coop_ui::{
|
||||||
button::Button,
|
button::Button,
|
||||||
dock::{Panel, PanelEvent, PanelState, TitleStyle},
|
dock::{Panel, PanelEvent, PanelState},
|
||||||
popup_menu::PopupMenu,
|
popup_menu::PopupMenu,
|
||||||
scroll::ScrollbarAxis,
|
scroll::ScrollbarAxis,
|
||||||
StyledExt,
|
StyledExt,
|
||||||
@@ -40,18 +40,14 @@ impl LeftDock {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Panel for LeftDock {
|
impl Panel for LeftDock {
|
||||||
fn panel_name(&self) -> &'static str {
|
fn panel_name(&self) -> SharedString {
|
||||||
"ChatPanel"
|
"LeftDock".into()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn title(&self, _cx: &WindowContext) -> AnyElement {
|
fn title(&self, _cx: &WindowContext) -> AnyElement {
|
||||||
self.name.clone().into_any_element()
|
self.name.clone().into_any_element()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn title_style(&self, _cx: &WindowContext) -> Option<TitleStyle> {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
|
|
||||||
fn closeable(&self, _cx: &WindowContext) -> bool {
|
fn closeable(&self, _cx: &WindowContext) -> bool {
|
||||||
self.closeable
|
self.closeable
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use coop_ui::{
|
use coop_ui::{
|
||||||
button::Button,
|
button::Button,
|
||||||
dock::{Panel, PanelEvent, PanelState, TitleStyle},
|
dock::{Panel, PanelEvent, PanelState},
|
||||||
popup_menu::PopupMenu,
|
popup_menu::PopupMenu,
|
||||||
theme::{ActiveTheme, Colorize},
|
theme::{ActiveTheme, Colorize},
|
||||||
StyledExt,
|
StyledExt,
|
||||||
@@ -30,18 +30,14 @@ impl WelcomePanel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Panel for WelcomePanel {
|
impl Panel for WelcomePanel {
|
||||||
fn panel_name(&self) -> &'static str {
|
fn panel_name(&self) -> SharedString {
|
||||||
"WelcomePanel"
|
"WelcomePanel".into()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn title(&self, _cx: &WindowContext) -> AnyElement {
|
fn title(&self, _cx: &WindowContext) -> AnyElement {
|
||||||
self.name.clone().into_any_element()
|
self.name.clone().into_any_element()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn title_style(&self, _cx: &WindowContext) -> Option<TitleStyle> {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
|
|
||||||
fn closeable(&self, _cx: &WindowContext) -> bool {
|
fn closeable(&self, _cx: &WindowContext) -> bool {
|
||||||
self.closeable
|
self.closeable
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,8 +22,8 @@ impl InvalidPanel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl Panel for InvalidPanel {
|
impl Panel for InvalidPanel {
|
||||||
fn panel_name(&self) -> &'static str {
|
fn panel_name(&self) -> SharedString {
|
||||||
"InvalidPanel"
|
"InvalidPanel".into()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn dump(&self, _cx: &AppContext) -> super::PanelState {
|
fn dump(&self, _cx: &AppContext) -> super::PanelState {
|
||||||
|
|||||||
@@ -32,18 +32,13 @@ pub trait Panel: EventEmitter<PanelEvent> + FocusableView {
|
|||||||
///
|
///
|
||||||
/// This is used to identify the panel when deserializing the panel.
|
/// This is used to identify the panel when deserializing the panel.
|
||||||
/// Once you have defined a panel name, this must not be changed.
|
/// Once you have defined a panel name, this must not be changed.
|
||||||
fn panel_name(&self) -> &'static str;
|
fn panel_name(&self) -> SharedString;
|
||||||
|
|
||||||
/// The title of the panel
|
/// The title of the panel
|
||||||
fn title(&self, _cx: &WindowContext) -> AnyElement {
|
fn title(&self, _cx: &WindowContext) -> AnyElement {
|
||||||
SharedString::from("Untitled").into_any_element()
|
SharedString::from("Untitled").into_any_element()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The theme of the panel title, default is `None`.
|
|
||||||
fn title_style(&self, _cx: &WindowContext) -> Option<TitleStyle> {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Whether the panel can be closed, default is `true`.
|
/// Whether the panel can be closed, default is `true`.
|
||||||
fn closeable(&self, _cx: &WindowContext) -> bool {
|
fn closeable(&self, _cx: &WindowContext) -> bool {
|
||||||
true
|
true
|
||||||
@@ -71,9 +66,8 @@ pub trait Panel: EventEmitter<PanelEvent> + FocusableView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub trait PanelView: 'static + Send + Sync {
|
pub trait PanelView: 'static + Send + Sync {
|
||||||
fn panel_name(&self, _cx: &AppContext) -> &'static str;
|
fn panel_name(&self, cx: &WindowContext) -> SharedString;
|
||||||
fn title(&self, _cx: &WindowContext) -> AnyElement;
|
fn title(&self, _cx: &WindowContext) -> AnyElement;
|
||||||
fn title_style(&self, _cx: &WindowContext) -> Option<TitleStyle>;
|
|
||||||
fn closeable(&self, cx: &WindowContext) -> bool;
|
fn closeable(&self, cx: &WindowContext) -> bool;
|
||||||
fn zoomable(&self, cx: &WindowContext) -> bool;
|
fn zoomable(&self, cx: &WindowContext) -> bool;
|
||||||
fn popup_menu(&self, menu: PopupMenu, cx: &WindowContext) -> PopupMenu;
|
fn popup_menu(&self, menu: PopupMenu, cx: &WindowContext) -> PopupMenu;
|
||||||
@@ -84,7 +78,7 @@ pub trait PanelView: 'static + Send + Sync {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Panel> PanelView for View<T> {
|
impl<T: Panel> PanelView for View<T> {
|
||||||
fn panel_name(&self, cx: &AppContext) -> &'static str {
|
fn panel_name(&self, cx: &WindowContext) -> SharedString {
|
||||||
self.read(cx).panel_name()
|
self.read(cx).panel_name()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,10 +86,6 @@ impl<T: Panel> PanelView for View<T> {
|
|||||||
self.read(cx).title(cx)
|
self.read(cx).title(cx)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn title_style(&self, cx: &WindowContext) -> Option<TitleStyle> {
|
|
||||||
self.read(cx).title_style(cx)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn closeable(&self, cx: &WindowContext) -> bool {
|
fn closeable(&self, cx: &WindowContext) -> bool {
|
||||||
self.read(cx).closeable(cx)
|
self.read(cx).closeable(cx)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,5 @@
|
|||||||
use gpui::{
|
use gpui::*;
|
||||||
prelude::FluentBuilder as _, AppContext, Axis, DismissEvent, EventEmitter, FocusHandle,
|
use prelude::FluentBuilder;
|
||||||
FocusableView, IntoElement, ParentElement, Pixels, Render, Styled, Subscription, View,
|
|
||||||
ViewContext, VisualContext, WeakView,
|
|
||||||
};
|
|
||||||
use smallvec::SmallVec;
|
use smallvec::SmallVec;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
@@ -28,8 +25,8 @@ pub struct StackPanel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Panel for StackPanel {
|
impl Panel for StackPanel {
|
||||||
fn panel_name(&self) -> &'static str {
|
fn panel_name(&self) -> SharedString {
|
||||||
"StackPanel"
|
"StackPanel".into()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn title(&self, _cx: &gpui::WindowContext) -> gpui::AnyElement {
|
fn title(&self, _cx: &gpui::WindowContext) -> gpui::AnyElement {
|
||||||
|
|||||||
@@ -71,7 +71,6 @@ pub struct TabPanel {
|
|||||||
/// If this is true, the Panel closeable will follow the active panel's closeable,
|
/// If this is true, the Panel closeable will follow the active panel's closeable,
|
||||||
/// otherwise this TabPanel will not able to close
|
/// otherwise this TabPanel will not able to close
|
||||||
pub(crate) closeable: bool,
|
pub(crate) closeable: bool,
|
||||||
|
|
||||||
tab_bar_scroll_handle: ScrollHandle,
|
tab_bar_scroll_handle: ScrollHandle,
|
||||||
is_zoomed: bool,
|
is_zoomed: bool,
|
||||||
is_collapsed: bool,
|
is_collapsed: bool,
|
||||||
@@ -80,8 +79,8 @@ pub struct TabPanel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Panel for TabPanel {
|
impl Panel for TabPanel {
|
||||||
fn panel_name(&self) -> &'static str {
|
fn panel_name(&self) -> SharedString {
|
||||||
"TabPanel"
|
"TabPanel".into()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn title(&self, cx: &WindowContext) -> gpui::AnyElement {
|
fn title(&self, cx: &WindowContext) -> gpui::AnyElement {
|
||||||
@@ -180,25 +179,32 @@ impl TabPanel {
|
|||||||
active: bool,
|
active: bool,
|
||||||
cx: &mut ViewContext<Self>,
|
cx: &mut ViewContext<Self>,
|
||||||
) {
|
) {
|
||||||
assert_ne!(
|
|
||||||
panel.panel_name(cx),
|
|
||||||
"StackPanel",
|
|
||||||
"can not allows add `StackPanel` to `TabPanel`"
|
|
||||||
);
|
|
||||||
|
|
||||||
if self
|
if self
|
||||||
.panels
|
.panels
|
||||||
.iter()
|
.iter()
|
||||||
.any(|p| p.view().entity_id() == panel.view().entity_id())
|
.any(|p| p.panel_name(cx) == panel.panel_name(cx))
|
||||||
{
|
{
|
||||||
|
// set the active panel to the matched panel
|
||||||
|
if active {
|
||||||
|
if let Some(ix) = self
|
||||||
|
.panels
|
||||||
|
.iter()
|
||||||
|
.position(|p| p.panel_name(cx) == panel.panel_name(cx))
|
||||||
|
{
|
||||||
|
self.set_active_ix(ix, cx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.panels.push(panel);
|
self.panels.push(panel);
|
||||||
|
|
||||||
// set the active panel to the new panel
|
// set the active panel to the new panel
|
||||||
if active {
|
if active {
|
||||||
self.set_active_ix(self.panels.len() - 1, cx);
|
self.set_active_ix(self.panels.len() - 1, cx);
|
||||||
}
|
}
|
||||||
|
|
||||||
cx.emit(PanelEvent::LayoutChanged);
|
cx.emit(PanelEvent::LayoutChanged);
|
||||||
cx.notify();
|
cx.notify();
|
||||||
}
|
}
|
||||||
@@ -466,7 +472,6 @@ impl TabPanel {
|
|||||||
|
|
||||||
if self.panels.len() == 1 && panel_style == PanelStyle::Default {
|
if self.panels.len() == 1 && panel_style == PanelStyle::Default {
|
||||||
let panel = self.panels.first().unwrap();
|
let panel = self.panels.first().unwrap();
|
||||||
let title_style = panel.title_style(cx);
|
|
||||||
|
|
||||||
return h_flex()
|
return h_flex()
|
||||||
.justify_between()
|
.justify_between()
|
||||||
@@ -477,9 +482,6 @@ impl TabPanel {
|
|||||||
.px_3()
|
.px_3()
|
||||||
.when(left_dock_button.is_some(), |this| this.pl_2())
|
.when(left_dock_button.is_some(), |this| this.pl_2())
|
||||||
.when(right_dock_button.is_some(), |this| this.pr_2())
|
.when(right_dock_button.is_some(), |this| this.pr_2())
|
||||||
.when_some(title_style, |this, theme| {
|
|
||||||
this.bg(theme.background).text_color(theme.foreground)
|
|
||||||
})
|
|
||||||
.when(
|
.when(
|
||||||
left_dock_button.is_some() || bottom_dock_button.is_some(),
|
left_dock_button.is_some() || bottom_dock_button.is_some(),
|
||||||
|this| {
|
|this| {
|
||||||
|
|||||||
@@ -1,10 +1,4 @@
|
|||||||
use gpui::{
|
use gpui::*;
|
||||||
canvas, div, point, px, size, AnyElement, AppContext, Bounds, DismissEvent, DragMoveEvent,
|
|
||||||
Entity, EntityId, EventEmitter, FocusHandle, FocusableView, Half, InteractiveElement,
|
|
||||||
IntoElement, MouseButton, MouseDownEvent, MouseUpEvent, ParentElement, Pixels, Point, Render,
|
|
||||||
ScrollHandle, Size, StatefulInteractiveElement, Styled, ViewContext, VisualContext, WeakView,
|
|
||||||
WindowContext,
|
|
||||||
};
|
|
||||||
use std::{
|
use std::{
|
||||||
cell::Cell,
|
cell::Cell,
|
||||||
fmt::{Debug, Formatter},
|
fmt::{Debug, Formatter},
|
||||||
@@ -92,8 +86,8 @@ pub struct Tiles {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Panel for Tiles {
|
impl Panel for Tiles {
|
||||||
fn panel_name(&self) -> &'static str {
|
fn panel_name(&self) -> SharedString {
|
||||||
"Tiles"
|
"Tiles".into()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn title(&self, _cx: &WindowContext) -> AnyElement {
|
fn title(&self, _cx: &WindowContext) -> AnyElement {
|
||||||
|
|||||||
Reference in New Issue
Block a user