Enable/disable scrollbar auto-hide based on OS setting

This commit is contained in:
Max Brunsfeld 2022-10-10 18:12:31 -07:00
parent b229bc69b9
commit e2700ff8c6
4 changed files with 35 additions and 9 deletions

View file

@ -239,6 +239,9 @@ pub enum Direction {
Next,
}
#[derive(Default)]
struct ScrollbarAutoHide(bool);
pub fn init(cx: &mut MutableAppContext) {
cx.add_action(Editor::new_file);
cx.add_action(|this: &mut Editor, action: &Scroll, cx| this.set_scroll_position(action.0, cx));
@ -327,6 +330,10 @@ pub fn init(cx: &mut MutableAppContext) {
cx.add_async_action(Editor::confirm_rename);
cx.add_async_action(Editor::find_all_references);
cx.set_global(ScrollbarAutoHide(
cx.platform().should_auto_hide_scrollbars(),
));
hover_popover::init(cx);
link_go_to_definition::init(cx);
mouse_context_menu::init(cx);
@ -5949,6 +5956,7 @@ impl Editor {
cx.notify();
}
if cx.default_global::<ScrollbarAutoHide>().0 {
self.hide_scrollbar_task = Some(cx.spawn_weak(|this, mut cx| async move {
Timer::after(SCROLLBAR_SHOW_INTERVAL).await;
if let Some(this) = this.upgrade(&cx) {
@ -5958,6 +5966,9 @@ impl Editor {
});
}
}));
} else {
self.hide_scrollbar_task = None;
}
}
fn on_buffer_changed(&mut self, _: ModelHandle<MultiBuffer>, cx: &mut ViewContext<Self>) {

View file

@ -63,6 +63,7 @@ pub trait Platform: Send + Sync {
fn delete_credentials(&self, url: &str) -> Result<()>;
fn set_cursor_style(&self, style: CursorStyle);
fn should_auto_hide_scrollbars(&self) -> bool;
fn local_timezone(&self) -> UtcOffset;

View file

@ -699,6 +699,16 @@ impl platform::Platform for MacPlatform {
}
}
fn should_auto_hide_scrollbars(&self) -> bool {
#[allow(non_upper_case_globals)]
const NSScrollerStyleOverlay: NSInteger = 1;
unsafe {
let style: NSInteger = msg_send![class!(NSScroller), preferredScrollerStyle];
style == NSScrollerStyleOverlay
}
}
fn local_timezone(&self) -> UtcOffset {
unsafe {
let local_timezone: id = msg_send![class!(NSTimeZone), localTimeZone];

View file

@ -177,6 +177,10 @@ impl super::Platform for Platform {
*self.cursor.lock() = style;
}
fn should_auto_hide_scrollbars(&self) -> bool {
false
}
fn local_timezone(&self) -> UtcOffset {
UtcOffset::UTC
}