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, Next,
} }
#[derive(Default)]
struct ScrollbarAutoHide(bool);
pub fn init(cx: &mut MutableAppContext) { pub fn init(cx: &mut MutableAppContext) {
cx.add_action(Editor::new_file); cx.add_action(Editor::new_file);
cx.add_action(|this: &mut Editor, action: &Scroll, cx| this.set_scroll_position(action.0, cx)); 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::confirm_rename);
cx.add_async_action(Editor::find_all_references); cx.add_async_action(Editor::find_all_references);
cx.set_global(ScrollbarAutoHide(
cx.platform().should_auto_hide_scrollbars(),
));
hover_popover::init(cx); hover_popover::init(cx);
link_go_to_definition::init(cx); link_go_to_definition::init(cx);
mouse_context_menu::init(cx); mouse_context_menu::init(cx);
@ -5949,15 +5956,19 @@ impl Editor {
cx.notify(); cx.notify();
} }
self.hide_scrollbar_task = Some(cx.spawn_weak(|this, mut cx| async move { if cx.default_global::<ScrollbarAutoHide>().0 {
Timer::after(SCROLLBAR_SHOW_INTERVAL).await; self.hide_scrollbar_task = Some(cx.spawn_weak(|this, mut cx| async move {
if let Some(this) = this.upgrade(&cx) { Timer::after(SCROLLBAR_SHOW_INTERVAL).await;
this.update(&mut cx, |this, cx| { if let Some(this) = this.upgrade(&cx) {
this.show_scrollbars = false; this.update(&mut cx, |this, cx| {
cx.notify(); this.show_scrollbars = false;
}); cx.notify();
} });
})); }
}));
} else {
self.hide_scrollbar_task = None;
}
} }
fn on_buffer_changed(&mut self, _: ModelHandle<MultiBuffer>, cx: &mut ViewContext<Self>) { 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 delete_credentials(&self, url: &str) -> Result<()>;
fn set_cursor_style(&self, style: CursorStyle); fn set_cursor_style(&self, style: CursorStyle);
fn should_auto_hide_scrollbars(&self) -> bool;
fn local_timezone(&self) -> UtcOffset; 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 { fn local_timezone(&self) -> UtcOffset {
unsafe { unsafe {
let local_timezone: id = msg_send![class!(NSTimeZone), localTimeZone]; let local_timezone: id = msg_send![class!(NSTimeZone), localTimeZone];

View file

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