gpui: Add support for observing window appearance (#7294)

This PR adds support to GPUI for observing when the appearance of a
window changes.

Based on the initial work done in
https://github.com/zed-industries/zed/pull/6881.

Release Notes:

- N/A
This commit is contained in:
Marshall Bowers 2024-02-02 13:13:35 -05:00 committed by GitHub
parent 1f6bd6760f
commit 115f0672fb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 45 additions and 6 deletions

View file

@ -171,7 +171,7 @@ impl PlatformWindow for TestWindow {
} }
fn appearance(&self) -> WindowAppearance { fn appearance(&self) -> WindowAppearance {
unimplemented!() WindowAppearance::Light
} }
fn display(&self) -> std::rc::Rc<dyn crate::PlatformDisplay> { fn display(&self) -> std::rc::Rc<dyn crate::PlatformDisplay> {
@ -276,9 +276,7 @@ impl PlatformWindow for TestWindow {
unimplemented!() unimplemented!()
} }
fn on_appearance_changed(&self, _callback: Box<dyn FnMut()>) { fn on_appearance_changed(&self, _callback: Box<dyn FnMut()>) {}
unimplemented!()
}
fn is_topmost_for_position(&self, _position: crate::Point<Pixels>) -> bool { fn is_topmost_for_position(&self, _position: crate::Point<Pixels>) -> bool {
unimplemented!() unimplemented!()

View file

@ -6,8 +6,8 @@ use crate::{
KeymatchResult, Keystroke, KeystrokeEvent, Model, ModelContext, Modifiers, MouseButton, KeymatchResult, Keystroke, KeystrokeEvent, Model, ModelContext, Modifiers, MouseButton,
MouseMoveEvent, MouseUpEvent, Pixels, PlatformAtlas, PlatformDisplay, PlatformInput, MouseMoveEvent, MouseUpEvent, Pixels, PlatformAtlas, PlatformDisplay, PlatformInput,
PlatformWindow, Point, PromptLevel, Render, ScaledPixels, SharedString, Size, SubscriberSet, PlatformWindow, Point, PromptLevel, Render, ScaledPixels, SharedString, Size, SubscriberSet,
Subscription, TaffyLayoutEngine, Task, View, VisualContext, WeakView, WindowBounds, Subscription, TaffyLayoutEngine, Task, View, VisualContext, WeakView, WindowAppearance,
WindowOptions, WindowTextSystem, WindowBounds, WindowOptions, WindowTextSystem,
}; };
use anyhow::{anyhow, Context as _, Result}; use anyhow::{anyhow, Context as _, Result};
use collections::FxHashSet; use collections::FxHashSet;
@ -269,6 +269,8 @@ pub struct Window {
scale_factor: f32, scale_factor: f32,
bounds: WindowBounds, bounds: WindowBounds,
bounds_observers: SubscriberSet<(), AnyObserver>, bounds_observers: SubscriberSet<(), AnyObserver>,
appearance: WindowAppearance,
appearance_observers: SubscriberSet<(), AnyObserver>,
active: bool, active: bool,
pub(crate) dirty: bool, pub(crate) dirty: bool,
pub(crate) refreshing: bool, pub(crate) refreshing: bool,
@ -338,6 +340,7 @@ impl Window {
let content_size = platform_window.content_size(); let content_size = platform_window.content_size();
let scale_factor = platform_window.scale_factor(); let scale_factor = platform_window.scale_factor();
let bounds = platform_window.bounds(); let bounds = platform_window.bounds();
let appearance = platform_window.appearance();
let text_system = Arc::new(WindowTextSystem::new(cx.text_system().clone())); let text_system = Arc::new(WindowTextSystem::new(cx.text_system().clone()));
platform_window.on_request_frame(Box::new({ platform_window.on_request_frame(Box::new({
@ -364,6 +367,14 @@ impl Window {
.log_err(); .log_err();
} }
})); }));
platform_window.on_appearance_changed(Box::new({
let mut cx = cx.to_async();
move || {
handle
.update(&mut cx, |_, cx| cx.appearance_changed())
.log_err();
}
}));
platform_window.on_active_status_change(Box::new({ platform_window.on_active_status_change(Box::new({
let mut cx = cx.to_async(); let mut cx = cx.to_async();
move |active| { move |active| {
@ -413,6 +424,8 @@ impl Window {
scale_factor, scale_factor,
bounds, bounds,
bounds_observers: SubscriberSet::new(), bounds_observers: SubscriberSet::new(),
appearance,
appearance_observers: SubscriberSet::new(),
active: false, active: false,
dirty: false, dirty: false,
refreshing: false, refreshing: false,
@ -742,6 +755,20 @@ impl<'a> WindowContext<'a> {
self.window.bounds self.window.bounds
} }
fn appearance_changed(&mut self) {
self.window.appearance = self.window.platform_window.appearance();
self.window
.appearance_observers
.clone()
.retain(&(), |callback| callback(self));
}
/// Returns the appearance of the current window.
pub fn appearance(&self) -> WindowAppearance {
self.window.appearance
}
/// Returns the size of the drawable area within the window. /// Returns the size of the drawable area within the window.
pub fn viewport_size(&self) -> Size<Pixels> { pub fn viewport_size(&self) -> Size<Pixels> {
self.window.viewport_size self.window.viewport_size
@ -2066,6 +2093,20 @@ impl<'a, V: 'static> ViewContext<'a, V> {
subscription subscription
} }
/// Registers a callback to be invoked when the window appearance changes.
pub fn observe_window_appearance(
&mut self,
mut callback: impl FnMut(&mut V, &mut ViewContext<V>) + 'static,
) -> Subscription {
let view = self.view.downgrade();
let (subscription, activate) = self.window.appearance_observers.insert(
(),
Box::new(move |cx| view.update(cx, |view, cx| callback(view, cx)).is_ok()),
);
activate();
subscription
}
/// Register a listener to be called when the given focus handle receives focus. /// Register a listener to be called when the given focus handle receives focus.
/// Returns a subscription and persists until the subscription is dropped. /// Returns a subscription and persists until the subscription is dropped.
pub fn on_focus( pub fn on_focus(