From 4b211bfe21e257935296aeaf7e3fc08c559b3867 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Thu, 19 Dec 2024 10:29:58 +0800 Subject: [PATCH] panel: Add `set_active`, `set_zoomed` method to Panel trait. (#501) It will called when the panel active, zoom state changed. --- crates/story/src/lib.rs | 14 +++++++++ crates/ui/src/dock/panel.rs | 55 ++++++++++++++++++++++++++------- crates/ui/src/dock/tab_panel.rs | 34 ++++++++++++++++++++ 3 files changed, 92 insertions(+), 11 deletions(-) diff --git a/crates/story/src/lib.rs b/crates/story/src/lib.rs index 5d7b64e0..015aad95 100644 --- a/crates/story/src/lib.rs +++ b/crates/story/src/lib.rs @@ -78,6 +78,12 @@ pub fn init(cx: &mut AppContext) { let view = cx.new_view(|cx| { let (title, description, closeable, zoomable, story) = story_state.to_story(cx); let mut container = StoryContainer::new(cx).story(story, story_state.story_klass); + + cx.on_focus_in(&container.focus_handle, |this: &mut StoryContainer, _| { + println!("StoryContainer focus in: {}", this.name); + }) + .detach(); + container.name = title.into(); container.description = description.into(); container.closeable = closeable; @@ -298,6 +304,14 @@ impl Panel for StoryContainer { self.zoomable } + fn set_zoomed(&self, zoomed: bool, _cx: &ViewContext) { + println!("panel: {} zoomed: {}", self.name, zoomed); + } + + fn set_active(&self, active: bool, _cx: &ViewContext) { + println!("panel: {} active: {}", self.name, active); + } + fn popup_menu(&self, menu: PopupMenu, _cx: &WindowContext) -> PopupMenu { menu.track_focus(&self.focus_handle) .menu("Info", Box::new(ShowPanelInfo)) diff --git a/crates/ui/src/dock/panel.rs b/crates/ui/src/dock/panel.rs index e5cdfd5f..5b880642 100644 --- a/crates/ui/src/dock/panel.rs +++ b/crates/ui/src/dock/panel.rs @@ -3,7 +3,7 @@ use std::{collections::HashMap, sync::Arc}; use crate::{button::Button, popup_menu::PopupMenu}; use gpui::{ AnyElement, AnyView, AppContext, EventEmitter, FocusHandle, FocusableView, Global, Hsla, - IntoElement, SharedString, View, WeakView, WindowContext, + IntoElement, SharedString, View, ViewContext, WeakView, WindowContext, }; use rust_i18n::t; @@ -30,6 +30,8 @@ pub struct TitleStyle { pub foreground: Hsla, } +/// The Panel trait used to define the panel. +#[allow(unused_variables)] pub trait Panel: EventEmitter + FocusableView { /// The name of the panel used to serialize, deserialize and identify the panel. /// @@ -38,47 +40,66 @@ pub trait Panel: EventEmitter + FocusableView { fn panel_name(&self) -> &'static str; /// The title of the panel - fn title(&self, _cx: &WindowContext) -> AnyElement { + fn title(&self, cx: &WindowContext) -> AnyElement { SharedString::from(t!("Dock.Unnamed")).into_any_element() } /// The theme of the panel title, default is `None`. - fn title_style(&self, _cx: &WindowContext) -> Option { + fn title_style(&self, cx: &WindowContext) -> Option { None } /// Whether the panel can be closed, default is `true`. - fn closeable(&self, _cx: &WindowContext) -> bool { + fn closeable(&self, cx: &WindowContext) -> bool { true } /// Return true if the panel is zoomable, default is `false`. - fn zoomable(&self, _cx: &WindowContext) -> bool { + fn zoomable(&self, cx: &WindowContext) -> bool { true } + /// Set active state of the panel. + /// + /// This method will be called when the panel is active or inactive. + /// + /// The last_active_panel and current_active_panel will be touched when the panel is active. + #[allow(unused_variables)] + fn set_active(&self, active: bool, cx: &ViewContext) {} + + /// Set zoomed state of the panel. + /// + /// This method will be called when the panel is zoomed or unzoomed. + /// + /// Only current Panel will touch this method. + fn set_zoomed(&self, zoomed: bool, cx: &ViewContext) {} + /// The addition popup menu of the panel, default is `None`. - fn popup_menu(&self, this: PopupMenu, _cx: &WindowContext) -> PopupMenu { + fn popup_menu(&self, this: PopupMenu, cx: &WindowContext) -> PopupMenu { this } /// The addition toolbar buttons of the panel used to show in the right of the title bar, default is `None`. - fn toolbar_buttons(&self, _cx: &WindowContext) -> Vec