logiguard fork v3: full patch set on verified 8c74db0 tree
Includes prior-session patches (carry forward so the app compiles): - crates/gpui/build.rs: cross-compile manifest fix - crates/gpui/src/platform.rs: PlatformWindow::activate_with_token trait method - crates/gpui/src/window.rs: Window::activate_with_token public API - crates/gpui_linux/src/linux/wayland/window.rs: WaylandWindow::activate_with_token + activate() keyboard-serial fix Plus the focus-serial fix: - serial.rs: SerialKind::KeyboardEnter - client.rs: store wl_keyboard.enter serial; latest_serial_of() Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
19
crates/command_palette_hooks/Cargo.toml
Normal file
19
crates/command_palette_hooks/Cargo.toml
Normal file
@@ -0,0 +1,19 @@
|
||||
[package]
|
||||
name = "command_palette_hooks"
|
||||
version = "0.1.0"
|
||||
edition.workspace = true
|
||||
publish.workspace = true
|
||||
license = "GPL-3.0-or-later"
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[lib]
|
||||
path = "src/command_palette_hooks.rs"
|
||||
doctest = false
|
||||
|
||||
[dependencies]
|
||||
collections.workspace = true
|
||||
derive_more.workspace = true
|
||||
gpui.workspace = true
|
||||
workspace.workspace = true
|
||||
1
crates/command_palette_hooks/LICENSE-GPL
Symbolic link
1
crates/command_palette_hooks/LICENSE-GPL
Symbolic link
@@ -0,0 +1 @@
|
||||
../../LICENSE-GPL
|
||||
153
crates/command_palette_hooks/src/command_palette_hooks.rs
Normal file
153
crates/command_palette_hooks/src/command_palette_hooks.rs
Normal file
@@ -0,0 +1,153 @@
|
||||
//! Provides hooks for customizing the behavior of the command palette.
|
||||
|
||||
#![deny(missing_docs)]
|
||||
|
||||
use std::{any::TypeId, rc::Rc};
|
||||
|
||||
use collections::HashSet;
|
||||
use derive_more::{Deref, DerefMut};
|
||||
use gpui::{Action, App, BorrowAppContext, Global, Task, WeakEntity};
|
||||
use workspace::Workspace;
|
||||
|
||||
/// Initializes the command palette hooks.
|
||||
pub fn init(cx: &mut App) {
|
||||
cx.set_global(GlobalCommandPaletteFilter::default());
|
||||
}
|
||||
|
||||
/// A filter for the command palette.
|
||||
#[derive(Default)]
|
||||
pub struct CommandPaletteFilter {
|
||||
hidden_namespaces: HashSet<&'static str>,
|
||||
hidden_action_types: HashSet<TypeId>,
|
||||
/// Actions that have explicitly been shown. These should be shown even if
|
||||
/// they are in a hidden namespace.
|
||||
shown_action_types: HashSet<TypeId>,
|
||||
}
|
||||
|
||||
#[derive(Deref, DerefMut, Default)]
|
||||
struct GlobalCommandPaletteFilter(CommandPaletteFilter);
|
||||
|
||||
impl Global for GlobalCommandPaletteFilter {}
|
||||
|
||||
impl CommandPaletteFilter {
|
||||
/// Returns the global [`CommandPaletteFilter`], if one is set.
|
||||
pub fn try_global(cx: &App) -> Option<&CommandPaletteFilter> {
|
||||
cx.try_global::<GlobalCommandPaletteFilter>()
|
||||
.map(|filter| &filter.0)
|
||||
}
|
||||
|
||||
/// Returns a mutable reference to the global [`CommandPaletteFilter`].
|
||||
pub fn global_mut(cx: &mut App) -> &mut Self {
|
||||
cx.global_mut::<GlobalCommandPaletteFilter>()
|
||||
}
|
||||
|
||||
/// Updates the global [`CommandPaletteFilter`] using the given closure.
|
||||
pub fn update_global<F>(cx: &mut App, update: F)
|
||||
where
|
||||
F: FnOnce(&mut Self, &mut App),
|
||||
{
|
||||
if cx.has_global::<GlobalCommandPaletteFilter>() {
|
||||
cx.update_global(|this: &mut GlobalCommandPaletteFilter, cx| update(&mut this.0, cx))
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns whether the given [`Action`] is hidden by the filter.
|
||||
pub fn is_hidden(&self, action: &dyn Action) -> bool {
|
||||
let name = action.name();
|
||||
let namespace = name.split("::").next().unwrap_or("malformed action name");
|
||||
|
||||
// If this action has specifically been shown then it should be visible.
|
||||
if self.shown_action_types.contains(&action.type_id()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
self.hidden_namespaces.contains(namespace)
|
||||
|| self.hidden_action_types.contains(&action.type_id())
|
||||
}
|
||||
|
||||
/// Hides all actions in the given namespace.
|
||||
pub fn hide_namespace(&mut self, namespace: &'static str) {
|
||||
self.hidden_namespaces.insert(namespace);
|
||||
}
|
||||
|
||||
/// Shows all actions in the given namespace.
|
||||
pub fn show_namespace(&mut self, namespace: &'static str) {
|
||||
self.hidden_namespaces.remove(namespace);
|
||||
}
|
||||
|
||||
/// Hides all actions with the given types.
|
||||
pub fn hide_action_types<'a>(&mut self, action_types: impl IntoIterator<Item = &'a TypeId>) {
|
||||
for action_type in action_types {
|
||||
self.hidden_action_types.insert(*action_type);
|
||||
self.shown_action_types.remove(action_type);
|
||||
}
|
||||
}
|
||||
|
||||
/// Shows all actions with the given types.
|
||||
pub fn show_action_types<'a>(&mut self, action_types: impl IntoIterator<Item = &'a TypeId>) {
|
||||
for action_type in action_types {
|
||||
self.shown_action_types.insert(*action_type);
|
||||
self.hidden_action_types.remove(action_type);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The result of intercepting a command palette command.
|
||||
#[derive(Debug)]
|
||||
pub struct CommandInterceptItem {
|
||||
/// The action produced as a result of the interception.
|
||||
pub action: Box<dyn Action>,
|
||||
/// The display string to show in the command palette for this result.
|
||||
pub string: String,
|
||||
/// The character positions in the string that match the query.
|
||||
/// Used for highlighting matched characters in the command palette UI.
|
||||
pub positions: Vec<usize>,
|
||||
}
|
||||
|
||||
/// The result of intercepting a command palette command.
|
||||
#[derive(Default, Debug)]
|
||||
pub struct CommandInterceptResult {
|
||||
/// The items
|
||||
pub results: Vec<CommandInterceptItem>,
|
||||
/// Whether or not to continue to show the normal matches
|
||||
pub exclusive: bool,
|
||||
}
|
||||
|
||||
/// An interceptor for the command palette.
|
||||
#[derive(Clone)]
|
||||
pub struct GlobalCommandPaletteInterceptor(
|
||||
Rc<dyn Fn(&str, WeakEntity<Workspace>, &mut App) -> Task<CommandInterceptResult>>,
|
||||
);
|
||||
|
||||
impl Global for GlobalCommandPaletteInterceptor {}
|
||||
|
||||
impl GlobalCommandPaletteInterceptor {
|
||||
/// Sets the global interceptor.
|
||||
///
|
||||
/// This will override the previous interceptor, if it exists.
|
||||
pub fn set(
|
||||
cx: &mut App,
|
||||
interceptor: impl Fn(&str, WeakEntity<Workspace>, &mut App) -> Task<CommandInterceptResult>
|
||||
+ 'static,
|
||||
) {
|
||||
cx.set_global(Self(Rc::new(interceptor)));
|
||||
}
|
||||
|
||||
/// Clears the global interceptor.
|
||||
pub fn clear(cx: &mut App) {
|
||||
if cx.has_global::<Self>() {
|
||||
cx.remove_global::<Self>();
|
||||
}
|
||||
}
|
||||
|
||||
/// Intercepts the given query from the command palette.
|
||||
pub fn intercept(
|
||||
query: &str,
|
||||
workspace: WeakEntity<Workspace>,
|
||||
cx: &mut App,
|
||||
) -> Option<Task<CommandInterceptResult>> {
|
||||
let interceptor = cx.try_global::<Self>()?;
|
||||
let handler = interceptor.0.clone();
|
||||
Some(handler(query, workspace, cx))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user