Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: show microphone levels in capture overlays

Richie McIlroyf76d6112762a
diff --git a/apps/desktop-gpui/src/main_window.rs b/apps/desktop-gpui/src/main_window.rs
index 881eb1625..a152d16be 100644
--- a/apps/desktop-gpui/src/main_window.rs
+++ b/apps/desktop-gpui/src/main_window.rs
@@ -597,87 +597,6 @@ struct MicrophoneWarning {
error: Option<String>,
}
-struct MicrophoneLevel {
- active: bool,
- fill: f32,
- color: Hsla,
-}
-
-impl MicrophoneLevel {
- fn new(feeds: &Entity<Feeds>, window: &mut Window, cx: &mut Context<Self>) -> Self {
- cx.observe_in(feeds, window, |this, feeds, window, cx| {
- let visible = if cfg!(any(target_os = "macos", target_os = "windows")) {
- crate::platform::window_is_visible(window)
- } else {
- window.is_window_active()
- };
- if this.active && visible {
- let fill = Self::fill(feeds.read(cx));
- if this.fill != fill {
- this.fill = fill;
- cx.notify();
- }
- }
- })
- .detach();
- Self {
- active: false,
- fill: 0.,
- color: gpui::transparent_black(),
- }
- }
-
- fn fill(feeds: &Feeds) -> f32 {
- if feeds.microphone.is_some() && feeds.mic_level_db.is_finite() {
- (1. - feeds::picker_level(feeds.mic_level_db)) as f32
- } else {
- 0.
- }
- }
-
- fn configure(&mut self, active: bool, color: Hsla, cx: &mut Context<Self>) {
- let fill = if active {
- Self::fill(Feeds::global(cx).read(cx))
- } else {
- 0.
- };
- if self.active != active || self.color != color || self.fill != fill {
- self.active = active;
- self.color = color;
- self.fill = fill;
- cx.notify();
- }
- }
-}
-
-impl Render for MicrophoneLevel {
- fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
- let mut background = self.color;
- background.a *= 0.1;
- div()
- .size_full()
- .when(self.active && self.fill > 0., |this| {
- this.child(
- div()
- .relative()
- .h_full()
- .w(gpui::relative(self.fill))
- .rounded(px(7.))
- .bg(background)
- .child(
- div()
- .absolute()
- .bottom_0()
- .left_0()
- .w_full()
- .h(px(2.))
- .bg(self.color),
- ),
- )
- })
- }
-}
-
pub struct MainWindow {
theme: Theme,
expanded: bool,
@@ -690,7 +609,7 @@ pub struct MainWindow {
camera: Option<CameraOption>,
camera_id: Option<recording::DeviceOrModelID>,
microphone: Option<MicrophoneOption>,
- microphone_level: Entity<MicrophoneLevel>,
+ microphone_level: Entity<ui::MicrophoneLevel>,
pending_device_restore: crate::store::RecordingInputSettings,
device_restore_suspended: bool,
device_format_target: Option<DeviceFormatTarget>,
@@ -857,7 +776,7 @@ impl MainWindow {
// meter notifies at ~20Hz and would otherwise repaint the home view
// for a level bar only the microphone picker shows.
let feeds = Feeds::global(cx);
- let microphone_level = cx.new(|cx| MicrophoneLevel::new(&feeds, window, cx));
+ let microphone_level = cx.new(|cx| ui::MicrophoneLevel::new(&feeds, window, cx));
cx.observe(&feeds, |this: &mut Self, feeds, cx| {
let feeds = feeds.read(cx);
let camera_id = feeds.camera.as_ref().map(|camera| &camera.id);
diff --git a/apps/desktop-gpui/src/target_overlay.rs b/apps/desktop-gpui/src/target_overlay.rs
index 34494e55e..409b84f36 100644
--- a/apps/desktop-gpui/src/target_overlay.rs
+++ b/apps/desktop-gpui/src/target_overlay.rs
@@ -221,6 +221,7 @@ pub struct TargetSelect {
/// A window locked in by a click (or seeded from the main window's picker),
/// which the highlight sticks to until another window is clicked.
pub pinned_window: Option<HoveredWindow>,
+ microphone_fill: f32,
/// App icons by window id, fetched once per window.
icons: HashMap<String, Arc<gpui::Image>>,
_poll: Option<gpui::Task<()>>,
@@ -237,6 +238,7 @@ impl TargetSelect {
cursor_display: None,
hovered_window: None,
pinned_window: None,
+ microphone_fill: 0.,
icons: HashMap::new(),
_poll: None,
});
@@ -330,6 +332,8 @@ impl TargetSelect {
let Ok((icon_wanted, active)) = this.update(cx, |this: &mut Self, cx| {
let (display, window) = probe;
+ this.microphone_fill =
+ crate::ui::MicrophoneLevel::fill(crate::feeds::Feeds::global(cx).read(cx));
let changed = this.cursor_display != display || this.hovered_window != window;
this.cursor_display = display;
this.hovered_window = window;
@@ -988,6 +992,7 @@ impl OverlayWindow {
} else {
"overlay-microphone"
})
+ .relative()
.flex()
.items_center()
.gap(px(8.))
@@ -1000,7 +1005,19 @@ impl OverlayWindow {
.border_color(theme.gray_5)
.bg(theme.gray_3)
.text_color(theme.gray_12)
+ .overflow_hidden()
.when(disabled, |row| row.opacity(0.7))
+ .when(!camera && enabled && !disabled, |row| {
+ row.child(
+ crate::ui::MicrophoneLevel::snapshot(
+ self.select.read(cx).microphone_fill,
+ theme.blue_9.into(),
+ )
+ .absolute()
+ .top_0()
+ .left_0(),
+ )
+ })
.child(
svg()
.path(if camera {
@@ -1009,7 +1026,8 @@ impl OverlayWindow {
"icons/microphone.svg"
})
.size(px(16.))
- .flex_shrink_0(),
+ .flex_shrink_0()
+ .text_color(theme.gray_11),
)
.child(
div()
diff --git a/apps/desktop-gpui/src/ui/microphone_level.rs b/apps/desktop-gpui/src/ui/microphone_level.rs
new file mode 100644
index 000000000..aa8eacbc8
--- /dev/null
+++ b/apps/desktop-gpui/src/ui/microphone_level.rs
@@ -0,0 +1,89 @@
+use gpui::{
+ Context, Entity, Hsla, IntoElement, ParentElement as _, Render, Styled as _, Window, div,
+ prelude::FluentBuilder as _, px,
+};
+
+use crate::feeds::{self, Feeds};
+
+pub(crate) struct MicrophoneLevel {
+ active: bool,
+ fill: f32,
+ color: Hsla,
+}
+
+impl MicrophoneLevel {
+ pub(crate) fn new(feeds: &Entity<Feeds>, window: &mut Window, cx: &mut Context<Self>) -> Self {
+ cx.observe_in(feeds, window, |this, feeds, window, cx| {
+ let visible = if cfg!(any(target_os = "macos", target_os = "windows")) {
+ crate::platform::window_is_visible(window)
+ } else {
+ window.is_window_active()
+ };
+ if this.active && visible {
+ let fill = Self::fill(feeds.read(cx));
+ if this.fill != fill {
+ this.fill = fill;
+ cx.notify();
+ }
+ }
+ })
+ .detach();
+ Self {
+ active: false,
+ fill: 0.,
+ color: gpui::transparent_black(),
+ }
+ }
+
+ pub(crate) fn fill(feeds: &Feeds) -> f32 {
+ if feeds.microphone.is_some() && feeds.mic_level_db.is_finite() {
+ (1. - feeds::picker_level(feeds.mic_level_db)) as f32
+ } else {
+ 0.
+ }
+ }
+
+ pub(crate) fn configure(&mut self, active: bool, color: Hsla, cx: &mut Context<Self>) {
+ let fill = if active {
+ Self::fill(Feeds::global(cx).read(cx))
+ } else {
+ 0.
+ };
+ if self.active != active || self.color != color || self.fill != fill {
+ self.active = active;
+ self.color = color;
+ self.fill = fill;
+ cx.notify();
+ }
+ }
+
+ pub(crate) fn snapshot(fill: f32, color: Hsla) -> gpui::Div {
+ let mut background = color;
+ background.a *= 0.1;
+ div().size_full().when(fill > 0., |this| {
+ this.child(
+ div()
+ .relative()
+ .h_full()
+ .w(gpui::relative(fill))
+ .rounded(px(7.))
+ .bg(background)
+ .child(
+ div()
+ .absolute()
+ .bottom_0()
+ .left_0()
+ .w_full()
+ .h(px(2.))
+ .bg(color),
+ ),
+ )
+ })
+ }
+}
+
+impl Render for MicrophoneLevel {
+ fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
+ Self::snapshot(self.fill, self.color)
+ }
+}
diff --git a/apps/desktop-gpui/src/ui/mod.rs b/apps/desktop-gpui/src/ui/mod.rs
index 6afa1dd70..e69609d4b 100644
--- a/apps/desktop-gpui/src/ui/mod.rs
+++ b/apps/desktop-gpui/src/ui/mod.rs
@@ -47,6 +47,7 @@ mod editor_button;
mod field;
mod kbd;
mod menu;
+mod microphone_level;
mod number_field;
mod position_pad;
mod progress;
@@ -88,6 +89,7 @@ pub use field::{Field, Subfield};
pub use kbd::{KbdChip, KbdSize, kbd_symbol};
#[allow(unused_imports)]
pub use menu::{Menu, MenuItem, MenuKey, MenuState};
+pub(crate) use microphone_level::MicrophoneLevel;
#[allow(unused_imports)]
pub use number_field::{
NumberChange, NumberField, NumberFieldState, NumberLimits, format_number, parse_number,