Commit 82ed796a91

82ed796a91605c31c231d9f3c52b8cae837106bd

parent: 3296f58859

Unregistered key

Aleš Katona <ales@katona.me> · 2025-05-29 04:48 UTC
committer: <noreply@github.com>

Add support for scrolling w/ mouse when explicitly enabled (#389)

Co-authored-by: Ulyssa <git@ulyssa.dev>

Layout: unified · split

src/config.rs +10
@@ -466,6 +466,12 @@ impl<'de> Deserialize<'de> for NotifyVia {
466466 }
467467}
468468
469#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq)]
470pub struct Mouse {
471 #[serde(default)]
472 pub enabled: bool,
473}
474
469475#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq)]
470476pub struct Notifications {
471477 #[serde(default)]
@@ -562,6 +568,7 @@ pub struct TunableValues {
562568 pub message_user_color: bool,
563569 pub default_room: Option<String>,
564570 pub open_command: Option<Vec<String>>,
571 pub mouse: Mouse,
565572 pub notifications: Notifications,
566573 pub image_preview: Option<ImagePreviewValues>,
567574 pub user_gutter_width: usize,
@@ -586,6 +593,7 @@ pub struct Tunables {
586593 pub message_user_color: Option<bool>,
587594 pub default_room: Option<String>,
588595 pub open_command: Option<Vec<String>>,
596 pub mouse: Option<Mouse>,
589597 pub notifications: Option<Notifications>,
590598 pub image_preview: Option<ImagePreview>,
591599 pub user_gutter_width: Option<usize>,
@@ -614,6 +622,7 @@ impl Tunables {
614622 message_user_color: self.message_user_color.or(other.message_user_color),
615623 default_room: self.default_room.or(other.default_room),
616624 open_command: self.open_command.or(other.open_command),
625 mouse: self.mouse.or(other.mouse),
617626 notifications: self.notifications.or(other.notifications),
618627 image_preview: self.image_preview.or(other.image_preview),
619628 user_gutter_width: self.user_gutter_width.or(other.user_gutter_width),
@@ -640,6 +649,7 @@ impl Tunables {
640649 message_user_color: self.message_user_color.unwrap_or(false),
641650 default_room: self.default_room,
642651 open_command: self.open_command,
652 mouse: self.mouse.unwrap_or_default(),
643653 notifications: self.notifications.unwrap_or_default(),
644654 image_preview: self.image_preview.map(ImagePreview::values),
645655 user_gutter_width: self.user_gutter_width.unwrap_or(30),
src/main.rs +42 −8
@@ -44,11 +44,14 @@ use modalkit::crossterm::{
4444 read,
4545 DisableBracketedPaste,
4646 DisableFocusChange,
47 DisableMouseCapture,
4748 EnableBracketedPaste,
4849 EnableFocusChange,
50 EnableMouseCapture,
4951 Event,
5052 KeyEventKind,
5153 KeyboardEnhancementFlags,
54 MouseEventKind,
5255 PopKeyboardEnhancementFlags,
5356 PushKeyboardEnhancementFlags,
5457 },
@@ -364,8 +367,30 @@ impl Application {
364367
365368 return Ok(ke.into());
366369 },
367 Event::Mouse(_) => {
368 // Do nothing for now.
370 Event::Mouse(me) => {
371 let dir = match me.kind {
372 MouseEventKind::ScrollUp => MoveDir2D::Up,
373 MouseEventKind::ScrollDown => MoveDir2D::Down,
374 MouseEventKind::ScrollLeft => MoveDir2D::Left,
375 MouseEventKind::ScrollRight => MoveDir2D::Right,
376 _ => continue,
377 };
378
379 let size = ScrollSize::Cell;
380 let style = ScrollStyle::Direction2D(dir, size, 1.into());
381 let ctx = ProgramContext::default();
382 let mut store = self.store.lock().await;
383
384 match self.screen.scroll(&style, &ctx, store.deref_mut()) {
385 Ok(None) => {},
386 Ok(Some(info)) => {
387 drop(store);
388 self.handle_info(info);
389 },
390 Err(e) => {
391 self.screen.push_error(e);
392 },
393 }
369394 },
370395 Event::FocusGained => {
371396 let mut store = self.store.lock().await;
@@ -932,8 +957,8 @@ async fn login_normal(
932957}
933958
934959/// Set up the terminal for drawing the TUI, and getting additional info.
935fn setup_tty(title: &str, enable_enhanced_keys: bool) -> std::io::Result<()> {
936 let title = format!("iamb ({})", title);
960fn setup_tty(settings: &ApplicationSettings, enable_enhanced_keys: bool) -> std::io::Result<()> {
961 let title = format!("iamb ({})", settings.profile.user_id.as_str());
937962
938963 // Enable raw mode and enter the alternate screen.
939964 crossterm::terminal::enable_raw_mode()?;
@@ -947,15 +972,23 @@ fn setup_tty(title: &str, enable_enhanced_keys: bool) -> std::io::Result<()> {
947972 )?;
948973 }
949974
975 if settings.tunables.mouse.enabled {
976 crossterm::execute!(stdout(), EnableMouseCapture)?;
977 }
978
950979 crossterm::execute!(stdout(), EnableBracketedPaste, EnableFocusChange, SetTitle(title))
951980}
952981
953982// Do our best to reverse what we did in setup_tty() when we exit or crash.
954fn restore_tty(enable_enhanced_keys: bool) {
983fn restore_tty(enable_enhanced_keys: bool, enable_mouse: bool) {
955984 if enable_enhanced_keys {
956985 let _ = crossterm::queue!(stdout(), PopKeyboardEnhancementFlags);
957986 }
958987
988 if enable_mouse {
989 let _ = crossterm::queue!(stdout(), DisableMouseCapture);
990 }
991
959992 let _ = crossterm::execute!(
960993 stdout(),
961994 DisableBracketedPaste,
@@ -1009,11 +1042,12 @@ async fn run(settings: ApplicationSettings) -> IambResult<()> {
10091042 false
10101043 },
10111044 };
1012 setup_tty(settings.profile.user_id.as_str(), enable_enhanced_keys)?;
1045 setup_tty(&settings, enable_enhanced_keys)?;
10131046
10141047 let orig_hook = std::panic::take_hook();
1048 let enable_mouse = settings.tunables.mouse.enabled;
10151049 std::panic::set_hook(Box::new(move |panic_info| {
1016 restore_tty(enable_enhanced_keys);
1050 restore_tty(enable_enhanced_keys, enable_mouse);
10171051 orig_hook(panic_info);
10181052 process::exit(1);
10191053 }));
@@ -1023,7 +1057,7 @@ async fn run(settings: ApplicationSettings) -> IambResult<()> {
10231057 application.run().await?;
10241058
10251059 // Clean up the terminal on exit.
1026 restore_tty(enable_enhanced_keys);
1060 restore_tty(enable_enhanced_keys, enable_mouse);
10271061
10281062 Ok(())
10291063}
src/tests.rs +1
@@ -189,6 +189,7 @@ pub fn mock_tunables() -> TunableValues {
189189 external_edit_file_suffix: String::from(".md"),
190190 username_display: UserDisplayStyle::Username,
191191 message_user_color: false,
192 mouse: Default::default(),
192193 notifications: Notifications {
193194 enabled: false,
194195 via: NotifyVia::default(),