Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat: keep the tray picker dynamic and update it in place

Ilya962713b91d88
diff --git a/apps/desktop-gpui/src/tray.rs b/apps/desktop-gpui/src/tray.rs
index 7b6f051a5..ea5cbb782 100644
--- a/apps/desktop-gpui/src/tray.rs
+++ b/apps/desktop-gpui/src/tray.rs
@@ -181,6 +181,9 @@ const ROW_ICON_SOURCES: &[(&str, &[u8])] = &[
/// `Entry::Item`'s `icon` size, and the raster size of every row glyph.
const ROW_ICON_SIZE: u32 = 32;
+/// The mode picker draws its icons at `SYMBOL_SIZE` points; rasterise at 4x so
+/// the downsample stays sharp on Retina displays.
+const PICKER_ICON_SIZE: u32 = 128;
/// An SVG rendered to a `size`×`size` PNG, scaled to fit and centred -- the
/// conversion `cap_mode_icon_png` makes at 90 for the palette, shared with the
@@ -211,6 +214,16 @@ fn menu_icon(name: &str) -> Option<Vec<u8>> {
ICONS.get(name).cloned()
}
+/// The Cap icon for a mode, rasterised for the picker's image views.
+fn mode_picker_icon(mode: Mode) -> Option<Vec<u8>> {
+ let source = match mode {
+ Mode::Instant => include_bytes!("../assets/icons/instant.svg") as &[u8],
+ Mode::Studio => include_bytes!("../assets/icons/film-cut.svg") as &[u8],
+ Mode::Screenshot => include_bytes!("../assets/icons/screenshot.svg") as &[u8],
+ };
+ svg_icon_png(source, PICKER_ICON_SIZE)
+}
+
/// The SF Symbol the macOS menu draws for an action row, keyed by its action;
/// Windows renders [`Entry::Item`]'s raster instead. Rows outside this list
/// (the chrome rows, the mode rows of the pre-14 menu) stay bare on both.
@@ -563,7 +576,10 @@ pub fn build_menu(mode: Mode, previous: &[PreviousItem], version: &str) -> Vec<E
for target in [TargetType::Display, TargetType::Window, TargetType::Area] {
entries.push(capture_entry(target, mode));
}
- if mode != Mode::Screenshot {
+ // macOS keeps the row in the menu and removes/inserts it in place; the
+ // other platforms rebuild on a mode change and only need it in recording
+ // modes. Keeping it on macOS also lets a dynamic mode switch add it back.
+ if cfg!(target_os = "macos") || mode != Mode::Screenshot {
entries.push(Entry::icon_item(
tr::tray::take_screenshot(),
TrayItem::TakeScreenshot,
@@ -1022,6 +1038,8 @@ mod mac {
static ROWS: RefCell<Vec<ActionRow>> = const { RefCell::new(Vec::new()) };
static TAKE_SCREENSHOT: RefCell<Option<Retained<NSMenuItem>>> =
const { RefCell::new(None) };
+ static TAKE_SCREENSHOT_ANCHOR: RefCell<Option<Retained<NSMenuItem>>> =
+ const { RefCell::new(None) };
static STATUS_ITEM: RefCell<Option<Retained<NSStatusItem>>> = const { RefCell::new(None) };
}
@@ -1328,6 +1346,8 @@ mod mac {
menu.removeAllItems();
ROWS.with(|rows| rows.borrow_mut().clear());
TAKE_SCREENSHOT.with(|item| *item.borrow_mut() = None);
+ TAKE_SCREENSHOT_ANCHOR.with(|item| *item.borrow_mut() = None);
+ let mut last_capture = None;
for entry in entries {
if let Entry::ModePicker { mode } = entry {
@@ -1369,6 +1389,7 @@ mod mac {
screenshot_accelerator,
});
});
+ last_capture = Some(menu_item.clone());
menu.addItem(&menu_item);
continue;
}
@@ -1382,6 +1403,9 @@ mod mac {
}
) {
TAKE_SCREENSHOT.with(|item| *item.borrow_mut() = Some(menu_item.clone()));
+ TAKE_SCREENSHOT_ANCHOR.with(|anchor| {
+ *anchor.borrow_mut() = last_capture.clone();
+ });
}
menu.addItem(&menu_item);
}
@@ -1454,10 +1478,34 @@ mod mac {
set_row_image(&row.item, item);
}
});
- TAKE_SCREENSHOT.with(|item| {
- if let Some(item) = item.borrow().as_ref() {
- item.setHidden(mode == Mode::Screenshot);
- }
+ TAKE_SCREENSHOT.with(|slot| {
+ let slot = slot.borrow();
+ let Some(item) = slot.as_ref() else {
+ return;
+ };
+ TAKE_SCREENSHOT_ANCHOR.with(|anchor| {
+ let anchor = anchor.borrow();
+ let menu = unsafe { item.menu() }
+ .or_else(|| anchor.as_ref().and_then(|item| unsafe { item.menu() }));
+ let Some(menu) = menu else {
+ return;
+ };
+ let index = menu.indexOfItem(item);
+ if mode == Mode::Screenshot {
+ if index >= 0 {
+ menu.removeItemAtIndex(index);
+ }
+ return;
+ }
+ if index < 0 {
+ let at = anchor
+ .as_ref()
+ .map(|item| menu.indexOfItem(item))
+ .filter(|index| *index >= 0)
+ .map_or(menu.numberOfItems(), |index| index + 1);
+ menu.insertItem_atIndex(item, at);
+ }
+ });
});
}
@@ -1494,6 +1542,14 @@ mod mac {
}
let block = block2::RcBlock::new(|_timer: NonNull<NSTimer>| {
let held = current_option();
+ let mode = Mode::from_store();
+ if CURRENT_MODE.get() != Some(mode) {
+ apply(mode, held);
+ set_status_mode(mode);
+ select_mode(mode);
+ OPTION_HELD.set(held);
+ return;
+ }
if OPTION_HELD.replace(held) != held
&& let Some(mode) = CURRENT_MODE.get()
{
@@ -2215,7 +2271,9 @@ mod tests {
"Screenshot Window",
]
);
- assert!(!titles.iter().any(|title| title == "Take a Screenshot"));
+ if !cfg!(target_os = "macos") {
+ assert!(!titles.iter().any(|title| title == "Take a Screenshot"));
+ }
}
#[test]
@@ -2397,7 +2455,13 @@ mod tests {
};
assert!(has(Mode::Studio));
assert!(has(Mode::Instant));
- assert!(!has(Mode::Screenshot));
+ if cfg!(target_os = "macos") {
+ // Kept in the model and removed from the live menu in screenshot
+ // mode, so it can be inserted again when the mode changes.
+ assert!(has(Mode::Screenshot));
+ } else {
+ assert!(!has(Mode::Screenshot));
+ }
}
#[test]
diff --git a/apps/desktop-gpui/src/tray/mac_mode_picker.rs b/apps/desktop-gpui/src/tray/mac_mode_picker.rs
index bda62f1b8..2a92b51f9 100644
--- a/apps/desktop-gpui/src/tray/mac_mode_picker.rs
+++ b/apps/desktop-gpui/src/tray/mac_mode_picker.rs
@@ -24,20 +24,19 @@ use objc2_foundation::{
use objc2_quartz_core::{CALayer, CATransaction, kCACornerCurveContinuous};
use super::mac::{post, set_status_mode};
-use super::{TrayItem, menu_icon, mode_name};
+use super::{TrayItem, mode_name, mode_picker_icon};
use crate::main_window::Mode;
-const PICKER_MODES: [(Mode, TrayItem, &str); 3] = [
- (Mode::Instant, TrayItem::ModeInstant, "mode-instant"),
- (Mode::Studio, TrayItem::ModeStudio, "mode-studio"),
- (
- Mode::Screenshot,
- TrayItem::ModeScreenshot,
- "mode-screenshot",
- ),
+const PICKER_MODES: [(Mode, TrayItem); 3] = [
+ (Mode::Instant, TrayItem::ModeInstant),
+ (Mode::Studio, TrayItem::ModeStudio),
+ (Mode::Screenshot, TrayItem::ModeScreenshot),
];
const FLASH_INTERVAL: f64 = 0.06;
+/// Temporarily disabled while the picker keeps the menu open and updates the
+/// capture rows in place.
+const FLASH_ON_PICK: bool = false;
const PICKER_TITLE: &str = "Recording Mode";
const PICKER_W: f64 = 300.0;
@@ -46,8 +45,8 @@ const PILL_W: f64 = 38.0;
const PILL_H: f64 = 29.0;
const PILL_GAP: f64 = 4.0;
const CAPSULE_PAD: f64 = 3.0;
-const RIGHT_INSET: f64 = 10.0;
-const TITLE_INSET: f64 = 17.0;
+const RIGHT_INSET: f64 = 5.0;
+const TITLE_INSET: f64 = 8.5;
const LINE_GAP: f64 = 2.0;
const SYMBOL_SIZE: f64 = 17.0;
const PILL_RADIUS: f64 = 14.0;
@@ -176,9 +175,9 @@ impl ModePickerView {
.push(make_layer(PILL_RADIUS));
}
- for (_, _, icon) in PICKER_MODES.iter() {
+ for (mode, _) in PICKER_MODES.iter() {
let view = NSImageView::new(mtm);
- if let Some(bytes) = menu_icon(icon)
+ if let Some(bytes) = mode_picker_icon(*mode)
&& let Some(image) =
NSImage::initWithData(NSImage::alloc(), &NSData::with_bytes(&bytes))
{
@@ -286,7 +285,7 @@ impl ModePickerView {
let is_sel = i == selected;
let is_hover = focused == Some(i) && !is_sel;
let fill = if is_sel {
- if flash { white.clone() } else { accent.clone() }
+ if flash { accent.clone() } else { white.clone() }
} else if is_hover {
label.colorWithAlphaComponent(0.14)
} else {
@@ -300,7 +299,7 @@ impl ModePickerView {
if let Some(icon) = icons.get(i) {
icon.setFrame(r);
let tint = if is_sel {
- if flash { accent.clone() } else { white.clone() }
+ if flash { white.clone() } else { accent.clone() }
} else {
label.clone()
};
@@ -369,12 +368,16 @@ impl ModePickerView {
let mode = PICKER_MODES[i].0;
self.ivars().selected.set(i);
self.ivars().focused.set(Some(i));
- self.ivars().flash.set(true);
self.refresh();
post(self.ivars().tag_base + i as isize);
super::mac::apply(mode, super::mac::current_option());
set_status_mode(mode);
+ if !FLASH_ON_PICK {
+ return;
+ }
+ self.ivars().flash.set(true);
+ self.refresh();
let block = RcBlock::new(|_timer: NonNull<NSTimer>| {
flash_step();
});
@@ -459,7 +462,7 @@ pub(super) fn stop_flash() {
pub(super) fn select_mode(mode: Mode) {
let Some(index) = PICKER_MODES
.iter()
- .position(|(candidate, _, _)| *candidate == mode)
+ .position(|(candidate, _)| *candidate == mode)
else {
return;
};
@@ -483,11 +486,11 @@ pub(super) fn append_mode_picker(
mtm: MainThreadMarker,
) -> Retained<NSMenuItem> {
let tag_base = actions.len() as isize;
- actions.extend(PICKER_MODES.iter().map(|(_, item, _)| item.clone()));
+ actions.extend(PICKER_MODES.iter().map(|(_, item)| item.clone()));
let index = PICKER_MODES
.iter()
- .position(|(m, _, _)| *m == selected)
+ .position(|(m, _)| *m == selected)
.unwrap_or(0);
let view = ModePickerView::new(mtm, index, tag_base);