Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Fix after merge + fmt

Ilyaf56acbf17f0e
diff --git a/apps/desktop-gpui/src/menus.rs b/apps/desktop-gpui/src/menus.rs
index 0974118e6..b7188c659 100644
--- a/apps/desktop-gpui/src/menus.rs
+++ b/apps/desktop-gpui/src/menus.rs
@@ -34,6 +34,7 @@ use crate::{
settings_window::Page,
store,
ui::text_input,
+ updates,
};
/// `productName` in `tauri.conf.json`, which is what macOS renames the first
@@ -60,8 +61,8 @@ const DOWNLOAD_TITLE: &str = "Download update…";
const RESTART_TITLE: &str = "Restart to update…";
impl UpdateRow {
- fn of(status: &crate::updates::UpdateStatus) -> Self {
- use crate::updates::UpdateStatus;
+ fn of(status: &updates::UpdateStatus) -> Self {
+ use updates::UpdateStatus;
let title = match status {
UpdateStatus::Idle => CHECK_TITLE,
@@ -78,8 +79,8 @@ impl UpdateRow {
}
#[cfg(target_os = "macos")]
-fn update_menu_badge(status: &crate::updates::UpdateStatus) -> Option<String> {
- use crate::updates::UpdateStatus;
+fn update_menu_badge(status: &updates::UpdateStatus) -> Option<String> {
+ use updates::UpdateStatus;
match status {
UpdateStatus::Downloading {
@@ -98,7 +99,7 @@ fn update_menu_badge(status: &crate::updates::UpdateStatus) -> Option<String> {
}
#[cfg(not(target_os = "macos"))]
-fn update_menu_badge(_: &crate::updates::UpdateStatus) -> Option<String> {
+fn update_menu_badge(_: &updates::UpdateStatus) -> Option<String> {
None
}
@@ -123,8 +124,8 @@ actions!(
HideOthers,
/// The Cap menu's explicit Quit item -- ⌘Q.
Quit,
- /// Open General settings from the application menu.
- Preferences,
+ /// Settings… -- ⌘, on macOS, Ctrl+, on Windows and Linux.
+ OpenSettings,
/// Choose a Cap recording to open.
OpenFile,
/// Import a video or image into the library.
@@ -148,8 +149,6 @@ actions!(
Zoom,
/// `PredefinedMenuItem::fullscreen` -- ⌃⌘F.
ToggleFullscreen,
- /// Settings… -- ⌘, on macOS, Ctrl+, on Windows and Linux.
- OpenSettings,
]
);
@@ -164,14 +163,11 @@ pub fn init(cx: &mut App) {
cx.bind_keys(bindings());
cx.on_action(|_: &CheckForUpdate, cx: &mut App| {
- crate::updates::check_manually(cx);
+ updates::check_manually(cx);
});
cx.on_action(|_: &HideSelf, cx: &mut App| cx.hide());
cx.on_action(|_: &HideOthers, cx: &mut App| cx.hide_other_apps());
cx.on_action(|_: &Quit, cx: &mut App| quit(cx));
- cx.on_action(|_: &Preferences, cx: &mut App| {
- cx.defer(|cx| app_windows::open_settings(crate::settings_window::Page::General, cx));
- });
cx.on_action(|_: &OpenFile, cx: &mut App| {
let (files, directories) = if cfg!(target_os = "macos") {
(true, false)
@@ -243,7 +239,7 @@ pub fn init(cx: &mut App) {
with_key_native(cx, platform::toggle_fullscreen_native)
});
- cx.set_global(MenuRegistration(UpdateRow::of(&crate::updates::status(cx))));
+ cx.set_global(MenuRegistration(UpdateRow::of(&updates::status(cx))));
install_menus(cx);
}
@@ -252,7 +248,7 @@ pub(crate) fn refresh_update_menu(cx: &mut App) {
return;
}
- let row = UpdateRow::of(&crate::updates::status(cx));
+ let row = UpdateRow::of(&updates::status(cx));
if cx.global::<MenuRegistration>().0 == row {
return;
}
@@ -265,17 +261,35 @@ pub(crate) fn refresh_update_menu(cx: &mut App) {
}
fn install_menus(cx: &mut App) {
- let update_status = crate::updates::status(cx);
+ let update_status = updates::status(cx);
cx.set_menus(build_menus(&update_status));
#[cfg(target_os = "macos")]
platform::patch_standard_application_menu();
}
+pub(crate) fn bindings() -> Vec<KeyBinding> {
+ vec![
+ KeyBinding::new("cmd-q", Quit, None),
+ KeyBinding::new("cmd-h", HideSelf, None),
+ KeyBinding::new("alt-cmd-h", HideOthers, None),
+ KeyBinding::new("cmd-w", CloseWindow, None),
+ KeyBinding::new("cmd-m", Minimize, None),
+ KeyBinding::new("ctrl-cmd-f", ToggleFullscreen, None),
+ KeyBinding::new("secondary-,", OpenSettings, None),
+ ]
+}
+
+fn settings_shortcut_allowed(idle: bool, onboarding_open: bool, hotkeys_paused: bool) -> bool {
+ idle && !onboarding_open && !hotkeys_paused
+}
+
/// The menu bar model.
///
/// One-to-one with `build_macos_app_menu`, including the empty Help submenu
/// (`Submenu::with_id_and_items(.., HELP_SUBMENU_ID, "Help", true, &[])`).
-pub fn build_menus() -> Vec<Menu> {
+pub fn build_menus(update_status: &updates::UpdateStatus) -> Vec<Menu> {
+ let update_row = UpdateRow::of(update_status);
+
vec![
Menu {
name: APP_NAME.into(),
@@ -284,8 +298,6 @@ pub fn build_menus() -> Vec<Menu> {
MenuItem::action(format!("About {APP_NAME}"), About),
MenuItem::action(update_row.title, CheckForUpdate).disabled(!update_row.enabled),
MenuItem::separator(),
- // MenuItem::action("Preferences…", Preferences),
- MenuItem::separator(),
MenuItem::action("Settings…", OpenSettings),
MenuItem::separator(),
MenuItem::os_submenu("Services", SystemMenuType::Services),
@@ -1143,7 +1155,7 @@ mod tests {
#[test]
fn menu_bar_includes_native_and_cap_destinations() {
- let menus: Vec<_> = build_menus(&crate::updates::UpdateStatus::Idle)
+ let menus: Vec<_> = build_menus(&updates::UpdateStatus::Idle)
.iter()
.map(describe)
.collect();
@@ -1218,7 +1230,7 @@ mod tests {
/// cannot fire another one.
#[test]
fn the_update_row_greys_out_while_checking() {
- fn row(status: &crate::updates::UpdateStatus) -> (String, bool) {
+ fn row(status: &updates::UpdateStatus) -> (String, bool) {
let menus = build_menus(status);
match &menus[0].items[1] {
MenuItem::Action { name, disabled, .. } => (name.to_string(), *disabled),
@@ -1227,11 +1239,11 @@ mod tests {
}
assert_eq!(
- row(&crate::updates::UpdateStatus::Idle),
+ row(&updates::UpdateStatus::Idle),
("Check for Update".to_string(), false)
);
assert_eq!(
- row(&crate::updates::UpdateStatus::Checking),
+ row(&updates::UpdateStatus::Checking),
("Checking for Update…".to_string(), true)
);
}
@@ -1241,8 +1253,8 @@ mod tests {
/// menu bar on every progress event.
#[test]
fn the_update_row_keeps_the_version_out_of_the_title() {
- use crate::updates::UpdateStatus;
use semver::Version;
+ use updates::UpdateStatus;
let version = Version::parse("0.4.0").unwrap();
let downloading = |fraction| UpdateStatus::Downloading {
@@ -1276,8 +1288,8 @@ mod tests {
#[cfg(target_os = "macos")]
#[test]
fn the_update_badge_shows_the_version_and_live_progress() {
- use crate::updates::UpdateStatus;
use semver::Version;
+ use updates::UpdateStatus;
let version = Version::parse("0.4.0").unwrap();
let badge = |status| UpdateRow::of(&status).badge;
diff --git a/apps/desktop-gpui/src/tray.rs b/apps/desktop-gpui/src/tray.rs
index 0ad5bd029..11d09a445 100644
--- a/apps/desktop-gpui/src/tray.rs
+++ b/apps/desktop-gpui/src/tray.rs
@@ -831,13 +831,10 @@ mod mac {
use objc2::runtime::{NSObjectProtocol, ProtocolObject, Sel};
use objc2::{AnyThread, MainThreadOnly, define_class, msg_send, sel};
use objc2_app_kit::{
- NSEventModifierFlags, NSImage, NSMenu, NSMenuDelegate,
- NSMenuItem, NSStatusBar, NSStatusBarButton, NSStatusItem,
+ NSEventModifierFlags, NSImage, NSMenu, NSMenuDelegate, NSMenuItem, NSStatusBar,
+ NSStatusBarButton, NSStatusItem,
};
- use objc2_foundation::{
- MainThreadMarker, NSData, NSString,
- };
-
+ use objc2_foundation::{MainThreadMarker, NSData, NSString};
use super::mac_mode_picker::{append_mode_picker, install_key_hook};
use super::{
@@ -990,18 +987,10 @@ mod mac {
fn create_status_item() -> Option<Tray> {
unsafe {
- let status_bar: *mut AnyObject = msg_send![class!(NSStatusBar), systemStatusBar];
- if status_bar.is_null() {
- return None;
- }
- let raw: *mut AnyObject =
- msg_send![status_bar, statusItemWithLength: NS_VARIABLE_STATUS_ITEM_LENGTH];
- // `statusItemWithLength:` hands back an autoreleased object owned
- // by the status bar; retain it for as long as we hold it, and never
- // release it -- removing a status item is what makes it disappear
- // from the menu bar, and this one lives for the process.
- let status_item = Id::retain(raw)?;
- let target: Id<TrayTarget> = msg_send_id![TrayTarget::alloc(), init];
+ let mtm = MainThreadMarker::new()?;
+ let status_item =
+ NSStatusBar::systemStatusBar().statusItemWithLength(NS_VARIABLE_STATUS_ITEM_LENGTH);
+ let target: Retained<TrayTarget> = unsafe { msg_send![TrayTarget::alloc(mtm), init] };
let mode = Mode::from_store();
let tray = Tray {
@@ -1015,19 +1004,20 @@ mod mac {
recording: false,
};
set_button_image(&tray, mode_icon(mode));
- let button: *mut AnyObject = msg_send![&*tray.status_item, button];
- let visible: bool = msg_send![&*tray.status_item, isVisible];
- let frame: objc2_foundation::NSRect = if button.is_null() {
- objc2_foundation::NSRect::new(
- objc2_foundation::NSPoint::new(0., 0.),
- objc2_foundation::NSSize::new(0., 0.),
- )
- } else {
- msg_send![button, frame]
- };
+ let button = tray.status_item.button(mtm);
+ let frame = button.as_ref().map_or_else(
+ || {
+ objc2_foundation::NSRect::new(
+ objc2_foundation::NSPoint::new(0.0, 0.0),
+ objc2_foundation::NSSize::new(0.0, 0.0),
+ )
+ },
+ |button| button.frame(),
+ );
+
tracing::info!(
- has_button = !button.is_null(),
- visible,
+ has_button = button.is_some(),
+ visible = tray.status_item.isVisible(),
width = frame.size.width,
height = frame.size.height,
?mode,
diff --git a/apps/desktop-gpui/src/tray/mac_mode_picker.rs b/apps/desktop-gpui/src/tray/mac_mode_picker.rs
index 181757157..95619728f 100644
--- a/apps/desktop-gpui/src/tray/mac_mode_picker.rs
+++ b/apps/desktop-gpui/src/tray/mac_mode_picker.rs
@@ -12,10 +12,10 @@ use objc2::rc::Retained;
use objc2::runtime::{AnyClass, AnyObject, Bool, Sel};
use objc2::{AnyThread, DefinedClass, MainThreadOnly, define_class, msg_send, sel};
use objc2_app_kit::{
- NSAppearance, NSColor, NSEvent, NSEventType, NSFont, NSFontAttributeName, NSFontManager,
- NSFontTraitMask, NSFontWeightMedium, NSFontWeightSemibold, NSForegroundColorAttributeName,
- NSImageScaling, NSImageSymbolConfiguration, NSImageView, NSMenu, NSMenuItem, NSStringDrawing,
- NSTrackingArea, NSTrackingAreaOptions, NSView, NSAppearanceCustomization,
+ NSAppearance, NSAppearanceCustomization, NSColor, NSEvent, NSEventType, NSFont,
+ NSFontAttributeName, NSFontManager, NSFontTraitMask, NSFontWeightMedium, NSFontWeightSemibold,
+ NSForegroundColorAttributeName, NSImageScaling, NSImageSymbolConfiguration, NSImageView,
+ NSMenu, NSMenuItem, NSStringDrawing, NSTrackingArea, NSTrackingAreaOptions, NSView,
};
use objc2_foundation::{MainThreadMarker, NSDictionary, NSPoint, NSRect, NSSize, NSString};
use objc2_quartz_core::{CALayer, CATransaction, kCACornerCurveContinuous};
@@ -157,7 +157,10 @@ impl ModePickerView {
};
*this.ivars().capsule.borrow_mut() = Some(make_layer(CAPSULE_RADIUS));
for _ in PICKER_MODES.iter() {
- this.ivars().pills.borrow_mut().push(make_layer(PILL_RADIUS));
+ this.ivars()
+ .pills
+ .borrow_mut()
+ .push(make_layer(PILL_RADIUS));
}
for (_, _, symbol) in PICKER_MODES.iter() {
@@ -201,7 +204,10 @@ impl ModePickerView {
let h = PILL_H + 2.0 * CAPSULE_PAD;
let bounds = self.bounds();
NSRect::new(
- NSPoint::new(bounds.size.width - RIGHT_INSET - w, (bounds.size.height - h) / 2.0),
+ NSPoint::new(
+ bounds.size.width - RIGHT_INSET - w,
+ (bounds.size.height - h) / 2.0,
+ ),
NSSize::new(w, h),
)
}
@@ -244,7 +250,9 @@ impl ModePickerView {
// CGColors don't follow appearance, so resolve the dynamic colours under this
// view's appearance. (Deprecated, but it has no block-free replacement.)
#[allow(deprecated)]
- unsafe { NSAppearance::setCurrentAppearance(Some(&self.effectiveAppearance())) };
+ unsafe {
+ NSAppearance::setCurrentAppearance(Some(&self.effectiveAppearance()))
+ };
let label = NSColor::labelColor();
CATransaction::begin();
@@ -293,7 +301,8 @@ impl ModePickerView {
let shown = focused.unwrap_or(selected);
let menu_size = NSFont::menuFontOfSize(0.0).pointSize();
- let title_font = NSFont::systemFontOfSize_weight(menu_size, unsafe { NSFontWeightSemibold });
+ let title_font =
+ NSFont::systemFontOfSize_weight(menu_size, unsafe { NSFontWeightSemibold });
let mut sub_font = NSFont::systemFontOfSize(menu_size - 2.0);
if shown != selected {
sub_font = NSFontManager::sharedFontManager(
@@ -307,11 +316,15 @@ impl ModePickerView {
let title = NSString::from_str(PICKER_TITLE);
let subtitle = NSString::from_str(mode_name(PICKER_MODES[shown].0));
let title_attrs = NSDictionary::from_slices(
- &[unsafe { NSFontAttributeName }, unsafe { NSForegroundColorAttributeName }],
+ &[unsafe { NSFontAttributeName }, unsafe {
+ NSForegroundColorAttributeName
+ }],
&[&*title_font as &AnyObject, &*label as &AnyObject],
);
let sub_attrs = NSDictionary::from_slices(
- &[unsafe { NSFontAttributeName }, unsafe { NSForegroundColorAttributeName }],
+ &[unsafe { NSFontAttributeName }, unsafe {
+ NSForegroundColorAttributeName
+ }],
&[&*sub_font as &AnyObject, &*secondary as &AnyObject],
);
@@ -341,7 +354,11 @@ impl ModePickerView {
/// Moves keyboard focus one pill. Returns false at either end, so the key falls
/// through to the menu and moves to the neighbouring tray menu.
fn move_focus(&self, delta: isize) -> bool {
- let cur = self.ivars().focused.get().unwrap_or(self.ivars().selected.get()) as isize;
+ let cur = self
+ .ivars()
+ .focused
+ .get()
+ .unwrap_or(self.ivars().selected.get()) as isize;
let next = cur + delta;
if next < 0 || next >= PICKER_MODES.len() as isize {
return false;
@@ -352,7 +369,11 @@ impl ModePickerView {
}
fn activate_focused(&self) {
- let i = self.ivars().focused.get().unwrap_or(self.ivars().selected.get());
+ let i = self
+ .ivars()
+ .focused
+ .get()
+ .unwrap_or(self.ivars().selected.get());
self.choose(i);
}
}
@@ -368,7 +389,10 @@ pub(super) fn append_mode_picker(
let tag_base = actions.len() as isize;
actions.extend(PICKER_MODES.iter().map(|(_, item, _)| item.clone()));
- let index = PICKER_MODES.iter().position(|(m, _, _)| *m == selected).unwrap_or(0);
+ let index = PICKER_MODES
+ .iter()
+ .position(|(m, _, _)| *m == selected)
+ .unwrap_or(0);
let view = ModePickerView::new(mtm, index, tag_base);
let item = NSMenuItem::new(mtm);
@@ -421,7 +445,9 @@ pub(super) fn install_key_hook() {
let p = objc2::ffi::method_getTypeEncoding(
method as *const _ as *const objc2::ffi::objc_method,
);
- if p.is_null() { return; }
+ if p.is_null() {
+ return;
+ }
std::ffi::CStr::from_ptr(p)
};
if enc.to_bytes() != b"B24@0:8@16" {
@@ -440,9 +466,7 @@ pub(super) fn install_key_hook() {
unsafe { orig(this, sel, event) }
});
unsafe {
- let imp = objc2::ffi::imp_implementationWithBlock(
- &*block as *const _ as *mut AnyObject,
- );
+ let imp = objc2::ffi::imp_implementationWithBlock(&*block as *const _ as *mut AnyObject);
method.set_implementation(std::mem::transmute(imp));
}
std::mem::forget(block); // the replacement must outlive the process